2006-08-17 Aaron Bockover <abockover@novell.com>
[mono.git] / eglib / src / glib.h
1 #ifndef __GLIB_H
2 #define __GLIB_H
3
4 #include <stdarg.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <stdio.h>
8
9 /*
10  * Basic data types
11  */
12 typedef int            gboolean;
13 typedef int            gint;
14 typedef unsigned int   gsize;
15 typedef unsigned int   guint;
16 typedef short          gshort;
17 typedef unsigned short gushort;
18 typedef long           glong;
19 typedef unsigned long  gulong;
20 typedef void *         gpointer;
21 typedef const void *   gconstpointer;
22 typedef char           gchar;
23 typedef unsigned char  guchar;
24
25 /*
26  * Macros
27  */
28 #define G_N_ELEMENTS(s)      (sizeof(s) / sizeof ((s) [0]))
29
30 #define FALSE                0
31 #define TRUE                 1
32
33 #define G_MAXINT32           0xf7777777
34 #define G_MININT32           0x80000000
35
36 #define GPOINTER_TO_INT(ptr)   ((int)(ptr))
37 #define GPOINTER_TO_UINT(ptr)  ((uint)(ptr))
38 #define GINT_TO_POINTER(v)     ((gpointer) (v))
39 #define GUINT_TO_POINTER(v)    ((gpointer) (v))
40
41 #define G_STRUCT_OFFSET(p_type,field) \
42         ((long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
43
44 /*
45  * Allocation
46  */
47 #define g_new(type,size)        ((type *) malloc (sizeof (type) * (size)))
48 #define g_new0(type,size)       ((type *) calloc (sizeof (type), (size))) 
49 #define g_free(obj)             free (obj);
50 #define g_realloc(obj,size)     realloc((obj), (size))
51 #define g_strdup(x)             strdup(x)
52 #define g_malloc(x)             malloc(x)
53 #define g_try_malloc(x)         malloc(x)
54 #define g_try_realloc(obj,size) realloc((obj),(size))
55 #define g_malloc0(x)            calloc(1,x)
56 #define g_memmove(dest,src,len) memmove (dest, src, len)
57 #define g_renew(struct_type, mem, n_structs) realloc (mem, sizeof (struct_type) * n_structs)
58 #define g_alloca(size)          alloca (size)
59
60 gpointer g_memdup (gconstpointer mem, guint byte_size);
61
62 /*
63  * Misc.
64  */
65 #define g_atexit(func)  ((void) atexit (func))
66 /*
67  * Precondition macros
68  */
69 #define g_return_if_fail(x)  do { if (!(x)) { printf ("%s:%d: assertion %s failed", __FILE__, __LINE__, #x); return; } } while (0) ;
70 #define g_return_val_if_fail(x,e)  do { if (!(x)) { printf ("%s:%d: assertion %s failed", __FILE__, __LINE__, #x); return (e); } } while (0) ;
71
72 /*
73  * Hashtables
74  */
75 typedef struct _GHashTable GHashTable;
76 typedef void     (*GFunc)          (gpointer data, gpointer user_data);
77 typedef gint     (*GCompareFunc)   (gconstpointer a, gconstpointer b);
78 typedef gint     (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
79 typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
80 typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
81 typedef void     (*GDestroyNotify) (gpointer data);
82 typedef guint    (*GHashFunc)      (gconstpointer key);
83 typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
84
85 GHashTable     *g_hash_table_new             (GHashFunc hash_func, GEqualFunc key_equal_func);
86 void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
87 guint           g_hash_table_size            (GHashTable *hash);
88 gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
89 gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
90 void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
91 gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
92 gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
93 guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
94 void            g_hash_table_destroy         (GHashTable *hash);
95
96 #define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
97 #define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
98
99 gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
100 guint    g_direct_hash  (gconstpointer v1);
101 gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
102 guint    g_int_hash     (gconstpointer v1);
103 gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
104 guint    g_str_hash     (gconstpointer v1);
105
106 #define  g_assert(x)     do { if (!(x)) g_error ("* Assertion at %s:%d, condition `%s' not met\n", __FILE__, __LINE__, #x); } while (0)
107 #define  g_assert_not_reached() do { g_error ("* Assertion: should not be reached at %s:%d\n", __FILE__, __LINE__); } while (0)
108
109 /*
110  * Strings utility
111  */
112 gchar       *g_strdup_printf  (const gchar *format, ...);
113 gchar       *g_strdup_vprintf (const gchar *format, va_list args);
114 gchar       *g_strndup        (const gchar *str, gsize n);
115 const gchar *g_strerror       (gint errnum);
116 gchar       *g_strndup        (const gchar *str, gsize n);
117 void         g_strfreev       (gchar **str_array);
118 gchar       *g_strconcat      (const gchar *first, ...);
119 gchar      **g_strsplit       (const gchar *string, const gchar *delimiter, gint max_tokens);
120 gchar       *g_strreverse     (gchar *str);
121 gboolean     g_str_has_prefix (const gchar *str, const gchar *prefix);
122 gboolean     g_str_has_suffix (const gchar *str, const gchar *suffix);
123 gchar       *g_strjoin        (const gchar *separator, ...);
124
125 /*
126  * String type
127  */
128 typedef struct {
129         char *str;
130         gsize len;
131         gsize allocated_len;
132 } GString;
133
134 GString     *g_string_new           (const gchar *init);
135 GString     *g_string_new_len       (const gchar *init, gsize len);
136 GString     *g_string_sized_new     (gsize default_size);
137 gchar       *g_string_free          (GString *string, gboolean free_segment);
138 GString     *g_string_append        (GString *string, const gchar *val);
139 void         g_string_printf        (GString *string, const gchar *format, ...);
140 void         g_string_append_printf (GString *string, const gchar *format, ...);
141 GString     *g_string_append_c      (GString *string, gchar c);
142 GString     *g_string_append        (GString *string, const gchar *val);
143 GString     *g_string_append_len    (GString *string, const gchar *val, gsize len);
144 #define g_string_sprintfa g_string_append_printf
145
146 /*
147  * Lists
148  */
149 typedef struct _GSList GSList;
150 struct _GSList {
151         gpointer data;
152         GSList *next;
153 };
154
155 GSList *g_slist_alloc     (void);
156 GSList *g_slist_append    (GSList* list, gpointer data);
157 GSList *g_slist_prepend   (GSList* list, gpointer data);
158 void    g_slist_free      (GSList* list);
159 void    g_slist_free_1    (GSList* list);
160 GSList *g_slist_copy      (GSList* list);
161 GSList *g_slist_concat    (GSList* list1, GSList* list2);
162 void    g_slist_foreach   (GSList* list, GFunc func, gpointer user_data);
163 GSList *g_slist_last      (GSList *list);
164 GSList *g_slist_find      (GSList *list, gconstpointer data);
165 GSList *g_slist_remove    (GSList *list, gconstpointer data);
166 GSList *g_slist_reverse   (GSList *list);
167 GSList *g_slist_remove_link (GSList *list, GSList *link);
168 GSList *g_slist_delete_link (GSList *list, GSList *link);
169 GSList *g_slist_insert_sorted (GSList *list, gpointer data, GCompareFunc func);
170 guint  g_slist_length     (GSList *list);
171 #define g_slist_next (slist) ((slist) ? (((GSList *) slist)->next) : NULL)
172
173 /*
174  * Pointer Array
175  */
176
177 typedef struct _GPtrArray GPtrArray;
178 struct _GPtrArray {
179         gpointer *pdata;
180         guint len;
181 };
182
183 GPtrArray *g_ptr_array_new                ();
184 GPtrArray *g_ptr_array_sized_new          (guint reserved_size);
185 void       g_ptr_array_add                (GPtrArray *array, gpointer data);
186 gboolean   g_ptr_array_remove             (GPtrArray *array, gpointer data);
187 gpointer   g_ptr_array_remove_index       (GPtrArray *array, guint index);
188 gboolean   g_ptr_array_remove_fast        (GPtrArray *array, gpointer data);
189 gpointer   g_ptr_array_remove_index_fast  (GPtrArray *array, gpointer data);
190 void       g_ptr_array_sort               (GPtrArray *array, GCompareFunc compare_func);
191 void       g_ptr_array_sort_with_data     (GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data);
192 void       g_ptr_array_set_size           (GPtrArray *array, gint length);
193 gpointer  *g_ptr_array_free               (GPtrArray *array, gboolean free_seg);
194 void       g_ptr_array_foreach            (GPtrArray *array, GFunc func, gpointer user_data);
195 #define    g_ptr_array_index(array,index) array->pdata[index]
196
197
198 /*
199  * Modules
200  */
201 typedef enum {
202         G_MODULE_BIND_LAZY = 0x01,
203         G_MODULE_BIND_LOCAL = 0x02,
204         G_MODULE_BIND_MASK = 0x03
205 } GModuleFlags;
206 typedef struct _GModule GModule;
207
208 GModule *g_module_open (const gchar *file, GModuleFlags flags);
209 gboolean g_module_symbol (GModule *module, const gchar *symbol_name,
210                           gpointer *symbol);
211 const gchar *g_module_error (void);
212 gboolean g_module_close (GModule *module);
213 gchar *  g_module_build_path (const gchar *directory, const gchar *module_name);
214 /*
215  * Messages
216  */
217 #ifndef G_LOG_DOMAIN
218 #define G_LOG_DOMAIN ((gchar*) 0)
219 #endif
220
221 typedef enum {
222         G_LOG_FLAG_RECURSION          = 1 << 0,
223         G_LOG_FLAG_FATAL              = 1 << 1,
224         
225         G_LOG_LEVEL_ERROR             = 1 << 2,
226         G_LOG_LEVEL_CRITICAL          = 1 << 3,
227         G_LOG_LEVEL_WARNING           = 1 << 4,
228         G_LOG_LEVEL_MESSAGE           = 1 << 5,
229         G_LOG_LEVEL_INFO              = 1 << 6,
230         G_LOG_LEVEL_DEBUG             = 1 << 7,
231         
232         G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
233 } GLogLevelFlags;
234
235 void           g_print                (const gchar *format, ...);
236 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
237 GLogLevelFlags g_log_set_fatal_mask   (const gchar *log_domain, GLogLevelFlags fatal_mask);
238 void           g_logv                 (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args);
239 void           g_log                  (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...);
240
241 #define g_error(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format)
242 #define g_critical(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format)
243 #define g_warning(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format)
244 #define g_message(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format)
245 #define g_debug(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
246
247 #endif