2006-08-18 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 #include <stdint.h>
9 #include <eglib-config.h>
10
11 /*
12  * Basic data types
13  */
14 typedef int            gboolean;
15 typedef int            gint;
16 typedef unsigned int   gsize;
17 typedef unsigned int   guint;
18 typedef short          gshort;
19 typedef unsigned short gushort;
20 typedef long           glong;
21 typedef unsigned long  gulong;
22 typedef void *         gpointer;
23 typedef const void *   gconstpointer;
24 typedef char           gchar;
25 typedef unsigned char  guchar;
26
27 /* Types defined in terms of the stdint.h */
28 typedef int8_t         gint8;
29 typedef uint8_t        guint8;
30 typedef int16_t        gint16;
31 typedef uint16_t       guint16;
32 typedef int32_t        gint32;
33 typedef uint32_t       guint32;
34 typedef int64_t        gint64;
35 typedef uint64_t       guint64;
36 typedef float          gfloat;
37 typedef double         gdouble;
38 /*
39  * Macros
40  */
41 #define G_N_ELEMENTS(s)      (sizeof(s) / sizeof ((s) [0]))
42
43 #define FALSE                0
44 #define TRUE                 1
45
46 #define G_MAXINT32           INT32_MAX
47 #define G_MININT32           INT32_MIN
48
49 #define GPOINTER_TO_INT(ptr)   ((int)(ptr))
50 #define GPOINTER_TO_UINT(ptr)  ((uint)(ptr))
51 #define GINT_TO_POINTER(v)     ((gpointer) (v))
52 #define GUINT_TO_POINTER(v)    ((gpointer) (v))
53
54 #define G_GSIZE_FORMAT "u"
55 #define G_LITTLE_ENDIAN 1234
56 #define G_BIG_ENDIAN    4321
57 #define G_STMT_START    do 
58 #define G_STMT_END      while (0)
59
60 #define G_STRUCT_OFFSET(p_type,field) \
61         ((long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
62
63
64 /*
65  * Allocation
66  */
67 #define g_new(type,size)        ((type *) malloc (sizeof (type) * (size)))
68 #define g_new0(type,size)       ((type *) calloc (sizeof (type), (size))) 
69 #define g_free(obj)             free (obj);
70 #define g_realloc(obj,size)     realloc((obj), (size))
71 #define g_strdup(x)             strdup(x)
72 #define g_malloc(x)             malloc(x)
73 #define g_try_malloc(x)         malloc(x)
74 #define g_try_realloc(obj,size) realloc((obj),(size))
75 #define g_malloc0(x)            calloc(1,x)
76 #define g_memmove(dest,src,len) memmove (dest, src, len)
77 #define g_renew(struct_type, mem, n_structs) realloc (mem, sizeof (struct_type) * n_structs)
78 #define g_alloca(size)          alloca (size)
79
80 gpointer g_memdup (gconstpointer mem, guint byte_size);
81
82 /*
83  * Misc.
84  */
85 #define g_atexit(func)  ((void) atexit (func))
86 /*
87  * Precondition macros
88  */
89 #define g_return_if_fail(x)  G_STMT_START { if (!(x)) { printf ("%s:%d: assertion %s failed", __FILE__, __LINE__, #x); return; } } G_STMT_END
90 #define g_return_val_if_fail(x,e)  G_STMT_START { if (!(x)) { printf ("%s:%d: assertion %s failed", __FILE__, __LINE__, #x); return (e); } } G_STMT_END
91
92 /*
93  * Hashtables
94  */
95 typedef struct _GHashTable GHashTable;
96 typedef void     (*GFunc)          (gpointer data, gpointer user_data);
97 typedef gint     (*GCompareFunc)   (gconstpointer a, gconstpointer b);
98 typedef gint     (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
99 typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
100 typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
101 typedef void     (*GDestroyNotify) (gpointer data);
102 typedef guint    (*GHashFunc)      (gconstpointer key);
103 typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
104
105 GHashTable     *g_hash_table_new             (GHashFunc hash_func, GEqualFunc key_equal_func);
106 void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
107 guint           g_hash_table_size            (GHashTable *hash);
108 gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
109 gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
110 void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
111 gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
112 gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
113 guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
114 void            g_hash_table_destroy         (GHashTable *hash);
115
116 #define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
117 #define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
118
119 gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
120 guint    g_direct_hash  (gconstpointer v1);
121 gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
122 guint    g_int_hash     (gconstpointer v1);
123 gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
124 guint    g_str_hash     (gconstpointer v1);
125
126 #define  g_assert(x)     G_STMT_START { if (!(x)) g_error ("* Assertion at %s:%d, condition `%s' not met\n", __FILE__, __LINE__, #x);  } G_STMT_END
127 #define  g_assert_not_reached() G_STMT_START { g_error ("* Assertion: should not be reached at %s:%d\n", __FILE__, __LINE__); } G_STMT_END
128
129 /*
130  * Strings utility
131  */
132 gchar       *g_strdup_printf  (const gchar *format, ...);
133 gchar       *g_strdup_vprintf (const gchar *format, va_list args);
134 gchar       *g_strndup        (const gchar *str, gsize n);
135 const gchar *g_strerror       (gint errnum);
136 gchar       *g_strndup        (const gchar *str, gsize n);
137 void         g_strfreev       (gchar **str_array);
138 gchar       *g_strconcat      (const gchar *first, ...);
139 gchar      **g_strsplit       (const gchar *string, const gchar *delimiter, gint max_tokens);
140 gchar       *g_strreverse     (gchar *str);
141 gboolean     g_str_has_prefix (const gchar *str, const gchar *prefix);
142 gboolean     g_str_has_suffix (const gchar *str, const gchar *suffix);
143 gchar       *g_strjoin        (const gchar *separator, ...);
144 gchar       *g_strchug        (gchar *str);
145 gchar       *g_strchomp       (gchar *str);
146
147 #define g_ascii_isspace(c) (isspace (c) != 0)
148 #define g_ascii_isalpha(c) (isalpha (c) != 0)
149 #define g_ascii_isprint(c) (isprint (c) != 0)
150
151 /* FIXME: g_strcasecmp supports utf8 unicode stuff */
152 #define g_strcasecmp strcasecmp
153 #define g_ascii_strcasecmp strcasecmp
154 #define g_strncasecmp strncasecmp
155 #define g_strstrip(a) g_strchug (g_strchomp (a))
156
157 /*
158  * String type
159  */
160 typedef struct {
161         char *str;
162         gsize len;
163         gsize allocated_len;
164 } GString;
165
166 GString     *g_string_new           (const gchar *init);
167 GString     *g_string_new_len       (const gchar *init, gsize len);
168 GString     *g_string_sized_new     (gsize default_size);
169 gchar       *g_string_free          (GString *string, gboolean free_segment);
170 GString     *g_string_append        (GString *string, const gchar *val);
171 void         g_string_printf        (GString *string, const gchar *format, ...);
172 void         g_string_append_printf (GString *string, const gchar *format, ...);
173 GString     *g_string_append_c      (GString *string, gchar c);
174 GString     *g_string_append        (GString *string, const gchar *val);
175 GString     *g_string_append_len    (GString *string, const gchar *val, gsize len);
176 #define g_string_sprintfa g_string_append_printf
177
178 /*
179  * Lists
180  */
181 typedef struct _GSList GSList;
182 struct _GSList {
183         gpointer data;
184         GSList *next;
185 };
186
187 GSList *g_slist_alloc         (void);
188 GSList *g_slist_append        (GSList        *list,
189                                gpointer       data);
190 GSList *g_slist_prepend       (GSList        *list,
191                                gpointer       data);
192 void    g_slist_free          (GSList        *list);
193 void    g_slist_free_1        (GSList        *list);
194 GSList *g_slist_copy          (GSList        *list);
195 GSList *g_slist_concat        (GSList        *list1,
196                                GSList        *list2);
197 void    g_slist_foreach       (GSList        *list,
198                                GFunc          func,
199                                gpointer       user_data);
200 GSList *g_slist_last          (GSList        *list);
201 GSList *g_slist_find          (GSList        *list,
202                                gconstpointer  data);
203 GSList *g_slist_remove        (GSList        *list,
204                                gconstpointer  data);
205 GSList *g_slist_reverse       (GSList        *list);
206 guint   g_slist_length        (GSList        *list);
207 GSList *g_slist_remove_link   (GSList        *list,
208                                GSList        *link);
209 GSList *g_slist_delete_link   (GSList        *list,
210                                GSList        *link);
211 GSList *g_slist_insert_sorted (GSList        *list,
212                                gpointer       data,
213                                GCompareFunc   func);
214
215 #define g_slist_next(slist) ((slist) ? (((GSList *) (slist))->next) : NULL)
216
217 typedef struct _GList GList;
218 struct _GList {
219   gpointer data;
220   GList *next;
221   GList *prev;
222 };
223
224 #define g_list_next(list) ((list) ? (((GList *) (list))->next) : NULL);
225
226 GList *g_list_alloc         (void);
227 GList *g_list_append        (GList         *list,
228                              gpointer       data);
229 GList *g_list_prepend       (GList         *list,
230                              gpointer       data);
231 void   g_list_free          (GList         *list);
232 void   g_list_free_1        (GList         *list);
233 GList *g_list_copy          (GList         *list);
234 guint  g_list_length        (GList         *list);
235 gint   g_list_index         (GList         *list,
236                              gconstpointer  data);
237 GList *g_list_nth           (GList         *list,
238                              guint          n);
239 gpointer g_list_nth_data      (GList         *list,
240                              guint          n);
241 GList *g_list_last          (GList         *list);
242 GList *g_list_concat        (GList         *list1,
243                              GList         *list2);
244 void   g_list_foreach       (GList         *list,
245                              GFunc          func,
246                              gpointer       user_data);
247 GList *g_list_first         (GList         *list);
248 GList *g_list_find          (GList         *list,
249                              gconstpointer  data);
250 GList *g_list_remove        (GList         *list,
251                              gconstpointer  data);
252 GList *g_list_reverse       (GList         *list);
253 GList *g_list_remove_link   (GList         *list,
254                              GList         *link);
255 GList *g_list_delete_link   (GList         *list,
256                              GList         *link);
257 GList *g_list_insert_sorted (GList         *list,
258                              gpointer       data,
259                              GCompareFunc   func);
260
261
262 /*
263  * Pointer Array
264  */
265
266 typedef struct _GPtrArray GPtrArray;
267 struct _GPtrArray {
268         gpointer *pdata;
269         guint len;
270 };
271
272 GPtrArray *g_ptr_array_new                ();
273 GPtrArray *g_ptr_array_sized_new          (guint reserved_size);
274 void       g_ptr_array_add                (GPtrArray *array, gpointer data);
275 gboolean   g_ptr_array_remove             (GPtrArray *array, gpointer data);
276 gpointer   g_ptr_array_remove_index       (GPtrArray *array, guint index);
277 gboolean   g_ptr_array_remove_fast        (GPtrArray *array, gpointer data);
278 gpointer   g_ptr_array_remove_index_fast  (GPtrArray *array, gpointer data);
279 void       g_ptr_array_sort               (GPtrArray *array, GCompareFunc compare_func);
280 void       g_ptr_array_sort_with_data     (GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data);
281 void       g_ptr_array_set_size           (GPtrArray *array, gint length);
282 gpointer  *g_ptr_array_free               (GPtrArray *array, gboolean free_seg);
283 void       g_ptr_array_foreach            (GPtrArray *array, GFunc func, gpointer user_data);
284 #define    g_ptr_array_index(array,index) (array)->pdata[(index)]
285
286
287 /*
288  * Modules
289  */
290 typedef enum {
291         G_MODULE_BIND_LAZY = 0x01,
292         G_MODULE_BIND_LOCAL = 0x02,
293         G_MODULE_BIND_MASK = 0x03
294 } GModuleFlags;
295 typedef struct _GModule GModule;
296
297 GModule *g_module_open (const gchar *file, GModuleFlags flags);
298 gboolean g_module_symbol (GModule *module, const gchar *symbol_name,
299                           gpointer *symbol);
300 const gchar *g_module_error (void);
301 gboolean g_module_close (GModule *module);
302 gchar *  g_module_build_path (const gchar *directory, const gchar *module_name);
303 /*
304  * Messages
305  */
306 #ifndef G_LOG_DOMAIN
307 #define G_LOG_DOMAIN ((gchar*) 0)
308 #endif
309
310 typedef enum {
311         G_LOG_FLAG_RECURSION          = 1 << 0,
312         G_LOG_FLAG_FATAL              = 1 << 1,
313         
314         G_LOG_LEVEL_ERROR             = 1 << 2,
315         G_LOG_LEVEL_CRITICAL          = 1 << 3,
316         G_LOG_LEVEL_WARNING           = 1 << 4,
317         G_LOG_LEVEL_MESSAGE           = 1 << 5,
318         G_LOG_LEVEL_INFO              = 1 << 6,
319         G_LOG_LEVEL_DEBUG             = 1 << 7,
320         
321         G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
322 } GLogLevelFlags;
323
324 void           g_print                (const gchar *format, ...);
325 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
326 GLogLevelFlags g_log_set_fatal_mask   (const gchar *log_domain, GLogLevelFlags fatal_mask);
327 void           g_logv                 (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args);
328 void           g_log                  (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...);
329
330 #define g_error(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format)
331 #define g_critical(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format)
332 #define g_warning(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format)
333 #define g_message(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format)
334 #define g_debug(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
335
336 /*
337  * Unicode Manipulation: most of this is not used by Mono by default, it is
338  * only used if the old collation code is activated, so this is only the
339  * bare minimum to build.
340  */
341 typedef guint32 gunichar;
342
343 typedef enum {
344         G_UNICODE_LOWERCASE_LETTER,
345 } GUnicodeType;
346
347 gunichar       g_unichar_tolower (gunichar c);
348 GUnicodeType   g_unichar_type    (gunichar c);
349
350 #endif