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 with g_free 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 /* Make a copy of a string interpreting C string -style escape
1519 * sequences. Inverse of g_strescape. The recognized sequences are \b
1520 * \f \n \r \t \\ \" and the octal format.
1522 gchar* g_strcompress (const gchar *source);
1524 /* Copy a string escaping nonprintable characters like in C strings.
1525 * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1526 * to a string containing characters that are not to be escaped.
1528 gchar* g_strescape (const gchar *source,
1529 const gchar *exceptions);
1531 * gchar* g_strescape (const gchar *source);
1532 * Luckily this function wasn't much used.
1533 * Add a second NULL parameter in calls for mostly identical semantics.
1536 gpointer g_memdup (gconstpointer mem,
1539 /* NULL terminated string arrays.
1540 * g_strsplit() splits up string into max_tokens tokens at delim and
1541 * returns a newly allocated string array.
1542 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1543 * optional separator, the returned string is newly allocated.
1544 * g_strfreev() frees the array itself and all of its strings.
1546 gchar** g_strsplit (const gchar *string,
1547 const gchar *delimiter,
1549 gchar* g_strjoinv (const gchar *separator,
1551 void g_strfreev (gchar **str_array);
1555 /* calculate a string size, guarranteed to fit format + args.
1557 guint g_printf_string_upper_bound (const gchar* format,
1561 /* Retrive static string info
1563 gchar* g_get_user_name (void);
1564 gchar* g_get_real_name (void);
1565 gchar* g_get_home_dir (void);
1566 gchar* g_get_tmp_dir (void);
1567 gchar* g_get_prgname (void);
1568 void g_set_prgname (const gchar *prgname);
1571 /* Miscellaneous utility functions
1573 guint g_parse_debug_string (const gchar *string,
1576 gint g_snprintf (gchar *string,
1578 gchar const *format,
1579 ...) G_GNUC_PRINTF (3, 4);
1580 gint g_vsnprintf (gchar *string,
1582 gchar const *format,
1584 gchar* g_basename (const gchar *file_name);
1585 /* Check if a file name is an absolute path */
1586 gboolean g_path_is_absolute (const gchar *file_name);
1587 /* In case of absolute paths, skip the root part */
1588 gchar* g_path_skip_root (gchar *file_name);
1590 /* strings are newly allocated with g_malloc() */
1591 gchar* g_dirname (const gchar *file_name);
1592 gchar* g_get_current_dir (void);
1593 gchar* g_getenv (const gchar *variable);
1596 /* we use a GLib function as a replacement for ATEXIT, so
1597 * the programmer is not required to check the return value
1598 * (if there is any in the implementation) and doesn't encounter
1599 * missing include files.
1601 void g_atexit (GVoidFunc func);
1606 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1610 g_bit_nth_lsf (guint32 mask,
1616 if (mask & (1 << (guint) nth_bit))
1619 while (nth_bit < 32);
1622 #endif /* G_CAN_INLINE */
1624 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1628 g_bit_nth_msf (guint32 mask,
1636 if (mask & (1 << (guint) nth_bit))
1639 while (nth_bit > 0);
1642 #endif /* G_CAN_INLINE */
1644 G_INLINE_FUNC guint g_bit_storage (guint number);
1647 g_bit_storage (guint number)
1649 register guint n_bits = 0;
1659 #endif /* G_CAN_INLINE */
1663 * elements need to be >= sizeof (gpointer)
1665 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1669 g_trash_stack_push (GTrashStack **stack_p,
1672 GTrashStack *data = data_p;
1674 data->next = *stack_p;
1677 #endif /* G_CAN_INLINE */
1679 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1681 G_INLINE_FUNC gpointer
1682 g_trash_stack_pop (GTrashStack **stack_p)
1689 *stack_p = data->next;
1690 /* NULLify private pointer here, most platforms store NULL as
1691 * subsequent 0 bytes
1698 #endif /* G_CAN_INLINE */
1700 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1702 G_INLINE_FUNC gpointer
1703 g_trash_stack_peek (GTrashStack **stack_p)
1711 #endif /* G_CAN_INLINE */
1713 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1716 g_trash_stack_height (GTrashStack **stack_p)
1721 for (data = *stack_p; data; data = data->next)
1726 #endif /* G_CAN_INLINE */
1731 GStringChunk* g_string_chunk_new (gint size);
1732 void g_string_chunk_free (GStringChunk *chunk);
1733 gchar* g_string_chunk_insert (GStringChunk *chunk,
1734 const gchar *string);
1735 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1736 const gchar *string);
1741 GString* g_string_new (const gchar *init);
1742 GString* g_string_sized_new (guint dfl_size);
1743 void g_string_free (GString *string,
1745 GString* g_string_assign (GString *lval,
1747 GString* g_string_truncate (GString *string,
1749 GString* g_string_append (GString *string,
1751 GString* g_string_append_c (GString *string,
1753 GString* g_string_prepend (GString *string,
1755 GString* g_string_prepend_c (GString *string,
1757 GString* g_string_insert (GString *string,
1760 GString* g_string_insert_c (GString *string,
1763 GString* g_string_erase (GString *string,
1766 GString* g_string_down (GString *string);
1767 GString* g_string_up (GString *string);
1768 void g_string_sprintf (GString *string,
1769 const gchar *format,
1770 ...) G_GNUC_PRINTF (2, 3);
1771 void g_string_sprintfa (GString *string,
1772 const gchar *format,
1773 ...) G_GNUC_PRINTF (2, 3);
1776 /* Resizable arrays, remove fills any cleared spot and shortens the
1777 * array, while preserving the order. remove_fast will distort the
1778 * order by moving the last element to the position of the removed
1781 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1782 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1783 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1784 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1786 GArray* g_array_new (gboolean zero_terminated,
1788 guint element_size);
1789 void g_array_free (GArray *array,
1790 gboolean free_segment);
1791 GArray* g_array_append_vals (GArray *array,
1794 GArray* g_array_prepend_vals (GArray *array,
1797 GArray* g_array_insert_vals (GArray *array,
1801 GArray* g_array_set_size (GArray *array,
1803 GArray* g_array_remove_index (GArray *array,
1805 GArray* g_array_remove_index_fast (GArray *array,
1808 /* Resizable pointer array. This interface is much less complicated
1809 * than the above. Add appends appends a pointer. Remove fills any
1810 * cleared spot and shortens the array. remove_fast will again distort
1813 #define g_ptr_array_index(array,index) (array->pdata)[index]
1814 GPtrArray* g_ptr_array_new (void);
1815 void g_ptr_array_free (GPtrArray *array,
1817 void g_ptr_array_set_size (GPtrArray *array,
1819 gpointer g_ptr_array_remove_index (GPtrArray *array,
1821 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1823 gboolean g_ptr_array_remove (GPtrArray *array,
1825 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1827 void g_ptr_array_add (GPtrArray *array,
1830 /* Byte arrays, an array of guint8. Implemented as a GArray,
1834 GByteArray* g_byte_array_new (void);
1835 void g_byte_array_free (GByteArray *array,
1836 gboolean free_segment);
1837 GByteArray* g_byte_array_append (GByteArray *array,
1840 GByteArray* g_byte_array_prepend (GByteArray *array,
1843 GByteArray* g_byte_array_set_size (GByteArray *array,
1845 GByteArray* g_byte_array_remove_index (GByteArray *array,
1847 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1853 gint g_str_equal (gconstpointer v,
1855 guint g_str_hash (gconstpointer v);
1857 gint g_int_equal (gconstpointer v,
1859 guint g_int_hash (gconstpointer v);
1861 /* This "hash" function will just return the key's adress as an
1862 * unsigned integer. Useful for hashing on plain adresses or
1863 * simple integer values.
1864 * passing NULL into g_hash_table_new() as GHashFunc has the
1865 * same effect as passing g_direct_hash().
1867 guint g_direct_hash (gconstpointer v);
1868 gint g_direct_equal (gconstpointer v,
1872 /* Quarks (string<->id association)
1874 GQuark g_quark_try_string (const gchar *string);
1875 GQuark g_quark_from_static_string (const gchar *string);
1876 GQuark g_quark_from_string (const gchar *string);
1877 gchar* g_quark_to_string (GQuark quark);
1882 void g_datalist_init (GData **datalist);
1883 void g_datalist_clear (GData **datalist);
1884 gpointer g_datalist_id_get_data (GData **datalist,
1886 void g_datalist_id_set_data_full (GData **datalist,
1889 GDestroyNotify destroy_func);
1890 void g_datalist_id_remove_no_notify (GData **datalist,
1892 void g_datalist_foreach (GData **datalist,
1893 GDataForeachFunc func,
1894 gpointer user_data);
1895 #define g_datalist_id_set_data(dl, q, d) \
1896 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1897 #define g_datalist_id_remove_data(dl, q) \
1898 g_datalist_id_set_data ((dl), (q), NULL)
1899 #define g_datalist_get_data(dl, k) \
1900 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1901 #define g_datalist_set_data_full(dl, k, d, f) \
1902 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1903 #define g_datalist_remove_no_notify(dl, k) \
1904 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1905 #define g_datalist_set_data(dl, k, d) \
1906 g_datalist_set_data_full ((dl), (k), (d), NULL)
1907 #define g_datalist_remove_data(dl, k) \
1908 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1911 /* Location Associated Keyed Data
1913 void g_dataset_destroy (gconstpointer dataset_location);
1914 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1916 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1919 GDestroyNotify destroy_func);
1920 void g_dataset_id_remove_no_notify (gconstpointer dataset_location,
1922 void g_dataset_foreach (gconstpointer dataset_location,
1923 GDataForeachFunc func,
1924 gpointer user_data);
1925 #define g_dataset_id_set_data(l, k, d) \
1926 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1927 #define g_dataset_id_remove_data(l, k) \
1928 g_dataset_id_set_data ((l), (k), NULL)
1929 #define g_dataset_get_data(l, k) \
1930 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1931 #define g_dataset_set_data_full(l, k, d, f) \
1932 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1933 #define g_dataset_remove_no_notify(l, k) \
1934 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1935 #define g_dataset_set_data(l, k, d) \
1936 g_dataset_set_data_full ((l), (k), (d), NULL)
1937 #define g_dataset_remove_data(l, k) \
1938 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1941 /* GScanner: Flexible lexical scanner for general purpose.
1944 /* Character sets */
1945 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1946 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1947 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1948 "\307\310\311\312\313\314\315\316\317\320"\
1949 "\321\322\323\324\325\326"\
1950 "\330\331\332\333\334\335\336"
1951 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1952 "\347\350\351\352\353\354\355\356\357\360"\
1953 "\361\362\363\364\365\366"\
1954 "\370\371\372\373\374\375\376\377"
1961 G_ERR_UNEXP_EOF_IN_STRING,
1962 G_ERR_UNEXP_EOF_IN_COMMENT,
1963 G_ERR_NON_DIGIT_IN_CONST,
1966 G_ERR_FLOAT_MALFORMED
1974 G_TOKEN_LEFT_PAREN = '(',
1975 G_TOKEN_RIGHT_PAREN = ')',
1976 G_TOKEN_LEFT_CURLY = '{',
1977 G_TOKEN_RIGHT_CURLY = '}',
1978 G_TOKEN_LEFT_BRACE = '[',
1979 G_TOKEN_RIGHT_BRACE = ']',
1980 G_TOKEN_EQUAL_SIGN = '=',
1981 G_TOKEN_COMMA = ',',
1997 G_TOKEN_IDENTIFIER_NULL,
1999 G_TOKEN_COMMENT_SINGLE,
2000 G_TOKEN_COMMENT_MULTI,
2007 gchar *v_identifier;
2019 struct _GScannerConfig
2023 gchar *cset_skip_characters; /* default: " \t\n" */
2024 gchar *cset_identifier_first;
2025 gchar *cset_identifier_nth;
2026 gchar *cpair_comment_single; /* default: "#\n" */
2028 /* Should symbol lookup work case sensitive?
2030 guint case_sensitive : 1;
2032 /* Boolean values to be adjusted "on the fly"
2033 * to configure scanning behaviour.
2035 guint skip_comment_multi : 1; /* C like comment */
2036 guint skip_comment_single : 1; /* single line comment */
2037 guint scan_comment_multi : 1; /* scan multi line comments? */
2038 guint scan_identifier : 1;
2039 guint scan_identifier_1char : 1;
2040 guint scan_identifier_NULL : 1;
2041 guint scan_symbols : 1;
2042 guint scan_binary : 1;
2043 guint scan_octal : 1;
2044 guint scan_float : 1;
2045 guint scan_hex : 1; /* `0x0ff0' */
2046 guint scan_hex_dollar : 1; /* `$0ff0' */
2047 guint scan_string_sq : 1; /* string: 'anything' */
2048 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2049 guint numbers_2_int : 1; /* bin, octal, hex => int */
2050 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2051 guint identifier_2_string : 1;
2052 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2053 guint symbol_2_token : 1;
2054 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2061 guint max_parse_errors;
2063 /* g_scanner_error() increments this field */
2066 /* name of input stream, featured by the default message handler */
2067 const gchar *input_name;
2069 /* data pointer for derived structures */
2070 gpointer derived_data;
2072 /* link into the scanner configuration */
2073 GScannerConfig *config;
2075 /* fields filled in after g_scanner_get_next_token() */
2081 /* fields filled in after g_scanner_peek_next_token() */
2082 GTokenType next_token;
2083 GTokenValue next_value;
2085 guint next_position;
2087 /* to be considered private */
2088 GHashTable *symbol_table;
2091 const gchar *text_end;
2095 /* handler function for _warn and _error */
2096 GScannerMsgFunc msg_handler;
2099 GScanner* g_scanner_new (GScannerConfig *config_templ);
2100 void g_scanner_destroy (GScanner *scanner);
2101 void g_scanner_input_file (GScanner *scanner,
2103 void g_scanner_sync_file_offset (GScanner *scanner);
2104 void g_scanner_input_text (GScanner *scanner,
2107 GTokenType g_scanner_get_next_token (GScanner *scanner);
2108 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2109 GTokenType g_scanner_cur_token (GScanner *scanner);
2110 GTokenValue g_scanner_cur_value (GScanner *scanner);
2111 guint g_scanner_cur_line (GScanner *scanner);
2112 guint g_scanner_cur_position (GScanner *scanner);
2113 gboolean g_scanner_eof (GScanner *scanner);
2114 guint g_scanner_set_scope (GScanner *scanner,
2116 void g_scanner_scope_add_symbol (GScanner *scanner,
2118 const gchar *symbol,
2120 void g_scanner_scope_remove_symbol (GScanner *scanner,
2122 const gchar *symbol);
2123 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2125 const gchar *symbol);
2126 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2129 gpointer user_data);
2130 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2131 const gchar *symbol);
2132 void g_scanner_freeze_symbol_table (GScanner *scanner);
2133 void g_scanner_thaw_symbol_table (GScanner *scanner);
2134 void g_scanner_unexp_token (GScanner *scanner,
2135 GTokenType expected_token,
2136 const gchar *identifier_spec,
2137 const gchar *symbol_spec,
2138 const gchar *symbol_name,
2139 const gchar *message,
2141 void g_scanner_error (GScanner *scanner,
2142 const gchar *format,
2143 ...) G_GNUC_PRINTF (2,3);
2144 void g_scanner_warn (GScanner *scanner,
2145 const gchar *format,
2146 ...) G_GNUC_PRINTF (2,3);
2147 gint g_scanner_stat_mode (const gchar *filename);
2148 /* keep downward source compatibility */
2149 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2150 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2152 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2153 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2155 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2156 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2166 GCompletionFunc func;
2172 GCompletion* g_completion_new (GCompletionFunc func);
2173 void g_completion_add_items (GCompletion* cmp,
2175 void g_completion_remove_items (GCompletion* cmp,
2177 void g_completion_clear_items (GCompletion* cmp);
2178 GList* g_completion_complete (GCompletion* cmp,
2180 gchar** new_prefix);
2181 void g_completion_free (GCompletion* cmp);
2186 * Date calculations (not time for now, to be resolved). These are a
2187 * mutant combination of Steffen Beyer's DateCalc routines
2188 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2189 * date routines (written for in-house software). Written by Havoc
2190 * Pennington <hp@pobox.com>
2193 typedef guint16 GDateYear;
2194 typedef guint8 GDateDay; /* day of the month */
2195 typedef struct _GDate GDate;
2196 /* make struct tm known without having to include time.h */
2199 /* enum used to specify order of appearance in parsed date strings */
2207 /* actual week and month values */
2210 G_DATE_BAD_WEEKDAY = 0,
2213 G_DATE_WEDNESDAY = 3,
2214 G_DATE_THURSDAY = 4,
2216 G_DATE_SATURDAY = 6,
2221 G_DATE_BAD_MONTH = 0,
2223 G_DATE_FEBRUARY = 2,
2230 G_DATE_SEPTEMBER = 9,
2231 G_DATE_OCTOBER = 10,
2232 G_DATE_NOVEMBER = 11,
2233 G_DATE_DECEMBER = 12
2236 #define G_DATE_BAD_JULIAN 0U
2237 #define G_DATE_BAD_DAY 0U
2238 #define G_DATE_BAD_YEAR 0U
2240 /* Note: directly manipulating structs is generally a bad idea, but
2241 * in this case it's an *incredibly* bad idea, because all or part
2242 * of this struct can be invalid at any given time. Use the functions,
2243 * or you will get hosed, I promise.
2247 guint julian_days : 32; /* julian days representation - we use a
2248 * bitfield hoping that 64 bit platforms
2249 * will pack this whole struct in one big
2253 guint julian : 1; /* julian is valid */
2254 guint dmy : 1; /* dmy is valid */
2256 /* DMY representation */
2262 /* g_date_new() returns an invalid date, you then have to _set() stuff
2263 * to get a usable object. You can also allocate a GDate statically,
2264 * then call g_date_clear() to initialize.
2266 GDate* g_date_new (void);
2267 GDate* g_date_new_dmy (GDateDay day,
2270 GDate* g_date_new_julian (guint32 julian_day);
2271 void g_date_free (GDate *date);
2273 /* check g_date_valid() after doing an operation that might fail, like
2274 * _parse. Almost all g_date operations are undefined on invalid
2275 * dates (the exceptions are the mutators, since you need those to
2276 * return to validity).
2278 gboolean g_date_valid (GDate *date);
2279 gboolean g_date_valid_day (GDateDay day);
2280 gboolean g_date_valid_month (GDateMonth month);
2281 gboolean g_date_valid_year (GDateYear year);
2282 gboolean g_date_valid_weekday (GDateWeekday weekday);
2283 gboolean g_date_valid_julian (guint32 julian_date);
2284 gboolean g_date_valid_dmy (GDateDay day,
2288 GDateWeekday g_date_weekday (GDate *date);
2289 GDateMonth g_date_month (GDate *date);
2290 GDateYear g_date_year (GDate *date);
2291 GDateDay g_date_day (GDate *date);
2292 guint32 g_date_julian (GDate *date);
2293 guint g_date_day_of_year (GDate *date);
2295 /* First monday/sunday is the start of week 1; if we haven't reached
2296 * that day, return 0. These are not ISO weeks of the year; that
2297 * routine needs to be added.
2298 * these functions return the number of weeks, starting on the
2301 guint g_date_monday_week_of_year (GDate *date);
2302 guint g_date_sunday_week_of_year (GDate *date);
2304 /* If you create a static date struct you need to clear it to get it
2305 * in a sane state before use. You can clear a whole array at
2306 * once with the ndates argument.
2308 void g_date_clear (GDate *date,
2311 /* The parse routine is meant for dates typed in by a user, so it
2312 * permits many formats but tries to catch common typos. If your data
2313 * needs to be strictly validated, it is not an appropriate function.
2315 void g_date_set_parse (GDate *date,
2317 void g_date_set_time (GDate *date,
2319 void g_date_set_month (GDate *date,
2321 void g_date_set_day (GDate *date,
2323 void g_date_set_year (GDate *date,
2325 void g_date_set_dmy (GDate *date,
2329 void g_date_set_julian (GDate *date,
2330 guint32 julian_date);
2331 gboolean g_date_is_first_of_month (GDate *date);
2332 gboolean g_date_is_last_of_month (GDate *date);
2334 /* To go forward by some number of weeks just go forward weeks*7 days */
2335 void g_date_add_days (GDate *date,
2337 void g_date_subtract_days (GDate *date,
2340 /* If you add/sub months while day > 28, the day might change */
2341 void g_date_add_months (GDate *date,
2343 void g_date_subtract_months (GDate *date,
2346 /* If it's feb 29, changing years can move you to the 28th */
2347 void g_date_add_years (GDate *date,
2349 void g_date_subtract_years (GDate *date,
2351 gboolean g_date_is_leap_year (GDateYear year);
2352 guint8 g_date_days_in_month (GDateMonth month,
2354 guint8 g_date_monday_weeks_in_year (GDateYear year);
2355 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2357 /* qsort-friendly (with a cast...) */
2358 gint g_date_compare (GDate *lhs,
2360 void g_date_to_struct_tm (GDate *date,
2363 /* Just like strftime() except you can only use date-related formats.
2364 * Using a time format is undefined.
2366 gsize g_date_strftime (gchar *s,
2368 const gchar *format,
2374 * Indexed Relations. Imagine a really simple table in a
2375 * database. Relations are not ordered. This data type is meant for
2376 * maintaining a N-way mapping.
2378 * g_relation_new() creates a relation with FIELDS fields
2380 * g_relation_destroy() frees all resources
2381 * g_tuples_destroy() frees the result of g_relation_select()
2383 * g_relation_index() indexes relation FIELD with the provided
2384 * equality and hash functions. this must be done before any
2385 * calls to insert are made.
2387 * g_relation_insert() inserts a new tuple. you are expected to
2388 * provide the right number of fields.
2390 * g_relation_delete() deletes all relations with KEY in FIELD
2391 * g_relation_select() returns ...
2392 * g_relation_count() counts ...
2395 GRelation* g_relation_new (gint fields);
2396 void g_relation_destroy (GRelation *relation);
2397 void g_relation_index (GRelation *relation,
2399 GHashFunc hash_func,
2400 GCompareFunc key_compare_func);
2401 void g_relation_insert (GRelation *relation,
2403 gint g_relation_delete (GRelation *relation,
2406 GTuples* g_relation_select (GRelation *relation,
2409 gint g_relation_count (GRelation *relation,
2412 gboolean g_relation_exists (GRelation *relation,
2414 void g_relation_print (GRelation *relation);
2416 void g_tuples_destroy (GTuples *tuples);
2417 gpointer g_tuples_index (GTuples *tuples,
2422 /* GRand - a good and fast random number generator: Mersenne Twister
2423 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2424 * The range functions return a value in the intervall [min,max).
2425 * int -> [0..2^32-1]
2426 * int_range -> [min..max-1]
2428 * double_range -> [min..max)
2431 GRand* g_rand_new_with_seed (guint32 seed);
2432 GRand* g_rand_new (void);
2433 void g_rand_free (GRand *rand);
2435 void g_rand_set_seed (GRand *rand,
2437 guint32 g_rand_int (GRand *rand);
2438 gint32 g_rand_int_range (GRand *rand,
2441 gdouble g_rand_double (GRand *rand);
2442 gdouble g_rand_double_range (GRand *rand,
2446 void g_random_set_seed (guint32 seed);
2447 guint32 g_random_int (void);
2448 gint32 g_random_int_range (gint32 min,
2450 gdouble g_random_double (void);
2451 gdouble g_random_double_range (gdouble min,
2458 /* This function returns prime numbers spaced by approximately 1.5-2.0
2459 * and is for use in resizing data structures which prefer
2460 * prime-valued sizes. The closest spaced prime function returns the
2461 * next largest prime, or the highest it knows about which is about
2464 guint g_spaced_primes_closest (guint num);
2470 typedef struct _GIOFuncs GIOFuncs;
2486 G_IO_IN GLIB_SYSDEF_POLLIN,
2487 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2488 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2489 G_IO_ERR GLIB_SYSDEF_POLLERR,
2490 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2491 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2496 guint channel_flags;
2501 typedef gboolean (*GIOFunc) (GIOChannel *source,
2502 GIOCondition condition,
2506 GIOError (*io_read) (GIOChannel *channel,
2510 GIOError (*io_write) (GIOChannel *channel,
2513 guint *bytes_written);
2514 GIOError (*io_seek) (GIOChannel *channel,
2517 void (*io_close) (GIOChannel *channel);
2518 guint (*io_add_watch) (GIOChannel *channel,
2520 GIOCondition condition,
2523 GDestroyNotify notify);
2524 void (*io_free) (GIOChannel *channel);
2527 void g_io_channel_init (GIOChannel *channel);
2528 void g_io_channel_ref (GIOChannel *channel);
2529 void g_io_channel_unref (GIOChannel *channel);
2530 GIOError g_io_channel_read (GIOChannel *channel,
2534 GIOError g_io_channel_write (GIOChannel *channel,
2537 guint *bytes_written);
2538 GIOError g_io_channel_seek (GIOChannel *channel,
2541 void g_io_channel_close (GIOChannel *channel);
2542 guint g_io_add_watch_full (GIOChannel *channel,
2544 GIOCondition condition,
2547 GDestroyNotify notify);
2548 guint g_io_add_watch (GIOChannel *channel,
2549 GIOCondition condition,
2551 gpointer user_data);
2556 typedef struct _GTimeVal GTimeVal;
2557 typedef struct _GSourceFuncs GSourceFuncs;
2558 typedef struct _GMainLoop GMainLoop; /* Opaque */
2565 struct _GSourceFuncs
2567 gboolean (*prepare) (gpointer source_data,
2568 GTimeVal *current_time,
2570 gpointer user_data);
2571 gboolean (*check) (gpointer source_data,
2572 GTimeVal *current_time,
2573 gpointer user_data);
2574 gboolean (*dispatch) (gpointer source_data,
2575 GTimeVal *current_time,
2576 gpointer user_data);
2577 GDestroyNotify destroy;
2580 /* Standard priorities */
2582 #define G_PRIORITY_HIGH -100
2583 #define G_PRIORITY_DEFAULT 0
2584 #define G_PRIORITY_HIGH_IDLE 100
2585 #define G_PRIORITY_DEFAULT_IDLE 200
2586 #define G_PRIORITY_LOW 300
2588 typedef gboolean (*GSourceFunc) (gpointer data);
2590 /* Hooks for adding to the main loop */
2591 guint g_source_add (gint priority,
2592 gboolean can_recurse,
2593 GSourceFuncs *funcs,
2594 gpointer source_data,
2596 GDestroyNotify notify);
2597 gboolean g_source_remove (guint tag);
2598 gboolean g_source_remove_by_user_data (gpointer user_data);
2599 gboolean g_source_remove_by_source_data (gpointer source_data);
2600 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2601 gpointer user_data);
2603 void g_get_current_time (GTimeVal *result);
2605 /* Running the main loop */
2606 GMainLoop* g_main_new (gboolean is_running);
2607 void g_main_run (GMainLoop *loop);
2608 void g_main_quit (GMainLoop *loop);
2609 void g_main_destroy (GMainLoop *loop);
2610 gboolean g_main_is_running (GMainLoop *loop);
2612 /* Run a single iteration of the mainloop. If block is FALSE,
2615 gboolean g_main_iteration (gboolean may_block);
2617 /* See if any events are pending */
2618 gboolean g_main_pending (void);
2620 /* Idles and timeouts */
2621 guint g_timeout_add_full (gint priority,
2623 GSourceFunc function,
2625 GDestroyNotify notify);
2626 guint g_timeout_add (guint interval,
2627 GSourceFunc function,
2629 guint g_idle_add (GSourceFunc function,
2631 guint g_idle_add_full (gint priority,
2632 GSourceFunc function,
2634 GDestroyNotify destroy);
2635 gboolean g_idle_remove_by_data (gpointer data);
2639 * System-specific IO and main loop calls
2641 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2642 * descriptor as provided by the C runtime) that can be used by
2643 * MsgWaitForMultipleObjects. This does *not* include file handles
2644 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2645 * WSAEventSelect to signal events when a SOCKET is readable).
2647 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2648 * indicate polling for messages. These message queue GPollFDs should
2649 * be added with the g_main_poll_win32_msg_add function.
2651 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2652 * (GTK) programs, as GDK itself wants to read messages and convert them
2655 * So, unless you really know what you are doing, it's best not to try
2656 * to use the main loop polling stuff for your own needs on
2657 * Win32. It's really only written for the GIMP's needs so
2661 typedef struct _GPollFD GPollFD;
2662 typedef gint (*GPollFunc) (GPollFD *ufds,
2672 void g_main_add_poll (GPollFD *fd,
2674 void g_main_remove_poll (GPollFD *fd);
2675 void g_main_set_poll_func (GPollFunc func);
2677 /* On Unix, IO channels created with this function for any file
2678 * descriptor or socket.
2680 * On Win32, use this only for plain files opened with the MSVCRT (the
2681 * Microsoft run-time C library) _open(), including file descriptors
2682 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2683 * Actually, don't do even that, this code isn't done yet.
2685 * The term file descriptor as used in the context of Win32 refers to
2686 * the emulated Unix-like file descriptors MSVCRT provides.
2688 GIOChannel* g_io_channel_unix_new (int fd);
2689 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2693 GUTILS_C_VAR guint g_pipe_readable_msg;
2695 #define G_WIN32_MSG_HANDLE 19981206
2697 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2698 * should *not* use this. (In fact, I can't think of any program that
2699 * would want to use this, but it's here just for completeness's sake.
2701 void g_main_poll_win32_msg_add(gint priority,
2705 /* An IO channel for Windows messages for window handle hwnd. */
2706 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2708 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2709 * _pipe(), with no mechanism for the writer to tell the reader when
2710 * there is data in the pipe.
2712 * This is not really implemented yet.
2714 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2716 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2717 * Windows user messages used to signal data in the pipe for the
2720 * fd is the file descriptor. For the write end, peer is the thread id
2721 * of the reader, and peer_fd is his file descriptor for the read end
2724 * This is used by the GIMP, and works.
2726 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2730 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2734 void g_io_channel_win32_pipe_readable (int fd,
2737 /* Get the C runtime file descriptor of a channel. */
2738 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2740 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2741 * actually a SOCKET.
2743 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2747 /* Windows emulation stubs for common Unix functions
2750 # define MAXPATHLEN 1024
2756 /* These POSIXish functions are available in the Microsoft C library
2757 * prefixed with underscore (which of course technically speaking is
2758 * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2759 * prevents Microsoft from practically requiring you to include
2760 * <windows.h> every now and then...).
2762 * You still need to include the appropriate headers to get the
2763 * prototypes, like <stdio.h>, <io.h>, <direct.h> or <process.h>.
2765 * For some functions, we provide emulators in glib, which are prefixed
2768 # define getcwd _getcwd
2769 # define getpid _getpid
2770 # define access _access
2773 # define fileno _fileno
2775 # define fstat _fstat
2776 # define unlink _unlink
2779 # define write _write
2780 # define lseek _lseek
2781 # define close _close
2782 # define rmdir _rmdir
2783 # define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2784 # define popen _popen
2785 # define pclose _pclose
2786 # define fdopen _fdopen
2787 # define hypot _hypot
2788 # define ftruncate(fd, size) gwin_ftruncate (fd, size)
2789 # define opendir gwin_opendir
2790 # define readdir gwin_readdir
2791 # define rewinddir gwin_rewinddir
2792 # define closedir gwin_closedir
2793 # define NAME_MAX 255
2797 gboolean just_opened;
2798 guint find_file_handle;
2799 gpointer find_file_data;
2801 typedef struct DIR DIR;
2804 gchar d_name[NAME_MAX + 1];
2806 /* emulation functions */
2807 extern int gwin_ftruncate (gint f,
2809 DIR* gwin_opendir (const gchar *dirname);
2810 struct dirent* gwin_readdir (DIR *dir);
2811 void gwin_rewinddir (DIR *dir);
2812 gint gwin_closedir (DIR *dir);
2813 #endif /* NATIVE_WIN32 */
2816 /* GLib Thread support
2819 typedef void (*GThreadFunc) (gpointer value);
2823 G_THREAD_PRIORITY_LOW,
2824 G_THREAD_PRIORITY_NORMAL,
2825 G_THREAD_PRIORITY_HIGH,
2826 G_THREAD_PRIORITY_URGENT,
2829 typedef struct _GThread GThread;
2832 GThreadPriority priority;
2837 typedef struct _GMutex GMutex;
2838 typedef struct _GCond GCond;
2839 typedef struct _GPrivate GPrivate;
2840 typedef struct _GStaticPrivate GStaticPrivate;
2842 typedef struct _GThreadFunctions GThreadFunctions;
2843 struct _GThreadFunctions
2845 GMutex* (*mutex_new) (void);
2846 void (*mutex_lock) (GMutex *mutex);
2847 gboolean (*mutex_trylock) (GMutex *mutex);
2848 void (*mutex_unlock) (GMutex *mutex);
2849 void (*mutex_free) (GMutex *mutex);
2850 GCond* (*cond_new) (void);
2851 void (*cond_signal) (GCond *cond);
2852 void (*cond_broadcast) (GCond *cond);
2853 void (*cond_wait) (GCond *cond,
2855 gboolean (*cond_timed_wait) (GCond *cond,
2857 GTimeVal *end_time);
2858 void (*cond_free) (GCond *cond);
2859 GPrivate* (*private_new) (GDestroyNotify destructor);
2860 gpointer (*private_get) (GPrivate *private_key);
2861 void (*private_set) (GPrivate *private_key,
2863 gpointer (*thread_create) (GThreadFunc thread_func,
2868 GThreadPriority priority);
2869 void (*thread_yield) (void);
2870 void (*thread_join) (gpointer thread);
2871 void (*thread_exit) (void);
2872 void (*thread_set_priority)(gpointer thread,
2873 GThreadPriority priority);
2874 gpointer (*thread_self) (void);
2877 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
2878 GUTILS_C_VAR gboolean g_thread_use_default_impl;
2879 GUTILS_C_VAR gboolean g_threads_got_initialized;
2881 /* initializes the mutex/cond/private implementation for glib, might
2882 * only be called once, and must not be called directly or indirectly
2883 * from another glib-function, e.g. as a callback.
2885 void g_thread_init (GThreadFunctions *vtable);
2887 /* internal function for fallback static mutex implementation */
2888 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
2890 /* shorthands for conditional and unconditional function calls */
2891 #define G_THREAD_UF(name, arglist) \
2892 (*g_thread_functions_for_glib_use . name) arglist
2893 #define G_THREAD_CF(name, fail, arg) \
2894 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2895 /* keep in mind, all those mutexes and static mutexes are not
2896 * recursive in general, don't rely on that
2898 #define g_thread_supported() (g_threads_got_initialized)
2899 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
2900 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
2901 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
2902 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
2903 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
2904 #define g_cond_new() G_THREAD_UF (cond_new, ())
2905 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
2906 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
2907 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
2909 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
2910 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2914 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
2915 #define g_private_get(private_key) G_THREAD_CF (private_get, \
2916 ((gpointer)private_key), \
2918 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2919 (void) (private_key = \
2920 (GPrivate*) (value)), \
2921 (private_key, value))
2922 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
2923 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
2925 GThread* g_thread_create (GThreadFunc thread_func,
2930 GThreadPriority priority);
2931 GThread* g_thread_self ();
2932 void g_thread_join (GThread* thread);
2933 void g_thread_set_priority (GThread* thread,
2934 GThreadPriority priority);
2936 /* GStaticMutexes can be statically initialized with the value
2937 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2938 * much easier, than having to explicitly allocate the mutex before
2941 #define g_static_mutex_lock(mutex) \
2942 g_mutex_lock (g_static_mutex_get_mutex (mutex))
2943 #define g_static_mutex_trylock(mutex) \
2944 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2945 #define g_static_mutex_unlock(mutex) \
2946 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
2948 struct _GStaticPrivate
2952 #define G_STATIC_PRIVATE_INIT { 0 }
2953 gpointer g_static_private_get (GStaticPrivate *private_key);
2954 void g_static_private_set (GStaticPrivate *private_key,
2956 GDestroyNotify notify);
2957 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
2959 void g_static_private_set_for_thread (GStaticPrivate *private_key,
2962 GDestroyNotify notify);
2963 #ifndef G_STATIC_REC_MUTEX_INIT
2964 /* if GStaticRecMutex is not just a differently initialized GStaticMutex,
2965 * the following is done:
2966 * This can't be done in glibconfig.h, as GStaticPrivate and gboolean
2967 * are not yet known there
2969 typedef struct _GStaticRecMutex GStaticRecMutex;
2970 struct _GStaticRecMutex
2973 GStaticPrivate counter;
2975 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, G_STATIC_PRIVATE_INIT }
2976 void g_static_rec_mutex_lock (GStaticRecMutex* mutex);
2977 gboolean g_static_rec_mutex_trylock (GStaticRecMutex* mutex);
2978 void g_static_rec_mutex_unlock (GStaticRecMutex* mutex);
2979 #define g_static_rec_mutex_get_mutex(mutex) ((mutex)->mutex)
2980 #endif /* G_STATIC_REC_MUTEX_INIT */
2982 typedef struct _GStaticRWLock GStaticRWLock;
2983 struct _GStaticRWLock
2990 guint want_to_write;
2993 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
2995 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
2996 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
2997 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
2998 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
2999 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3000 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
3001 void g_static_rw_lock_free (GStaticRWLock* lock);
3003 /* these are some convenience macros that expand to nothing if GLib
3004 * was configured with --disable-threads. for using StaticMutexes,
3005 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3006 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3007 * declare such an globally defined lock. name is a unique identifier
3008 * for the protected varibale or code portion. locking, testing and
3009 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3010 * G_TRYLOCK() respectively.
3012 extern void glib_dummy_decl (void);
3013 #define G_LOCK_NAME(name) (g__ ## name ## _lock)
3014 #ifdef G_THREADS_ENABLED
3015 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3016 # define G_LOCK_DEFINE(name) \
3017 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3018 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3020 # ifdef G_DEBUG_LOCKS
3021 # define G_LOCK(name) G_STMT_START{ \
3022 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3023 "file %s: line %d (%s): locking: %s ", \
3024 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3026 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3028 # define G_UNLOCK(name) G_STMT_START{ \
3029 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3030 "file %s: line %d (%s): unlocking: %s ", \
3031 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3033 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3035 # define G_TRYLOCK(name) G_STMT_START{ \
3036 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3037 "file %s: line %d (%s): try locking: %s ", \
3038 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3040 }G_STMT_END, g_static_mutex_trylock (&G_LOCK_NAME (name))
3041 # else /* !G_DEBUG_LOCKS */
3042 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3043 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3044 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3045 # endif /* !G_DEBUG_LOCKS */
3046 #else /* !G_THREADS_ENABLED */
3047 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3048 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3049 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3050 # define G_LOCK(name)
3051 # define G_UNLOCK(name)
3052 # define G_TRYLOCK(name) (FALSE)
3053 #endif /* !G_THREADS_ENABLED */
3057 #endif /* __cplusplus */
3060 #endif /* __G_LIB_H__ */