2c
[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
8 /*
9  * Macros
10  */
11 #define G_N_ELEMENTS(s)      (sizeof(s) / sizeof ((s) [0]))
12
13 #define FALSE                0
14 #define TRUE                 1
15
16 #define G_MAXINT32           0xf7777777
17 #define G_MININT32           0x80000000
18
19 #define GPOINTER_TO_INT(ptr)   ((int)(ptr))
20 #define GPOINTER_TO_UINT(ptr)  ((uint)(ptr))
21 #define GINT_TO_POINTER(v)     ((gpointer) (v))
22 #define GUINT_TO_POINTER(v)    ((gpointer) (v))
23
24 #define G_STRUCT_OFFSET(p_type,field) \
25         ((long) (((char *) (&(((p_type)NULL)->field))) - ((char *) NULL)))
26
27 /*
28  * Allocation
29  */
30 #define g_new(type,size)        ((type *) malloc (sizeof (type) * (size)))
31 #define g_new0(type,size)       ((type *) calloc (sizeof (type), (size))) 
32 #define g_free(obj)             free (obj);
33 #define g_realloc(obj,size)     realloc((obj), (size))
34 #define g_strdup(x)             strdup(x)
35 #define g_malloc(x)             malloc(x)
36 #define g_try_malloc(x)         malloc(x)
37 #define g_try_realloc(obj,size) realloc((obj),(size))
38 #define g_malloc0(x)            calloc(1,x)
39 #define g_memmove(dest,src,len) memmove (dest, src, len)
40 #define g_renew(struct_type, mem, n_structs) realloc (mem, sizeof (struct_type) * n_structs)
41 #define g_alloca(size)          alloca (size)
42
43 /*
44  * Misc.
45  */
46 #define g_atexit(func)  ((void) atexit (func))
47 /*
48  * Basic data types
49  */
50 typedef int            gboolean;
51 typedef int            gint;
52 typedef unsigned int   gsize;
53 typedef unsigned int   guint;
54 typedef short          gshort;
55 typedef unsigned short gushort;
56 typedef long           glong;
57 typedef unsigned long  gulong;
58 typedef void *         gpointer;
59 typedef const void *   gconstpointer;
60 typedef char           gchar;
61 typedef unsigned char  guchar;
62
63 /*
64  * Precondition macros
65  */
66 #define g_return_if_fail(x)  do { if (!(x)) { printf ("%s:%d: assertion %s failed", __FILE__, __LINE__, #x); return; } } while (0) ;
67 #define g_return_val_if_fail(x,e)  do { if (!(x)) { printf ("%s:%d: assertion %s failed", __FILE__, __LINE__, #x); return (e); } } while (0) ;
68
69 /*
70  * Hashtables
71  */
72 typedef struct _GHashTable GHashTable;
73 typedef void     (* GFunc)         (gpointer data, gpointer user_data);
74 typedef void     (*GHFunc)         (gpointer key, gpointer value, gpointer user_data);
75 typedef gboolean (*GHRFunc)        (gpointer key, gpointer value, gpointer user_data);
76 typedef void     (*GDestroyNotify) (gpointer data);
77 typedef guint    (*GHashFunc)      (gconstpointer key);
78 typedef gboolean (*GEqualFunc)     (gconstpointer a, gconstpointer b);
79
80 GHashTable     *g_hash_table_new             (GHashFunc hash_func, GEqualFunc key_equal_func);
81 void            g_hash_table_insert_replace  (GHashTable *hash, gpointer key, gpointer value, gboolean replace);
82 guint           g_hash_table_size            (GHashTable *hash);
83 gpointer        g_hash_table_lookup          (GHashTable *hash, gconstpointer key);
84 gboolean        g_hash_table_lookup_extended (GHashTable *hash, gconstpointer key, gpointer *orig_key, gpointer *value);
85 void            g_hash_table_foreach         (GHashTable *hash, GHFunc func, gpointer user_data);
86 gpointer        g_hash_table_find            (GHashTable *hash, GHRFunc predicate, gpointer user_data);
87 gboolean        g_hash_table_remove          (GHashTable *hash, gconstpointer key);
88 guint           g_hash_table_foreach_remove  (GHashTable *hash, GHRFunc func, gpointer user_data);
89 void            g_hash_table_destroy         (GHashTable *hash);
90
91 #define g_hash_table_insert(h,k,v)    g_hash_table_insert_replace ((h),(k),(v),FALSE)
92 #define g_hash_table_replace(h,k,v)   g_hash_table_insert_replace ((h),(k),(v),TRUE)
93
94 gboolean g_direct_equal (gconstpointer v1, gconstpointer v2);
95 guint    g_direct_hash  (gconstpointer v1);
96 gboolean g_int_equal    (gconstpointer v1, gconstpointer v2);
97 guint    g_int_hash     (gconstpointer v1);
98 gboolean g_str_equal    (gconstpointer v1, gconstpointer v2);
99 guint    g_str_hash     (gconstpointer v1);
100
101 #define  g_assert(x)     do { if (!(x)) g_error ("* Assertion at %s:%d, condition `%s' not met\n", __FILE__, __LINE__, #x); } while (0)
102 #define  g_assert_not_reached() do { g_error ("* Assertion: should not be reached at %s:%d\n", __FILE__, __LINE__); } while (0)
103
104 /*
105  * Strings utility
106  */
107 gchar       *g_strdup_printf  (const gchar *format, ...);
108 gchar       *g_strdup_vprintf (const gchar *format, va_list args);
109 gchar       *g_strndup        (const gchar *str, gsize n);
110 const gchar *g_strerror       (gint errnum);
111 gchar       *g_strndup        (const gchar *str, gsize n);
112 void         g_strfreev       (gchar **str_array);
113 gchar       *g_strconcat      (const gchar *first, ...);
114 gchar      **g_strsplit       (const gchar *string, const gchar *delimiter, gint max_tokens);
115
116 /*
117  * String type
118  */
119 typedef struct {
120         char *str;
121         gsize len;
122         gsize allocated_len;
123 } GString;
124
125 GString     *g_string_new           (const gchar *init);
126 GString     *g_string_new_len       (const gchar *init, gsize len);
127 GString     *g_string_sized_new     (gsize default_size);
128 gchar       *g_string_free          (GString *string, gboolean free_segment);
129 GString     *g_string_append        (GString *string, const gchar *val);
130 void         g_string_printf        (GString *string, const gchar *format, ...);
131 void         g_string_append_printf (GString *string, const gchar *format, ...);
132 GString     *g_string_append_c      (GString *string, gchar c);
133 GString     *g_string_append        (GString *string, const gchar *val);
134
135 #define g_string_sprintfa g_string_append_printf
136
137 /*
138  * Lists
139  */
140 typedef struct _GSList GSList;
141 struct _GSList {
142         gpointer data;
143         GSList *next;
144 };
145
146 GSList *g_slist_alloc     (void);
147 GSList *g_slist_append    (GSList* list, gpointer data);
148 GSList *g_slist_prepend   (GSList* list, gpointer data);
149 void    g_slist_free      (GSList* list);
150 void    g_slist_free_1    (GSList* list);
151 GSList *g_slist_copy      (GSList* list);
152 GSList *g_slist_concat    (GSList* list1, GSList* list2);
153 void    g_slist_foreach   (GSList* list, GFunc func, gpointer user_data);
154 GSList *g_slist_last      (GSList *list);
155
156 #define g_slist_next (slist) ((slist) ? (((GSList *) slist)->next) : NULL)
157
158 /*
159  * Messages
160  */
161 #ifndef G_LOG_DOMAIN
162 #define G_LOG_DOMAIN ((gchar*) 0)
163 #endif
164
165 typedef enum {
166         G_LOG_FLAG_RECURSION          = 1 << 0,
167         G_LOG_FLAG_FATAL              = 1 << 1,
168         
169         G_LOG_LEVEL_ERROR             = 1 << 2,
170         G_LOG_LEVEL_CRITICAL          = 1 << 3,
171         G_LOG_LEVEL_WARNING           = 1 << 4,
172         G_LOG_LEVEL_MESSAGE           = 1 << 5,
173         G_LOG_LEVEL_INFO              = 1 << 6,
174         G_LOG_LEVEL_DEBUG             = 1 << 7,
175         
176         G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
177 } GLogLevelFlags;
178
179 void           g_print                (const gchar *format, ...);
180 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
181 GLogLevelFlags g_log_set_fatal_mask   (const gchar *log_domain, GLogLevelFlags fatal_mask);
182 void           g_logv                 (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, va_list args);
183 void           g_log                  (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format, ...);
184
185 #define g_error(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format)
186 #define g_critical(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format)
187 #define g_warning(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format)
188 #define g_message(format...)  g_log (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format)
189 #define g_debug(format...)    g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
190
191 #endif