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