• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef __GMAIN_H
2 #define __GMAIN_H
3 
4 #include <stdlib.h>
5 #include <sys/poll.h>
6 #include <sys/types.h>
7 #include <netinet/in.h>
8 #include <inttypes.h>
9 
10 typedef char	gchar;
11 typedef short	gshort;
12 typedef long	glong;
13 typedef int	gint;
14 typedef gint	gboolean;
15 
16 typedef unsigned char	guchar;
17 typedef unsigned short	gushort;
18 typedef unsigned long	gulong;
19 typedef unsigned int	guint;
20 
21 typedef uint8_t  guint8;
22 typedef uint16_t guint16;
23 typedef uint32_t guint32;
24 
25 typedef float	gfloat;
26 typedef double	gdouble;
27 
28 typedef void *		gpointer;
29 typedef const void *	gconstpointer;
30 
31 typedef size_t	gsize;
32 typedef ssize_t	gssize;
33 
34 #define GPOINTER_TO_INT(p)	((gint)  (glong) (p))
35 #define GPOINTER_TO_UINT(p)	((guint) (gulong) (p))
36 
37 #define GINT_TO_POINTER(i)	((gpointer) (glong) (i))
38 #define GUINT_TO_POINTER(u)	((gpointer) (gulong) (u))
39 
40 #ifndef SSIZE_MAX
41 #define SSIZE_MAX	INT_MAX
42 #endif
43 
44 #define g_ntohs(val) ntohs(val)
45 #define g_ntohl(val) ntohl(val)
46 #define g_htons(val) htons(val)
47 #define g_htonl(val) htonl(val)
48 
49 typedef pid_t GPid;
50 
51 #define MIN_TIMEOUT(a, b)  (((a) < (b)) ? (a) : (b))
52 
53 typedef struct _GIOChannel GIOChannel;
54 
55 typedef gboolean (*GSourceFunc) (gpointer data);
56 
57 typedef struct _GMainContext GMainContext;
58 
59 typedef struct _GMainLoop GMainLoop;
60 
61 typedef enum {
62 	G_IO_ERROR_NONE,
63 	G_IO_ERROR_AGAIN,
64 	G_IO_ERROR_INVAL,
65 	G_IO_ERROR_UNKNOWN
66 } GIOError;
67 
68 typedef enum {
69 	G_IO_STATUS_ERROR	= -1,
70 	G_IO_STATUS_NORMAL	= 0,
71 	G_IO_STATUS_EOF		= 1,
72 	G_IO_STATUS_AGAIN	= 2
73 } GIOStatus;
74 
75 typedef enum {
76 	G_IO_FLAG_APPEND	= 1 << 0,
77 	G_IO_FLAG_NONBLOCK	= 1 << 1,
78 	G_IO_FLAG_IS_READABLE	= 1 << 2,
79 	G_IO_FLAG_IS_WRITEABLE	= 1 << 3,
80 	G_IO_FLAG_IS_SEEKABLE	= 1 << 4,
81 	G_IO_FLAG_MASK		= (1 << 5) - 1,
82 	G_IO_FLAG_GET_MASK	= G_IO_FLAG_MASK,
83 	G_IO_FLAG_SET_MASK	= G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK
84 } GIOFlags;
85 
86 #ifndef FALSE
87 #define FALSE (0)
88 #endif
89 
90 #ifndef TRUE
91 #define TRUE (!FALSE)
92 #endif
93 
94 #undef MAX
95 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
96 
97 #undef MIN
98 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
99 
100 #undef ABS
101 #define ABS(a) (((a) < 0) ? -(a) : (a))
102 
103 #undef CLAMP
104 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
105 
106 /* GError */
107 
108 typedef guint32 GQuark;
109 
110 typedef struct {
111 	GQuark       domain;
112 	gint         code;
113 	gchar       *message;
114 } GError;
115 
116 void g_set_error(GError **err, GQuark domain, gint code,
117 			const gchar *format, ...);
118 GError* g_error_new_literal(GQuark domain, gint code, const gchar *message);
119 void g_error_free(GError *err);
120 
121 typedef enum {
122 	G_IO_IN		= POLLIN,
123 	G_IO_OUT	= POLLOUT,
124 	G_IO_PRI	= POLLPRI,
125 	G_IO_ERR	= POLLERR,
126 	G_IO_HUP	= POLLHUP,
127 	G_IO_NVAL	= POLLNVAL
128 } GIOCondition;
129 
130 #define G_PRIORITY_HIGH		-100
131 #define G_PRIORITY_DEFAULT	0
132 #define G_PRIORITY_HIGH_IDLE	100
133 #define G_PRIORITY_DEFAULT_IDLE	200
134 #define G_PRIORITY_LOW		300
135 
136 typedef void (*GDestroyNotify) (gpointer data);
137 typedef gboolean (*GIOFunc) (GIOChannel *source, GIOCondition condition,
138 								gpointer data);
139 
140 GIOError g_io_channel_read(GIOChannel *channel, gchar *buf, gsize count,
141 							gsize *bytes_read);
142 GIOError g_io_channel_write(GIOChannel *channel, const gchar *buf, gsize count,
143 							gsize *bytes_written);
144 
145 void g_io_channel_close(GIOChannel *channel);
146 
147 GIOChannel *g_io_channel_unix_new(int fd);
148 GIOChannel *g_io_channel_ref(GIOChannel *channel);
149 void g_io_channel_unref(GIOChannel *channel);
150 void g_io_channel_set_close_on_unref(GIOChannel *channel, gboolean do_close);
151 gint g_io_channel_unix_get_fd(GIOChannel *channel);
152 GIOStatus g_io_channel_set_flags(GIOChannel *channel, GIOFlags flags,
153 				GError **error);
154 guint g_io_add_watch(GIOChannel *channel, GIOCondition condition,
155 					GIOFunc func, gpointer user_data);
156 guint g_io_add_watch_full(GIOChannel *channel, gint priority,
157 				GIOCondition condition, GIOFunc func,
158 				gpointer user_data, GDestroyNotify notify);
159 
160 GMainLoop *g_main_loop_new(GMainContext *context, gboolean is_running);
161 void g_main_loop_run(GMainLoop *loop);
162 void g_main_loop_quit(GMainLoop *loop);
163 void g_main_loop_unref(GMainLoop *loop);
164 guint g_timeout_add(guint interval, GSourceFunc function, gpointer data);
165 guint g_timeout_add_seconds(guint interval, GSourceFunc function, gpointer data);
166 guint g_idle_add(GSourceFunc function, gpointer data);
167 gboolean g_source_remove(guint tag);
168 
169 /* Spawning related functions */
170 
171 typedef enum {
172 	G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
173 	G_SPAWN_DO_NOT_REAP_CHILD      = 1 << 1,
174 	/* look for argv[0] in the path i.e. use execvp() */
175 	G_SPAWN_SEARCH_PATH            = 1 << 2,
176 	/* Dump output to /dev/null */
177 	G_SPAWN_STDOUT_TO_DEV_NULL     = 1 << 3,
178 	G_SPAWN_STDERR_TO_DEV_NULL     = 1 << 4,
179 	G_SPAWN_CHILD_INHERITS_STDIN   = 1 << 5,
180 	G_SPAWN_FILE_AND_ARGV_ZERO     = 1 << 6
181 } GSpawnFlags;
182 
183 typedef void (*GSpawnChildSetupFunc) (gpointer user_data);
184 
185 gboolean g_spawn_async(const gchar *working_directory,
186 			gchar **argv, gchar **envp,
187 			GSpawnFlags flags,
188 			GSpawnChildSetupFunc child_setup,
189 			gpointer user_data,
190 			GPid *child_pid,
191 			GError **error);
192 
193 void g_spawn_close_pid(GPid pid);
194 
195 typedef void (*GChildWatchFunc) (GPid pid, gint status, gpointer data);
196 
197 guint g_child_watch_add(GPid pid, GChildWatchFunc func, gpointer user_data);
198 
199 gboolean g_utf8_validate(const gchar *str, gssize max_len, const gchar **end);
200 
201 #define g_main_new(is_running)	g_main_loop_new(NULL, is_running);
202 #define g_main_run(loop)	g_main_loop_run(loop)
203 #define g_main_quit(loop)	g_main_loop_quit(loop)
204 #define g_main_unref(loop)	g_main_loop_unref(loop)
205 
206 /* GSList declarations */
207 
208 typedef struct _GSList {
209 	void *data;
210 	struct _GSList *next;
211 } GSList;
212 
213 typedef int (*GCompareFunc)(const void *a, const void *b);
214 typedef void (*GFunc)(void *data, void *user_data);
215 
216 GSList *g_slist_append(GSList *list, void *data);
217 
218 GSList *g_slist_prepend(GSList *list, void *data);
219 
220 GSList *g_slist_insert_sorted(GSList *list, void *data, GCompareFunc cmp_func);
221 
222 GSList *g_slist_remove(GSList *list, void *data);
223 
224 GSList *g_slist_find(GSList *list, gconstpointer data);
225 
226 GSList *g_slist_find_custom(GSList *list, const void *data,
227 							GCompareFunc cmp_func);
228 
229 GSList *g_slist_sort(GSList *list, GCompareFunc cmp_func);
230 
231 int g_slist_length(GSList *list);
232 
233 void g_slist_foreach(GSList *list, GFunc func, void *user_data);
234 void g_slist_free(GSList *list);
235 GSList *g_slist_delete_link(GSList *list, GSList *link);
236 
237 GSList *g_slist_nth(GSList *list, guint n);
238 gpointer g_slist_nth_data(GSList *list, guint n);
239 int g_slist_position(GSList *list, GSList *link);
240 GSList* g_slist_last(GSList *list);
241 
242 #define g_slist_next(l) ((l)->next)
243 
244 /* Memory allocation related */
245 
246 gpointer g_malloc(gulong n_bytes);
247 gpointer g_malloc0(gulong n_bytes);
248 gpointer g_try_malloc(gulong n_bytes);
249 gpointer g_try_malloc0(gulong n_bytes);
250 gpointer g_realloc(gpointer mem, gulong n_bytes);
251 
252 void g_free(gpointer mem);
253 
254 gchar *g_strdup(const gchar *str);
255 gchar* g_strdup_printf(const gchar *format, ...);
256 gchar* g_strdelimit(gchar *string, const gchar *delimiters, gchar new_delim);
257 gchar *g_strconcat(const gchar *string1, ...);
258 gsize g_strlcat(gchar *dest, const gchar *src, gsize dest_size);
259 gchar **g_strsplit(const gchar *string, const gchar *delimiter, gint max_tokens);
260 gchar *g_ascii_strup(const gchar *str, gssize len);
261 gboolean g_str_equal(gconstpointer v1, gconstpointer v2);
262 gboolean g_str_has_prefix(const gchar *str, const gchar *prefix);
263 gboolean g_str_has_suffix(const gchar *str, const gchar *suffix);
264 void g_strfreev(gchar **str_array);
265 
266 #define g_new(struct_type, n_structs) \
267 	((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
268 #define g_new0(struct_type, n_structs) \
269 	((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
270 #define g_try_new(struct_type, n_structs) \
271 	((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
272 #define g_try_new0(struct_type, n_structs) \
273 	((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
274 #define g_renew(struct_type, mem, n_structs) \
275 	((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs))))
276 
277 /* GKeyFile */
278 
279 typedef enum {
280 	G_KEY_FILE_NONE              = 0,
281 	G_KEY_FILE_KEEP_COMMENTS     = 1 << 0,
282 	G_KEY_FILE_KEEP_TRANSLATIONS = 1 << 1
283 } GKeyFileFlags;
284 
285 typedef struct _GKeyFile GKeyFile;
286 
287 GKeyFile *g_key_file_new(void);
288 
289 void g_key_file_free(GKeyFile *key_file);
290 
291 gboolean g_key_file_load_from_file(GKeyFile *key_file,
292 				const gchar *file,
293 				GKeyFileFlags flags,
294 				GError **error);
295 
296 gchar *g_key_file_get_string(GKeyFile *key_file,
297 				const gchar *group_name,
298 				const gchar *key,
299 				GError **error);
300 
301 gboolean g_key_file_get_boolean(GKeyFile *key_file,
302 				const gchar *group_name,
303 				const gchar *key,
304 				GError **error);
305 
306 gint g_key_file_get_integer(GKeyFile *key_file,
307 				const gchar *group_name,
308 				const gchar *key,
309 				GError **error);
310 
311 gchar **g_key_file_get_string_list(GKeyFile *key_file,
312 					const gchar *group_name,
313 					const gchar *key, gsize *length,
314 					GError **error);
315 /* GString */
316 
317 typedef struct {
318 	gchar *str;
319 	gsize len;
320 	gsize allocated_len;
321 } GString;
322 
323 GString *g_string_new(const gchar *init);
324 
325 void g_string_append_printf(GString *string, const gchar *format, ...);
326 
327 gchar *g_string_free(GString *string, gboolean free_segment);
328 
329 /* GMarkup */
330 
331 typedef enum {
332 	G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG	= 1 << 0,
333 	G_MARKUP_TREAT_CDATA_AS_TEXT			= 1 << 1
334 } GMarkupParseFlags;
335 
336 typedef struct _GMarkupParseContext GMarkupParseContext;
337 typedef struct _GMarkupParser GMarkupParser;
338 
339 struct _GMarkupParser {
340 	/* Called for open tags <foo bar="baz"> */
341 	void (*start_element) (GMarkupParseContext *context,
342 			const gchar *element_name,
343 			const gchar **attribute_names,
344 			const gchar **attribute_values,
345 			gpointer user_data,
346 			GError **error);
347 
348 	/* Called for close tags </foo> */
349 	void (*end_element) (GMarkupParseContext *context,
350 			const gchar         *element_name,
351 			gpointer             user_data,
352 			GError             **error);
353 
354 	/* Called for character data */
355 	/* text is not nul-terminated */
356 	void (*text) (GMarkupParseContext *context,
357 			const gchar *text,
358 			gsize text_len,
359 			gpointer user_data,
360 			GError **error);
361 
362 	/* Called for strings that should be re-saved verbatim in this same
363 	 * position, but are not otherwise interpretable.  At the moment
364 	 * this includes comments and processing instructions.
365 	 */
366 	/* text is not nul-terminated. */
367 	void (*passthrough) (GMarkupParseContext *context,
368 			const gchar *passthrough_text,
369 			gsize text_len,
370 			gpointer user_data,
371 			GError **error);
372 
373 	/* Called on error, including one set by other
374 	 * methods in the vtable. The GError should not be freed.
375 	 */
376 	void (*error) (GMarkupParseContext *context,
377 			GError *error,
378 			gpointer user_data);
379 };
380 
381 GMarkupParseContext *g_markup_parse_context_new(const GMarkupParser *parser,
382 					GMarkupParseFlags flags,
383 					gpointer user_data,
384 					GDestroyNotify user_data_dnotify);
385 
386 gboolean g_markup_parse_context_parse(GMarkupParseContext *context,
387 					const gchar *text, gssize text_len,
388 					GError **error);
389 
390 void g_markup_parse_context_free(GMarkupParseContext *context);
391 
392 /* GDir */
393 
394 typedef struct _GDir GDir;
395 
396 GDir *g_dir_open(const gchar *path, guint flags, GError **error);
397 const gchar *g_dir_read_name(GDir *dir);
398 void g_dir_close(GDir *dir);
399 
400 /* Various */
401 
402 gchar *g_build_filename(const gchar *first_element, ...);
403 
404 #endif /* __GMAIN_H */
405