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.
22 /* system specific config file glibconfig.h provides definitions for
23 * the extrema of many of the standard types. These are:
25 * G_MINSHORT, G_MAXSHORT
27 * G_MINLONG, G_MAXLONG
28 * G_MINFLOAT, G_MAXFLOAT
29 * G_MINDOUBLE, G_MAXDOUBLE
31 * It also provides the following typedefs:
38 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
41 * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
42 * This is useful to pass an integer instead of a pointer to a callback.
44 * GINT_TO_POINTER(i), GUINT_TO_POINTER(i)
45 * GPOINTER_TO_INT(p), GPOINTER_TO_UINT(p)
47 * Finally, it provide the following wrappers to STDC functions:
50 * To register hooks which are executed on exit().
51 * Usually a wrapper for STDC atexit.
53 * void *g_memmove(void *dest, const void *src, guint count);
54 * A wrapper for STDC memmove, or an implementation, if memmove doesn't
55 * exist. The prototype looks like the above, give or take a const,
58 #include <glibconfig.h>
60 /* include varargs functions for assertment macros
64 /* optionally feature DMALLOC memory allocation debugger
73 /* On native Win32, directory separator is the backslash, and search path
74 * separator is the semicolon.
76 #define G_DIR_SEPARATOR '\\'
77 #define G_DIR_SEPARATOR_S "\\"
78 #define G_SEARCHPATH_SEPARATOR ';'
79 #define G_SEARCHPATH_SEPARATOR_S ";"
81 #else /* !NATIVE_WIN32 */
85 #define G_DIR_SEPARATOR '/'
86 #define G_DIR_SEPARATOR_S "/"
87 #define G_SEARCHPATH_SEPARATOR ':'
88 #define G_SEARCHPATH_SEPARATOR_S ":"
90 #endif /* !NATIVE_WIN32 */
94 #endif /* __cplusplus */
97 /* Provide definitions for some commonly used macros.
98 * Some of them are only provided if they haven't already
99 * been defined. It is assumed that if they are already
100 * defined then the current definition is correct.
103 #define NULL ((void*) 0)
111 #define TRUE (!FALSE)
115 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
118 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
121 #define ABS(a) (((a) < 0) ? -(a) : (a))
124 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
127 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
128 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
130 #if !defined (G_VA_COPY)
131 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
132 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
133 # elif defined (G_VA_COPY_AS_ARRAY)
134 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
135 # else /* va_list is a pointer */
136 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
137 # endif /* va_list is a pointer */
138 #endif /* !G_VA_COPY */
141 /* Provide convenience macros for handling structure
142 * fields through their offsets.
144 #define G_STRUCT_OFFSET(struct_type, member) \
145 ((gulong) ((gchar*) &((struct_type*) 0)->member))
146 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
147 ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
148 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
149 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
152 /* inlining hassle. for compilers that don't allow the `inline' keyword,
153 * mostly because of strict ANSI C compliance or dumbness, we try to fall
154 * back to either `__inline__' or `__inline'.
155 * we define G_CAN_INLINE, if the compiler seems to be actually
156 * *capable* to do function inlining, in which case inline function bodys
157 * do make sense. we also define G_INLINE_FUNC to properly export the
158 * function prototypes if no inlining can be performed.
159 * we special case most of the stuff, so inline functions can have a normal
160 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
162 #ifndef G_INLINE_FUNC
163 # define G_CAN_INLINE 1
166 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
168 # define inline __inline__
170 #else /* !G_HAVE_INLINE */
172 # if defined (G_HAVE___INLINE__)
173 # define inline __inline__
174 # else /* !inline && !__inline__ */
175 # if defined (G_HAVE___INLINE)
176 # define inline __inline
177 # else /* !inline && !__inline__ && !__inline */
178 # define inline /* don't inline, then */
179 # ifndef G_INLINE_FUNC
185 #ifndef G_INLINE_FUNC
188 # define G_INLINE_FUNC extern inline
191 # define G_INLINE_FUNC extern
193 # else /* !__GNUC__ */
195 # define G_INLINE_FUNC static inline
197 # define G_INLINE_FUNC extern
199 # endif /* !__GNUC__ */
200 #endif /* !G_INLINE_FUNC */
203 /* Provide simple macro statement wrappers (adapted from Perl):
204 * G_STMT_START { statements; } G_STMT_END;
205 * can be used as a single statement, as in
206 * if (x) G_STMT_START { ... } G_STMT_END; else ...
208 * For gcc we will wrap the statements within `({' and `})' braces.
209 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
210 * and otherwise within `do' and `while (0)'.
212 #if !(defined (G_STMT_START) && defined (G_STMT_END))
213 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
214 # define G_STMT_START (void)(
215 # define G_STMT_END )
217 # if (defined (sun) || defined (__sun__))
218 # define G_STMT_START if (1)
219 # define G_STMT_END else (void)0
221 # define G_STMT_START do
222 # define G_STMT_END while (0)
228 /* Provide macros to feature the GCC function attribute.
230 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
231 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
232 __attribute__((format (printf, format_idx, arg_idx)))
233 #define G_GNUC_SCANF( format_idx, arg_idx ) \
234 __attribute__((format (scanf, format_idx, arg_idx)))
235 #define G_GNUC_FORMAT( arg_idx ) \
236 __attribute__((format_arg (arg_idx)))
237 #define G_GNUC_NORETURN \
238 __attribute__((noreturn))
239 #define G_GNUC_CONST \
240 __attribute__((const))
241 #define G_GNUC_UNUSED \
242 __attribute__((unused))
243 #else /* !__GNUC__ */
244 #define G_GNUC_PRINTF( format_idx, arg_idx )
245 #define G_GNUC_SCANF( format_idx, arg_idx )
246 #define G_GNUC_FORMAT( arg_idx )
247 #define G_GNUC_NORETURN
249 #define G_GNUC_UNUSED
250 #endif /* !__GNUC__ */
253 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
254 * macros, so we can refer to them as strings unconditionally.
257 #define G_GNUC_FUNCTION (__FUNCTION__)
258 #define G_GNUC_PRETTY_FUNCTION (__PRETTY_FUNCTION__)
259 #else /* !__GNUC__ */
260 #define G_GNUC_FUNCTION ("")
261 #define G_GNUC_PRETTY_FUNCTION ("")
262 #endif /* !__GNUC__ */
264 /* we try to provide a usefull equivalent for ATEXIT if it is
265 * not defined, but use is actually abandoned. people should
266 * use g_atexit() instead.
269 # define ATEXIT(proc) g_ATEXIT(proc)
271 # define G_NATIVE_ATEXIT
274 /* Hacker macro to place breakpoints for elected machines.
275 * Actual use is strongly deprecated of course ;)
277 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
278 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
279 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
280 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
281 #else /* !__i386__ && !__alpha__ */
282 #define G_BREAKPOINT()
283 #endif /* __i386__ */
286 /* Provide macros for easily allocating memory. The macros
287 * will cast the allocated memory to the specified type
288 * in order to avoid compiler warnings. (Makes the code neater).
292 # define g_new(type, count) (ALLOC (type, count))
293 # define g_new0(type, count) (CALLOC (type, count))
294 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
295 #else /* __DMALLOC_H__ */
296 # define g_new(type, count) \
297 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
298 # define g_new0(type, count) \
299 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
300 # define g_renew(type, mem, count) \
301 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
302 #endif /* __DMALLOC_H__ */
304 /* alloca-based counterparts of the above routines */
306 # define g_alloca alloca
307 # define g_new_a(type, count) \
308 ((type *) alloca ((unsigned) sizeof (type) * (count)))
309 # define g_new0_a(type, count) \
310 ((type *) memset (alloca ((unsigned) sizeof (type) * (count)), 0, \
311 ((unsigned) sizeof (type) * (count))))
314 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
315 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
317 sizeof (type) * (pre_alloc), \
320 #define g_chunk_new(type, chunk) ( \
321 (type *) g_mem_chunk_alloc (chunk) \
323 #define g_chunk_new0(type, chunk) ( \
324 (type *) g_mem_chunk_alloc0 (chunk) \
326 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
327 g_mem_chunk_free ((mem_chunk), (mem)); \
331 #define g_string(x) #x
334 /* Provide macros for error handling. The "assert" macros will
335 * exit on failure. The "return" macros will exit the current
336 * function. Two different definitions are given for the macros
337 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
338 * __PRETTY_FUNCTION__ capability.
341 #ifdef G_DISABLE_ASSERT
343 #define g_assert(expr)
344 #define g_assert_not_reached()
346 #else /* !G_DISABLE_ASSERT */
350 #define g_assert(expr) G_STMT_START{ \
352 g_log (G_LOG_DOMAIN, \
354 "file %s: line %d (%s): assertion failed: (%s)", \
357 __PRETTY_FUNCTION__, \
360 #define g_assert_not_reached() G_STMT_START{ \
361 g_log (G_LOG_DOMAIN, \
363 "file %s: line %d (%s): should not be reached", \
366 __PRETTY_FUNCTION__); }G_STMT_END
368 #else /* !__GNUC__ */
370 #define g_assert(expr) G_STMT_START{ \
372 g_log (G_LOG_DOMAIN, \
374 "file %s: line %d: assertion failed: (%s)", \
379 #define g_assert_not_reached() G_STMT_START{ \
380 g_log (G_LOG_DOMAIN, \
382 "file %s: line %d: should not be reached", \
384 __LINE__); }G_STMT_END
386 #endif /* __GNUC__ */
388 #endif /* !G_DISABLE_ASSERT */
391 #ifdef G_DISABLE_CHECKS
393 #define g_return_if_fail(expr)
394 #define g_return_val_if_fail(expr,val)
396 #else /* !G_DISABLE_CHECKS */
400 #define g_return_if_fail(expr) G_STMT_START{ \
403 g_log (G_LOG_DOMAIN, \
404 G_LOG_LEVEL_CRITICAL, \
405 "file %s: line %d (%s): assertion `%s' failed.", \
408 __PRETTY_FUNCTION__, \
413 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
416 g_log (G_LOG_DOMAIN, \
417 G_LOG_LEVEL_CRITICAL, \
418 "file %s: line %d (%s): assertion `%s' failed.", \
421 __PRETTY_FUNCTION__, \
426 #else /* !__GNUC__ */
428 #define g_return_if_fail(expr) G_STMT_START{ \
431 g_log (G_LOG_DOMAIN, \
432 G_LOG_LEVEL_CRITICAL, \
433 "file %s: line %d: assertion `%s' failed.", \
440 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
443 g_log (G_LOG_DOMAIN, \
444 G_LOG_LEVEL_CRITICAL, \
445 "file %s: line %d: assertion `%s' failed.", \
452 #endif /* !__GNUC__ */
454 #endif /* !G_DISABLE_CHECKS */
457 /* Provide type definitions for commonly used types.
458 * These are useful because a "gint8" can be adjusted
459 * to be 1 byte (8 bits) on all platforms. Similarly and
460 * more importantly, "gint32" can be adjusted to be
461 * 4 bytes (32 bits) on all platforms.
465 typedef short gshort;
468 typedef gint gboolean;
470 typedef unsigned char guchar;
471 typedef unsigned short gushort;
472 typedef unsigned long gulong;
473 typedef unsigned int guint;
475 typedef float gfloat;
476 typedef double gdouble;
478 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
479 * Since gldouble isn't used anywhere, just disable it for now */
482 #ifdef HAVE_LONG_DOUBLE
483 typedef long double gldouble;
484 #else /* HAVE_LONG_DOUBLE */
485 typedef double gldouble;
486 #endif /* HAVE_LONG_DOUBLE */
489 typedef void* gpointer;
490 typedef const void *gconstpointer;
493 typedef gint32 gssize;
494 typedef guint32 gsize;
495 typedef guint32 GQuark;
496 typedef gint32 GTime;
499 /* Portable endian checks and conversions
501 * glibconfig.h defines G_BYTE_ORDER which expands to one of
504 #define G_LITTLE_ENDIAN 1234
505 #define G_BIG_ENDIAN 4321
506 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
509 /* Basic bit swapping functions
511 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
512 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
513 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
514 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
515 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
516 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
517 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
518 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
520 /* Intel specific stuff for speed
522 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
523 # define GUINT16_SWAP_LE_BE_X86(val) \
525 ({ register guint16 __v; \
526 if (__builtin_constant_p (val)) \
527 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
529 __asm__ __const__ ("rorw $8, %w0" \
531 : "0" ((guint16) (val))); \
533 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
534 # if !defined(__i486__) && !defined(__i586__) \
535 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
536 # define GUINT32_SWAP_LE_BE_X86(val) \
538 ({ register guint32 __v; \
539 if (__builtin_constant_p (val)) \
540 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
542 __asm__ __const__ ("rorw $8, %w0\n\t" \
546 : "0" ((guint32) (val))); \
548 # else /* 486 and higher has bswap */
549 # define GUINT32_SWAP_LE_BE_X86(val) \
551 ({ register guint32 __v; \
552 if (__builtin_constant_p (val)) \
553 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
555 __asm__ __const__ ("bswap %0" \
557 : "0" ((guint32) (val))); \
559 # endif /* processor specific 32-bit stuff */
560 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
561 #else /* !__i386__ */
562 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
563 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
564 #endif /* __i386__ */
567 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
568 (((guint64) (val) & \
569 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
570 (((guint64) (val) & \
571 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
572 (((guint64) (val) & \
573 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
574 (((guint64) (val) & \
575 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
576 (((guint64) (val) & \
577 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
578 (((guint64) (val) & \
579 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
580 (((guint64) (val) & \
581 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
582 (((guint64) (val) & \
583 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
584 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
585 # define GUINT64_SWAP_LE_BE_X86(val) \
587 ({ union { guint64 __ll; \
588 guint32 __l[2]; } __r; \
589 if (__builtin_constant_p (val)) \
590 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
593 union { guint64 __ll; \
594 guint32 __l[2]; } __w; \
595 __w.__ll = ((guint64) val); \
596 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
597 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
600 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
601 # else /* !__i386__ */
602 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
606 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
607 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
608 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
609 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
610 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
611 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
612 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
613 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
615 /* The G*_TO_?E() macros are defined in glibconfig.h.
616 * The transformation is symmetric, so the FROM just maps to the TO.
618 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
619 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
620 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
621 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
622 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
623 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
624 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
625 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
628 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
629 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
630 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
631 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
634 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
635 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
636 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
637 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
639 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
640 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
641 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
642 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
645 /* Portable versions of host-network order stuff
647 #define g_ntohl(val) (GUINT32_FROM_BE (val))
648 #define g_ntohs(val) (GUINT16_FROM_BE (val))
649 #define g_htonl(val) (GUINT32_TO_BE (val))
650 #define g_htons(val) (GUINT16_TO_BE (val))
654 * we prefix variable declarations so they can
655 * properly get exported in windows dlls.
658 # ifdef GLIB_COMPILATION
659 # define GUTILS_C_VAR __declspec(dllexport)
660 # else /* !GLIB_COMPILATION */
661 # define GUTILS_C_VAR extern __declspec(dllimport)
662 # endif /* !GLIB_COMPILATION */
663 #else /* !NATIVE_WIN32 */
664 # define GUTILS_C_VAR extern
665 #endif /* !NATIVE_WIN32 */
667 GUTILS_C_VAR const guint glib_major_version;
668 GUTILS_C_VAR const guint glib_minor_version;
669 GUTILS_C_VAR const guint glib_micro_version;
670 GUTILS_C_VAR const guint glib_interface_age;
671 GUTILS_C_VAR const guint glib_binary_age;
674 /* Forward declarations of glib types.
676 typedef struct _GAllocator GAllocator;
677 typedef struct _GArray GArray;
678 typedef struct _GByteArray GByteArray;
679 typedef struct _GCache GCache;
680 typedef struct _GCompletion GCompletion;
681 typedef struct _GData GData;
682 typedef struct _GDebugKey GDebugKey;
683 typedef struct _GHashTable GHashTable;
684 typedef struct _GHook GHook;
685 typedef struct _GHookList GHookList;
686 typedef struct _GList GList;
687 typedef struct _GMemChunk GMemChunk;
688 typedef struct _GNode GNode;
689 typedef struct _GPtrArray GPtrArray;
690 typedef struct _GRelation GRelation;
691 typedef struct _GScanner GScanner;
692 typedef struct _GScannerConfig GScannerConfig;
693 typedef struct _GSList GSList;
694 typedef struct _GString GString;
695 typedef struct _GStringChunk GStringChunk;
696 typedef struct _GTimer GTimer;
697 typedef struct _GTree GTree;
698 typedef struct _GTuples GTuples;
699 typedef union _GTokenValue GTokenValue;
700 typedef struct _GIOChannel GIOChannel;
704 G_TRAVERSE_LEAFS = 1 << 0,
705 G_TRAVERSE_NON_LEAFS = 1 << 1,
706 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
707 G_TRAVERSE_MASK = 0x03
718 /* Log level shift offset for user defined
719 * log levels (0-7 are used by GLib).
721 #define G_LOG_LEVEL_USER_SHIFT (8)
723 /* Glib log levels and flags.
728 G_LOG_FLAG_RECURSION = 1 << 0,
729 G_LOG_FLAG_FATAL = 1 << 1,
731 /* GLib log levels */
732 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
733 G_LOG_LEVEL_CRITICAL = 1 << 3,
734 G_LOG_LEVEL_WARNING = 1 << 4,
735 G_LOG_LEVEL_MESSAGE = 1 << 5,
736 G_LOG_LEVEL_INFO = 1 << 6,
737 G_LOG_LEVEL_DEBUG = 1 << 7,
739 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
742 /* GLib log levels that are considered fatal by default */
743 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
746 typedef gpointer (*GCacheNewFunc) (gpointer key);
747 typedef gpointer (*GCacheDupFunc) (gpointer value);
748 typedef void (*GCacheDestroyFunc) (gpointer value);
749 typedef gint (*GCompareFunc) (gconstpointer a,
751 typedef gchar* (*GCompletionFunc) (gpointer);
752 typedef void (*GDestroyNotify) (gpointer data);
753 typedef void (*GDataForeachFunc) (GQuark key_id,
756 typedef void (*GFunc) (gpointer data,
758 typedef guint (*GHashFunc) (gconstpointer key);
759 typedef void (*GFreeFunc) (gpointer data);
760 typedef void (*GHFunc) (gpointer key,
763 typedef gboolean (*GHRFunc) (gpointer key,
766 typedef gint (*GHookCompareFunc) (GHook *new_hook,
768 typedef gboolean (*GHookFindFunc) (GHook *hook,
770 typedef void (*GHookMarshaller) (GHook *hook,
772 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
774 typedef void (*GHookFunc) (gpointer data);
775 typedef gboolean (*GHookCheckFunc) (gpointer data);
776 typedef void (*GHookFreeFunc) (GHookList *hook_list,
778 typedef void (*GLogFunc) (const gchar *log_domain,
779 GLogLevelFlags log_level,
780 const gchar *message,
782 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
784 typedef void (*GNodeForeachFunc) (GNode *node,
786 typedef gint (*GSearchFunc) (gpointer key,
788 typedef void (*GScannerMsgFunc) (GScanner *scanner,
791 typedef gint (*GTraverseFunc) (gpointer key,
794 typedef void (*GVoidFunc) (void);
846 /* Doubly linked lists
848 void g_list_push_allocator (GAllocator *allocator);
849 void g_list_pop_allocator (void);
850 GList* g_list_alloc (void);
851 void g_list_free (GList *list);
852 void g_list_free_1 (GList *list);
853 GList* g_list_append (GList *list,
855 GList* g_list_prepend (GList *list,
857 GList* g_list_insert (GList *list,
860 GList* g_list_insert_sorted (GList *list,
863 GList* g_list_concat (GList *list1,
865 GList* g_list_remove (GList *list,
867 GList* g_list_remove_link (GList *list,
869 GList* g_list_reverse (GList *list);
870 GList* g_list_copy (GList *list);
871 GList* g_list_nth (GList *list,
873 GList* g_list_find (GList *list,
875 GList* g_list_find_custom (GList *list,
878 gint g_list_position (GList *list,
880 gint g_list_index (GList *list,
882 GList* g_list_last (GList *list);
883 GList* g_list_first (GList *list);
884 guint g_list_length (GList *list);
885 void g_list_foreach (GList *list,
888 GList* g_list_sort (GList *list,
889 GCompareFunc compare_func);
890 gpointer g_list_nth_data (GList *list,
892 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
893 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
896 /* Singly linked lists
898 void g_slist_push_allocator (GAllocator *allocator);
899 void g_slist_pop_allocator (void);
900 GSList* g_slist_alloc (void);
901 void g_slist_free (GSList *list);
902 void g_slist_free_1 (GSList *list);
903 GSList* g_slist_append (GSList *list,
905 GSList* g_slist_prepend (GSList *list,
907 GSList* g_slist_insert (GSList *list,
910 GSList* g_slist_insert_sorted (GSList *list,
913 GSList* g_slist_concat (GSList *list1,
915 GSList* g_slist_remove (GSList *list,
917 GSList* g_slist_remove_link (GSList *list,
919 GSList* g_slist_reverse (GSList *list);
920 GSList* g_slist_copy (GSList *list);
921 GSList* g_slist_nth (GSList *list,
923 GSList* g_slist_find (GSList *list,
925 GSList* g_slist_find_custom (GSList *list,
928 gint g_slist_position (GSList *list,
930 gint g_slist_index (GSList *list,
932 GSList* g_slist_last (GSList *list);
933 guint g_slist_length (GSList *list);
934 void g_slist_foreach (GSList *list,
937 GSList* g_slist_sort (GSList *list,
938 GCompareFunc compare_func);
939 gpointer g_slist_nth_data (GSList *list,
941 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
946 GHashTable* g_hash_table_new (GHashFunc hash_func,
947 GCompareFunc key_compare_func);
948 void g_hash_table_destroy (GHashTable *hash_table);
949 void g_hash_table_insert (GHashTable *hash_table,
952 void g_hash_table_remove (GHashTable *hash_table,
954 gpointer g_hash_table_lookup (GHashTable *hash_table,
956 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
957 gconstpointer lookup_key,
960 void g_hash_table_freeze (GHashTable *hash_table);
961 void g_hash_table_thaw (GHashTable *hash_table);
962 void g_hash_table_foreach (GHashTable *hash_table,
965 gint g_hash_table_foreach_remove (GHashTable *hash_table,
968 gint g_hash_table_size (GHashTable *hash_table);
973 GCache* g_cache_new (GCacheNewFunc value_new_func,
974 GCacheDestroyFunc value_destroy_func,
975 GCacheDupFunc key_dup_func,
976 GCacheDestroyFunc key_destroy_func,
977 GHashFunc hash_key_func,
978 GHashFunc hash_value_func,
979 GCompareFunc key_compare_func);
980 void g_cache_destroy (GCache *cache);
981 gpointer g_cache_insert (GCache *cache,
983 void g_cache_remove (GCache *cache,
985 void g_cache_key_foreach (GCache *cache,
988 void g_cache_value_foreach (GCache *cache,
993 /* Balanced binary trees
995 GTree* g_tree_new (GCompareFunc key_compare_func);
996 void g_tree_destroy (GTree *tree);
997 void g_tree_insert (GTree *tree,
1000 void g_tree_remove (GTree *tree,
1002 gpointer g_tree_lookup (GTree *tree,
1004 void g_tree_traverse (GTree *tree,
1005 GTraverseFunc traverse_func,
1006 GTraverseType traverse_type,
1008 gpointer g_tree_search (GTree *tree,
1009 GSearchFunc search_func,
1011 gint g_tree_height (GTree *tree);
1012 gint g_tree_nnodes (GTree *tree);
1016 /* N-way tree implementation
1027 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1028 ((GNode*) (node))->prev == NULL && \
1029 ((GNode*) (node))->next == NULL)
1030 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1032 void g_node_push_allocator (GAllocator *allocator);
1033 void g_node_pop_allocator (void);
1034 GNode* g_node_new (gpointer data);
1035 void g_node_destroy (GNode *root);
1036 void g_node_unlink (GNode *node);
1037 GNode* g_node_insert (GNode *parent,
1040 GNode* g_node_insert_before (GNode *parent,
1043 GNode* g_node_prepend (GNode *parent,
1045 guint g_node_n_nodes (GNode *root,
1046 GTraverseFlags flags);
1047 GNode* g_node_get_root (GNode *node);
1048 gboolean g_node_is_ancestor (GNode *node,
1050 guint g_node_depth (GNode *node);
1051 GNode* g_node_find (GNode *root,
1052 GTraverseType order,
1053 GTraverseFlags flags,
1056 /* convenience macros */
1057 #define g_node_append(parent, node) \
1058 g_node_insert_before ((parent), NULL, (node))
1059 #define g_node_insert_data(parent, position, data) \
1060 g_node_insert ((parent), (position), g_node_new (data))
1061 #define g_node_insert_data_before(parent, sibling, data) \
1062 g_node_insert_before ((parent), (sibling), g_node_new (data))
1063 #define g_node_prepend_data(parent, data) \
1064 g_node_prepend ((parent), g_node_new (data))
1065 #define g_node_append_data(parent, data) \
1066 g_node_insert_before ((parent), NULL, g_node_new (data))
1068 /* traversal function, assumes that `node' is root
1069 * (only traverses `node' and its subtree).
1070 * this function is just a high level interface to
1071 * low level traversal functions, optimized for speed.
1073 void g_node_traverse (GNode *root,
1074 GTraverseType order,
1075 GTraverseFlags flags,
1077 GNodeTraverseFunc func,
1080 /* return the maximum tree height starting with `node', this is an expensive
1081 * operation, since we need to visit all nodes. this could be shortened by
1082 * adding `guint height' to struct _GNode, but then again, this is not very
1083 * often needed, and would make g_node_insert() more time consuming.
1085 guint g_node_max_height (GNode *root);
1087 void g_node_children_foreach (GNode *node,
1088 GTraverseFlags flags,
1089 GNodeForeachFunc func,
1091 void g_node_reverse_children (GNode *node);
1092 guint g_node_n_children (GNode *node);
1093 GNode* g_node_nth_child (GNode *node,
1095 GNode* g_node_last_child (GNode *node);
1096 GNode* g_node_find_child (GNode *node,
1097 GTraverseFlags flags,
1099 gint g_node_child_position (GNode *node,
1101 gint g_node_child_index (GNode *node,
1104 GNode* g_node_first_sibling (GNode *node);
1105 GNode* g_node_last_sibling (GNode *node);
1107 #define g_node_prev_sibling(node) ((node) ? \
1108 ((GNode*) (node))->prev : NULL)
1109 #define g_node_next_sibling(node) ((node) ? \
1110 ((GNode*) (node))->next : NULL)
1111 #define g_node_first_child(node) ((node) ? \
1112 ((GNode*) (node))->children : NULL)
1115 /* Callback maintenance functions
1117 #define G_HOOK_FLAG_USER_SHIFT (4)
1120 G_HOOK_FLAG_ACTIVE = 1 << 0,
1121 G_HOOK_FLAG_IN_CALL = 1 << 1,
1122 G_HOOK_FLAG_MASK = 0x0f
1131 GMemChunk *hook_memchunk;
1132 GHookFreeFunc hook_free; /* virtual function */
1144 GDestroyNotify destroy;
1147 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1148 G_HOOK_FLAG_ACTIVE) != 0)
1149 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1150 G_HOOK_FLAG_IN_CALL) != 0)
1151 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1152 G_HOOK_ACTIVE (hook))
1153 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1154 ((GHook*) hook)->prev == NULL && \
1155 ((GHook*) hook)->hook_id == 0 && \
1156 ((GHook*) hook)->ref_count == 0)
1158 void g_hook_list_init (GHookList *hook_list,
1160 void g_hook_list_clear (GHookList *hook_list);
1161 GHook* g_hook_alloc (GHookList *hook_list);
1162 void g_hook_free (GHookList *hook_list,
1164 void g_hook_ref (GHookList *hook_list,
1166 void g_hook_unref (GHookList *hook_list,
1168 gboolean g_hook_destroy (GHookList *hook_list,
1170 void g_hook_destroy_link (GHookList *hook_list,
1172 void g_hook_prepend (GHookList *hook_list,
1174 void g_hook_insert_before (GHookList *hook_list,
1177 void g_hook_insert_sorted (GHookList *hook_list,
1179 GHookCompareFunc func);
1180 GHook* g_hook_get (GHookList *hook_list,
1182 GHook* g_hook_find (GHookList *hook_list,
1183 gboolean need_valids,
1186 GHook* g_hook_find_data (GHookList *hook_list,
1187 gboolean need_valids,
1189 GHook* g_hook_find_func (GHookList *hook_list,
1190 gboolean need_valids,
1192 GHook* g_hook_find_func_data (GHookList *hook_list,
1193 gboolean need_valids,
1196 /* return the first valid hook, and increment its reference count */
1197 GHook* g_hook_first_valid (GHookList *hook_list,
1198 gboolean may_be_in_call);
1199 /* return the next valid hook with incremented reference count, and
1200 * decrement the reference count of the original hook
1202 GHook* g_hook_next_valid (GHookList *hook_list,
1204 gboolean may_be_in_call);
1206 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1207 gint g_hook_compare_ids (GHook *new_hook,
1210 /* convenience macros */
1211 #define g_hook_append( hook_list, hook ) \
1212 g_hook_insert_before ((hook_list), NULL, (hook))
1214 /* invoke all valid hooks with the (*GHookFunc) signature.
1216 void g_hook_list_invoke (GHookList *hook_list,
1217 gboolean may_recurse);
1218 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1219 * and destroy the hook if FALSE is returned.
1221 void g_hook_list_invoke_check (GHookList *hook_list,
1222 gboolean may_recurse);
1223 /* invoke a marshaller on all valid hooks.
1225 void g_hook_list_marshal (GHookList *hook_list,
1226 gboolean may_recurse,
1227 GHookMarshaller marshaller,
1229 void g_hook_list_marshal_check (GHookList *hook_list,
1230 gboolean may_recurse,
1231 GHookCheckMarshaller marshaller,
1235 /* Fatal error handlers.
1236 * g_on_error_query() will prompt the user to either
1237 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1238 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1239 * process and shows a stack trace.
1240 * These function may cause different actions on non-unix platforms.
1241 * The prg_name arg is required by gdb to find the executable, if it is
1242 * passed as NULL, g_on_error_query() will try g_get_prgname().
1244 void g_on_error_query (const gchar *prg_name);
1245 void g_on_error_stack_trace (const gchar *prg_name);
1248 /* Logging mechanism
1250 extern const gchar *g_log_domain_glib;
1251 guint g_log_set_handler (const gchar *log_domain,
1252 GLogLevelFlags log_levels,
1254 gpointer user_data);
1255 void g_log_remove_handler (const gchar *log_domain,
1257 void g_log_default_handler (const gchar *log_domain,
1258 GLogLevelFlags log_level,
1259 const gchar *message,
1260 gpointer unused_data);
1261 void g_log (const gchar *log_domain,
1262 GLogLevelFlags log_level,
1263 const gchar *format,
1264 ...) G_GNUC_PRINTF (3, 4);
1265 void g_logv (const gchar *log_domain,
1266 GLogLevelFlags log_level,
1267 const gchar *format,
1269 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1270 GLogLevelFlags fatal_mask);
1271 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1272 #ifndef G_LOG_DOMAIN
1273 #define G_LOG_DOMAIN (NULL)
1274 #endif /* G_LOG_DOMAIN */
1276 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1277 G_LOG_LEVEL_ERROR, \
1279 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1280 G_LOG_LEVEL_MESSAGE, \
1282 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1283 G_LOG_LEVEL_WARNING, \
1285 #else /* !__GNUC__ */
1287 g_error (const gchar *format,
1291 va_start (args, format);
1292 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1296 g_message (const gchar *format,
1300 va_start (args, format);
1301 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1305 g_warning (const gchar *format,
1309 va_start (args, format);
1310 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1313 #endif /* !__GNUC__ */
1315 typedef void (*GPrintFunc) (const gchar *string);
1316 void g_print (const gchar *format,
1317 ...) G_GNUC_PRINTF (1, 2);
1318 GPrintFunc g_set_print_handler (GPrintFunc func);
1319 void g_printerr (const gchar *format,
1320 ...) G_GNUC_PRINTF (1, 2);
1321 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1323 /* deprecated compatibility functions, use g_log_set_handler() instead */
1324 typedef void (*GErrorFunc) (const gchar *str);
1325 typedef void (*GWarningFunc) (const gchar *str);
1326 GErrorFunc g_set_error_handler (GErrorFunc func);
1327 GWarningFunc g_set_warning_handler (GWarningFunc func);
1328 GPrintFunc g_set_message_handler (GPrintFunc func);
1331 /* Memory allocation and debugging
1335 #define g_malloc(size) ((gpointer) MALLOC (size))
1336 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1337 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1338 #define g_free(mem) FREE (mem)
1340 #else /* !USE_DMALLOC */
1342 gpointer g_malloc (gulong size);
1343 gpointer g_malloc0 (gulong size);
1344 gpointer g_realloc (gpointer mem,
1346 void g_free (gpointer mem);
1348 #endif /* !USE_DMALLOC */
1350 void g_mem_profile (void);
1351 void g_mem_check (gpointer mem);
1353 /* Generic allocators
1355 GAllocator* g_allocator_new (const gchar *name,
1357 void g_allocator_free (GAllocator *allocator);
1359 #define G_ALLOCATOR_LIST (1)
1360 #define G_ALLOCATOR_SLIST (2)
1361 #define G_ALLOCATOR_NODE (3)
1364 /* "g_mem_chunk_new" creates a new memory chunk.
1365 * Memory chunks are used to allocate pieces of memory which are
1366 * always the same size. Lists are a good example of such a data type.
1367 * The memory chunk allocates and frees blocks of memory as needed.
1368 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1369 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1370 * fault...somewhere).
1372 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1373 * want to know what's going on inside do you?)
1376 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1377 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1378 * atom. (They are also useful for lists which use MemChunk to allocate
1379 * memory but are also part of the MemChunk implementation).
1380 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1383 #define G_ALLOC_ONLY 1
1384 #define G_ALLOC_AND_FREE 2
1386 GMemChunk* g_mem_chunk_new (gchar *name,
1390 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1391 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1392 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1393 void g_mem_chunk_free (GMemChunk *mem_chunk,
1395 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1396 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1397 void g_mem_chunk_print (GMemChunk *mem_chunk);
1398 void g_mem_chunk_info (void);
1400 /* Ah yes...we have a "g_blow_chunks" function.
1401 * "g_blow_chunks" simply compresses all the chunks. This operation
1402 * consists of freeing every memory area that should be freed (but
1403 * which we haven't gotten around to doing yet). And, no,
1404 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1405 * much better name than "g_mem_chunk_clean_all" or something
1408 void g_blow_chunks (void);
1413 GTimer* g_timer_new (void);
1414 void g_timer_destroy (GTimer *timer);
1415 void g_timer_start (GTimer *timer);
1416 void g_timer_stop (GTimer *timer);
1417 void g_timer_reset (GTimer *timer);
1418 gdouble g_timer_elapsed (GTimer *timer,
1419 gulong *microseconds);
1422 /* String utility functions that modify a string argument or
1423 * return a constant string that must not be freed.
1425 #define G_STR_DELIMITERS "_-|> <."
1426 gchar* g_strdelimit (gchar *string,
1427 const gchar *delimiters,
1428 gchar new_delimiter);
1429 gdouble g_strtod (const gchar *nptr,
1431 gchar* g_strerror (gint errnum);
1432 gchar* g_strsignal (gint signum);
1433 gint g_strcasecmp (const gchar *s1,
1435 gint g_strncasecmp (const gchar *s1,
1438 void g_strdown (gchar *string);
1439 void g_strup (gchar *string);
1440 void g_strreverse (gchar *string);
1441 /* removes leading spaces */
1442 gchar* g_strchug (gchar *string);
1443 /* removes trailing spaces */
1444 gchar* g_strchomp (gchar *string);
1445 /* removes leading & trailing spaces */
1446 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1448 /* String utility functions that return a newly allocated string which
1449 * ought to be freed from the caller at some point.
1451 gchar* g_strdup (const gchar *str);
1452 gchar* g_strdup_printf (const gchar *format,
1453 ...) G_GNUC_PRINTF (1, 2);
1454 gchar* g_strdup_vprintf (const gchar *format,
1456 gchar* g_strndup (const gchar *str,
1458 gchar* g_strnfill (guint length,
1460 gchar* g_strconcat (const gchar *string1,
1461 ...); /* NULL terminated */
1462 gchar* g_strjoin (const gchar *separator,
1463 ...); /* NULL terminated */
1464 gchar* g_strescape (gchar *string);
1465 gpointer g_memdup (gconstpointer mem,
1468 /* Macros for dynamic strings via fast stack allocation
1469 * All macros take a special first argument: the target gchar* string
1473 # define g_strdup_a(newstr,str) G_STMT_START { \
1474 if ((str) == NULL) (newstr) = NULL; \
1476 const char *__old = (str); \
1478 size_t __len = strlen (__old) + 1; \
1479 __new = alloca (__len); \
1480 memcpy (__new, __old, __len); \
1485 # define g_strndup_a(newstr,str,n) G_STMT_START { \
1486 if ((str) == NULL) (newstr) = NULL; \
1488 const char *__old = (str); \
1490 size_t __len = strlen (__old); \
1491 if (__len > (n)) __len = (n); \
1492 __new = alloca (__len + 1); \
1493 memcpy (__new, __old, __len); \
1499 # define g_strconcat3_a(newstr,str1,str2,str3) G_STMT_START { \
1500 size_t __len1 = ((str1) == (gchar*)NULL) ? 0 : strlen((str1)); \
1501 size_t __len2 = ((str2) == (gchar*)NULL) ? 0 : strlen((str2)); \
1502 size_t __len3 = ((str3) == (gchar*)NULL) ? 0 : strlen((str3)); \
1503 char *__sptr, *__new = \
1504 alloca (__len1 + __len2 + __len3 + 1); \
1506 if (__len1){memcpy (__sptr, (str1), __len1); __sptr += __len1;} \
1507 if (__len2){memcpy (__sptr, (str2), __len2); __sptr += __len2;} \
1508 if (__len3){memcpy (__sptr, (str3), __len3); __sptr += __len3;} \
1513 #endif /* G_HAVE_ALLOCA */
1516 /* NULL terminated string arrays.
1517 * g_strsplit() splits up string into max_tokens tokens at delim and
1518 * returns a newly allocated string array.
1519 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1520 * optional separator, the returned string is newly allocated.
1521 * g_strfreev() frees the array itself and all of its strings.
1523 gchar** g_strsplit (const gchar *string,
1524 const gchar *delimiter,
1526 gchar* g_strjoinv (const gchar *separator,
1528 void g_strfreev (gchar **str_array);
1532 /* calculate a string size, guarranteed to fit format + args.
1534 guint g_printf_string_upper_bound (const gchar* format,
1538 /* Retrive static string info
1540 gchar* g_get_user_name (void);
1541 gchar* g_get_real_name (void);
1542 gchar* g_get_home_dir (void);
1543 gchar* g_get_tmp_dir (void);
1544 gchar* g_get_prgname (void);
1545 void g_set_prgname (const gchar *prgname);
1548 /* Miscellaneous utility functions
1550 guint g_parse_debug_string (const gchar *string,
1553 gint g_snprintf (gchar *string,
1555 gchar const *format,
1556 ...) G_GNUC_PRINTF (3, 4);
1557 gint g_vsnprintf (gchar *string,
1559 gchar const *format,
1561 gchar* g_basename (const gchar *file_name);
1562 /* Check if a file name is an absolute path */
1563 gboolean g_path_is_absolute (const gchar *file_name);
1564 /* In case of absolute paths, skip the root part */
1565 gchar* g_path_skip_root (gchar *file_name);
1567 /* strings are newly allocated with g_malloc() */
1568 gchar* g_dirname (const gchar *file_name);
1569 gchar* g_get_current_dir (void);
1570 gchar* g_getenv (const gchar *variable);
1573 /* we use a GLib function as a replacement for ATEXIT, so
1574 * the programmer is not required to check the return value
1575 * (if there is any in the implementation) and doesn't encounter
1576 * missing include files.
1578 void g_atexit (GVoidFunc func);
1583 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1587 g_bit_nth_lsf (guint32 mask,
1593 if (mask & (1 << (guint) nth_bit))
1596 while (nth_bit < 32);
1599 #endif /* G_CAN_INLINE */
1601 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1605 g_bit_nth_msf (guint32 mask,
1613 if (mask & (1 << (guint) nth_bit))
1616 while (nth_bit > 0);
1619 #endif /* G_CAN_INLINE */
1621 G_INLINE_FUNC guint g_bit_storage (guint number);
1624 g_bit_storage (guint number)
1626 register guint n_bits = 0;
1636 #endif /* G_CAN_INLINE */
1640 GStringChunk* g_string_chunk_new (gint size);
1641 void g_string_chunk_free (GStringChunk *chunk);
1642 gchar* g_string_chunk_insert (GStringChunk *chunk,
1643 const gchar *string);
1644 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1645 const gchar *string);
1650 GString* g_string_new (const gchar *init);
1651 GString* g_string_sized_new (guint dfl_size);
1652 void g_string_free (GString *string,
1654 GString* g_string_assign (GString *lval,
1656 GString* g_string_truncate (GString *string,
1658 GString* g_string_append (GString *string,
1660 GString* g_string_append_c (GString *string,
1662 GString* g_string_prepend (GString *string,
1664 GString* g_string_prepend_c (GString *string,
1666 GString* g_string_insert (GString *string,
1669 GString* g_string_insert_c (GString *string,
1672 GString* g_string_erase (GString *string,
1675 GString* g_string_down (GString *string);
1676 GString* g_string_up (GString *string);
1677 void g_string_sprintf (GString *string,
1678 const gchar *format,
1679 ...) G_GNUC_PRINTF (2, 3);
1680 void g_string_sprintfa (GString *string,
1681 const gchar *format,
1682 ...) G_GNUC_PRINTF (2, 3);
1685 /* Resizable arrays, remove fills any cleared spot and shortens the
1686 * array, while preserving the order. remove_fast will distort the
1687 * order by moving the last element to the position of the removed
1690 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1691 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1692 #define g_array_insert_val(a,i,v) g_array_prepend_vals(a,i,&v,1)
1693 #define g_array_index(a,t,i) (((t*)a->data)[i])
1695 GArray* g_array_new (gboolean zero_terminated,
1697 guint element_size);
1698 void g_array_free (GArray *array,
1699 gboolean free_segment);
1700 GArray* g_array_append_vals (GArray *array,
1703 GArray* g_array_prepend_vals (GArray *array,
1706 GArray* g_array_insert_vals (GArray *array,
1710 GArray* g_array_set_size (GArray *array,
1712 GArray* g_array_remove_index (GArray *array,
1714 GArray* g_array_remove_index_fast (GArray *array,
1717 /* Resizable pointer array. This interface is much less complicated
1718 * than the above. Add appends appends a pointer. Remove fills any
1719 * cleared spot and shortens the array. remove_fast will again distort
1722 #define g_ptr_array_index(array,index) (array->pdata)[index]
1723 GPtrArray* g_ptr_array_new (void);
1724 void g_ptr_array_free (GPtrArray *array,
1726 void g_ptr_array_set_size (GPtrArray *array,
1728 gpointer g_ptr_array_remove_index (GPtrArray *array,
1730 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1732 gboolean g_ptr_array_remove (GPtrArray *array,
1734 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1736 void g_ptr_array_add (GPtrArray *array,
1739 /* Byte arrays, an array of guint8. Implemented as a GArray,
1743 GByteArray* g_byte_array_new (void);
1744 void g_byte_array_free (GByteArray *array,
1745 gboolean free_segment);
1746 GByteArray* g_byte_array_append (GByteArray *array,
1749 GByteArray* g_byte_array_prepend (GByteArray *array,
1752 GByteArray* g_byte_array_set_size (GByteArray *array,
1754 GByteArray* g_byte_array_remove_index (GByteArray *array,
1756 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1762 gint g_str_equal (gconstpointer v,
1764 guint g_str_hash (gconstpointer v);
1766 gint g_int_equal (gconstpointer v,
1768 guint g_int_hash (gconstpointer v);
1770 /* This "hash" function will just return the key's adress as an
1771 * unsigned integer. Useful for hashing on plain adresses or
1772 * simple integer values.
1774 guint g_direct_hash (gconstpointer v);
1775 gint g_direct_equal (gconstpointer v,
1779 /* Quarks (string<->id association)
1781 GQuark g_quark_try_string (const gchar *string);
1782 GQuark g_quark_from_static_string (const gchar *string);
1783 GQuark g_quark_from_string (const gchar *string);
1784 gchar* g_quark_to_string (GQuark quark);
1789 void g_datalist_init (GData **datalist);
1790 void g_datalist_clear (GData **datalist);
1791 gpointer g_datalist_id_get_data (GData **datalist,
1793 void g_datalist_id_set_data_full (GData **datalist,
1796 GDestroyNotify destroy_func);
1797 void g_datalist_id_remove_no_notify (GData **datalist,
1799 void g_datalist_foreach (GData **datalist,
1800 GDataForeachFunc func,
1801 gpointer user_data);
1802 #define g_datalist_id_set_data(dl, q, d) \
1803 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1804 #define g_datalist_id_remove_data(dl, q) \
1805 g_datalist_id_set_data ((dl), (q), NULL)
1806 #define g_datalist_get_data(dl, k) \
1807 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1808 #define g_datalist_set_data_full(dl, k, d, f) \
1809 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1810 #define g_datalist_remove_no_notify(dl, k) \
1811 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1812 #define g_datalist_set_data(dl, k, d) \
1813 g_datalist_set_data_full ((dl), (k), (d), NULL)
1814 #define g_datalist_remove_data(dl, k) \
1815 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1818 /* Location Associated Keyed Data
1820 void g_dataset_destroy (gconstpointer dataset_location);
1821 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1823 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1826 GDestroyNotify destroy_func);
1827 void g_dataset_id_remove_no_notify (gconstpointer dataset_location,
1829 void g_dataset_foreach (gconstpointer dataset_location,
1830 GDataForeachFunc func,
1831 gpointer user_data);
1832 #define g_dataset_id_set_data(l, k, d) \
1833 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1834 #define g_dataset_id_remove_data(l, k) \
1835 g_dataset_id_set_data ((l), (k), NULL)
1836 #define g_dataset_get_data(l, k) \
1837 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1838 #define g_dataset_set_data_full(l, k, d, f) \
1839 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1840 #define g_dataset_remove_no_notify(l, k) \
1841 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1842 #define g_dataset_set_data(l, k, d) \
1843 g_dataset_set_data_full ((l), (k), (d), NULL)
1844 #define g_dataset_remove_data(l, k) \
1845 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1848 /* GScanner: Flexible lexical scanner for general purpose.
1851 /* Character sets */
1852 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1853 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1854 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1855 "\307\310\311\312\313\314\315\316\317\320"\
1856 "\321\322\323\324\325\326"\
1857 "\330\331\332\333\334\335\336"
1858 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1859 "\347\350\351\352\353\354\355\356\357\360"\
1860 "\361\362\363\364\365\366"\
1861 "\370\371\372\373\374\375\376\377"
1868 G_ERR_UNEXP_EOF_IN_STRING,
1869 G_ERR_UNEXP_EOF_IN_COMMENT,
1870 G_ERR_NON_DIGIT_IN_CONST,
1873 G_ERR_FLOAT_MALFORMED
1881 G_TOKEN_LEFT_PAREN = '(',
1882 G_TOKEN_RIGHT_PAREN = ')',
1883 G_TOKEN_LEFT_CURLY = '{',
1884 G_TOKEN_RIGHT_CURLY = '}',
1885 G_TOKEN_LEFT_BRACE = '[',
1886 G_TOKEN_RIGHT_BRACE = ']',
1887 G_TOKEN_EQUAL_SIGN = '=',
1888 G_TOKEN_COMMA = ',',
1904 G_TOKEN_IDENTIFIER_NULL,
1906 G_TOKEN_COMMENT_SINGLE,
1907 G_TOKEN_COMMENT_MULTI,
1914 gchar *v_identifier;
1926 struct _GScannerConfig
1930 gchar *cset_skip_characters; /* default: " \t\n" */
1931 gchar *cset_identifier_first;
1932 gchar *cset_identifier_nth;
1933 gchar *cpair_comment_single; /* default: "#\n" */
1935 /* Should symbol lookup work case sensitive?
1937 guint case_sensitive : 1;
1939 /* Boolean values to be adjusted "on the fly"
1940 * to configure scanning behaviour.
1942 guint skip_comment_multi : 1; /* C like comment */
1943 guint skip_comment_single : 1; /* single line comment */
1944 guint scan_comment_multi : 1; /* scan multi line comments? */
1945 guint scan_identifier : 1;
1946 guint scan_identifier_1char : 1;
1947 guint scan_identifier_NULL : 1;
1948 guint scan_symbols : 1;
1949 guint scan_binary : 1;
1950 guint scan_octal : 1;
1951 guint scan_float : 1;
1952 guint scan_hex : 1; /* `0x0ff0' */
1953 guint scan_hex_dollar : 1; /* `$0ff0' */
1954 guint scan_string_sq : 1; /* string: 'anything' */
1955 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
1956 guint numbers_2_int : 1; /* bin, octal, hex => int */
1957 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
1958 guint identifier_2_string : 1;
1959 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
1960 guint symbol_2_token : 1;
1961 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
1968 guint max_parse_errors;
1970 /* g_scanner_error() increments this field */
1973 /* name of input stream, featured by the default message handler */
1974 const gchar *input_name;
1976 /* data pointer for derived structures */
1977 gpointer derived_data;
1979 /* link into the scanner configuration */
1980 GScannerConfig *config;
1982 /* fields filled in after g_scanner_get_next_token() */
1988 /* fields filled in after g_scanner_peek_next_token() */
1989 GTokenType next_token;
1990 GTokenValue next_value;
1992 guint next_position;
1994 /* to be considered private */
1995 GHashTable *symbol_table;
1998 const gchar *text_end;
2002 /* handler function for _warn and _error */
2003 GScannerMsgFunc msg_handler;
2006 GScanner* g_scanner_new (GScannerConfig *config_templ);
2007 void g_scanner_destroy (GScanner *scanner);
2008 void g_scanner_input_file (GScanner *scanner,
2010 void g_scanner_sync_file_offset (GScanner *scanner);
2011 void g_scanner_input_text (GScanner *scanner,
2014 GTokenType g_scanner_get_next_token (GScanner *scanner);
2015 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2016 GTokenType g_scanner_cur_token (GScanner *scanner);
2017 GTokenValue g_scanner_cur_value (GScanner *scanner);
2018 guint g_scanner_cur_line (GScanner *scanner);
2019 guint g_scanner_cur_position (GScanner *scanner);
2020 gboolean g_scanner_eof (GScanner *scanner);
2021 guint g_scanner_set_scope (GScanner *scanner,
2023 void g_scanner_scope_add_symbol (GScanner *scanner,
2025 const gchar *symbol,
2027 void g_scanner_scope_remove_symbol (GScanner *scanner,
2029 const gchar *symbol);
2030 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2032 const gchar *symbol);
2033 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2036 gpointer user_data);
2037 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2038 const gchar *symbol);
2039 void g_scanner_freeze_symbol_table (GScanner *scanner);
2040 void g_scanner_thaw_symbol_table (GScanner *scanner);
2041 void g_scanner_unexp_token (GScanner *scanner,
2042 GTokenType expected_token,
2043 const gchar *identifier_spec,
2044 const gchar *symbol_spec,
2045 const gchar *symbol_name,
2046 const gchar *message,
2048 void g_scanner_error (GScanner *scanner,
2049 const gchar *format,
2050 ...) G_GNUC_PRINTF (2,3);
2051 void g_scanner_warn (GScanner *scanner,
2052 const gchar *format,
2053 ...) G_GNUC_PRINTF (2,3);
2054 gint g_scanner_stat_mode (const gchar *filename);
2055 /* keep downward source compatibility */
2056 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2057 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2059 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2060 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2062 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2063 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2073 GCompletionFunc func;
2079 GCompletion* g_completion_new (GCompletionFunc func);
2080 void g_completion_add_items (GCompletion* cmp,
2082 void g_completion_remove_items (GCompletion* cmp,
2084 void g_completion_clear_items (GCompletion* cmp);
2085 GList* g_completion_complete (GCompletion* cmp,
2087 gchar** new_prefix);
2088 void g_completion_free (GCompletion* cmp);
2093 * Date calculations (not time for now, to be resolved). These are a
2094 * mutant combination of Steffen Beyer's DateCalc routines
2095 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2096 * date routines (written for in-house software). Written by Havoc
2097 * Pennington <hp@pobox.com>
2100 typedef guint16 GDateYear;
2101 typedef guint8 GDateDay; /* day of the month */
2102 typedef struct _GDate GDate;
2103 /* make struct tm known without having to include time.h */
2106 /* enum used to specify order of appearance in parsed date strings */
2114 /* actual week and month values */
2117 G_DATE_BAD_WEEKDAY = 0,
2120 G_DATE_WEDNESDAY = 3,
2121 G_DATE_THURSDAY = 4,
2123 G_DATE_SATURDAY = 6,
2128 G_DATE_BAD_MONTH = 0,
2130 G_DATE_FEBRUARY = 2,
2137 G_DATE_SEPTEMBER = 9,
2138 G_DATE_OCTOBER = 10,
2139 G_DATE_NOVEMBER = 11,
2140 G_DATE_DECEMBER = 12
2143 #define G_DATE_BAD_JULIAN 0U
2144 #define G_DATE_BAD_DAY 0U
2145 #define G_DATE_BAD_YEAR 0U
2147 /* Note: directly manipulating structs is generally a bad idea, but
2148 * in this case it's an *incredibly* bad idea, because all or part
2149 * of this struct can be invalid at any given time. Use the functions,
2150 * or you will get hosed, I promise.
2154 guint julian_days : 32; /* julian days representation - we use a
2155 * bitfield hoping that 64 bit platforms
2156 * will pack this whole struct in one big
2160 guint julian : 1; /* julian is valid */
2161 guint dmy : 1; /* dmy is valid */
2163 /* DMY representation */
2169 /* g_date_new() returns an invalid date, you then have to _set() stuff
2170 * to get a usable object. You can also allocate a GDate statically,
2171 * then call g_date_clear() to initialize.
2173 GDate* g_date_new (void);
2174 GDate* g_date_new_dmy (GDateDay day,
2177 GDate* g_date_new_julian (guint32 julian_day);
2178 void g_date_free (GDate *date);
2180 /* check g_date_valid() after doing an operation that might fail, like
2181 * _parse. Almost all g_date operations are undefined on invalid
2182 * dates (the exceptions are the mutators, since you need those to
2183 * return to validity).
2185 gboolean g_date_valid (GDate *date);
2186 gboolean g_date_valid_day (GDateDay day);
2187 gboolean g_date_valid_month (GDateMonth month);
2188 gboolean g_date_valid_year (GDateYear year);
2189 gboolean g_date_valid_weekday (GDateWeekday weekday);
2190 gboolean g_date_valid_julian (guint32 julian_date);
2191 gboolean g_date_valid_dmy (GDateDay day,
2195 GDateWeekday g_date_weekday (GDate *date);
2196 GDateMonth g_date_month (GDate *date);
2197 GDateYear g_date_year (GDate *date);
2198 GDateDay g_date_day (GDate *date);
2199 guint32 g_date_julian (GDate *date);
2200 guint g_date_day_of_year (GDate *date);
2202 /* First monday/sunday is the start of week 1; if we haven't reached
2203 * that day, return 0. These are not ISO weeks of the year; that
2204 * routine needs to be added.
2205 * these functions return the number of weeks, starting on the
2208 guint g_date_monday_week_of_year (GDate *date);
2209 guint g_date_sunday_week_of_year (GDate *date);
2211 /* If you create a static date struct you need to clear it to get it
2212 * in a sane state before use. You can clear a whole array at
2213 * once with the ndates argument.
2215 void g_date_clear (GDate *date,
2218 /* The parse routine is meant for dates typed in by a user, so it
2219 * permits many formats but tries to catch common typos. If your data
2220 * needs to be strictly validated, it is not an appropriate function.
2222 void g_date_set_parse (GDate *date,
2224 void g_date_set_time (GDate *date,
2226 void g_date_set_month (GDate *date,
2228 void g_date_set_day (GDate *date,
2230 void g_date_set_year (GDate *date,
2232 void g_date_set_dmy (GDate *date,
2236 void g_date_set_julian (GDate *date,
2237 guint32 julian_date);
2238 gboolean g_date_is_first_of_month (GDate *date);
2239 gboolean g_date_is_last_of_month (GDate *date);
2241 /* To go forward by some number of weeks just go forward weeks*7 days */
2242 void g_date_add_days (GDate *date,
2244 void g_date_subtract_days (GDate *date,
2247 /* If you add/sub months while day > 28, the day might change */
2248 void g_date_add_months (GDate *date,
2250 void g_date_subtract_months (GDate *date,
2253 /* If it's feb 29, changing years can move you to the 28th */
2254 void g_date_add_years (GDate *date,
2256 void g_date_subtract_years (GDate *date,
2258 gboolean g_date_is_leap_year (GDateYear year);
2259 guint8 g_date_days_in_month (GDateMonth month,
2261 guint8 g_date_monday_weeks_in_year (GDateYear year);
2262 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2264 /* qsort-friendly (with a cast...) */
2265 gint g_date_compare (GDate *lhs,
2267 void g_date_to_struct_tm (GDate *date,
2270 /* Just like strftime() except you can only use date-related formats.
2271 * Using a time format is undefined.
2273 gsize g_date_strftime (gchar *s,
2275 const gchar *format,
2281 * Indexed Relations. Imagine a really simple table in a
2282 * database. Relations are not ordered. This data type is meant for
2283 * maintaining a N-way mapping.
2285 * g_relation_new() creates a relation with FIELDS fields
2287 * g_relation_destroy() frees all resources
2288 * g_tuples_destroy() frees the result of g_relation_select()
2290 * g_relation_index() indexes relation FIELD with the provided
2291 * equality and hash functions. this must be done before any
2292 * calls to insert are made.
2294 * g_relation_insert() inserts a new tuple. you are expected to
2295 * provide the right number of fields.
2297 * g_relation_delete() deletes all relations with KEY in FIELD
2298 * g_relation_select() returns ...
2299 * g_relation_count() counts ...
2302 GRelation* g_relation_new (gint fields);
2303 void g_relation_destroy (GRelation *relation);
2304 void g_relation_index (GRelation *relation,
2306 GHashFunc hash_func,
2307 GCompareFunc key_compare_func);
2308 void g_relation_insert (GRelation *relation,
2310 gint g_relation_delete (GRelation *relation,
2313 GTuples* g_relation_select (GRelation *relation,
2316 gint g_relation_count (GRelation *relation,
2319 gboolean g_relation_exists (GRelation *relation,
2321 void g_relation_print (GRelation *relation);
2323 void g_tuples_destroy (GTuples *tuples);
2324 gpointer g_tuples_index (GTuples *tuples,
2332 /* This function returns prime numbers spaced by approximately 1.5-2.0
2333 * and is for use in resizing data structures which prefer
2334 * prime-valued sizes. The closest spaced prime function returns the
2335 * next largest prime, or the highest it knows about which is about
2338 guint g_spaced_primes_closest (guint num);
2344 typedef struct _GIOFuncs GIOFuncs;
2360 G_IO_IN GLIB_SYSDEF_POLLIN,
2361 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2362 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2363 G_IO_ERR GLIB_SYSDEF_POLLERR,
2364 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2365 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2370 guint channel_flags;
2375 typedef gboolean (*GIOFunc) (GIOChannel *source,
2376 GIOCondition condition,
2380 GIOError (*io_read) (GIOChannel *channel,
2384 GIOError (*io_write) (GIOChannel *channel,
2387 guint *bytes_written);
2388 GIOError (*io_seek) (GIOChannel *channel,
2391 void (*io_close) (GIOChannel *channel);
2392 guint (*io_add_watch) (GIOChannel *channel,
2394 GIOCondition condition,
2397 GDestroyNotify notify);
2398 void (*io_free) (GIOChannel *channel);
2401 void g_io_channel_init (GIOChannel *channel);
2402 void g_io_channel_ref (GIOChannel *channel);
2403 void g_io_channel_unref (GIOChannel *channel);
2404 GIOError g_io_channel_read (GIOChannel *channel,
2408 GIOError g_io_channel_write (GIOChannel *channel,
2411 guint *bytes_written);
2412 GIOError g_io_channel_seek (GIOChannel *channel,
2415 void g_io_channel_close (GIOChannel *channel);
2416 guint g_io_add_watch_full (GIOChannel *channel,
2418 GIOCondition condition,
2421 GDestroyNotify notify);
2422 guint g_io_add_watch (GIOChannel *channel,
2423 GIOCondition condition,
2425 gpointer user_data);
2430 typedef struct _GTimeVal GTimeVal;
2431 typedef struct _GSourceFuncs GSourceFuncs;
2432 typedef struct _GMainLoop GMainLoop; /* Opaque */
2439 struct _GSourceFuncs
2441 gboolean (*prepare) (gpointer source_data,
2442 GTimeVal *current_time,
2444 gboolean (*check) (gpointer source_data,
2445 GTimeVal *current_time);
2446 gboolean (*dispatch) (gpointer source_data,
2447 GTimeVal *current_time,
2448 gpointer user_data);
2449 GDestroyNotify destroy;
2452 /* Standard priorities */
2454 #define G_PRIORITY_HIGH -100
2455 #define G_PRIORITY_DEFAULT 0
2456 #define G_PRIORITY_HIGH_IDLE 100
2457 #define G_PRIORITY_DEFAULT_IDLE 200
2458 #define G_PRIORITY_LOW 300
2460 typedef gboolean (*GSourceFunc) (gpointer data);
2462 /* Hooks for adding to the main loop */
2463 guint g_source_add (gint priority,
2464 gboolean can_recurse,
2465 GSourceFuncs *funcs,
2466 gpointer source_data,
2468 GDestroyNotify notify);
2469 void g_source_remove (guint tag);
2470 void g_source_remove_by_user_data (gpointer user_data);
2471 void g_source_remove_by_source_data (gpointer source_data);
2473 void g_get_current_time (GTimeVal *result);
2475 /* Running the main loop */
2476 GMainLoop* g_main_new (gboolean is_running);
2477 void g_main_run (GMainLoop *loop);
2478 void g_main_quit (GMainLoop *loop);
2479 void g_main_destroy (GMainLoop *loop);
2480 gboolean g_main_is_running (GMainLoop *loop);
2482 /* Run a single iteration of the mainloop. If block is FALSE,
2485 gboolean g_main_iteration (gboolean may_block);
2487 /* See if any events are pending */
2488 gboolean g_main_pending (void);
2490 /* Idles and timeouts */
2491 guint g_timeout_add_full (gint priority,
2493 GSourceFunc function,
2495 GDestroyNotify notify);
2496 guint g_timeout_add (guint interval,
2497 GSourceFunc function,
2499 guint g_idle_add (GSourceFunc function,
2501 guint g_idle_add_full (gint priority,
2502 GSourceFunc function,
2504 GDestroyNotify destroy);
2508 * Unix-specific IO and main loop calls
2511 typedef struct _GPollFD GPollFD;
2512 typedef gint (*GPollFunc) (GPollFD *ufds,
2522 void g_main_add_poll (GPollFD *fd,
2524 void g_main_remove_poll (GPollFD *fd);
2525 void g_main_set_poll_func (GPollFunc func);
2527 GIOChannel* g_io_channel_unix_new (int fd);
2528 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2531 /* old IO Channels */
2534 * These are used for plug-in communication in the GIMP, for instance.
2535 * On Unix, it's simply an encapsulated file descriptor (a pipe).
2536 * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2537 * of the writable end) a thread id to post a message to when you have written
2542 gint fd; /* file handle (pseudo such in Win32) */
2544 guint peer; /* thread to post message to */
2545 guint peer_fd; /* read handle (in the other process) */
2546 guint offset; /* counter of accumulated bytes, to
2547 * be included in the message posted
2548 * so we keep in sync.
2550 guint need_wakeups; /* in output channels whether the reader
2555 GIOChannel *g_iochannel_new (gint fd);
2556 void g_iochannel_free (GIOChannel *channel);
2557 void g_iochannel_close_and_free (GIOChannel *channel);
2558 void g_iochannel_wakeup_peer (GIOChannel *channel);
2559 #ifndef NATIVE_WIN32
2560 # define g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2562 #endif /* old IO Channels */
2565 /* Windows emulation stubs for common unix functions
2568 # define MAXPATHLEN 1024
2572 /* These POSIXish functions are available in the Microsoft C library
2573 * prefixed with underscore (which of course technically speaking is
2574 * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2575 * prevents Microsoft from practically requiring you to include
2576 * <windows.h> every now and then...).
2578 * You still need to include the appropriate headers to get the
2579 * prototypes, <io.h> or <direct.h>.
2581 * For some functions, we provide emulators in glib, which are prefixed
2584 # define getcwd _getcwd
2585 # define getpid _getpid
2586 # define access _access
2589 # define write _write
2590 # define lseek _lseek
2591 # define close _close
2592 # define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2593 # define popen _popen
2594 # define pclose _pclose
2595 # define fdopen _fdopen
2596 # define ftruncate(fd, size) gwin_ftruncate (fd, size)
2597 # define opendir gwin_opendir
2598 # define readdir gwin_readdir
2599 # define rewinddir gwin_rewinddir
2600 # define closedir gwin_closedir
2601 # define NAME_MAX 255
2605 gboolean just_opened;
2606 guint find_file_handle;
2607 gpointer find_file_data;
2609 typedef struct DIR DIR;
2612 gchar d_name[NAME_MAX + 1];
2614 /* emulation functions */
2615 extern int gwin_ftruncate (gint f,
2617 DIR* gwin_opendir (const gchar *dirname);
2618 struct dirent* gwin_readdir (DIR *dir);
2619 void gwin_rewinddir (DIR *dir);
2620 gint gwin_closedir (DIR *dir);
2621 # endif /* _MSC_VER */
2622 #endif /* NATIVE_WIN32 */
2625 /* GLib Thread support
2627 typedef struct _GMutex GMutex;
2628 typedef struct _GCond GCond;
2629 typedef struct _GPrivate GPrivate;
2630 typedef struct _GStaticPrivate GStaticPrivate;
2631 typedef struct _GThreadFunctions GThreadFunctions;
2632 struct _GThreadFunctions
2634 GMutex* (*mutex_new) (void);
2635 void (*mutex_lock) (GMutex *mutex);
2636 gboolean (*mutex_trylock) (GMutex *mutex);
2637 void (*mutex_unlock) (GMutex *mutex);
2638 void (*mutex_free) (GMutex *mutex);
2639 GCond* (*cond_new) (void);
2640 void (*cond_signal) (GCond *cond);
2641 void (*cond_broadcast) (GCond *cond);
2642 void (*cond_wait) (GCond *cond,
2644 gboolean (*cond_timed_wait) (GCond *cond,
2646 GTimeVal *end_time);
2647 void (*cond_free) (GCond *cond);
2648 GPrivate* (*private_new) (GDestroyNotify destructor);
2649 gpointer (*private_get) (GPrivate *private_key);
2650 void (*private_set) (GPrivate *private_key,
2654 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
2655 GUTILS_C_VAR gboolean g_thread_use_default_impl;
2656 GUTILS_C_VAR gboolean g_threads_got_initialized;
2658 /* initializes the mutex/cond/private implementation for glib, might
2659 * only be called once, and must not be called directly or indirectly
2660 * from another glib-function, e.g. as a callback.
2662 void g_thread_init (GThreadFunctions *vtable);
2664 /* internal function for fallback static mutex implementation */
2665 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
2667 /* shorthands for conditional and unconditional function calls */
2668 #define G_THREAD_UF(name, arglist) \
2669 (*g_thread_functions_for_glib_use . name) arglist
2670 #define G_THREAD_CF(name, fail, arg) \
2671 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2672 /* keep in mind, all those mutexes and static mutexes are not
2673 * recursive in general, don't rely on that
2675 #define g_thread_supported() (g_threads_got_initialized)
2676 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
2677 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
2678 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
2679 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
2680 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
2681 #define g_cond_new() G_THREAD_UF (cond_new, ())
2682 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
2683 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
2684 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
2686 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
2687 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2691 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
2692 #define g_private_get(private_key) G_THREAD_CF (private_get, \
2693 ((gpointer)private_key), \
2695 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2696 (void) (private_key = \
2697 (GPrivate*) (value)), \
2698 (private_key, value))
2699 /* GStaticMutexes can be statically initialized with the value
2700 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2701 * much easier, than having to explicitly allocate the mutex before
2704 #define g_static_mutex_lock(mutex) \
2705 g_mutex_lock (g_static_mutex_get_mutex (mutex))
2706 #define g_static_mutex_trylock(mutex) \
2707 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2708 #define g_static_mutex_unlock(mutex) \
2709 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
2710 struct _GStaticPrivate
2714 #define G_STATIC_PRIVATE_INIT { 0 }
2715 gpointer g_static_private_get (GStaticPrivate *private_key);
2716 void g_static_private_set (GStaticPrivate *private_key,
2718 GDestroyNotify notify);
2720 /* these are some convenience macros that expand to nothing if GLib was
2721 * configured with --deisable-threads. for using StaticMutexes, you
2722 * declare them with G_LOCK_DECLARE_STATIC (name) or G_LOCK_DECLARE (name)
2723 * if you need to export the mutex. name is a unique identifier for the
2724 * protected varibale or code portion. locking, testing and unlocking of
2725 * such mutexes can be done with G_LOCK(), G_UNLOCK() and G_TRYLOCK()
2728 extern void glib_dummy_decl (void);
2729 #define G_LOCK_NAME(name) (g__ ## name ## _lock)
2730 #ifdef G_THREADS_ENABLED
2731 # define G_LOCK_DECLARE_STATIC(name) static G_LOCK_DECLARE (name)
2732 # define G_LOCK_DECLARE(name) \
2733 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
2735 # ifdef G_DEBUG_LOCKS
2736 # define G_LOCK(name) G_STMT_START{ \
2737 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2738 "file %s: line %d (%s): locking: %s ", \
2739 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2741 g_static_mutex_lock (G_LOCK_NAME (name)); \
2743 # define G_UNLOCK(name) G_STMT_START{ \
2744 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2745 "file %s: line %d (%s): unlocking: %s ", \
2746 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2748 g_static_mutex_unlock (G_LOCK_NAME (name)); \
2750 # define G_TRYLOCK(name) G_STMT_START{ \
2751 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2752 "file %s: line %d (%s): try locking: %s ", \
2753 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2755 }G_STMT_END, g_static_mutex_trylock (G_LOCK_NAME (name))
2756 # else /* !G_DEBUG_LOCKS */
2757 # define G_LOCK(name) g_static_mutex_lock (G_LOCK_NAME (name))
2758 # define G_UNLOCK(name) g_static_mutex_unlock (G_LOCK_NAME (name))
2759 # define G_TRYLOCK(name) g_static_mutex_trylock (G_LOCK_NAME (name))
2760 # endif /* !G_DEBUG_LOCKS */
2761 #else /* !G_THREADS_ENABLED */
2762 # define G_LOCK_DECLARE_STATIC(name) extern void glib_dummy_decl (void)
2763 # define G_LOCK_DECLARE(name) extern void glib_dummy_decl (void)
2764 # define G_LOCK(name)
2765 # define G_UNLOCK(name)
2766 # define G_TRYLOCK(name) (FALSE)
2767 #endif /* !G_THREADS_ENABLED */
2771 #endif /* __cplusplus */
2774 #endif /* __G_LIB_H__ */