1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 /* system specific config file glibconfig.h provides definitions for
31 * the extrema of many of the standard types. These are:
33 * G_MINSHORT, G_MAXSHORT
35 * G_MINLONG, G_MAXLONG
36 * G_MINFLOAT, G_MAXFLOAT
37 * G_MINDOUBLE, G_MAXDOUBLE
39 * It also provides the following typedefs:
46 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
49 * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
50 * This is useful to pass an integer instead of a pointer to a callback.
52 * GINT_TO_POINTER (i), GUINT_TO_POINTER (i)
53 * GPOINTER_TO_INT (p), GPOINTER_TO_UINT (p)
55 * Finally, it provides the following wrappers to STDC functions:
57 * void g_memmove (gpointer dest, gconstpointer void *src, gulong count);
58 * A wrapper for STDC memmove, or an implementation, if memmove doesn't
59 * exist. The prototype looks like the above, give or take a const,
62 #include <glibconfig.h>
64 /* include varargs functions for assertment macros
68 /* optionally feature DMALLOC memory allocation debugger
77 /* On native Win32, directory separator is the backslash, and search path
78 * separator is the semicolon.
80 #define G_DIR_SEPARATOR '\\'
81 #define G_DIR_SEPARATOR_S "\\"
82 #define G_SEARCHPATH_SEPARATOR ';'
83 #define G_SEARCHPATH_SEPARATOR_S ";"
85 #else /* !NATIVE_WIN32 */
89 #define G_DIR_SEPARATOR '/'
90 #define G_DIR_SEPARATOR_S "/"
91 #define G_SEARCHPATH_SEPARATOR ':'
92 #define G_SEARCHPATH_SEPARATOR_S ":"
94 #endif /* !NATIVE_WIN32 */
98 #endif /* __cplusplus */
101 /* Provide definitions for some commonly used macros.
102 * Some of them are only provided if they haven't already
103 * been defined. It is assumed that if they are already
104 * defined then the current definition is correct.
107 #define NULL ((void*) 0)
115 #define TRUE (!FALSE)
119 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
122 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
125 #define ABS(a) (((a) < 0) ? -(a) : (a))
128 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
131 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
132 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
134 #if !defined (G_VA_COPY)
135 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
136 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
137 # elif defined (G_VA_COPY_AS_ARRAY)
138 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
139 # else /* va_list is a pointer */
140 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
141 # endif /* va_list is a pointer */
142 #endif /* !G_VA_COPY */
145 /* Provide convenience macros for handling structure
146 * fields through their offsets.
148 #define G_STRUCT_OFFSET(struct_type, member) \
149 ((gulong) ((gchar*) &((struct_type*) 0)->member))
150 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
151 ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
152 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
153 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
156 /* inlining hassle. for compilers that don't allow the `inline' keyword,
157 * mostly because of strict ANSI C compliance or dumbness, we try to fall
158 * back to either `__inline__' or `__inline'.
159 * we define G_CAN_INLINE, if the compiler seems to be actually
160 * *capable* to do function inlining, in which case inline function bodys
161 * do make sense. we also define G_INLINE_FUNC to properly export the
162 * function prototypes if no inlining can be performed.
163 * we special case most of the stuff, so inline functions can have a normal
164 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
166 #ifndef G_INLINE_FUNC
167 # define G_CAN_INLINE 1
170 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
172 # define inline __inline__
174 #else /* !G_HAVE_INLINE */
176 # if defined (G_HAVE___INLINE__)
177 # define inline __inline__
178 # else /* !inline && !__inline__ */
179 # if defined (G_HAVE___INLINE)
180 # define inline __inline
181 # else /* !inline && !__inline__ && !__inline */
182 # define inline /* don't inline, then */
183 # ifndef G_INLINE_FUNC
189 #ifndef G_INLINE_FUNC
192 # define G_INLINE_FUNC extern inline
195 # define G_INLINE_FUNC extern
197 # else /* !__GNUC__ */
199 # define G_INLINE_FUNC static inline
201 # define G_INLINE_FUNC extern
203 # endif /* !__GNUC__ */
204 #endif /* !G_INLINE_FUNC */
207 /* Provide simple macro statement wrappers (adapted from Perl):
208 * G_STMT_START { statements; } G_STMT_END;
209 * can be used as a single statement, as in
210 * if (x) G_STMT_START { ... } G_STMT_END; else ...
212 * For gcc we will wrap the statements within `({' and `})' braces.
213 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
214 * and otherwise within `do' and `while (0)'.
216 #if !(defined (G_STMT_START) && defined (G_STMT_END))
217 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
218 # define G_STMT_START (void)(
219 # define G_STMT_END )
221 # if (defined (sun) || defined (__sun__))
222 # define G_STMT_START if (1)
223 # define G_STMT_END else (void)0
225 # define G_STMT_START do
226 # define G_STMT_END while (0)
232 /* Provide macros to feature the GCC function attribute.
234 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
235 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
236 __attribute__((format (printf, format_idx, arg_idx)))
237 #define G_GNUC_SCANF( format_idx, arg_idx ) \
238 __attribute__((format (scanf, format_idx, arg_idx)))
239 #define G_GNUC_FORMAT( arg_idx ) \
240 __attribute__((format_arg (arg_idx)))
241 #define G_GNUC_NORETURN \
242 __attribute__((noreturn))
243 #define G_GNUC_CONST \
244 __attribute__((const))
245 #define G_GNUC_UNUSED \
246 __attribute__((unused))
247 #else /* !__GNUC__ */
248 #define G_GNUC_PRINTF( format_idx, arg_idx )
249 #define G_GNUC_SCANF( format_idx, arg_idx )
250 #define G_GNUC_FORMAT( arg_idx )
251 #define G_GNUC_NORETURN
253 #define G_GNUC_UNUSED
254 #endif /* !__GNUC__ */
257 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
258 * macros, so we can refer to them as strings unconditionally.
261 #define G_GNUC_FUNCTION __FUNCTION__
262 #define G_GNUC_PRETTY_FUNCTION __PRETTY_FUNCTION__
263 #else /* !__GNUC__ */
264 #define G_GNUC_FUNCTION ""
265 #define G_GNUC_PRETTY_FUNCTION ""
266 #endif /* !__GNUC__ */
268 /* we try to provide a usefull equivalent for ATEXIT if it is
269 * not defined, but use is actually abandoned. people should
270 * use g_atexit() instead.
273 # define ATEXIT(proc) g_ATEXIT(proc)
275 # define G_NATIVE_ATEXIT
278 /* Hacker macro to place breakpoints for elected machines.
279 * Actual use is strongly deprecated of course ;)
281 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
282 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
283 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
284 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
285 #else /* !__i386__ && !__alpha__ */
286 #define G_BREAKPOINT()
287 #endif /* __i386__ */
290 /* Provide macros for easily allocating memory. The macros
291 * will cast the allocated memory to the specified type
292 * in order to avoid compiler warnings. (Makes the code neater).
296 # define g_new(type, count) (ALLOC (type, count))
297 # define g_new0(type, count) (CALLOC (type, count))
298 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
299 #else /* __DMALLOC_H__ */
300 # define g_new(type, count) \
301 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
302 # define g_new0(type, count) \
303 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
304 # define g_renew(type, mem, count) \
305 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
306 #endif /* __DMALLOC_H__ */
308 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
309 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
311 sizeof (type) * (pre_alloc), \
314 #define g_chunk_new(type, chunk) ( \
315 (type *) g_mem_chunk_alloc (chunk) \
317 #define g_chunk_new0(type, chunk) ( \
318 (type *) g_mem_chunk_alloc0 (chunk) \
320 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
321 g_mem_chunk_free ((mem_chunk), (mem)); \
325 #define g_string(x) #x
328 /* Provide macros for error handling. The "assert" macros will
329 * exit on failure. The "return" macros will exit the current
330 * function. Two different definitions are given for the macros
331 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
332 * __PRETTY_FUNCTION__ capability.
335 #ifdef G_DISABLE_ASSERT
337 #define g_assert(expr)
338 #define g_assert_not_reached()
340 #else /* !G_DISABLE_ASSERT */
344 #define g_assert(expr) G_STMT_START{ \
346 g_log (G_LOG_DOMAIN, \
348 "file %s: line %d (%s): assertion failed: (%s)", \
351 __PRETTY_FUNCTION__, \
354 #define g_assert_not_reached() G_STMT_START{ \
355 g_log (G_LOG_DOMAIN, \
357 "file %s: line %d (%s): should not be reached", \
360 __PRETTY_FUNCTION__); }G_STMT_END
362 #else /* !__GNUC__ */
364 #define g_assert(expr) G_STMT_START{ \
366 g_log (G_LOG_DOMAIN, \
368 "file %s: line %d: assertion failed: (%s)", \
373 #define g_assert_not_reached() G_STMT_START{ \
374 g_log (G_LOG_DOMAIN, \
376 "file %s: line %d: should not be reached", \
378 __LINE__); }G_STMT_END
380 #endif /* __GNUC__ */
382 #endif /* !G_DISABLE_ASSERT */
385 #ifdef G_DISABLE_CHECKS
387 #define g_return_if_fail(expr)
388 #define g_return_val_if_fail(expr,val)
390 #else /* !G_DISABLE_CHECKS */
394 #define g_return_if_fail(expr) G_STMT_START{ \
397 g_log (G_LOG_DOMAIN, \
398 G_LOG_LEVEL_CRITICAL, \
399 "file %s: line %d (%s): assertion `%s' failed.", \
402 __PRETTY_FUNCTION__, \
407 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
410 g_log (G_LOG_DOMAIN, \
411 G_LOG_LEVEL_CRITICAL, \
412 "file %s: line %d (%s): assertion `%s' failed.", \
415 __PRETTY_FUNCTION__, \
420 #else /* !__GNUC__ */
422 #define g_return_if_fail(expr) G_STMT_START{ \
425 g_log (G_LOG_DOMAIN, \
426 G_LOG_LEVEL_CRITICAL, \
427 "file %s: line %d: assertion `%s' failed.", \
434 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
437 g_log (G_LOG_DOMAIN, \
438 G_LOG_LEVEL_CRITICAL, \
439 "file %s: line %d: assertion `%s' failed.", \
446 #endif /* !__GNUC__ */
448 #endif /* !G_DISABLE_CHECKS */
451 /* Provide type definitions for commonly used types.
452 * These are useful because a "gint8" can be adjusted
453 * to be 1 byte (8 bits) on all platforms. Similarly and
454 * more importantly, "gint32" can be adjusted to be
455 * 4 bytes (32 bits) on all platforms.
459 typedef short gshort;
462 typedef gint gboolean;
464 typedef unsigned char guchar;
465 typedef unsigned short gushort;
466 typedef unsigned long gulong;
467 typedef unsigned int guint;
469 #define G_GSHORT_FORMAT "hi"
470 #define G_GUSHORT_FORMAT "hu"
471 #define G_GINT_FORMAT "i"
472 #define G_GUINT_FORMAT "u"
473 #define G_GLONG_FORMAT "li"
474 #define G_GULONG_FORMAT "lu"
476 typedef float gfloat;
477 typedef double gdouble;
479 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
480 * Since gldouble isn't used anywhere, just disable it for now */
483 #ifdef HAVE_LONG_DOUBLE
484 typedef long double gldouble;
485 #else /* HAVE_LONG_DOUBLE */
486 typedef double gldouble;
487 #endif /* HAVE_LONG_DOUBLE */
490 typedef void* gpointer;
491 typedef const void *gconstpointer;
494 typedef gint32 gssize;
495 typedef guint32 gsize;
496 typedef guint32 GQuark;
497 typedef gint32 GTime;
500 /* Portable endian checks and conversions
502 * glibconfig.h defines G_BYTE_ORDER which expands to one of
505 #define G_LITTLE_ENDIAN 1234
506 #define G_BIG_ENDIAN 4321
507 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
510 /* Basic bit swapping functions
512 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
513 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
514 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
515 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
516 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
517 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
518 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
519 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
521 /* Intel specific stuff for speed
523 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
524 # define GUINT16_SWAP_LE_BE_X86(val) \
526 ({ register guint16 __v; \
527 if (__builtin_constant_p (val)) \
528 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
530 __asm__ __const__ ("rorw $8, %w0" \
532 : "0" ((guint16) (val))); \
534 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
535 # if !defined(__i486__) && !defined(__i586__) \
536 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
537 # define GUINT32_SWAP_LE_BE_X86(val) \
539 ({ register guint32 __v; \
540 if (__builtin_constant_p (val)) \
541 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
543 __asm__ __const__ ("rorw $8, %w0\n\t" \
547 : "0" ((guint32) (val))); \
549 # else /* 486 and higher has bswap */
550 # define GUINT32_SWAP_LE_BE_X86(val) \
552 ({ register guint32 __v; \
553 if (__builtin_constant_p (val)) \
554 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
556 __asm__ __const__ ("bswap %0" \
558 : "0" ((guint32) (val))); \
560 # endif /* processor specific 32-bit stuff */
561 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
562 #else /* !__i386__ */
563 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
564 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
565 #endif /* __i386__ */
568 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
569 (((guint64) (val) & \
570 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
571 (((guint64) (val) & \
572 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
573 (((guint64) (val) & \
574 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
575 (((guint64) (val) & \
576 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
577 (((guint64) (val) & \
578 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
579 (((guint64) (val) & \
580 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
581 (((guint64) (val) & \
582 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
583 (((guint64) (val) & \
584 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
585 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
586 # define GUINT64_SWAP_LE_BE_X86(val) \
588 ({ union { guint64 __ll; \
589 guint32 __l[2]; } __r; \
590 if (__builtin_constant_p (val)) \
591 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
594 union { guint64 __ll; \
595 guint32 __l[2]; } __w; \
596 __w.__ll = ((guint64) val); \
597 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
598 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
601 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
602 # else /* !__i386__ */
603 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
607 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
608 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
609 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
610 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
611 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
612 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
613 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
614 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
616 /* The G*_TO_?E() macros are defined in glibconfig.h.
617 * The transformation is symmetric, so the FROM just maps to the TO.
619 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
620 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
621 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
622 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
623 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
624 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
625 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
626 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
629 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
630 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
631 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
632 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
635 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
636 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
637 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
638 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
640 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
641 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
642 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
643 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
646 /* Portable versions of host-network order stuff
648 #define g_ntohl(val) (GUINT32_FROM_BE (val))
649 #define g_ntohs(val) (GUINT16_FROM_BE (val))
650 #define g_htonl(val) (GUINT32_TO_BE (val))
651 #define g_htons(val) (GUINT16_TO_BE (val))
655 * we prefix variable declarations so they can
656 * properly get exported in windows dlls.
659 # ifdef GLIB_COMPILATION
660 # define GUTILS_C_VAR __declspec(dllexport)
661 # else /* !GLIB_COMPILATION */
662 # define GUTILS_C_VAR extern __declspec(dllimport)
663 # endif /* !GLIB_COMPILATION */
664 #else /* !NATIVE_WIN32 */
665 # define GUTILS_C_VAR extern
666 #endif /* !NATIVE_WIN32 */
668 GUTILS_C_VAR const guint glib_major_version;
669 GUTILS_C_VAR const guint glib_minor_version;
670 GUTILS_C_VAR const guint glib_micro_version;
671 GUTILS_C_VAR const guint glib_interface_age;
672 GUTILS_C_VAR const guint glib_binary_age;
674 #define GLIB_CHECK_VERSION(major,minor,micro) \
675 (GLIB_MAJOR_VERSION > (major) || \
676 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
677 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
678 GLIB_MICRO_VERSION >= (micro)))
680 /* Forward declarations of glib types.
682 typedef struct _GAllocator GAllocator;
683 typedef struct _GArray GArray;
684 typedef struct _GByteArray GByteArray;
685 typedef struct _GCache GCache;
686 typedef struct _GCompletion GCompletion;
687 typedef struct _GData GData;
688 typedef struct _GDebugKey GDebugKey;
689 typedef struct _GHashTable GHashTable;
690 typedef struct _GHook GHook;
691 typedef struct _GHookList GHookList;
692 typedef struct _GList GList;
693 typedef struct _GMemChunk GMemChunk;
694 typedef struct _GNode GNode;
695 typedef struct _GPtrArray GPtrArray;
696 typedef struct _GQueue GQueue;
697 typedef struct _GRand GRand;
698 typedef struct _GRelation GRelation;
699 typedef struct _GScanner GScanner;
700 typedef struct _GScannerConfig GScannerConfig;
701 typedef struct _GSList GSList;
702 typedef struct _GString GString;
703 typedef struct _GStringChunk GStringChunk;
704 typedef struct _GTimer GTimer;
705 typedef struct _GTrashStack GTrashStack;
706 typedef struct _GTree GTree;
707 typedef struct _GTuples GTuples;
708 typedef union _GTokenValue GTokenValue;
709 typedef struct _GIOChannel GIOChannel;
711 /* Tree traverse flags */
714 G_TRAVERSE_LEAFS = 1 << 0,
715 G_TRAVERSE_NON_LEAFS = 1 << 1,
716 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
717 G_TRAVERSE_MASK = 0x03
720 /* Tree traverse orders */
729 /* Log level shift offset for user defined
730 * log levels (0-7 are used by GLib).
732 #define G_LOG_LEVEL_USER_SHIFT (8)
734 /* Glib log levels and flags.
739 G_LOG_FLAG_RECURSION = 1 << 0,
740 G_LOG_FLAG_FATAL = 1 << 1,
742 /* GLib log levels */
743 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
744 G_LOG_LEVEL_CRITICAL = 1 << 3,
745 G_LOG_LEVEL_WARNING = 1 << 4,
746 G_LOG_LEVEL_MESSAGE = 1 << 5,
747 G_LOG_LEVEL_INFO = 1 << 6,
748 G_LOG_LEVEL_DEBUG = 1 << 7,
750 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
753 /* GLib log levels that are considered fatal by default */
754 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
757 typedef gpointer (*GCacheNewFunc) (gpointer key);
758 typedef gpointer (*GCacheDupFunc) (gpointer value);
759 typedef void (*GCacheDestroyFunc) (gpointer value);
760 typedef gint (*GCompareFunc) (gconstpointer a,
762 typedef gchar* (*GCompletionFunc) (gpointer);
763 typedef void (*GDestroyNotify) (gpointer data);
764 typedef void (*GDataForeachFunc) (GQuark key_id,
767 typedef void (*GFunc) (gpointer data,
769 typedef guint (*GHashFunc) (gconstpointer key);
770 typedef void (*GFreeFunc) (gpointer data);
771 typedef void (*GHFunc) (gpointer key,
774 typedef gboolean (*GHRFunc) (gpointer key,
777 typedef gint (*GHookCompareFunc) (GHook *new_hook,
779 typedef gboolean (*GHookFindFunc) (GHook *hook,
781 typedef void (*GHookMarshaller) (GHook *hook,
783 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
785 typedef void (*GHookFunc) (gpointer data);
786 typedef gboolean (*GHookCheckFunc) (gpointer data);
787 typedef void (*GHookFreeFunc) (GHookList *hook_list,
789 typedef void (*GLogFunc) (const gchar *log_domain,
790 GLogLevelFlags log_level,
791 const gchar *message,
793 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
795 typedef void (*GNodeForeachFunc) (GNode *node,
797 typedef gint (*GSearchFunc) (gpointer key,
799 typedef void (*GScannerMsgFunc) (GScanner *scanner,
802 typedef gint (*GTraverseFunc) (gpointer key,
805 typedef void (*GVoidFunc) (void);
869 /* Doubly linked lists
871 void g_list_push_allocator (GAllocator *allocator);
872 void g_list_pop_allocator (void);
873 GList* g_list_alloc (void);
874 void g_list_free (GList *list);
875 void g_list_free_1 (GList *list);
876 GList* g_list_append (GList *list,
878 GList* g_list_prepend (GList *list,
880 GList* g_list_insert (GList *list,
883 GList* g_list_insert_sorted (GList *list,
886 GList* g_list_concat (GList *list1,
888 GList* g_list_remove (GList *list,
890 GList* g_list_remove_link (GList *list,
892 GList* g_list_delete_link (GList *list,
894 GList* g_list_reverse (GList *list);
895 GList* g_list_copy (GList *list);
896 GList* g_list_nth (GList *list,
898 GList* g_list_find (GList *list,
900 GList* g_list_find_custom (GList *list,
903 gint g_list_position (GList *list,
905 gint g_list_index (GList *list,
907 GList* g_list_last (GList *list);
908 GList* g_list_first (GList *list);
909 guint g_list_length (GList *list);
910 void g_list_foreach (GList *list,
913 GList* g_list_sort (GList *list,
914 GCompareFunc compare_func);
915 gpointer g_list_nth_data (GList *list,
917 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
918 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
921 /* Singly linked lists
923 void g_slist_push_allocator (GAllocator *allocator);
924 void g_slist_pop_allocator (void);
925 GSList* g_slist_alloc (void);
926 void g_slist_free (GSList *list);
927 void g_slist_free_1 (GSList *list);
928 GSList* g_slist_append (GSList *list,
930 GSList* g_slist_prepend (GSList *list,
932 GSList* g_slist_insert (GSList *list,
935 GSList* g_slist_insert_sorted (GSList *list,
938 GSList* g_slist_concat (GSList *list1,
940 GSList* g_slist_remove (GSList *list,
942 GSList* g_slist_remove_link (GSList *list,
944 GSList* g_slist_delete_link (GSList *list,
946 GSList* g_slist_reverse (GSList *list);
947 GSList* g_slist_copy (GSList *list);
948 GSList* g_slist_nth (GSList *list,
950 GSList* g_slist_find (GSList *list,
952 GSList* g_slist_find_custom (GSList *list,
955 gint g_slist_position (GSList *list,
957 gint g_slist_index (GSList *list,
959 GSList* g_slist_last (GSList *list);
960 guint g_slist_length (GSList *list);
961 void g_slist_foreach (GSList *list,
964 GSList* g_slist_sort (GSList *list,
965 GCompareFunc compare_func);
966 gpointer g_slist_nth_data (GSList *list,
968 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
973 GQueue* g_queue_create (void);
974 void g_queue_free (GQueue *queue);
975 void g_queue_push_head (GQueue *queue,
977 void g_queue_push_tail (GQueue *queue,
979 gpointer g_queue_pop_head (GQueue *queue);
980 gpointer g_queue_pop_tail (GQueue *queue);
981 gboolean g_queue_is_empty (GQueue *queue);
982 gpointer g_queue_peek_head (GQueue *queue);
983 gpointer g_queue_peek_tail (GQueue *queue);
984 void g_queue_push_head_link (GQueue *queue,
986 void g_queue_push_tail_link (GQueue *queue,
988 GList* g_queue_pop_head_link (GQueue *queue);
989 GList* g_queue_pop_tail_link (GQueue *queue);
994 GHashTable* g_hash_table_new (GHashFunc hash_func,
995 GCompareFunc key_compare_func);
996 void g_hash_table_destroy (GHashTable *hash_table);
997 void g_hash_table_insert (GHashTable *hash_table,
1000 void g_hash_table_remove (GHashTable *hash_table,
1002 gpointer g_hash_table_lookup (GHashTable *hash_table,
1004 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1005 gconstpointer lookup_key,
1008 void g_hash_table_freeze (GHashTable *hash_table);
1009 void g_hash_table_thaw (GHashTable *hash_table);
1010 void g_hash_table_foreach (GHashTable *hash_table,
1012 gpointer user_data);
1013 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1015 gpointer user_data);
1016 guint g_hash_table_size (GHashTable *hash_table);
1021 GCache* g_cache_new (GCacheNewFunc value_new_func,
1022 GCacheDestroyFunc value_destroy_func,
1023 GCacheDupFunc key_dup_func,
1024 GCacheDestroyFunc key_destroy_func,
1025 GHashFunc hash_key_func,
1026 GHashFunc hash_value_func,
1027 GCompareFunc key_compare_func);
1028 void g_cache_destroy (GCache *cache);
1029 gpointer g_cache_insert (GCache *cache,
1031 void g_cache_remove (GCache *cache,
1033 void g_cache_key_foreach (GCache *cache,
1035 gpointer user_data);
1036 void g_cache_value_foreach (GCache *cache,
1038 gpointer user_data);
1041 /* Balanced binary trees
1043 GTree* g_tree_new (GCompareFunc key_compare_func);
1044 void g_tree_destroy (GTree *tree);
1045 void g_tree_insert (GTree *tree,
1048 void g_tree_remove (GTree *tree,
1050 gpointer g_tree_lookup (GTree *tree,
1052 void g_tree_traverse (GTree *tree,
1053 GTraverseFunc traverse_func,
1054 GTraverseType traverse_type,
1056 gpointer g_tree_search (GTree *tree,
1057 GSearchFunc search_func,
1059 gint g_tree_height (GTree *tree);
1060 gint g_tree_nnodes (GTree *tree);
1064 /* N-way tree implementation
1075 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1076 ((GNode*) (node))->prev == NULL && \
1077 ((GNode*) (node))->next == NULL)
1078 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1080 void g_node_push_allocator (GAllocator *allocator);
1081 void g_node_pop_allocator (void);
1082 GNode* g_node_new (gpointer data);
1083 void g_node_destroy (GNode *root);
1084 void g_node_unlink (GNode *node);
1085 GNode* g_node_insert (GNode *parent,
1088 GNode* g_node_insert_before (GNode *parent,
1091 GNode* g_node_prepend (GNode *parent,
1093 guint g_node_n_nodes (GNode *root,
1094 GTraverseFlags flags);
1095 GNode* g_node_get_root (GNode *node);
1096 gboolean g_node_is_ancestor (GNode *node,
1098 guint g_node_depth (GNode *node);
1099 GNode* g_node_find (GNode *root,
1100 GTraverseType order,
1101 GTraverseFlags flags,
1104 /* convenience macros */
1105 #define g_node_append(parent, node) \
1106 g_node_insert_before ((parent), NULL, (node))
1107 #define g_node_insert_data(parent, position, data) \
1108 g_node_insert ((parent), (position), g_node_new (data))
1109 #define g_node_insert_data_before(parent, sibling, data) \
1110 g_node_insert_before ((parent), (sibling), g_node_new (data))
1111 #define g_node_prepend_data(parent, data) \
1112 g_node_prepend ((parent), g_node_new (data))
1113 #define g_node_append_data(parent, data) \
1114 g_node_insert_before ((parent), NULL, g_node_new (data))
1116 /* traversal function, assumes that `node' is root
1117 * (only traverses `node' and its subtree).
1118 * this function is just a high level interface to
1119 * low level traversal functions, optimized for speed.
1121 void g_node_traverse (GNode *root,
1122 GTraverseType order,
1123 GTraverseFlags flags,
1125 GNodeTraverseFunc func,
1128 /* return the maximum tree height starting with `node', this is an expensive
1129 * operation, since we need to visit all nodes. this could be shortened by
1130 * adding `guint height' to struct _GNode, but then again, this is not very
1131 * often needed, and would make g_node_insert() more time consuming.
1133 guint g_node_max_height (GNode *root);
1135 void g_node_children_foreach (GNode *node,
1136 GTraverseFlags flags,
1137 GNodeForeachFunc func,
1139 void g_node_reverse_children (GNode *node);
1140 guint g_node_n_children (GNode *node);
1141 GNode* g_node_nth_child (GNode *node,
1143 GNode* g_node_last_child (GNode *node);
1144 GNode* g_node_find_child (GNode *node,
1145 GTraverseFlags flags,
1147 gint g_node_child_position (GNode *node,
1149 gint g_node_child_index (GNode *node,
1152 GNode* g_node_first_sibling (GNode *node);
1153 GNode* g_node_last_sibling (GNode *node);
1155 #define g_node_prev_sibling(node) ((node) ? \
1156 ((GNode*) (node))->prev : NULL)
1157 #define g_node_next_sibling(node) ((node) ? \
1158 ((GNode*) (node))->next : NULL)
1159 #define g_node_first_child(node) ((node) ? \
1160 ((GNode*) (node))->children : NULL)
1163 /* Callback maintenance functions
1165 #define G_HOOK_FLAG_USER_SHIFT (4)
1168 G_HOOK_FLAG_ACTIVE = 1 << 0,
1169 G_HOOK_FLAG_IN_CALL = 1 << 1,
1170 G_HOOK_FLAG_MASK = 0x0f
1173 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1181 GMemChunk *hook_memchunk;
1182 GHookFreeFunc hook_free; /* virtual function */
1183 GHookFreeFunc hook_destroy; /* virtual function */
1195 GDestroyNotify destroy;
1198 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1199 G_HOOK_FLAG_ACTIVE) != 0)
1200 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1201 G_HOOK_FLAG_IN_CALL) != 0)
1202 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1203 G_HOOK_ACTIVE (hook))
1204 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1205 ((GHook*) hook)->prev == NULL && \
1206 ((GHook*) hook)->hook_id == 0 && \
1207 ((GHook*) hook)->ref_count == 0)
1209 void g_hook_list_init (GHookList *hook_list,
1211 void g_hook_list_clear (GHookList *hook_list);
1212 GHook* g_hook_alloc (GHookList *hook_list);
1213 void g_hook_free (GHookList *hook_list,
1215 void g_hook_ref (GHookList *hook_list,
1217 void g_hook_unref (GHookList *hook_list,
1219 gboolean g_hook_destroy (GHookList *hook_list,
1221 void g_hook_destroy_link (GHookList *hook_list,
1223 void g_hook_prepend (GHookList *hook_list,
1225 void g_hook_insert_before (GHookList *hook_list,
1228 void g_hook_insert_sorted (GHookList *hook_list,
1230 GHookCompareFunc func);
1231 GHook* g_hook_get (GHookList *hook_list,
1233 GHook* g_hook_find (GHookList *hook_list,
1234 gboolean need_valids,
1237 GHook* g_hook_find_data (GHookList *hook_list,
1238 gboolean need_valids,
1240 GHook* g_hook_find_func (GHookList *hook_list,
1241 gboolean need_valids,
1243 GHook* g_hook_find_func_data (GHookList *hook_list,
1244 gboolean need_valids,
1247 /* return the first valid hook, and increment its reference count */
1248 GHook* g_hook_first_valid (GHookList *hook_list,
1249 gboolean may_be_in_call);
1250 /* return the next valid hook with incremented reference count, and
1251 * decrement the reference count of the original hook
1253 GHook* g_hook_next_valid (GHookList *hook_list,
1255 gboolean may_be_in_call);
1257 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1258 gint g_hook_compare_ids (GHook *new_hook,
1261 /* convenience macros */
1262 #define g_hook_append( hook_list, hook ) \
1263 g_hook_insert_before ((hook_list), NULL, (hook))
1265 /* invoke all valid hooks with the (*GHookFunc) signature.
1267 void g_hook_list_invoke (GHookList *hook_list,
1268 gboolean may_recurse);
1269 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1270 * and destroy the hook if FALSE is returned.
1272 void g_hook_list_invoke_check (GHookList *hook_list,
1273 gboolean may_recurse);
1274 /* invoke a marshaller on all valid hooks.
1276 void g_hook_list_marshal (GHookList *hook_list,
1277 gboolean may_recurse,
1278 GHookMarshaller marshaller,
1280 void g_hook_list_marshal_check (GHookList *hook_list,
1281 gboolean may_recurse,
1282 GHookCheckMarshaller marshaller,
1286 /* Fatal error handlers.
1287 * g_on_error_query() will prompt the user to either
1288 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1289 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1290 * process and shows a stack trace.
1291 * These function may cause different actions on non-unix platforms.
1292 * The prg_name arg is required by gdb to find the executable, if it is
1293 * passed as NULL, g_on_error_query() will try g_get_prgname().
1295 void g_on_error_query (const gchar *prg_name);
1296 void g_on_error_stack_trace (const gchar *prg_name);
1299 /* Logging mechanism
1301 extern const gchar *g_log_domain_glib;
1302 guint g_log_set_handler (const gchar *log_domain,
1303 GLogLevelFlags log_levels,
1305 gpointer user_data);
1306 void g_log_remove_handler (const gchar *log_domain,
1308 void g_log_default_handler (const gchar *log_domain,
1309 GLogLevelFlags log_level,
1310 const gchar *message,
1311 gpointer unused_data);
1312 void g_log (const gchar *log_domain,
1313 GLogLevelFlags log_level,
1314 const gchar *format,
1315 ...) G_GNUC_PRINTF (3, 4);
1316 void g_logv (const gchar *log_domain,
1317 GLogLevelFlags log_level,
1318 const gchar *format,
1320 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1321 GLogLevelFlags fatal_mask);
1322 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1323 #ifndef G_LOG_DOMAIN
1324 #define G_LOG_DOMAIN ((gchar*) 0)
1325 #endif /* G_LOG_DOMAIN */
1327 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1328 G_LOG_LEVEL_ERROR, \
1330 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1331 G_LOG_LEVEL_MESSAGE, \
1333 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1334 G_LOG_LEVEL_WARNING, \
1336 #else /* !__GNUC__ */
1338 g_error (const gchar *format,
1342 va_start (args, format);
1343 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1347 g_message (const gchar *format,
1351 va_start (args, format);
1352 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1356 g_warning (const gchar *format,
1360 va_start (args, format);
1361 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1364 #endif /* !__GNUC__ */
1366 typedef void (*GPrintFunc) (const gchar *string);
1367 void g_print (const gchar *format,
1368 ...) G_GNUC_PRINTF (1, 2);
1369 GPrintFunc g_set_print_handler (GPrintFunc func);
1370 void g_printerr (const gchar *format,
1371 ...) G_GNUC_PRINTF (1, 2);
1372 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1374 /* deprecated compatibility functions, use g_log_set_handler() instead */
1375 typedef void (*GErrorFunc) (const gchar *str);
1376 typedef void (*GWarningFunc) (const gchar *str);
1377 GErrorFunc g_set_error_handler (GErrorFunc func);
1378 GWarningFunc g_set_warning_handler (GWarningFunc func);
1379 GPrintFunc g_set_message_handler (GPrintFunc func);
1382 /* Memory allocation and debugging
1386 #define g_malloc(size) ((gpointer) MALLOC (size))
1387 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1388 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1389 #define g_free(mem) FREE (mem)
1391 #else /* !USE_DMALLOC */
1393 gpointer g_malloc (gulong size);
1394 gpointer g_malloc0 (gulong size);
1395 gpointer g_realloc (gpointer mem,
1397 void g_free (gpointer mem);
1399 #endif /* !USE_DMALLOC */
1401 void g_mem_profile (void);
1402 void g_mem_check (gpointer mem);
1404 /* Generic allocators
1406 GAllocator* g_allocator_new (const gchar *name,
1408 void g_allocator_free (GAllocator *allocator);
1410 #define G_ALLOCATOR_LIST (1)
1411 #define G_ALLOCATOR_SLIST (2)
1412 #define G_ALLOCATOR_NODE (3)
1415 /* "g_mem_chunk_new" creates a new memory chunk.
1416 * Memory chunks are used to allocate pieces of memory which are
1417 * always the same size. Lists are a good example of such a data type.
1418 * The memory chunk allocates and frees blocks of memory as needed.
1419 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1420 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1421 * fault...somewhere).
1423 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1424 * want to know what's going on inside do you?)
1427 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1428 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1429 * atom. (They are also useful for lists which use MemChunk to allocate
1430 * memory but are also part of the MemChunk implementation).
1431 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1434 #define G_ALLOC_ONLY 1
1435 #define G_ALLOC_AND_FREE 2
1437 GMemChunk* g_mem_chunk_new (gchar *name,
1441 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1442 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1443 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1444 void g_mem_chunk_free (GMemChunk *mem_chunk,
1446 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1447 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1448 void g_mem_chunk_print (GMemChunk *mem_chunk);
1449 void g_mem_chunk_info (void);
1451 /* Ah yes...we have a "g_blow_chunks" function.
1452 * "g_blow_chunks" simply compresses all the chunks. This operation
1453 * consists of freeing every memory area that should be freed (but
1454 * which we haven't gotten around to doing yet). And, no,
1455 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1456 * much better name than "g_mem_chunk_clean_all" or something
1459 void g_blow_chunks (void);
1465 #define G_MICROSEC 1000000
1467 GTimer* g_timer_new (void);
1468 void g_timer_destroy (GTimer *timer);
1469 void g_timer_start (GTimer *timer);
1470 void g_timer_stop (GTimer *timer);
1471 void g_timer_reset (GTimer *timer);
1472 gdouble g_timer_elapsed (GTimer *timer,
1473 gulong *microseconds);
1474 void g_usleep (gulong microseconds);
1476 /* String utility functions that modify a string argument or
1477 * return a constant string that must not be freed.
1479 #define G_STR_DELIMITERS "_-|> <."
1480 gchar* g_strdelimit (gchar *string,
1481 const gchar *delimiters,
1482 gchar new_delimiter);
1483 gdouble g_strtod (const gchar *nptr,
1485 gchar* g_strerror (gint errnum);
1486 gchar* g_strsignal (gint signum);
1487 gint g_strcasecmp (const gchar *s1,
1489 gint g_strncasecmp (const gchar *s1,
1492 void g_strdown (gchar *string);
1493 void g_strup (gchar *string);
1494 void g_strreverse (gchar *string);
1495 /* removes leading spaces */
1496 gchar* g_strchug (gchar *string);
1497 /* removes trailing spaces */
1498 gchar* g_strchomp (gchar *string);
1499 /* removes leading & trailing spaces */
1500 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1502 /* String utility functions that return a newly allocated string which
1503 * ought to be freed from the caller at some point.
1505 gchar* g_strdup (const gchar *str);
1506 gchar* g_strdup_printf (const gchar *format,
1507 ...) G_GNUC_PRINTF (1, 2);
1508 gchar* g_strdup_vprintf (const gchar *format,
1510 gchar* g_strndup (const gchar *str,
1512 gchar* g_strnfill (guint length,
1514 gchar* g_strconcat (const gchar *string1,
1515 ...); /* NULL terminated */
1516 gchar* g_strjoin (const gchar *separator,
1517 ...); /* NULL terminated */
1518 /* deprecated function */
1519 gchar* g_strescape (gchar *string);
1521 gpointer g_memdup (gconstpointer mem,
1524 /* NULL terminated string arrays.
1525 * g_strsplit() splits up string into max_tokens tokens at delim and
1526 * returns a newly allocated string array.
1527 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1528 * optional separator, the returned string is newly allocated.
1529 * g_strfreev() frees the array itself and all of its strings.
1531 gchar** g_strsplit (const gchar *string,
1532 const gchar *delimiter,
1534 gchar* g_strjoinv (const gchar *separator,
1536 void g_strfreev (gchar **str_array);
1540 /* calculate a string size, guarranteed to fit format + args.
1542 guint g_printf_string_upper_bound (const gchar* format,
1546 /* Retrive static string info
1548 gchar* g_get_user_name (void);
1549 gchar* g_get_real_name (void);
1550 gchar* g_get_home_dir (void);
1551 gchar* g_get_tmp_dir (void);
1552 gchar* g_get_prgname (void);
1553 void g_set_prgname (const gchar *prgname);
1556 /* Miscellaneous utility functions
1558 guint g_parse_debug_string (const gchar *string,
1561 gint g_snprintf (gchar *string,
1563 gchar const *format,
1564 ...) G_GNUC_PRINTF (3, 4);
1565 gint g_vsnprintf (gchar *string,
1567 gchar const *format,
1569 gchar* g_basename (const gchar *file_name);
1570 /* Check if a file name is an absolute path */
1571 gboolean g_path_is_absolute (const gchar *file_name);
1572 /* In case of absolute paths, skip the root part */
1573 gchar* g_path_skip_root (gchar *file_name);
1575 /* strings are newly allocated with g_malloc() */
1576 gchar* g_dirname (const gchar *file_name);
1577 gchar* g_get_current_dir (void);
1578 gchar* g_getenv (const gchar *variable);
1581 /* we use a GLib function as a replacement for ATEXIT, so
1582 * the programmer is not required to check the return value
1583 * (if there is any in the implementation) and doesn't encounter
1584 * missing include files.
1586 void g_atexit (GVoidFunc func);
1591 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1595 g_bit_nth_lsf (guint32 mask,
1601 if (mask & (1 << (guint) nth_bit))
1604 while (nth_bit < 32);
1607 #endif /* G_CAN_INLINE */
1609 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1613 g_bit_nth_msf (guint32 mask,
1621 if (mask & (1 << (guint) nth_bit))
1624 while (nth_bit > 0);
1627 #endif /* G_CAN_INLINE */
1629 G_INLINE_FUNC guint g_bit_storage (guint number);
1632 g_bit_storage (guint number)
1634 register guint n_bits = 0;
1644 #endif /* G_CAN_INLINE */
1648 * elements need to be >= sizeof (gpointer)
1650 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1654 g_trash_stack_push (GTrashStack **stack_p,
1657 GTrashStack *data = data_p;
1659 data->next = *stack_p;
1662 #endif /* G_CAN_INLINE */
1664 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1666 G_INLINE_FUNC gpointer
1667 g_trash_stack_pop (GTrashStack **stack_p)
1674 *stack_p = data->next;
1675 memset (data, 0, sizeof (GTrashStack));
1680 #endif /* G_CAN_INLINE */
1682 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1684 G_INLINE_FUNC gpointer
1685 g_trash_stack_peek (GTrashStack **stack_p)
1693 #endif /* G_CAN_INLINE */
1695 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1698 g_trash_stack_height (GTrashStack **stack_p)
1703 for (data = *stack_p; data; data = data->next)
1708 #endif /* G_CAN_INLINE */
1713 GStringChunk* g_string_chunk_new (gint size);
1714 void g_string_chunk_free (GStringChunk *chunk);
1715 gchar* g_string_chunk_insert (GStringChunk *chunk,
1716 const gchar *string);
1717 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1718 const gchar *string);
1723 GString* g_string_new (const gchar *init);
1724 GString* g_string_sized_new (guint dfl_size);
1725 void g_string_free (GString *string,
1727 GString* g_string_assign (GString *lval,
1729 GString* g_string_truncate (GString *string,
1731 GString* g_string_append (GString *string,
1733 GString* g_string_append_c (GString *string,
1735 GString* g_string_prepend (GString *string,
1737 GString* g_string_prepend_c (GString *string,
1739 GString* g_string_insert (GString *string,
1742 GString* g_string_insert_c (GString *string,
1745 GString* g_string_erase (GString *string,
1748 GString* g_string_down (GString *string);
1749 GString* g_string_up (GString *string);
1750 void g_string_sprintf (GString *string,
1751 const gchar *format,
1752 ...) G_GNUC_PRINTF (2, 3);
1753 void g_string_sprintfa (GString *string,
1754 const gchar *format,
1755 ...) G_GNUC_PRINTF (2, 3);
1758 /* Resizable arrays, remove fills any cleared spot and shortens the
1759 * array, while preserving the order. remove_fast will distort the
1760 * order by moving the last element to the position of the removed
1763 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1764 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1765 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1766 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1768 GArray* g_array_new (gboolean zero_terminated,
1770 guint element_size);
1771 void g_array_free (GArray *array,
1772 gboolean free_segment);
1773 GArray* g_array_append_vals (GArray *array,
1776 GArray* g_array_prepend_vals (GArray *array,
1779 GArray* g_array_insert_vals (GArray *array,
1783 GArray* g_array_set_size (GArray *array,
1785 GArray* g_array_remove_index (GArray *array,
1787 GArray* g_array_remove_index_fast (GArray *array,
1790 /* Resizable pointer array. This interface is much less complicated
1791 * than the above. Add appends appends a pointer. Remove fills any
1792 * cleared spot and shortens the array. remove_fast will again distort
1795 #define g_ptr_array_index(array,index) (array->pdata)[index]
1796 GPtrArray* g_ptr_array_new (void);
1797 void g_ptr_array_free (GPtrArray *array,
1799 void g_ptr_array_set_size (GPtrArray *array,
1801 gpointer g_ptr_array_remove_index (GPtrArray *array,
1803 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1805 gboolean g_ptr_array_remove (GPtrArray *array,
1807 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1809 void g_ptr_array_add (GPtrArray *array,
1812 /* Byte arrays, an array of guint8. Implemented as a GArray,
1816 GByteArray* g_byte_array_new (void);
1817 void g_byte_array_free (GByteArray *array,
1818 gboolean free_segment);
1819 GByteArray* g_byte_array_append (GByteArray *array,
1822 GByteArray* g_byte_array_prepend (GByteArray *array,
1825 GByteArray* g_byte_array_set_size (GByteArray *array,
1827 GByteArray* g_byte_array_remove_index (GByteArray *array,
1829 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1835 gint g_str_equal (gconstpointer v,
1837 guint g_str_hash (gconstpointer v);
1839 gint g_int_equal (gconstpointer v,
1841 guint g_int_hash (gconstpointer v);
1843 /* This "hash" function will just return the key's adress as an
1844 * unsigned integer. Useful for hashing on plain adresses or
1845 * simple integer values.
1846 * passing NULL into g_hash_table_new() as GHashFunc has the
1847 * same effect as passing g_direct_hash().
1849 guint g_direct_hash (gconstpointer v);
1850 gint g_direct_equal (gconstpointer v,
1854 /* Quarks (string<->id association)
1856 GQuark g_quark_try_string (const gchar *string);
1857 GQuark g_quark_from_static_string (const gchar *string);
1858 GQuark g_quark_from_string (const gchar *string);
1859 gchar* g_quark_to_string (GQuark quark);
1864 void g_datalist_init (GData **datalist);
1865 void g_datalist_clear (GData **datalist);
1866 gpointer g_datalist_id_get_data (GData **datalist,
1868 void g_datalist_id_set_data_full (GData **datalist,
1871 GDestroyNotify destroy_func);
1872 void g_datalist_id_remove_no_notify (GData **datalist,
1874 void g_datalist_foreach (GData **datalist,
1875 GDataForeachFunc func,
1876 gpointer user_data);
1877 #define g_datalist_id_set_data(dl, q, d) \
1878 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1879 #define g_datalist_id_remove_data(dl, q) \
1880 g_datalist_id_set_data ((dl), (q), NULL)
1881 #define g_datalist_get_data(dl, k) \
1882 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1883 #define g_datalist_set_data_full(dl, k, d, f) \
1884 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1885 #define g_datalist_remove_no_notify(dl, k) \
1886 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1887 #define g_datalist_set_data(dl, k, d) \
1888 g_datalist_set_data_full ((dl), (k), (d), NULL)
1889 #define g_datalist_remove_data(dl, k) \
1890 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1893 /* Location Associated Keyed Data
1895 void g_dataset_destroy (gconstpointer dataset_location);
1896 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1898 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1901 GDestroyNotify destroy_func);
1902 void g_dataset_id_remove_no_notify (gconstpointer dataset_location,
1904 void g_dataset_foreach (gconstpointer dataset_location,
1905 GDataForeachFunc func,
1906 gpointer user_data);
1907 #define g_dataset_id_set_data(l, k, d) \
1908 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1909 #define g_dataset_id_remove_data(l, k) \
1910 g_dataset_id_set_data ((l), (k), NULL)
1911 #define g_dataset_get_data(l, k) \
1912 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1913 #define g_dataset_set_data_full(l, k, d, f) \
1914 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1915 #define g_dataset_remove_no_notify(l, k) \
1916 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1917 #define g_dataset_set_data(l, k, d) \
1918 g_dataset_set_data_full ((l), (k), (d), NULL)
1919 #define g_dataset_remove_data(l, k) \
1920 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1923 /* GScanner: Flexible lexical scanner for general purpose.
1926 /* Character sets */
1927 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1928 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1929 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1930 "\307\310\311\312\313\314\315\316\317\320"\
1931 "\321\322\323\324\325\326"\
1932 "\330\331\332\333\334\335\336"
1933 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1934 "\347\350\351\352\353\354\355\356\357\360"\
1935 "\361\362\363\364\365\366"\
1936 "\370\371\372\373\374\375\376\377"
1943 G_ERR_UNEXP_EOF_IN_STRING,
1944 G_ERR_UNEXP_EOF_IN_COMMENT,
1945 G_ERR_NON_DIGIT_IN_CONST,
1948 G_ERR_FLOAT_MALFORMED
1956 G_TOKEN_LEFT_PAREN = '(',
1957 G_TOKEN_RIGHT_PAREN = ')',
1958 G_TOKEN_LEFT_CURLY = '{',
1959 G_TOKEN_RIGHT_CURLY = '}',
1960 G_TOKEN_LEFT_BRACE = '[',
1961 G_TOKEN_RIGHT_BRACE = ']',
1962 G_TOKEN_EQUAL_SIGN = '=',
1963 G_TOKEN_COMMA = ',',
1979 G_TOKEN_IDENTIFIER_NULL,
1981 G_TOKEN_COMMENT_SINGLE,
1982 G_TOKEN_COMMENT_MULTI,
1989 gchar *v_identifier;
2001 struct _GScannerConfig
2005 gchar *cset_skip_characters; /* default: " \t\n" */
2006 gchar *cset_identifier_first;
2007 gchar *cset_identifier_nth;
2008 gchar *cpair_comment_single; /* default: "#\n" */
2010 /* Should symbol lookup work case sensitive?
2012 guint case_sensitive : 1;
2014 /* Boolean values to be adjusted "on the fly"
2015 * to configure scanning behaviour.
2017 guint skip_comment_multi : 1; /* C like comment */
2018 guint skip_comment_single : 1; /* single line comment */
2019 guint scan_comment_multi : 1; /* scan multi line comments? */
2020 guint scan_identifier : 1;
2021 guint scan_identifier_1char : 1;
2022 guint scan_identifier_NULL : 1;
2023 guint scan_symbols : 1;
2024 guint scan_binary : 1;
2025 guint scan_octal : 1;
2026 guint scan_float : 1;
2027 guint scan_hex : 1; /* `0x0ff0' */
2028 guint scan_hex_dollar : 1; /* `$0ff0' */
2029 guint scan_string_sq : 1; /* string: 'anything' */
2030 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2031 guint numbers_2_int : 1; /* bin, octal, hex => int */
2032 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2033 guint identifier_2_string : 1;
2034 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2035 guint symbol_2_token : 1;
2036 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2043 guint max_parse_errors;
2045 /* g_scanner_error() increments this field */
2048 /* name of input stream, featured by the default message handler */
2049 const gchar *input_name;
2051 /* data pointer for derived structures */
2052 gpointer derived_data;
2054 /* link into the scanner configuration */
2055 GScannerConfig *config;
2057 /* fields filled in after g_scanner_get_next_token() */
2063 /* fields filled in after g_scanner_peek_next_token() */
2064 GTokenType next_token;
2065 GTokenValue next_value;
2067 guint next_position;
2069 /* to be considered private */
2070 GHashTable *symbol_table;
2073 const gchar *text_end;
2077 /* handler function for _warn and _error */
2078 GScannerMsgFunc msg_handler;
2081 GScanner* g_scanner_new (GScannerConfig *config_templ);
2082 void g_scanner_destroy (GScanner *scanner);
2083 void g_scanner_input_file (GScanner *scanner,
2085 void g_scanner_sync_file_offset (GScanner *scanner);
2086 void g_scanner_input_text (GScanner *scanner,
2089 GTokenType g_scanner_get_next_token (GScanner *scanner);
2090 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2091 GTokenType g_scanner_cur_token (GScanner *scanner);
2092 GTokenValue g_scanner_cur_value (GScanner *scanner);
2093 guint g_scanner_cur_line (GScanner *scanner);
2094 guint g_scanner_cur_position (GScanner *scanner);
2095 gboolean g_scanner_eof (GScanner *scanner);
2096 guint g_scanner_set_scope (GScanner *scanner,
2098 void g_scanner_scope_add_symbol (GScanner *scanner,
2100 const gchar *symbol,
2102 void g_scanner_scope_remove_symbol (GScanner *scanner,
2104 const gchar *symbol);
2105 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2107 const gchar *symbol);
2108 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2111 gpointer user_data);
2112 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2113 const gchar *symbol);
2114 void g_scanner_freeze_symbol_table (GScanner *scanner);
2115 void g_scanner_thaw_symbol_table (GScanner *scanner);
2116 void g_scanner_unexp_token (GScanner *scanner,
2117 GTokenType expected_token,
2118 const gchar *identifier_spec,
2119 const gchar *symbol_spec,
2120 const gchar *symbol_name,
2121 const gchar *message,
2123 void g_scanner_error (GScanner *scanner,
2124 const gchar *format,
2125 ...) G_GNUC_PRINTF (2,3);
2126 void g_scanner_warn (GScanner *scanner,
2127 const gchar *format,
2128 ...) G_GNUC_PRINTF (2,3);
2129 gint g_scanner_stat_mode (const gchar *filename);
2130 /* keep downward source compatibility */
2131 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2132 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2134 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2135 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2137 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2138 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2148 GCompletionFunc func;
2154 GCompletion* g_completion_new (GCompletionFunc func);
2155 void g_completion_add_items (GCompletion* cmp,
2157 void g_completion_remove_items (GCompletion* cmp,
2159 void g_completion_clear_items (GCompletion* cmp);
2160 GList* g_completion_complete (GCompletion* cmp,
2162 gchar** new_prefix);
2163 void g_completion_free (GCompletion* cmp);
2168 * Date calculations (not time for now, to be resolved). These are a
2169 * mutant combination of Steffen Beyer's DateCalc routines
2170 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2171 * date routines (written for in-house software). Written by Havoc
2172 * Pennington <hp@pobox.com>
2175 typedef guint16 GDateYear;
2176 typedef guint8 GDateDay; /* day of the month */
2177 typedef struct _GDate GDate;
2178 /* make struct tm known without having to include time.h */
2181 /* enum used to specify order of appearance in parsed date strings */
2189 /* actual week and month values */
2192 G_DATE_BAD_WEEKDAY = 0,
2195 G_DATE_WEDNESDAY = 3,
2196 G_DATE_THURSDAY = 4,
2198 G_DATE_SATURDAY = 6,
2203 G_DATE_BAD_MONTH = 0,
2205 G_DATE_FEBRUARY = 2,
2212 G_DATE_SEPTEMBER = 9,
2213 G_DATE_OCTOBER = 10,
2214 G_DATE_NOVEMBER = 11,
2215 G_DATE_DECEMBER = 12
2218 #define G_DATE_BAD_JULIAN 0U
2219 #define G_DATE_BAD_DAY 0U
2220 #define G_DATE_BAD_YEAR 0U
2222 /* Note: directly manipulating structs is generally a bad idea, but
2223 * in this case it's an *incredibly* bad idea, because all or part
2224 * of this struct can be invalid at any given time. Use the functions,
2225 * or you will get hosed, I promise.
2229 guint julian_days : 32; /* julian days representation - we use a
2230 * bitfield hoping that 64 bit platforms
2231 * will pack this whole struct in one big
2235 guint julian : 1; /* julian is valid */
2236 guint dmy : 1; /* dmy is valid */
2238 /* DMY representation */
2244 /* g_date_new() returns an invalid date, you then have to _set() stuff
2245 * to get a usable object. You can also allocate a GDate statically,
2246 * then call g_date_clear() to initialize.
2248 GDate* g_date_new (void);
2249 GDate* g_date_new_dmy (GDateDay day,
2252 GDate* g_date_new_julian (guint32 julian_day);
2253 void g_date_free (GDate *date);
2255 /* check g_date_valid() after doing an operation that might fail, like
2256 * _parse. Almost all g_date operations are undefined on invalid
2257 * dates (the exceptions are the mutators, since you need those to
2258 * return to validity).
2260 gboolean g_date_valid (GDate *date);
2261 gboolean g_date_valid_day (GDateDay day);
2262 gboolean g_date_valid_month (GDateMonth month);
2263 gboolean g_date_valid_year (GDateYear year);
2264 gboolean g_date_valid_weekday (GDateWeekday weekday);
2265 gboolean g_date_valid_julian (guint32 julian_date);
2266 gboolean g_date_valid_dmy (GDateDay day,
2270 GDateWeekday g_date_weekday (GDate *date);
2271 GDateMonth g_date_month (GDate *date);
2272 GDateYear g_date_year (GDate *date);
2273 GDateDay g_date_day (GDate *date);
2274 guint32 g_date_julian (GDate *date);
2275 guint g_date_day_of_year (GDate *date);
2277 /* First monday/sunday is the start of week 1; if we haven't reached
2278 * that day, return 0. These are not ISO weeks of the year; that
2279 * routine needs to be added.
2280 * these functions return the number of weeks, starting on the
2283 guint g_date_monday_week_of_year (GDate *date);
2284 guint g_date_sunday_week_of_year (GDate *date);
2286 /* If you create a static date struct you need to clear it to get it
2287 * in a sane state before use. You can clear a whole array at
2288 * once with the ndates argument.
2290 void g_date_clear (GDate *date,
2293 /* The parse routine is meant for dates typed in by a user, so it
2294 * permits many formats but tries to catch common typos. If your data
2295 * needs to be strictly validated, it is not an appropriate function.
2297 void g_date_set_parse (GDate *date,
2299 void g_date_set_time (GDate *date,
2301 void g_date_set_month (GDate *date,
2303 void g_date_set_day (GDate *date,
2305 void g_date_set_year (GDate *date,
2307 void g_date_set_dmy (GDate *date,
2311 void g_date_set_julian (GDate *date,
2312 guint32 julian_date);
2313 gboolean g_date_is_first_of_month (GDate *date);
2314 gboolean g_date_is_last_of_month (GDate *date);
2316 /* To go forward by some number of weeks just go forward weeks*7 days */
2317 void g_date_add_days (GDate *date,
2319 void g_date_subtract_days (GDate *date,
2322 /* If you add/sub months while day > 28, the day might change */
2323 void g_date_add_months (GDate *date,
2325 void g_date_subtract_months (GDate *date,
2328 /* If it's feb 29, changing years can move you to the 28th */
2329 void g_date_add_years (GDate *date,
2331 void g_date_subtract_years (GDate *date,
2333 gboolean g_date_is_leap_year (GDateYear year);
2334 guint8 g_date_days_in_month (GDateMonth month,
2336 guint8 g_date_monday_weeks_in_year (GDateYear year);
2337 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2339 /* qsort-friendly (with a cast...) */
2340 gint g_date_compare (GDate *lhs,
2342 void g_date_to_struct_tm (GDate *date,
2345 /* Just like strftime() except you can only use date-related formats.
2346 * Using a time format is undefined.
2348 gsize g_date_strftime (gchar *s,
2350 const gchar *format,
2356 * Indexed Relations. Imagine a really simple table in a
2357 * database. Relations are not ordered. This data type is meant for
2358 * maintaining a N-way mapping.
2360 * g_relation_new() creates a relation with FIELDS fields
2362 * g_relation_destroy() frees all resources
2363 * g_tuples_destroy() frees the result of g_relation_select()
2365 * g_relation_index() indexes relation FIELD with the provided
2366 * equality and hash functions. this must be done before any
2367 * calls to insert are made.
2369 * g_relation_insert() inserts a new tuple. you are expected to
2370 * provide the right number of fields.
2372 * g_relation_delete() deletes all relations with KEY in FIELD
2373 * g_relation_select() returns ...
2374 * g_relation_count() counts ...
2377 GRelation* g_relation_new (gint fields);
2378 void g_relation_destroy (GRelation *relation);
2379 void g_relation_index (GRelation *relation,
2381 GHashFunc hash_func,
2382 GCompareFunc key_compare_func);
2383 void g_relation_insert (GRelation *relation,
2385 gint g_relation_delete (GRelation *relation,
2388 GTuples* g_relation_select (GRelation *relation,
2391 gint g_relation_count (GRelation *relation,
2394 gboolean g_relation_exists (GRelation *relation,
2396 void g_relation_print (GRelation *relation);
2398 void g_tuples_destroy (GTuples *tuples);
2399 gpointer g_tuples_index (GTuples *tuples,
2404 /* GRand - a good and fast random number generator: Mersenne Twister
2405 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2406 * The range functions return a value in the intervall [min,max).
2407 * int -> [0..2^32-1]
2408 * int_range -> [min..max-1]
2410 * double_range -> [min..max)
2413 GRand* g_rand_new_with_seed (guint32 seed);
2414 GRand* g_rand_new (void);
2415 void g_rand_free (GRand *rand);
2417 void g_rand_set_seed (GRand *rand,
2419 guint32 g_rand_int (GRand *rand);
2420 gint32 g_rand_int_range (GRand *rand,
2423 gdouble g_rand_double (GRand *rand);
2424 gdouble g_rand_double_range (GRand *rand,
2428 void g_random_set_seed (guint32 seed);
2429 guint32 g_random_int (void);
2430 gint32 g_random_int_range (gint32 min,
2432 gdouble g_random_double (void);
2433 gdouble g_random_double_range (gdouble min,
2440 /* This function returns prime numbers spaced by approximately 1.5-2.0
2441 * and is for use in resizing data structures which prefer
2442 * prime-valued sizes. The closest spaced prime function returns the
2443 * next largest prime, or the highest it knows about which is about
2446 guint g_spaced_primes_closest (guint num);
2452 typedef struct _GIOFuncs GIOFuncs;
2468 G_IO_IN GLIB_SYSDEF_POLLIN,
2469 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2470 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2471 G_IO_ERR GLIB_SYSDEF_POLLERR,
2472 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2473 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2478 guint channel_flags;
2483 typedef gboolean (*GIOFunc) (GIOChannel *source,
2484 GIOCondition condition,
2488 GIOError (*io_read) (GIOChannel *channel,
2492 GIOError (*io_write) (GIOChannel *channel,
2495 guint *bytes_written);
2496 GIOError (*io_seek) (GIOChannel *channel,
2499 void (*io_close) (GIOChannel *channel);
2500 guint (*io_add_watch) (GIOChannel *channel,
2502 GIOCondition condition,
2505 GDestroyNotify notify);
2506 void (*io_free) (GIOChannel *channel);
2509 void g_io_channel_init (GIOChannel *channel);
2510 void g_io_channel_ref (GIOChannel *channel);
2511 void g_io_channel_unref (GIOChannel *channel);
2512 GIOError g_io_channel_read (GIOChannel *channel,
2516 GIOError g_io_channel_write (GIOChannel *channel,
2519 guint *bytes_written);
2520 GIOError g_io_channel_seek (GIOChannel *channel,
2523 void g_io_channel_close (GIOChannel *channel);
2524 guint g_io_add_watch_full (GIOChannel *channel,
2526 GIOCondition condition,
2529 GDestroyNotify notify);
2530 guint g_io_add_watch (GIOChannel *channel,
2531 GIOCondition condition,
2533 gpointer user_data);
2538 typedef struct _GTimeVal GTimeVal;
2539 typedef struct _GSourceFuncs GSourceFuncs;
2540 typedef struct _GMainLoop GMainLoop; /* Opaque */
2547 struct _GSourceFuncs
2549 gboolean (*prepare) (gpointer source_data,
2550 GTimeVal *current_time,
2552 gpointer user_data);
2553 gboolean (*check) (gpointer source_data,
2554 GTimeVal *current_time,
2555 gpointer user_data);
2556 gboolean (*dispatch) (gpointer source_data,
2557 GTimeVal *current_time,
2558 gpointer user_data);
2559 GDestroyNotify destroy;
2562 /* Standard priorities */
2564 #define G_PRIORITY_HIGH -100
2565 #define G_PRIORITY_DEFAULT 0
2566 #define G_PRIORITY_HIGH_IDLE 100
2567 #define G_PRIORITY_DEFAULT_IDLE 200
2568 #define G_PRIORITY_LOW 300
2570 typedef gboolean (*GSourceFunc) (gpointer data);
2572 /* Hooks for adding to the main loop */
2573 guint g_source_add (gint priority,
2574 gboolean can_recurse,
2575 GSourceFuncs *funcs,
2576 gpointer source_data,
2578 GDestroyNotify notify);
2579 gboolean g_source_remove (guint tag);
2580 gboolean g_source_remove_by_user_data (gpointer user_data);
2581 gboolean g_source_remove_by_source_data (gpointer source_data);
2582 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2583 gpointer user_data);
2585 void g_get_current_time (GTimeVal *result);
2587 /* Running the main loop */
2588 GMainLoop* g_main_new (gboolean is_running);
2589 void g_main_run (GMainLoop *loop);
2590 void g_main_quit (GMainLoop *loop);
2591 void g_main_destroy (GMainLoop *loop);
2592 gboolean g_main_is_running (GMainLoop *loop);
2594 /* Run a single iteration of the mainloop. If block is FALSE,
2597 gboolean g_main_iteration (gboolean may_block);
2599 /* See if any events are pending */
2600 gboolean g_main_pending (void);
2602 /* Idles and timeouts */
2603 guint g_timeout_add_full (gint priority,
2605 GSourceFunc function,
2607 GDestroyNotify notify);
2608 guint g_timeout_add (guint interval,
2609 GSourceFunc function,
2611 guint g_idle_add (GSourceFunc function,
2613 guint g_idle_add_full (gint priority,
2614 GSourceFunc function,
2616 GDestroyNotify destroy);
2617 gboolean g_idle_remove_by_data (gpointer data);
2621 * System-specific IO and main loop calls
2623 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2624 * descriptor as provided by the C runtime) that can be used by
2625 * MsgWaitForMultipleObjects. This does *not* include file handles
2626 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2627 * WSAEventSelect to signal events when a SOCKET is readable).
2629 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2630 * indicate polling for messages. These message queue GPollFDs should
2631 * be added with the g_main_poll_win32_msg_add function.
2633 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2634 * (GTK) programs, as GDK itself wants to read messages and convert them
2637 * So, unless you really know what you are doing, it's best not to try
2638 * to use the main loop polling stuff for your own needs on
2639 * Win32. It's really only written for the GIMP's needs so
2643 typedef struct _GPollFD GPollFD;
2644 typedef gint (*GPollFunc) (GPollFD *ufds,
2654 void g_main_add_poll (GPollFD *fd,
2656 void g_main_remove_poll (GPollFD *fd);
2657 void g_main_set_poll_func (GPollFunc func);
2659 /* On Unix, IO channels created with this function for any file
2660 * descriptor or socket.
2662 * On Win32, use this only for plain files opened with the MSVCRT (the
2663 * Microsoft run-time C library) _open(), including file descriptors
2664 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2665 * Actually, don't do even that, this code isn't done yet.
2667 * The term file descriptor as used in the context of Win32 refers to
2668 * the emulated Unix-like file descriptors MSVCRT provides.
2670 GIOChannel* g_io_channel_unix_new (int fd);
2671 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2675 GUTILS_C_VAR guint g_pipe_readable_msg;
2677 #define G_WIN32_MSG_HANDLE 19981206
2679 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2680 * should *not* use this. (In fact, I can't think of any program that
2681 * would want to use this, but it's here just for completeness's sake.
2683 void g_main_poll_win32_msg_add(gint priority,
2687 /* An IO channel for Windows messages for window handle hwnd. */
2688 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2690 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2691 * _pipe(), with no mechanism for the writer to tell the reader when
2692 * there is data in the pipe.
2694 * This is not really implemented yet.
2696 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2698 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2699 * Windows user messages used to signal data in the pipe for the
2702 * fd is the file descriptor. For the write end, peer is the thread id
2703 * of the reader, and peer_fd is his file descriptor for the read end
2706 * This is used by the GIMP, and works.
2708 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2712 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2716 void g_io_channel_win32_pipe_readable (int fd,
2719 /* Get the C runtime file descriptor of a channel. */
2720 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2722 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2723 * actually a SOCKET.
2725 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2729 /* Windows emulation stubs for common Unix functions
2732 # define MAXPATHLEN 1024
2738 /* These POSIXish functions are available in the Microsoft C library
2739 * prefixed with underscore (which of course technically speaking is
2740 * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2741 * prevents Microsoft from practically requiring you to include
2742 * <windows.h> every now and then...).
2744 * You still need to include the appropriate headers to get the
2745 * prototypes, like <stdio.h>, <io.h>, <direct.h> or <process.h>.
2747 * For some functions, we provide emulators in glib, which are prefixed
2750 # define getcwd _getcwd
2751 # define getpid _getpid
2752 # define access _access
2755 # define fileno _fileno
2757 # define fstat _fstat
2758 # define unlink _unlink
2761 # define write _write
2762 # define lseek _lseek
2763 # define close _close
2764 # define rmdir _rmdir
2765 # define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2766 # define popen _popen
2767 # define pclose _pclose
2768 # define fdopen _fdopen
2769 # define hypot _hypot
2770 # define ftruncate(fd, size) gwin_ftruncate (fd, size)
2771 # define opendir gwin_opendir
2772 # define readdir gwin_readdir
2773 # define rewinddir gwin_rewinddir
2774 # define closedir gwin_closedir
2775 # define NAME_MAX 255
2779 gboolean just_opened;
2780 guint find_file_handle;
2781 gpointer find_file_data;
2783 typedef struct DIR DIR;
2786 gchar d_name[NAME_MAX + 1];
2788 /* emulation functions */
2789 extern int gwin_ftruncate (gint f,
2791 DIR* gwin_opendir (const gchar *dirname);
2792 struct dirent* gwin_readdir (DIR *dir);
2793 void gwin_rewinddir (DIR *dir);
2794 gint gwin_closedir (DIR *dir);
2795 #endif /* NATIVE_WIN32 */
2798 /* GLib Thread support
2801 typedef void (*GThreadFunc) (gpointer value);
2805 G_THREAD_PRIORITY_LOW,
2806 G_THREAD_PRIORITY_NORMAL,
2807 G_THREAD_PRIORITY_HIGH,
2808 G_THREAD_PRIORITY_URGENT,
2811 typedef struct _GThread GThread;
2814 GThreadPriority priority;
2819 typedef struct _GMutex GMutex;
2820 typedef struct _GCond GCond;
2821 typedef struct _GPrivate GPrivate;
2822 typedef struct _GStaticPrivate GStaticPrivate;
2824 typedef struct _GThreadFunctions GThreadFunctions;
2825 struct _GThreadFunctions
2827 GMutex* (*mutex_new) (void);
2828 void (*mutex_lock) (GMutex *mutex);
2829 gboolean (*mutex_trylock) (GMutex *mutex);
2830 void (*mutex_unlock) (GMutex *mutex);
2831 void (*mutex_free) (GMutex *mutex);
2832 GCond* (*cond_new) (void);
2833 void (*cond_signal) (GCond *cond);
2834 void (*cond_broadcast) (GCond *cond);
2835 void (*cond_wait) (GCond *cond,
2837 gboolean (*cond_timed_wait) (GCond *cond,
2839 GTimeVal *end_time);
2840 void (*cond_free) (GCond *cond);
2841 GPrivate* (*private_new) (GDestroyNotify destructor);
2842 gpointer (*private_get) (GPrivate *private_key);
2843 void (*private_set) (GPrivate *private_key,
2845 gpointer (*thread_create) (GThreadFunc thread_func,
2850 GThreadPriority priority);
2851 void (*thread_yield) (void);
2852 void (*thread_join) (gpointer thread);
2853 void (*thread_exit) (void);
2854 void (*thread_set_priority)(gpointer thread,
2855 GThreadPriority priority);
2856 gpointer (*thread_self) (void);
2859 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
2860 GUTILS_C_VAR gboolean g_thread_use_default_impl;
2861 GUTILS_C_VAR gboolean g_threads_got_initialized;
2863 /* initializes the mutex/cond/private implementation for glib, might
2864 * only be called once, and must not be called directly or indirectly
2865 * from another glib-function, e.g. as a callback.
2867 void g_thread_init (GThreadFunctions *vtable);
2869 /* internal function for fallback static mutex implementation */
2870 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
2872 /* shorthands for conditional and unconditional function calls */
2873 #define G_THREAD_UF(name, arglist) \
2874 (*g_thread_functions_for_glib_use . name) arglist
2875 #define G_THREAD_CF(name, fail, arg) \
2876 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2877 /* keep in mind, all those mutexes and static mutexes are not
2878 * recursive in general, don't rely on that
2880 #define g_thread_supported() (g_threads_got_initialized)
2881 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
2882 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
2883 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
2884 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
2885 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
2886 #define g_cond_new() G_THREAD_UF (cond_new, ())
2887 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
2888 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
2889 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
2891 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
2892 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2896 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
2897 #define g_private_get(private_key) G_THREAD_CF (private_get, \
2898 ((gpointer)private_key), \
2900 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2901 (void) (private_key = \
2902 (GPrivate*) (value)), \
2903 (private_key, value))
2904 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
2905 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
2907 GThread* g_thread_create (GThreadFunc thread_func,
2912 GThreadPriority priority);
2913 GThread* g_thread_self ();
2914 void g_thread_join (GThread* thread);
2915 void g_thread_set_priority (GThread* thread,
2916 GThreadPriority priority);
2918 /* GStaticMutexes can be statically initialized with the value
2919 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2920 * much easier, than having to explicitly allocate the mutex before
2923 #define g_static_mutex_lock(mutex) \
2924 g_mutex_lock (g_static_mutex_get_mutex (mutex))
2925 #define g_static_mutex_trylock(mutex) \
2926 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2927 #define g_static_mutex_unlock(mutex) \
2928 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
2930 struct _GStaticPrivate
2934 #define G_STATIC_PRIVATE_INIT { 0 }
2935 gpointer g_static_private_get (GStaticPrivate *private_key);
2936 void g_static_private_set (GStaticPrivate *private_key,
2938 GDestroyNotify notify);
2939 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
2941 void g_static_private_set_for_thread (GStaticPrivate *private_key,
2944 GDestroyNotify notify);
2945 #ifndef G_STATIC_REC_MUTEX_INIT
2946 /* if GStaticRecMutex is not just a differently initialized GStaticMutex,
2947 * the following is done:
2948 * This can't be done in glibconfig.h, as GStaticPrivate and gboolean
2949 * are not yet known there
2951 typedef struct _GStaticRecMutex GStaticRecMutex;
2952 struct _GStaticRecMutex
2955 GStaticPrivate counter;
2957 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, G_STATIC_PRIVATE_INIT }
2958 void g_static_rec_mutex_lock (GStaticRecMutex* mutex);
2959 gboolean g_static_rec_mutex_trylock (GStaticRecMutex* mutex);
2960 void g_static_rec_mutex_unlock (GStaticRecMutex* mutex);
2961 #define g_static_rec_mutex_get_mutex(mutex) ((mutex)->mutex)
2962 #endif /* G_STATIC_REC_MUTEX_INIT */
2964 typedef struct _GStaticRWLock GStaticRWLock;
2965 struct _GStaticRWLock
2972 guint want_to_write;
2975 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
2977 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
2978 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
2979 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
2980 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
2981 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
2982 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
2983 void g_static_rw_lock_free (GStaticRWLock* lock);
2985 /* these are some convenience macros that expand to nothing if GLib
2986 * was configured with --disable-threads. for using StaticMutexes,
2987 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
2988 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
2989 * declare such an globally defined lock. name is a unique identifier
2990 * for the protected varibale or code portion. locking, testing and
2991 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
2992 * G_TRYLOCK() respectively.
2994 extern void glib_dummy_decl (void);
2995 #define G_LOCK_NAME(name) (g__ ## name ## _lock)
2996 #ifdef G_THREADS_ENABLED
2997 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
2998 # define G_LOCK_DEFINE(name) \
2999 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3000 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3002 # ifdef G_DEBUG_LOCKS
3003 # define G_LOCK(name) G_STMT_START{ \
3004 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3005 "file %s: line %d (%s): locking: %s ", \
3006 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3008 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3010 # define G_UNLOCK(name) G_STMT_START{ \
3011 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3012 "file %s: line %d (%s): unlocking: %s ", \
3013 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3015 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3017 # define G_TRYLOCK(name) G_STMT_START{ \
3018 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3019 "file %s: line %d (%s): try locking: %s ", \
3020 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3022 }G_STMT_END, g_static_mutex_trylock (&G_LOCK_NAME (name))
3023 # else /* !G_DEBUG_LOCKS */
3024 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3025 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3026 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3027 # endif /* !G_DEBUG_LOCKS */
3028 #else /* !G_THREADS_ENABLED */
3029 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3030 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3031 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3032 # define G_LOCK(name)
3033 # define G_UNLOCK(name)
3034 # define G_TRYLOCK(name) (FALSE)
3035 #endif /* !G_THREADS_ENABLED */
3039 #endif /* __cplusplus */
3042 #endif /* __G_LIB_H__ */