2fc6ba41219dadab44a6f2a7e5518b473ab08df0
[mono.git] / eglib / src / glib.h
1 #ifndef __GLIB_H
2 #define __GLIB_H
3
4 #include <stdarg.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <ctype.h>
10 #include <eglib-config.h>
11
12 /*
13  * Basic data types
14  */
15 typedef int            gboolean;
16 typedef int            gint;
17 typedef unsigned int   guint;
18 typedef short          gshort;
19 typedef unsigned short gushort;
20 typedef long           glong;
21 typedef unsigned long  gulong;
22 typedef void *         gpointer;
23 typedef const void *   gconstpointer;
24 typedef char           gchar;
25 typedef unsigned char  guchar;
26
27 /* Types defined in terms of the stdint.h */
28 typedef int8_t         gint8;
29 typedef uint8_t        guint8;
30 typedef int16_t        gint16;
31 typedef uint16_t       guint16;
32 typedef int32_t        gint32;
33 typedef uint32_t       guint32;
34 typedef int64_t        gint64;
35 typedef uint64_t       guint64;
36 typedef float          gfloat;
37 typedef double         gdouble;
38 /*
39  * Macros
40  */
41 #define G_N_ELEMENTS(s)      (sizeof(s) / sizeof ((s) [0]))
42
43 #define FALSE                0
44 #define TRUE                 1
45
46 #define G_MAXINT32           INT32_MAX
47 #define G_MININT32           INT32_MIN
48
49 #define G_LITTLE_ENDIAN 1234
50 #define G_BIG_ENDIAN    4321
51 #define G_STMT_START    do 
52 #define G_STMT_END      while (0)
53
54 #define ABS(a,b)        (((a)>(b)) ? ((a)-(b)) : ((b)-(a)))
55
56 #define G_STRUCT_OFFSET(p_type,field) \
57         ((long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
58
59
60 /*
61  * Allocation
62  */
63 #define g_new(type,size)        ((type *) malloc (sizeof (type) * (size)))
64 #define g_new0(type,size)       ((type *) calloc (sizeof (type), (size))) 
65 #define g_realloc(obj,size)     realloc((obj), (size))
66 #define g_strdup(x)             strdup(x)
67 #define g_malloc(x)             malloc(x)
68 #define g_try_malloc(x)         malloc(x)
69 #define g_try_realloc(obj,size) realloc((obj),(size))
70 #define g_malloc0(x)            calloc(1,x)
71 #define g_memmove(dest,src,len) memmove (dest, src, len)
72 #define g_renew(struct_type, mem, n_structs) realloc (mem, sizeof (struct_type) * n_structs)
73 #define g_alloca(size)          alloca (size)
74 #define g_free                  free
75
76 gpointer g_memdup (gconstpointer mem, guint byte_size);
77
78 /*
79  * Misc.
80  */
81 #define g_atexit(func)  ((void) atexit (func))
82
83 const gchar *    g_getenv(const gchar *variable);
84 gboolean         g_setenv(const gchar *variable, const gchar *value, gboolean overwrite);
85 void             g_unsetenv(const gchar *variable);
86
87 /*
88  * Precondition macros
89  */
90 #define g_return_if_fail(x)  G_STMT_START { if (!(x)) { printf ("%s:%d: assertion '%s' failed", __FILE__, __LINE__, #x); return; } } G_STMT_END
91 #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
92
93 /*
94  * Hashtables
95  */
96 typedef struct _GHashTable GHashTable;
97 typedef void     (*GFunc)          (gpointer data, gpointer user_data);
98 typedef gint     (*GCompareFunc)   (gconstpointer a, gconstpointer b);
99 typedef gint     (*GCompareDataFunc) (gconstpointer a, gconstpointer b, gpointer user_data);
100 typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
101 typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
102 typedef void     (*GDestroyNotify) (gpointer data);
103 typedef guint    (*GHashFunc)      (gconstpointer key);
104 typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
105
106 GHashTable     *g_hash_table_new             (GHashFunc hash_func, GEqualFunc key_equal_func);
107 GHashTable     *g_hash_table_new_full        (GHashFunc hash_func, GEqualFunc key_equal_func,
108                                               GDestroyNotify key_destroy_func, GDestroyNotify value_destroy_func);
109 void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
110 guint           g_hash_table_size            (GHashTable *hash);
111 gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
112 gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
113 void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
114 gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
115 gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
116 guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
117 void            g_hash_table_destroy         (GHashTable *hash);
118
119 #define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
120 #define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
121
122 gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
123 guint    g_direct_hash  (gconstpointer v1);
124 gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
125 guint    g_int_hash     (gconstpointer v1);
126 gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
127 guint    g_str_hash     (gconstpointer v1);
128
129 #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
130 #define  g_assert_not_reached() G_STMT_START { g_error ("* Assertion: should not be reached at %s:%d\n", __FILE__, __LINE__); } G_STMT_END
131
132 /*
133  * Strings utility
134  */
135 gchar       *g_strdup_printf  (const gchar *format, ...);
136 gchar       *g_strdup_vprintf (const gchar *format, va_list args);
137 gchar       *g_strndup        (const gchar *str, gsize n);
138 const gchar *g_strerror       (gint errnum);
139 gchar       *g_strndup        (const gchar *str, gsize n);
140 void         g_strfreev       (gchar **str_array);
141 gchar       *g_strconcat      (const gchar *first, ...);
142 gchar      **g_strsplit       (const gchar *string, const gchar *delimiter, gint max_tokens);
143 gchar       *g_strreverse     (gchar *str);
144 gboolean     g_str_has_prefix (const gchar *str, const gchar *prefix);
145 gboolean     g_str_has_suffix (const gchar *str, const gchar *suffix);
146 guint        g_strv_length    (gchar **str_array);
147 gchar       *g_strjoin        (const gchar *separator, ...);
148 gchar       *g_strchug        (gchar *str);
149 gchar       *g_strchomp       (gchar *str);
150
151 gint         g_printf          (gchar const *format, ...);
152 gint         g_fprintf         (FILE *file, gchar const *format, ...);
153 gint         g_sprintf         (gchar *string, gchar const *format, ...);
154 gint         g_snprintf        (gchar *string, gulong n, gchar const *format, ...);
155 #define g_vprintf vprintf
156 #define g_vfprintf vfprintf
157 #define g_vsprintf vsprintf
158 #define g_vsnprintf vsnprintf
159 #define g_vasprintf vasprintf
160
161 #define g_ascii_isspace(c) (isspace (c) != 0)
162 #define g_ascii_isalpha(c) (isalpha (c) != 0)
163 #define g_ascii_isprint(c) (isprint (c) != 0)
164 #define g_ascii_isxdigit(c) (isxdigit (c) != 0)
165
166 /* FIXME: g_strcasecmp supports utf8 unicode stuff */
167 #define g_strcasecmp strcasecmp
168 #define g_ascii_strcasecmp strcasecmp
169 #define g_strncasecmp strncasecmp
170 #define g_strstrip(a) g_strchug (g_strchomp (a))
171
172 /*
173  * String type
174  */
175 typedef struct {
176         char *str;
177         gsize len;
178         gsize allocated_len;
179 } GString;
180
181 GString     *g_string_new           (const gchar *init);
182 GString     *g_string_new_len       (const gchar *init, gssize len);
183 GString     *g_string_sized_new     (gsize default_size);
184 gchar       *g_string_free          (GString *string, gboolean free_segment);
185 GString     *g_string_append        (GString *string, const gchar *val);
186 void         g_string_printf        (GString *string, const gchar *format, ...);
187 void         g_string_append_printf (GString *string, const gchar *format, ...);
188 GString     *g_string_append_c      (GString *string, gchar c);
189 GString     *g_string_append        (GString *string, const gchar *val);
190 GString     *g_string_append_len    (GString *string, const gchar *val, gssize len);
191 GString     *g_string_truncate      (GString *string, gsize len);
192 GString     *g_string_prepend       (GString *string, const gchar *val);
193
194 #define g_string_sprintfa g_string_append_printf
195
196 /*
197  * Lists
198  */
199 typedef struct _GSList GSList;
200 struct _GSList {
201         gpointer data;
202         GSList *next;
203 };
204
205 GSList *g_slist_alloc         (void);
206 GSList *g_slist_append        (GSList        *list,
207                                gpointer       data);
208 GSList *g_slist_prepend       (GSList        *list,
209                                gpointer       data);
210 void    g_slist_free          (GSList        *list);
211 void    g_slist_free_1        (GSList        *list);
212 GSList *g_slist_copy          (GSList        *list);
213 GSList *g_slist_concat        (GSList        *list1,
214                                GSList        *list2);
215 void    g_slist_foreach       (GSList        *list,
216                                GFunc          func,
217                                gpointer       user_data);
218 GSList *g_slist_last          (GSList        *list);
219 GSList *g_slist_find          (GSList        *list,
220                                gconstpointer  data);
221 GSList *g_slist_remove        (GSList        *list,
222                                gconstpointer  data);
223 GSList *g_slist_remove_all    (GSList        *list,
224                                gconstpointer  data);
225 GSList *g_slist_reverse       (GSList        *list);
226 guint   g_slist_length        (GSList        *list);
227 GSList *g_slist_remove_link   (GSList        *list,
228                                GSList        *link);
229 GSList *g_slist_delete_link   (GSList        *list,
230                                GSList        *link);
231 GSList *g_slist_insert_sorted (GSList        *list,
232                                gpointer       data,
233                                GCompareFunc   func);
234 GSList *g_slist_insert_before (GSList        *list,
235                                GSList        *sibling,
236                                gpointer       data);
237 GSList *g_slist_sort          (GSList        *list,
238                                GCompareFunc   func);
239
240 #define g_slist_next(slist) ((slist) ? (((GSList *) (slist))->next) : NULL)
241
242 typedef struct _GList GList;
243 struct _GList {
244   gpointer data;
245   GList *next;
246   GList *prev;
247 };
248
249 #define g_list_next(list) ((list) ? (((GList *) (list))->next) : NULL);
250
251 GList *g_list_alloc         (void);
252 GList *g_list_append        (GList         *list,
253                              gpointer       data);
254 GList *g_list_prepend       (GList         *list,
255                              gpointer       data);
256 void   g_list_free          (GList         *list);
257 void   g_list_free_1        (GList         *list);
258 GList *g_list_copy          (GList         *list);
259 guint  g_list_length        (GList         *list);
260 gint   g_list_index         (GList         *list,
261                              gconstpointer  data);
262 GList *g_list_nth           (GList         *list,
263                              guint          n);
264 gpointer g_list_nth_data      (GList         *list,
265                              guint          n);
266 GList *g_list_last          (GList         *list);
267 GList *g_list_concat        (GList         *list1,
268                              GList         *list2);
269 void   g_list_foreach       (GList         *list,
270                              GFunc          func,
271                              gpointer       user_data);
272 GList *g_list_first         (GList         *list);
273 GList *g_list_find          (GList         *list,
274                              gconstpointer  data);
275 GList *g_list_remove        (GList         *list,
276                              gconstpointer  data);
277 GList *g_list_reverse       (GList         *list);
278 GList *g_list_remove_link   (GList         *list,
279                              GList         *link);
280 GList *g_list_delete_link   (GList         *list,
281                              GList         *link);
282 GList *g_list_insert_sorted (GList         *list,
283                              gpointer       data,
284                              GCompareFunc   func);
285 GList *g_list_insert_before (GList         *list,
286                              GList         *sibling,
287                              gpointer       data);
288 GList *g_list_sort          (GList         *sort,
289                              GCompareFunc   func);
290
291 /*
292  * Array
293  */
294
295 typedef struct _GArray GArray;
296 struct _GArray {
297         gchar *data;
298         gint len;
299 };
300
301 GArray *g_array_new               (gboolean zero_terminated, gboolean clear_, guint element_size);
302 gchar*  g_array_free              (GArray *array, gboolean free_segment);
303 GArray *g_array_append_vals       (GArray *array, gconstpointer data, guint len);
304 GArray* g_array_insert_vals       (GArray *array, guint index_, gconstpointer data, guint len);
305 GArray* g_array_remove_index      (GArray *array, guint index_);
306
307 #define g_array_append_val(a,v)   (g_array_append_vals((a),&(v),1))
308 #define g_array_insert_val(a,i,v) (g_array_insert_vals((a),(i),&(v),1))
309 #define g_array_index(a,t,i)      *(t*)(((a)->data) + sizeof(t) * (i))
310
311 /*
312  * Pointer Array
313  */
314
315 typedef struct _GPtrArray GPtrArray;
316 struct _GPtrArray {
317         gpointer *pdata;
318         guint len;
319 };
320
321 GPtrArray *g_ptr_array_new                ();
322 GPtrArray *g_ptr_array_sized_new          (guint reserved_size);
323 void       g_ptr_array_add                (GPtrArray *array, gpointer data);
324 gboolean   g_ptr_array_remove             (GPtrArray *array, gpointer data);
325 gpointer   g_ptr_array_remove_index       (GPtrArray *array, guint index);
326 gboolean   g_ptr_array_remove_fast        (GPtrArray *array, gpointer data);
327 gpointer   g_ptr_array_remove_index_fast  (GPtrArray *array, gpointer data);
328 void       g_ptr_array_sort               (GPtrArray *array, GCompareFunc compare_func);
329 void       g_ptr_array_sort_with_data     (GPtrArray *array, GCompareDataFunc compare_func, gpointer user_data);
330 void       g_ptr_array_set_size           (GPtrArray *array, gint length);
331 gpointer  *g_ptr_array_free               (GPtrArray *array, gboolean free_seg);
332 void       g_ptr_array_foreach            (GPtrArray *array, GFunc func, gpointer user_data);
333 #define    g_ptr_array_index(array,index) (array)->pdata[(index)]
334
335 /*
336  * Queues
337  */
338 typedef struct {
339         GList *head;
340         GList *tail;
341         guint length;
342 } GQueue;
343
344 gpointer g_queue_pop_head  (GQueue   *queue);
345 void     g_queue_push_head (GQueue   *queue,
346                             gpointer  data);
347 gboolean g_queue_is_empty  (GQueue   *queue);
348 GQueue  *g_queue_new       (void);
349 void     g_queue_free      (GQueue   *queue);
350
351
352 /*
353  * Modules
354  */
355 typedef enum {
356         G_MODULE_BIND_LAZY = 0x01,
357         G_MODULE_BIND_LOCAL = 0x02,
358         G_MODULE_BIND_MASK = 0x03
359 } GModuleFlags;
360 typedef struct _GModule GModule;
361
362 GModule *g_module_open (const gchar *file, GModuleFlags flags);
363 gboolean g_module_symbol (GModule *module, const gchar *symbol_name,
364                           gpointer *symbol);
365 const gchar *g_module_error (void);
366 gboolean g_module_close (GModule *module);
367 gchar *  g_module_build_path (const gchar *directory, const gchar *module_name);
368 /*
369  * Messages
370  */
371 #ifndef G_LOG_DOMAIN
372 #define G_LOG_DOMAIN ((gchar*) 0)
373 #endif
374
375 typedef enum {
376         G_LOG_FLAG_RECURSION          = 1 << 0,
377         G_LOG_FLAG_FATAL              = 1 << 1,
378         
379         G_LOG_LEVEL_ERROR             = 1 << 2,
380         G_LOG_LEVEL_CRITICAL          = 1 << 3,
381         G_LOG_LEVEL_WARNING           = 1 << 4,
382         G_LOG_LEVEL_MESSAGE           = 1 << 5,
383         G_LOG_LEVEL_INFO              = 1 << 6,
384         G_LOG_LEVEL_DEBUG             = 1 << 7,
385         
386         G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
387 } GLogLevelFlags;
388
389 void           g_print                (const gchar *format, ...);
390 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
391 GLogLevelFlags g_log_set_fatal_mask   (const gchar *log_domain, GLogLevelFlags fatal_mask);
392 void           g_logv                 (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args);
393 void           g_log                  (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...);
394
395 #define g_error(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format)
396 #define g_critical(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format)
397 #define g_warning(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format)
398 #define g_message(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format)
399 #define g_debug(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
400
401 /*
402  * Unicode Manipulation: most of this is not used by Mono by default, it is
403  * only used if the old collation code is activated, so this is only the
404  * bare minimum to build.
405  */
406 typedef guint32 gunichar;
407
408 typedef enum {
409         G_UNICODE_LOWERCASE_LETTER,
410 } GUnicodeType;
411
412 gunichar       g_unichar_tolower (gunichar c);
413 GUnicodeType   g_unichar_type    (gunichar c);
414
415 #ifndef MAX
416 #define MAX(a,b) (((a)>(b)) ? (a) : (b))
417 #endif
418
419 /* FIXME: Implement these two for gcc */
420 #define G_LIKELY(x) (x)
421 #define G_UNLIKELY(x) (x)
422
423 /*
424  * Errors
425  */
426 typedef struct {
427         /* In the real glib, this is a GQuark, but we dont use/need that */
428         gpointer domain;
429         gint     code;
430         gchar   *message;
431 } GError;
432
433 void    g_error_free (GError *error);
434 GError *g_error_new (gpointer domain, gint code, const char *format, ...);
435
436 /*
437  * Path
438  */
439 gchar  *g_build_path           (const gchar *separator, const gchar *first_element, ...);
440 #define g_build_filename(x...) g_build_path(G_DIR_SEPARATOR_S, x)
441 gchar  *g_path_get_dirname     (const gchar *filename);
442 gchar  *g_path_get_basename    (const char *filename);
443 gchar  *g_find_program_in_path (const gchar *program);
444 gchar  *g_get_current_dir      (void);
445
446 const gchar *g_get_home_dir    (void);
447 const gchar *g_get_tmp_dir     (void);
448 const gchar *g_get_user_name   (void);
449
450 /*
451  * Shell
452  */
453
454 gboolean g_shell_parse_argv (const gchar *command_line, gint *argcp, gchar ***argvp, GError **error);
455
456 /*
457  * Spawn
458  */
459
460 gboolean g_spawn_command_line_sync (const gchar *command_line, gchar **standard_output, gchar **standard_error, gint *exit_status, GError **error);
461
462
463 /*
464  * Timer
465  */
466 typedef struct _GTimer GTimer;
467
468 GTimer *g_timer_new (void);
469 void g_timer_destroy (GTimer *timer);
470 gdouble g_timer_elapsed (GTimer *timer, gulong *microseconds);
471 void g_timer_stop (GTimer *timer);
472 void g_timer_start (GTimer *timer);
473
474 /*
475  * Date and time
476  */
477 typedef struct {
478         glong tv_sec;
479         glong tv_usec;
480 } GTimeVal;
481
482 void g_get_current_time (GTimeVal *result);
483
484 /*
485  * File
486  */
487
488 typedef enum {
489         G_FILE_ERROR_EXIST,
490         G_FILE_ERROR_ISDIR,
491         G_FILE_ERROR_ACCES,
492         G_FILE_ERROR_NAMETOOLONG,
493         G_FILE_ERROR_NOENT,
494         G_FILE_ERROR_NOTDIR,
495         G_FILE_ERROR_NXIO,
496         G_FILE_ERROR_NODEV,
497         G_FILE_ERROR_ROFS,
498         G_FILE_ERROR_TXTBSY,
499         G_FILE_ERROR_FAULT,
500         G_FILE_ERROR_LOOP,
501         G_FILE_ERROR_NOSPC,
502         G_FILE_ERROR_NOMEM,
503         G_FILE_ERROR_MFILE,
504         G_FILE_ERROR_NFILE,
505         G_FILE_ERROR_BADF,
506         G_FILE_ERROR_INVAL,
507         G_FILE_ERROR_PIPE,
508         G_FILE_ERROR_AGAIN,
509         G_FILE_ERROR_INTR,
510         G_FILE_ERROR_IO,
511         G_FILE_ERROR_PERM,
512         G_FILE_ERROR_NOSYS,
513         G_FILE_ERROR_FAILED
514 } GFileError;
515
516 gboolean   g_file_get_contents (const gchar *filename, gchar **contents, gsize *length, GError **error);
517 GFileError g_file_error_from_errno (gint err_no);
518 gint       g_file_open_tmp (const gchar *tmpl, gchar **name_used, GError **error);
519 #endif
520