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))))
312 #endif /* G_HAVE_ALLOCA */
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
1472 # define g_strdup_a(newstr,str) G_STMT_START { \
1474 const gchar *__old = (str); \
1477 size_t __len = strlen (__old) + 1; \
1478 __new = alloca (__len); \
1479 memcpy (__new, __old, __len); \
1485 # define g_strndup_a(newstr,str,n) G_STMT_START { \
1487 const gchar *__old = (str); \
1491 size_t __len = strlen (__old); \
1492 if (__len > (__n)) \
1494 __new = alloca (__len + 1); \
1495 memcpy (__new, __old, __len); \
1502 # define g_strconcat3_a(newstr,str1,str2,str3) G_STMT_START { \
1503 const gchar *__str1 = (str1); \
1504 const gchar *__str2 = (str2); \
1505 const gchar *__str3 = (str3); \
1508 size_t __len1 = strlen (__str1); \
1510 size_t __len2 = strlen (__str2); \
1512 size_t __len3 = strlen (__str3); \
1513 __new = alloca (__len1 + __len2 + __len3 + 1); \
1514 __new[__len1 + __len2 + __len3] = 0; \
1515 memcpy (__new + __len1 + __len2, __str3, __len3); \
1517 __new = alloca (__len1 + __len2 + 1); \
1518 __new[__len1 + __len2] = 0; \
1520 memcpy (__new + __len1, __str2, __len2); \
1522 __new = alloca (__len1 + 1); \
1523 __new[__len1] = 0; \
1525 memcpy (__new, __str1, __len1); \
1530 #endif /* G_HAVE_ALLOCA */
1533 /* NULL terminated string arrays.
1534 * g_strsplit() splits up string into max_tokens tokens at delim and
1535 * returns a newly allocated string array.
1536 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1537 * optional separator, the returned string is newly allocated.
1538 * g_strfreev() frees the array itself and all of its strings.
1540 gchar** g_strsplit (const gchar *string,
1541 const gchar *delimiter,
1543 gchar* g_strjoinv (const gchar *separator,
1545 void g_strfreev (gchar **str_array);
1549 /* calculate a string size, guarranteed to fit format + args.
1551 guint g_printf_string_upper_bound (const gchar* format,
1555 /* Retrive static string info
1557 gchar* g_get_user_name (void);
1558 gchar* g_get_real_name (void);
1559 gchar* g_get_home_dir (void);
1560 gchar* g_get_tmp_dir (void);
1561 gchar* g_get_prgname (void);
1562 void g_set_prgname (const gchar *prgname);
1565 /* Miscellaneous utility functions
1567 guint g_parse_debug_string (const gchar *string,
1570 gint g_snprintf (gchar *string,
1572 gchar const *format,
1573 ...) G_GNUC_PRINTF (3, 4);
1574 gint g_vsnprintf (gchar *string,
1576 gchar const *format,
1578 gchar* g_basename (const gchar *file_name);
1579 /* Check if a file name is an absolute path */
1580 gboolean g_path_is_absolute (const gchar *file_name);
1581 /* In case of absolute paths, skip the root part */
1582 gchar* g_path_skip_root (gchar *file_name);
1584 /* strings are newly allocated with g_malloc() */
1585 gchar* g_dirname (const gchar *file_name);
1586 gchar* g_get_current_dir (void);
1587 gchar* g_getenv (const gchar *variable);
1590 /* we use a GLib function as a replacement for ATEXIT, so
1591 * the programmer is not required to check the return value
1592 * (if there is any in the implementation) and doesn't encounter
1593 * missing include files.
1595 void g_atexit (GVoidFunc func);
1600 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1604 g_bit_nth_lsf (guint32 mask,
1610 if (mask & (1 << (guint) nth_bit))
1613 while (nth_bit < 32);
1616 #endif /* G_CAN_INLINE */
1618 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1622 g_bit_nth_msf (guint32 mask,
1630 if (mask & (1 << (guint) nth_bit))
1633 while (nth_bit > 0);
1636 #endif /* G_CAN_INLINE */
1638 G_INLINE_FUNC guint g_bit_storage (guint number);
1641 g_bit_storage (guint number)
1643 register guint n_bits = 0;
1653 #endif /* G_CAN_INLINE */
1657 GStringChunk* g_string_chunk_new (gint size);
1658 void g_string_chunk_free (GStringChunk *chunk);
1659 gchar* g_string_chunk_insert (GStringChunk *chunk,
1660 const gchar *string);
1661 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1662 const gchar *string);
1667 GString* g_string_new (const gchar *init);
1668 GString* g_string_sized_new (guint dfl_size);
1669 void g_string_free (GString *string,
1671 GString* g_string_assign (GString *lval,
1673 GString* g_string_truncate (GString *string,
1675 GString* g_string_append (GString *string,
1677 GString* g_string_append_c (GString *string,
1679 GString* g_string_prepend (GString *string,
1681 GString* g_string_prepend_c (GString *string,
1683 GString* g_string_insert (GString *string,
1686 GString* g_string_insert_c (GString *string,
1689 GString* g_string_erase (GString *string,
1692 GString* g_string_down (GString *string);
1693 GString* g_string_up (GString *string);
1694 void g_string_sprintf (GString *string,
1695 const gchar *format,
1696 ...) G_GNUC_PRINTF (2, 3);
1697 void g_string_sprintfa (GString *string,
1698 const gchar *format,
1699 ...) G_GNUC_PRINTF (2, 3);
1702 /* Resizable arrays, remove fills any cleared spot and shortens the
1703 * array, while preserving the order. remove_fast will distort the
1704 * order by moving the last element to the position of the removed
1707 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1708 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1709 #define g_array_insert_val(a,i,v) g_array_insert_vals(a,i,&v,1)
1710 #define g_array_index(a,t,i) (((t*)a->data)[i])
1712 GArray* g_array_new (gboolean zero_terminated,
1714 guint element_size);
1715 void g_array_free (GArray *array,
1716 gboolean free_segment);
1717 GArray* g_array_append_vals (GArray *array,
1720 GArray* g_array_prepend_vals (GArray *array,
1723 GArray* g_array_insert_vals (GArray *array,
1727 GArray* g_array_set_size (GArray *array,
1729 GArray* g_array_remove_index (GArray *array,
1731 GArray* g_array_remove_index_fast (GArray *array,
1734 /* Resizable pointer array. This interface is much less complicated
1735 * than the above. Add appends appends a pointer. Remove fills any
1736 * cleared spot and shortens the array. remove_fast will again distort
1739 #define g_ptr_array_index(array,index) (array->pdata)[index]
1740 GPtrArray* g_ptr_array_new (void);
1741 void g_ptr_array_free (GPtrArray *array,
1743 void g_ptr_array_set_size (GPtrArray *array,
1745 gpointer g_ptr_array_remove_index (GPtrArray *array,
1747 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1749 gboolean g_ptr_array_remove (GPtrArray *array,
1751 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1753 void g_ptr_array_add (GPtrArray *array,
1756 /* Byte arrays, an array of guint8. Implemented as a GArray,
1760 GByteArray* g_byte_array_new (void);
1761 void g_byte_array_free (GByteArray *array,
1762 gboolean free_segment);
1763 GByteArray* g_byte_array_append (GByteArray *array,
1766 GByteArray* g_byte_array_prepend (GByteArray *array,
1769 GByteArray* g_byte_array_set_size (GByteArray *array,
1771 GByteArray* g_byte_array_remove_index (GByteArray *array,
1773 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1779 gint g_str_equal (gconstpointer v,
1781 guint g_str_hash (gconstpointer v);
1783 gint g_int_equal (gconstpointer v,
1785 guint g_int_hash (gconstpointer v);
1787 /* This "hash" function will just return the key's adress as an
1788 * unsigned integer. Useful for hashing on plain adresses or
1789 * simple integer values.
1791 guint g_direct_hash (gconstpointer v);
1792 gint g_direct_equal (gconstpointer v,
1796 /* Quarks (string<->id association)
1798 GQuark g_quark_try_string (const gchar *string);
1799 GQuark g_quark_from_static_string (const gchar *string);
1800 GQuark g_quark_from_string (const gchar *string);
1801 gchar* g_quark_to_string (GQuark quark);
1806 void g_datalist_init (GData **datalist);
1807 void g_datalist_clear (GData **datalist);
1808 gpointer g_datalist_id_get_data (GData **datalist,
1810 void g_datalist_id_set_data_full (GData **datalist,
1813 GDestroyNotify destroy_func);
1814 void g_datalist_id_remove_no_notify (GData **datalist,
1816 void g_datalist_foreach (GData **datalist,
1817 GDataForeachFunc func,
1818 gpointer user_data);
1819 #define g_datalist_id_set_data(dl, q, d) \
1820 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1821 #define g_datalist_id_remove_data(dl, q) \
1822 g_datalist_id_set_data ((dl), (q), NULL)
1823 #define g_datalist_get_data(dl, k) \
1824 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1825 #define g_datalist_set_data_full(dl, k, d, f) \
1826 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1827 #define g_datalist_remove_no_notify(dl, k) \
1828 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1829 #define g_datalist_set_data(dl, k, d) \
1830 g_datalist_set_data_full ((dl), (k), (d), NULL)
1831 #define g_datalist_remove_data(dl, k) \
1832 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1835 /* Location Associated Keyed Data
1837 void g_dataset_destroy (gconstpointer dataset_location);
1838 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1840 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1843 GDestroyNotify destroy_func);
1844 void g_dataset_id_remove_no_notify (gconstpointer dataset_location,
1846 void g_dataset_foreach (gconstpointer dataset_location,
1847 GDataForeachFunc func,
1848 gpointer user_data);
1849 #define g_dataset_id_set_data(l, k, d) \
1850 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1851 #define g_dataset_id_remove_data(l, k) \
1852 g_dataset_id_set_data ((l), (k), NULL)
1853 #define g_dataset_get_data(l, k) \
1854 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1855 #define g_dataset_set_data_full(l, k, d, f) \
1856 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1857 #define g_dataset_remove_no_notify(l, k) \
1858 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1859 #define g_dataset_set_data(l, k, d) \
1860 g_dataset_set_data_full ((l), (k), (d), NULL)
1861 #define g_dataset_remove_data(l, k) \
1862 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1865 /* GScanner: Flexible lexical scanner for general purpose.
1868 /* Character sets */
1869 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1870 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1871 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1872 "\307\310\311\312\313\314\315\316\317\320"\
1873 "\321\322\323\324\325\326"\
1874 "\330\331\332\333\334\335\336"
1875 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1876 "\347\350\351\352\353\354\355\356\357\360"\
1877 "\361\362\363\364\365\366"\
1878 "\370\371\372\373\374\375\376\377"
1885 G_ERR_UNEXP_EOF_IN_STRING,
1886 G_ERR_UNEXP_EOF_IN_COMMENT,
1887 G_ERR_NON_DIGIT_IN_CONST,
1890 G_ERR_FLOAT_MALFORMED
1898 G_TOKEN_LEFT_PAREN = '(',
1899 G_TOKEN_RIGHT_PAREN = ')',
1900 G_TOKEN_LEFT_CURLY = '{',
1901 G_TOKEN_RIGHT_CURLY = '}',
1902 G_TOKEN_LEFT_BRACE = '[',
1903 G_TOKEN_RIGHT_BRACE = ']',
1904 G_TOKEN_EQUAL_SIGN = '=',
1905 G_TOKEN_COMMA = ',',
1921 G_TOKEN_IDENTIFIER_NULL,
1923 G_TOKEN_COMMENT_SINGLE,
1924 G_TOKEN_COMMENT_MULTI,
1931 gchar *v_identifier;
1943 struct _GScannerConfig
1947 gchar *cset_skip_characters; /* default: " \t\n" */
1948 gchar *cset_identifier_first;
1949 gchar *cset_identifier_nth;
1950 gchar *cpair_comment_single; /* default: "#\n" */
1952 /* Should symbol lookup work case sensitive?
1954 guint case_sensitive : 1;
1956 /* Boolean values to be adjusted "on the fly"
1957 * to configure scanning behaviour.
1959 guint skip_comment_multi : 1; /* C like comment */
1960 guint skip_comment_single : 1; /* single line comment */
1961 guint scan_comment_multi : 1; /* scan multi line comments? */
1962 guint scan_identifier : 1;
1963 guint scan_identifier_1char : 1;
1964 guint scan_identifier_NULL : 1;
1965 guint scan_symbols : 1;
1966 guint scan_binary : 1;
1967 guint scan_octal : 1;
1968 guint scan_float : 1;
1969 guint scan_hex : 1; /* `0x0ff0' */
1970 guint scan_hex_dollar : 1; /* `$0ff0' */
1971 guint scan_string_sq : 1; /* string: 'anything' */
1972 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
1973 guint numbers_2_int : 1; /* bin, octal, hex => int */
1974 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
1975 guint identifier_2_string : 1;
1976 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
1977 guint symbol_2_token : 1;
1978 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
1985 guint max_parse_errors;
1987 /* g_scanner_error() increments this field */
1990 /* name of input stream, featured by the default message handler */
1991 const gchar *input_name;
1993 /* data pointer for derived structures */
1994 gpointer derived_data;
1996 /* link into the scanner configuration */
1997 GScannerConfig *config;
1999 /* fields filled in after g_scanner_get_next_token() */
2005 /* fields filled in after g_scanner_peek_next_token() */
2006 GTokenType next_token;
2007 GTokenValue next_value;
2009 guint next_position;
2011 /* to be considered private */
2012 GHashTable *symbol_table;
2015 const gchar *text_end;
2019 /* handler function for _warn and _error */
2020 GScannerMsgFunc msg_handler;
2023 GScanner* g_scanner_new (GScannerConfig *config_templ);
2024 void g_scanner_destroy (GScanner *scanner);
2025 void g_scanner_input_file (GScanner *scanner,
2027 void g_scanner_sync_file_offset (GScanner *scanner);
2028 void g_scanner_input_text (GScanner *scanner,
2031 GTokenType g_scanner_get_next_token (GScanner *scanner);
2032 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2033 GTokenType g_scanner_cur_token (GScanner *scanner);
2034 GTokenValue g_scanner_cur_value (GScanner *scanner);
2035 guint g_scanner_cur_line (GScanner *scanner);
2036 guint g_scanner_cur_position (GScanner *scanner);
2037 gboolean g_scanner_eof (GScanner *scanner);
2038 guint g_scanner_set_scope (GScanner *scanner,
2040 void g_scanner_scope_add_symbol (GScanner *scanner,
2042 const gchar *symbol,
2044 void g_scanner_scope_remove_symbol (GScanner *scanner,
2046 const gchar *symbol);
2047 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2049 const gchar *symbol);
2050 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2053 gpointer user_data);
2054 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2055 const gchar *symbol);
2056 void g_scanner_freeze_symbol_table (GScanner *scanner);
2057 void g_scanner_thaw_symbol_table (GScanner *scanner);
2058 void g_scanner_unexp_token (GScanner *scanner,
2059 GTokenType expected_token,
2060 const gchar *identifier_spec,
2061 const gchar *symbol_spec,
2062 const gchar *symbol_name,
2063 const gchar *message,
2065 void g_scanner_error (GScanner *scanner,
2066 const gchar *format,
2067 ...) G_GNUC_PRINTF (2,3);
2068 void g_scanner_warn (GScanner *scanner,
2069 const gchar *format,
2070 ...) G_GNUC_PRINTF (2,3);
2071 gint g_scanner_stat_mode (const gchar *filename);
2072 /* keep downward source compatibility */
2073 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2074 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2076 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2077 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2079 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2080 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2090 GCompletionFunc func;
2096 GCompletion* g_completion_new (GCompletionFunc func);
2097 void g_completion_add_items (GCompletion* cmp,
2099 void g_completion_remove_items (GCompletion* cmp,
2101 void g_completion_clear_items (GCompletion* cmp);
2102 GList* g_completion_complete (GCompletion* cmp,
2104 gchar** new_prefix);
2105 void g_completion_free (GCompletion* cmp);
2110 * Date calculations (not time for now, to be resolved). These are a
2111 * mutant combination of Steffen Beyer's DateCalc routines
2112 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2113 * date routines (written for in-house software). Written by Havoc
2114 * Pennington <hp@pobox.com>
2117 typedef guint16 GDateYear;
2118 typedef guint8 GDateDay; /* day of the month */
2119 typedef struct _GDate GDate;
2120 /* make struct tm known without having to include time.h */
2123 /* enum used to specify order of appearance in parsed date strings */
2131 /* actual week and month values */
2134 G_DATE_BAD_WEEKDAY = 0,
2137 G_DATE_WEDNESDAY = 3,
2138 G_DATE_THURSDAY = 4,
2140 G_DATE_SATURDAY = 6,
2145 G_DATE_BAD_MONTH = 0,
2147 G_DATE_FEBRUARY = 2,
2154 G_DATE_SEPTEMBER = 9,
2155 G_DATE_OCTOBER = 10,
2156 G_DATE_NOVEMBER = 11,
2157 G_DATE_DECEMBER = 12
2160 #define G_DATE_BAD_JULIAN 0U
2161 #define G_DATE_BAD_DAY 0U
2162 #define G_DATE_BAD_YEAR 0U
2164 /* Note: directly manipulating structs is generally a bad idea, but
2165 * in this case it's an *incredibly* bad idea, because all or part
2166 * of this struct can be invalid at any given time. Use the functions,
2167 * or you will get hosed, I promise.
2171 guint julian_days : 32; /* julian days representation - we use a
2172 * bitfield hoping that 64 bit platforms
2173 * will pack this whole struct in one big
2177 guint julian : 1; /* julian is valid */
2178 guint dmy : 1; /* dmy is valid */
2180 /* DMY representation */
2186 /* g_date_new() returns an invalid date, you then have to _set() stuff
2187 * to get a usable object. You can also allocate a GDate statically,
2188 * then call g_date_clear() to initialize.
2190 GDate* g_date_new (void);
2191 GDate* g_date_new_dmy (GDateDay day,
2194 GDate* g_date_new_julian (guint32 julian_day);
2195 void g_date_free (GDate *date);
2197 /* check g_date_valid() after doing an operation that might fail, like
2198 * _parse. Almost all g_date operations are undefined on invalid
2199 * dates (the exceptions are the mutators, since you need those to
2200 * return to validity).
2202 gboolean g_date_valid (GDate *date);
2203 gboolean g_date_valid_day (GDateDay day);
2204 gboolean g_date_valid_month (GDateMonth month);
2205 gboolean g_date_valid_year (GDateYear year);
2206 gboolean g_date_valid_weekday (GDateWeekday weekday);
2207 gboolean g_date_valid_julian (guint32 julian_date);
2208 gboolean g_date_valid_dmy (GDateDay day,
2212 GDateWeekday g_date_weekday (GDate *date);
2213 GDateMonth g_date_month (GDate *date);
2214 GDateYear g_date_year (GDate *date);
2215 GDateDay g_date_day (GDate *date);
2216 guint32 g_date_julian (GDate *date);
2217 guint g_date_day_of_year (GDate *date);
2219 /* First monday/sunday is the start of week 1; if we haven't reached
2220 * that day, return 0. These are not ISO weeks of the year; that
2221 * routine needs to be added.
2222 * these functions return the number of weeks, starting on the
2225 guint g_date_monday_week_of_year (GDate *date);
2226 guint g_date_sunday_week_of_year (GDate *date);
2228 /* If you create a static date struct you need to clear it to get it
2229 * in a sane state before use. You can clear a whole array at
2230 * once with the ndates argument.
2232 void g_date_clear (GDate *date,
2235 /* The parse routine is meant for dates typed in by a user, so it
2236 * permits many formats but tries to catch common typos. If your data
2237 * needs to be strictly validated, it is not an appropriate function.
2239 void g_date_set_parse (GDate *date,
2241 void g_date_set_time (GDate *date,
2243 void g_date_set_month (GDate *date,
2245 void g_date_set_day (GDate *date,
2247 void g_date_set_year (GDate *date,
2249 void g_date_set_dmy (GDate *date,
2253 void g_date_set_julian (GDate *date,
2254 guint32 julian_date);
2255 gboolean g_date_is_first_of_month (GDate *date);
2256 gboolean g_date_is_last_of_month (GDate *date);
2258 /* To go forward by some number of weeks just go forward weeks*7 days */
2259 void g_date_add_days (GDate *date,
2261 void g_date_subtract_days (GDate *date,
2264 /* If you add/sub months while day > 28, the day might change */
2265 void g_date_add_months (GDate *date,
2267 void g_date_subtract_months (GDate *date,
2270 /* If it's feb 29, changing years can move you to the 28th */
2271 void g_date_add_years (GDate *date,
2273 void g_date_subtract_years (GDate *date,
2275 gboolean g_date_is_leap_year (GDateYear year);
2276 guint8 g_date_days_in_month (GDateMonth month,
2278 guint8 g_date_monday_weeks_in_year (GDateYear year);
2279 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2281 /* qsort-friendly (with a cast...) */
2282 gint g_date_compare (GDate *lhs,
2284 void g_date_to_struct_tm (GDate *date,
2287 /* Just like strftime() except you can only use date-related formats.
2288 * Using a time format is undefined.
2290 gsize g_date_strftime (gchar *s,
2292 const gchar *format,
2298 * Indexed Relations. Imagine a really simple table in a
2299 * database. Relations are not ordered. This data type is meant for
2300 * maintaining a N-way mapping.
2302 * g_relation_new() creates a relation with FIELDS fields
2304 * g_relation_destroy() frees all resources
2305 * g_tuples_destroy() frees the result of g_relation_select()
2307 * g_relation_index() indexes relation FIELD with the provided
2308 * equality and hash functions. this must be done before any
2309 * calls to insert are made.
2311 * g_relation_insert() inserts a new tuple. you are expected to
2312 * provide the right number of fields.
2314 * g_relation_delete() deletes all relations with KEY in FIELD
2315 * g_relation_select() returns ...
2316 * g_relation_count() counts ...
2319 GRelation* g_relation_new (gint fields);
2320 void g_relation_destroy (GRelation *relation);
2321 void g_relation_index (GRelation *relation,
2323 GHashFunc hash_func,
2324 GCompareFunc key_compare_func);
2325 void g_relation_insert (GRelation *relation,
2327 gint g_relation_delete (GRelation *relation,
2330 GTuples* g_relation_select (GRelation *relation,
2333 gint g_relation_count (GRelation *relation,
2336 gboolean g_relation_exists (GRelation *relation,
2338 void g_relation_print (GRelation *relation);
2340 void g_tuples_destroy (GTuples *tuples);
2341 gpointer g_tuples_index (GTuples *tuples,
2349 /* This function returns prime numbers spaced by approximately 1.5-2.0
2350 * and is for use in resizing data structures which prefer
2351 * prime-valued sizes. The closest spaced prime function returns the
2352 * next largest prime, or the highest it knows about which is about
2355 guint g_spaced_primes_closest (guint num);
2361 typedef struct _GIOFuncs GIOFuncs;
2377 G_IO_IN GLIB_SYSDEF_POLLIN,
2378 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2379 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2380 G_IO_ERR GLIB_SYSDEF_POLLERR,
2381 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2382 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2387 guint channel_flags;
2392 typedef gboolean (*GIOFunc) (GIOChannel *source,
2393 GIOCondition condition,
2397 GIOError (*io_read) (GIOChannel *channel,
2401 GIOError (*io_write) (GIOChannel *channel,
2404 guint *bytes_written);
2405 GIOError (*io_seek) (GIOChannel *channel,
2408 void (*io_close) (GIOChannel *channel);
2409 guint (*io_add_watch) (GIOChannel *channel,
2411 GIOCondition condition,
2414 GDestroyNotify notify);
2415 void (*io_free) (GIOChannel *channel);
2418 void g_io_channel_init (GIOChannel *channel);
2419 void g_io_channel_ref (GIOChannel *channel);
2420 void g_io_channel_unref (GIOChannel *channel);
2421 GIOError g_io_channel_read (GIOChannel *channel,
2425 GIOError g_io_channel_write (GIOChannel *channel,
2428 guint *bytes_written);
2429 GIOError g_io_channel_seek (GIOChannel *channel,
2432 void g_io_channel_close (GIOChannel *channel);
2433 guint g_io_add_watch_full (GIOChannel *channel,
2435 GIOCondition condition,
2438 GDestroyNotify notify);
2439 guint g_io_add_watch (GIOChannel *channel,
2440 GIOCondition condition,
2442 gpointer user_data);
2447 typedef struct _GTimeVal GTimeVal;
2448 typedef struct _GSourceFuncs GSourceFuncs;
2449 typedef struct _GMainLoop GMainLoop; /* Opaque */
2456 struct _GSourceFuncs
2458 gboolean (*prepare) (gpointer source_data,
2459 GTimeVal *current_time,
2461 gboolean (*check) (gpointer source_data,
2462 GTimeVal *current_time);
2463 gboolean (*dispatch) (gpointer source_data,
2464 GTimeVal *current_time,
2465 gpointer user_data);
2466 GDestroyNotify destroy;
2469 /* Standard priorities */
2471 #define G_PRIORITY_HIGH -100
2472 #define G_PRIORITY_DEFAULT 0
2473 #define G_PRIORITY_HIGH_IDLE 100
2474 #define G_PRIORITY_DEFAULT_IDLE 200
2475 #define G_PRIORITY_LOW 300
2477 typedef gboolean (*GSourceFunc) (gpointer data);
2479 /* Hooks for adding to the main loop */
2480 guint g_source_add (gint priority,
2481 gboolean can_recurse,
2482 GSourceFuncs *funcs,
2483 gpointer source_data,
2485 GDestroyNotify notify);
2486 void g_source_remove (guint tag);
2487 void g_source_remove_by_user_data (gpointer user_data);
2488 void g_source_remove_by_source_data (gpointer source_data);
2490 void g_get_current_time (GTimeVal *result);
2492 /* Running the main loop */
2493 GMainLoop* g_main_new (gboolean is_running);
2494 void g_main_run (GMainLoop *loop);
2495 void g_main_quit (GMainLoop *loop);
2496 void g_main_destroy (GMainLoop *loop);
2497 gboolean g_main_is_running (GMainLoop *loop);
2499 /* Run a single iteration of the mainloop. If block is FALSE,
2502 gboolean g_main_iteration (gboolean may_block);
2504 /* See if any events are pending */
2505 gboolean g_main_pending (void);
2507 /* Idles and timeouts */
2508 guint g_timeout_add_full (gint priority,
2510 GSourceFunc function,
2512 GDestroyNotify notify);
2513 guint g_timeout_add (guint interval,
2514 GSourceFunc function,
2516 guint g_idle_add (GSourceFunc function,
2518 guint g_idle_add_full (gint priority,
2519 GSourceFunc function,
2521 GDestroyNotify destroy);
2525 * Unix-specific IO and main loop calls
2528 typedef struct _GPollFD GPollFD;
2529 typedef gint (*GPollFunc) (GPollFD *ufds,
2539 void g_main_add_poll (GPollFD *fd,
2541 void g_main_remove_poll (GPollFD *fd);
2542 void g_main_set_poll_func (GPollFunc func);
2544 GIOChannel* g_io_channel_unix_new (int fd);
2545 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2548 /* old IO Channels */
2551 * These are used for plug-in communication in the GIMP, for instance.
2552 * On Unix, it's simply an encapsulated file descriptor (a pipe).
2553 * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2554 * of the writable end) a thread id to post a message to when you have written
2559 gint fd; /* file handle (pseudo such in Win32) */
2561 guint peer; /* thread to post message to */
2562 guint peer_fd; /* read handle (in the other process) */
2563 guint offset; /* counter of accumulated bytes, to
2564 * be included in the message posted
2565 * so we keep in sync.
2567 guint need_wakeups; /* in output channels whether the reader
2572 GIOChannel *g_iochannel_new (gint fd);
2573 void g_iochannel_free (GIOChannel *channel);
2574 void g_iochannel_close_and_free (GIOChannel *channel);
2575 void g_iochannel_wakeup_peer (GIOChannel *channel);
2576 #ifndef NATIVE_WIN32
2577 # define g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2579 #endif /* old IO Channels */
2582 /* Windows emulation stubs for common unix functions
2585 # define MAXPATHLEN 1024
2589 /* These POSIXish functions are available in the Microsoft C library
2590 * prefixed with underscore (which of course technically speaking is
2591 * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2592 * prevents Microsoft from practically requiring you to include
2593 * <windows.h> every now and then...).
2595 * You still need to include the appropriate headers to get the
2596 * prototypes, <io.h> or <direct.h>.
2598 * For some functions, we provide emulators in glib, which are prefixed
2601 # define getcwd _getcwd
2602 # define getpid _getpid
2603 # define access _access
2606 # define write _write
2607 # define lseek _lseek
2608 # define close _close
2609 # define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2610 # define popen _popen
2611 # define pclose _pclose
2612 # define fdopen _fdopen
2613 # define ftruncate(fd, size) gwin_ftruncate (fd, size)
2614 # define opendir gwin_opendir
2615 # define readdir gwin_readdir
2616 # define rewinddir gwin_rewinddir
2617 # define closedir gwin_closedir
2618 # define NAME_MAX 255
2622 gboolean just_opened;
2623 guint find_file_handle;
2624 gpointer find_file_data;
2626 typedef struct DIR DIR;
2629 gchar d_name[NAME_MAX + 1];
2631 /* emulation functions */
2632 extern int gwin_ftruncate (gint f,
2634 DIR* gwin_opendir (const gchar *dirname);
2635 struct dirent* gwin_readdir (DIR *dir);
2636 void gwin_rewinddir (DIR *dir);
2637 gint gwin_closedir (DIR *dir);
2638 # endif /* _MSC_VER */
2639 #endif /* NATIVE_WIN32 */
2642 /* GLib Thread support
2644 typedef struct _GMutex GMutex;
2645 typedef struct _GCond GCond;
2646 typedef struct _GPrivate GPrivate;
2647 typedef struct _GStaticPrivate GStaticPrivate;
2648 typedef struct _GThreadFunctions GThreadFunctions;
2649 struct _GThreadFunctions
2651 GMutex* (*mutex_new) (void);
2652 void (*mutex_lock) (GMutex *mutex);
2653 gboolean (*mutex_trylock) (GMutex *mutex);
2654 void (*mutex_unlock) (GMutex *mutex);
2655 void (*mutex_free) (GMutex *mutex);
2656 GCond* (*cond_new) (void);
2657 void (*cond_signal) (GCond *cond);
2658 void (*cond_broadcast) (GCond *cond);
2659 void (*cond_wait) (GCond *cond,
2661 gboolean (*cond_timed_wait) (GCond *cond,
2663 GTimeVal *end_time);
2664 void (*cond_free) (GCond *cond);
2665 GPrivate* (*private_new) (GDestroyNotify destructor);
2666 gpointer (*private_get) (GPrivate *private_key);
2667 void (*private_set) (GPrivate *private_key,
2671 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
2672 GUTILS_C_VAR gboolean g_thread_use_default_impl;
2673 GUTILS_C_VAR gboolean g_threads_got_initialized;
2675 /* initializes the mutex/cond/private implementation for glib, might
2676 * only be called once, and must not be called directly or indirectly
2677 * from another glib-function, e.g. as a callback.
2679 void g_thread_init (GThreadFunctions *vtable);
2681 /* internal function for fallback static mutex implementation */
2682 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
2684 /* shorthands for conditional and unconditional function calls */
2685 #define G_THREAD_UF(name, arglist) \
2686 (*g_thread_functions_for_glib_use . name) arglist
2687 #define G_THREAD_CF(name, fail, arg) \
2688 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2689 /* keep in mind, all those mutexes and static mutexes are not
2690 * recursive in general, don't rely on that
2692 #define g_thread_supported() (g_threads_got_initialized)
2693 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
2694 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
2695 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
2696 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
2697 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
2698 #define g_cond_new() G_THREAD_UF (cond_new, ())
2699 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
2700 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
2701 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
2703 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
2704 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2708 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
2709 #define g_private_get(private_key) G_THREAD_CF (private_get, \
2710 ((gpointer)private_key), \
2712 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2713 (void) (private_key = \
2714 (GPrivate*) (value)), \
2715 (private_key, value))
2716 /* GStaticMutexes can be statically initialized with the value
2717 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2718 * much easier, than having to explicitly allocate the mutex before
2721 #define g_static_mutex_lock(mutex) \
2722 g_mutex_lock (g_static_mutex_get_mutex (mutex))
2723 #define g_static_mutex_trylock(mutex) \
2724 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2725 #define g_static_mutex_unlock(mutex) \
2726 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
2727 struct _GStaticPrivate
2731 #define G_STATIC_PRIVATE_INIT { 0 }
2732 gpointer g_static_private_get (GStaticPrivate *private_key);
2733 void g_static_private_set (GStaticPrivate *private_key,
2735 GDestroyNotify notify);
2737 /* these are some convenience macros that expand to nothing if GLib was
2738 * configured with --deisable-threads. for using StaticMutexes, you
2739 * declare them with G_LOCK_DECLARE_STATIC (name) or G_LOCK_DECLARE (name)
2740 * if you need to export the mutex. name is a unique identifier for the
2741 * protected varibale or code portion. locking, testing and unlocking of
2742 * such mutexes can be done with G_LOCK(), G_UNLOCK() and G_TRYLOCK()
2745 extern void glib_dummy_decl (void);
2746 #define G_LOCK_NAME(name) (g__ ## name ## _lock)
2747 #ifdef G_THREADS_ENABLED
2748 # define G_LOCK_DECLARE_STATIC(name) static G_LOCK_DECLARE (name)
2749 # define G_LOCK_DECLARE(name) \
2750 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
2752 # ifdef G_DEBUG_LOCKS
2753 # define G_LOCK(name) G_STMT_START{ \
2754 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2755 "file %s: line %d (%s): locking: %s ", \
2756 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2758 g_static_mutex_lock (G_LOCK_NAME (name)); \
2760 # define G_UNLOCK(name) G_STMT_START{ \
2761 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2762 "file %s: line %d (%s): unlocking: %s ", \
2763 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2765 g_static_mutex_unlock (G_LOCK_NAME (name)); \
2767 # define G_TRYLOCK(name) G_STMT_START{ \
2768 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2769 "file %s: line %d (%s): try locking: %s ", \
2770 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2772 }G_STMT_END, g_static_mutex_trylock (G_LOCK_NAME (name))
2773 # else /* !G_DEBUG_LOCKS */
2774 # define G_LOCK(name) g_static_mutex_lock (G_LOCK_NAME (name))
2775 # define G_UNLOCK(name) g_static_mutex_unlock (G_LOCK_NAME (name))
2776 # define G_TRYLOCK(name) g_static_mutex_trylock (G_LOCK_NAME (name))
2777 # endif /* !G_DEBUG_LOCKS */
2778 #else /* !G_THREADS_ENABLED */
2779 # define G_LOCK_DECLARE_STATIC(name) extern void glib_dummy_decl (void)
2780 # define G_LOCK_DECLARE(name) extern void glib_dummy_decl (void)
2781 # define G_LOCK(name)
2782 # define G_UNLOCK(name)
2783 # define G_TRYLOCK(name) (FALSE)
2784 #endif /* !G_THREADS_ENABLED */
2788 #endif /* __cplusplus */
2791 #endif /* __G_LIB_H__ */