Update the TODO
[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   guint;
17 typedef short          gshort;
18 typedef unsigned short gushort;
19 typedef long           glong;
20 typedef unsigned long  gulong;
21 typedef void *         gpointer;
22 typedef const void *   gconstpointer;
23 typedef char           gchar;
24 typedef unsigned char  guchar;
25
26 /* Types defined in terms of the stdint.h */
27 typedef int8_t         gint8;
28 typedef uint8_t        guint8;
29 typedef int16_t        gint16;
30 typedef uint16_t       guint16;
31 typedef int32_t        gint32;
32 typedef uint32_t       guint32;
33 typedef int64_t        gint64;
34 typedef uint64_t       guint64;
35 typedef float          gfloat;
36 typedef double         gdouble;
37 /*
38  * Macros
39  */
40 #define G_N_ELEMENTS(s)      (sizeof(s) / sizeof ((s) [0]))
41
42 #define FALSE                0
43 #define TRUE                 1
44
45 #define G_MAXINT32           INT32_MAX
46 #define G_MININT32           INT32_MIN
47
48 #define G_LITTLE_ENDIAN 1234
49 #define G_BIG_ENDIAN    4321
50 #define G_STMT_START    do 
51 #define G_STMT_END      while (0)
52
53 #define ABS(a,b)        (((a)>(b)) ? ((a)-(b)) : ((b)-(a)))
54
55 #define G_STRUCT_OFFSET(p_type,field) \
56         ((long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
57
58
59 /*
60  * Allocation
61  */
62 #define g_new(type,size)        ((type *) malloc (sizeof (type) * (size)))
63 #define g_new0(type,size)       ((type *) calloc (sizeof (type), (size))) 
64 #define g_realloc(obj,size)     realloc((obj), (size))
65 #define g_strdup(x)             strdup(x)
66 #define g_malloc(x)             malloc(x)
67 #define g_try_malloc(x)         malloc(x)
68 #define g_try_realloc(obj,size) realloc((obj),(size))
69 #define g_malloc0(x)            calloc(1,x)
70 #define g_memmove(dest,src,len) memmove (dest, src, len)
71 #define g_renew(struct_type, mem, n_structs) realloc (mem, sizeof (struct_type) * n_structs)
72 #define g_alloca(size)          alloca (size)
73 #define g_free                  free
74
75 gpointer g_memdup (gconstpointer mem, guint byte_size);
76
77 /*
78  * Misc.
79  */
80 #define g_atexit(func)  ((void) atexit (func))
81
82 const gchar *    g_getenv(const gchar *variable);
83 gboolean         g_setenv(const gchar *variable, const gchar *value, gboolean overwrite);
84 void             g_unsetenv(const gchar *variable);
85
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 GHashTable     *g_hash_table_new_full        (GHashFunc hash_func, GEqualFunc key_equal_func,
107                                               GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func);
108 void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
109 guint           g_hash_table_size            (GHashTable *hash);
110 gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
111 gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
112 void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
113 gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
114 gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
115 guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
116 void            g_hash_table_destroy         (GHashTable *hash);
117
118 #define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
119 #define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
120
121 gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
122 guint    g_direct_hash  (gconstpointer v1);
123 gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
124 guint    g_int_hash     (gconstpointer v1);
125 gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
126 guint    g_str_hash     (gconstpointer v1);
127
128 #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
129 #define  g_assert_not_reached() G_STMT_START { g_error ("* Assertion: should not be reached at %s:%d\n", __FILE__, __LINE__); } G_STMT_END
130
131 /*
132  * Strings utility
133  */
134 gchar       *g_strdup_printf  (const gchar *format, ...);
135 gchar       *g_strdup_vprintf (const gchar *format, va_list args);
136 gchar       *g_strndup        (const gchar *str, gsize n);
137 const gchar *g_strerror       (gint errnum);
138 gchar       *g_strndup        (const gchar *str, gsize n);
139 void         g_strfreev       (gchar **str_array);
140 gchar       *g_strconcat      (const gchar *first, ...);
141 gchar      **g_strsplit       (const gchar *string, const gchar *delimiter, gint max_tokens);
142 gchar       *g_strreverse     (gchar *str);
143 gboolean     g_str_has_prefix (const gchar *str, const gchar *prefix);
144 gboolean     g_str_has_suffix (const gchar *str, const gchar *suffix);
145 gchar       *g_strjoin        (const gchar *separator, ...);
146 gchar       *g_strchug        (gchar *str);
147 gchar       *g_strchomp       (gchar *str);
148
149 gint         g_printf          (gchar const *format, ...);
150 gint         g_fprintf         (FILE *file, gchar const *format, ...);
151 gint         g_sprintf         (gchar *string, gchar const *format, ...);
152 gint         g_snprintf        (gchar *string, gulong n, gchar const *format, ...);
153
154 #define g_vprintf vprintf
155 #define g_vfprintf vfprintf
156 #define g_vsprintf vsprintf
157 #define g_vsnprintf vsnprintf
158 #define g_vasprintf vasprintf
159
160 #define g_ascii_isspace(c) (isspace (c) != 0)
161 #define g_ascii_isalpha(c) (isalpha (c) != 0)
162 #define g_ascii_isprint(c) (isprint (c) != 0)
163
164 /* FIXME: g_strcasecmp supports utf8 unicode stuff */
165 #define g_strcasecmp strcasecmp
166 #define g_ascii_strcasecmp strcasecmp
167 #define g_strncasecmp strncasecmp
168 #define g_strstrip(a) g_strchug (g_strchomp (a))
169
170 /*
171  * String type
172  */
173 typedef struct {
174         char *str;
175         gsize len;
176         gsize allocated_len;
177 } GString;
178
179 GString     *g_string_new           (const gchar *init);
180 GString     *g_string_new_len       (const gchar *init, gssize len);
181 GString     *g_string_sized_new     (gsize default_size);
182 gchar       *g_string_free          (GString *string, gboolean free_segment);
183 GString     *g_string_append        (GString *string, const gchar *val);
184 void         g_string_printf        (GString *string, const gchar *format, ...);
185 void         g_string_append_printf (GString *string, const gchar *format, ...);
186 GString     *g_string_append_c      (GString *string, gchar c);
187 GString     *g_string_append        (GString *string, const gchar *val);
188 GString     *g_string_append_len    (GString *string, const gchar *val, gssize len);
189 GString     *g_string_truncate      (GString *string, gsize len);
190 GString     *g_string_prepend       (GString *string, const gchar *val);
191
192 #define g_string_sprintfa g_string_append_printf
193
194 /*
195  * Lists
196  */
197 typedef struct _GSList GSList;
198 struct _GSList {
199         gpointer data;
200         GSList *next;
201 };
202
203 GSList *g_slist_alloc         (void);
204 GSList *g_slist_append        (GSList        *list,
205                                gpointer       data);
206 GSList *g_slist_prepend       (GSList        *list,
207                                gpointer       data);
208 void    g_slist_free          (GSList        *list);
209 void    g_slist_free_1        (GSList        *list);
210 GSList *g_slist_copy          (GSList        *list);
211 GSList *g_slist_concat        (GSList        *list1,
212                                GSList        *list2);
213 void    g_slist_foreach       (GSList        *list,
214                                GFunc          func,
215                                gpointer       user_data);
216 GSList *g_slist_last          (GSList        *list);
217 GSList *g_slist_find          (GSList        *list,
218                                gconstpointer  data);
219 GSList *g_slist_remove        (GSList        *list,
220                                gconstpointer  data);
221 GSList *g_slist_reverse       (GSList        *list);
222 guint   g_slist_length        (GSList        *list);
223 GSList *g_slist_remove_link   (GSList        *list,
224                                GSList        *link);
225 GSList *g_slist_delete_link   (GSList        *list,
226                                GSList        *link);
227 GSList *g_slist_insert_sorted (GSList        *list,
228                                gpointer       data,
229                                GCompareFunc   func);
230
231 #define g_slist_next(slist) ((slist) ? (((GSList *) (slist))->next) : NULL)
232
233 typedef struct _GList GList;
234 struct _GList {
235   gpointer data;
236   GList *next;
237   GList *prev;
238 };
239
240 #define g_list_next(list) ((list) ? (((GList *) (list))->next) : NULL);
241
242 GList *g_list_alloc         (void);
243 GList *g_list_append        (GList         *list,
244                              gpointer       data);
245 GList *g_list_prepend       (GList         *list,
246                              gpointer       data);
247 void   g_list_free          (GList         *list);
248 void   g_list_free_1        (GList         *list);
249 GList *g_list_copy          (GList         *list);
250 guint  g_list_length        (GList         *list);
251 gint   g_list_index         (GList         *list,
252                              gconstpointer  data);
253 GList *g_list_nth           (GList         *list,
254                              guint          n);
255 gpointer g_list_nth_data      (GList         *list,
256                              guint          n);
257 GList *g_list_last          (GList         *list);
258 GList *g_list_concat        (GList         *list1,
259                              GList         *list2);
260 void   g_list_foreach       (GList         *list,
261                              GFunc          func,
262                              gpointer       user_data);
263 GList *g_list_first         (GList         *list);
264 GList *g_list_find          (GList         *list,
265                              gconstpointer  data);
266 GList *g_list_remove        (GList         *list,
267                              gconstpointer  data);
268 GList *g_list_reverse       (GList         *list);
269 GList *g_list_remove_link   (GList         *list,
270                              GList         *link);
271 GList *g_list_delete_link   (GList         *list,
272                              GList         *link);
273 GList *g_list_insert_sorted (GList         *list,
274                              gpointer       data,
275                              GCompareFunc   func);
276 GList *g_list_insert_before (GList         *list,
277                              GList         *sibling,
278                              gpointer       data);
279
280 /*
281  * Array
282  */
283
284 typedef struct _GArray GArray;
285 struct _GArray {
286         gchar *data;
287         gint len;
288 };
289
290 GArray *g_array_new               (gboolean zero_terminated, gboolean clear_, guint element_size);
291 gchar*  g_array_free              (GArray *array, gboolean free_segment);
292 GArray *g_array_append_vals       (GArray *array, gconstpointer data, guint len);
293 GArray* g_array_insert_vals       (GArray *array, guint index_, gconstpointer data, guint len);
294 GArray* g_array_remove_index      (GArray *array, guint index_);
295
296 #define g_array_append_val(a,v)   (g_array_append_vals((a),&(v),1))
297 #define g_array_insert_val(a,i,v) (g_array_insert_vals((a),(i),&(v),1))
298 #define g_array_index(a,t,i)      *(t*)(((a)->data) + sizeof(t) * (i))
299
300 /*
301  * Pointer Array
302  */
303
304 typedef struct _GPtrArray GPtrArray;
305 struct _GPtrArray {
306         gpointer *pdata;
307         guint len;
308 };
309
310 GPtrArray *g_ptr_array_new                ();
311 GPtrArray *g_ptr_array_sized_new          (guint reserved_size);
312 void       g_ptr_array_add                (GPtrArray *array, gpointer data);
313 gboolean   g_ptr_array_remove             (GPtrArray *array, gpointer data);
314 gpointer   g_ptr_array_remove_index       (GPtrArray *array, guint index);
315 gboolean   g_ptr_array_remove_fast        (GPtrArray *array, gpointer data);
316 gpointer   g_ptr_array_remove_index_fast  (GPtrArray *array, gpointer data);
317 void       g_ptr_array_sort               (GPtrArray *array, GCompareFunc compare_func);
318 void       g_ptr_array_sort_with_data     (GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data);
319 void       g_ptr_array_set_size           (GPtrArray *array, gint length);
320 gpointer  *g_ptr_array_free               (GPtrArray *array, gboolean free_seg);
321 void       g_ptr_array_foreach            (GPtrArray *array, GFunc func, gpointer user_data);
322 #define    g_ptr_array_index(array,index) (array)->pdata[(index)]
323
324 /*
325  * Queues
326  */
327 typedef struct {
328         GList *head;
329         GList *tail;
330         guint length;
331 } GQueue;
332
333 gpointer g_queue_pop_head  (GQueue   *queue);
334 void     g_queue_push_head (GQueue   *queue,
335                             gpointer  data);
336 gboolean g_queue_is_empty  (GQueue   *queue);
337 GQueue  *g_queue_new       (void);
338 void     g_queue_free      (GQueue   *queue);
339
340
341 /*
342  * Modules
343  */
344 typedef enum {
345         G_MODULE_BIND_LAZY = 0x01,
346         G_MODULE_BIND_LOCAL = 0x02,
347         G_MODULE_BIND_MASK = 0x03
348 } GModuleFlags;
349 typedef struct _GModule GModule;
350
351 GModule *g_module_open (const gchar *file, GModuleFlags flags);
352 gboolean g_module_symbol (GModule *module, const gchar *symbol_name,
353                           gpointer *symbol);
354 const gchar *g_module_error (void);
355 gboolean g_module_close (GModule *module);
356 gchar *  g_module_build_path (const gchar *directory, const gchar *module_name);
357 /*
358  * Messages
359  */
360 #ifndef G_LOG_DOMAIN
361 #define G_LOG_DOMAIN ((gchar*) 0)
362 #endif
363
364 typedef enum {
365         G_LOG_FLAG_RECURSION          = 1 << 0,
366         G_LOG_FLAG_FATAL              = 1 << 1,
367         
368         G_LOG_LEVEL_ERROR             = 1 << 2,
369         G_LOG_LEVEL_CRITICAL          = 1 << 3,
370         G_LOG_LEVEL_WARNING           = 1 << 4,
371         G_LOG_LEVEL_MESSAGE           = 1 << 5,
372         G_LOG_LEVEL_INFO              = 1 << 6,
373         G_LOG_LEVEL_DEBUG             = 1 << 7,
374         
375         G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
376 } GLogLevelFlags;
377
378 void           g_print                (const gchar *format, ...);
379 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
380 GLogLevelFlags g_log_set_fatal_mask   (const gchar *log_domain, GLogLevelFlags fatal_mask);
381 void           g_logv                 (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args);
382 void           g_log                  (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...);
383
384 #define g_error(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format)
385 #define g_critical(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format)
386 #define g_warning(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format)
387 #define g_message(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format)
388 #define g_debug(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
389
390 /*
391  * Unicode Manipulation: most of this is not used by Mono by default, it is
392  * only used if the old collation code is activated, so this is only the
393  * bare minimum to build.
394  */
395 typedef guint32 gunichar;
396
397 typedef enum {
398         G_UNICODE_LOWERCASE_LETTER,
399 } GUnicodeType;
400
401 gunichar       g_unichar_tolower (gunichar c);
402 GUnicodeType   g_unichar_type    (gunichar c);
403
404 #ifndef MAX
405 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
406 #endif
407
408 /* FIXME: Implement these two for gcc */
409 #define G_LIKELY(x) (x)
410 #define G_UNLIKELY(x) (x)
411 #endif