XBPS Library API 20240111
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
34static bool
35remove_obj_from_array(xbps_array_t array, const char *str, int mode)
36{
37 xbps_object_iterator_t iter;
38 xbps_object_t obj;
39 const char *curname, *pkgdep;
40 char curpkgname[XBPS_NAME_SIZE];
41 unsigned int idx = 0;
42 bool found = false;
43
44 assert(xbps_object_type(array) == XBPS_TYPE_ARRAY);
45
46 iter = xbps_array_iterator(array);
47 if (iter == NULL)
48 return false;
49
50 while ((obj = xbps_object_iterator_next(iter))) {
51 if (mode == 0) {
52 /* exact match, obj is a string */
53 if (xbps_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 = xbps_string_cstring_nocopy(obj);
60 if (!xbps_pkg_name(curpkgname, sizeof(curpkgname), pkgdep))
61 break;
62
63 if (strcmp(curpkgname, str) == 0) {
64 found = true;
65 break;
66 }
67 } else if (mode == 2) {
68 /* match by pkgname, obj is a dictionary */
69 xbps_dictionary_get_cstring_nocopy(obj,
70 "pkgname", &curname);
71 if (strcmp(curname, str) == 0) {
72 found = true;
73 break;
74 }
75 } else if (mode == 3) {
76 /* match by pkgver, obj is a dictionary */
77 xbps_dictionary_get_cstring_nocopy(obj,
78 "pkgver", &curname);
79 if (strcmp(curname, str) == 0) {
80 found = true;
81 break;
82 }
83 } else if (mode == 4) {
84 /* match by pattern, obj is a dictionary */
85 xbps_dictionary_get_cstring_nocopy(obj,
86 "pkgver", &curname);
87 if (xbps_pkgpattern_match(curname, str)) {
88 found = true;
89 break;
90 }
91 }
92 idx++;
93 }
94 xbps_object_iterator_release(iter);
95
96 if (!found) {
97 errno = ENOENT;
98 return false;
99 }
100
101 xbps_array_remove(array, idx);
102 return true;
103}
104
105bool
106xbps_remove_string_from_array(xbps_array_t array, const char *str)
107{
108 return remove_obj_from_array(array, str, 0);
109}
110
111bool
112xbps_remove_pkgname_from_array(xbps_array_t array, const char *str)
113{
114 return remove_obj_from_array(array, str, 1);
115}
116
117bool HIDDEN
118xbps_remove_pkg_from_array_by_name(xbps_array_t array, const char *str)
119{
120 return remove_obj_from_array(array, str, 2);
121}
122
123bool HIDDEN
124xbps_remove_pkg_from_array_by_pkgver(xbps_array_t array, const char *str)
125{
126 return remove_obj_from_array(array, str, 3);
127}
128
129bool HIDDEN
130xbps_remove_pkg_from_array_by_pattern(xbps_array_t array, const char *str)
131{
132 return remove_obj_from_array(array, str, 4);
133}
bool xbps_remove_pkgname_from_array(xbps_array_t array, const char *str)
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:253
bool xbps_remove_string_from_array(xbps_array_t array, const char *str)
int xbps_pkgpattern_match(const char *pkgver, const char *pattern)
Definition util.c:530