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 /* Copy a string interpreting C string -style escape sequences.
1519 * The recognized sequences are \b \f \n \r \t \\ \" and the octal format.
1521 gchar* g_strccpy (gchar *dest,
1522 const gchar *source);
1523 /* Copy a string escaping nonprintable characters like in C strings.
1524 * Inverse of g_strccpy. The exceptions parameter if non-NULL points
1525 * to a string containing characters that are not escaped.
1527 gchar* g_strecpy (gchar *dest,
1528 const gchar *source,
1529 const gchar *exceptions);
1531 /* deprecated function (used to be a real function) */
1532 #define g_strescape(src) g_strecpy (g_new (char, strlen (src)*4+1), (src), NULL)
1534 gpointer g_memdup (gconstpointer mem,
1537 /* NULL terminated string arrays.
1538 * g_strsplit() splits up string into max_tokens tokens at delim and
1539 * returns a newly allocated string array.
1540 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1541 * optional separator, the returned string is newly allocated.
1542 * g_strfreev() frees the array itself and all of its strings.
1544 gchar** g_strsplit (const gchar *string,
1545 const gchar *delimiter,
1547 gchar* g_strjoinv (const gchar *separator,
1549 void g_strfreev (gchar **str_array);
1553 /* calculate a string size, guarranteed to fit format + args.
1555 guint g_printf_string_upper_bound (const gchar* format,
1559 /* Retrive static string info
1561 gchar* g_get_user_name (void);
1562 gchar* g_get_real_name (void);
1563 gchar* g_get_home_dir (void);
1564 gchar* g_get_tmp_dir (void);
1565 gchar* g_get_prgname (void);
1566 void g_set_prgname (const gchar *prgname);
1569 /* Miscellaneous utility functions
1571 guint g_parse_debug_string (const gchar *string,
1574 gint g_snprintf (gchar *string,
1576 gchar const *format,
1577 ...) G_GNUC_PRINTF (3, 4);
1578 gint g_vsnprintf (gchar *string,
1580 gchar const *format,
1582 gchar* g_basename (const gchar *file_name);
1583 /* Check if a file name is an absolute path */
1584 gboolean g_path_is_absolute (const gchar *file_name);
1585 /* In case of absolute paths, skip the root part */
1586 gchar* g_path_skip_root (gchar *file_name);
1588 /* strings are newly allocated with g_malloc() */
1589 gchar* g_dirname (const gchar *file_name);
1590 gchar* g_get_current_dir (void);
1591 gchar* g_getenv (const gchar *variable);
1594 /* we use a GLib function as a replacement for ATEXIT, so
1595 * the programmer is not required to check the return value
1596 * (if there is any in the implementation) and doesn't encounter
1597 * missing include files.
1599 void g_atexit (GVoidFunc func);
1604 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1608 g_bit_nth_lsf (guint32 mask,
1614 if (mask & (1 << (guint) nth_bit))
1617 while (nth_bit < 32);
1620 #endif /* G_CAN_INLINE */
1622 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1626 g_bit_nth_msf (guint32 mask,
1634 if (mask & (1 << (guint) nth_bit))
1637 while (nth_bit > 0);
1640 #endif /* G_CAN_INLINE */
1642 G_INLINE_FUNC guint g_bit_storage (guint number);
1645 g_bit_storage (guint number)
1647 register guint n_bits = 0;
1657 #endif /* G_CAN_INLINE */
1661 * elements need to be >= sizeof (gpointer)
1663 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
1667 g_trash_stack_push (GTrashStack **stack_p,
1670 GTrashStack *data = data_p;
1672 data->next = *stack_p;
1675 #endif /* G_CAN_INLINE */
1677 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
1679 G_INLINE_FUNC gpointer
1680 g_trash_stack_pop (GTrashStack **stack_p)
1687 *stack_p = data->next;
1688 memset (data, 0, sizeof (GTrashStack));
1693 #endif /* G_CAN_INLINE */
1695 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
1697 G_INLINE_FUNC gpointer
1698 g_trash_stack_peek (GTrashStack **stack_p)
1706 #endif /* G_CAN_INLINE */
1708 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
1711 g_trash_stack_height (GTrashStack **stack_p)
1716 for (data = *stack_p; data; data = data->next)
1721 #endif /* G_CAN_INLINE */
1726 GStringChunk* g_string_chunk_new (gint size);
1727 void g_string_chunk_free (GStringChunk *chunk);
1728 gchar* g_string_chunk_insert (GStringChunk *chunk,
1729 const gchar *string);
1730 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1731 const gchar *string);
1736 GString* g_string_new (const gchar *init);
1737 GString* g_string_sized_new (guint dfl_size);
1738 void g_string_free (GString *string,
1740 GString* g_string_assign (GString *lval,
1742 GString* g_string_truncate (GString *string,
1744 GString* g_string_append (GString *string,
1746 GString* g_string_append_c (GString *string,
1748 GString* g_string_prepend (GString *string,
1750 GString* g_string_prepend_c (GString *string,
1752 GString* g_string_insert (GString *string,
1755 GString* g_string_insert_c (GString *string,
1758 GString* g_string_erase (GString *string,
1761 GString* g_string_down (GString *string);
1762 GString* g_string_up (GString *string);
1763 void g_string_sprintf (GString *string,
1764 const gchar *format,
1765 ...) G_GNUC_PRINTF (2, 3);
1766 void g_string_sprintfa (GString *string,
1767 const gchar *format,
1768 ...) G_GNUC_PRINTF (2, 3);
1771 /* Resizable arrays, remove fills any cleared spot and shortens the
1772 * array, while preserving the order. remove_fast will distort the
1773 * order by moving the last element to the position of the removed
1776 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1777 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1778 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1779 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1781 GArray* g_array_new (gboolean zero_terminated,
1783 guint element_size);
1784 void g_array_free (GArray *array,
1785 gboolean free_segment);
1786 GArray* g_array_append_vals (GArray *array,
1789 GArray* g_array_prepend_vals (GArray *array,
1792 GArray* g_array_insert_vals (GArray *array,
1796 GArray* g_array_set_size (GArray *array,
1798 GArray* g_array_remove_index (GArray *array,
1800 GArray* g_array_remove_index_fast (GArray *array,
1803 /* Resizable pointer array. This interface is much less complicated
1804 * than the above. Add appends appends a pointer. Remove fills any
1805 * cleared spot and shortens the array. remove_fast will again distort
1808 #define g_ptr_array_index(array,index) (array->pdata)[index]
1809 GPtrArray* g_ptr_array_new (void);
1810 void g_ptr_array_free (GPtrArray *array,
1812 void g_ptr_array_set_size (GPtrArray *array,
1814 gpointer g_ptr_array_remove_index (GPtrArray *array,
1816 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1818 gboolean g_ptr_array_remove (GPtrArray *array,
1820 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1822 void g_ptr_array_add (GPtrArray *array,
1825 /* Byte arrays, an array of guint8. Implemented as a GArray,
1829 GByteArray* g_byte_array_new (void);
1830 void g_byte_array_free (GByteArray *array,
1831 gboolean free_segment);
1832 GByteArray* g_byte_array_append (GByteArray *array,
1835 GByteArray* g_byte_array_prepend (GByteArray *array,
1838 GByteArray* g_byte_array_set_size (GByteArray *array,
1840 GByteArray* g_byte_array_remove_index (GByteArray *array,
1842 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1848 gint g_str_equal (gconstpointer v,
1850 guint g_str_hash (gconstpointer v);
1852 gint g_int_equal (gconstpointer v,
1854 guint g_int_hash (gconstpointer v);
1856 /* This "hash" function will just return the key's adress as an
1857 * unsigned integer. Useful for hashing on plain adresses or
1858 * simple integer values.
1859 * passing NULL into g_hash_table_new() as GHashFunc has the
1860 * same effect as passing g_direct_hash().
1862 guint g_direct_hash (gconstpointer v);
1863 gint g_direct_equal (gconstpointer v,
1867 /* Quarks (string<->id association)
1869 GQuark g_quark_try_string (const gchar *string);
1870 GQuark g_quark_from_static_string (const gchar *string);
1871 GQuark g_quark_from_string (const gchar *string);
1872 gchar* g_quark_to_string (GQuark quark);
1877 void g_datalist_init (GData **datalist);
1878 void g_datalist_clear (GData **datalist);
1879 gpointer g_datalist_id_get_data (GData **datalist,
1881 void g_datalist_id_set_data_full (GData **datalist,
1884 GDestroyNotify destroy_func);
1885 void g_datalist_id_remove_no_notify (GData **datalist,
1887 void g_datalist_foreach (GData **datalist,
1888 GDataForeachFunc func,
1889 gpointer user_data);
1890 #define g_datalist_id_set_data(dl, q, d) \
1891 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1892 #define g_datalist_id_remove_data(dl, q) \
1893 g_datalist_id_set_data ((dl), (q), NULL)
1894 #define g_datalist_get_data(dl, k) \
1895 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1896 #define g_datalist_set_data_full(dl, k, d, f) \
1897 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1898 #define g_datalist_remove_no_notify(dl, k) \
1899 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1900 #define g_datalist_set_data(dl, k, d) \
1901 g_datalist_set_data_full ((dl), (k), (d), NULL)
1902 #define g_datalist_remove_data(dl, k) \
1903 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1906 /* Location Associated Keyed Data
1908 void g_dataset_destroy (gconstpointer dataset_location);
1909 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1911 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1914 GDestroyNotify destroy_func);
1915 void g_dataset_id_remove_no_notify (gconstpointer dataset_location,
1917 void g_dataset_foreach (gconstpointer dataset_location,
1918 GDataForeachFunc func,
1919 gpointer user_data);
1920 #define g_dataset_id_set_data(l, k, d) \
1921 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1922 #define g_dataset_id_remove_data(l, k) \
1923 g_dataset_id_set_data ((l), (k), NULL)
1924 #define g_dataset_get_data(l, k) \
1925 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1926 #define g_dataset_set_data_full(l, k, d, f) \
1927 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1928 #define g_dataset_remove_no_notify(l, k) \
1929 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1930 #define g_dataset_set_data(l, k, d) \
1931 g_dataset_set_data_full ((l), (k), (d), NULL)
1932 #define g_dataset_remove_data(l, k) \
1933 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1936 /* GScanner: Flexible lexical scanner for general purpose.
1939 /* Character sets */
1940 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1941 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1942 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1943 "\307\310\311\312\313\314\315\316\317\320"\
1944 "\321\322\323\324\325\326"\
1945 "\330\331\332\333\334\335\336"
1946 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1947 "\347\350\351\352\353\354\355\356\357\360"\
1948 "\361\362\363\364\365\366"\
1949 "\370\371\372\373\374\375\376\377"
1956 G_ERR_UNEXP_EOF_IN_STRING,
1957 G_ERR_UNEXP_EOF_IN_COMMENT,
1958 G_ERR_NON_DIGIT_IN_CONST,
1961 G_ERR_FLOAT_MALFORMED
1969 G_TOKEN_LEFT_PAREN = '(',
1970 G_TOKEN_RIGHT_PAREN = ')',
1971 G_TOKEN_LEFT_CURLY = '{',
1972 G_TOKEN_RIGHT_CURLY = '}',
1973 G_TOKEN_LEFT_BRACE = '[',
1974 G_TOKEN_RIGHT_BRACE = ']',
1975 G_TOKEN_EQUAL_SIGN = '=',
1976 G_TOKEN_COMMA = ',',
1992 G_TOKEN_IDENTIFIER_NULL,
1994 G_TOKEN_COMMENT_SINGLE,
1995 G_TOKEN_COMMENT_MULTI,
2002 gchar *v_identifier;
2014 struct _GScannerConfig
2018 gchar *cset_skip_characters; /* default: " \t\n" */
2019 gchar *cset_identifier_first;
2020 gchar *cset_identifier_nth;
2021 gchar *cpair_comment_single; /* default: "#\n" */
2023 /* Should symbol lookup work case sensitive?
2025 guint case_sensitive : 1;
2027 /* Boolean values to be adjusted "on the fly"
2028 * to configure scanning behaviour.
2030 guint skip_comment_multi : 1; /* C like comment */
2031 guint skip_comment_single : 1; /* single line comment */
2032 guint scan_comment_multi : 1; /* scan multi line comments? */
2033 guint scan_identifier : 1;
2034 guint scan_identifier_1char : 1;
2035 guint scan_identifier_NULL : 1;
2036 guint scan_symbols : 1;
2037 guint scan_binary : 1;
2038 guint scan_octal : 1;
2039 guint scan_float : 1;
2040 guint scan_hex : 1; /* `0x0ff0' */
2041 guint scan_hex_dollar : 1; /* `$0ff0' */
2042 guint scan_string_sq : 1; /* string: 'anything' */
2043 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2044 guint numbers_2_int : 1; /* bin, octal, hex => int */
2045 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2046 guint identifier_2_string : 1;
2047 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2048 guint symbol_2_token : 1;
2049 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2056 guint max_parse_errors;
2058 /* g_scanner_error() increments this field */
2061 /* name of input stream, featured by the default message handler */
2062 const gchar *input_name;
2064 /* data pointer for derived structures */
2065 gpointer derived_data;
2067 /* link into the scanner configuration */
2068 GScannerConfig *config;
2070 /* fields filled in after g_scanner_get_next_token() */
2076 /* fields filled in after g_scanner_peek_next_token() */
2077 GTokenType next_token;
2078 GTokenValue next_value;
2080 guint next_position;
2082 /* to be considered private */
2083 GHashTable *symbol_table;
2086 const gchar *text_end;
2090 /* handler function for _warn and _error */
2091 GScannerMsgFunc msg_handler;
2094 GScanner* g_scanner_new (GScannerConfig *config_templ);
2095 void g_scanner_destroy (GScanner *scanner);
2096 void g_scanner_input_file (GScanner *scanner,
2098 void g_scanner_sync_file_offset (GScanner *scanner);
2099 void g_scanner_input_text (GScanner *scanner,
2102 GTokenType g_scanner_get_next_token (GScanner *scanner);
2103 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2104 GTokenType g_scanner_cur_token (GScanner *scanner);
2105 GTokenValue g_scanner_cur_value (GScanner *scanner);
2106 guint g_scanner_cur_line (GScanner *scanner);
2107 guint g_scanner_cur_position (GScanner *scanner);
2108 gboolean g_scanner_eof (GScanner *scanner);
2109 guint g_scanner_set_scope (GScanner *scanner,
2111 void g_scanner_scope_add_symbol (GScanner *scanner,
2113 const gchar *symbol,
2115 void g_scanner_scope_remove_symbol (GScanner *scanner,
2117 const gchar *symbol);
2118 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2120 const gchar *symbol);
2121 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2124 gpointer user_data);
2125 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2126 const gchar *symbol);
2127 void g_scanner_freeze_symbol_table (GScanner *scanner);
2128 void g_scanner_thaw_symbol_table (GScanner *scanner);
2129 void g_scanner_unexp_token (GScanner *scanner,
2130 GTokenType expected_token,
2131 const gchar *identifier_spec,
2132 const gchar *symbol_spec,
2133 const gchar *symbol_name,
2134 const gchar *message,
2136 void g_scanner_error (GScanner *scanner,
2137 const gchar *format,
2138 ...) G_GNUC_PRINTF (2,3);
2139 void g_scanner_warn (GScanner *scanner,
2140 const gchar *format,
2141 ...) G_GNUC_PRINTF (2,3);
2142 gint g_scanner_stat_mode (const gchar *filename);
2143 /* keep downward source compatibility */
2144 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2145 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2147 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2148 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2150 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2151 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2161 GCompletionFunc func;
2167 GCompletion* g_completion_new (GCompletionFunc func);
2168 void g_completion_add_items (GCompletion* cmp,
2170 void g_completion_remove_items (GCompletion* cmp,
2172 void g_completion_clear_items (GCompletion* cmp);
2173 GList* g_completion_complete (GCompletion* cmp,
2175 gchar** new_prefix);
2176 void g_completion_free (GCompletion* cmp);
2181 * Date calculations (not time for now, to be resolved). These are a
2182 * mutant combination of Steffen Beyer's DateCalc routines
2183 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2184 * date routines (written for in-house software). Written by Havoc
2185 * Pennington <hp@pobox.com>
2188 typedef guint16 GDateYear;
2189 typedef guint8 GDateDay; /* day of the month */
2190 typedef struct _GDate GDate;
2191 /* make struct tm known without having to include time.h */
2194 /* enum used to specify order of appearance in parsed date strings */
2202 /* actual week and month values */
2205 G_DATE_BAD_WEEKDAY = 0,
2208 G_DATE_WEDNESDAY = 3,
2209 G_DATE_THURSDAY = 4,
2211 G_DATE_SATURDAY = 6,
2216 G_DATE_BAD_MONTH = 0,
2218 G_DATE_FEBRUARY = 2,
2225 G_DATE_SEPTEMBER = 9,
2226 G_DATE_OCTOBER = 10,
2227 G_DATE_NOVEMBER = 11,
2228 G_DATE_DECEMBER = 12
2231 #define G_DATE_BAD_JULIAN 0U
2232 #define G_DATE_BAD_DAY 0U
2233 #define G_DATE_BAD_YEAR 0U
2235 /* Note: directly manipulating structs is generally a bad idea, but
2236 * in this case it's an *incredibly* bad idea, because all or part
2237 * of this struct can be invalid at any given time. Use the functions,
2238 * or you will get hosed, I promise.
2242 guint julian_days : 32; /* julian days representation - we use a
2243 * bitfield hoping that 64 bit platforms
2244 * will pack this whole struct in one big
2248 guint julian : 1; /* julian is valid */
2249 guint dmy : 1; /* dmy is valid */
2251 /* DMY representation */
2257 /* g_date_new() returns an invalid date, you then have to _set() stuff
2258 * to get a usable object. You can also allocate a GDate statically,
2259 * then call g_date_clear() to initialize.
2261 GDate* g_date_new (void);
2262 GDate* g_date_new_dmy (GDateDay day,
2265 GDate* g_date_new_julian (guint32 julian_day);
2266 void g_date_free (GDate *date);
2268 /* check g_date_valid() after doing an operation that might fail, like
2269 * _parse. Almost all g_date operations are undefined on invalid
2270 * dates (the exceptions are the mutators, since you need those to
2271 * return to validity).
2273 gboolean g_date_valid (GDate *date);
2274 gboolean g_date_valid_day (GDateDay day);
2275 gboolean g_date_valid_month (GDateMonth month);
2276 gboolean g_date_valid_year (GDateYear year);
2277 gboolean g_date_valid_weekday (GDateWeekday weekday);
2278 gboolean g_date_valid_julian (guint32 julian_date);
2279 gboolean g_date_valid_dmy (GDateDay day,
2283 GDateWeekday g_date_weekday (GDate *date);
2284 GDateMonth g_date_month (GDate *date);
2285 GDateYear g_date_year (GDate *date);
2286 GDateDay g_date_day (GDate *date);
2287 guint32 g_date_julian (GDate *date);
2288 guint g_date_day_of_year (GDate *date);
2290 /* First monday/sunday is the start of week 1; if we haven't reached
2291 * that day, return 0. These are not ISO weeks of the year; that
2292 * routine needs to be added.
2293 * these functions return the number of weeks, starting on the
2296 guint g_date_monday_week_of_year (GDate *date);
2297 guint g_date_sunday_week_of_year (GDate *date);
2299 /* If you create a static date struct you need to clear it to get it
2300 * in a sane state before use. You can clear a whole array at
2301 * once with the ndates argument.
2303 void g_date_clear (GDate *date,
2306 /* The parse routine is meant for dates typed in by a user, so it
2307 * permits many formats but tries to catch common typos. If your data
2308 * needs to be strictly validated, it is not an appropriate function.
2310 void g_date_set_parse (GDate *date,
2312 void g_date_set_time (GDate *date,
2314 void g_date_set_month (GDate *date,
2316 void g_date_set_day (GDate *date,
2318 void g_date_set_year (GDate *date,
2320 void g_date_set_dmy (GDate *date,
2324 void g_date_set_julian (GDate *date,
2325 guint32 julian_date);
2326 gboolean g_date_is_first_of_month (GDate *date);
2327 gboolean g_date_is_last_of_month (GDate *date);
2329 /* To go forward by some number of weeks just go forward weeks*7 days */
2330 void g_date_add_days (GDate *date,
2332 void g_date_subtract_days (GDate *date,
2335 /* If you add/sub months while day > 28, the day might change */
2336 void g_date_add_months (GDate *date,
2338 void g_date_subtract_months (GDate *date,
2341 /* If it's feb 29, changing years can move you to the 28th */
2342 void g_date_add_years (GDate *date,
2344 void g_date_subtract_years (GDate *date,
2346 gboolean g_date_is_leap_year (GDateYear year);
2347 guint8 g_date_days_in_month (GDateMonth month,
2349 guint8 g_date_monday_weeks_in_year (GDateYear year);
2350 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2352 /* qsort-friendly (with a cast...) */
2353 gint g_date_compare (GDate *lhs,
2355 void g_date_to_struct_tm (GDate *date,
2358 /* Just like strftime() except you can only use date-related formats.
2359 * Using a time format is undefined.
2361 gsize g_date_strftime (gchar *s,
2363 const gchar *format,
2369 * Indexed Relations. Imagine a really simple table in a
2370 * database. Relations are not ordered. This data type is meant for
2371 * maintaining a N-way mapping.
2373 * g_relation_new() creates a relation with FIELDS fields
2375 * g_relation_destroy() frees all resources
2376 * g_tuples_destroy() frees the result of g_relation_select()
2378 * g_relation_index() indexes relation FIELD with the provided
2379 * equality and hash functions. this must be done before any
2380 * calls to insert are made.
2382 * g_relation_insert() inserts a new tuple. you are expected to
2383 * provide the right number of fields.
2385 * g_relation_delete() deletes all relations with KEY in FIELD
2386 * g_relation_select() returns ...
2387 * g_relation_count() counts ...
2390 GRelation* g_relation_new (gint fields);
2391 void g_relation_destroy (GRelation *relation);
2392 void g_relation_index (GRelation *relation,
2394 GHashFunc hash_func,
2395 GCompareFunc key_compare_func);
2396 void g_relation_insert (GRelation *relation,
2398 gint g_relation_delete (GRelation *relation,
2401 GTuples* g_relation_select (GRelation *relation,
2404 gint g_relation_count (GRelation *relation,
2407 gboolean g_relation_exists (GRelation *relation,
2409 void g_relation_print (GRelation *relation);
2411 void g_tuples_destroy (GTuples *tuples);
2412 gpointer g_tuples_index (GTuples *tuples,
2417 /* GRand - a good and fast random number generator: Mersenne Twister
2418 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2419 * The range functions return a value in the intervall [min,max).
2420 * int -> [0..2^32-1]
2421 * int_range -> [min..max-1]
2423 * double_range -> [min..max)
2426 GRand* g_rand_new_with_seed (guint32 seed);
2427 GRand* g_rand_new (void);
2428 void g_rand_free (GRand *rand);
2430 void g_rand_set_seed (GRand *rand,
2432 guint32 g_rand_int (GRand *rand);
2433 gint32 g_rand_int_range (GRand *rand,
2436 gdouble g_rand_double (GRand *rand);
2437 gdouble g_rand_double_range (GRand *rand,
2441 void g_random_set_seed (guint32 seed);
2442 guint32 g_random_int (void);
2443 gint32 g_random_int_range (gint32 min,
2445 gdouble g_random_double (void);
2446 gdouble g_random_double_range (gdouble min,
2453 /* This function returns prime numbers spaced by approximately 1.5-2.0
2454 * and is for use in resizing data structures which prefer
2455 * prime-valued sizes. The closest spaced prime function returns the
2456 * next largest prime, or the highest it knows about which is about
2459 guint g_spaced_primes_closest (guint num);
2465 typedef struct _GIOFuncs GIOFuncs;
2481 G_IO_IN GLIB_SYSDEF_POLLIN,
2482 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2483 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2484 G_IO_ERR GLIB_SYSDEF_POLLERR,
2485 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2486 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2491 guint channel_flags;
2496 typedef gboolean (*GIOFunc) (GIOChannel *source,
2497 GIOCondition condition,
2501 GIOError (*io_read) (GIOChannel *channel,
2505 GIOError (*io_write) (GIOChannel *channel,
2508 guint *bytes_written);
2509 GIOError (*io_seek) (GIOChannel *channel,
2512 void (*io_close) (GIOChannel *channel);
2513 guint (*io_add_watch) (GIOChannel *channel,
2515 GIOCondition condition,
2518 GDestroyNotify notify);
2519 void (*io_free) (GIOChannel *channel);
2522 void g_io_channel_init (GIOChannel *channel);
2523 void g_io_channel_ref (GIOChannel *channel);
2524 void g_io_channel_unref (GIOChannel *channel);
2525 GIOError g_io_channel_read (GIOChannel *channel,
2529 GIOError g_io_channel_write (GIOChannel *channel,
2532 guint *bytes_written);
2533 GIOError g_io_channel_seek (GIOChannel *channel,
2536 void g_io_channel_close (GIOChannel *channel);
2537 guint g_io_add_watch_full (GIOChannel *channel,
2539 GIOCondition condition,
2542 GDestroyNotify notify);
2543 guint g_io_add_watch (GIOChannel *channel,
2544 GIOCondition condition,
2546 gpointer user_data);
2551 typedef struct _GTimeVal GTimeVal;
2552 typedef struct _GSourceFuncs GSourceFuncs;
2553 typedef struct _GMainLoop GMainLoop; /* Opaque */
2560 struct _GSourceFuncs
2562 gboolean (*prepare) (gpointer source_data,
2563 GTimeVal *current_time,
2565 gpointer user_data);
2566 gboolean (*check) (gpointer source_data,
2567 GTimeVal *current_time,
2568 gpointer user_data);
2569 gboolean (*dispatch) (gpointer source_data,
2570 GTimeVal *current_time,
2571 gpointer user_data);
2572 GDestroyNotify destroy;
2575 /* Standard priorities */
2577 #define G_PRIORITY_HIGH -100
2578 #define G_PRIORITY_DEFAULT 0
2579 #define G_PRIORITY_HIGH_IDLE 100
2580 #define G_PRIORITY_DEFAULT_IDLE 200
2581 #define G_PRIORITY_LOW 300
2583 typedef gboolean (*GSourceFunc) (gpointer data);
2585 /* Hooks for adding to the main loop */
2586 guint g_source_add (gint priority,
2587 gboolean can_recurse,
2588 GSourceFuncs *funcs,
2589 gpointer source_data,
2591 GDestroyNotify notify);
2592 gboolean g_source_remove (guint tag);
2593 gboolean g_source_remove_by_user_data (gpointer user_data);
2594 gboolean g_source_remove_by_source_data (gpointer source_data);
2595 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2596 gpointer user_data);
2598 void g_get_current_time (GTimeVal *result);
2600 /* Running the main loop */
2601 GMainLoop* g_main_new (gboolean is_running);
2602 void g_main_run (GMainLoop *loop);
2603 void g_main_quit (GMainLoop *loop);
2604 void g_main_destroy (GMainLoop *loop);
2605 gboolean g_main_is_running (GMainLoop *loop);
2607 /* Run a single iteration of the mainloop. If block is FALSE,
2610 gboolean g_main_iteration (gboolean may_block);
2612 /* See if any events are pending */
2613 gboolean g_main_pending (void);
2615 /* Idles and timeouts */
2616 guint g_timeout_add_full (gint priority,
2618 GSourceFunc function,
2620 GDestroyNotify notify);
2621 guint g_timeout_add (guint interval,
2622 GSourceFunc function,
2624 guint g_idle_add (GSourceFunc function,
2626 guint g_idle_add_full (gint priority,
2627 GSourceFunc function,
2629 GDestroyNotify destroy);
2630 gboolean g_idle_remove_by_data (gpointer data);
2634 * System-specific IO and main loop calls
2636 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2637 * descriptor as provided by the C runtime) that can be used by
2638 * MsgWaitForMultipleObjects. This does *not* include file handles
2639 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2640 * WSAEventSelect to signal events when a SOCKET is readable).
2642 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2643 * indicate polling for messages. These message queue GPollFDs should
2644 * be added with the g_main_poll_win32_msg_add function.
2646 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2647 * (GTK) programs, as GDK itself wants to read messages and convert them
2650 * So, unless you really know what you are doing, it's best not to try
2651 * to use the main loop polling stuff for your own needs on
2652 * Win32. It's really only written for the GIMP's needs so
2656 typedef struct _GPollFD GPollFD;
2657 typedef gint (*GPollFunc) (GPollFD *ufds,
2667 void g_main_add_poll (GPollFD *fd,
2669 void g_main_remove_poll (GPollFD *fd);
2670 void g_main_set_poll_func (GPollFunc func);
2672 /* On Unix, IO channels created with this function for any file
2673 * descriptor or socket.
2675 * On Win32, use this only for plain files opened with the MSVCRT (the
2676 * Microsoft run-time C library) _open(), including file descriptors
2677 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2678 * Actually, don't do even that, this code isn't done yet.
2680 * The term file descriptor as used in the context of Win32 refers to
2681 * the emulated Unix-like file descriptors MSVCRT provides.
2683 GIOChannel* g_io_channel_unix_new (int fd);
2684 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2688 GUTILS_C_VAR guint g_pipe_readable_msg;
2690 #define G_WIN32_MSG_HANDLE 19981206
2692 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2693 * should *not* use this. (In fact, I can't think of any program that
2694 * would want to use this, but it's here just for completeness's sake.
2696 void g_main_poll_win32_msg_add(gint priority,
2700 /* An IO channel for Windows messages for window handle hwnd. */
2701 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2703 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2704 * _pipe(), with no mechanism for the writer to tell the reader when
2705 * there is data in the pipe.
2707 * This is not really implemented yet.
2709 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2711 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2712 * Windows user messages used to signal data in the pipe for the
2715 * fd is the file descriptor. For the write end, peer is the thread id
2716 * of the reader, and peer_fd is his file descriptor for the read end
2719 * This is used by the GIMP, and works.
2721 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2725 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2729 void g_io_channel_win32_pipe_readable (int fd,
2732 /* Get the C runtime file descriptor of a channel. */
2733 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2735 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2736 * actually a SOCKET.
2738 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2742 /* Windows emulation stubs for common Unix functions
2745 # define MAXPATHLEN 1024
2751 /* These POSIXish functions are available in the Microsoft C library
2752 * prefixed with underscore (which of course technically speaking is
2753 * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2754 * prevents Microsoft from practically requiring you to include
2755 * <windows.h> every now and then...).
2757 * You still need to include the appropriate headers to get the
2758 * prototypes, like <stdio.h>, <io.h>, <direct.h> or <process.h>.
2760 * For some functions, we provide emulators in glib, which are prefixed
2763 # define getcwd _getcwd
2764 # define getpid _getpid
2765 # define access _access
2768 # define fileno _fileno
2770 # define fstat _fstat
2771 # define unlink _unlink
2774 # define write _write
2775 # define lseek _lseek
2776 # define close _close
2777 # define rmdir _rmdir
2778 # define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2779 # define popen _popen
2780 # define pclose _pclose
2781 # define fdopen _fdopen
2782 # define hypot _hypot
2783 # define ftruncate(fd, size) gwin_ftruncate (fd, size)
2784 # define opendir gwin_opendir
2785 # define readdir gwin_readdir
2786 # define rewinddir gwin_rewinddir
2787 # define closedir gwin_closedir
2788 # define NAME_MAX 255
2792 gboolean just_opened;
2793 guint find_file_handle;
2794 gpointer find_file_data;
2796 typedef struct DIR DIR;
2799 gchar d_name[NAME_MAX + 1];
2801 /* emulation functions */
2802 extern int gwin_ftruncate (gint f,
2804 DIR* gwin_opendir (const gchar *dirname);
2805 struct dirent* gwin_readdir (DIR *dir);
2806 void gwin_rewinddir (DIR *dir);
2807 gint gwin_closedir (DIR *dir);
2808 #endif /* NATIVE_WIN32 */
2811 /* GLib Thread support
2814 typedef void (*GThreadFunc) (gpointer value);
2818 G_THREAD_PRIORITY_LOW,
2819 G_THREAD_PRIORITY_NORMAL,
2820 G_THREAD_PRIORITY_HIGH,
2821 G_THREAD_PRIORITY_URGENT,
2824 typedef struct _GThread GThread;
2827 GThreadPriority priority;
2832 typedef struct _GMutex GMutex;
2833 typedef struct _GCond GCond;
2834 typedef struct _GPrivate GPrivate;
2835 typedef struct _GStaticPrivate GStaticPrivate;
2837 typedef struct _GThreadFunctions GThreadFunctions;
2838 struct _GThreadFunctions
2840 GMutex* (*mutex_new) (void);
2841 void (*mutex_lock) (GMutex *mutex);
2842 gboolean (*mutex_trylock) (GMutex *mutex);
2843 void (*mutex_unlock) (GMutex *mutex);
2844 void (*mutex_free) (GMutex *mutex);
2845 GCond* (*cond_new) (void);
2846 void (*cond_signal) (GCond *cond);
2847 void (*cond_broadcast) (GCond *cond);
2848 void (*cond_wait) (GCond *cond,
2850 gboolean (*cond_timed_wait) (GCond *cond,
2852 GTimeVal *end_time);
2853 void (*cond_free) (GCond *cond);
2854 GPrivate* (*private_new) (GDestroyNotify destructor);
2855 gpointer (*private_get) (GPrivate *private_key);
2856 void (*private_set) (GPrivate *private_key,
2858 gpointer (*thread_create) (GThreadFunc thread_func,
2863 GThreadPriority priority);
2864 void (*thread_yield) (void);
2865 void (*thread_join) (gpointer thread);
2866 void (*thread_exit) (void);
2867 void (*thread_set_priority)(gpointer thread,
2868 GThreadPriority priority);
2869 gpointer (*thread_self) (void);
2872 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
2873 GUTILS_C_VAR gboolean g_thread_use_default_impl;
2874 GUTILS_C_VAR gboolean g_threads_got_initialized;
2876 /* initializes the mutex/cond/private implementation for glib, might
2877 * only be called once, and must not be called directly or indirectly
2878 * from another glib-function, e.g. as a callback.
2880 void g_thread_init (GThreadFunctions *vtable);
2882 /* internal function for fallback static mutex implementation */
2883 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
2885 /* shorthands for conditional and unconditional function calls */
2886 #define G_THREAD_UF(name, arglist) \
2887 (*g_thread_functions_for_glib_use . name) arglist
2888 #define G_THREAD_CF(name, fail, arg) \
2889 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2890 /* keep in mind, all those mutexes and static mutexes are not
2891 * recursive in general, don't rely on that
2893 #define g_thread_supported() (g_threads_got_initialized)
2894 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
2895 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
2896 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
2897 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
2898 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
2899 #define g_cond_new() G_THREAD_UF (cond_new, ())
2900 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
2901 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
2902 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
2904 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
2905 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2909 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
2910 #define g_private_get(private_key) G_THREAD_CF (private_get, \
2911 ((gpointer)private_key), \
2913 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2914 (void) (private_key = \
2915 (GPrivate*) (value)), \
2916 (private_key, value))
2917 #define g_thread_yield() G_THREAD_CF (thread_yield, (void)0, ())
2918 #define g_thread_exit() G_THREAD_CF (thread_exit, (void)0, ())
2920 GThread* g_thread_create (GThreadFunc thread_func,
2925 GThreadPriority priority);
2926 GThread* g_thread_self ();
2927 void g_thread_join (GThread* thread);
2928 void g_thread_set_priority (GThread* thread,
2929 GThreadPriority priority);
2931 /* GStaticMutexes can be statically initialized with the value
2932 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2933 * much easier, than having to explicitly allocate the mutex before
2936 #define g_static_mutex_lock(mutex) \
2937 g_mutex_lock (g_static_mutex_get_mutex (mutex))
2938 #define g_static_mutex_trylock(mutex) \
2939 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2940 #define g_static_mutex_unlock(mutex) \
2941 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
2943 struct _GStaticPrivate
2947 #define G_STATIC_PRIVATE_INIT { 0 }
2948 gpointer g_static_private_get (GStaticPrivate *private_key);
2949 void g_static_private_set (GStaticPrivate *private_key,
2951 GDestroyNotify notify);
2952 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
2954 void g_static_private_set_for_thread (GStaticPrivate *private_key,
2957 GDestroyNotify notify);
2958 #ifndef G_STATIC_REC_MUTEX_INIT
2959 /* if GStaticRecMutex is not just a differently initialized GStaticMutex,
2960 * the following is done:
2961 * This can't be done in glibconfig.h, as GStaticPrivate and gboolean
2962 * are not yet known there
2964 typedef struct _GStaticRecMutex GStaticRecMutex;
2965 struct _GStaticRecMutex
2968 GStaticPrivate counter;
2970 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, G_STATIC_PRIVATE_INIT }
2971 void g_static_rec_mutex_lock (GStaticRecMutex* mutex);
2972 gboolean g_static_rec_mutex_trylock (GStaticRecMutex* mutex);
2973 void g_static_rec_mutex_unlock (GStaticRecMutex* mutex);
2974 #define g_static_rec_mutex_get_mutex(mutex) ((mutex)->mutex)
2975 #endif /* G_STATIC_REC_MUTEX_INIT */
2977 typedef struct _GStaticRWLock GStaticRWLock;
2978 struct _GStaticRWLock
2985 guint want_to_write;
2988 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
2990 void g_static_rw_lock_reader_lock (GStaticRWLock* lock);
2991 gboolean g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
2992 void g_static_rw_lock_reader_unlock (GStaticRWLock* lock);
2993 void g_static_rw_lock_writer_lock (GStaticRWLock* lock);
2994 gboolean g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
2995 void g_static_rw_lock_writer_unlock (GStaticRWLock* lock);
2996 void g_static_rw_lock_free (GStaticRWLock* lock);
2998 /* these are some convenience macros that expand to nothing if GLib
2999 * was configured with --disable-threads. for using StaticMutexes,
3000 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3001 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3002 * declare such an globally defined lock. name is a unique identifier
3003 * for the protected varibale or code portion. locking, testing and
3004 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3005 * G_TRYLOCK() respectively.
3007 extern void glib_dummy_decl (void);
3008 #define G_LOCK_NAME(name) (g__ ## name ## _lock)
3009 #ifdef G_THREADS_ENABLED
3010 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
3011 # define G_LOCK_DEFINE(name) \
3012 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
3013 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
3015 # ifdef G_DEBUG_LOCKS
3016 # define G_LOCK(name) G_STMT_START{ \
3017 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3018 "file %s: line %d (%s): locking: %s ", \
3019 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3021 g_static_mutex_lock (&G_LOCK_NAME (name)); \
3023 # define G_UNLOCK(name) G_STMT_START{ \
3024 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3025 "file %s: line %d (%s): unlocking: %s ", \
3026 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3028 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
3030 # define G_TRYLOCK(name) G_STMT_START{ \
3031 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
3032 "file %s: line %d (%s): try locking: %s ", \
3033 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
3035 }G_STMT_END, g_static_mutex_trylock (&G_LOCK_NAME (name))
3036 # else /* !G_DEBUG_LOCKS */
3037 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
3038 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
3039 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3040 # endif /* !G_DEBUG_LOCKS */
3041 #else /* !G_THREADS_ENABLED */
3042 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
3043 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
3044 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
3045 # define G_LOCK(name)
3046 # define G_UNLOCK(name)
3047 # define G_TRYLOCK(name) (FALSE)
3048 #endif /* !G_THREADS_ENABLED */
3052 #endif /* __cplusplus */
3055 #endif /* __G_LIB_H__ */