Imported Upstream version 3.2.0
[debian/amanda] / common-src / glib-util.c
index dd6b38e0e12397d01856a4be924171eb267cbf74..7ba6702259e4040867a5ff661d83c008610f3333 100644 (file)
@@ -1,21 +1,21 @@
 /*
- * Copyright (c) 2005-2008 Zmanda Inc.  All Rights Reserved.
- * 
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License version 2.1 as 
- * published by the Free Software Foundation.
- * 
- * This library is distributed in the hope that it will be useful, but
+ * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
- * License for more details.
- * 
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
- * 
- * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
- * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+ *
+ * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
+ * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
  */
 
 /*
@@ -38,26 +38,15 @@ glib_init(void) {
     if (did_glib_init) return;
     did_glib_init = TRUE;
 
-    /* Initialize glib's type system */
-    g_type_init();
-
     /* set up libcurl (this must happen before threading 
      * is initialized) */
 #ifdef HAVE_LIBCURL
 # ifdef G_THREADS_ENABLED
-    g_assert(!g_thread_supported());
+    g_assert(!g_thread_supported()); /* assert threads aren't initialized yet */
 # endif
     g_assert(curl_global_init(CURL_GLOBAL_ALL) == 0);
 #endif
 
-    /* And set up glib's threads */
-#if defined(G_THREADS_ENABLED) && !defined(G_THREADS_IMPL_NONE)
-    if (g_thread_supported()) {
-        return;
-    }
-    g_thread_init(NULL);
-#endif
-
     /* do a version check */
 #if GLIB_CHECK_VERSION(2,6,0)
     {
@@ -71,6 +60,16 @@ glib_init(void) {
        }
     }
 #endif
+
+    /* Initialize glib's type system.  On glib >= 2.24, this will initialize
+     * threads, so it must be done after curl is initialized. */
+    g_type_init();
+
+    /* And set up glib's threads */
+#if defined(G_THREADS_ENABLED) && !defined(G_THREADS_IMPL_NONE)
+    if (!g_thread_supported())
+       g_thread_init(NULL);
+#endif
 }
 
 typedef enum {
@@ -131,6 +130,10 @@ void g_slist_free_full(GSList * list) {
     g_slist_free(list);
 }
 
+void g_slist_free_full_gpointer(gpointer list) {
+    g_slist_free_full((GSList *)list);
+}
+
 void g_queue_free_full(GQueue * queue) {
     while (!g_queue_is_empty(queue)) {
         gpointer data;
@@ -175,19 +178,17 @@ gboolean g_value_compare(GValue * a, GValue * b) {
     g_assert_not_reached();
 }
 
-static gboolean g_value_set_boolean_from_string(GValue * val, char * string) {
-    if (strcasecmp(string, "true") == 0 ||
-        strcasecmp(string, "yes") == 0 ||
-        strcmp(string, "1") == 0) {
-        g_value_set_boolean(val, TRUE);
-    } else if (strcasecmp(string, "false") == 0 ||
-               strcasecmp(string, "no") == 0 ||
-               strcmp(string, "0") == 0) {
-        g_value_set_boolean(val, FALSE);
-    } else {
-        return FALSE;
-    }
+static gboolean
+g_value_set_boolean_from_string(
+    GValue * val,
+    char * str)
+{
+    int b = string_to_boolean(str);
 
+    if (b == -1)
+       return FALSE;
+
+    g_value_set_boolean(val, b);
     return TRUE;
 }
 
@@ -500,25 +501,36 @@ g_ptr_array_foreach (GPtrArray *array,
 #endif
 
 guint
-g_str_case_hash(
+g_str_amanda_hash(
        gconstpointer key)
 {
     /* modified version of glib's hash function, copyright
      * GLib Team and others 1997-2000. */
-    const char *p = key;
-    guint h = g_ascii_toupper(*p);
+    const char *p;
+    guint h = 0;
 
-    if (h)
-       for (p += 1; *p != '\0'; p++)
-           h = (h << 5) - h + g_ascii_toupper(*p);
+    for (p = key; *p != '\0'; p++)
+        h = (h << 5) - h + (('_' == *p) ? '-' : g_ascii_tolower(*p));
 
     return h;
 }
 
 gboolean
-g_str_case_equal(
+g_str_amanda_equal(
        gconstpointer v1,
        gconstpointer v2)
 {
-    return (0 == g_ascii_strcasecmp((char *)v1, (char *)v2));
+    const gchar *p1 = v1, *p2 = v2;
+    while (*p1) {
+        /* letting '-' == '_' */
+        if (!('-' == *p1 || '_' == *p1) || !('-' == *p2 || '_' == *p2))
+            if (g_ascii_tolower(*p1) != g_ascii_tolower(*p2))
+                return FALSE;
+
+        p1++;
+        p2++;
+    }
+
+    /* p1 is at '\0' is p2 too? */
+    return *p2? FALSE : TRUE;
 }