D'oh, forgot to add g_hash_table_steal() to glib.h
[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 <stddef.h>
9 #include <ctype.h>
10 #include <limits.h>
11
12 #ifdef _MSC_VER
13 #pragma include_alias(<eglib-config.h>, <eglib-config.hw>)
14 #endif
15
16 /* VS 2010 and later have stdint.h */
17 #if defined(_MSC_VER) && _MSC_VER < 1600
18 #else
19 #include <stdint.h>
20 #endif
21
22 #include <eglib-config.h>
23 #ifndef EGLIB_NO_REMAP
24 #include <eglib-remap.h>
25 #endif
26
27 #ifdef G_HAVE_ALLOCA_H
28 #include <alloca.h>
29 #endif
30
31 #ifdef WIN32
32 /* For alloca */
33 #include <malloc.h>
34 #endif
35
36 #ifndef offsetof
37 #   define offsetof(s_name,n_name) (size_t)(char *)&(((s_name*)0)->m_name)
38 #endif
39
40 #define __EGLIB_X11 1
41
42 #ifdef  __cplusplus
43 #define G_BEGIN_DECLS  extern "C" {
44 #define G_END_DECLS    }
45 #else
46 #define G_BEGIN_DECLS
47 #define G_END_DECLS
48 #endif
49
50 G_BEGIN_DECLS
51
52 #ifdef G_OS_WIN32
53 /* MSC and Cross-compilatin will use this */
54 int vasprintf (char **strp, const char *fmt, va_list ap);
55 #endif
56
57
58 /*
59  * Basic data types
60  */
61 typedef int            gint;
62 typedef unsigned int   guint;
63 typedef short          gshort;
64 typedef unsigned short gushort;
65 typedef long           glong;
66 typedef unsigned long  gulong;
67 typedef void *         gpointer;
68 typedef const void *   gconstpointer;
69 typedef char           gchar;
70 typedef unsigned char  guchar;
71
72 #if !G_TYPES_DEFINED
73 /* VS 2010 and later have stdint.h */
74 #if defined(_MSC_VER) && _MSC_VER < 1600
75 typedef __int8                          gint8;
76 typedef unsigned __int8         guint8;
77 typedef __int16                         gint16;
78 typedef unsigned __int16        guint16;
79 typedef __int32                         gint32;
80 typedef unsigned __int32        guint32;
81 typedef __int64                         gint64;
82 typedef unsigned __int64        guint64;
83 typedef float                           gfloat;
84 typedef double                          gdouble;
85 typedef unsigned __int16        gunichar2;
86 typedef int                 gboolean;
87 #else
88 /* Types defined in terms of the stdint.h */
89 typedef int8_t         gint8;
90 typedef uint8_t        guint8;
91 typedef int16_t        gint16;
92 typedef uint16_t       guint16;
93 typedef int32_t        gint32;
94 typedef uint32_t       guint32;
95 typedef int64_t        gint64;
96 typedef uint64_t       guint64;
97 typedef float          gfloat;
98 typedef double         gdouble;
99 typedef uint16_t       gunichar2;
100 typedef int32_t        gboolean;
101 #endif
102 #endif
103
104 /*
105  * Macros
106  */
107 #define G_N_ELEMENTS(s)      (sizeof(s) / sizeof ((s) [0]))
108
109 #define FALSE                0
110 #define TRUE                 1
111
112 #define G_MAXINT             INT_MAX
113 #define G_MININT             INT_MIN
114 #define G_MAXINT32           INT32_MAX
115 #define G_MININT32           INT32_MIN
116 #define G_MININT64           INT64_MIN
117 #define G_MAXINT64           INT64_MAX
118 #define G_MAXUINT64          UINT64_MAX
119
120 #define G_LITTLE_ENDIAN 1234
121 #define G_BIG_ENDIAN    4321
122 #define G_STMT_START    do 
123 #define G_STMT_END      while (0)
124
125 #define G_USEC_PER_SEC  1000000
126
127 #define ABS(a)         ((a) > 0 ? (a) : -(a))
128
129 #define G_STRUCT_OFFSET(p_type,field) offsetof(p_type,field)
130
131 #define EGLIB_STRINGIFY(x) #x
132 #define EGLIB_TOSTRING(x) EGLIB_STRINGIFY(x)
133 #define G_STRLOC __FILE__ ":" EGLIB_TOSTRING(__LINE__) ":"
134
135 #define G_CONST_RETURN const
136
137 /*
138  * Allocation
139  */
140 void g_free (void *ptr);
141 gpointer g_realloc (gpointer obj, gsize size);
142 gpointer g_malloc (gsize x);
143 gpointer g_malloc0 (gsize x);
144 gpointer g_try_malloc (gsize x);
145 gpointer g_try_realloc (gpointer obj, gsize size);
146
147 #define g_new(type,size)        ((type *) g_malloc (sizeof (type) * (size)))
148 #define g_new0(type,size)       ((type *) g_malloc0 (sizeof (type)* (size)))
149 #define g_newa(type,size)       ((type *) alloca (sizeof (type) * (size)))
150
151 #define g_memmove(dest,src,len) memmove (dest, src, len)
152 #define g_renew(struct_type, mem, n_structs) g_realloc (mem, sizeof (struct_type) * n_structs)
153 #define g_alloca(size)          alloca (size)
154
155 gpointer g_memdup (gconstpointer mem, guint byte_size);
156 static inline gchar   *g_strdup (const gchar *str) { if (str) {return strdup (str);} return NULL; }
157 gchar **g_strdupv (gchar **str_array);
158
159 typedef struct {
160         gpointer (*malloc)      (gsize    n_bytes);
161         gpointer (*realloc)     (gpointer mem, gsize n_bytes);
162         void     (*free)        (gpointer mem);
163         gpointer (*calloc)      (gsize    n_blocks, gsize n_block_bytes);
164         gpointer (*try_malloc)  (gsize    n_bytes);
165         gpointer (*try_realloc) (gpointer mem, gsize n_bytes);
166 } GMemVTable;
167
168 #define g_mem_set_vtable(x)
169
170 struct _GMemChunk {
171         guint alloc_size;
172 };
173
174 typedef struct _GMemChunk GMemChunk;
175 /*
176  * Misc.
177  */
178 #define g_atexit(func)  ((void) atexit (func))
179
180 const gchar *    g_getenv(const gchar *variable);
181 gboolean         g_setenv(const gchar *variable, const gchar *value, gboolean overwrite);
182 void             g_unsetenv(const gchar *variable);
183
184 gchar*           g_win32_getlocale(void);
185
186 /*
187  * Precondition macros
188  */
189 #define g_return_if_fail(x)  G_STMT_START { if (!(x)) { g_critical ("%s:%d: assertion '%s' failed", __FILE__, __LINE__, #x); return; } } G_STMT_END
190 #define g_return_val_if_fail(x,e)  G_STMT_START { if (!(x)) { g_critical ("%s:%d: assertion '%s' failed", __FILE__, __LINE__, #x); return (e); } } G_STMT_END
191
192 /*
193  * Hashtables
194  */
195 typedef struct _GHashTable GHashTable;
196 typedef struct _GHashTableIter GHashTableIter;
197
198 /* Private, but needed for stack allocation */
199 struct _GHashTableIter
200 {
201         gpointer dummy [8];
202 };
203
204 typedef void     (*GFunc)          (gpointer data, gpointer user_data);
205 typedef gint     (*GCompareFunc)   (gconstpointer a, gconstpointer b);
206 typedef gint     (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
207 typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
208 typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
209 typedef void     (*GDestroyNotify) (gpointer data);
210 typedef guint    (*GHashFunc)      (gconstpointer key);
211 typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
212 typedef void     (*GFreeFunc)      (gpointer       data);
213
214 GHashTable     *g_hash_table_new             (GHashFunc hash_func, GEqualFunc key_equal_func);
215 GHashTable     *g_hash_table_new_full        (GHashFunc hash_func, GEqualFunc key_equal_func,
216                                               GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func);
217 void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
218 guint           g_hash_table_size            (GHashTable *hash);
219 gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
220 gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
221 void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
222 gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
223 gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
224 gboolean        g_hash_table_steal           (GHashTable *hash, gconstpointer key);
225 void            g_hash_table_remove_all      (GHashTable *hash);
226 guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
227 guint           g_hash_table_foreach_steal   (GHashTable *hash, GHRFunc func, gpointer user_data);
228 void            g_hash_table_destroy         (GHashTable *hash);
229 void            g_hash_table_print_stats     (GHashTable *table);
230
231 void            g_hash_table_iter_init       (GHashTableIter *iter, GHashTable *hash_table);
232 gboolean        g_hash_table_iter_next       (GHashTableIter *iter, gpointer *key, gpointer *value);
233
234 guint           g_spaced_primes_closest      (guint x);
235
236 #define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
237 #define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
238
239 gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
240 guint    g_direct_hash  (gconstpointer v1);
241 gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
242 guint    g_int_hash     (gconstpointer v1);
243 gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
244 guint    g_str_hash     (gconstpointer v1);
245
246 /*
247  * Errors
248  */
249 typedef struct {
250         /* In the real glib, this is a GQuark, but we dont use/need that */
251         gpointer domain;
252         gint     code;
253         gchar   *message;
254 } GError;
255
256 void    g_clear_error (GError **error);
257 void    g_error_free (GError *error);
258 GError *g_error_new  (gpointer domain, gint code, const char *format, ...);
259 void    g_set_error  (GError **err, gpointer domain, gint code, const gchar *format, ...);
260 void    g_propagate_error (GError **dest, GError *src);
261
262 /*
263  * Strings utility
264  */
265 gchar       *g_strdup_printf  (const gchar *format, ...);
266 gchar       *g_strdup_vprintf (const gchar *format, va_list args);
267 gchar       *g_strndup        (const gchar *str, gsize n);
268 const gchar *g_strerror       (gint errnum);
269 gchar       *g_strndup        (const gchar *str, gsize n);
270 void         g_strfreev       (gchar **str_array);
271 gchar       *g_strconcat      (const gchar *first, ...);
272 gchar      **g_strsplit       (const gchar *string, const gchar *delimiter, gint max_tokens);
273 gchar      **g_strsplit_set   (const gchar *string, const gchar *delimiter, gint max_tokens);
274 gchar       *g_strreverse     (gchar *str);
275 gboolean     g_str_has_prefix (const gchar *str, const gchar *prefix);
276 gboolean     g_str_has_suffix (const gchar *str, const gchar *suffix);
277 guint        g_strv_length    (gchar **str_array);
278 gchar       *g_strjoin        (const gchar *separator, ...);
279 gchar       *g_strjoinv       (const gchar *separator, gchar **str_array);
280 gchar       *g_strchug        (gchar *str);
281 gchar       *g_strchomp       (gchar *str);
282 void         g_strdown        (gchar *string);
283 gchar       *g_strnfill       (gsize length, gchar fill_char);
284
285 gchar       *g_strdelimit     (gchar *string, const gchar *delimiters, gchar new_delimiter);
286 gchar       *g_strescape      (const gchar *source, const gchar *exceptions);
287
288 gchar       *g_filename_to_uri   (const gchar *filename, const gchar *hostname, GError **error);
289 gchar       *g_filename_from_uri (const gchar *uri, gchar **hostname, GError **error);
290
291 gint         g_printf          (gchar const *format, ...);
292 gint         g_fprintf         (FILE *file, gchar const *format, ...);
293 gint         g_sprintf         (gchar *string, gchar const *format, ...);
294 gint         g_snprintf        (gchar *string, gulong n, gchar const *format, ...);
295 #define g_vprintf vprintf
296 #define g_vfprintf vfprintf
297 #define g_vsprintf vsprintf
298 #define g_vsnprintf vsnprintf
299 #define g_vasprintf vasprintf
300
301 gsize       g_strlcpy          (gchar *dest, const gchar *src, gsize dest_size);
302
303 gchar   g_ascii_tolower      (gchar c);
304 gchar  *g_ascii_strdown      (const gchar *str, gssize len);
305 gint    g_ascii_strncasecmp  (const gchar *s1, const gchar *s2, gsize n);
306 gint    g_ascii_xdigit_value (gchar c);
307 #define g_ascii_isspace(c)   (isspace (c) != 0)
308 #define g_ascii_isalpha(c)   (isalpha (c) != 0)
309 #define g_ascii_isprint(c)   (isprint (c) != 0)
310 #define g_ascii_isxdigit(c)  (isxdigit (c) != 0)
311
312 /* FIXME: g_strcasecmp supports utf8 unicode stuff */
313 #ifdef _MSC_VER
314 #define g_strcasecmp stricmp
315 #define g_ascii_strcasecmp stricmp
316 #define g_strncasecmp strnicmp
317 #define g_strstrip(a) g_strchug (g_strchomp (a))
318 #else
319 #define g_strcasecmp strcasecmp
320 #define g_ascii_strcasecmp strcasecmp
321 #define g_ascii_strtoull strtoull
322 #define g_strncasecmp strncasecmp
323 #define g_strstrip(a) g_strchug (g_strchomp (a))
324 #endif
325
326
327 #define G_STR_DELIMITERS "_-|> <."
328
329 /*
330  * String type
331  */
332 typedef struct {
333         char *str;
334         gsize len;
335         gsize allocated_len;
336 } GString;
337
338 GString     *g_string_new           (const gchar *init);
339 GString     *g_string_new_len       (const gchar *init, gssize len);
340 GString     *g_string_sized_new     (gsize default_size);
341 gchar       *g_string_free          (GString *string, gboolean free_segment);
342 GString     *g_string_append        (GString *string, const gchar *val);
343 void         g_string_printf        (GString *string, const gchar *format, ...);
344 void         g_string_append_printf (GString *string, const gchar *format, ...);
345 GString     *g_string_append_c      (GString *string, gchar c);
346 GString     *g_string_append        (GString *string, const gchar *val);
347 GString     *g_string_append_len    (GString *string, const gchar *val, gssize len);
348 GString     *g_string_truncate      (GString *string, gsize len);
349 GString     *g_string_prepend       (GString *string, const gchar *val);
350
351 #define g_string_sprintfa g_string_append_printf
352
353 /*
354  * Lists
355  */
356 typedef struct _GSList GSList;
357 struct _GSList {
358         gpointer data;
359         GSList *next;
360 };
361
362 GSList *g_slist_alloc         (void);
363 GSList *g_slist_append        (GSList        *list,
364                                gpointer       data);
365 GSList *g_slist_prepend       (GSList        *list,
366                                gpointer       data);
367 void    g_slist_free          (GSList        *list);
368 void    g_slist_free_1        (GSList        *list);
369 GSList *g_slist_copy          (GSList        *list);
370 GSList *g_slist_concat        (GSList        *list1,
371                                GSList        *list2);
372 void    g_slist_foreach       (GSList        *list,
373                                GFunc          func,
374                                gpointer       user_data);
375 GSList *g_slist_last          (GSList        *list);
376 GSList *g_slist_find          (GSList        *list,
377                                gconstpointer  data);
378 GSList *g_slist_find_custom   (GSList        *list,
379                                gconstpointer  data,
380                                GCompareFunc   func);
381 GSList *g_slist_remove        (GSList        *list,
382                                gconstpointer  data);
383 GSList *g_slist_remove_all    (GSList        *list,
384                                gconstpointer  data);
385 GSList *g_slist_reverse       (GSList        *list);
386 guint   g_slist_length        (GSList        *list);
387 GSList *g_slist_remove_link   (GSList        *list,
388                                GSList        *link);
389 GSList *g_slist_delete_link   (GSList        *list,
390                                GSList        *link);
391 GSList *g_slist_insert_sorted (GSList        *list,
392                                gpointer       data,
393                                GCompareFunc   func);
394 GSList *g_slist_insert_before (GSList        *list,
395                                GSList        *sibling,
396                                gpointer       data);
397 GSList *g_slist_sort          (GSList        *list,
398                                GCompareFunc   func);
399 gint    g_slist_index         (GSList        *list,
400                                gconstpointer  data);
401 GSList *g_slist_nth           (GSList        *list,
402                                guint          n);
403 gpointer g_slist_nth_data     (GSList        *list,
404                                guint          n);
405
406 #define g_slist_next(slist) ((slist) ? (((GSList *) (slist))->next) : NULL)
407
408
409 typedef struct _GList GList;
410 struct _GList {
411   gpointer data;
412   GList *next;
413   GList *prev;
414 };
415
416 #define g_list_next(list) ((list) ? (((GList *) (list))->next) : NULL)
417 #define g_list_previous(list) ((list) ? (((GList *) (list))->prev) : NULL)
418
419 GList *g_list_alloc         (void);
420 GList *g_list_append        (GList         *list,
421                              gpointer       data);
422 GList *g_list_prepend       (GList         *list,
423                              gpointer       data);
424 void   g_list_free          (GList         *list);
425 void   g_list_free_1        (GList         *list);
426 GList *g_list_copy          (GList         *list);
427 guint  g_list_length        (GList         *list);
428 gint   g_list_index         (GList         *list,
429                              gconstpointer  data);
430 GList *g_list_nth           (GList         *list,
431                              guint          n);
432 gpointer g_list_nth_data      (GList         *list,
433                              guint          n);
434 GList *g_list_last          (GList         *list);
435 GList *g_list_concat        (GList         *list1,
436                              GList         *list2);
437 void   g_list_foreach       (GList         *list,
438                              GFunc          func,
439                              gpointer       user_data);
440 GList *g_list_first         (GList         *list);
441 GList *g_list_find          (GList         *list,
442                              gconstpointer  data);
443 GList *g_list_find_custom   (GList         *list,
444                              gconstpointer  data,
445                              GCompareFunc   func);
446 GList *g_list_remove        (GList         *list,
447                              gconstpointer  data);
448 GList *g_list_reverse       (GList         *list);
449 GList *g_list_remove_link   (GList         *list,
450                              GList         *link);
451 GList *g_list_delete_link   (GList         *list,
452                              GList         *link);
453 GList *g_list_insert_sorted (GList         *list,
454                              gpointer       data,
455                              GCompareFunc   func);
456 GList *g_list_insert_before (GList         *list,
457                              GList         *sibling,
458                              gpointer       data);
459 GList *g_list_sort          (GList         *sort,
460                              GCompareFunc   func);
461
462 /*
463  * ByteArray
464  */
465
466 typedef struct _GByteArray GByteArray;
467 struct _GByteArray {
468         guint8 *data;
469         gint len;
470 };
471
472 GByteArray *g_byte_array_new    (void);
473 GByteArray* g_byte_array_append (GByteArray *array, const guint8 *data, guint len);
474 guint8*  g_byte_array_free      (GByteArray *array, gboolean free_segment);
475
476 /*
477  * Array
478  */
479
480 typedef struct _GArray GArray;
481 struct _GArray {
482         gchar *data;
483         gint len;
484 };
485
486 GArray *g_array_new               (gboolean zero_terminated, gboolean clear_, guint element_size);
487 gchar*  g_array_free              (GArray *array, gboolean free_segment);
488 GArray *g_array_append_vals       (GArray *array, gconstpointer data, guint len);
489 GArray* g_array_insert_vals       (GArray *array, guint index_, gconstpointer data, guint len);
490 GArray* g_array_remove_index      (GArray *array, guint index_);
491 GArray* g_array_remove_index_fast (GArray *array, guint index_);
492
493 #define g_array_append_val(a,v)   (g_array_append_vals((a),&(v),1))
494 #define g_array_insert_val(a,i,v) (g_array_insert_vals((a),(i),&(v),1))
495 #define g_array_index(a,t,i)      *(t*)(((a)->data) + sizeof(t) * (i))
496
497 /*
498  * QSort
499 */
500
501 void g_qsort_with_data (gpointer base, size_t nmemb, size_t size, GCompareDataFunc compare, gpointer user_data);
502
503 /*
504  * Pointer Array
505  */
506
507 typedef struct _GPtrArray GPtrArray;
508 struct _GPtrArray {
509         gpointer *pdata;
510         guint len;
511 };
512
513 GPtrArray *g_ptr_array_new                (void);
514 GPtrArray *g_ptr_array_sized_new          (guint reserved_size);
515 void       g_ptr_array_add                (GPtrArray *array, gpointer data);
516 gboolean   g_ptr_array_remove             (GPtrArray *array, gpointer data);
517 gpointer   g_ptr_array_remove_index       (GPtrArray *array, guint index);
518 gboolean   g_ptr_array_remove_fast        (GPtrArray *array, gpointer data);
519 gpointer   g_ptr_array_remove_index_fast  (GPtrArray *array, guint index);
520 void       g_ptr_array_sort               (GPtrArray *array, GCompareFunc compare_func);
521 void       g_ptr_array_sort_with_data     (GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data);
522 void       g_ptr_array_set_size           (GPtrArray *array, gint length);
523 gpointer  *g_ptr_array_free               (GPtrArray *array, gboolean free_seg);
524 void       g_ptr_array_foreach            (GPtrArray *array, GFunc func, gpointer user_data);
525 #define    g_ptr_array_index(array,index) (array)->pdata[(index)]
526
527 /*
528  * Queues
529  */
530 typedef struct {
531         GList *head;
532         GList *tail;
533         guint length;
534 } GQueue;
535
536 gpointer g_queue_pop_head  (GQueue   *queue);
537 void     g_queue_push_head (GQueue   *queue,
538                             gpointer  data);
539 void     g_queue_push_tail (GQueue   *queue,
540                             gpointer  data);
541 gboolean g_queue_is_empty  (GQueue   *queue);
542 GQueue  *g_queue_new       (void);
543 void     g_queue_free      (GQueue   *queue);
544 void     g_queue_foreach   (GQueue   *queue, GFunc func, gpointer user_data);
545
546 /*
547  * Messages
548  */
549 #ifndef G_LOG_DOMAIN
550 #define G_LOG_DOMAIN ((gchar*) 0)
551 #endif
552
553 typedef enum {
554         G_LOG_FLAG_RECURSION          = 1 << 0,
555         G_LOG_FLAG_FATAL              = 1 << 1,
556         
557         G_LOG_LEVEL_ERROR             = 1 << 2,
558         G_LOG_LEVEL_CRITICAL          = 1 << 3,
559         G_LOG_LEVEL_WARNING           = 1 << 4,
560         G_LOG_LEVEL_MESSAGE           = 1 << 5,
561         G_LOG_LEVEL_INFO              = 1 << 6,
562         G_LOG_LEVEL_DEBUG             = 1 << 7,
563         
564         G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
565 } GLogLevelFlags;
566
567 void           g_print                (const gchar *format, ...);
568 void           g_printerr             (const gchar *format, ...);
569 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
570 GLogLevelFlags g_log_set_fatal_mask   (const gchar *log_domain, GLogLevelFlags fatal_mask);
571 void           g_logv                 (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args);
572 void           g_log                  (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...);
573 void           g_assertion_message    (const gchar *format, ...) G_GNUC_NORETURN;
574
575 #ifdef HAVE_C99_SUPPORT
576 /* The for (;;) tells gc thats g_error () doesn't return, avoiding warnings */
577 #define g_error(format, ...)    do { g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, __VA_ARGS__); for (;;); } while (0)
578 #define g_critical(format, ...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, __VA_ARGS__)
579 #define g_warning(format, ...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, __VA_ARGS__)
580 #define g_message(format, ...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, __VA_ARGS__)
581 #define g_debug(format, ...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, __VA_ARGS__)
582 #else   /* HAVE_C99_SUPPORT */
583 #define g_error(...)    do { g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, __VA_ARGS__); for (;;); } while (0)
584 #define g_critical(...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, __VA_ARGS__)
585 #define g_warning(...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, __VA_ARGS__)
586 #define g_message(...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, __VA_ARGS__)
587 #define g_debug(...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, __VA_ARGS__)
588 #endif  /* ndef HAVE_C99_SUPPORT */
589 #define g_log_set_handler(a,b,c,d)
590
591 #define G_GNUC_INTERNAL
592
593 /*
594  * Conversions
595  */
596
597 gpointer g_convert_error_quark(void);
598
599
600 /*
601  * Unicode Manipulation: most of this is not used by Mono by default, it is
602  * only used if the old collation code is activated, so this is only the
603  * bare minimum to build.
604  */
605 typedef guint32 gunichar;
606
607 typedef enum {
608         G_UNICODE_CONTROL,
609         G_UNICODE_FORMAT,
610         G_UNICODE_UNASSIGNED,
611         G_UNICODE_PRIVATE_USE,
612         G_UNICODE_SURROGATE,
613         G_UNICODE_LOWERCASE_LETTER,
614         G_UNICODE_MODIFIER_LETTER,
615         G_UNICODE_OTHER_LETTER,
616         G_UNICODE_TITLECASE_LETTER,
617         G_UNICODE_UPPERCASE_LETTER,
618         G_UNICODE_COMBINING_MARK,
619         G_UNICODE_ENCLOSING_MARK,
620         G_UNICODE_NON_SPACING_MARK,
621         G_UNICODE_DECIMAL_NUMBER,
622         G_UNICODE_LETTER_NUMBER,
623         G_UNICODE_OTHER_NUMBER,
624         G_UNICODE_CONNECT_PUNCTUATION,
625         G_UNICODE_DASH_PUNCTUATION,
626         G_UNICODE_CLOSE_PUNCTUATION,
627         G_UNICODE_FINAL_PUNCTUATION,
628         G_UNICODE_INITIAL_PUNCTUATION,
629         G_UNICODE_OTHER_PUNCTUATION,
630         G_UNICODE_OPEN_PUNCTUATION,
631         G_UNICODE_CURRENCY_SYMBOL,
632         G_UNICODE_MODIFIER_SYMBOL,
633         G_UNICODE_MATH_SYMBOL,
634         G_UNICODE_OTHER_SYMBOL,
635         G_UNICODE_LINE_SEPARATOR,
636         G_UNICODE_PARAGRAPH_SEPARATOR,
637         G_UNICODE_SPACE_SEPARATOR
638 } GUnicodeType;
639
640 gunichar       g_unichar_toupper (gunichar c);
641 gunichar       g_unichar_tolower (gunichar c);
642 gunichar       g_unichar_totitle (gunichar c);
643 GUnicodeType   g_unichar_type    (gunichar c);
644 gboolean       g_unichar_isxdigit (gunichar c);
645 gint           g_unichar_xdigit_value (gunichar c);
646
647 #ifndef MAX
648 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
649 #endif
650
651 #ifndef MIN
652 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
653 #endif
654
655 #ifndef CLAMP
656 #define CLAMP(a,low,high) (((a) < (low)) ? (low) : (((a) > (high)) ? (high) : (a)))
657 #endif
658
659 #if defined(__GNUC__) && (__GNUC__ > 2)
660 #define G_LIKELY(expr) (__builtin_expect ((expr) != 0, 1))
661 #define G_UNLIKELY(expr) (__builtin_expect ((expr) != 0, 0))
662 #else
663 #define G_LIKELY(x) (x)
664 #define G_UNLIKELY(x) (x)
665 #endif
666
667 #define  g_assert(x)     G_STMT_START { if (G_UNLIKELY (!(x))) g_assertion_message ("* Assertion at %s:%d, condition `%s' not met\n", __FILE__, __LINE__, #x);  } G_STMT_END
668 #define  g_assert_not_reached() G_STMT_START { g_assertion_message ("* Assertion: should not be reached at %s:%d\n", __FILE__, __LINE__); } G_STMT_END
669
670 /*
671  * Unicode conversion
672  */
673
674 #define G_CONVERT_ERROR g_convert_error_quark()
675
676 typedef enum {
677         G_CONVERT_ERROR_NO_CONVERSION,
678         G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
679         G_CONVERT_ERROR_FAILED,
680         G_CONVERT_ERROR_PARTIAL_INPUT,
681         G_CONVERT_ERROR_BAD_URI,
682         G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
683 } GConvertError;
684
685 gchar* g_utf8_strup (const gchar *str, gssize len);
686 gchar* g_utf8_strdown (const gchar *str, gssize len);
687 gunichar2 *g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **error);
688 gchar     *g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **error);
689 gunichar2 *g_ucs4_to_utf16 (const gunichar *str, glong len, glong *items_read, glong *items_written, GError **error);
690 gunichar  *g_utf16_to_ucs4 (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **error);
691
692 #define u8to16(str) g_utf8_to_utf16(str, (glong)strlen(str), NULL, NULL, NULL)
693
694 #ifdef G_OS_WIN32
695 #define u16to8(str) g_utf16_to_utf8((gunichar2 *) (str), (glong)wcslen((wchar_t *) (str)), NULL, NULL, NULL)
696 #else
697 #define u16to8(str) g_utf16_to_utf8(str, (glong)strlen(str), NULL, NULL, NULL)
698 #endif
699
700 /*
701  * Path
702  */
703 gchar  *g_build_path           (const gchar *separator, const gchar *first_element, ...);
704 #define g_build_filename(x, ...) g_build_path(G_DIR_SEPARATOR_S, x, __VA_ARGS__)
705 gchar  *g_path_get_dirname     (const gchar *filename);
706 gchar  *g_path_get_basename    (const char *filename);
707 gchar  *g_find_program_in_path (const gchar *program);
708 gchar  *g_get_current_dir      (void);
709 gboolean g_path_is_absolute    (const char *filename);
710
711 const gchar *g_get_home_dir    (void);
712 const gchar *g_get_tmp_dir     (void);
713 const gchar *g_get_user_name   (void);
714 gchar *g_get_prgname           (void);
715 void  g_set_prgname            (const gchar *prgname);
716
717 /*
718  * Shell
719  */
720
721 gboolean  g_shell_parse_argv (const gchar *command_line, gint *argcp, gchar ***argvp, GError **error);
722 gchar    *g_shell_unquote    (const gchar *quoted_string, GError **error);
723 gchar    *g_shell_quote      (const gchar *unquoted_string);
724
725 /*
726  * Spawn
727  */
728 typedef enum {
729         G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1,
730         G_SPAWN_DO_NOT_REAP_CHILD      = 1 << 1,
731         G_SPAWN_SEARCH_PATH            = 1 << 2,
732         G_SPAWN_STDOUT_TO_DEV_NULL     = 1 << 3,
733         G_SPAWN_STDERR_TO_DEV_NULL     = 1 << 4,
734         G_SPAWN_CHILD_INHERITS_STDIN   = 1 << 5,
735         G_SPAWN_FILE_AND_ARGV_ZERO     = 1 << 6
736 } GSpawnFlags;
737
738 typedef void (*GSpawnChildSetupFunc) (gpointer user_data);
739
740 gboolean g_spawn_command_line_sync (const gchar *command_line, gchar **standard_output, gchar **standard_error, gint *exit_status, GError **error);
741 gboolean g_spawn_async_with_pipes  (const gchar *working_directory, gchar **argv, gchar **envp, GSpawnFlags flags, GSpawnChildSetupFunc child_setup,
742                                 gpointer user_data, GPid *child_pid, gint *standard_input, gint *standard_output, gint *standard_error, GError **error);
743
744
745 /*
746  * Timer
747  */
748 typedef struct _GTimer GTimer;
749
750 GTimer *g_timer_new (void);
751 void g_timer_destroy (GTimer *timer);
752 gdouble g_timer_elapsed (GTimer *timer, gulong *microseconds);
753 void g_timer_stop (GTimer *timer);
754 void g_timer_start (GTimer *timer);
755
756 /*
757  * Date and time
758  */
759 typedef struct {
760         glong tv_sec;
761         glong tv_usec;
762 } GTimeVal;
763
764 void g_get_current_time (GTimeVal *result);
765 void g_usleep (gulong microseconds);
766
767 /*
768  * File
769  */
770
771 typedef enum {
772         G_FILE_ERROR_EXIST,
773         G_FILE_ERROR_ISDIR,
774         G_FILE_ERROR_ACCES,
775         G_FILE_ERROR_NAMETOOLONG,
776         G_FILE_ERROR_NOENT,
777         G_FILE_ERROR_NOTDIR,
778         G_FILE_ERROR_NXIO,
779         G_FILE_ERROR_NODEV,
780         G_FILE_ERROR_ROFS,
781         G_FILE_ERROR_TXTBSY,
782         G_FILE_ERROR_FAULT,
783         G_FILE_ERROR_LOOP,
784         G_FILE_ERROR_NOSPC,
785         G_FILE_ERROR_NOMEM,
786         G_FILE_ERROR_MFILE,
787         G_FILE_ERROR_NFILE,
788         G_FILE_ERROR_BADF,
789         G_FILE_ERROR_INVAL,
790         G_FILE_ERROR_PIPE,
791         G_FILE_ERROR_AGAIN,
792         G_FILE_ERROR_INTR,
793         G_FILE_ERROR_IO,
794         G_FILE_ERROR_PERM,
795         G_FILE_ERROR_NOSYS,
796         G_FILE_ERROR_FAILED
797 } GFileError;
798
799 typedef enum {
800         G_FILE_TEST_IS_REGULAR = 1 << 0,
801         G_FILE_TEST_IS_SYMLINK = 1 << 1,
802         G_FILE_TEST_IS_DIR = 1 << 2,
803         G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
804         G_FILE_TEST_EXISTS = 1 << 4
805 } GFileTest;
806
807
808 gboolean   g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error);
809 GFileError g_file_error_from_errno (gint err_no);
810 gint       g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error);
811 gboolean   g_file_test (const gchar *filename, GFileTest test);
812
813 #define g_open open
814 #define g_rename rename
815 #define g_stat stat
816 #define g_unlink unlink
817
818 /*
819  * Pattern matching
820  */
821 typedef struct _GPatternSpec GPatternSpec;
822 GPatternSpec * g_pattern_spec_new (const gchar *pattern);
823 void           g_pattern_spec_free (GPatternSpec *pspec);
824 gboolean       g_pattern_match_string (GPatternSpec *pspec, const gchar *string);
825
826 /*
827  * Directory
828  */
829 typedef struct _GDir GDir;
830 GDir        *g_dir_open (const gchar *path, guint flags, GError **error);
831 const gchar *g_dir_read_name (GDir *dir);
832 void         g_dir_rewind (GDir *dir);
833 void         g_dir_close (GDir *dir);
834 #define g_mkdir mkdir
835
836 /*
837  * GMarkup
838  */
839 typedef struct _GMarkupParseContext GMarkupParseContext;
840
841 typedef enum
842 {
843         G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
844         G_MARKUP_TREAT_CDATA_AS_TEXT              = 1 << 1
845 } GMarkupParseFlags;
846
847 typedef struct {
848         void (*start_element)  (GMarkupParseContext *context,
849                                 const gchar *element_name,
850                                 const gchar **attribute_names,
851                                 const gchar **attribute_values,
852                                 gpointer user_data,
853                                 GError **error);
854
855         void (*end_element)    (GMarkupParseContext *context,
856                                 const gchar         *element_name,
857                                 gpointer             user_data,
858                                 GError             **error);
859         
860         void (*text)           (GMarkupParseContext *context,
861                                 const gchar         *text,
862                                 gsize                text_len,  
863                                 gpointer             user_data,
864                                 GError             **error);
865         
866         void (*passthrough)    (GMarkupParseContext *context,
867                                 const gchar         *passthrough_text,
868                                 gsize                text_len,  
869                                 gpointer             user_data,
870                                 GError             **error);
871         void (*error)          (GMarkupParseContext *context,
872                                 GError              *error,
873                                 gpointer             user_data);
874 } GMarkupParser;
875
876 GMarkupParseContext *g_markup_parse_context_new   (const GMarkupParser *parser,
877                                                    GMarkupParseFlags flags,
878                                                    gpointer user_data,
879                                                    GDestroyNotify user_data_dnotify);
880 void                 g_markup_parse_context_free  (GMarkupParseContext *context);
881 gboolean             g_markup_parse_context_parse (GMarkupParseContext *context,
882                                                    const gchar *text, gssize text_len,
883                                                    GError **error);
884 gboolean         g_markup_parse_context_end_parse (GMarkupParseContext *context,
885                                                    GError **error);
886
887 /*
888  * Character set conversion
889  */
890 /*
891 * Index into the table below with the first byte of a UTF-8 sequence to
892 * get the number of trailing bytes that are supposed to follow it.
893 * Note that *legal* UTF-8 values can't have 4 or 5-bytes. The table is
894 * left as-is for anyone who may want to do such conversion, which was
895 * allowed in earlier algorithms.
896 */
897 extern const gchar g_trailingBytesForUTF8[256];
898
899 gboolean  g_get_charset        (G_CONST_RETURN char **charset);
900 gchar    *g_locale_to_utf8     (const gchar *opsysstring, gssize len,
901                                 gsize *bytes_read, gsize *bytes_written,
902                                 GError **error);
903 gchar    *g_locale_from_utf8   (const gchar *utf8string, gssize len, gsize *bytes_read,
904                                 gsize *bytes_written, GError **error);
905 gchar    *g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read,
906                                 gsize *bytes_written, GError **error);
907 gchar    *g_convert            (const gchar *str, gssize len,
908                                 const gchar *to_codeset, const gchar *from_codeset,
909                                 gsize *bytes_read, gsize *bytes_written, GError **error);
910 gboolean  g_utf8_validate      (const gchar *str, gssize max_len, const gchar **end);
911 gunichar  g_utf8_get_char      (const gchar *src);
912 glong     g_utf8_strlen        (const gchar *str, gssize max);
913 #define   g_utf8_next_char(p) p + (g_trailingBytesForUTF8[(guchar)(*p)] + 1)
914
915 /*
916  * Empty thread functions, not used by eglib
917  */
918 #define g_thread_supported()   TRUE
919 #define g_thread_init(x)       G_STMT_START { if (x != NULL) { g_error ("No vtable supported in g_thread_init"); } } G_STMT_END
920
921 #define G_LOCK_DEFINE(name)        int name;
922 #define G_LOCK_DEFINE_STATIC(name) static int name;
923 #define G_LOCK_EXTERN(name)
924 #define G_LOCK(name)
925 #define G_TRYLOCK(name)
926 #define G_UNLOCK(name)
927
928 #define GUINT16_SWAP_LE_BE_CONSTANT(x) ((((guint16) x) >> 8) | ((((guint16) x) << 8)))
929
930 #define GUINT16_SWAP_LE_BE(x) ((guint16) (((guint16) x) >> 8) | ((((guint16)(x)) & 0xff) << 8))
931 #define GUINT32_SWAP_LE_BE(x) ((guint32) \
932                                ( (((guint32) (x)) << 24)| \
933                                  ((((guint32) (x)) & 0xff0000) >> 8) | \
934                                  ((((guint32) (x)) & 0xff00) << 8) | \
935                                  (((guint32) (x)) >> 24)) )
936  
937 #define GUINT64_SWAP_LE_BE(x) ((guint64) (((guint64)(GUINT32_SWAP_LE_BE(((guint64)x) & 0xffffffff))) << 32) | \
938                                GUINT32_SWAP_LE_BE(((guint64)x) >> 32))
939
940                                   
941  
942 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
943 #   define GUINT32_TO_LE(x) (x)
944 #   define GUINT64_TO_LE(x) (x)
945 #   define GUINT16_TO_LE(x) (x)
946 #   define GUINT_TO_LE(x)   (x)
947 #   define GUINT32_TO_BE(x) GUINT32_SWAP_LE_BE(x)
948 #   define GUINT16_FROM_BE(x) GUINT16_SWAP_LE_BE(x)
949 #   define GUINT32_FROM_BE(x) GUINT32_SWAP_LE_BE(x)
950 #   define GUINT64_FROM_BE(x) GUINT64_SWAP_LE_BE(x)
951 #   define GINT16_FROM_BE(x) GUINT16_SWAP_LE_BE(x)
952 #   define GINT32_FROM_BE(x) GUINT32_SWAP_LE_BE(x)
953 #   define GINT64_FROM_BE(x) GUINT64_SWAP_LE_BE(x)
954 #else
955 #   define GUINT32_TO_LE(x) GUINT32_SWAP_LE_BE(x)
956 #   define GUINT64_TO_LE(x) GUINT64_SWAP_LE_BE(x)
957 #   define GUINT16_TO_LE(x) GUINT16_SWAP_LE_BE(x)
958 #   define GUINT_TO_LE(x)   GUINT32_SWAP_LE_BE(x)
959 #   define GUINT32_TO_BE(x) (x)
960 #   define GUINT16_FROM_BE(x) (x)
961 #   define GUINT32_FROM_BE(x) (x)
962 #   define GUINT64_FROM_BE(x) (x)
963 #   define GINT16_FROM_BE(x) (x)
964 #   define GINT32_FROM_BE(x) (x)
965 #   define GINT64_FROM_BE(x) (x)
966 #endif
967
968 #define GINT64_FROM_LE(x)   (GUINT64_TO_LE (x))
969 #define GINT32_FROM_LE(x)   (GUINT32_TO_LE (x))
970 #define GINT16_FROM_LE(x)   (GUINT16_TO_LE (x))
971
972 #define GUINT32_FROM_LE(x)  (GUINT32_TO_LE (x))
973 #define GUINT64_FROM_LE(x)  (GUINT64_TO_LE (x))
974 #define GUINT16_FROM_LE(x)  (GUINT16_TO_LE (x))
975 #define GUINT_FROM_LE(x)    (GUINT_TO_LE (x))
976
977 #define _EGLIB_MAJOR  2
978 #define _EGLIB_MIDDLE 4
979 #define _EGLIB_MINOR  0
980  
981 #define GLIB_CHECK_VERSION(a,b,c) ((a < _EGLIB_MAJOR) || (a == _EGLIB_MAJOR && (b < _EGLIB_MIDDLE || (b == _EGLIB_MIDDLE && c <= _EGLIB_MINOR))))
982  
983 G_END_DECLS
984
985 #endif
986
987
988