Imported Upstream version 3.3.3
[debian/amanda] / common-src / glib-util.h
1 /*
2  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21 /*
22  * Utilities that aren't quite included in glib
23  *
24  * Author: Dustin J. Mitchell <dustin@zmanda.com>, Ian Turner <ian@zmanda.com>
25  */
26
27 #ifndef GLIB_UTIL_H
28 #define GLIB_UTIL_H
29
30 #include <glib.h>
31 #include <glib-object.h>
32
33 /* Call the requisite glib init functions, including calling
34  * g_init_types and setting up threading support.  This function can
35  * be called multiple times with no harm, although it is not
36  * re-entrant.
37  */
38 void glib_init(void);
39
40 /* like g_[s]list_foreach, but with a function taking only
41  * one argument.
42  */
43 #define g_list_foreach_nodata(list, func)                               \
44     g_list_foreach((list), _glib_util_foreach_glue, (gpointer)(func));
45 #define g_slist_foreach_nodata(list, func)                              \
46     g_slist_foreach((list), _glib_util_foreach_glue, (gpointer)(func));
47 void _glib_util_foreach_glue(gpointer data, gpointer func);
48
49 /* This function takes a GValue, which may be zero-filled or
50  * initialized. In either case, this function causes the GValue to be
51  * initialized with the given type. Note that this function lacks the
52  * safety of the standard g_value_ functions; it assumes that the
53  * passed value is zeroed or valid.
54  *
55  * Returns its first argument.*/
56 GValue* g_value_unset_init(GValue* val, GType type);
57
58 /* This does the same thing but also copies the contents of one value
59  * into another. Note that this function lacks the safety of the
60  * standard g_value_ functions; it assumes that the passed value is
61  * zeroed or valid.
62  *
63  * Returns its second (reset) argument.*/
64 GValue* g_value_unset_copy(const GValue* from, GValue * to);
65
66 /* This function is available in glib-2.28.0 and higher; for lower versions
67  * we build our own version with a different name */
68 #if (GLIB_MAJOR_VERSION < 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 28))
69 void slist_free_full(GSList * list, GDestroyNotify free_fn);
70 #else
71 #define slist_free_full(list, free_fn) g_slist_free_full((list), (free_fn))
72 #endif
73
74 /* These functions all take a GLib container, and call free() on all the
75  * pointers in the container before free()ing the container itself. */
76 void g_ptr_array_free_full(GPtrArray * array);
77
78 /* g_value_compare() does what you expect. It returns TRUE if and
79    only if the two values have the same type and the same value. Note
80    that it will return FALSE if the same value is stored with two
81    different types: For example, a GValue with a UCHAR of 1 and a
82    GValue with a CHAR of 1 will be considered inequal. Also, this is a
83    'shallow' comparison; pointers to distinct but equivalent objects
84    are considered inequal. */
85 gboolean g_value_compare(GValue * a, GValue * b);
86
87 /* Given a string and a GValue, parse the string and store it in the
88    GValue. The GValue should be pre-initalized to whatever type you want
89    parsed. */
90 gboolean g_value_set_from_string(GValue * val, char * string);
91
92 /* A GCompareFunc that will sort strings alphabetically (using strcmp) */
93 gint g_compare_strings(gconstpointer a, gconstpointer b);
94
95 /* These functions all take a Flags class and stringify it. They
96  * return a NULL-terminated array of strings that can be
97  * passed to g_strjoinv(), g_strfreev(), g_strdupv(), and
98  * g_strv_length(). Example output looks like:
99  * - g_flags_name_to_strv() -> "MEDIA_ACCESS_MODE_READ_ONLY"
100  * - g_flags_short_name_to_strv() -> "READ_ONLY"
101  * - g_flags_nick_to_strv() -> "read-only"
102  */
103
104 char ** g_flags_name_to_strv(int value, GType type);
105 char ** g_flags_short_name_to_strv(int value, GType type);
106 char ** g_flags_nick_to_strv(int value, GType type);
107
108 /* Just like g_strjoinv, but frees the array as well. */
109 char * g_strjoinv_and_free(char ** strv, const char * seperator);
110
111 /* Just like g_strjoinv, but joins like an English list. The string would
112  * usually be "and" or "or". */
113 char * g_english_strjoinv(char ** strv, const char * conjunction);
114
115 /* Just like g_english_strjoinv, but also frees the array. */
116 char * g_english_strjoinv_and_free(char ** strv, const char * conjunction);
117
118 /* Replacement for built-in functions. */
119 #if !(GLIB_CHECK_VERSION(2,6,0))
120 guint g_strv_length(gchar ** strv);
121 #endif
122
123 #if !GLIB_CHECK_VERSION(2,4,0)
124 void g_ptr_array_foreach (GPtrArray *array,
125                           GFunc func,
126                           gpointer user_data);
127 #endif
128
129 /* functions for g_hash_table_new to hash and compare case-insensitive strings */
130 guint g_str_amanda_hash(gconstpointer v);
131 gboolean g_str_amanda_equal(gconstpointer v1, gconstpointer v2);
132
133 #endif