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