Debian Codegraph @

lib/packagekit-glib2/pk-bitfield.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2007-2008 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 /**
23 * SECTION:pk-bitfield
24 * @title: PkBitfield
25 * @short_description: Bitfield object
26 *
27 * #PkBitfield provides a method of using enumerations in a bitfield.
28 */
29
30 #include "config.h"
31
32 #include <glib.h>
33 #include <packagekit-glib2/pk-enum.h>
34 #include <packagekit-glib2/pk-bitfield.h>
35
36 /**
37 * pk_bitfield_contain_priority:
38 * @values: a valid bitfield instance
39 * @value: the first value we are searching for
40 * @...: additional values to search for, terminated by -1
41 *
42 * Finds elements in a list, but with priority going to the preceeding entry
43 *
44 * Return value: The return enumerated type, or -1 if none are found
45 *
46 * Since: 0.5.2
47 **/
48 gint
49 pk_bitfield_contain_priority (PkBitfield values, gint value, ...)
50 {
51 va_list args;
52 gint value_temp;
53 gint retval = -1;
54
55 /* we must query at least one thing */
56 if (pk_bitfield_contain (values, value))
57 return value;
58
59 /* process the valist */
60 va_start (args, value);
61 while (TRUE) {
62 value_temp = va_arg (args, gint);
63 /* do we have this one? */
64 if (pk_bitfield_contain (values, value_temp)) {
65 retval = value_temp;
66 break;
67 }
68 /* end of the list */
69 if (value_temp == -1)
70 break;
71 }
72 va_end (args);
73
74 return retval;
75 }
76
77 /**
78 * pk_bitfield_from_enums:
79 * @value: the first value we want to add to the bitfield
80 * @...: other values to add, terminated by -1
81 *
82 * Create a bitfield with the suppied values set.
83 *
84 * Return value: a #PkBitfield, or 0 if invalid
85 *
86 * Since: 0.5.2
87 **/
88 PkBitfield
89 pk_bitfield_from_enums (gint value, ...)
90 {
91 va_list args;
92 gint value_temp;
93 PkBitfield values;
94
95 /* we must query at least one thing */
96 values = pk_bitfield_value (value);
97
98 /* process the valist */
99 va_start (args, value);
100 while (TRUE) {
101 value_temp = va_arg (args, gint);
102 if (value_temp == -1)
103 break;
104 values += pk_bitfield_value (value_temp);
105 }
106 va_end (args);
107
108 return values;
109 }
110
111 /**
112 * pk_role_bitfield_to_string:
113 * @roles: The enumerated type values
114 *
115 * Converts a enumerated type bitfield to its text representation
116 *
117 * Return value: the enumerated constant value, e.g. "install-file;update-system"
118 *
119 * Since: 0.5.2
120 **/
121 gchar *
122 pk_role_bitfield_to_string (PkBitfield roles)
123 {
124 GString *string;
125 guint i;
126
127 string = g_string_new ("");
128 for (i = 0; i < PK_ROLE_ENUM_LAST; i++) {
129 if ((roles & pk_bitfield_value (i)) == 0)
130 continue;
131 g_string_append_printf (string, "%s;", pk_role_enum_to_string (i));
132 }
133 /* do we have a no bitfield? \n */
134 if (string->len == 0) {
135 g_warning ("not valid!");
136 g_string_append (string, pk_role_enum_to_string (PK_ROLE_ENUM_UNKNOWN));
137 } else {
138 /* remove last \n */
139 g_string_set_size (string, string->len - 1);
140 }
141 return g_string_free (string, FALSE);
142 }
143
144 /**
145 * pk_role_bitfield_from_string:
146 * @roles: the enumerated constant value, e.g. "search-file;update-system"
147 *
148 * Converts text representation to its enumerated type bitfield
149 *
150 * Return value: The enumerated type values, or 0 for invalid
151 *
152 * Since: 0.5.2
153 **/
154 PkBitfield
155 pk_role_bitfield_from_string (const gchar *roles)
156 {
157 PkBitfield roles_enum = 0;
158 guint length;
159 guint i;
160 PkRoleEnum role;
161 g_auto(GStrv) split = NULL;
162
163 split = g_strsplit (roles, ";", 0);
164 if (split == NULL) {
165 g_warning ("unable to split");
166 return 0;
167 }
168
169 length = g_strv_length (split);
170 for (i = 0; i < length; i++) {
171 role = pk_role_enum_from_string (split[i]);
172 if (role != PK_ROLE_ENUM_UNKNOWN)
173 roles_enum += pk_bitfield_value (role);
174 }
175 return roles_enum;
176 }
177
178 /**
179 * pk_group_bitfield_to_string:
180 * @groups: The enumerated type values
181 *
182 * Converts a enumerated type bitfield to its text representation
183 *
184 * Return value: the enumerated constant value, e.g. "gnome;kde"
185 *
186 * Since: 0.5.2
187 **/
188 gchar *
189 pk_group_bitfield_to_string (PkBitfield groups)
190 {
191 GString *string;
192 guint i;
193
194 string = g_string_new ("");
195 for (i = 0; i < PK_GROUP_ENUM_LAST; i++) {
196 if ((groups & pk_bitfield_value (i)) == 0)
197 continue;
198 g_string_append_printf (string, "%s;", pk_group_enum_to_string (i));
199 }
200 /* do we have a no bitfield? \n */
201 if (string->len == 0) {
202 g_warning ("not valid!");
203 g_string_append (string, pk_group_enum_to_string (PK_GROUP_ENUM_UNKNOWN));
204 } else {
205 /* remove last \n */
206 g_string_set_size (string, string->len - 1);
207 }
208 return g_string_free (string, FALSE);
209 }
210
211 /**
212 * pk_group_bitfield_from_string:
213 * @groups: the enumerated constant value, e.g. "available;~gui"
214 *
215 * Converts text representation to its enumerated type bitfield
216 *
217 * Return value: The enumerated type values, or 0 for invalid
218 *
219 * Since: 0.5.2
220 **/
221 PkBitfield
222 pk_group_bitfield_from_string (const gchar *groups)
223 {
224 PkBitfield groups_enum = 0;
225 guint length;
226 guint i;
227 PkGroupEnum group;
228 g_auto(GStrv) split = NULL;
229
230 split = g_strsplit (groups, ";", 0);
231 if (split == NULL) {
232 g_warning ("unable to split");
233 return 0;
234 }
235
236 length = g_strv_length (split);
237 for (i = 0; i < length; i++) {
238 group = pk_group_enum_from_string (split[i]);
239 if (group != PK_GROUP_ENUM_UNKNOWN)
240 groups_enum += pk_bitfield_value (group);
241 }
242 return groups_enum;
243 }
244
245 /**
246 * pk_filter_bitfield_to_string:
247 * @filters: The enumerated type values
248 *
249 * Converts a enumerated type bitfield to its text representation
250 *
251 * Return value: the enumerated constant value, e.g. "available;~gui"
252 *
253 * Since: 0.5.2
254 **/
255 gchar *
256 pk_filter_bitfield_to_string (PkBitfield filters)
257 {
258 GString *string;
259 guint i;
260
261 /* shortcut */
262 if (filters == 0)
263 return g_strdup (pk_filter_enum_to_string (PK_FILTER_ENUM_NONE));
264
265 string = g_string_new ("");
266 for (i = 0; i < PK_FILTER_ENUM_LAST; i++) {
267 if ((filters & pk_bitfield_value (i)) == 0)
268 continue;
269 g_string_append_printf (string, "%s;", pk_filter_enum_to_string (i));
270 }
271 /* do we have a 'none' filter? \n */
272 if (string->len == 0) {
273 g_warning ("not valid!");
274 g_string_append (string, pk_filter_enum_to_string (PK_FILTER_ENUM_NONE));
275 } else {
276 /* remove last \n */
277 g_string_set_size (string, string->len - 1);
278 }
279 return g_string_free (string, FALSE);
280 }
281
282 /**
283 * pk_filter_bitfield_from_string:
284 * @filters: the enumerated constant value, e.g. "available;~gui"
285 *
286 * Converts text representation to its enumerated type bitfield, or 0 for invalid
287 *
288 * Return value: The enumerated type values
289 *
290 * Since: 0.5.2
291 **/
292 PkBitfield
293 pk_filter_bitfield_from_string (const gchar *filters)
294 {
295 PkBitfield filters_enum = 0;
296 guint length;
297 guint i;
298 PkFilterEnum filter;
299 g_auto(GStrv) split = NULL;
300
301 split = g_strsplit (filters, ";", 0);
302 if (split == NULL) {
303 g_warning ("unable to split");
304 return 0;
305 }
306
307 length = g_strv_length (split);
308 for (i = 0; i < length; i++) {
309 filter = pk_filter_enum_from_string (split[i]);
310 if (filter != PK_FILTER_ENUM_UNKNOWN)
311 filters_enum += pk_bitfield_value (filter);
312 }
313 return filters_enum;
314 }
315
316 /**
317 * pk_transaction_flag_bitfield_to_string:
318 * @transaction_flags: The enumerated type values
319 *
320 * Converts a enumerated type bitfield to its text representation
321 *
322 * Return value: the enumerated constant value, e.g. "only-trusted;simulate"
323 *
324 * Since: 0.8.1
325 **/
326 gchar *
327 pk_transaction_flag_bitfield_to_string (PkBitfield transaction_flags)
328 {
329 GString *string;
330 guint i;
331
332 /* shortcut */
333 if (transaction_flags == 0)
334 return g_strdup (pk_transaction_flag_enum_to_string (PK_TRANSACTION_FLAG_ENUM_NONE));
335
336 string = g_string_new ("");
337 for (i = 0; i < PK_TRANSACTION_FLAG_ENUM_LAST; i++) {
338 if ((transaction_flags & pk_bitfield_value (i)) == 0)
339 continue;
340 g_string_append_printf (string, "%s;", pk_transaction_flag_enum_to_string (i));
341 }
342 /* do we have a 'none' transaction_flag? \n */
343 if (string->len == 0) {
344 g_warning ("not valid!");
345 g_string_append (string, pk_transaction_flag_enum_to_string (PK_TRANSACTION_FLAG_ENUM_NONE));
346 } else {
347 /* remove last \n */
348 g_string_set_size (string, string->len - 1);
349 }
350 return g_string_free (string, FALSE);
351 }
352
353 /**
354 * pk_transaction_flag_bitfield_from_string:
355 * @transaction_flags: the enumerated constant value, e.g. "only-trusted;simulate"
356 *
357 * Converts text representation to its enumerated type bitfield, or 0 for invalid
358 *
359 * Return value: The enumerated type values
360 *
361 * Since: 0.8.1
362 **/
363 PkBitfield
364 pk_transaction_flag_bitfield_from_string (const gchar *transaction_flags)
365 {
366 PkBitfield transaction_flags_enum = 0;
367 guint length;
368 guint i;
369 PkTransactionFlagEnum transaction_flag;
370 g_auto(GStrv) split = NULL;
371
372 split = g_strsplit (transaction_flags, ";", 0);
373 if (split == NULL) {
374 g_warning ("unable to split");
375 return 0;
376 }
377
378 length = g_strv_length (split);
379 for (i = 0; i < length; i++) {
380 transaction_flag = pk_transaction_flag_enum_from_string (split[i]);
381 transaction_flags_enum += pk_bitfield_value (transaction_flag);
382 }
383 return transaction_flags_enum;
384 }