1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 /* system specific config file glibconfig.h provides definitions for
31 * the extrema of many of the standard types. These are:
33 * G_MINSHORT, G_MAXSHORT
35 * G_MINLONG, G_MAXLONG
36 * G_MINFLOAT, G_MAXFLOAT
37 * G_MINDOUBLE, G_MAXDOUBLE
39 * It also provides the following typedefs:
46 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
49 * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
50 * This is useful to pass an integer instead of a pointer to a callback.
52 * GINT_TO_POINTER(i), GUINT_TO_POINTER(i)
53 * GPOINTER_TO_INT(p), GPOINTER_TO_UINT(p)
55 * Finally, it provide the following wrappers to STDC functions:
58 * To register hooks which are executed on exit().
59 * Usually a wrapper for STDC atexit.
61 * void *g_memmove(void *dest, const void *src, guint count);
62 * A wrapper for STDC memmove, or an implementation, if memmove doesn't
63 * exist. The prototype looks like the above, give or take a const,
66 #include <glibconfig.h>
68 /* include varargs functions for assertment macros
72 /* optionally feature DMALLOC memory allocation debugger
81 /* On native Win32, directory separator is the backslash, and search path
82 * separator is the semicolon.
84 #define G_DIR_SEPARATOR '\\'
85 #define G_DIR_SEPARATOR_S "\\"
86 #define G_SEARCHPATH_SEPARATOR ';'
87 #define G_SEARCHPATH_SEPARATOR_S ";"
89 #else /* !NATIVE_WIN32 */
93 #define G_DIR_SEPARATOR '/'
94 #define G_DIR_SEPARATOR_S "/"
95 #define G_SEARCHPATH_SEPARATOR ':'
96 #define G_SEARCHPATH_SEPARATOR_S ":"
98 #endif /* !NATIVE_WIN32 */
102 #endif /* __cplusplus */
105 /* Provide definitions for some commonly used macros.
106 * Some of them are only provided if they haven't already
107 * been defined. It is assumed that if they are already
108 * defined then the current definition is correct.
111 #define NULL ((void*) 0)
119 #define TRUE (!FALSE)
123 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
126 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
129 #define ABS(a) (((a) < 0) ? -(a) : (a))
132 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
135 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
136 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
138 #if !defined (G_VA_COPY)
139 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
140 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
141 # elif defined (G_VA_COPY_AS_ARRAY)
142 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
143 # else /* va_list is a pointer */
144 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
145 # endif /* va_list is a pointer */
146 #endif /* !G_VA_COPY */
149 /* Provide convenience macros for handling structure
150 * fields through their offsets.
152 #define G_STRUCT_OFFSET(struct_type, member) \
153 ((gulong) ((gchar*) &((struct_type*) 0)->member))
154 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
155 ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
156 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
157 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
160 /* inlining hassle. for compilers that don't allow the `inline' keyword,
161 * mostly because of strict ANSI C compliance or dumbness, we try to fall
162 * back to either `__inline__' or `__inline'.
163 * we define G_CAN_INLINE, if the compiler seems to be actually
164 * *capable* to do function inlining, in which case inline function bodys
165 * do make sense. we also define G_INLINE_FUNC to properly export the
166 * function prototypes if no inlining can be performed.
167 * we special case most of the stuff, so inline functions can have a normal
168 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
170 #ifndef G_INLINE_FUNC
171 # define G_CAN_INLINE 1
174 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
176 # define inline __inline__
178 #else /* !G_HAVE_INLINE */
180 # if defined (G_HAVE___INLINE__)
181 # define inline __inline__
182 # else /* !inline && !__inline__ */
183 # if defined (G_HAVE___INLINE)
184 # define inline __inline
185 # else /* !inline && !__inline__ && !__inline */
186 # define inline /* don't inline, then */
187 # ifndef G_INLINE_FUNC
193 #ifndef G_INLINE_FUNC
196 # define G_INLINE_FUNC extern inline
199 # define G_INLINE_FUNC extern
201 # else /* !__GNUC__ */
203 # define G_INLINE_FUNC static inline
205 # define G_INLINE_FUNC extern
207 # endif /* !__GNUC__ */
208 #endif /* !G_INLINE_FUNC */
211 /* Provide simple macro statement wrappers (adapted from Perl):
212 * G_STMT_START { statements; } G_STMT_END;
213 * can be used as a single statement, as in
214 * if (x) G_STMT_START { ... } G_STMT_END; else ...
216 * For gcc we will wrap the statements within `({' and `})' braces.
217 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
218 * and otherwise within `do' and `while (0)'.
220 #if !(defined (G_STMT_START) && defined (G_STMT_END))
221 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
222 # define G_STMT_START (void)(
223 # define G_STMT_END )
225 # if (defined (sun) || defined (__sun__))
226 # define G_STMT_START if (1)
227 # define G_STMT_END else (void)0
229 # define G_STMT_START do
230 # define G_STMT_END while (0)
236 /* Provide macros to feature the GCC function attribute.
238 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
239 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
240 __attribute__((format (printf, format_idx, arg_idx)))
241 #define G_GNUC_SCANF( format_idx, arg_idx ) \
242 __attribute__((format (scanf, format_idx, arg_idx)))
243 #define G_GNUC_FORMAT( arg_idx ) \
244 __attribute__((format_arg (arg_idx)))
245 #define G_GNUC_NORETURN \
246 __attribute__((noreturn))
247 #define G_GNUC_CONST \
248 __attribute__((const))
249 #define G_GNUC_UNUSED \
250 __attribute__((unused))
251 #else /* !__GNUC__ */
252 #define G_GNUC_PRINTF( format_idx, arg_idx )
253 #define G_GNUC_SCANF( format_idx, arg_idx )
254 #define G_GNUC_FORMAT( arg_idx )
255 #define G_GNUC_NORETURN
257 #define G_GNUC_UNUSED
258 #endif /* !__GNUC__ */
261 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
262 * macros, so we can refer to them as strings unconditionally.
265 #define G_GNUC_FUNCTION (__FUNCTION__)
266 #define G_GNUC_PRETTY_FUNCTION (__PRETTY_FUNCTION__)
267 #else /* !__GNUC__ */
268 #define G_GNUC_FUNCTION ("")
269 #define G_GNUC_PRETTY_FUNCTION ("")
270 #endif /* !__GNUC__ */
272 /* we try to provide a usefull equivalent for ATEXIT if it is
273 * not defined, but use is actually abandoned. people should
274 * use g_atexit() instead.
277 # define ATEXIT(proc) g_ATEXIT(proc)
279 # define G_NATIVE_ATEXIT
282 /* Hacker macro to place breakpoints for elected machines.
283 * Actual use is strongly deprecated of course ;)
285 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
286 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
287 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
288 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
289 #else /* !__i386__ && !__alpha__ */
290 #define G_BREAKPOINT()
291 #endif /* __i386__ */
294 /* Provide macros for easily allocating memory. The macros
295 * will cast the allocated memory to the specified type
296 * in order to avoid compiler warnings. (Makes the code neater).
300 # define g_new(type, count) (ALLOC (type, count))
301 # define g_new0(type, count) (CALLOC (type, count))
302 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
303 #else /* __DMALLOC_H__ */
304 # define g_new(type, count) \
305 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
306 # define g_new0(type, count) \
307 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
308 # define g_renew(type, mem, count) \
309 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
310 #endif /* __DMALLOC_H__ */
312 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
313 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
315 sizeof (type) * (pre_alloc), \
318 #define g_chunk_new(type, chunk) ( \
319 (type *) g_mem_chunk_alloc (chunk) \
321 #define g_chunk_new0(type, chunk) ( \
322 (type *) g_mem_chunk_alloc0 (chunk) \
324 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
325 g_mem_chunk_free ((mem_chunk), (mem)); \
329 #define g_string(x) #x
332 /* Provide macros for error handling. The "assert" macros will
333 * exit on failure. The "return" macros will exit the current
334 * function. Two different definitions are given for the macros
335 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
336 * __PRETTY_FUNCTION__ capability.
339 #ifdef G_DISABLE_ASSERT
341 #define g_assert(expr)
342 #define g_assert_not_reached()
344 #else /* !G_DISABLE_ASSERT */
348 #define g_assert(expr) G_STMT_START{ \
350 g_log (G_LOG_DOMAIN, \
352 "file %s: line %d (%s): assertion failed: (%s)", \
355 __PRETTY_FUNCTION__, \
358 #define g_assert_not_reached() G_STMT_START{ \
359 g_log (G_LOG_DOMAIN, \
361 "file %s: line %d (%s): should not be reached", \
364 __PRETTY_FUNCTION__); }G_STMT_END
366 #else /* !__GNUC__ */
368 #define g_assert(expr) G_STMT_START{ \
370 g_log (G_LOG_DOMAIN, \
372 "file %s: line %d: assertion failed: (%s)", \
377 #define g_assert_not_reached() G_STMT_START{ \
378 g_log (G_LOG_DOMAIN, \
380 "file %s: line %d: should not be reached", \
382 __LINE__); }G_STMT_END
384 #endif /* __GNUC__ */
386 #endif /* !G_DISABLE_ASSERT */
389 #ifdef G_DISABLE_CHECKS
391 #define g_return_if_fail(expr)
392 #define g_return_val_if_fail(expr,val)
394 #else /* !G_DISABLE_CHECKS */
398 #define g_return_if_fail(expr) G_STMT_START{ \
401 g_log (G_LOG_DOMAIN, \
402 G_LOG_LEVEL_CRITICAL, \
403 "file %s: line %d (%s): assertion `%s' failed.", \
406 __PRETTY_FUNCTION__, \
411 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
414 g_log (G_LOG_DOMAIN, \
415 G_LOG_LEVEL_CRITICAL, \
416 "file %s: line %d (%s): assertion `%s' failed.", \
419 __PRETTY_FUNCTION__, \
424 #else /* !__GNUC__ */
426 #define g_return_if_fail(expr) G_STMT_START{ \
429 g_log (G_LOG_DOMAIN, \
430 G_LOG_LEVEL_CRITICAL, \
431 "file %s: line %d: assertion `%s' failed.", \
438 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
441 g_log (G_LOG_DOMAIN, \
442 G_LOG_LEVEL_CRITICAL, \
443 "file %s: line %d: assertion `%s' failed.", \
450 #endif /* !__GNUC__ */
452 #endif /* !G_DISABLE_CHECKS */
455 /* Provide type definitions for commonly used types.
456 * These are useful because a "gint8" can be adjusted
457 * to be 1 byte (8 bits) on all platforms. Similarly and
458 * more importantly, "gint32" can be adjusted to be
459 * 4 bytes (32 bits) on all platforms.
463 typedef short gshort;
466 typedef gint gboolean;
468 typedef unsigned char guchar;
469 typedef unsigned short gushort;
470 typedef unsigned long gulong;
471 typedef unsigned int guint;
473 #define G_GSHORT_FORMAT "hi"
474 #define G_GUSHORT_FORMAT "hu"
475 #define G_GINT_FORMAT "i"
476 #define G_GUINT_FORMAT "u"
477 #define G_GLONG_FORMAT "li"
478 #define G_GULONG_FORMAT "lu"
480 typedef float gfloat;
481 typedef double gdouble;
483 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
484 * Since gldouble isn't used anywhere, just disable it for now */
487 #ifdef HAVE_LONG_DOUBLE
488 typedef long double gldouble;
489 #else /* HAVE_LONG_DOUBLE */
490 typedef double gldouble;
491 #endif /* HAVE_LONG_DOUBLE */
494 typedef void* gpointer;
495 typedef const void *gconstpointer;
498 typedef gint32 gssize;
499 typedef guint32 gsize;
500 typedef guint32 GQuark;
501 typedef gint32 GTime;
504 /* Portable endian checks and conversions
506 * glibconfig.h defines G_BYTE_ORDER which expands to one of
509 #define G_LITTLE_ENDIAN 1234
510 #define G_BIG_ENDIAN 4321
511 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
514 /* Basic bit swapping functions
516 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
517 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
518 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
519 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
520 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
521 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
522 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
523 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
525 /* Intel specific stuff for speed
527 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
528 # define GUINT16_SWAP_LE_BE_X86(val) \
530 ({ register guint16 __v; \
531 if (__builtin_constant_p (val)) \
532 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
534 __asm__ __const__ ("rorw $8, %w0" \
536 : "0" ((guint16) (val))); \
538 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
539 # if !defined(__i486__) && !defined(__i586__) \
540 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
541 # define GUINT32_SWAP_LE_BE_X86(val) \
543 ({ register guint32 __v; \
544 if (__builtin_constant_p (val)) \
545 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
547 __asm__ __const__ ("rorw $8, %w0\n\t" \
551 : "0" ((guint32) (val))); \
553 # else /* 486 and higher has bswap */
554 # define GUINT32_SWAP_LE_BE_X86(val) \
556 ({ register guint32 __v; \
557 if (__builtin_constant_p (val)) \
558 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
560 __asm__ __const__ ("bswap %0" \
562 : "0" ((guint32) (val))); \
564 # endif /* processor specific 32-bit stuff */
565 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
566 #else /* !__i386__ */
567 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
568 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
569 #endif /* __i386__ */
572 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
573 (((guint64) (val) & \
574 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
575 (((guint64) (val) & \
576 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
577 (((guint64) (val) & \
578 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
579 (((guint64) (val) & \
580 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
581 (((guint64) (val) & \
582 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
583 (((guint64) (val) & \
584 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
585 (((guint64) (val) & \
586 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
587 (((guint64) (val) & \
588 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
589 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
590 # define GUINT64_SWAP_LE_BE_X86(val) \
592 ({ union { guint64 __ll; \
593 guint32 __l[2]; } __r; \
594 if (__builtin_constant_p (val)) \
595 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
598 union { guint64 __ll; \
599 guint32 __l[2]; } __w; \
600 __w.__ll = ((guint64) val); \
601 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
602 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
605 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
606 # else /* !__i386__ */
607 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
611 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
612 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
613 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
614 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
615 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
616 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
617 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
618 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
620 /* The G*_TO_?E() macros are defined in glibconfig.h.
621 * The transformation is symmetric, so the FROM just maps to the TO.
623 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
624 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
625 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
626 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
627 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
628 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
629 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
630 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
633 #define GINT64_FROM_LE(val) (GINT64_TO_LE (val))
634 #define GUINT64_FROM_LE(val) (GUINT64_TO_LE (val))
635 #define GINT64_FROM_BE(val) (GINT64_TO_BE (val))
636 #define GUINT64_FROM_BE(val) (GUINT64_TO_BE (val))
639 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
640 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
641 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
642 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
644 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
645 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
646 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
647 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
650 /* Portable versions of host-network order stuff
652 #define g_ntohl(val) (GUINT32_FROM_BE (val))
653 #define g_ntohs(val) (GUINT16_FROM_BE (val))
654 #define g_htonl(val) (GUINT32_TO_BE (val))
655 #define g_htons(val) (GUINT16_TO_BE (val))
659 * we prefix variable declarations so they can
660 * properly get exported in windows dlls.
663 # ifdef GLIB_COMPILATION
664 # define GUTILS_C_VAR __declspec(dllexport)
665 # else /* !GLIB_COMPILATION */
666 # define GUTILS_C_VAR extern __declspec(dllimport)
667 # endif /* !GLIB_COMPILATION */
668 #else /* !NATIVE_WIN32 */
669 # define GUTILS_C_VAR extern
670 #endif /* !NATIVE_WIN32 */
672 GUTILS_C_VAR const guint glib_major_version;
673 GUTILS_C_VAR const guint glib_minor_version;
674 GUTILS_C_VAR const guint glib_micro_version;
675 GUTILS_C_VAR const guint glib_interface_age;
676 GUTILS_C_VAR const guint glib_binary_age;
678 #define GLIB_CHECK_VERSION(major,minor,micro) \
679 (GLIB_MAJOR_VERSION > (major) || \
680 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
681 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
682 GLIB_MICRO_VERSION >= (micro)))
684 /* Forward declarations of glib types.
686 typedef struct _GAllocator GAllocator;
687 typedef struct _GArray GArray;
688 typedef struct _GByteArray GByteArray;
689 typedef struct _GCache GCache;
690 typedef struct _GCompletion GCompletion;
691 typedef struct _GData GData;
692 typedef struct _GDebugKey GDebugKey;
693 typedef struct _GHashTable GHashTable;
694 typedef struct _GHook GHook;
695 typedef struct _GHookList GHookList;
696 typedef struct _GList GList;
697 typedef struct _GMemChunk GMemChunk;
698 typedef struct _GNode GNode;
699 typedef struct _GPtrArray GPtrArray;
700 typedef struct _GQueue GQueue;
701 typedef struct _GRand GRand;
702 typedef struct _GRelation GRelation;
703 typedef struct _GScanner GScanner;
704 typedef struct _GScannerConfig GScannerConfig;
705 typedef struct _GSList GSList;
706 typedef struct _GStack GStack;
707 typedef struct _GString GString;
708 typedef struct _GStringChunk GStringChunk;
709 typedef struct _GTimer GTimer;
710 typedef struct _GTree GTree;
711 typedef struct _GTuples GTuples;
712 typedef union _GTokenValue GTokenValue;
713 typedef struct _GIOChannel GIOChannel;
717 G_TRAVERSE_LEAFS = 1 << 0,
718 G_TRAVERSE_NON_LEAFS = 1 << 1,
719 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
720 G_TRAVERSE_MASK = 0x03
731 /* Log level shift offset for user defined
732 * log levels (0-7 are used by GLib).
734 #define G_LOG_LEVEL_USER_SHIFT (8)
736 /* Glib log levels and flags.
741 G_LOG_FLAG_RECURSION = 1 << 0,
742 G_LOG_FLAG_FATAL = 1 << 1,
744 /* GLib log levels */
745 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
746 G_LOG_LEVEL_CRITICAL = 1 << 3,
747 G_LOG_LEVEL_WARNING = 1 << 4,
748 G_LOG_LEVEL_MESSAGE = 1 << 5,
749 G_LOG_LEVEL_INFO = 1 << 6,
750 G_LOG_LEVEL_DEBUG = 1 << 7,
752 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
755 /* GLib log levels that are considered fatal by default */
756 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
759 typedef gpointer (*GCacheNewFunc) (gpointer key);
760 typedef gpointer (*GCacheDupFunc) (gpointer value);
761 typedef void (*GCacheDestroyFunc) (gpointer value);
762 typedef gint (*GCompareFunc) (gconstpointer a,
764 typedef gchar* (*GCompletionFunc) (gpointer);
765 typedef void (*GDestroyNotify) (gpointer data);
766 typedef void (*GDataForeachFunc) (GQuark key_id,
769 typedef void (*GFunc) (gpointer data,
771 typedef guint (*GHashFunc) (gconstpointer key);
772 typedef void (*GFreeFunc) (gpointer data);
773 typedef void (*GHFunc) (gpointer key,
776 typedef gboolean (*GHRFunc) (gpointer key,
779 typedef gint (*GHookCompareFunc) (GHook *new_hook,
781 typedef gboolean (*GHookFindFunc) (GHook *hook,
783 typedef void (*GHookMarshaller) (GHook *hook,
785 typedef gboolean (*GHookCheckMarshaller) (GHook *hook,
787 typedef void (*GHookFunc) (gpointer data);
788 typedef gboolean (*GHookCheckFunc) (gpointer data);
789 typedef void (*GHookFreeFunc) (GHookList *hook_list,
791 typedef void (*GLogFunc) (const gchar *log_domain,
792 GLogLevelFlags log_level,
793 const gchar *message,
795 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
797 typedef void (*GNodeForeachFunc) (GNode *node,
799 typedef gint (*GSearchFunc) (gpointer key,
801 typedef void (*GScannerMsgFunc) (GScanner *scanner,
804 typedef gint (*GTraverseFunc) (gpointer key,
807 typedef void (*GVoidFunc) (void);
871 /* Doubly linked lists
873 void g_list_push_allocator (GAllocator *allocator);
874 void g_list_pop_allocator (void);
875 GList* g_list_alloc (void);
876 void g_list_free (GList *list);
877 void g_list_free_1 (GList *list);
878 GList* g_list_append (GList *list,
880 GList* g_list_prepend (GList *list,
882 GList* g_list_insert (GList *list,
885 GList* g_list_insert_sorted (GList *list,
888 GList* g_list_concat (GList *list1,
890 GList* g_list_delete (GList *list,
892 GList* g_list_remove (GList *list,
894 GList* g_list_remove_link (GList *list,
896 GList* g_list_reverse (GList *list);
897 GList* g_list_copy (GList *list);
898 GList* g_list_nth (GList *list,
900 GList* g_list_find (GList *list,
902 GList* g_list_find_custom (GList *list,
905 gint g_list_position (GList *list,
907 gint g_list_index (GList *list,
909 GList* g_list_last (GList *list);
910 GList* g_list_first (GList *list);
911 guint g_list_length (GList *list);
912 void g_list_foreach (GList *list,
915 GList* g_list_sort (GList *list,
916 GCompareFunc compare_func);
917 gpointer g_list_nth_data (GList *list,
919 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
920 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
923 /* Singly linked lists
925 void g_slist_push_allocator (GAllocator *allocator);
926 void g_slist_pop_allocator (void);
927 GSList* g_slist_alloc (void);
928 void g_slist_free (GSList *list);
929 void g_slist_free_1 (GSList *list);
930 GSList* g_slist_append (GSList *list,
932 GSList* g_slist_prepend (GSList *list,
934 GSList* g_slist_insert (GSList *list,
937 GSList* g_slist_insert_sorted (GSList *list,
940 GSList* g_slist_concat (GSList *list1,
942 GSList* g_slist_remove (GSList *list,
944 GSList* g_slist_remove_link (GSList *list,
946 GSList* g_slist_reverse (GSList *list);
947 GSList* g_slist_copy (GSList *list);
948 GSList* g_slist_nth (GSList *list,
950 GSList* g_slist_find (GSList *list,
952 GSList* g_slist_find_custom (GSList *list,
955 gint g_slist_position (GSList *list,
957 gint g_slist_index (GSList *list,
959 GSList* g_slist_last (GSList *list);
960 guint g_slist_length (GSList *list);
961 void g_slist_foreach (GSList *list,
964 GSList* g_slist_sort (GSList *list,
965 GCompareFunc compare_func);
966 gpointer g_slist_nth_data (GSList *list,
968 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
974 GStack * g_stack_new (void);
975 void g_stack_free (GStack *stack);
976 gpointer g_stack_pop (GStack *stack);
978 #define g_stack_empty(stack) \
979 ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? FALSE : TRUE)
981 #define g_stack_peek(stack) \
982 ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
983 ((GStack *)(stack))->list->data : NULL)
985 #define g_stack_index(stack,ptr) \
986 ((((GStack *)(stack)) && ((GStack *)(stack))->list) ? \
987 g_list_index (((GStack *)(stack))->list, (ptr)) : -1)
989 #define g_stack_push(stack,data) G_STMT_START { \
990 if ((GStack *)(stack)) \
991 ((GStack *)(stack))->list = \
992 g_list_prepend (((GStack *)(stack))->list, (data)); \
1000 GQueue * g_queue_new (void);
1001 void g_queue_free (GQueue *q);
1002 guint g_queue_get_size (GQueue *q);
1003 void g_queue_push_front (GQueue *q, gpointer data);
1004 void g_queue_push_back (GQueue *q, gpointer data);
1005 gpointer g_queue_pop_front (GQueue *q);
1006 gpointer g_queue_pop_back (GQueue *q);
1008 #define g_queue_empty(queue) \
1009 ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? FALSE : TRUE)
1011 #define g_queue_peek_front(queue) \
1012 ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
1013 ((GQueue *)(queue))->list->data : NULL)
1015 #define g_queue_peek_back(queue) \
1016 ((((GQueue *)(queue)) && ((GQueue *)(queue))->list_end) ? \
1017 ((GQueue *)(queue))->list_end->data : NULL)
1019 #define g_queue_index(queue,ptr) \
1020 ((((GQueue *)(queue)) && ((GQueue *)(queue))->list) ? \
1021 g_list_index (((GQueue *)(queue))->list, (ptr)) : -1)
1023 #define g_queue_push g_queue_push_back
1024 #define g_queue_pop g_queue_pop_front
1025 #define g_queue_peek g_queue_peek_front
1033 GHashTable* g_hash_table_new (GHashFunc hash_func,
1034 GCompareFunc key_compare_func);
1035 void g_hash_table_destroy (GHashTable *hash_table);
1036 void g_hash_table_insert (GHashTable *hash_table,
1039 void g_hash_table_remove (GHashTable *hash_table,
1041 gpointer g_hash_table_lookup (GHashTable *hash_table,
1043 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
1044 gconstpointer lookup_key,
1047 void g_hash_table_freeze (GHashTable *hash_table);
1048 void g_hash_table_thaw (GHashTable *hash_table);
1049 void g_hash_table_foreach (GHashTable *hash_table,
1051 gpointer user_data);
1052 guint g_hash_table_foreach_remove (GHashTable *hash_table,
1054 gpointer user_data);
1055 guint g_hash_table_size (GHashTable *hash_table);
1060 GCache* g_cache_new (GCacheNewFunc value_new_func,
1061 GCacheDestroyFunc value_destroy_func,
1062 GCacheDupFunc key_dup_func,
1063 GCacheDestroyFunc key_destroy_func,
1064 GHashFunc hash_key_func,
1065 GHashFunc hash_value_func,
1066 GCompareFunc key_compare_func);
1067 void g_cache_destroy (GCache *cache);
1068 gpointer g_cache_insert (GCache *cache,
1070 void g_cache_remove (GCache *cache,
1072 void g_cache_key_foreach (GCache *cache,
1074 gpointer user_data);
1075 void g_cache_value_foreach (GCache *cache,
1077 gpointer user_data);
1080 /* Balanced binary trees
1082 GTree* g_tree_new (GCompareFunc key_compare_func);
1083 void g_tree_destroy (GTree *tree);
1084 void g_tree_insert (GTree *tree,
1087 void g_tree_remove (GTree *tree,
1089 gpointer g_tree_lookup (GTree *tree,
1091 void g_tree_traverse (GTree *tree,
1092 GTraverseFunc traverse_func,
1093 GTraverseType traverse_type,
1095 gpointer g_tree_search (GTree *tree,
1096 GSearchFunc search_func,
1098 gint g_tree_height (GTree *tree);
1099 gint g_tree_nnodes (GTree *tree);
1103 /* N-way tree implementation
1114 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1115 ((GNode*) (node))->prev == NULL && \
1116 ((GNode*) (node))->next == NULL)
1117 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1119 void g_node_push_allocator (GAllocator *allocator);
1120 void g_node_pop_allocator (void);
1121 GNode* g_node_new (gpointer data);
1122 void g_node_destroy (GNode *root);
1123 void g_node_unlink (GNode *node);
1124 GNode* g_node_insert (GNode *parent,
1127 GNode* g_node_insert_before (GNode *parent,
1130 GNode* g_node_prepend (GNode *parent,
1132 guint g_node_n_nodes (GNode *root,
1133 GTraverseFlags flags);
1134 GNode* g_node_get_root (GNode *node);
1135 gboolean g_node_is_ancestor (GNode *node,
1137 guint g_node_depth (GNode *node);
1138 GNode* g_node_find (GNode *root,
1139 GTraverseType order,
1140 GTraverseFlags flags,
1143 /* convenience macros */
1144 #define g_node_append(parent, node) \
1145 g_node_insert_before ((parent), NULL, (node))
1146 #define g_node_insert_data(parent, position, data) \
1147 g_node_insert ((parent), (position), g_node_new (data))
1148 #define g_node_insert_data_before(parent, sibling, data) \
1149 g_node_insert_before ((parent), (sibling), g_node_new (data))
1150 #define g_node_prepend_data(parent, data) \
1151 g_node_prepend ((parent), g_node_new (data))
1152 #define g_node_append_data(parent, data) \
1153 g_node_insert_before ((parent), NULL, g_node_new (data))
1155 /* traversal function, assumes that `node' is root
1156 * (only traverses `node' and its subtree).
1157 * this function is just a high level interface to
1158 * low level traversal functions, optimized for speed.
1160 void g_node_traverse (GNode *root,
1161 GTraverseType order,
1162 GTraverseFlags flags,
1164 GNodeTraverseFunc func,
1167 /* return the maximum tree height starting with `node', this is an expensive
1168 * operation, since we need to visit all nodes. this could be shortened by
1169 * adding `guint height' to struct _GNode, but then again, this is not very
1170 * often needed, and would make g_node_insert() more time consuming.
1172 guint g_node_max_height (GNode *root);
1174 void g_node_children_foreach (GNode *node,
1175 GTraverseFlags flags,
1176 GNodeForeachFunc func,
1178 void g_node_reverse_children (GNode *node);
1179 guint g_node_n_children (GNode *node);
1180 GNode* g_node_nth_child (GNode *node,
1182 GNode* g_node_last_child (GNode *node);
1183 GNode* g_node_find_child (GNode *node,
1184 GTraverseFlags flags,
1186 gint g_node_child_position (GNode *node,
1188 gint g_node_child_index (GNode *node,
1191 GNode* g_node_first_sibling (GNode *node);
1192 GNode* g_node_last_sibling (GNode *node);
1194 #define g_node_prev_sibling(node) ((node) ? \
1195 ((GNode*) (node))->prev : NULL)
1196 #define g_node_next_sibling(node) ((node) ? \
1197 ((GNode*) (node))->next : NULL)
1198 #define g_node_first_child(node) ((node) ? \
1199 ((GNode*) (node))->children : NULL)
1202 /* Callback maintenance functions
1204 #define G_HOOK_FLAG_USER_SHIFT (4)
1207 G_HOOK_FLAG_ACTIVE = 1 << 0,
1208 G_HOOK_FLAG_IN_CALL = 1 << 1,
1209 G_HOOK_FLAG_MASK = 0x0f
1212 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1220 GMemChunk *hook_memchunk;
1221 GHookFreeFunc hook_free; /* virtual function */
1222 GHookFreeFunc hook_destroy; /* virtual function */
1234 GDestroyNotify destroy;
1237 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1238 G_HOOK_FLAG_ACTIVE) != 0)
1239 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1240 G_HOOK_FLAG_IN_CALL) != 0)
1241 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1242 G_HOOK_ACTIVE (hook))
1243 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1244 ((GHook*) hook)->prev == NULL && \
1245 ((GHook*) hook)->hook_id == 0 && \
1246 ((GHook*) hook)->ref_count == 0)
1248 void g_hook_list_init (GHookList *hook_list,
1250 void g_hook_list_clear (GHookList *hook_list);
1251 GHook* g_hook_alloc (GHookList *hook_list);
1252 void g_hook_free (GHookList *hook_list,
1254 void g_hook_ref (GHookList *hook_list,
1256 void g_hook_unref (GHookList *hook_list,
1258 gboolean g_hook_destroy (GHookList *hook_list,
1260 void g_hook_destroy_link (GHookList *hook_list,
1262 void g_hook_prepend (GHookList *hook_list,
1264 void g_hook_insert_before (GHookList *hook_list,
1267 void g_hook_insert_sorted (GHookList *hook_list,
1269 GHookCompareFunc func);
1270 GHook* g_hook_get (GHookList *hook_list,
1272 GHook* g_hook_find (GHookList *hook_list,
1273 gboolean need_valids,
1276 GHook* g_hook_find_data (GHookList *hook_list,
1277 gboolean need_valids,
1279 GHook* g_hook_find_func (GHookList *hook_list,
1280 gboolean need_valids,
1282 GHook* g_hook_find_func_data (GHookList *hook_list,
1283 gboolean need_valids,
1286 /* return the first valid hook, and increment its reference count */
1287 GHook* g_hook_first_valid (GHookList *hook_list,
1288 gboolean may_be_in_call);
1289 /* return the next valid hook with incremented reference count, and
1290 * decrement the reference count of the original hook
1292 GHook* g_hook_next_valid (GHookList *hook_list,
1294 gboolean may_be_in_call);
1296 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1297 gint g_hook_compare_ids (GHook *new_hook,
1300 /* convenience macros */
1301 #define g_hook_append( hook_list, hook ) \
1302 g_hook_insert_before ((hook_list), NULL, (hook))
1304 /* invoke all valid hooks with the (*GHookFunc) signature.
1306 void g_hook_list_invoke (GHookList *hook_list,
1307 gboolean may_recurse);
1308 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1309 * and destroy the hook if FALSE is returned.
1311 void g_hook_list_invoke_check (GHookList *hook_list,
1312 gboolean may_recurse);
1313 /* invoke a marshaller on all valid hooks.
1315 void g_hook_list_marshal (GHookList *hook_list,
1316 gboolean may_recurse,
1317 GHookMarshaller marshaller,
1319 void g_hook_list_marshal_check (GHookList *hook_list,
1320 gboolean may_recurse,
1321 GHookCheckMarshaller marshaller,
1325 /* Fatal error handlers.
1326 * g_on_error_query() will prompt the user to either
1327 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1328 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1329 * process and shows a stack trace.
1330 * These function may cause different actions on non-unix platforms.
1331 * The prg_name arg is required by gdb to find the executable, if it is
1332 * passed as NULL, g_on_error_query() will try g_get_prgname().
1334 void g_on_error_query (const gchar *prg_name);
1335 void g_on_error_stack_trace (const gchar *prg_name);
1338 /* Logging mechanism
1340 extern const gchar *g_log_domain_glib;
1341 guint g_log_set_handler (const gchar *log_domain,
1342 GLogLevelFlags log_levels,
1344 gpointer user_data);
1345 void g_log_remove_handler (const gchar *log_domain,
1347 void g_log_default_handler (const gchar *log_domain,
1348 GLogLevelFlags log_level,
1349 const gchar *message,
1350 gpointer unused_data);
1351 void g_log (const gchar *log_domain,
1352 GLogLevelFlags log_level,
1353 const gchar *format,
1354 ...) G_GNUC_PRINTF (3, 4);
1355 void g_logv (const gchar *log_domain,
1356 GLogLevelFlags log_level,
1357 const gchar *format,
1359 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1360 GLogLevelFlags fatal_mask);
1361 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1362 #ifndef G_LOG_DOMAIN
1363 #define G_LOG_DOMAIN ((gchar*) 0)
1364 #endif /* G_LOG_DOMAIN */
1366 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1367 G_LOG_LEVEL_ERROR, \
1369 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1370 G_LOG_LEVEL_MESSAGE, \
1372 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1373 G_LOG_LEVEL_WARNING, \
1375 #else /* !__GNUC__ */
1377 g_error (const gchar *format,
1381 va_start (args, format);
1382 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1386 g_message (const gchar *format,
1390 va_start (args, format);
1391 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1395 g_warning (const gchar *format,
1399 va_start (args, format);
1400 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1403 #endif /* !__GNUC__ */
1405 typedef void (*GPrintFunc) (const gchar *string);
1406 void g_print (const gchar *format,
1407 ...) G_GNUC_PRINTF (1, 2);
1408 GPrintFunc g_set_print_handler (GPrintFunc func);
1409 void g_printerr (const gchar *format,
1410 ...) G_GNUC_PRINTF (1, 2);
1411 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1413 /* deprecated compatibility functions, use g_log_set_handler() instead */
1414 typedef void (*GErrorFunc) (const gchar *str);
1415 typedef void (*GWarningFunc) (const gchar *str);
1416 GErrorFunc g_set_error_handler (GErrorFunc func);
1417 GWarningFunc g_set_warning_handler (GWarningFunc func);
1418 GPrintFunc g_set_message_handler (GPrintFunc func);
1421 /* Memory allocation and debugging
1425 #define g_malloc(size) ((gpointer) MALLOC (size))
1426 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1427 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1428 #define g_free(mem) FREE (mem)
1430 #else /* !USE_DMALLOC */
1432 gpointer g_malloc (gulong size);
1433 gpointer g_malloc0 (gulong size);
1434 gpointer g_realloc (gpointer mem,
1436 void g_free (gpointer mem);
1438 #endif /* !USE_DMALLOC */
1440 void g_mem_profile (void);
1441 void g_mem_check (gpointer mem);
1443 /* Generic allocators
1445 GAllocator* g_allocator_new (const gchar *name,
1447 void g_allocator_free (GAllocator *allocator);
1449 #define G_ALLOCATOR_LIST (1)
1450 #define G_ALLOCATOR_SLIST (2)
1451 #define G_ALLOCATOR_NODE (3)
1454 /* "g_mem_chunk_new" creates a new memory chunk.
1455 * Memory chunks are used to allocate pieces of memory which are
1456 * always the same size. Lists are a good example of such a data type.
1457 * The memory chunk allocates and frees blocks of memory as needed.
1458 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1459 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1460 * fault...somewhere).
1462 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1463 * want to know what's going on inside do you?)
1466 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1467 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1468 * atom. (They are also useful for lists which use MemChunk to allocate
1469 * memory but are also part of the MemChunk implementation).
1470 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1473 #define G_ALLOC_ONLY 1
1474 #define G_ALLOC_AND_FREE 2
1476 GMemChunk* g_mem_chunk_new (gchar *name,
1480 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1481 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1482 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1483 void g_mem_chunk_free (GMemChunk *mem_chunk,
1485 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1486 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1487 void g_mem_chunk_print (GMemChunk *mem_chunk);
1488 void g_mem_chunk_info (void);
1490 /* Ah yes...we have a "g_blow_chunks" function.
1491 * "g_blow_chunks" simply compresses all the chunks. This operation
1492 * consists of freeing every memory area that should be freed (but
1493 * which we haven't gotten around to doing yet). And, no,
1494 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1495 * much better name than "g_mem_chunk_clean_all" or something
1498 void g_blow_chunks (void);
1503 GTimer* g_timer_new (void);
1504 void g_timer_destroy (GTimer *timer);
1505 void g_timer_start (GTimer *timer);
1506 void g_timer_stop (GTimer *timer);
1507 void g_timer_reset (GTimer *timer);
1508 gdouble g_timer_elapsed (GTimer *timer,
1509 gulong *microseconds);
1512 /* String utility functions that modify a string argument or
1513 * return a constant string that must not be freed.
1515 #define G_STR_DELIMITERS "_-|> <."
1516 gchar* g_strdelimit (gchar *string,
1517 const gchar *delimiters,
1518 gchar new_delimiter);
1519 gdouble g_strtod (const gchar *nptr,
1521 gchar* g_strerror (gint errnum);
1522 gchar* g_strsignal (gint signum);
1523 gint g_strcasecmp (const gchar *s1,
1525 gint g_strncasecmp (const gchar *s1,
1528 void g_strdown (gchar *string);
1529 void g_strup (gchar *string);
1530 void g_strreverse (gchar *string);
1531 /* removes leading spaces */
1532 gchar* g_strchug (gchar *string);
1533 /* removes trailing spaces */
1534 gchar* g_strchomp (gchar *string);
1535 /* removes leading & trailing spaces */
1536 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1538 /* String utility functions that return a newly allocated string which
1539 * ought to be freed from the caller at some point.
1541 gchar* g_strdup (const gchar *str);
1542 gchar* g_strdup_printf (const gchar *format,
1543 ...) G_GNUC_PRINTF (1, 2);
1544 gchar* g_strdup_vprintf (const gchar *format,
1546 gchar* g_strndup (const gchar *str,
1548 gchar* g_strnfill (guint length,
1550 gchar* g_strconcat (const gchar *string1,
1551 ...); /* NULL terminated */
1552 gchar* g_strjoin (const gchar *separator,
1553 ...); /* NULL terminated */
1554 gchar* g_strescape (gchar *string);
1555 gpointer g_memdup (gconstpointer mem,
1558 /* NULL terminated string arrays.
1559 * g_strsplit() splits up string into max_tokens tokens at delim and
1560 * returns a newly allocated string array.
1561 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1562 * optional separator, the returned string is newly allocated.
1563 * g_strfreev() frees the array itself and all of its strings.
1565 gchar** g_strsplit (const gchar *string,
1566 const gchar *delimiter,
1568 gchar* g_strjoinv (const gchar *separator,
1570 void g_strfreev (gchar **str_array);
1574 /* calculate a string size, guarranteed to fit format + args.
1576 guint g_printf_string_upper_bound (const gchar* format,
1580 /* Retrive static string info
1582 gchar* g_get_user_name (void);
1583 gchar* g_get_real_name (void);
1584 gchar* g_get_home_dir (void);
1585 gchar* g_get_tmp_dir (void);
1586 gchar* g_get_prgname (void);
1587 void g_set_prgname (const gchar *prgname);
1590 /* Miscellaneous utility functions
1592 guint g_parse_debug_string (const gchar *string,
1595 gint g_snprintf (gchar *string,
1597 gchar const *format,
1598 ...) G_GNUC_PRINTF (3, 4);
1599 gint g_vsnprintf (gchar *string,
1601 gchar const *format,
1603 gchar* g_basename (const gchar *file_name);
1604 /* Check if a file name is an absolute path */
1605 gboolean g_path_is_absolute (const gchar *file_name);
1606 /* In case of absolute paths, skip the root part */
1607 gchar* g_path_skip_root (gchar *file_name);
1609 /* strings are newly allocated with g_malloc() */
1610 gchar* g_dirname (const gchar *file_name);
1611 gchar* g_get_current_dir (void);
1612 gchar* g_getenv (const gchar *variable);
1615 /* we use a GLib function as a replacement for ATEXIT, so
1616 * the programmer is not required to check the return value
1617 * (if there is any in the implementation) and doesn't encounter
1618 * missing include files.
1620 void g_atexit (GVoidFunc func);
1625 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1629 g_bit_nth_lsf (guint32 mask,
1635 if (mask & (1 << (guint) nth_bit))
1638 while (nth_bit < 32);
1641 #endif /* G_CAN_INLINE */
1643 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1647 g_bit_nth_msf (guint32 mask,
1655 if (mask & (1 << (guint) nth_bit))
1658 while (nth_bit > 0);
1661 #endif /* G_CAN_INLINE */
1663 G_INLINE_FUNC guint g_bit_storage (guint number);
1666 g_bit_storage (guint number)
1668 register guint n_bits = 0;
1678 #endif /* G_CAN_INLINE */
1682 GStringChunk* g_string_chunk_new (gint size);
1683 void g_string_chunk_free (GStringChunk *chunk);
1684 gchar* g_string_chunk_insert (GStringChunk *chunk,
1685 const gchar *string);
1686 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1687 const gchar *string);
1694 G_STRING_ERROR_NONE, /* No error occurred */
1695 G_STRING_ERROR_INVAL, /* Invalid input value to function */
1696 G_STRING_ERROR_READ, /* read() returned an error - check errno */
1697 G_STRING_ERROR_NODATA, /* No more input data - result string may contain data */
1698 G_STRING_ERROR_LENGTH /* max_length reached */
1701 #define g_string_length(fstring) (fstring ? fstring->len : 0)
1702 #define g_string_str(fstring) (fstring ? fstring->str : NULL)
1703 #define g_string_char(fstring, n) (fstring->str[n])
1705 #define g_string_copy(a,b) (g_string_assign(a, b->str))
1706 #define g_string_dup(fstring) (fstring ? g_string_new(fstring->str) :\
1709 #define g_string_cmp(a,b) (strcmp(g_string_str(a), \
1711 #define g_string_ncmp(a,b,n) (strncmp(g_string_str(a), \
1712 g_string_str(b), n))
1713 #define g_string_casecmp(a,b) (g_strcasecmp(g_string_str(a), \
1715 #define g_string_ncasecmp(a,b,n) (g_strncasecmp(g_string_str(a), \
1716 g_string_str(b), n))
1718 #define g_string_strcmp(a,b) (strcmp(g_string_str(a), b))
1719 #define g_string_strcasecmp(a,b) (g_strcasecmp(g_string_str(a), b))
1720 #define g_string_strncasecmp(a,b,n) (g_strncasecmp(g_string_str(a), b, n))
1722 GString* g_string_new (const gchar *init);
1723 GString* g_string_sized_new (guint dfl_size);
1724 void g_string_free (GString *string,
1726 GString* g_string_assign (GString *lval,
1728 GString* g_string_truncate (GString *string,
1730 GString* g_string_append (GString *string,
1732 GString* g_string_append_c (GString *string,
1734 GString* g_string_prepend (GString *string,
1736 GString* g_string_prepend_c (GString *string,
1738 GString* g_string_insert (GString *string,
1741 GString* g_string_insert_c (GString *string,
1744 GString* g_string_erase (GString *string,
1747 GString* g_string_down (GString *string);
1748 GString* g_string_up (GString *string);
1749 void g_string_sprintf (GString *string,
1750 const gchar *format,
1751 ...) G_GNUC_PRINTF (2, 3);
1752 void g_string_sprintfa (GString *string,
1753 const gchar *format,
1754 ...) G_GNUC_PRINTF (2, 3);
1755 GStringError g_string_readline (GString *dest_str,
1758 GStringError g_string_readline_buffered (GString *dest_str,
1762 gint match_bare_cr);
1763 GList* g_string_tokenise (GString *string,
1767 void g_string_tokenise_free (GList *tokens,
1771 /* Resizable arrays, remove fills any cleared spot and shortens the
1772 * array, while preserving the order. remove_fast will distort the
1773 * order by moving the last element to the position of the removed
1776 #define g_array_append_val(a,v) g_array_append_vals (a, &v, 1)
1777 #define g_array_prepend_val(a,v) g_array_prepend_vals (a, &v, 1)
1778 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1779 #define g_array_index(a,t,i) (((t*) (a)->data) [(i)])
1781 GArray* g_array_new (gboolean zero_terminated,
1783 guint element_size);
1784 void g_array_free (GArray *array,
1785 gboolean free_segment);
1786 GArray* g_array_append_vals (GArray *array,
1789 GArray* g_array_prepend_vals (GArray *array,
1792 GArray* g_array_insert_vals (GArray *array,
1796 GArray* g_array_set_size (GArray *array,
1798 GArray* g_array_remove_index (GArray *array,
1800 GArray* g_array_remove_index_fast (GArray *array,
1803 /* Resizable pointer array. This interface is much less complicated
1804 * than the above. Add appends appends a pointer. Remove fills any
1805 * cleared spot and shortens the array. remove_fast will again distort
1808 #define g_ptr_array_index(array,index) (array->pdata)[index]
1809 GPtrArray* g_ptr_array_new (void);
1810 void g_ptr_array_free (GPtrArray *array,
1812 void g_ptr_array_set_size (GPtrArray *array,
1814 gpointer g_ptr_array_remove_index (GPtrArray *array,
1816 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1818 gboolean g_ptr_array_remove (GPtrArray *array,
1820 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1822 void g_ptr_array_add (GPtrArray *array,
1825 /* Byte arrays, an array of guint8. Implemented as a GArray,
1829 GByteArray* g_byte_array_new (void);
1830 void g_byte_array_free (GByteArray *array,
1831 gboolean free_segment);
1832 GByteArray* g_byte_array_append (GByteArray *array,
1835 GByteArray* g_byte_array_prepend (GByteArray *array,
1838 GByteArray* g_byte_array_set_size (GByteArray *array,
1840 GByteArray* g_byte_array_remove_index (GByteArray *array,
1842 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1848 gint g_str_equal (gconstpointer v,
1850 guint g_str_hash (gconstpointer v);
1852 gint g_int_equal (gconstpointer v,
1854 guint g_int_hash (gconstpointer v);
1856 /* This "hash" function will just return the key's adress as an
1857 * unsigned integer. Useful for hashing on plain adresses or
1858 * simple integer values.
1859 * passing NULL into g_hash_table_new() as GHashFunc has the
1860 * same effect as passing g_direct_hash().
1862 guint g_direct_hash (gconstpointer v);
1863 gint g_direct_equal (gconstpointer v,
1867 /* Quarks (string<->id association)
1869 GQuark g_quark_try_string (const gchar *string);
1870 GQuark g_quark_from_static_string (const gchar *string);
1871 GQuark g_quark_from_string (const gchar *string);
1872 gchar* g_quark_to_string (GQuark quark);
1877 void g_datalist_init (GData **datalist);
1878 void g_datalist_clear (GData **datalist);
1879 gpointer g_datalist_id_get_data (GData **datalist,
1881 void g_datalist_id_set_data_full (GData **datalist,
1884 GDestroyNotify destroy_func);
1885 void g_datalist_id_remove_no_notify (GData **datalist,
1887 void g_datalist_foreach (GData **datalist,
1888 GDataForeachFunc func,
1889 gpointer user_data);
1890 #define g_datalist_id_set_data(dl, q, d) \
1891 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1892 #define g_datalist_id_remove_data(dl, q) \
1893 g_datalist_id_set_data ((dl), (q), NULL)
1894 #define g_datalist_get_data(dl, k) \
1895 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1896 #define g_datalist_set_data_full(dl, k, d, f) \
1897 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1898 #define g_datalist_remove_no_notify(dl, k) \
1899 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1900 #define g_datalist_set_data(dl, k, d) \
1901 g_datalist_set_data_full ((dl), (k), (d), NULL)
1902 #define g_datalist_remove_data(dl, k) \
1903 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1906 /* Location Associated Keyed Data
1908 void g_dataset_destroy (gconstpointer dataset_location);
1909 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1911 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1914 GDestroyNotify destroy_func);
1915 void g_dataset_id_remove_no_notify (gconstpointer dataset_location,
1917 void g_dataset_foreach (gconstpointer dataset_location,
1918 GDataForeachFunc func,
1919 gpointer user_data);
1920 #define g_dataset_id_set_data(l, k, d) \
1921 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1922 #define g_dataset_id_remove_data(l, k) \
1923 g_dataset_id_set_data ((l), (k), NULL)
1924 #define g_dataset_get_data(l, k) \
1925 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1926 #define g_dataset_set_data_full(l, k, d, f) \
1927 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1928 #define g_dataset_remove_no_notify(l, k) \
1929 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1930 #define g_dataset_set_data(l, k, d) \
1931 g_dataset_set_data_full ((l), (k), (d), NULL)
1932 #define g_dataset_remove_data(l, k) \
1933 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1936 /* GScanner: Flexible lexical scanner for general purpose.
1939 /* Character sets */
1940 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1941 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1942 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1943 "\307\310\311\312\313\314\315\316\317\320"\
1944 "\321\322\323\324\325\326"\
1945 "\330\331\332\333\334\335\336"
1946 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1947 "\347\350\351\352\353\354\355\356\357\360"\
1948 "\361\362\363\364\365\366"\
1949 "\370\371\372\373\374\375\376\377"
1956 G_ERR_UNEXP_EOF_IN_STRING,
1957 G_ERR_UNEXP_EOF_IN_COMMENT,
1958 G_ERR_NON_DIGIT_IN_CONST,
1961 G_ERR_FLOAT_MALFORMED
1969 G_TOKEN_LEFT_PAREN = '(',
1970 G_TOKEN_RIGHT_PAREN = ')',
1971 G_TOKEN_LEFT_CURLY = '{',
1972 G_TOKEN_RIGHT_CURLY = '}',
1973 G_TOKEN_LEFT_BRACE = '[',
1974 G_TOKEN_RIGHT_BRACE = ']',
1975 G_TOKEN_EQUAL_SIGN = '=',
1976 G_TOKEN_COMMA = ',',
1992 G_TOKEN_IDENTIFIER_NULL,
1994 G_TOKEN_COMMENT_SINGLE,
1995 G_TOKEN_COMMENT_MULTI,
2002 gchar *v_identifier;
2014 struct _GScannerConfig
2018 gchar *cset_skip_characters; /* default: " \t\n" */
2019 gchar *cset_identifier_first;
2020 gchar *cset_identifier_nth;
2021 gchar *cpair_comment_single; /* default: "#\n" */
2023 /* Should symbol lookup work case sensitive?
2025 guint case_sensitive : 1;
2027 /* Boolean values to be adjusted "on the fly"
2028 * to configure scanning behaviour.
2030 guint skip_comment_multi : 1; /* C like comment */
2031 guint skip_comment_single : 1; /* single line comment */
2032 guint scan_comment_multi : 1; /* scan multi line comments? */
2033 guint scan_identifier : 1;
2034 guint scan_identifier_1char : 1;
2035 guint scan_identifier_NULL : 1;
2036 guint scan_symbols : 1;
2037 guint scan_binary : 1;
2038 guint scan_octal : 1;
2039 guint scan_float : 1;
2040 guint scan_hex : 1; /* `0x0ff0' */
2041 guint scan_hex_dollar : 1; /* `$0ff0' */
2042 guint scan_string_sq : 1; /* string: 'anything' */
2043 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
2044 guint numbers_2_int : 1; /* bin, octal, hex => int */
2045 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
2046 guint identifier_2_string : 1;
2047 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
2048 guint symbol_2_token : 1;
2049 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
2056 guint max_parse_errors;
2058 /* g_scanner_error() increments this field */
2061 /* name of input stream, featured by the default message handler */
2062 const gchar *input_name;
2064 /* data pointer for derived structures */
2065 gpointer derived_data;
2067 /* link into the scanner configuration */
2068 GScannerConfig *config;
2070 /* fields filled in after g_scanner_get_next_token() */
2076 /* fields filled in after g_scanner_peek_next_token() */
2077 GTokenType next_token;
2078 GTokenValue next_value;
2080 guint next_position;
2082 /* to be considered private */
2083 GHashTable *symbol_table;
2086 const gchar *text_end;
2090 /* handler function for _warn and _error */
2091 GScannerMsgFunc msg_handler;
2094 GScanner* g_scanner_new (GScannerConfig *config_templ);
2095 void g_scanner_destroy (GScanner *scanner);
2096 void g_scanner_input_file (GScanner *scanner,
2098 void g_scanner_sync_file_offset (GScanner *scanner);
2099 void g_scanner_input_text (GScanner *scanner,
2102 GTokenType g_scanner_get_next_token (GScanner *scanner);
2103 GTokenType g_scanner_peek_next_token (GScanner *scanner);
2104 GTokenType g_scanner_cur_token (GScanner *scanner);
2105 GTokenValue g_scanner_cur_value (GScanner *scanner);
2106 guint g_scanner_cur_line (GScanner *scanner);
2107 guint g_scanner_cur_position (GScanner *scanner);
2108 gboolean g_scanner_eof (GScanner *scanner);
2109 guint g_scanner_set_scope (GScanner *scanner,
2111 void g_scanner_scope_add_symbol (GScanner *scanner,
2113 const gchar *symbol,
2115 void g_scanner_scope_remove_symbol (GScanner *scanner,
2117 const gchar *symbol);
2118 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
2120 const gchar *symbol);
2121 void g_scanner_scope_foreach_symbol (GScanner *scanner,
2124 gpointer user_data);
2125 gpointer g_scanner_lookup_symbol (GScanner *scanner,
2126 const gchar *symbol);
2127 void g_scanner_freeze_symbol_table (GScanner *scanner);
2128 void g_scanner_thaw_symbol_table (GScanner *scanner);
2129 void g_scanner_unexp_token (GScanner *scanner,
2130 GTokenType expected_token,
2131 const gchar *identifier_spec,
2132 const gchar *symbol_spec,
2133 const gchar *symbol_name,
2134 const gchar *message,
2136 void g_scanner_error (GScanner *scanner,
2137 const gchar *format,
2138 ...) G_GNUC_PRINTF (2,3);
2139 void g_scanner_warn (GScanner *scanner,
2140 const gchar *format,
2141 ...) G_GNUC_PRINTF (2,3);
2142 gint g_scanner_stat_mode (const gchar *filename);
2143 /* keep downward source compatibility */
2144 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2145 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2147 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2148 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2150 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2151 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2161 GCompletionFunc func;
2167 GCompletion* g_completion_new (GCompletionFunc func);
2168 void g_completion_add_items (GCompletion* cmp,
2170 void g_completion_remove_items (GCompletion* cmp,
2172 void g_completion_clear_items (GCompletion* cmp);
2173 GList* g_completion_complete (GCompletion* cmp,
2175 gchar** new_prefix);
2176 void g_completion_free (GCompletion* cmp);
2181 * Date calculations (not time for now, to be resolved). These are a
2182 * mutant combination of Steffen Beyer's DateCalc routines
2183 * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2184 * date routines (written for in-house software). Written by Havoc
2185 * Pennington <hp@pobox.com>
2188 typedef guint16 GDateYear;
2189 typedef guint8 GDateDay; /* day of the month */
2190 typedef struct _GDate GDate;
2191 /* make struct tm known without having to include time.h */
2194 /* enum used to specify order of appearance in parsed date strings */
2202 /* actual week and month values */
2205 G_DATE_BAD_WEEKDAY = 0,
2208 G_DATE_WEDNESDAY = 3,
2209 G_DATE_THURSDAY = 4,
2211 G_DATE_SATURDAY = 6,
2216 G_DATE_BAD_MONTH = 0,
2218 G_DATE_FEBRUARY = 2,
2225 G_DATE_SEPTEMBER = 9,
2226 G_DATE_OCTOBER = 10,
2227 G_DATE_NOVEMBER = 11,
2228 G_DATE_DECEMBER = 12
2231 #define G_DATE_BAD_JULIAN 0U
2232 #define G_DATE_BAD_DAY 0U
2233 #define G_DATE_BAD_YEAR 0U
2235 /* Note: directly manipulating structs is generally a bad idea, but
2236 * in this case it's an *incredibly* bad idea, because all or part
2237 * of this struct can be invalid at any given time. Use the functions,
2238 * or you will get hosed, I promise.
2242 guint julian_days : 32; /* julian days representation - we use a
2243 * bitfield hoping that 64 bit platforms
2244 * will pack this whole struct in one big
2248 guint julian : 1; /* julian is valid */
2249 guint dmy : 1; /* dmy is valid */
2251 /* DMY representation */
2257 /* g_date_new() returns an invalid date, you then have to _set() stuff
2258 * to get a usable object. You can also allocate a GDate statically,
2259 * then call g_date_clear() to initialize.
2261 GDate* g_date_new (void);
2262 GDate* g_date_new_dmy (GDateDay day,
2265 GDate* g_date_new_julian (guint32 julian_day);
2266 void g_date_free (GDate *date);
2268 /* check g_date_valid() after doing an operation that might fail, like
2269 * _parse. Almost all g_date operations are undefined on invalid
2270 * dates (the exceptions are the mutators, since you need those to
2271 * return to validity).
2273 gboolean g_date_valid (GDate *date);
2274 gboolean g_date_valid_day (GDateDay day);
2275 gboolean g_date_valid_month (GDateMonth month);
2276 gboolean g_date_valid_year (GDateYear year);
2277 gboolean g_date_valid_weekday (GDateWeekday weekday);
2278 gboolean g_date_valid_julian (guint32 julian_date);
2279 gboolean g_date_valid_dmy (GDateDay day,
2283 GDateWeekday g_date_weekday (GDate *date);
2284 GDateMonth g_date_month (GDate *date);
2285 GDateYear g_date_year (GDate *date);
2286 GDateDay g_date_day (GDate *date);
2287 guint32 g_date_julian (GDate *date);
2288 guint g_date_day_of_year (GDate *date);
2290 /* First monday/sunday is the start of week 1; if we haven't reached
2291 * that day, return 0. These are not ISO weeks of the year; that
2292 * routine needs to be added.
2293 * these functions return the number of weeks, starting on the
2296 guint g_date_monday_week_of_year (GDate *date);
2297 guint g_date_sunday_week_of_year (GDate *date);
2299 /* If you create a static date struct you need to clear it to get it
2300 * in a sane state before use. You can clear a whole array at
2301 * once with the ndates argument.
2303 void g_date_clear (GDate *date,
2306 /* The parse routine is meant for dates typed in by a user, so it
2307 * permits many formats but tries to catch common typos. If your data
2308 * needs to be strictly validated, it is not an appropriate function.
2310 void g_date_set_parse (GDate *date,
2312 void g_date_set_time (GDate *date,
2314 void g_date_set_month (GDate *date,
2316 void g_date_set_day (GDate *date,
2318 void g_date_set_year (GDate *date,
2320 void g_date_set_dmy (GDate *date,
2324 void g_date_set_julian (GDate *date,
2325 guint32 julian_date);
2326 gboolean g_date_is_first_of_month (GDate *date);
2327 gboolean g_date_is_last_of_month (GDate *date);
2329 /* To go forward by some number of weeks just go forward weeks*7 days */
2330 void g_date_add_days (GDate *date,
2332 void g_date_subtract_days (GDate *date,
2335 /* If you add/sub months while day > 28, the day might change */
2336 void g_date_add_months (GDate *date,
2338 void g_date_subtract_months (GDate *date,
2341 /* If it's feb 29, changing years can move you to the 28th */
2342 void g_date_add_years (GDate *date,
2344 void g_date_subtract_years (GDate *date,
2346 gboolean g_date_is_leap_year (GDateYear year);
2347 guint8 g_date_days_in_month (GDateMonth month,
2349 guint8 g_date_monday_weeks_in_year (GDateYear year);
2350 guint8 g_date_sunday_weeks_in_year (GDateYear year);
2352 /* qsort-friendly (with a cast...) */
2353 gint g_date_compare (GDate *lhs,
2355 void g_date_to_struct_tm (GDate *date,
2358 /* Just like strftime() except you can only use date-related formats.
2359 * Using a time format is undefined.
2361 gsize g_date_strftime (gchar *s,
2363 const gchar *format,
2369 * Indexed Relations. Imagine a really simple table in a
2370 * database. Relations are not ordered. This data type is meant for
2371 * maintaining a N-way mapping.
2373 * g_relation_new() creates a relation with FIELDS fields
2375 * g_relation_destroy() frees all resources
2376 * g_tuples_destroy() frees the result of g_relation_select()
2378 * g_relation_index() indexes relation FIELD with the provided
2379 * equality and hash functions. this must be done before any
2380 * calls to insert are made.
2382 * g_relation_insert() inserts a new tuple. you are expected to
2383 * provide the right number of fields.
2385 * g_relation_delete() deletes all relations with KEY in FIELD
2386 * g_relation_select() returns ...
2387 * g_relation_count() counts ...
2390 GRelation* g_relation_new (gint fields);
2391 void g_relation_destroy (GRelation *relation);
2392 void g_relation_index (GRelation *relation,
2394 GHashFunc hash_func,
2395 GCompareFunc key_compare_func);
2396 void g_relation_insert (GRelation *relation,
2398 gint g_relation_delete (GRelation *relation,
2401 GTuples* g_relation_select (GRelation *relation,
2404 gint g_relation_count (GRelation *relation,
2407 gboolean g_relation_exists (GRelation *relation,
2409 void g_relation_print (GRelation *relation);
2411 void g_tuples_destroy (GTuples *tuples);
2412 gpointer g_tuples_index (GTuples *tuples,
2417 /* GRand - a good and fast random number generator: Mersenne Twister
2418 * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2419 * The range functions return a value in the intervall [min,max).
2420 * int -> [0..2^32-1]
2421 * int_range -> [min..max-1]
2423 * double_range -> [min..max)
2426 GRand* g_rand_new_with_seed (guint32 seed);
2427 GRand* g_rand_new (void);
2428 void g_rand_free (GRand *rand);
2430 void g_rand_set_seed (GRand *rand,
2432 guint32 g_rand_int (GRand *rand);
2433 gint32 g_rand_int_range (GRand *rand,
2436 gdouble g_rand_double (GRand *rand);
2437 gdouble g_rand_double_range (GRand *rand,
2440 /* This might go in, if -lm is no problem for you guys
2441 gdouble g_rand_normal (GRand *rand,
2443 gdouble standard_deviation);
2446 void g_random_set_seed (guint32 seed);
2447 guint32 g_random_int (void);
2448 gint32 g_random_int_range (gint32 min,
2450 gdouble g_random_double (void);
2451 gdouble g_random_double_range (gdouble min,
2454 gdouble g_random_normal (gdouble mean,
2455 gdouble standard_deviation);
2462 /* This function returns prime numbers spaced by approximately 1.5-2.0
2463 * and is for use in resizing data structures which prefer
2464 * prime-valued sizes. The closest spaced prime function returns the
2465 * next largest prime, or the highest it knows about which is about
2468 guint g_spaced_primes_closest (guint num);
2474 typedef struct _GIOFuncs GIOFuncs;
2490 G_IO_IN GLIB_SYSDEF_POLLIN,
2491 G_IO_OUT GLIB_SYSDEF_POLLOUT,
2492 G_IO_PRI GLIB_SYSDEF_POLLPRI,
2493 G_IO_ERR GLIB_SYSDEF_POLLERR,
2494 G_IO_HUP GLIB_SYSDEF_POLLHUP,
2495 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
2500 guint channel_flags;
2505 typedef gboolean (*GIOFunc) (GIOChannel *source,
2506 GIOCondition condition,
2510 GIOError (*io_read) (GIOChannel *channel,
2514 GIOError (*io_write) (GIOChannel *channel,
2517 guint *bytes_written);
2518 GIOError (*io_seek) (GIOChannel *channel,
2521 void (*io_close) (GIOChannel *channel);
2522 guint (*io_add_watch) (GIOChannel *channel,
2524 GIOCondition condition,
2527 GDestroyNotify notify);
2528 void (*io_free) (GIOChannel *channel);
2531 void g_io_channel_init (GIOChannel *channel);
2532 void g_io_channel_ref (GIOChannel *channel);
2533 void g_io_channel_unref (GIOChannel *channel);
2534 GIOError g_io_channel_read (GIOChannel *channel,
2538 GIOError g_io_channel_write (GIOChannel *channel,
2541 guint *bytes_written);
2542 GIOError g_io_channel_seek (GIOChannel *channel,
2545 void g_io_channel_close (GIOChannel *channel);
2546 guint g_io_add_watch_full (GIOChannel *channel,
2548 GIOCondition condition,
2551 GDestroyNotify notify);
2552 guint g_io_add_watch (GIOChannel *channel,
2553 GIOCondition condition,
2555 gpointer user_data);
2560 typedef struct _GTimeVal GTimeVal;
2561 typedef struct _GSourceFuncs GSourceFuncs;
2562 typedef struct _GMainLoop GMainLoop; /* Opaque */
2569 struct _GSourceFuncs
2571 gboolean (*prepare) (gpointer source_data,
2572 GTimeVal *current_time,
2574 gboolean (*check) (gpointer source_data,
2575 GTimeVal *current_time);
2576 gboolean (*dispatch) (gpointer source_data,
2577 GTimeVal *current_time,
2578 gpointer user_data);
2579 GDestroyNotify destroy;
2582 /* Standard priorities */
2584 #define G_PRIORITY_HIGH -100
2585 #define G_PRIORITY_DEFAULT 0
2586 #define G_PRIORITY_HIGH_IDLE 100
2587 #define G_PRIORITY_DEFAULT_IDLE 200
2588 #define G_PRIORITY_LOW 300
2590 typedef gboolean (*GSourceFunc) (gpointer data);
2592 /* Hooks for adding to the main loop */
2593 guint g_source_add (gint priority,
2594 gboolean can_recurse,
2595 GSourceFuncs *funcs,
2596 gpointer source_data,
2598 GDestroyNotify notify);
2599 gboolean g_source_remove (guint tag);
2600 gboolean g_source_remove_by_user_data (gpointer user_data);
2601 gboolean g_source_remove_by_source_data (gpointer source_data);
2602 gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2603 gpointer user_data);
2605 void g_get_current_time (GTimeVal *result);
2607 /* Running the main loop */
2608 GMainLoop* g_main_new (gboolean is_running);
2609 void g_main_run (GMainLoop *loop);
2610 void g_main_quit (GMainLoop *loop);
2611 void g_main_destroy (GMainLoop *loop);
2612 gboolean g_main_is_running (GMainLoop *loop);
2614 /* Run a single iteration of the mainloop. If block is FALSE,
2617 gboolean g_main_iteration (gboolean may_block);
2619 /* See if any events are pending */
2620 gboolean g_main_pending (void);
2622 /* Idles and timeouts */
2623 guint g_timeout_add_full (gint priority,
2625 GSourceFunc function,
2627 GDestroyNotify notify);
2628 guint g_timeout_add (guint interval,
2629 GSourceFunc function,
2631 guint g_idle_add (GSourceFunc function,
2633 guint g_idle_add_full (gint priority,
2634 GSourceFunc function,
2636 GDestroyNotify destroy);
2637 gboolean g_idle_remove_by_data (gpointer data);
2641 * System-specific IO and main loop calls
2643 * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2644 * descriptor as provided by the C runtime) that can be used by
2645 * MsgWaitForMultipleObjects. This does *not* include file handles
2646 * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2647 * WSAEventSelect to signal events when a SOCKET is readable).
2649 * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2650 * indicate polling for messages. These message queue GPollFDs should
2651 * be added with the g_main_poll_win32_msg_add function.
2653 * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2654 * (GTK) programs, as GDK itself wants to read messages and convert them
2657 * So, unless you really know what you are doing, it's best not to try
2658 * to use the main loop polling stuff for your own needs on
2659 * Win32. It's really only written for the GIMP's needs so
2663 typedef struct _GPollFD GPollFD;
2664 typedef gint (*GPollFunc) (GPollFD *ufds,
2674 void g_main_add_poll (GPollFD *fd,
2676 void g_main_remove_poll (GPollFD *fd);
2677 void g_main_set_poll_func (GPollFunc func);
2679 /* On Unix, IO channels created with this function for any file
2680 * descriptor or socket.
2682 * On Win32, use this only for plain files opened with the MSVCRT (the
2683 * Microsoft run-time C library) _open(), including file descriptors
2684 * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2685 * Actually, don't do even that, this code isn't done yet.
2687 * The term file descriptor as used in the context of Win32 refers to
2688 * the emulated Unix-like file descriptors MSVCRT provides.
2690 GIOChannel* g_io_channel_unix_new (int fd);
2691 gint g_io_channel_unix_get_fd (GIOChannel *channel);
2695 GUTILS_C_VAR guint g_pipe_readable_msg;
2697 #define G_WIN32_MSG_HANDLE 19981206
2699 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2700 * should *not* use this. (In fact, I can't think of any program that
2701 * would want to use this, but it's here just for completeness's sake.
2703 void g_main_poll_win32_msg_add(gint priority,
2707 /* An IO channel for Windows messages for window handle hwnd. */
2708 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2710 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2711 * _pipe(), with no mechanism for the writer to tell the reader when
2712 * there is data in the pipe.
2714 * This is not really implemented yet.
2716 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2718 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2719 * Windows user messages used to signal data in the pipe for the
2722 * fd is the file descriptor. For the write end, peer is the thread id
2723 * of the reader, and peer_fd is his file descriptor for the read end
2726 * This is used by the GIMP, and works.
2728 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int fd,
2732 void g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2736 void g_io_channel_win32_pipe_readable (int fd,
2739 /* Get the C runtime file descriptor of a channel. */
2740 gint g_io_channel_win32_get_fd (GIOChannel *channel);
2742 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2743 * actually a SOCKET.
2745 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2749 /* Windows emulation stubs for common Unix functions
2752 # define MAXPATHLEN 1024
2758 /* These POSIXish functions are available in the Microsoft C library
2759 * prefixed with underscore (which of course technically speaking is
2760 * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2761 * prevents Microsoft from practically requiring you to include
2762 * <windows.h> every now and then...).
2764 * You still need to include the appropriate headers to get the
2765 * prototypes, <io.h> or <direct.h>.
2767 * For some functions, we provide emulators in glib, which are prefixed
2770 # define getcwd _getcwd
2771 # define getpid _getpid
2772 # define access _access
2778 # define write _write
2779 # define lseek _lseek
2780 # define close _close
2781 # define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2782 # define popen _popen
2783 # define pclose _pclose
2784 # define fdopen _fdopen
2785 # define ftruncate(fd, size) gwin_ftruncate (fd, size)
2786 # define opendir gwin_opendir
2787 # define readdir gwin_readdir
2788 # define rewinddir gwin_rewinddir
2789 # define closedir gwin_closedir
2790 # define NAME_MAX 255
2794 gboolean just_opened;
2795 guint find_file_handle;
2796 gpointer find_file_data;
2798 typedef struct DIR DIR;
2801 gchar d_name[NAME_MAX + 1];
2803 /* emulation functions */
2804 extern int gwin_ftruncate (gint f,
2806 DIR* gwin_opendir (const gchar *dirname);
2807 struct dirent* gwin_readdir (DIR *dir);
2808 void gwin_rewinddir (DIR *dir);
2809 gint gwin_closedir (DIR *dir);
2810 #endif /* NATIVE_WIN32 */
2813 /* GLib Thread support
2815 typedef struct _GMutex GMutex;
2816 typedef struct _GCond GCond;
2817 typedef struct _GPrivate GPrivate;
2818 typedef struct _GStaticPrivate GStaticPrivate;
2819 typedef struct _GThreadFunctions GThreadFunctions;
2820 struct _GThreadFunctions
2822 GMutex* (*mutex_new) (void);
2823 void (*mutex_lock) (GMutex *mutex);
2824 gboolean (*mutex_trylock) (GMutex *mutex);
2825 void (*mutex_unlock) (GMutex *mutex);
2826 void (*mutex_free) (GMutex *mutex);
2827 GCond* (*cond_new) (void);
2828 void (*cond_signal) (GCond *cond);
2829 void (*cond_broadcast) (GCond *cond);
2830 void (*cond_wait) (GCond *cond,
2832 gboolean (*cond_timed_wait) (GCond *cond,
2834 GTimeVal *end_time);
2835 void (*cond_free) (GCond *cond);
2836 GPrivate* (*private_new) (GDestroyNotify destructor);
2837 gpointer (*private_get) (GPrivate *private_key);
2838 void (*private_set) (GPrivate *private_key,
2842 GUTILS_C_VAR GThreadFunctions g_thread_functions_for_glib_use;
2843 GUTILS_C_VAR gboolean g_thread_use_default_impl;
2844 GUTILS_C_VAR gboolean g_threads_got_initialized;
2846 /* initializes the mutex/cond/private implementation for glib, might
2847 * only be called once, and must not be called directly or indirectly
2848 * from another glib-function, e.g. as a callback.
2850 void g_thread_init (GThreadFunctions *vtable);
2852 /* internal function for fallback static mutex implementation */
2853 GMutex* g_static_mutex_get_mutex_impl (GMutex **mutex);
2855 /* shorthands for conditional and unconditional function calls */
2856 #define G_THREAD_UF(name, arglist) \
2857 (*g_thread_functions_for_glib_use . name) arglist
2858 #define G_THREAD_CF(name, fail, arg) \
2859 (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2860 /* keep in mind, all those mutexes and static mutexes are not
2861 * recursive in general, don't rely on that
2863 #define g_thread_supported() (g_threads_got_initialized)
2864 #define g_mutex_new() G_THREAD_UF (mutex_new, ())
2865 #define g_mutex_lock(mutex) G_THREAD_CF (mutex_lock, (void)0, (mutex))
2866 #define g_mutex_trylock(mutex) G_THREAD_CF (mutex_trylock, TRUE, (mutex))
2867 #define g_mutex_unlock(mutex) G_THREAD_CF (mutex_unlock, (void)0, (mutex))
2868 #define g_mutex_free(mutex) G_THREAD_CF (mutex_free, (void)0, (mutex))
2869 #define g_cond_new() G_THREAD_UF (cond_new, ())
2870 #define g_cond_signal(cond) G_THREAD_CF (cond_signal, (void)0, (cond))
2871 #define g_cond_broadcast(cond) G_THREAD_CF (cond_broadcast, (void)0, (cond))
2872 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait, (void)0, (cond, \
2874 #define g_cond_free(cond) G_THREAD_CF (cond_free, (void)0, (cond))
2875 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2879 #define g_private_new(destructor) G_THREAD_UF (private_new, (destructor))
2880 #define g_private_get(private_key) G_THREAD_CF (private_get, \
2881 ((gpointer)private_key), \
2883 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2884 (void) (private_key = \
2885 (GPrivate*) (value)), \
2886 (private_key, value))
2887 /* GStaticMutexes can be statically initialized with the value
2888 * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2889 * much easier, than having to explicitly allocate the mutex before
2892 #define g_static_mutex_lock(mutex) \
2893 g_mutex_lock (g_static_mutex_get_mutex (mutex))
2894 #define g_static_mutex_trylock(mutex) \
2895 g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2896 #define g_static_mutex_unlock(mutex) \
2897 g_mutex_unlock (g_static_mutex_get_mutex (mutex))
2898 struct _GStaticPrivate
2902 #define G_STATIC_PRIVATE_INIT { 0 }
2903 gpointer g_static_private_get (GStaticPrivate *private_key);
2904 void g_static_private_set (GStaticPrivate *private_key,
2906 GDestroyNotify notify);
2908 /* these are some convenience macros that expand to nothing if GLib
2909 * was configured with --disable-threads. for using StaticMutexes,
2910 * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
2911 * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
2912 * declare such an globally defined lock. name is a unique identifier
2913 * for the protected varibale or code portion. locking, testing and
2914 * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
2915 * G_TRYLOCK() respectively.
2917 extern void glib_dummy_decl (void);
2918 #define G_LOCK_NAME(name) (g__ ## name ## _lock)
2919 #ifdef G_THREADS_ENABLED
2920 # define G_LOCK_DEFINE_STATIC(name) static G_LOCK_DEFINE (name)
2921 # define G_LOCK_DEFINE(name) \
2922 GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT
2923 # define G_LOCK_EXTERN(name) extern GStaticMutex G_LOCK_NAME (name)
2925 # ifdef G_DEBUG_LOCKS
2926 # define G_LOCK(name) G_STMT_START{ \
2927 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2928 "file %s: line %d (%s): locking: %s ", \
2929 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2931 g_static_mutex_lock (&G_LOCK_NAME (name)); \
2933 # define G_UNLOCK(name) G_STMT_START{ \
2934 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2935 "file %s: line %d (%s): unlocking: %s ", \
2936 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2938 g_static_mutex_unlock (&G_LOCK_NAME (name)); \
2940 # define G_TRYLOCK(name) G_STMT_START{ \
2941 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, \
2942 "file %s: line %d (%s): try locking: %s ", \
2943 __FILE__, __LINE__, G_GNUC_PRETTY_FUNCTION, \
2945 }G_STMT_END, g_static_mutex_trylock (&G_LOCK_NAME (name))
2946 # else /* !G_DEBUG_LOCKS */
2947 # define G_LOCK(name) g_static_mutex_lock (&G_LOCK_NAME (name))
2948 # define G_UNLOCK(name) g_static_mutex_unlock (&G_LOCK_NAME (name))
2949 # define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
2950 # endif /* !G_DEBUG_LOCKS */
2951 #else /* !G_THREADS_ENABLED */
2952 # define G_LOCK_DEFINE_STATIC(name) extern void glib_dummy_decl (void)
2953 # define G_LOCK_DEFINE(name) extern void glib_dummy_decl (void)
2954 # define G_LOCK_EXTERN(name) extern void glib_dummy_decl (void)
2955 # define G_LOCK(name)
2956 # define G_UNLOCK(name)
2957 # define G_TRYLOCK(name) (FALSE)
2958 #endif /* !G_THREADS_ENABLED */
2962 #endif /* __cplusplus */
2965 #endif /* __G_LIB_H__ */