Debian Codegraph @

lib/packagekit-glib2/pk-package-ids.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2008-2009 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <packagekit-glib2/pk-package-id.h>
26 #include <packagekit-glib2/pk-package-ids.h>
27
28 /**
29 * pk_package_ids_from_id:
30 * @package_id: A single package_id
31 *
32 * Form a composite string array of package_id's from
33 * a single package_id
34 *
35 * Return value: (transfer full): the string array, or %NULL if invalid, free with g_strfreev()
36 *
37 * Since: 0.5.2
38 **/
39 gchar **
40 pk_package_ids_from_id (const gchar *package_id)
41 {
42 g_return_val_if_fail (package_id != NULL, NULL);
43 return g_strsplit (package_id, PK_PACKAGE_IDS_DELIM, 1);
44 }
45
46 /**
47 * pk_package_ids_from_string:
48 * @package_id: A single package_id
49 *
50 * Form a composite string array of package_id's from
51 * a delimited string
52 *
53 * Return value: (transfer full): the string array, or %NULL if invalid, free with g_strfreev()
54 *
55 * Since: 0.5.2
56 **/
57 gchar **
58 pk_package_ids_from_string (const gchar *package_id)
59 {
60 g_return_val_if_fail (package_id != NULL, NULL);
61 return g_strsplit (package_id, PK_PACKAGE_IDS_DELIM, 0);
62 }
63
64 /**
65 * pk_package_ids_check:
66 * @package_ids: a string array of package_id's
67 *
68 * Check the string array of package_id's for validity
69 *
70 * Return value: %TRUE if the package_ids are all valid.
71 *
72 * Since: 0.5.2
73 **/
74 gboolean
75 pk_package_ids_check (gchar **package_ids)
76 {
77 guint i;
78 guint size;
79 gboolean ret = FALSE;
80 const gchar *package_id;
81
82 g_return_val_if_fail (package_ids != NULL, FALSE);
83
84 /* check all */
85 size = g_strv_length (package_ids);
86 for (i = 0; i < size; i++) {
87 package_id = package_ids[i];
88 ret = pk_package_id_check (package_id);
89 if (!ret)
90 goto out;
91 }
92 out:
93 return ret;
94 }
95
96 /**
97 * pk_package_ids_to_string:
98 * @package_ids: a string array of package_id's
99 *
100 * Cats the string array of package_id's into one delimited string
101 *
102 * Return value: a string representation of all the package_id's.
103 *
104 * Since: 0.5.2
105 **/
106 gchar *
107 pk_package_ids_to_string (gchar **package_ids)
108 {
109 /* special case as this is allowed */
110 if (package_ids == NULL)
111 return NULL;
112 return g_strjoinv (PK_PACKAGE_IDS_DELIM, package_ids);
113 }
114
115 /**
116 * pk_package_ids_present_id:
117 * @package_ids: a string array of package_id's
118 * @package_id: a single package_id
119 *
120 * Finds out if a package ID is present in the list.
121 *
122 * Return value: %TRUE if the package ID is present
123 *
124 * Since: 0.5.2
125 **/
126 gboolean
127 pk_package_ids_present_id (gchar **package_ids, const gchar *package_id)
128 {
129 guint i;
130
131 g_return_val_if_fail (package_ids != NULL, FALSE);
132 g_return_val_if_fail (package_id != NULL, FALSE);
133
134 /* iterate */
135 for (i = 0; package_ids[i] != NULL; i++) {
136 if (g_strcmp0 (package_id, package_ids[i]) == 0)
137 return TRUE;
138 }
139 return FALSE;
140 }
141
142 /**
143 * pk_package_ids_add_id:
144 * @package_ids: a string array of package_id's
145 * @package_id: a single package_id
146 *
147 * Adds a package_id to an existing list.
148 *
149 * Return value: (transfer full): the new list, free g_strfreev()
150 *
151 * Since: 0.5.2
152 **/
153 gchar **
154 pk_package_ids_add_id (gchar **package_ids, const gchar *package_id)
155 {
156 guint i;
157 guint len;
158 gchar **result;
159
160 g_return_val_if_fail (package_ids != NULL, NULL);
161 g_return_val_if_fail (package_id != NULL, NULL);
162
163 len = g_strv_length (package_ids);
164 result = g_new0 (gchar *, len+2);
165
166 /* iterate */
167 for (i = 0; package_ids[i] != NULL; i++)
168 result[i] = g_strdup (package_ids[i]);
169 result[i] = g_strdup (package_id);
170 return result;
171 }
172
173 /**
174 * pk_package_ids_add_ids:
175 * @package_ids: a string array of package_id's
176 * @package_ids_new: a string array of package_id's
177 *
178 * Adds a package_id to an existing list.
179 *
180 * Return value: (transfer full): the new list, free g_strfreev()
181 *
182 * Since: 0.5.2
183 **/
184 gchar **
185 pk_package_ids_add_ids (gchar **package_ids, gchar **package_ids_new)
186 {
187 guint i;
188 guint j = 0;
189 guint len;
190 gchar **result;
191
192 g_return_val_if_fail (package_ids != NULL, NULL);
193 g_return_val_if_fail (package_ids_new != NULL, NULL);
194
195 /* get length of both arrays */
196 len = g_strv_length (package_ids) + g_strv_length (package_ids_new);
197 result = g_new0 (gchar *, len+1);
198
199 /* iterate */
200 for (i = 0; package_ids[i] != NULL; i++)
201 result[j++] = g_strdup (package_ids[i]);
202 for (i = 0; package_ids_new[i] != NULL; i++)
203 result[j++] = g_strdup (package_ids_new[i]);
204 return result;
205 }
206
207 /**
208 * pk_package_ids_remove_id:
209 * @package_ids: a string array of package_id's
210 * @package_id: a single package_id
211 *
212 * Removes a package ID from the the list.
213 *
214 * Return value: (transfer full): the new list, free g_strfreev()
215 *
216 * Since: 0.5.2
217 **/
218 gchar **
219 pk_package_ids_remove_id (gchar **package_ids, const gchar *package_id)
220 {
221 guint i;
222 guint j = 0;
223 guint len;
224 gchar **result;
225
226 g_return_val_if_fail (package_ids != NULL, NULL);
227 g_return_val_if_fail (package_id != NULL, NULL);
228
229 len = g_strv_length (package_ids);
230 result = g_new0 (gchar *, len+1);
231
232 /* iterate */
233 for (i = 0; package_ids[i] != NULL; i++) {
234 if (g_strcmp0 (package_id, package_ids[i]) != 0)
235 result[j++] = g_strdup (package_ids[i]);
236 }
237 return result;
238 }