2007-10-19 Nagappan A <anagappan@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 <stddef.h>
9 #include <ctype.h>
10
11 #ifdef _MSC_VER
12 #pragma include_alias(<eglib-config.h>, <eglib-config.hw>)
13 #else
14 #include <stdint.h>
15 #endif
16
17 #include <eglib-config.h>
18
19 #ifndef offsetof
20 #   define offsetof(s_name,n_name) (size_t)(char *)&(((s_name*)0)->m_name)
21 #endif
22
23 #define __EGLIB_X11 1
24
25 /*
26  * Basic data types
27  */
28 typedef int            gboolean;
29 typedef int            gint;
30 typedef unsigned int   guint;
31 typedef short          gshort;
32 typedef unsigned short gushort;
33 typedef long           glong;
34 typedef unsigned long  gulong;
35 typedef void *         gpointer;
36 typedef const void *   gconstpointer;
37 typedef char           gchar;
38 typedef unsigned char  guchar;
39
40 #ifdef _MSC_VER
41 typedef __int8                          gint8;
42 typedef unsigned __int8         guint8;
43 typedef __int16                         gint16;
44 typedef unsigned __int16        guint16;
45 typedef __int32                         gint32;
46 typedef unsigned __int32        guint32;
47 typedef __int64                         gint64;
48 typedef unsigned __int64        guint64;
49 typedef float                           gfloat;
50 typedef double                          gdouble;
51 typedef unsigned __int16        gunichar2;
52 #else
53 /* Types defined in terms of the stdint.h */
54 typedef int8_t         gint8;
55 typedef uint8_t        guint8;
56 typedef int16_t        gint16;
57 typedef uint16_t       guint16;
58 typedef int32_t        gint32;
59 typedef uint32_t       guint32;
60 typedef int64_t        gint64;
61 typedef uint64_t       guint64;
62 typedef float          gfloat;
63 typedef double         gdouble;
64 typedef uint16_t       gunichar2;
65 #endif
66
67 /*
68  * Macros
69  */
70 #define G_N_ELEMENTS(s)      (sizeof(s) / sizeof ((s) [0]))
71
72 #define FALSE                0
73 #define TRUE                 1
74
75 #define G_MAXINT32           INT32_MAX
76 #define G_MININT32           INT32_MIN
77 #define G_MININT64           INT64_MIN
78 #define G_MAXINT64           INT64_MAX
79
80 #define G_LITTLE_ENDIAN 1234
81 #define G_BIG_ENDIAN    4321
82 #define G_STMT_START    do 
83 #define G_STMT_END      while (0)
84
85 #define G_USEC_PER_SEC  1000000
86
87 #define ABS(a)         ((a) > 0 ? (a) : -(a))
88
89 #define G_STRUCT_OFFSET(p_type,field) offsetof(p_type,field)
90
91 #define EGLIB_STRINGIFY(x) #x
92 #define EGLIB_TOSTRING(x) EGLIB_STRINGIFY(x)
93 #define G_STRLOC __FILE__ ":" EGLIB_TOSTRING(__LINE__) ":"
94
95 #define G_BEGIN_DECLS
96 #define G_END_DECLS
97  
98 #define G_CONST_RETURN const
99
100 /*
101  * Allocation
102  */
103 #define g_new(type,size)        ((type *) malloc (sizeof (type) * (size)))
104 #define g_new0(type,size)       ((type *) calloc (sizeof (type), (size)))
105 #define g_newa(type,size)       ((type *) alloca (sizeof (type) * (size)))
106 #define g_realloc(obj,size)     realloc((obj), (size))
107 #define g_malloc(x)             malloc(x)
108 #define g_try_malloc(x)         malloc(x)
109 #define g_try_realloc(obj,size) realloc((obj),(size))
110 #define g_malloc0(x)            calloc(1,x)
111 #define g_memmove(dest,src,len) memmove (dest, src, len)
112 #define g_renew(struct_type, mem, n_structs) realloc (mem, sizeof (struct_type) * n_structs)
113 #define g_alloca(size)          alloca (size)
114 #define g_free                  free
115
116 gpointer g_memdup (gconstpointer mem, guint byte_size);
117 gchar   *g_strdup (const gchar *str);
118
119 typedef struct {
120         gpointer (*malloc)      (gsize    n_bytes);
121         gpointer (*realloc)     (gpointer mem, gsize n_bytes);
122         void     (*free)        (gpointer mem);
123         gpointer (*calloc)      (gsize    n_blocks, gsize n_block_bytes);
124         gpointer (*try_malloc)  (gsize    n_bytes);
125         gpointer (*try_realloc) (gpointer mem, gsize n_bytes);
126 } GMemVTable;
127
128 #define g_mem_set_vtable(x)
129 /*
130  * Misc.
131  */
132 #define g_atexit(func)  ((void) atexit (func))
133
134 const gchar *    g_getenv(const gchar *variable);
135 gboolean         g_setenv(const gchar *variable, const gchar *value, gboolean overwrite);
136 void             g_unsetenv(const gchar *variable);
137
138 gchar*           g_win32_getlocale(void);
139
140 /*
141  * Precondition macros
142  */
143 #define g_return_if_fail(x)  G_STMT_START { if (!(x)) { printf ("%s:%d: assertion '%s' failed", __FILE__, __LINE__, #x); return; } } G_STMT_END
144 #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
145
146 /*
147  * Hashtables
148  */
149 typedef struct _GHashTable GHashTable;
150 typedef void     (*GFunc)          (gpointer data, gpointer user_data);
151 typedef gint     (*GCompareFunc)   (gconstpointer a, gconstpointer b);
152 typedef gint     (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
153 typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
154 typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
155 typedef void     (*GDestroyNotify) (gpointer data);
156 typedef guint    (*GHashFunc)      (gconstpointer key);
157 typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
158 typedef void     (*GFreeFunc)      (gpointer       data);
159
160 GHashTable     *g_hash_table_new             (GHashFunc hash_func, GEqualFunc key_equal_func);
161 GHashTable     *g_hash_table_new_full        (GHashFunc hash_func, GEqualFunc key_equal_func,
162                                               GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func);
163 void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
164 guint           g_hash_table_size            (GHashTable *hash);
165 gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
166 gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
167 void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
168 gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
169 gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
170 guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
171 void            g_hash_table_destroy         (GHashTable *hash);
172
173 guint           g_spaced_primes_closest      (guint x);
174
175 #define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
176 #define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
177
178 gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
179 guint    g_direct_hash  (gconstpointer v1);
180 gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
181 guint    g_int_hash     (gconstpointer v1);
182 gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
183 guint    g_str_hash     (gconstpointer v1);
184
185 #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
186 #define  g_assert_not_reached() G_STMT_START { g_error ("* Assertion: should not be reached at %s:%d\n", __FILE__, __LINE__); } G_STMT_END
187
188 /*
189  * Errors
190  */
191 typedef struct {
192         /* In the real glib, this is a GQuark, but we dont use/need that */
193         gpointer domain;
194         gint     code;
195         gchar   *message;
196 } GError;
197
198 void    g_error_free (GError *error);
199 GError *g_error_new  (gpointer domain, gint code, const char *format, ...);
200 void    g_set_error  (GError **err, gpointer domain, gint code, const gchar *format, ...);
201 void    g_propagate_error (GError **dest, GError *src);
202
203 /*
204  * Strings utility
205  */
206 gchar       *g_strdup_printf  (const gchar *format, ...);
207 gchar       *g_strdup_vprintf (const gchar *format, va_list args);
208 gchar       *g_strndup        (const gchar *str, gsize n);
209 const gchar *g_strerror       (gint errnum);
210 gchar       *g_strndup        (const gchar *str, gsize n);
211 void         g_strfreev       (gchar **str_array);
212 gchar       *g_strconcat      (const gchar *first, ...);
213 gchar      **g_strsplit       (const gchar *string, const gchar *delimiter, gint max_tokens);
214 gchar       *g_strreverse     (gchar *str);
215 gboolean     g_str_has_prefix (const gchar *str, const gchar *prefix);
216 gboolean     g_str_has_suffix (const gchar *str, const gchar *suffix);
217 guint        g_strv_length    (gchar **str_array);
218 gchar       *g_strjoin        (const gchar *separator, ...);
219 gchar       *g_strjoinv       (const gchar *separator, gchar **str_array);
220 gchar       *g_strchug        (gchar *str);
221 gchar       *g_strchomp       (gchar *str);
222 void         g_strdown        (gchar *string);
223
224 gchar       *g_strdelimit     (gchar *string, const gchar *delimiters, gchar new_delimiter);
225 gchar       *g_strescape      (const gchar *source, const gchar *exceptions);
226
227 gchar       *g_filename_to_uri   (const gchar *filename, const gchar *hostname, GError **error);
228 gchar       *g_filename_from_uri (const gchar *uri, gchar **hostname, GError **error);
229
230 gint         g_printf          (gchar const *format, ...);
231 gint         g_fprintf         (FILE *file, gchar const *format, ...);
232 gint         g_sprintf         (gchar *string, gchar const *format, ...);
233 gint         g_snprintf        (gchar *string, gulong n, gchar const *format, ...);
234 #define g_vprintf vprintf
235 #define g_vfprintf vfprintf
236 #define g_vsprintf vsprintf
237 #define g_vsnprintf vsnprintf
238 #define g_vasprintf vasprintf
239
240 gsize       g_strlcpy          (gchar *dest, const gchar *src, gsize dest_size);
241
242 gchar   g_ascii_tolower      (gchar c);
243 gchar  *g_ascii_strdown      (const gchar *str, gssize len);
244 gint    g_ascii_strncasecmp  (const gchar *s1, const gchar *s2, gsize n);
245 gint    g_ascii_xdigit_value (gchar c);
246 #define g_ascii_isspace(c)   (isspace (c) != 0)
247 #define g_ascii_isalpha(c)   (isalpha (c) != 0)
248 #define g_ascii_isprint(c)   (isprint (c) != 0)
249 #define g_ascii_isxdigit(c)  (isxdigit (c) != 0)
250
251 /* FIXME: g_strcasecmp supports utf8 unicode stuff */
252 #ifdef _MSC_VER
253 #define g_strcasecmp stricmp
254 #define g_ascii_strcasecmp stricmp
255 #define g_strncasecmp strnicmp
256 #define g_strstrip(a) g_strchug (g_strchomp (a))
257 #else
258 #define g_strcasecmp strcasecmp
259 #define g_ascii_strcasecmp strcasecmp
260 #define g_strncasecmp strncasecmp
261 #define g_strstrip(a) g_strchug (g_strchomp (a))
262 #endif
263
264
265 #define G_STR_DELIMITERS "_-|> <."
266
267 /*
268  * String type
269  */
270 typedef struct {
271         char *str;
272         gsize len;
273         gsize allocated_len;
274 } GString;
275
276 GString     *g_string_new           (const gchar *init);
277 GString     *g_string_new_len       (const gchar *init, gssize len);
278 GString     *g_string_sized_new     (gsize default_size);
279 gchar       *g_string_free          (GString *string, gboolean free_segment);
280 GString     *g_string_append        (GString *string, const gchar *val);
281 void         g_string_printf        (GString *string, const gchar *format, ...);
282 void         g_string_append_printf (GString *string, const gchar *format, ...);
283 GString     *g_string_append_c      (GString *string, gchar c);
284 GString     *g_string_append        (GString *string, const gchar *val);
285 GString     *g_string_append_len    (GString *string, const gchar *val, gssize len);
286 GString     *g_string_truncate      (GString *string, gsize len);
287 GString     *g_string_prepend       (GString *string, const gchar *val);
288
289 #define g_string_sprintfa g_string_append_printf
290
291 /*
292  * Lists
293  */
294 typedef struct _GSList GSList;
295 struct _GSList {
296         gpointer data;
297         GSList *next;
298 };
299
300 GSList *g_slist_alloc         (void);
301 GSList *g_slist_append        (GSList        *list,
302                                gpointer       data);
303 GSList *g_slist_prepend       (GSList        *list,
304                                gpointer       data);
305 void    g_slist_free          (GSList        *list);
306 void    g_slist_free_1        (GSList        *list);
307 GSList *g_slist_copy          (GSList        *list);
308 GSList *g_slist_concat        (GSList        *list1,
309                                GSList        *list2);
310 void    g_slist_foreach       (GSList        *list,
311                                GFunc          func,
312                                gpointer       user_data);
313 GSList *g_slist_last          (GSList        *list);
314 GSList *g_slist_find          (GSList        *list,
315                                gconstpointer  data);
316 GSList *g_slist_remove        (GSList        *list,
317                                gconstpointer  data);
318 GSList *g_slist_remove_all    (GSList        *list,
319                                gconstpointer  data);
320 GSList *g_slist_reverse       (GSList        *list);
321 guint   g_slist_length        (GSList        *list);
322 GSList *g_slist_remove_link   (GSList        *list,
323                                GSList        *link);
324 GSList *g_slist_delete_link   (GSList        *list,
325                                GSList        *link);
326 GSList *g_slist_insert_sorted (GSList        *list,
327                                gpointer       data,
328                                GCompareFunc   func);
329 GSList *g_slist_insert_before (GSList        *list,
330                                GSList        *sibling,
331                                gpointer       data);
332 GSList *g_slist_sort          (GSList        *list,
333                                GCompareFunc   func);
334 #define g_slist_next(slist) ((slist) ? (((GSList *) (slist))->next) : NULL)
335
336 typedef struct _GList GList;
337 struct _GList {
338   gpointer data;
339   GList *next;
340   GList *prev;
341 };
342
343 #define g_list_next(list) ((list) ? (((GList *) (list))->next) : NULL)
344
345 GList *g_list_alloc         (void);
346 GList *g_list_append        (GList         *list,
347                              gpointer       data);
348 GList *g_list_prepend       (GList         *list,
349                              gpointer       data);
350 void   g_list_free          (GList         *list);
351 void   g_list_free_1        (GList         *list);
352 GList *g_list_copy          (GList         *list);
353 guint  g_list_length        (GList         *list);
354 gint   g_list_index         (GList         *list,
355                              gconstpointer  data);
356 GList *g_list_nth           (GList         *list,
357                              guint          n);
358 gpointer g_list_nth_data      (GList         *list,
359                              guint          n);
360 GList *g_list_last          (GList         *list);
361 GList *g_list_concat        (GList         *list1,
362                              GList         *list2);
363 void   g_list_foreach       (GList         *list,
364                              GFunc          func,
365                              gpointer       user_data);
366 GList *g_list_first         (GList         *list);
367 GList *g_list_find          (GList         *list,
368                              gconstpointer  data);
369 GList *g_list_remove        (GList         *list,
370                              gconstpointer  data);
371 GList *g_list_reverse       (GList         *list);
372 GList *g_list_remove_link   (GList         *list,
373                              GList         *link);
374 GList *g_list_delete_link   (GList         *list,
375                              GList         *link);
376 GList *g_list_insert_sorted (GList         *list,
377                              gpointer       data,
378                              GCompareFunc   func);
379 GList *g_list_insert_before (GList         *list,
380                              GList         *sibling,
381                              gpointer       data);
382 GList *g_list_sort          (GList         *sort,
383                              GCompareFunc   func);
384
385 /*
386  * Array
387  */
388
389 typedef struct _GArray GArray;
390 struct _GArray {
391         gchar *data;
392         gint len;
393 };
394
395 GArray *g_array_new               (gboolean zero_terminated, gboolean clear_, guint element_size);
396 gchar*  g_array_free              (GArray *array, gboolean free_segment);
397 GArray *g_array_append_vals       (GArray *array, gconstpointer data, guint len);
398 GArray* g_array_insert_vals       (GArray *array, guint index_, gconstpointer data, guint len);
399 GArray* g_array_remove_index      (GArray *array, guint index_);
400
401 #define g_array_append_val(a,v)   (g_array_append_vals((a),&(v),1))
402 #define g_array_insert_val(a,i,v) (g_array_insert_vals((a),(i),&(v),1))
403 #define g_array_index(a,t,i)      *(t*)(((a)->data) + sizeof(t) * (i))
404
405 /*
406  * Pointer Array
407  */
408
409 typedef struct _GPtrArray GPtrArray;
410 struct _GPtrArray {
411         gpointer *pdata;
412         guint len;
413 };
414
415 GPtrArray *g_ptr_array_new                (void);
416 GPtrArray *g_ptr_array_sized_new          (guint reserved_size);
417 void       g_ptr_array_add                (GPtrArray *array, gpointer data);
418 gboolean   g_ptr_array_remove             (GPtrArray *array, gpointer data);
419 gpointer   g_ptr_array_remove_index       (GPtrArray *array, guint index);
420 gboolean   g_ptr_array_remove_fast        (GPtrArray *array, gpointer data);
421 gpointer   g_ptr_array_remove_index_fast  (GPtrArray *array, gpointer data);
422 void       g_ptr_array_sort               (GPtrArray *array, GCompareFunc compare_func);
423 void       g_ptr_array_sort_with_data     (GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data);
424 void       g_ptr_array_set_size           (GPtrArray *array, gint length);
425 gpointer  *g_ptr_array_free               (GPtrArray *array, gboolean free_seg);
426 void       g_ptr_array_foreach            (GPtrArray *array, GFunc func, gpointer user_data);
427 #define    g_ptr_array_index(array,index) (array)->pdata[(index)]
428
429 /*
430  * Queues
431  */
432 typedef struct {
433         GList *head;
434         GList *tail;
435         guint length;
436 } GQueue;
437
438 gpointer g_queue_pop_head  (GQueue   *queue);
439 void     g_queue_push_head (GQueue   *queue,
440                             gpointer  data);
441 gboolean g_queue_is_empty  (GQueue   *queue);
442 GQueue  *g_queue_new       (void);
443 void     g_queue_free      (GQueue   *queue);
444
445 /*
446  * Messages
447  */
448 #ifndef G_LOG_DOMAIN
449 #define G_LOG_DOMAIN ((gchar*) 0)
450 #endif
451
452 typedef enum {
453         G_LOG_FLAG_RECURSION          = 1 << 0,
454         G_LOG_FLAG_FATAL              = 1 << 1,
455         
456         G_LOG_LEVEL_ERROR             = 1 << 2,
457         G_LOG_LEVEL_CRITICAL          = 1 << 3,
458         G_LOG_LEVEL_WARNING           = 1 << 4,
459         G_LOG_LEVEL_MESSAGE           = 1 << 5,
460         G_LOG_LEVEL_INFO              = 1 << 6,
461         G_LOG_LEVEL_DEBUG             = 1 << 7,
462         
463         G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
464 } GLogLevelFlags;
465
466 void           g_print                (const gchar *format, ...);
467 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
468 GLogLevelFlags g_log_set_fatal_mask   (const gchar *log_domain, GLogLevelFlags fatal_mask);
469 void           g_logv                 (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args);
470 void           g_log                  (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...);
471
472 #define g_error(format, ...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, __VA_ARGS__)
473 #define g_critical(format, ...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, __VA_ARGS__)
474 #define g_warning(format, ...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, __VA_ARGS__)
475 #define g_message(format, ...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, __VA_ARGS__)
476 #define g_debug(format, ...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format, __VA_ARGS__)
477
478 #define g_log_set_handler(a,b,c,d)
479 #define g_printerr(format, ...) fprintf (stderr, format, __VA_ARGS__)
480 /*
481  * Conversions
482  */
483
484 gpointer g_convert_error_quark(void);
485
486
487 /*
488  * Unicode Manipulation: most of this is not used by Mono by default, it is
489  * only used if the old collation code is activated, so this is only the
490  * bare minimum to build.
491  */
492 typedef guint32 gunichar;
493
494 typedef enum {
495         G_UNICODE_LOWERCASE_LETTER,
496 } GUnicodeType;
497
498 gunichar       g_unichar_tolower (gunichar c);
499 GUnicodeType   g_unichar_type    (gunichar c);
500 gboolean       g_unichar_isxdigit (gunichar c);
501 gint           g_unichar_xdigit_value (gunichar c);
502
503 #ifndef MAX
504 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
505 #endif
506
507 #ifndef MIN
508 #define MIN(a,b) (((a)<(b)) ? (a) : (b))
509 #endif
510
511 #ifndef CLAMP
512 #define CLAMP(a,low,high) (((a) < (low)) ? (low) : (((a) > (high)) ? (high) : (a)))
513 #endif
514
515 /* FIXME: Implement these two for gcc */
516 #define G_LIKELY(x) (x)
517 #define G_UNLIKELY(x) (x)
518
519 /*
520  * Unicode conversion
521  */
522
523 #define G_CONVERT_ERROR g_convert_error_quark()
524
525 typedef enum {
526         G_CONVERT_ERROR_NO_CONVERSION,
527         G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
528         G_CONVERT_ERROR_FAILED,
529         G_CONVERT_ERROR_PARTIAL_INPUT,
530         G_CONVERT_ERROR_BAD_URI,
531         G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
532 } GConvertError;
533
534 gunichar2 *g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong     *items_written, GError **error);
535 gchar     *g_utf16_to_utf8 (const gunichar2 *str, glong len, glong *items_read, glong     *items_written, GError **error);
536
537 #define u8to16(str) g_utf8_to_utf16(str, (glong)strlen(str), NULL, NULL, NULL)
538
539 #ifdef G_OS_WIN32
540 #define u16to8(str) g_utf16_to_utf8(str, (glong)wcslen(str), NULL, NULL, NULL)
541 #else
542 #define u16to8(str) g_utf16_to_utf8(str, (glong)strlen(str), NULL, NULL, NULL)
543 #endif
544
545 /*
546  * Path
547  */
548 gchar  *g_build_path           (const gchar *separator, const gchar *first_element, ...);
549 #define g_build_filename(x, ...) g_build_path(G_DIR_SEPARATOR_S, x, __VA_ARGS__)
550 gchar  *g_path_get_dirname     (const gchar *filename);
551 gchar  *g_path_get_basename    (const char *filename);
552 gchar  *g_find_program_in_path (const gchar *program);
553 gchar  *g_get_current_dir      (void);
554 gboolean g_path_is_absolute    (const char *filename);
555
556 const gchar *g_get_home_dir    (void);
557 const gchar *g_get_tmp_dir     (void);
558 const gchar *g_get_user_name   (void);
559 gchar *g_get_prgname           (void);
560 void  g_set_prgname            (const gchar *prgname);
561
562 /*
563  * Shell
564  */
565
566 gboolean  g_shell_parse_argv (const gchar *command_line, gint *argcp, gchar ***argvp, GError **error);
567 gchar    *g_shell_unquote    (const gchar *quoted_string, GError **error);
568 gchar    *g_shell_quote      (const gchar *unquoted_string);
569
570 /*
571  * Spawn
572  */
573 typedef enum {
574         G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1,
575         G_SPAWN_DO_NOT_REAP_CHILD      = 1 << 1,
576         G_SPAWN_SEARCH_PATH            = 1 << 2,
577         G_SPAWN_STDOUT_TO_DEV_NULL     = 1 << 3,
578         G_SPAWN_STDERR_TO_DEV_NULL     = 1 << 4,
579         G_SPAWN_CHILD_INHERITS_STDIN   = 1 << 5,
580         G_SPAWN_FILE_AND_ARGV_ZERO     = 1 << 6
581 } GSpawnFlags;
582
583 typedef pid_t GPid;
584
585 typedef void (*GSpawnChildSetupFunc) (gpointer user_data);
586
587 gboolean g_spawn_command_line_sync (const gchar *command_line, gchar **standard_output, gchar **standard_error, gint *exit_status, GError **error);
588 gboolean g_spawn_async_with_pipes  (const gchar *working_directory, gchar **argv, gchar **envp, GSpawnFlags flags, GSpawnChildSetupFunc child_setup,
589                                 gpointer user_data, GPid *child_pid, gint *standard_input, gint *standard_output, gint *standard_error, GError **error);
590
591
592 /*
593  * Timer
594  */
595 typedef struct _GTimer GTimer;
596
597 GTimer *g_timer_new (void);
598 void g_timer_destroy (GTimer *timer);
599 gdouble g_timer_elapsed (GTimer *timer, gulong *microseconds);
600 void g_timer_stop (GTimer *timer);
601 void g_timer_start (GTimer *timer);
602
603 /*
604  * Date and time
605  */
606 typedef struct {
607         glong tv_sec;
608         glong tv_usec;
609 } GTimeVal;
610
611 void g_get_current_time (GTimeVal *result);
612
613 /*
614  * File
615  */
616
617 typedef enum {
618         G_FILE_ERROR_EXIST,
619         G_FILE_ERROR_ISDIR,
620         G_FILE_ERROR_ACCES,
621         G_FILE_ERROR_NAMETOOLONG,
622         G_FILE_ERROR_NOENT,
623         G_FILE_ERROR_NOTDIR,
624         G_FILE_ERROR_NXIO,
625         G_FILE_ERROR_NODEV,
626         G_FILE_ERROR_ROFS,
627         G_FILE_ERROR_TXTBSY,
628         G_FILE_ERROR_FAULT,
629         G_FILE_ERROR_LOOP,
630         G_FILE_ERROR_NOSPC,
631         G_FILE_ERROR_NOMEM,
632         G_FILE_ERROR_MFILE,
633         G_FILE_ERROR_NFILE,
634         G_FILE_ERROR_BADF,
635         G_FILE_ERROR_INVAL,
636         G_FILE_ERROR_PIPE,
637         G_FILE_ERROR_AGAIN,
638         G_FILE_ERROR_INTR,
639         G_FILE_ERROR_IO,
640         G_FILE_ERROR_PERM,
641         G_FILE_ERROR_NOSYS,
642         G_FILE_ERROR_FAILED
643 } GFileError;
644
645 typedef enum {
646         G_FILE_TEST_IS_REGULAR = 1 << 0,
647         G_FILE_TEST_IS_SYMLINK = 1 << 1,
648         G_FILE_TEST_IS_DIR = 1 << 2,
649         G_FILE_TEST_IS_EXECUTABLE = 1 << 3,
650         G_FILE_TEST_EXISTS = 1 << 4
651 } GFileTest;
652
653
654 gboolean   g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error);
655 GFileError g_file_error_from_errno (gint err_no);
656 gint       g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error);
657 gboolean   g_file_test (const gchar *filename, GFileTest test);
658
659 /*
660  * Pattern matching
661  */
662 typedef struct _GPatternSpec GPatternSpec;
663 GPatternSpec * g_pattern_spec_new (const gchar *pattern);
664 void           g_pattern_spec_free (GPatternSpec *pspec);
665 gboolean       g_pattern_match_string (GPatternSpec *pspec, const gchar *string);
666
667 /*
668  * Directory
669  */
670 typedef struct _GDir GDir;
671 GDir        *g_dir_open (const gchar *path, guint flags, GError **error);
672 const gchar *g_dir_read_name (GDir *dir);
673 void         g_dir_rewind (GDir *dir);
674 void         g_dir_close (GDir *dir);
675
676 /*
677  * GMarkup
678  */
679 typedef struct _GMarkupParseContext GMarkupParseContext;
680
681 typedef enum
682 {
683         G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
684         G_MARKUP_TREAT_CDATA_AS_TEXT              = 1 << 1
685 } GMarkupParseFlags;
686
687 typedef struct {
688         void (*start_element)  (GMarkupParseContext *context,
689                                 const gchar *element_name,
690                                 const gchar **attribute_names,
691                                 const gchar **attribute_values,
692                                 gpointer user_data,
693                                 GError **error);
694
695         void (*end_element)    (GMarkupParseContext *context,
696                                 const gchar         *element_name,
697                                 gpointer             user_data,
698                                 GError             **error);
699         
700         void (*text)           (GMarkupParseContext *context,
701                                 const gchar         *text,
702                                 gsize                text_len,  
703                                 gpointer             user_data,
704                                 GError             **error);
705         
706         void (*passthrough)    (GMarkupParseContext *context,
707                                 const gchar         *passthrough_text,
708                                 gsize                text_len,  
709                                 gpointer             user_data,
710                                 GError             **error);
711         void (*error)          (GMarkupParseContext *context,
712                                 GError              *error,
713                                 gpointer             user_data);
714 } GMarkupParser;
715
716 GMarkupParseContext *g_markup_parse_context_new   (const GMarkupParser *parser,
717                                                    GMarkupParseFlags flags,
718                                                    gpointer user_data,
719                                                    GDestroyNotify user_data_dnotify);
720 void                 g_markup_parse_context_free  (GMarkupParseContext *context);
721 gboolean             g_markup_parse_context_parse (GMarkupParseContext *context,
722                                                    const gchar *text, gssize text_len,
723                                                    GError **error);
724 gboolean         g_markup_parse_context_end_parse (GMarkupParseContext *context,
725                                                    GError **error);
726
727 /*
728  * Character set conversion
729  */
730 gboolean  g_get_charset        (G_CONST_RETURN char **charset);
731 gchar    *g_locale_to_utf8     (const gchar *opsysstring, gssize len,
732                                 gsize *bytes_read, gsize *bytes_written,
733                                 GError **error);
734 gchar    *g_locale_from_utf8   (const gchar *utf8string, gssize len, gsize *bytes_read,
735                                 gsize *bytes_written, GError **error);
736 gchar    *g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize *bytes_read,
737                                 gsize *bytes_written, GError **error);
738 gchar    *g_convert            (const gchar *str, gssize len,
739                                 const gchar *to_codeset, const gchar *from_codeset,
740                                 gsize *bytes_read, gsize *bytes_written, GError **error);
741 gboolean  g_utf8_validate      (const gchar *str, gssize max_len, const gchar **end);
742
743 /*
744  * Empty thread functions, not used by eglib
745  */
746 #define g_thread_supported()   TRUE
747 #define g_thread_init(x)       G_STMT_START { if (x != NULL) { g_error ("No vtable supported in g_thread_init"); } } G_STMT_END
748
749 #define G_LOCK_DEFINE(name)
750 #define G_LOCK_DEFINE_STATIC(name)
751 #define G_LOCK_EXTERN(name)
752 #define G_LOCK(name)
753 #define G_TRYLOCK(name)
754 #define G_UNLOCK(name)
755
756 #define GUINT16_SWAP_LE_BE(x) ((guint16) (((guint16) x) >> 8) | ((((guint16)(x)) & 0xff) << 8))
757 #define GUINT32_SWAP_LE_BE(x) ((guint32) \
758                                ( (((guint32) (x)) << 24)| \
759                                  ((((guint32) (x)) & 0xff0000) >> 8) | \
760                                  ((((guint32) (x)) & 0xff00) << 8) | \
761                                  (((guint32) (x)) >> 24)) )
762  
763 #define GUINT64_SWAP_LE_BE(x) ((guint64) (((guint64)(GUINT32_SWAP_LE_BE(((guint64)x) & 0xffffffff))) << 32) | \
764                                GUINT32_SWAP_LE_BE(((guint64)x) >> 32))
765
766                                   
767  
768 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
769 #   define GUINT32_TO_LE(x) (x)
770 #   define GUINT64_TO_LE(x) (x)
771 #   define GUINT16_TO_LE(x) (x)
772 #   define GUINT_TO_LE(x)   (x)
773 #else
774 #   define GUINT32_TO_LE(x) GUINT32_SWAP_LE_BE(x)
775 #   define GUINT64_TO_LE(x) GUINT64_SWAP_LE_BE(x)
776 #   define GUINT16_TO_LE(x) GUINT16_SWAP_LE_BE(x)
777 #   define GUINT_TO_LE(x)   GUINT32_SWAP_LE_BE(x)
778 #endif
779
780 #define GUINT32_FROM_LE(x)  (GUINT32_TO_LE (x))
781 #define GUINT64_FROM_LE(x)  (GUINT64_TO_LE (x))
782 #define GUINT16_FROM_LE(x)  (GUINT16_TO_LE (x))
783 #define GUINT_FROM_LE(x)    (GUINT_TO_LE (x))
784
785 #define _EGLIB_MAJOR  2
786 #define _EGLIB_MIDDLE 4
787 #define _EGLIB_MINOR  0
788  
789 #define GLIB_CHECK_VERSION(a,b,c) ((a < _EGLIB_MAJOR) || (a == _EGLIB_MAJOR && (b < _EGLIB_MIDDLE || (b == _EGLIB_MIDDLE && c <= _EGLIB_MINOR))))
790  
791 #endif
792
793