XBPS Library API  0.19
The X Binary Package System
plist_remove.c
1 /*-
2  * Copyright (c) 2008-2012 Juan Romero Pardines.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 
32 #include "xbps_api_impl.h"
33 
34 static bool
35 remove_obj_from_array(prop_array_t array, const char *str, int mode)
36 {
37  prop_object_iterator_t iter;
38  prop_object_t obj;
39  const char *curname, *pkgdep;
40  char *curpkgname;
41  size_t idx = 0;
42  bool found = false;
43 
44  assert(prop_object_type(array) == PROP_TYPE_ARRAY);
45 
46  iter = prop_array_iterator(array);
47  if (iter == NULL)
48  return false;
49 
50  while ((obj = prop_object_iterator_next(iter))) {
51  if (mode == 0) {
52  /* exact match, obj is a string */
53  if (prop_string_equals_cstring(obj, str)) {
54  found = true;
55  break;
56  }
57  } else if (mode == 1) {
58  /* match by pkgname, obj is a string */
59  pkgdep = prop_string_cstring_nocopy(obj);
60  curpkgname = xbps_pkg_name(pkgdep);
61  if (curpkgname == NULL)
62  break;
63  if (strcmp(curpkgname, str) == 0) {
64  free(curpkgname);
65  found = true;
66  break;
67  }
68  free(curpkgname);
69  } else if (mode == 2) {
70  /* match by pkgname, obj is a dictionary */
71  prop_dictionary_get_cstring_nocopy(obj,
72  "pkgname", &curname);
73  if (strcmp(curname, str) == 0) {
74  found = true;
75  break;
76  }
77  } else if (mode == 3) {
78  /* match by pkgver, obj is a dictionary */
79  prop_dictionary_get_cstring_nocopy(obj,
80  "pkgver", &curname);
81  if (strcmp(curname, str) == 0) {
82  found = true;
83  break;
84  }
85  } else if (mode == 4) {
86  /* match by pattern, obj is a dictionary */
87  prop_dictionary_get_cstring_nocopy(obj,
88  "pkgver", &curname);
89  if (xbps_pkgpattern_match(curname, str)) {
90  found = true;
91  break;
92  }
93  }
94  idx++;
95  }
96  prop_object_iterator_release(iter);
97 
98  if (!found) {
99  errno = ENOENT;
100  return false;
101  }
102 
103  prop_array_remove(array, idx);
104  return true;
105 }
106 
107 bool HIDDEN
108 xbps_remove_string_from_array(prop_array_t array, const char *str)
109 {
110  return remove_obj_from_array(array, str, 0);
111 }
112 
113 bool HIDDEN
114 xbps_remove_pkgname_from_array(prop_array_t array, const char *str)
115 {
116  return remove_obj_from_array(array, str, 1);
117 }
118 
119 bool HIDDEN
120 xbps_remove_pkg_from_array_by_name(prop_array_t array, const char *str)
121 {
122  return remove_obj_from_array(array, str, 2);
123 }
124 
125 bool HIDDEN
126 xbps_remove_pkg_from_array_by_pkgver(prop_array_t array, const char *str)
127 {
128  return remove_obj_from_array(array, str, 3);
129 }
130 
131 bool HIDDEN
132 xbps_remove_pkg_from_array_by_pattern(prop_array_t array, const char *str)
133 {
134  return remove_obj_from_array(array, str, 4);
135 }