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