2 * Copyright (c) 2005 Zmanda, Inc. All Rights Reserved.
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 2.1 as
6 * published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11 * License for more details.
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
22 * Utilities that aren't quite included in glib
24 * Author: Dustin J. Mitchell <dustin@zmanda.com>, Ian Turner <ian@zmanda.com>
28 #include "glib-util.h"
29 #include "conffile.h" /* For find_multiplier. */
33 FLAG_STRING_SHORT_NAME,
37 static char ** g_flags_to_strv(int value, GType type, FlagString source);
40 _glib_util_foreach_glue(gpointer data, gpointer func)
42 void (*one_arg_fn)(gpointer) = (void (*)(gpointer))func;
46 GValue* g_value_unset_init(GValue* value, GType type) {
47 g_return_val_if_fail(value != NULL, NULL);
49 if (G_IS_VALUE(value)) {
52 g_value_init(value, type);
56 GValue* g_value_unset_copy(const GValue * from, GValue * to) {
57 g_return_val_if_fail(from != NULL, NULL);
58 g_return_val_if_fail(to != NULL, NULL);
60 g_value_unset_init(to, G_VALUE_TYPE(from));
61 g_value_copy(from, to);
65 void g_list_free_full(GList * list) {
69 gpointer data = cur->data;
71 cur = g_list_next(cur);
77 void g_slist_free_full(GSList * list) {
81 gpointer data = cur->data;
83 cur = g_slist_next(cur);
89 void g_queue_free_full(GQueue * queue) {
90 while (!g_queue_is_empty(queue)) {
92 data = g_queue_pop_head(queue);
98 void g_ptr_array_free_full(GPtrArray * array) {
101 for (i = 0; i < array->len; i ++) {
102 amfree(g_ptr_array_index(array, i));
104 g_ptr_array_free(array, TRUE);
107 gboolean g_value_compare(GValue * a, GValue * b) {
108 if (a == NULL && b == NULL)
110 if (a == NULL || b == NULL)
112 if (G_VALUE_TYPE(a) != G_VALUE_TYPE(b))
114 if (g_value_fits_pointer(a) && g_value_fits_pointer(b)) {
115 return g_value_peek_pointer(a) == g_value_peek_pointer(b);
117 /* Since there is no builtin comparison function, we resort to
118 comparing serialized strings. Yuck. */
122 a_str = g_strdup_value_contents(a);
123 b_str = g_strdup_value_contents(b);
124 rval = (0 == strcmp(a_str, b_str));
130 g_assert_not_reached();
133 static gboolean g_value_set_boolean_from_string(GValue * val, char * string) {
134 if (strcasecmp(string, "true") == 0 ||
135 strcasecmp(string, "yes") == 0 ||
136 strcmp(string, "1") == 0) {
137 g_value_set_boolean(val, TRUE);
138 } else if (strcasecmp(string, "false") == 0 ||
139 strcasecmp(string, "no") == 0 ||
140 strcmp(string, "0") == 0) {
141 g_value_set_boolean(val, FALSE);
149 static gboolean g_value_set_int_from_string(GValue * val, char * string) {
150 long int strto_result;
153 strto_result = strtol(string, &strto_end, 0);
154 multiplier = find_multiplier(strto_end);
155 if (multiplier == G_MAXINT64) {
156 if (strto_result >= 0) {
157 g_value_set_int(val, G_MAXINT);
159 g_value_set_int(val, G_MININT);
162 } else if (*string == '\0' || multiplier == 0
163 || strto_result < G_MININT / multiplier
164 || strto_result > G_MAXINT / multiplier) {
167 g_value_set_int(val, (int)(strto_result * multiplier));
172 static gboolean g_value_set_uint_from_string(GValue * val, char * string) {
173 unsigned long int strto_result;
176 strto_result = strtoul(string, &strto_end, 0);
177 multiplier = find_multiplier(strto_end); /* casts */
178 if (multiplier == G_MAXINT64) {
179 g_value_set_uint(val, G_MAXUINT);
181 } else if (multiplier == 0 || *string == '\0' ||
182 strto_result > G_MAXUINT / multiplier) {
185 g_value_set_uint(val, (guint)(strto_result * multiplier));
190 static gboolean g_value_set_uint64_from_string(GValue * val, char * string) {
191 unsigned long long int strto_result;
194 strto_result = strtoull(string, &strto_end, 0);
195 multiplier = find_multiplier(strto_end); /* casts */
196 if (multiplier == G_MAXINT64) {
197 g_value_set_uint64(val, G_MAXUINT64);
199 } else if (multiplier == 0 || *string == '\0' ||
200 strto_result > G_MAXUINT64 / multiplier) {
203 g_value_set_uint64(val, (guint64)(strto_result * multiplier));
208 /* Flags can contain multiple values. We assume here that values are like
209 * C identifiers (that is, they match /[A-Za-z_][A-Za-z0-9_]+/), although
210 * that doesn't seem to be a requirement of GLib. With that assumption in
211 * mind, we look for the format "FLAG_1 | FLAG_2 | ... | FLAG_N". */
212 static gboolean g_value_set_flags_from_string(GValue * val, char * string) {
214 char * strtok_saveptr;
216 char * strtok_first_arg;
217 const char delim[] = " \t,|";
218 GFlagsClass * flags_class;
220 flags_class = (GFlagsClass*) g_type_class_ref(G_VALUE_TYPE(val));
221 g_return_val_if_fail(flags_class != NULL, FALSE);
222 g_return_val_if_fail(G_IS_FLAGS_CLASS(flags_class), FALSE);
224 /* Don't let strtok stop on original. */
225 strtok_first_arg = string_copy = strdup(string);
228 GFlagsValue * flag_value;
229 char * token = strtok_r(strtok_first_arg, delim, &strtok_saveptr);
230 strtok_first_arg = NULL;
236 flag_value = g_flags_get_value_by_name(flags_class, token);
237 if (flag_value == NULL) {
238 flag_value = g_flags_get_value_by_nick(flags_class, token);
240 if (flag_value == NULL) {
241 g_fprintf(stderr, _("Invalid flag %s for type %s\n"), token,
242 g_type_name(G_VALUE_TYPE(val)));
246 value |= flag_value->value;
252 g_fprintf(stderr, _("No valid flags for type %s in string %s\n"),
253 g_type_name(G_VALUE_TYPE(val)), string);
257 g_value_set_flags(val, value);
262 /* This function really ought not to be part of Amanda. In my (Ian's) opinion,
263 serialization and deserialization should be a part of the GValue
264 interface. But it's not, and here we are. */
265 gboolean g_value_set_from_string(GValue * val, char * string) {
266 g_return_val_if_fail(val != NULL, FALSE);
267 g_return_val_if_fail(G_IS_VALUE(val), FALSE);
269 if (G_VALUE_HOLDS_BOOLEAN(val)) {
270 return g_value_set_boolean_from_string(val, string);
271 } else if (G_VALUE_HOLDS_INT(val)) {
272 return g_value_set_int_from_string(val, string);
273 } else if (G_VALUE_HOLDS_UINT(val)) {
274 return g_value_set_uint_from_string(val, string);
275 } else if (G_VALUE_HOLDS_UINT64(val)) {
276 return g_value_set_uint64_from_string(val, string);
277 } else if (G_VALUE_HOLDS_STRING(val)) {
278 g_value_set_string(val, string);
280 } else if (G_VALUE_HOLDS_FLAGS(val)) {
281 return g_value_set_flags_from_string(val, string);
292 return strcmp((char *)a, (char *)b);
295 char * g_strjoinv_and_free(char ** strv, const char * seperator) {
296 char * rval = g_strjoinv(seperator, strv);
301 char ** g_flags_name_to_strv(int value, GType type) {
302 return g_flags_to_strv(value, type, FLAG_STRING_NAME);
305 char ** g_flags_short_name_to_strv(int value, GType type) {
306 return g_flags_to_strv(value, type, FLAG_STRING_SHORT_NAME);
309 char ** g_flags_nick_to_strv(int value, GType type) {
310 return g_flags_to_strv(value, type, FLAG_STRING_NICK);
313 static char * get_name_from_value(GFlagsValue * value, FlagString source) {
315 case FLAG_STRING_NAME:
316 case FLAG_STRING_SHORT_NAME:
317 return strdup(value->value_name);
318 case FLAG_STRING_NICK:
319 return strdup(value->value_nick);
325 /* If freed and notfreed have a common prefix that is different from freed,
326 then return that and free freed. Otherwise, return freed. */
327 static char * find_common_prefix(char * freed, const char * notfreed) {
328 char * freed_ptr = freed;
329 const char * notfreed_ptr = notfreed;
332 if (notfreed == NULL) {
335 return strdup(notfreed);
337 } else if (notfreed == NULL) {
342 while (*freed_ptr == *notfreed_ptr) {
351 static char ** g_flags_to_strv(int value, GType type,
354 GFlagsValue * flagsvalue;
355 char * common_prefix = NULL;
356 int common_prefix_len;
359 g_return_val_if_fail(G_TYPE_IS_FLAGS(type), NULL);
360 g_return_val_if_fail((class = g_type_class_ref(type)) != NULL, NULL);
361 g_return_val_if_fail(G_IS_FLAGS_CLASS(class), NULL);
363 rval = g_ptr_array_new();
364 for (flagsvalue = class->values;
365 flagsvalue->value_name != NULL;
367 if (source == FLAG_STRING_SHORT_NAME) {
368 common_prefix = find_common_prefix(common_prefix,
369 flagsvalue->value_name);
372 if ((flagsvalue->value == 0 && value == 0) ||
373 (flagsvalue->value != 0 && (value & flagsvalue->value))) {
374 g_ptr_array_add(rval, get_name_from_value(flagsvalue, source));
378 if (source == FLAG_STRING_SHORT_NAME && common_prefix != NULL &&
379 ((common_prefix_len = strlen(common_prefix))) > 0) {
383 for (i = 0; i < rval->len; i ++) {
384 old = g_ptr_array_index(rval, i);
385 new = strdup(old + common_prefix_len);
386 g_ptr_array_index(rval, i) = new;
391 g_ptr_array_add(rval, NULL);
393 amfree(common_prefix);
394 return (char**)g_ptr_array_free(rval, FALSE);
397 char * g_english_strjoinv(char ** strv, const char * conjunction) {
402 strv = g_strdupv(strv);
404 length = g_strv_length(strv);
405 last = strv[length - 1];
406 strv[length - 1] = NULL;
408 joined = g_strjoinv(", ", strv);
409 rval = g_strdup_printf("%s, %s %s", joined, conjunction, last);
417 char * g_english_strjoinv_and_free(char ** strv, const char * conjunction) {
418 char * rval = g_english_strjoinv(strv, conjunction);
423 #if !(GLIB_CHECK_VERSION(2,6,0))
424 guint g_strv_length(gchar ** strv) {
427 if (G_UNLIKELY(strv == NULL))
430 while (*strv != NULL) {
437 #endif /* GLIB_CHECK_VERSION(2.6.0) */