Add a cast.
[platform/upstream/glib.git] / glib.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #ifndef __G_LIB_H__
28 #define __G_LIB_H__
29
30 /* system specific config file glibconfig.h provides definitions for
31  * the extrema of many of the standard types. These are:
32  *
33  *  G_MINSHORT, G_MAXSHORT
34  *  G_MININT, G_MAXINT
35  *  G_MINLONG, G_MAXLONG
36  *  G_MINFLOAT, G_MAXFLOAT
37  *  G_MINDOUBLE, G_MAXDOUBLE
38  *
39  * It also provides the following typedefs:
40  *
41  *  gint8, guint8
42  *  gint16, guint16
43  *  gint32, guint32
44  *  gint64, guint64
45  *
46  * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
47  * this file). 
48  *
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.
51  *
52  *  GINT_TO_POINTER (i), GUINT_TO_POINTER (i)
53  *  GPOINTER_TO_INT (p), GPOINTER_TO_UINT (p)
54  *
55  * Finally, it provides the following wrappers to STDC functions:
56  *
57  *  void g_memmove (gpointer dest, gconstpointer void *src, gulong count);
58  *    A wrapper for STDC memmove, or an implementation, if memmove doesn't
59  *    exist.  The prototype looks like the above, give or take a const,
60  *    or size_t.
61  */
62 #include <glibconfig.h>
63
64 /* include varargs functions for assertment macros
65  */
66 #include <stdarg.h>
67
68 /* optionally feature DMALLOC memory allocation debugger
69  */
70 #ifdef USE_DMALLOC
71 #include "dmalloc.h"
72 #endif
73
74
75 #ifdef NATIVE_WIN32
76
77 /* On native Win32, directory separator is the backslash, and search path
78  * separator is the semicolon.
79  */
80 #define G_DIR_SEPARATOR '\\'
81 #define G_DIR_SEPARATOR_S "\\"
82 #define G_SEARCHPATH_SEPARATOR ';'
83 #define G_SEARCHPATH_SEPARATOR_S ";"
84
85 #else  /* !NATIVE_WIN32 */
86
87 /* Unix */
88
89 #define G_DIR_SEPARATOR '/'
90 #define G_DIR_SEPARATOR_S "/"
91 #define G_SEARCHPATH_SEPARATOR ':'
92 #define G_SEARCHPATH_SEPARATOR_S ":"
93
94 #endif /* !NATIVE_WIN32 */
95
96 #ifdef __cplusplus
97 extern "C" {
98 #endif /* __cplusplus */
99
100
101 /* Provide definitions for some commonly used macros.
102  *  Some of them are only provided if they haven't already
103  *  been defined. It is assumed that if they are already
104  *  defined then the current definition is correct.
105  */
106 #ifndef NULL
107 #define NULL    ((void*) 0)
108 #endif
109
110 #ifndef FALSE
111 #define FALSE   (0)
112 #endif
113
114 #ifndef TRUE
115 #define TRUE    (!FALSE)
116 #endif
117
118 #undef  MAX
119 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
120
121 #undef  MIN
122 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
123
124 #undef  ABS
125 #define ABS(a)     (((a) < 0) ? -(a) : (a))
126
127 #undef  CLAMP
128 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
129
130
131 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
132  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
133  */
134 #if !defined (G_VA_COPY)
135 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
136 #  define G_VA_COPY(ap1, ap2)     (*(ap1) = *(ap2))
137 #  elif defined (G_VA_COPY_AS_ARRAY)
138 #  define G_VA_COPY(ap1, ap2)     g_memmove ((ap1), (ap2), sizeof (va_list))
139 #  else /* va_list is a pointer */
140 #  define G_VA_COPY(ap1, ap2)     ((ap1) = (ap2))
141 #  endif /* va_list is a pointer */
142 #endif /* !G_VA_COPY */
143
144
145 /* Provide convenience macros for handling structure
146  * fields through their offsets.
147  */
148 #define G_STRUCT_OFFSET(struct_type, member)    \
149     ((gulong) ((gchar*) &((struct_type*) 0)->member))
150 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
151     ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
152 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
153     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
154
155
156 /* inlining hassle. for compilers that don't allow the `inline' keyword,
157  * mostly because of strict ANSI C compliance or dumbness, we try to fall
158  * back to either `__inline__' or `__inline'.
159  * we define G_CAN_INLINE, if the compiler seems to be actually
160  * *capable* to do function inlining, in which case inline function bodys
161  * do make sense. we also define G_INLINE_FUNC to properly export the
162  * function prototypes if no inlining can be performed.
163  * we special case most of the stuff, so inline functions can have a normal
164  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
165  */
166 #ifndef G_INLINE_FUNC
167 #  define G_CAN_INLINE 1
168 #endif
169 #ifdef G_HAVE_INLINE
170 #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
171 #    undef inline
172 #    define inline __inline__
173 #  endif
174 #else /* !G_HAVE_INLINE */
175 #  undef inline
176 #  if defined (G_HAVE___INLINE__)
177 #    define inline __inline__
178 #  else /* !inline && !__inline__ */
179 #    if defined (G_HAVE___INLINE)
180 #      define inline __inline
181 #    else /* !inline && !__inline__ && !__inline */
182 #      define inline /* don't inline, then */
183 #      ifndef G_INLINE_FUNC
184 #        undef G_CAN_INLINE
185 #      endif
186 #    endif
187 #  endif
188 #endif
189 #ifndef G_INLINE_FUNC
190 #  ifdef __GNUC__
191 #    ifdef __OPTIMIZE__
192 #      define G_INLINE_FUNC extern inline
193 #    else
194 #      undef G_CAN_INLINE
195 #      define G_INLINE_FUNC extern
196 #    endif
197 #  else /* !__GNUC__ */
198 #    ifdef G_CAN_INLINE
199 #      define G_INLINE_FUNC static inline
200 #    else
201 #      define G_INLINE_FUNC extern
202 #    endif
203 #  endif /* !__GNUC__ */
204 #endif /* !G_INLINE_FUNC */
205
206
207 /* Provide simple macro statement wrappers (adapted from Perl):
208  *  G_STMT_START { statements; } G_STMT_END;
209  *  can be used as a single statement, as in
210  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
211  *
212  *  For gcc we will wrap the statements within `({' and `})' braces.
213  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
214  *  and otherwise within `do' and `while (0)'.
215  */
216 #if !(defined (G_STMT_START) && defined (G_STMT_END))
217 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
218 #    define G_STMT_START        (void)(
219 #    define G_STMT_END          )
220 #  else
221 #    if (defined (sun) || defined (__sun__))
222 #      define G_STMT_START      if (1)
223 #      define G_STMT_END        else (void)0
224 #    else
225 #      define G_STMT_START      do
226 #      define G_STMT_END        while (0)
227 #    endif
228 #  endif
229 #endif
230
231
232 /* Provide macros to feature the GCC function attribute.
233  */
234 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
235 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
236   __attribute__((format (printf, format_idx, arg_idx)))
237 #define G_GNUC_SCANF( format_idx, arg_idx )     \
238   __attribute__((format (scanf, format_idx, arg_idx)))
239 #define G_GNUC_FORMAT( arg_idx )                \
240   __attribute__((format_arg (arg_idx)))
241 #define G_GNUC_NORETURN                         \
242   __attribute__((noreturn))
243 #define G_GNUC_CONST                            \
244   __attribute__((const))
245 #define G_GNUC_UNUSED                           \
246   __attribute__((unused))
247 #else   /* !__GNUC__ */
248 #define G_GNUC_PRINTF( format_idx, arg_idx )
249 #define G_GNUC_SCANF( format_idx, arg_idx )
250 #define G_GNUC_FORMAT( arg_idx )
251 #define G_GNUC_NORETURN
252 #define G_GNUC_CONST
253 #define G_GNUC_UNUSED
254 #endif  /* !__GNUC__ */
255
256
257 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
258  * macros, so we can refer to them as strings unconditionally.
259  */
260 #ifdef  __GNUC__
261 #define G_GNUC_FUNCTION         __FUNCTION__
262 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
263 #else   /* !__GNUC__ */
264 #define G_GNUC_FUNCTION         ""
265 #define G_GNUC_PRETTY_FUNCTION  ""
266 #endif  /* !__GNUC__ */
267
268 /* we try to provide a usefull equivalent for ATEXIT if it is
269  * not defined, but use is actually abandoned. people should
270  * use g_atexit() instead.
271  */
272 #ifndef ATEXIT
273 # define ATEXIT(proc)   g_ATEXIT(proc)
274 #else
275 # define G_NATIVE_ATEXIT
276 #endif /* ATEXIT */
277
278 /* Hacker macro to place breakpoints for elected machines.
279  * Actual use is strongly deprecated of course ;)
280  */
281 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
282 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
283 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
284 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
285 #else   /* !__i386__ && !__alpha__ */
286 #define G_BREAKPOINT()
287 #endif  /* __i386__ */
288
289
290 /* Provide macros for easily allocating memory. The macros
291  *  will cast the allocated memory to the specified type
292  *  in order to avoid compiler warnings. (Makes the code neater).
293  */
294
295 #ifdef __DMALLOC_H__
296 #  define g_new(type, count)            (ALLOC (type, count))
297 #  define g_new0(type, count)           (CALLOC (type, count))
298 #  define g_renew(type, mem, count)     (REALLOC (mem, type, count))
299 #else /* __DMALLOC_H__ */
300 #  define g_new(type, count)      \
301       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
302 #  define g_new0(type, count)     \
303       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
304 #  define g_renew(type, mem, count)       \
305       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
306 #endif /* __DMALLOC_H__ */
307
308 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
309   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
310                    sizeof (type), \
311                    sizeof (type) * (pre_alloc), \
312                    (alloc_type)) \
313 )
314 #define g_chunk_new(type, chunk)        ( \
315   (type *) g_mem_chunk_alloc (chunk) \
316 )
317 #define g_chunk_new0(type, chunk)       ( \
318   (type *) g_mem_chunk_alloc0 (chunk) \
319 )
320 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
321   g_mem_chunk_free ((mem_chunk), (mem)); \
322 } G_STMT_END
323
324
325 #define g_string(x) #x
326
327
328 /* Provide macros for error handling. The "assert" macros will
329  *  exit on failure. The "return" macros will exit the current
330  *  function. Two different definitions are given for the macros
331  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
332  *  __PRETTY_FUNCTION__ capability.
333  */
334
335 #ifdef G_DISABLE_ASSERT
336
337 #define g_assert(expr)
338 #define g_assert_not_reached()
339
340 #else /* !G_DISABLE_ASSERT */
341
342 #ifdef __GNUC__
343
344 #define g_assert(expr)                  G_STMT_START{           \
345      if (!(expr))                                               \
346        g_log (G_LOG_DOMAIN,                                     \
347               G_LOG_LEVEL_ERROR,                                \
348               "file %s: line %d (%s): assertion failed: (%s)",  \
349               __FILE__,                                         \
350               __LINE__,                                         \
351               __PRETTY_FUNCTION__,                              \
352               #expr);                   }G_STMT_END
353
354 #define g_assert_not_reached()          G_STMT_START{           \
355      g_log (G_LOG_DOMAIN,                                       \
356             G_LOG_LEVEL_ERROR,                                  \
357             "file %s: line %d (%s): should not be reached",     \
358             __FILE__,                                           \
359             __LINE__,                                           \
360             __PRETTY_FUNCTION__);       }G_STMT_END
361
362 #else /* !__GNUC__ */
363
364 #define g_assert(expr)                  G_STMT_START{           \
365      if (!(expr))                                               \
366        g_log (G_LOG_DOMAIN,                                     \
367               G_LOG_LEVEL_ERROR,                                \
368               "file %s: line %d: assertion failed: (%s)",       \
369               __FILE__,                                         \
370               __LINE__,                                         \
371               #expr);                   }G_STMT_END
372
373 #define g_assert_not_reached()          G_STMT_START{   \
374      g_log (G_LOG_DOMAIN,                               \
375             G_LOG_LEVEL_ERROR,                          \
376             "file %s: line %d: should not be reached",  \
377             __FILE__,                                   \
378             __LINE__);          }G_STMT_END
379
380 #endif /* __GNUC__ */
381
382 #endif /* !G_DISABLE_ASSERT */
383
384
385 #ifdef G_DISABLE_CHECKS
386
387 #define g_return_if_fail(expr)
388 #define g_return_val_if_fail(expr,val)
389
390 #else /* !G_DISABLE_CHECKS */
391
392 #ifdef __GNUC__
393
394 #define g_return_if_fail(expr)          G_STMT_START{                   \
395      if (!(expr))                                                       \
396        {                                                                \
397          g_log (G_LOG_DOMAIN,                                           \
398                 G_LOG_LEVEL_CRITICAL,                                   \
399                 "file %s: line %d (%s): assertion `%s' failed.",        \
400                 __FILE__,                                               \
401                 __LINE__,                                               \
402                 __PRETTY_FUNCTION__,                                    \
403                 #expr);                                                 \
404          return;                                                        \
405        };                               }G_STMT_END
406
407 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
408      if (!(expr))                                                       \
409        {                                                                \
410          g_log (G_LOG_DOMAIN,                                           \
411                 G_LOG_LEVEL_CRITICAL,                                   \
412                 "file %s: line %d (%s): assertion `%s' failed.",        \
413                 __FILE__,                                               \
414                 __LINE__,                                               \
415                 __PRETTY_FUNCTION__,                                    \
416                 #expr);                                                 \
417          return val;                                                    \
418        };                               }G_STMT_END
419
420 #else /* !__GNUC__ */
421
422 #define g_return_if_fail(expr)          G_STMT_START{           \
423      if (!(expr))                                               \
424        {                                                        \
425          g_log (G_LOG_DOMAIN,                                   \
426                 G_LOG_LEVEL_CRITICAL,                           \
427                 "file %s: line %d: assertion `%s' failed.",     \
428                 __FILE__,                                       \
429                 __LINE__,                                       \
430                 #expr);                                         \
431          return;                                                \
432        };                               }G_STMT_END
433
434 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
435      if (!(expr))                                               \
436        {                                                        \
437          g_log (G_LOG_DOMAIN,                                   \
438                 G_LOG_LEVEL_CRITICAL,                           \
439                 "file %s: line %d: assertion `%s' failed.",     \
440                 __FILE__,                                       \
441                 __LINE__,                                       \
442                 #expr);                                         \
443          return val;                                            \
444        };                               }G_STMT_END
445
446 #endif /* !__GNUC__ */
447
448 #endif /* !G_DISABLE_CHECKS */
449
450
451 /* Provide type definitions for commonly used types.
452  *  These are useful because a "gint8" can be adjusted
453  *  to be 1 byte (8 bits) on all platforms. Similarly and
454  *  more importantly, "gint32" can be adjusted to be
455  *  4 bytes (32 bits) on all platforms.
456  */
457
458 typedef char   gchar;
459 typedef short  gshort;
460 typedef long   glong;
461 typedef int    gint;
462 typedef gint   gboolean;
463
464 typedef unsigned char   guchar;
465 typedef unsigned short  gushort;
466 typedef unsigned long   gulong;
467 typedef unsigned int    guint;
468
469 #define G_GSHORT_FORMAT  "hi"
470 #define G_GUSHORT_FORMAT "hu"
471 #define G_GINT_FORMAT    "i"
472 #define G_GUINT_FORMAT   "u"
473 #define G_GLONG_FORMAT   "li"
474 #define G_GULONG_FORMAT  "lu"
475
476 typedef float   gfloat;
477 typedef double  gdouble;
478
479 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
480  * Since gldouble isn't used anywhere, just disable it for now */
481
482 #if 0
483 #ifdef HAVE_LONG_DOUBLE
484 typedef long double gldouble;
485 #else /* HAVE_LONG_DOUBLE */
486 typedef double gldouble;
487 #endif /* HAVE_LONG_DOUBLE */
488 #endif /* 0 */
489
490 typedef void* gpointer;
491 typedef const void *gconstpointer;
492
493
494 typedef gint32  gssize;
495 typedef guint32 gsize;
496 typedef guint32 GQuark;
497 typedef gint32  GTime;
498
499
500 /* Portable endian checks and conversions
501  *
502  * glibconfig.h defines G_BYTE_ORDER which expands to one of
503  * the below macros.
504  */
505 #define G_LITTLE_ENDIAN 1234
506 #define G_BIG_ENDIAN    4321
507 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
508
509
510 /* Basic bit swapping functions
511  */
512 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
513     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
514     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
515 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
516     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
517     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
518     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
519     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
520
521 /* Intel specific stuff for speed
522  */
523 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
524 #  define GUINT16_SWAP_LE_BE_X86(val) \
525      (__extension__                                     \
526       ({ register guint16 __v;                          \
527          if (__builtin_constant_p (val))                \
528            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
529          else                                           \
530            __asm__ __const__ ("rorw $8, %w0"            \
531                               : "=r" (__v)              \
532                               : "0" ((guint16) (val))); \
533         __v; }))
534 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
535 #  if !defined(__i486__) && !defined(__i586__) \
536       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
537 #     define GUINT32_SWAP_LE_BE_X86(val) \
538         (__extension__                                          \
539          ({ register guint32 __v;                               \
540             if (__builtin_constant_p (val))                     \
541               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
542           else                                                  \
543             __asm__ __const__ ("rorw $8, %w0\n\t"               \
544                                "rorl $16, %0\n\t"               \
545                                "rorw $8, %w0"                   \
546                                : "=r" (__v)                     \
547                                : "0" ((guint32) (val)));        \
548         __v; }))
549 #  else /* 486 and higher has bswap */
550 #     define GUINT32_SWAP_LE_BE_X86(val) \
551         (__extension__                                          \
552          ({ register guint32 __v;                               \
553             if (__builtin_constant_p (val))                     \
554               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
555           else                                                  \
556             __asm__ __const__ ("bswap %0"                       \
557                                : "=r" (__v)                     \
558                                : "0" ((guint32) (val)));        \
559         __v; }))
560 #  endif /* processor specific 32-bit stuff */
561 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
562 #else /* !__i386__ */
563 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
564 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
565 #endif /* __i386__ */
566
567 #ifdef G_HAVE_GINT64
568 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
569       (((guint64) (val) &                                               \
570         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
571       (((guint64) (val) &                                               \
572         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
573       (((guint64) (val) &                                               \
574         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
575       (((guint64) (val) &                                               \
576         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
577       (((guint64) (val) &                                               \
578         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
579       (((guint64) (val) &                                               \
580         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
581       (((guint64) (val) &                                               \
582         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
583       (((guint64) (val) &                                               \
584         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
585 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
586 #    define GUINT64_SWAP_LE_BE_X86(val) \
587         (__extension__                                          \
588          ({ union { guint64 __ll;                               \
589                     guint32 __l[2]; } __r;                      \
590             if (__builtin_constant_p (val))                     \
591               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
592             else                                                \
593               {                                                 \
594                 union { guint64 __ll;                           \
595                         guint32 __l[2]; } __w;                  \
596                 __w.__ll = ((guint64) val);                     \
597                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
598                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
599               }                                                 \
600           __r.__ll; }))
601 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
602 #  else /* !__i386__ */
603 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
604 #  endif
605 #endif
606
607 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
608 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
609 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
610     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
611     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
612 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
613     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
614     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
615
616 /* The G*_TO_?E() macros are defined in glibconfig.h.
617  * The transformation is symmetric, so the FROM just maps to the TO.
618  */
619 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
620 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
621 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
622 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
623 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
624 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
625 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
626 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
627
628 #ifdef G_HAVE_GINT64
629 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
630 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
631 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
632 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
633 #endif
634
635 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
636 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
637 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
638 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
639
640 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
641 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
642 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
643 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
644
645
646 /* Portable versions of host-network order stuff
647  */
648 #define g_ntohl(val) (GUINT32_FROM_BE (val))
649 #define g_ntohs(val) (GUINT16_FROM_BE (val))
650 #define g_htonl(val) (GUINT32_TO_BE (val))
651 #define g_htons(val) (GUINT16_TO_BE (val))
652
653
654 /* Glib version.
655  * we prefix variable declarations so they can
656  * properly get exported in windows dlls.
657  */
658 #ifdef NATIVE_WIN32
659 #  ifdef GLIB_COMPILATION
660 #    define GUTILS_C_VAR __declspec(dllexport)
661 #  else /* !GLIB_COMPILATION */
662 #    define GUTILS_C_VAR extern __declspec(dllimport)
663 #  endif /* !GLIB_COMPILATION */
664 #else /* !NATIVE_WIN32 */
665 #  define GUTILS_C_VAR extern
666 #endif /* !NATIVE_WIN32 */
667
668 GUTILS_C_VAR const guint glib_major_version;
669 GUTILS_C_VAR const guint glib_minor_version;
670 GUTILS_C_VAR const guint glib_micro_version;
671 GUTILS_C_VAR const guint glib_interface_age;
672 GUTILS_C_VAR const guint glib_binary_age;
673
674 #define GLIB_CHECK_VERSION(major,minor,micro)    \
675     (GLIB_MAJOR_VERSION > (major) || \
676      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
677      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
678       GLIB_MICRO_VERSION >= (micro)))
679
680 /* Forward declarations of glib types.
681  */
682 typedef struct _GAllocator      GAllocator;
683 typedef struct _GArray          GArray;
684 typedef struct _GByteArray      GByteArray;
685 typedef struct _GCache          GCache;
686 typedef struct _GCompletion     GCompletion;
687 typedef struct _GData           GData;
688 typedef struct _GDebugKey       GDebugKey;
689 typedef struct _GHashTable      GHashTable;
690 typedef struct _GHook           GHook;
691 typedef struct _GHookList       GHookList;
692 typedef struct _GList           GList;
693 typedef struct _GMemChunk       GMemChunk;
694 typedef struct _GNode           GNode;
695 typedef struct _GPtrArray       GPtrArray;
696 typedef struct _GQueue          GQueue;
697 typedef struct _GRand           GRand;
698 typedef struct _GRelation       GRelation;
699 typedef struct _GScanner        GScanner;
700 typedef struct _GScannerConfig  GScannerConfig;
701 typedef struct _GSList          GSList;
702 typedef struct _GString         GString;
703 typedef struct _GStringChunk    GStringChunk;
704 typedef struct _GTimer          GTimer;
705 typedef struct _GTrashStack     GTrashStack;
706 typedef struct _GTree           GTree;
707 typedef struct _GTuples         GTuples;
708 typedef union  _GTokenValue     GTokenValue;
709 typedef struct _GIOChannel      GIOChannel;
710
711 /* Tree traverse flags */
712 typedef enum
713 {
714   G_TRAVERSE_LEAFS      = 1 << 0,
715   G_TRAVERSE_NON_LEAFS  = 1 << 1,
716   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
717   G_TRAVERSE_MASK       = 0x03
718 } GTraverseFlags;
719
720 /* Tree traverse orders */
721 typedef enum
722 {
723   G_IN_ORDER,
724   G_PRE_ORDER,
725   G_POST_ORDER,
726   G_LEVEL_ORDER
727 } GTraverseType;
728
729 /* Log level shift offset for user defined
730  * log levels (0-7 are used by GLib).
731  */
732 #define G_LOG_LEVEL_USER_SHIFT  (8)
733
734 /* Glib log levels and flags.
735  */
736 typedef enum
737 {
738   /* log flags */
739   G_LOG_FLAG_RECURSION          = 1 << 0,
740   G_LOG_FLAG_FATAL              = 1 << 1,
741   
742   /* GLib log levels */
743   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
744   G_LOG_LEVEL_CRITICAL          = 1 << 3,
745   G_LOG_LEVEL_WARNING           = 1 << 4,
746   G_LOG_LEVEL_MESSAGE           = 1 << 5,
747   G_LOG_LEVEL_INFO              = 1 << 6,
748   G_LOG_LEVEL_DEBUG             = 1 << 7,
749   
750   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
751 } GLogLevelFlags;
752
753 /* GLib log levels that are considered fatal by default */
754 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
755
756
757 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
758 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
759 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
760 typedef gint            (*GCompareFunc)         (gconstpointer  a,
761                                                  gconstpointer  b);
762 typedef gchar*          (*GCompletionFunc)      (gpointer);
763 typedef void            (*GDestroyNotify)       (gpointer       data);
764 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
765                                                  gpointer       data,
766                                                  gpointer       user_data);
767 typedef void            (*GFunc)                (gpointer       data,
768                                                  gpointer       user_data);
769 typedef guint           (*GHashFunc)            (gconstpointer  key);
770 typedef void            (*GFreeFunc)            (gpointer       data);
771 typedef void            (*GHFunc)               (gpointer       key,
772                                                  gpointer       value,
773                                                  gpointer       user_data);
774 typedef gboolean        (*GHRFunc)              (gpointer       key,
775                                                  gpointer       value,
776                                                  gpointer       user_data);
777 typedef gint            (*GHookCompareFunc)     (GHook          *new_hook,
778                                                  GHook          *sibling);
779 typedef gboolean        (*GHookFindFunc)        (GHook          *hook,
780                                                  gpointer        data);
781 typedef void            (*GHookMarshaller)      (GHook          *hook,
782                                                  gpointer        data);
783 typedef gboolean        (*GHookCheckMarshaller) (GHook          *hook,
784                                                  gpointer        data);
785 typedef void            (*GHookFunc)            (gpointer        data);
786 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
787 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
788                                                  GHook          *hook);
789 typedef void            (*GLogFunc)             (const gchar   *log_domain,
790                                                  GLogLevelFlags log_level,
791                                                  const gchar   *message,
792                                                  gpointer       user_data);
793 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
794                                                  gpointer       data);
795 typedef void            (*GNodeForeachFunc)     (GNode         *node,
796                                                  gpointer       data);
797 typedef gint            (*GSearchFunc)          (gpointer       key,
798                                                  gpointer       data);
799 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
800                                                  gchar         *message,
801                                                  gint           error);
802 typedef gint            (*GTraverseFunc)        (gpointer       key,
803                                                  gpointer       value,
804                                                  gpointer       data);
805 typedef void            (*GVoidFunc)            (void);
806
807
808 struct _GArray
809 {
810   gchar *data;
811   guint len;
812 };
813
814 struct _GByteArray
815 {
816   guint8 *data;
817   guint   len;
818 };
819
820 struct _GDebugKey
821 {
822   gchar *key;
823   guint  value;
824 };
825
826 struct _GList
827 {
828   gpointer data;
829   GList *next;
830   GList *prev;
831 };
832
833 struct _GPtrArray
834 {
835   gpointer *pdata;
836   guint     len;
837 };
838
839 struct _GQueue
840 {
841   GList *head;
842   GList *tail;
843   guint  length;
844 };
845
846 struct _GSList
847 {
848   gpointer data;
849   GSList *next;
850 };
851
852 struct _GString
853 {
854   gchar *str;
855   gint len;
856 };
857
858 struct _GTrashStack
859 {
860   GTrashStack *next;
861 };
862
863 struct _GTuples
864 {
865   guint len;
866 };
867
868
869 /* Doubly linked lists
870  */
871 void   g_list_push_allocator    (GAllocator     *allocator);
872 void   g_list_pop_allocator     (void);
873 GList* g_list_alloc             (void);
874 void   g_list_free              (GList          *list);
875 void   g_list_free_1            (GList          *list);
876 GList* g_list_append            (GList          *list,
877                                  gpointer        data);
878 GList* g_list_prepend           (GList          *list,
879                                  gpointer        data);
880 GList* g_list_insert            (GList          *list,
881                                  gpointer        data,
882                                  gint            position);
883 GList* g_list_insert_sorted     (GList          *list,
884                                  gpointer        data,
885                                  GCompareFunc    func);
886 GList* g_list_concat            (GList          *list1,
887                                  GList          *list2);
888 GList* g_list_remove            (GList          *list,
889                                  gpointer        data);
890 GList* g_list_remove_link       (GList          *list,
891                                  GList          *llink);
892 GList* g_list_delete_link       (GList          *list,
893                                  GList          *link);
894 GList* g_list_reverse           (GList          *list);
895 GList* g_list_copy              (GList          *list);
896 GList* g_list_nth               (GList          *list,
897                                  guint           n);
898 GList* g_list_find              (GList          *list,
899                                  gpointer        data);
900 GList* g_list_find_custom       (GList          *list,
901                                  gpointer        data,
902                                  GCompareFunc    func);
903 gint   g_list_position          (GList          *list,
904                                  GList          *llink);
905 gint   g_list_index             (GList          *list,
906                                  gpointer        data);
907 GList* g_list_last              (GList          *list);
908 GList* g_list_first             (GList          *list);
909 guint  g_list_length            (GList          *list);
910 void   g_list_foreach           (GList          *list,
911                                  GFunc           func,
912                                  gpointer        user_data);
913 GList* g_list_sort              (GList          *list,
914                                  GCompareFunc    compare_func);
915 gpointer g_list_nth_data        (GList          *list,
916                                  guint           n);
917 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
918 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
919
920
921 /* Singly linked lists
922  */
923 void    g_slist_push_allocator  (GAllocator     *allocator);
924 void    g_slist_pop_allocator   (void);
925 GSList* g_slist_alloc           (void);
926 void    g_slist_free            (GSList         *list);
927 void    g_slist_free_1          (GSList         *list);
928 GSList* g_slist_append          (GSList         *list,
929                                  gpointer        data);
930 GSList* g_slist_prepend         (GSList         *list,
931                                  gpointer        data);
932 GSList* g_slist_insert          (GSList         *list,
933                                  gpointer        data,
934                                  gint            position);
935 GSList* g_slist_insert_sorted   (GSList         *list,
936                                  gpointer        data,
937                                  GCompareFunc    func);
938 GSList* g_slist_concat          (GSList         *list1,
939                                  GSList         *list2);
940 GSList* g_slist_remove          (GSList         *list,
941                                  gpointer        data);
942 GSList* g_slist_remove_link     (GSList         *list,
943                                  GSList         *link);
944 GSList* g_slist_delete_link     (GSList         *list,
945                                  GSList         *link);
946 GSList* g_slist_reverse         (GSList         *list);
947 GSList* g_slist_copy            (GSList         *list);
948 GSList* g_slist_nth             (GSList         *list,
949                                  guint           n);
950 GSList* g_slist_find            (GSList         *list,
951                                  gpointer        data);
952 GSList* g_slist_find_custom     (GSList         *list,
953                                  gpointer        data,
954                                  GCompareFunc    func);
955 gint    g_slist_position        (GSList         *list,
956                                  GSList         *llink);
957 gint    g_slist_index           (GSList         *list,
958                                  gpointer        data);
959 GSList* g_slist_last            (GSList         *list);
960 guint   g_slist_length          (GSList         *list);
961 void    g_slist_foreach         (GSList         *list,
962                                  GFunc           func,
963                                  gpointer        user_data);
964 GSList*  g_slist_sort           (GSList          *list,
965                                  GCompareFunc    compare_func);
966 gpointer g_slist_nth_data       (GSList         *list,
967                                  guint           n);
968 #define  g_slist_next(slist)    ((slist) ? (((GSList *)(slist))->next) : NULL)
969
970
971 /* Queues
972  */
973 GQueue*  g_queue_create         (void);
974 void     g_queue_free           (GQueue  *queue);
975 void     g_queue_push_head      (GQueue  *queue,
976                                  gpointer data);
977 void     g_queue_push_tail      (GQueue  *queue,
978                                  gpointer data);
979 gpointer g_queue_pop_head       (GQueue  *queue);
980 gpointer g_queue_pop_tail       (GQueue  *queue);
981 gboolean g_queue_is_empty       (GQueue  *queue);
982 gpointer g_queue_peek_head      (GQueue  *queue);
983 gpointer g_queue_peek_tail      (GQueue  *queue);
984 void     g_queue_push_head_link (GQueue  *queue,
985                                  GList   *link);
986 void     g_queue_push_tail_link (GQueue  *queue,
987                                  GList   *link);
988 GList*   g_queue_pop_head_link  (GQueue  *queue);
989 GList*   g_queue_pop_tail_link  (GQueue  *queue);
990
991
992 /* Hash tables
993  */
994 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
995                                          GCompareFunc    key_compare_func);
996 void        g_hash_table_destroy        (GHashTable     *hash_table);
997 void        g_hash_table_insert         (GHashTable     *hash_table,
998                                          gpointer        key,
999                                          gpointer        value);
1000 void        g_hash_table_remove         (GHashTable     *hash_table,
1001                                          gconstpointer   key);
1002 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
1003                                          gconstpointer   key);
1004 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
1005                                          gconstpointer   lookup_key,
1006                                          gpointer       *orig_key,
1007                                          gpointer       *value);
1008 void        g_hash_table_freeze         (GHashTable     *hash_table);
1009 void        g_hash_table_thaw           (GHashTable     *hash_table);
1010 void        g_hash_table_foreach        (GHashTable     *hash_table,
1011                                          GHFunc          func,
1012                                          gpointer        user_data);
1013 guint       g_hash_table_foreach_remove (GHashTable     *hash_table,
1014                                          GHRFunc         func,
1015                                          gpointer        user_data);
1016 guint       g_hash_table_size           (GHashTable     *hash_table);
1017
1018
1019 /* Caches
1020  */
1021 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
1022                                 GCacheDestroyFunc  value_destroy_func,
1023                                 GCacheDupFunc      key_dup_func,
1024                                 GCacheDestroyFunc  key_destroy_func,
1025                                 GHashFunc          hash_key_func,
1026                                 GHashFunc          hash_value_func,
1027                                 GCompareFunc       key_compare_func);
1028 void     g_cache_destroy       (GCache            *cache);
1029 gpointer g_cache_insert        (GCache            *cache,
1030                                 gpointer           key);
1031 void     g_cache_remove        (GCache            *cache,
1032                                 gpointer           value);
1033 void     g_cache_key_foreach   (GCache            *cache,
1034                                 GHFunc             func,
1035                                 gpointer           user_data);
1036 void     g_cache_value_foreach (GCache            *cache,
1037                                 GHFunc             func,
1038                                 gpointer           user_data);
1039
1040
1041 /* Balanced binary trees
1042  */
1043 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
1044 void     g_tree_destroy  (GTree         *tree);
1045 void     g_tree_insert   (GTree         *tree,
1046                           gpointer       key,
1047                           gpointer       value);
1048 void     g_tree_remove   (GTree         *tree,
1049                           gpointer       key);
1050 gpointer g_tree_lookup   (GTree         *tree,
1051                           gpointer       key);
1052 void     g_tree_traverse (GTree         *tree,
1053                           GTraverseFunc  traverse_func,
1054                           GTraverseType  traverse_type,
1055                           gpointer       data);
1056 gpointer g_tree_search   (GTree         *tree,
1057                           GSearchFunc    search_func,
1058                           gpointer       data);
1059 gint     g_tree_height   (GTree         *tree);
1060 gint     g_tree_nnodes   (GTree         *tree);
1061
1062
1063
1064 /* N-way tree implementation
1065  */
1066 struct _GNode
1067 {
1068   gpointer data;
1069   GNode   *next;
1070   GNode   *prev;
1071   GNode   *parent;
1072   GNode   *children;
1073 };
1074
1075 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1076                                  ((GNode*) (node))->prev == NULL && \
1077                                  ((GNode*) (node))->next == NULL)
1078 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1079
1080 void     g_node_push_allocator  (GAllocator       *allocator);
1081 void     g_node_pop_allocator   (void);
1082 GNode*   g_node_new             (gpointer          data);
1083 void     g_node_destroy         (GNode            *root);
1084 void     g_node_unlink          (GNode            *node);
1085 GNode*   g_node_insert          (GNode            *parent,
1086                                  gint              position,
1087                                  GNode            *node);
1088 GNode*   g_node_insert_before   (GNode            *parent,
1089                                  GNode            *sibling,
1090                                  GNode            *node);
1091 GNode*   g_node_prepend         (GNode            *parent,
1092                                  GNode            *node);
1093 guint    g_node_n_nodes         (GNode            *root,
1094                                  GTraverseFlags    flags);
1095 GNode*   g_node_get_root        (GNode            *node);
1096 gboolean g_node_is_ancestor     (GNode            *node,
1097                                  GNode            *descendant);
1098 guint    g_node_depth           (GNode            *node);
1099 GNode*   g_node_find            (GNode            *root,
1100                                  GTraverseType     order,
1101                                  GTraverseFlags    flags,
1102                                  gpointer          data);
1103
1104 /* convenience macros */
1105 #define g_node_append(parent, node)                             \
1106      g_node_insert_before ((parent), NULL, (node))
1107 #define g_node_insert_data(parent, position, data)              \
1108      g_node_insert ((parent), (position), g_node_new (data))
1109 #define g_node_insert_data_before(parent, sibling, data)        \
1110      g_node_insert_before ((parent), (sibling), g_node_new (data))
1111 #define g_node_prepend_data(parent, data)                       \
1112      g_node_prepend ((parent), g_node_new (data))
1113 #define g_node_append_data(parent, data)                        \
1114      g_node_insert_before ((parent), NULL, g_node_new (data))
1115
1116 /* traversal function, assumes that `node' is root
1117  * (only traverses `node' and its subtree).
1118  * this function is just a high level interface to
1119  * low level traversal functions, optimized for speed.
1120  */
1121 void     g_node_traverse        (GNode            *root,
1122                                  GTraverseType     order,
1123                                  GTraverseFlags    flags,
1124                                  gint              max_depth,
1125                                  GNodeTraverseFunc func,
1126                                  gpointer          data);
1127
1128 /* return the maximum tree height starting with `node', this is an expensive
1129  * operation, since we need to visit all nodes. this could be shortened by
1130  * adding `guint height' to struct _GNode, but then again, this is not very
1131  * often needed, and would make g_node_insert() more time consuming.
1132  */
1133 guint    g_node_max_height       (GNode *root);
1134
1135 void     g_node_children_foreach (GNode           *node,
1136                                   GTraverseFlags   flags,
1137                                   GNodeForeachFunc func,
1138                                   gpointer         data);
1139 void     g_node_reverse_children (GNode           *node);
1140 guint    g_node_n_children       (GNode           *node);
1141 GNode*   g_node_nth_child        (GNode           *node,
1142                                   guint            n);
1143 GNode*   g_node_last_child       (GNode           *node);
1144 GNode*   g_node_find_child       (GNode           *node,
1145                                   GTraverseFlags   flags,
1146                                   gpointer         data);
1147 gint     g_node_child_position   (GNode           *node,
1148                                   GNode           *child);
1149 gint     g_node_child_index      (GNode           *node,
1150                                   gpointer         data);
1151
1152 GNode*   g_node_first_sibling    (GNode           *node);
1153 GNode*   g_node_last_sibling     (GNode           *node);
1154
1155 #define  g_node_prev_sibling(node)      ((node) ? \
1156                                          ((GNode*) (node))->prev : NULL)
1157 #define  g_node_next_sibling(node)      ((node) ? \
1158                                          ((GNode*) (node))->next : NULL)
1159 #define  g_node_first_child(node)       ((node) ? \
1160                                          ((GNode*) (node))->children : NULL)
1161
1162
1163 /* Callback maintenance functions
1164  */
1165 #define G_HOOK_FLAG_USER_SHIFT  (4)
1166 typedef enum
1167 {
1168   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1169   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1170   G_HOOK_FLAG_MASK      = 0x0f
1171 } GHookFlagMask;
1172
1173 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1174
1175 struct _GHookList
1176 {
1177   guint          seq_id;
1178   guint          hook_size;
1179   guint          is_setup : 1;
1180   GHook         *hooks;
1181   GMemChunk     *hook_memchunk;
1182   GHookFreeFunc  hook_free; /* virtual function */
1183   GHookFreeFunc  hook_destroy; /* virtual function */
1184 };
1185
1186 struct _GHook
1187 {
1188   gpointer       data;
1189   GHook         *next;
1190   GHook         *prev;
1191   guint          ref_count;
1192   guint          hook_id;
1193   guint          flags;
1194   gpointer       func;
1195   GDestroyNotify destroy;
1196 };
1197
1198 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1199                                           G_HOOK_FLAG_ACTIVE) != 0)
1200 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1201                                           G_HOOK_FLAG_IN_CALL) != 0)
1202 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1203                                          G_HOOK_ACTIVE (hook))
1204 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1205                                          ((GHook*) hook)->prev == NULL && \
1206                                          ((GHook*) hook)->hook_id == 0 && \
1207                                          ((GHook*) hook)->ref_count == 0)
1208
1209 void     g_hook_list_init               (GHookList              *hook_list,
1210                                          guint                   hook_size);
1211 void     g_hook_list_clear              (GHookList              *hook_list);
1212 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1213 void     g_hook_free                    (GHookList              *hook_list,
1214                                          GHook                  *hook);
1215 void     g_hook_ref                     (GHookList              *hook_list,
1216                                          GHook                  *hook);
1217 void     g_hook_unref                   (GHookList              *hook_list,
1218                                          GHook                  *hook);
1219 gboolean g_hook_destroy                 (GHookList              *hook_list,
1220                                          guint                   hook_id);
1221 void     g_hook_destroy_link            (GHookList              *hook_list,
1222                                          GHook                  *hook);
1223 void     g_hook_prepend                 (GHookList              *hook_list,
1224                                          GHook                  *hook);
1225 void     g_hook_insert_before           (GHookList              *hook_list,
1226                                          GHook                  *sibling,
1227                                          GHook                  *hook);
1228 void     g_hook_insert_sorted           (GHookList              *hook_list,
1229                                          GHook                  *hook,
1230                                          GHookCompareFunc        func);
1231 GHook*   g_hook_get                     (GHookList              *hook_list,
1232                                          guint                   hook_id);
1233 GHook*   g_hook_find                    (GHookList              *hook_list,
1234                                          gboolean                need_valids,
1235                                          GHookFindFunc           func,
1236                                          gpointer                data);
1237 GHook*   g_hook_find_data               (GHookList              *hook_list,
1238                                          gboolean                need_valids,
1239                                          gpointer                data);
1240 GHook*   g_hook_find_func               (GHookList              *hook_list,
1241                                          gboolean                need_valids,
1242                                          gpointer                func);
1243 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1244                                          gboolean                need_valids,
1245                                          gpointer                func,
1246                                          gpointer                data);
1247 /* return the first valid hook, and increment its reference count */
1248 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1249                                          gboolean                may_be_in_call);
1250 /* return the next valid hook with incremented reference count, and
1251  * decrement the reference count of the original hook
1252  */
1253 GHook*   g_hook_next_valid              (GHookList              *hook_list,
1254                                          GHook                  *hook,
1255                                          gboolean                may_be_in_call);
1256
1257 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1258 gint     g_hook_compare_ids             (GHook                  *new_hook,
1259                                          GHook                  *sibling);
1260
1261 /* convenience macros */
1262 #define  g_hook_append( hook_list, hook )  \
1263      g_hook_insert_before ((hook_list), NULL, (hook))
1264
1265 /* invoke all valid hooks with the (*GHookFunc) signature.
1266  */
1267 void     g_hook_list_invoke             (GHookList              *hook_list,
1268                                          gboolean                may_recurse);
1269 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1270  * and destroy the hook if FALSE is returned.
1271  */
1272 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1273                                          gboolean                may_recurse);
1274 /* invoke a marshaller on all valid hooks.
1275  */
1276 void     g_hook_list_marshal            (GHookList              *hook_list,
1277                                          gboolean                may_recurse,
1278                                          GHookMarshaller         marshaller,
1279                                          gpointer                data);
1280 void     g_hook_list_marshal_check      (GHookList              *hook_list,
1281                                          gboolean                may_recurse,
1282                                          GHookCheckMarshaller    marshaller,
1283                                          gpointer                data);
1284
1285
1286 /* Fatal error handlers.
1287  * g_on_error_query() will prompt the user to either
1288  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1289  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1290  * process and shows a stack trace.
1291  * These function may cause different actions on non-unix platforms.
1292  * The prg_name arg is required by gdb to find the executable, if it is
1293  * passed as NULL, g_on_error_query() will try g_get_prgname().
1294  */
1295 void g_on_error_query (const gchar *prg_name);
1296 void g_on_error_stack_trace (const gchar *prg_name);
1297
1298
1299 /* Logging mechanism
1300  */
1301 extern          const gchar             *g_log_domain_glib;
1302 guint           g_log_set_handler       (const gchar    *log_domain,
1303                                          GLogLevelFlags  log_levels,
1304                                          GLogFunc        log_func,
1305                                          gpointer        user_data);
1306 void            g_log_remove_handler    (const gchar    *log_domain,
1307                                          guint           handler_id);
1308 void            g_log_default_handler   (const gchar    *log_domain,
1309                                          GLogLevelFlags  log_level,
1310                                          const gchar    *message,
1311                                          gpointer        unused_data);
1312 void            g_log                   (const gchar    *log_domain,
1313                                          GLogLevelFlags  log_level,
1314                                          const gchar    *format,
1315                                          ...) G_GNUC_PRINTF (3, 4);
1316 void            g_logv                  (const gchar    *log_domain,
1317                                          GLogLevelFlags  log_level,
1318                                          const gchar    *format,
1319                                          va_list         args);
1320 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1321                                          GLogLevelFlags  fatal_mask);
1322 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1323 #ifndef G_LOG_DOMAIN
1324 #define G_LOG_DOMAIN    ((gchar*) 0)
1325 #endif  /* G_LOG_DOMAIN */
1326 #ifdef  __GNUC__
1327 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1328                                                G_LOG_LEVEL_ERROR, \
1329                                                format, ##args)
1330 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1331                                                G_LOG_LEVEL_MESSAGE, \
1332                                                format, ##args)
1333 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1334                                                G_LOG_LEVEL_WARNING, \
1335                                                format, ##args)
1336 #else   /* !__GNUC__ */
1337 static void
1338 g_error (const gchar *format,
1339          ...)
1340 {
1341   va_list args;
1342   va_start (args, format);
1343   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1344   va_end (args);
1345 }
1346 static void
1347 g_message (const gchar *format,
1348            ...)
1349 {
1350   va_list args;
1351   va_start (args, format);
1352   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1353   va_end (args);
1354 }
1355 static void
1356 g_warning (const gchar *format,
1357            ...)
1358 {
1359   va_list args;
1360   va_start (args, format);
1361   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1362   va_end (args);
1363 }
1364 #endif  /* !__GNUC__ */
1365
1366 typedef void    (*GPrintFunc)           (const gchar    *string);
1367 void            g_print                 (const gchar    *format,
1368                                          ...) G_GNUC_PRINTF (1, 2);
1369 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1370 void            g_printerr              (const gchar    *format,
1371                                          ...) G_GNUC_PRINTF (1, 2);
1372 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1373
1374 /* deprecated compatibility functions, use g_log_set_handler() instead */
1375 typedef void            (*GErrorFunc)           (const gchar *str);
1376 typedef void            (*GWarningFunc)         (const gchar *str);
1377 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1378 GWarningFunc g_set_warning_handler (GWarningFunc func);
1379 GPrintFunc   g_set_message_handler (GPrintFunc func);
1380
1381
1382 /* Memory allocation and debugging
1383  */
1384 #ifdef USE_DMALLOC
1385
1386 #define g_malloc(size)       ((gpointer) MALLOC (size))
1387 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1388 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1389 #define g_free(mem)          FREE (mem)
1390
1391 #else /* !USE_DMALLOC */
1392
1393 gpointer g_malloc      (gulong    size);
1394 gpointer g_malloc0     (gulong    size);
1395 gpointer g_realloc     (gpointer  mem,
1396                         gulong    size);
1397 void     g_free        (gpointer  mem);
1398
1399 #endif /* !USE_DMALLOC */
1400
1401 void     g_mem_profile (void);
1402 void     g_mem_check   (gpointer  mem);
1403
1404 /* Generic allocators
1405  */
1406 GAllocator* g_allocator_new   (const gchar  *name,
1407                                guint         n_preallocs);
1408 void        g_allocator_free  (GAllocator   *allocator);
1409
1410 #define G_ALLOCATOR_LIST        (1)
1411 #define G_ALLOCATOR_SLIST       (2)
1412 #define G_ALLOCATOR_NODE        (3)
1413
1414
1415 /* "g_mem_chunk_new" creates a new memory chunk.
1416  * Memory chunks are used to allocate pieces of memory which are
1417  *  always the same size. Lists are a good example of such a data type.
1418  * The memory chunk allocates and frees blocks of memory as needed.
1419  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1420  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1421  *  fault...somewhere).
1422  *
1423  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1424  *  want to know what's going on inside do you?)
1425  */
1426
1427 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1428  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1429  *  atom. (They are also useful for lists which use MemChunk to allocate
1430  *  memory but are also part of the MemChunk implementation).
1431  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1432  */
1433
1434 #define G_ALLOC_ONLY      1
1435 #define G_ALLOC_AND_FREE  2
1436
1437 GMemChunk* g_mem_chunk_new     (gchar     *name,
1438                                 gint       atom_size,
1439                                 gulong     area_size,
1440                                 gint       type);
1441 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1442 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1443 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1444 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1445                                 gpointer   mem);
1446 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1447 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1448 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1449 void       g_mem_chunk_info    (void);
1450
1451 /* Ah yes...we have a "g_blow_chunks" function.
1452  * "g_blow_chunks" simply compresses all the chunks. This operation
1453  *  consists of freeing every memory area that should be freed (but
1454  *  which we haven't gotten around to doing yet). And, no,
1455  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1456  *  much better name than "g_mem_chunk_clean_all" or something
1457  *  similar.
1458  */
1459 void g_blow_chunks (void);
1460
1461
1462 /* Timer
1463  */
1464
1465 #define G_MICROSEC 1000000
1466
1467 GTimer* g_timer_new     (void);
1468 void    g_timer_destroy (GTimer  *timer);
1469 void    g_timer_start   (GTimer  *timer);
1470 void    g_timer_stop    (GTimer  *timer);
1471 void    g_timer_reset   (GTimer  *timer);
1472 gdouble g_timer_elapsed (GTimer  *timer,
1473                          gulong  *microseconds);
1474 void    g_usleep        (gulong microseconds);
1475
1476 /* String utility functions that modify a string argument or
1477  * return a constant string that must not be freed.
1478  */
1479 #define  G_STR_DELIMITERS       "_-|> <."
1480 gchar*   g_strdelimit           (gchar       *string,
1481                                  const gchar *delimiters,
1482                                  gchar        new_delimiter);
1483 gdouble  g_strtod               (const gchar *nptr,
1484                                  gchar      **endptr);
1485 gchar*   g_strerror             (gint         errnum);
1486 gchar*   g_strsignal            (gint         signum);
1487 gint     g_strcasecmp           (const gchar *s1,
1488                                  const gchar *s2);
1489 gint     g_strncasecmp          (const gchar *s1,
1490                                  const gchar *s2,
1491                                  guint        n);
1492 void     g_strdown              (gchar       *string);
1493 void     g_strup                (gchar       *string);
1494 void     g_strreverse           (gchar       *string);
1495 /* removes leading spaces */
1496 gchar*   g_strchug              (gchar        *string);
1497 /* removes trailing spaces */
1498 gchar*  g_strchomp              (gchar        *string);
1499 /* removes leading & trailing spaces */
1500 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1501
1502 /* String utility functions that return a newly allocated string which
1503  * ought to be freed with g_free from the caller at some point.
1504  */
1505 gchar*   g_strdup               (const gchar *str);
1506 gchar*   g_strdup_printf        (const gchar *format,
1507                                  ...) G_GNUC_PRINTF (1, 2);
1508 gchar*   g_strdup_vprintf       (const gchar *format,
1509                                  va_list      args);
1510 gchar*   g_strndup              (const gchar *str,
1511                                  guint        n);
1512 gchar*   g_strnfill             (guint        length,
1513                                  gchar        fill_char);
1514 gchar*   g_strconcat            (const gchar *string1,
1515                                  ...); /* NULL terminated */
1516 gchar*   g_strjoin              (const gchar  *separator,
1517                                  ...); /* NULL terminated */
1518 /* Make a copy of a string interpreting C string -style escape
1519  * sequences. Inverse of g_strescape. The recognized sequences are \b
1520  * \f \n \r \t \\ \" and the octal format.
1521  */
1522 gchar*   g_strcompress          (const gchar *source);
1523
1524 /* Copy a string escaping nonprintable characters like in C strings.
1525  * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1526  * to a string containing characters that are not to be escaped.
1527  */
1528 gchar*   g_strescape            (const gchar *source,
1529                                  const gchar *exceptions);
1530 /* Deprecated API:
1531  * gchar* g_strescape (const gchar *source);
1532  * Luckily this function wasn't much used.
1533  * Add a second NULL parameter in calls for mostly identical semantics.
1534  */
1535
1536 gpointer g_memdup               (gconstpointer mem,
1537                                  guint         byte_size);
1538
1539 /* NULL terminated string arrays.
1540  * g_strsplit() splits up string into max_tokens tokens at delim and
1541  * returns a newly allocated string array.
1542  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1543  * optional separator, the returned string is newly allocated.
1544  * g_strfreev() frees the array itself and all of its strings.
1545  */
1546 gchar**  g_strsplit             (const gchar  *string,
1547                                  const gchar  *delimiter,
1548                                  gint          max_tokens);
1549 gchar*   g_strjoinv             (const gchar  *separator,
1550                                  gchar       **str_array);
1551 void     g_strfreev             (gchar       **str_array);
1552
1553
1554
1555 /* calculate a string size, guarranteed to fit format + args.
1556  */
1557 guint   g_printf_string_upper_bound (const gchar* format,
1558                                      va_list      args);
1559
1560
1561 /* Retrive static string info
1562  */
1563 gchar*  g_get_user_name         (void);
1564 gchar*  g_get_real_name         (void);
1565 gchar*  g_get_home_dir          (void);
1566 gchar*  g_get_tmp_dir           (void);
1567 gchar*  g_get_prgname           (void);
1568 void    g_set_prgname           (const gchar *prgname);
1569
1570
1571 /* Miscellaneous utility functions
1572  */
1573 guint   g_parse_debug_string    (const gchar *string,
1574                                  GDebugKey   *keys,
1575                                  guint        nkeys);
1576 gint    g_snprintf              (gchar       *string,
1577                                  gulong       n,
1578                                  gchar const *format,
1579                                  ...) G_GNUC_PRINTF (3, 4);
1580 gint    g_vsnprintf             (gchar       *string,
1581                                  gulong       n,
1582                                  gchar const *format,
1583                                  va_list      args);
1584 gchar*  g_basename              (const gchar *file_name);
1585 /* Check if a file name is an absolute path */
1586 gboolean g_path_is_absolute     (const gchar *file_name);
1587 /* In case of absolute paths, skip the root part */
1588 gchar*  g_path_skip_root        (gchar       *file_name);
1589
1590 /* strings are newly allocated with g_malloc() */
1591 gchar*  g_dirname               (const gchar *file_name);
1592 gchar*  g_get_current_dir       (void);
1593 gchar*  g_getenv                (const gchar *variable);
1594
1595
1596 /* we use a GLib function as a replacement for ATEXIT, so
1597  * the programmer is not required to check the return value
1598  * (if there is any in the implementation) and doesn't encounter
1599  * missing include files.
1600  */
1601 void    g_atexit                (GVoidFunc    func);
1602
1603
1604 /* Bit tests
1605  */
1606 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1607                                        gint    nth_bit);
1608 #ifdef  G_CAN_INLINE
1609 G_INLINE_FUNC gint
1610 g_bit_nth_lsf (guint32 mask,
1611                gint    nth_bit)
1612 {
1613   do
1614     {
1615       nth_bit++;
1616       if (mask & (1 << (guint) nth_bit))
1617         return nth_bit;
1618     }
1619   while (nth_bit < 32);
1620   return -1;
1621 }
1622 #endif  /* G_CAN_INLINE */
1623
1624 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1625                                        gint    nth_bit);
1626 #ifdef G_CAN_INLINE
1627 G_INLINE_FUNC gint
1628 g_bit_nth_msf (guint32 mask,
1629                gint    nth_bit)
1630 {
1631   if (nth_bit < 0)
1632     nth_bit = 32;
1633   do
1634     {
1635       nth_bit--;
1636       if (mask & (1 << (guint) nth_bit))
1637         return nth_bit;
1638     }
1639   while (nth_bit > 0);
1640   return -1;
1641 }
1642 #endif  /* G_CAN_INLINE */
1643
1644 G_INLINE_FUNC guint     g_bit_storage (guint number);
1645 #ifdef G_CAN_INLINE
1646 G_INLINE_FUNC guint
1647 g_bit_storage (guint number)
1648 {
1649   register guint n_bits = 0;
1650   
1651   do
1652     {
1653       n_bits++;
1654       number >>= 1;
1655     }
1656   while (number);
1657   return n_bits;
1658 }
1659 #endif  /* G_CAN_INLINE */
1660
1661
1662 /* Trash Stacks
1663  * elements need to be >= sizeof (gpointer)
1664  */
1665 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
1666                                                  gpointer      data_p);
1667 #ifdef G_CAN_INLINE
1668 G_INLINE_FUNC void
1669 g_trash_stack_push (GTrashStack **stack_p,
1670                     gpointer      data_p)
1671 {
1672   GTrashStack *data = (GTrashStack *) data_p;
1673
1674   data->next = *stack_p;
1675   *stack_p = data;
1676 }
1677 #endif  /* G_CAN_INLINE */
1678
1679 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
1680 #ifdef G_CAN_INLINE
1681 G_INLINE_FUNC gpointer
1682 g_trash_stack_pop (GTrashStack **stack_p)
1683 {
1684   GTrashStack *data;
1685
1686   data = *stack_p;
1687   if (data)
1688     {
1689       *stack_p = data->next;
1690       /* NULLify private pointer here, most platforms store NULL as
1691        * subsequent 0 bytes
1692        */
1693       data->next = NULL;
1694     }
1695
1696   return data;
1697 }
1698 #endif  /* G_CAN_INLINE */
1699
1700 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
1701 #ifdef G_CAN_INLINE
1702 G_INLINE_FUNC gpointer
1703 g_trash_stack_peek (GTrashStack **stack_p)
1704 {
1705   GTrashStack *data;
1706
1707   data = *stack_p;
1708
1709   return data;
1710 }
1711 #endif  /* G_CAN_INLINE */
1712
1713 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
1714 #ifdef G_CAN_INLINE
1715 G_INLINE_FUNC guint
1716 g_trash_stack_height (GTrashStack **stack_p)
1717 {
1718   GTrashStack *data;
1719   guint i = 0;
1720
1721   for (data = *stack_p; data; data = data->next)
1722     i++;
1723
1724   return i;
1725 }
1726 #endif  /* G_CAN_INLINE */
1727
1728
1729 /* String Chunks
1730  */
1731 GStringChunk* g_string_chunk_new           (gint size);
1732 void          g_string_chunk_free          (GStringChunk *chunk);
1733 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1734                                             const gchar  *string);
1735 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1736                                             const gchar  *string);
1737
1738
1739 /* Strings
1740  */
1741 GString*     g_string_new               (const gchar     *init);
1742 GString*     g_string_sized_new         (guint            dfl_size);
1743 void         g_string_free              (GString         *string,
1744                                          gint             free_segment);
1745 GString*     g_string_assign            (GString         *lval,
1746                                          const gchar     *rval);
1747 GString*     g_string_truncate          (GString         *string,
1748                                          gint             len);
1749 GString*     g_string_append            (GString         *string,
1750                                          const gchar     *val);
1751 GString*     g_string_append_c          (GString         *string,
1752                                          gchar            c);
1753 GString*     g_string_prepend           (GString         *string,
1754                                          const gchar     *val);
1755 GString*     g_string_prepend_c         (GString         *string,
1756                                          gchar            c);
1757 GString*     g_string_insert            (GString         *string,
1758                                          gint             pos,
1759                                          const gchar     *val);
1760 GString*     g_string_insert_c          (GString         *string,
1761                                          gint             pos,
1762                                          gchar            c);
1763 GString*     g_string_erase             (GString         *string,
1764                                          gint             pos,
1765                                          gint             len);
1766 GString*     g_string_down              (GString         *string);
1767 GString*     g_string_up                (GString         *string);
1768 void         g_string_sprintf           (GString         *string,
1769                                          const gchar     *format,
1770                                          ...) G_GNUC_PRINTF (2, 3);
1771 void         g_string_sprintfa          (GString         *string,
1772                                          const gchar     *format,
1773                                          ...) G_GNUC_PRINTF (2, 3);
1774
1775
1776 /* Resizable arrays, remove fills any cleared spot and shortens the
1777  * array, while preserving the order. remove_fast will distort the
1778  * order by moving the last element to the position of the removed 
1779  */
1780
1781 #define g_array_append_val(a,v)   g_array_append_vals (a, &v, 1)
1782 #define g_array_prepend_val(a,v)  g_array_prepend_vals (a, &v, 1)
1783 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1784 #define g_array_index(a,t,i)      (((t*) (a)->data) [(i)])
1785
1786 GArray* g_array_new               (gboolean         zero_terminated,
1787                                    gboolean         clear,
1788                                    guint            element_size);
1789 void    g_array_free              (GArray          *array,
1790                                    gboolean         free_segment);
1791 GArray* g_array_append_vals       (GArray          *array,
1792                                    gconstpointer    data,
1793                                    guint            len);
1794 GArray* g_array_prepend_vals      (GArray          *array,
1795                                    gconstpointer    data,
1796                                    guint            len);
1797 GArray* g_array_insert_vals       (GArray          *array,
1798                                    guint            index,
1799                                    gconstpointer    data,
1800                                    guint            len);
1801 GArray* g_array_set_size          (GArray          *array,
1802                                    guint            length);
1803 GArray* g_array_remove_index      (GArray          *array,
1804                                    guint            index);
1805 GArray* g_array_remove_index_fast (GArray          *array,
1806                                    guint            index);
1807
1808 /* Resizable pointer array.  This interface is much less complicated
1809  * than the above.  Add appends appends a pointer.  Remove fills any
1810  * cleared spot and shortens the array. remove_fast will again distort
1811  * order.  
1812  */
1813 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1814 GPtrArray*  g_ptr_array_new                (void);
1815 void        g_ptr_array_free               (GPtrArray   *array,
1816                                             gboolean     free_seg);
1817 void        g_ptr_array_set_size           (GPtrArray   *array,
1818                                             gint         length);
1819 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1820                                             guint        index);
1821 gpointer    g_ptr_array_remove_index_fast  (GPtrArray   *array,
1822                                             guint        index);
1823 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1824                                             gpointer     data);
1825 gboolean    g_ptr_array_remove_fast        (GPtrArray   *array,
1826                                             gpointer     data);
1827 void        g_ptr_array_add                (GPtrArray   *array,
1828                                             gpointer     data);
1829
1830 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1831  * but type-safe.
1832  */
1833
1834 GByteArray* g_byte_array_new               (void);
1835 void        g_byte_array_free              (GByteArray   *array,
1836                                             gboolean      free_segment);
1837 GByteArray* g_byte_array_append            (GByteArray   *array,
1838                                             const guint8 *data,
1839                                             guint         len);
1840 GByteArray* g_byte_array_prepend           (GByteArray   *array,
1841                                             const guint8 *data,
1842                                             guint         len);
1843 GByteArray* g_byte_array_set_size          (GByteArray   *array,
1844                                             guint         length);
1845 GByteArray* g_byte_array_remove_index      (GByteArray   *array,
1846                                             guint         index);
1847 GByteArray* g_byte_array_remove_index_fast (GByteArray   *array,
1848                                             guint         index);
1849
1850
1851 /* Hash Functions
1852  */
1853 gint  g_str_equal (gconstpointer   v,
1854                    gconstpointer   v2);
1855 guint g_str_hash  (gconstpointer   v);
1856
1857 gint  g_int_equal (gconstpointer   v,
1858                    gconstpointer   v2);
1859 guint g_int_hash  (gconstpointer   v);
1860
1861 /* This "hash" function will just return the key's adress as an
1862  * unsigned integer. Useful for hashing on plain adresses or
1863  * simple integer values.
1864  * passing NULL into g_hash_table_new() as GHashFunc has the
1865  * same effect as passing g_direct_hash().
1866  */
1867 guint g_direct_hash  (gconstpointer v);
1868 gint  g_direct_equal (gconstpointer v,
1869                       gconstpointer v2);
1870
1871
1872 /* Quarks (string<->id association)
1873  */
1874 GQuark    g_quark_try_string            (const gchar    *string);
1875 GQuark    g_quark_from_static_string    (const gchar    *string);
1876 GQuark    g_quark_from_string           (const gchar    *string);
1877 gchar*    g_quark_to_string             (GQuark          quark);
1878
1879
1880 /* Keyed Data List
1881  */
1882 void      g_datalist_init                (GData          **datalist);
1883 void      g_datalist_clear               (GData          **datalist);
1884 gpointer  g_datalist_id_get_data         (GData          **datalist,
1885                                           GQuark           key_id);
1886 void      g_datalist_id_set_data_full    (GData          **datalist,
1887                                           GQuark           key_id,
1888                                           gpointer         data,
1889                                           GDestroyNotify   destroy_func);
1890 void      g_datalist_id_remove_no_notify (GData          **datalist,
1891                                           GQuark           key_id);
1892 void      g_datalist_foreach             (GData          **datalist,
1893                                           GDataForeachFunc func,
1894                                           gpointer         user_data);
1895 #define   g_datalist_id_set_data(dl, q, d)      \
1896      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1897 #define   g_datalist_id_remove_data(dl, q)      \
1898      g_datalist_id_set_data ((dl), (q), NULL)
1899 #define   g_datalist_get_data(dl, k)            \
1900      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1901 #define   g_datalist_set_data_full(dl, k, d, f) \
1902      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1903 #define   g_datalist_remove_no_notify(dl, k)    \
1904      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1905 #define   g_datalist_set_data(dl, k, d)         \
1906      g_datalist_set_data_full ((dl), (k), (d), NULL)
1907 #define   g_datalist_remove_data(dl, k)         \
1908      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1909
1910
1911 /* Location Associated Keyed Data
1912  */
1913 void      g_dataset_destroy             (gconstpointer    dataset_location);
1914 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
1915                                          GQuark           key_id);
1916 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
1917                                          GQuark           key_id,
1918                                          gpointer         data,
1919                                          GDestroyNotify   destroy_func);
1920 void      g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
1921                                          GQuark           key_id);
1922 void      g_dataset_foreach             (gconstpointer    dataset_location,
1923                                          GDataForeachFunc func,
1924                                          gpointer         user_data);
1925 #define   g_dataset_id_set_data(l, k, d)        \
1926      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1927 #define   g_dataset_id_remove_data(l, k)        \
1928      g_dataset_id_set_data ((l), (k), NULL)
1929 #define   g_dataset_get_data(l, k)              \
1930      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1931 #define   g_dataset_set_data_full(l, k, d, f)   \
1932      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1933 #define   g_dataset_remove_no_notify(l, k)      \
1934      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1935 #define   g_dataset_set_data(l, k, d)           \
1936      g_dataset_set_data_full ((l), (k), (d), NULL)
1937 #define   g_dataset_remove_data(l, k)           \
1938      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1939
1940
1941 /* GScanner: Flexible lexical scanner for general purpose.
1942  */
1943
1944 /* Character sets */
1945 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1946 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1947 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1948                         "\307\310\311\312\313\314\315\316\317\320"\
1949                         "\321\322\323\324\325\326"\
1950                         "\330\331\332\333\334\335\336"
1951 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1952                         "\347\350\351\352\353\354\355\356\357\360"\
1953                         "\361\362\363\364\365\366"\
1954                         "\370\371\372\373\374\375\376\377"
1955
1956 /* Error types */
1957 typedef enum
1958 {
1959   G_ERR_UNKNOWN,
1960   G_ERR_UNEXP_EOF,
1961   G_ERR_UNEXP_EOF_IN_STRING,
1962   G_ERR_UNEXP_EOF_IN_COMMENT,
1963   G_ERR_NON_DIGIT_IN_CONST,
1964   G_ERR_DIGIT_RADIX,
1965   G_ERR_FLOAT_RADIX,
1966   G_ERR_FLOAT_MALFORMED
1967 } GErrorType;
1968
1969 /* Token types */
1970 typedef enum
1971 {
1972   G_TOKEN_EOF                   =   0,
1973   
1974   G_TOKEN_LEFT_PAREN            = '(',
1975   G_TOKEN_RIGHT_PAREN           = ')',
1976   G_TOKEN_LEFT_CURLY            = '{',
1977   G_TOKEN_RIGHT_CURLY           = '}',
1978   G_TOKEN_LEFT_BRACE            = '[',
1979   G_TOKEN_RIGHT_BRACE           = ']',
1980   G_TOKEN_EQUAL_SIGN            = '=',
1981   G_TOKEN_COMMA                 = ',',
1982   
1983   G_TOKEN_NONE                  = 256,
1984   
1985   G_TOKEN_ERROR,
1986   
1987   G_TOKEN_CHAR,
1988   G_TOKEN_BINARY,
1989   G_TOKEN_OCTAL,
1990   G_TOKEN_INT,
1991   G_TOKEN_HEX,
1992   G_TOKEN_FLOAT,
1993   G_TOKEN_STRING,
1994   
1995   G_TOKEN_SYMBOL,
1996   G_TOKEN_IDENTIFIER,
1997   G_TOKEN_IDENTIFIER_NULL,
1998   
1999   G_TOKEN_COMMENT_SINGLE,
2000   G_TOKEN_COMMENT_MULTI,
2001   G_TOKEN_LAST
2002 } GTokenType;
2003
2004 union   _GTokenValue
2005 {
2006   gpointer      v_symbol;
2007   gchar         *v_identifier;
2008   gulong        v_binary;
2009   gulong        v_octal;
2010   gulong        v_int;
2011   gdouble       v_float;
2012   gulong        v_hex;
2013   gchar         *v_string;
2014   gchar         *v_comment;
2015   guchar        v_char;
2016   guint         v_error;
2017 };
2018
2019 struct  _GScannerConfig
2020 {
2021   /* Character sets
2022    */
2023   gchar         *cset_skip_characters;          /* default: " \t\n" */
2024   gchar         *cset_identifier_first;
2025   gchar         *cset_identifier_nth;
2026   gchar         *cpair_comment_single;          /* default: "#\n" */
2027   
2028   /* Should symbol lookup work case sensitive?
2029    */
2030   guint         case_sensitive : 1;
2031   
2032   /* Boolean values to be adjusted "on the fly"
2033    * to configure scanning behaviour.
2034    */
2035   guint         skip_comment_multi : 1;         /* C like comment */
2036   guint         skip_comment_single : 1;        /* single line comment */
2037   guint         scan_comment_multi : 1;         /* scan multi line comments? */
2038   guint         scan_identifier : 1;
2039   guint         scan_identifier_1char : 1;
2040   guint         scan_identifier_NULL : 1;
2041   guint         scan_symbols : 1;
2042   guint         scan_binary : 1;
2043   guint         scan_octal : 1;
2044   guint         scan_float : 1;
2045   guint         scan_hex : 1;                   /* `0x0ff0' */
2046   guint         scan_hex_dollar : 1;            /* `$0ff0' */
2047   guint         scan_string_sq : 1;             /* string: 'anything' */
2048   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
2049   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
2050   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
2051   guint         identifier_2_string : 1;
2052   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
2053   guint         symbol_2_token : 1;
2054   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
2055 };
2056
2057 struct  _GScanner
2058 {
2059   /* unused fields */
2060   gpointer              user_data;
2061   guint                 max_parse_errors;
2062   
2063   /* g_scanner_error() increments this field */
2064   guint                 parse_errors;
2065   
2066   /* name of input stream, featured by the default message handler */
2067   const gchar           *input_name;
2068   
2069   /* data pointer for derived structures */
2070   gpointer              derived_data;
2071   
2072   /* link into the scanner configuration */
2073   GScannerConfig        *config;
2074   
2075   /* fields filled in after g_scanner_get_next_token() */
2076   GTokenType            token;
2077   GTokenValue           value;
2078   guint                 line;
2079   guint                 position;
2080   
2081   /* fields filled in after g_scanner_peek_next_token() */
2082   GTokenType            next_token;
2083   GTokenValue           next_value;
2084   guint                 next_line;
2085   guint                 next_position;
2086   
2087   /* to be considered private */
2088   GHashTable            *symbol_table;
2089   gint                  input_fd;
2090   const gchar           *text;
2091   const gchar           *text_end;
2092   gchar                 *buffer;
2093   guint                 scope_id;
2094   
2095   /* handler function for _warn and _error */
2096   GScannerMsgFunc       msg_handler;
2097 };
2098
2099 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
2100 void            g_scanner_destroy               (GScanner       *scanner);
2101 void            g_scanner_input_file            (GScanner       *scanner,
2102                                                  gint           input_fd);
2103 void            g_scanner_sync_file_offset      (GScanner       *scanner);
2104 void            g_scanner_input_text            (GScanner       *scanner,
2105                                                  const  gchar   *text,
2106                                                  guint          text_len);
2107 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
2108 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
2109 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
2110 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
2111 guint           g_scanner_cur_line              (GScanner       *scanner);
2112 guint           g_scanner_cur_position          (GScanner       *scanner);
2113 gboolean        g_scanner_eof                   (GScanner       *scanner);
2114 guint           g_scanner_set_scope             (GScanner       *scanner,
2115                                                  guint           scope_id);
2116 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
2117                                                  guint           scope_id,
2118                                                  const gchar    *symbol,
2119                                                  gpointer       value);
2120 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
2121                                                  guint           scope_id,
2122                                                  const gchar    *symbol);
2123 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
2124                                                  guint           scope_id,
2125                                                  const gchar    *symbol);
2126 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
2127                                                  guint           scope_id,
2128                                                  GHFunc          func,
2129                                                  gpointer        user_data);
2130 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
2131                                                  const gchar    *symbol);
2132 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
2133 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
2134 void            g_scanner_unexp_token           (GScanner       *scanner,
2135                                                  GTokenType     expected_token,
2136                                                  const gchar    *identifier_spec,
2137                                                  const gchar    *symbol_spec,
2138                                                  const gchar    *symbol_name,
2139                                                  const gchar    *message,
2140                                                  gint            is_error);
2141 void            g_scanner_error                 (GScanner       *scanner,
2142                                                  const gchar    *format,
2143                                                  ...) G_GNUC_PRINTF (2,3);
2144 void            g_scanner_warn                  (GScanner       *scanner,
2145                                                  const gchar    *format,
2146                                                  ...) G_GNUC_PRINTF (2,3);
2147 gint            g_scanner_stat_mode             (const gchar    *filename);
2148 /* keep downward source compatibility */
2149 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
2150   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2151 } G_STMT_END
2152 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
2153   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2154 } G_STMT_END
2155 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2156   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2157 } G_STMT_END
2158
2159
2160 /* GCompletion
2161  */
2162
2163 struct _GCompletion
2164 {
2165   GList* items;
2166   GCompletionFunc func;
2167   
2168   gchar* prefix;
2169   GList* cache;
2170 };
2171
2172 GCompletion* g_completion_new          (GCompletionFunc func);
2173 void         g_completion_add_items    (GCompletion*    cmp,
2174                                         GList*          items);
2175 void         g_completion_remove_items (GCompletion*    cmp,
2176                                         GList*          items);
2177 void         g_completion_clear_items  (GCompletion*    cmp);
2178 GList*       g_completion_complete     (GCompletion*    cmp,
2179                                         gchar*          prefix,
2180                                         gchar**         new_prefix);
2181 void         g_completion_free         (GCompletion*    cmp);
2182
2183
2184 /* GDate
2185  *
2186  * Date calculations (not time for now, to be resolved). These are a
2187  * mutant combination of Steffen Beyer's DateCalc routines
2188  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2189  * date routines (written for in-house software).  Written by Havoc
2190  * Pennington <hp@pobox.com> 
2191  */
2192
2193 typedef guint16 GDateYear;
2194 typedef guint8  GDateDay;   /* day of the month */
2195 typedef struct _GDate GDate;
2196 /* make struct tm known without having to include time.h */
2197 struct tm;
2198
2199 /* enum used to specify order of appearance in parsed date strings */
2200 typedef enum
2201 {
2202   G_DATE_DAY   = 0,
2203   G_DATE_MONTH = 1,
2204   G_DATE_YEAR  = 2
2205 } GDateDMY;
2206
2207 /* actual week and month values */
2208 typedef enum
2209 {
2210   G_DATE_BAD_WEEKDAY  = 0,
2211   G_DATE_MONDAY       = 1,
2212   G_DATE_TUESDAY      = 2,
2213   G_DATE_WEDNESDAY    = 3,
2214   G_DATE_THURSDAY     = 4,
2215   G_DATE_FRIDAY       = 5,
2216   G_DATE_SATURDAY     = 6,
2217   G_DATE_SUNDAY       = 7
2218 } GDateWeekday;
2219 typedef enum
2220 {
2221   G_DATE_BAD_MONTH = 0,
2222   G_DATE_JANUARY   = 1,
2223   G_DATE_FEBRUARY  = 2,
2224   G_DATE_MARCH     = 3,
2225   G_DATE_APRIL     = 4,
2226   G_DATE_MAY       = 5,
2227   G_DATE_JUNE      = 6,
2228   G_DATE_JULY      = 7,
2229   G_DATE_AUGUST    = 8,
2230   G_DATE_SEPTEMBER = 9,
2231   G_DATE_OCTOBER   = 10,
2232   G_DATE_NOVEMBER  = 11,
2233   G_DATE_DECEMBER  = 12
2234 } GDateMonth;
2235
2236 #define G_DATE_BAD_JULIAN 0U
2237 #define G_DATE_BAD_DAY    0U
2238 #define G_DATE_BAD_YEAR   0U
2239
2240 /* Note: directly manipulating structs is generally a bad idea, but
2241  * in this case it's an *incredibly* bad idea, because all or part
2242  * of this struct can be invalid at any given time. Use the functions,
2243  * or you will get hosed, I promise.
2244  */
2245 struct _GDate
2246
2247   guint julian_days : 32; /* julian days representation - we use a
2248                            *  bitfield hoping that 64 bit platforms
2249                            *  will pack this whole struct in one big
2250                            *  int 
2251                            */
2252
2253   guint julian : 1;    /* julian is valid */
2254   guint dmy    : 1;    /* dmy is valid */
2255
2256   /* DMY representation */
2257   guint day    : 6;  
2258   guint month  : 4; 
2259   guint year   : 16; 
2260 };
2261
2262 /* g_date_new() returns an invalid date, you then have to _set() stuff 
2263  * to get a usable object. You can also allocate a GDate statically,
2264  * then call g_date_clear() to initialize.
2265  */
2266 GDate*       g_date_new                   (void);
2267 GDate*       g_date_new_dmy               (GDateDay     day, 
2268                                            GDateMonth   month, 
2269                                            GDateYear    year);
2270 GDate*       g_date_new_julian            (guint32      julian_day);
2271 void         g_date_free                  (GDate       *date);
2272
2273 /* check g_date_valid() after doing an operation that might fail, like
2274  * _parse.  Almost all g_date operations are undefined on invalid
2275  * dates (the exceptions are the mutators, since you need those to
2276  * return to validity).  
2277  */
2278 gboolean     g_date_valid                 (GDate       *date);
2279 gboolean     g_date_valid_day             (GDateDay     day);
2280 gboolean     g_date_valid_month           (GDateMonth   month);
2281 gboolean     g_date_valid_year            (GDateYear    year);
2282 gboolean     g_date_valid_weekday         (GDateWeekday weekday);
2283 gboolean     g_date_valid_julian          (guint32      julian_date);
2284 gboolean     g_date_valid_dmy             (GDateDay     day,
2285                                            GDateMonth   month,
2286                                            GDateYear    year);
2287
2288 GDateWeekday g_date_weekday               (GDate       *date);
2289 GDateMonth   g_date_month                 (GDate       *date);
2290 GDateYear    g_date_year                  (GDate       *date);
2291 GDateDay     g_date_day                   (GDate       *date);
2292 guint32      g_date_julian                (GDate       *date);
2293 guint        g_date_day_of_year           (GDate       *date);
2294
2295 /* First monday/sunday is the start of week 1; if we haven't reached
2296  * that day, return 0. These are not ISO weeks of the year; that
2297  * routine needs to be added.
2298  * these functions return the number of weeks, starting on the
2299  * corrsponding day
2300  */
2301 guint        g_date_monday_week_of_year   (GDate      *date);
2302 guint        g_date_sunday_week_of_year   (GDate      *date);
2303
2304 /* If you create a static date struct you need to clear it to get it
2305  * in a sane state before use. You can clear a whole array at
2306  * once with the ndates argument.
2307  */
2308 void         g_date_clear                 (GDate       *date, 
2309                                            guint        n_dates);
2310
2311 /* The parse routine is meant for dates typed in by a user, so it
2312  * permits many formats but tries to catch common typos. If your data
2313  * needs to be strictly validated, it is not an appropriate function.
2314  */
2315 void         g_date_set_parse             (GDate       *date,
2316                                            const gchar *str);
2317 void         g_date_set_time              (GDate       *date, 
2318                                            GTime        time);
2319 void         g_date_set_month             (GDate       *date, 
2320                                            GDateMonth   month);
2321 void         g_date_set_day               (GDate       *date, 
2322                                            GDateDay     day);
2323 void         g_date_set_year              (GDate       *date,
2324                                            GDateYear    year);
2325 void         g_date_set_dmy               (GDate       *date,
2326                                            GDateDay     day,
2327                                            GDateMonth   month,
2328                                            GDateYear    y);
2329 void         g_date_set_julian            (GDate       *date,
2330                                            guint32      julian_date);
2331 gboolean     g_date_is_first_of_month     (GDate       *date);
2332 gboolean     g_date_is_last_of_month      (GDate       *date);
2333
2334 /* To go forward by some number of weeks just go forward weeks*7 days */
2335 void         g_date_add_days              (GDate       *date, 
2336                                            guint        n_days);
2337 void         g_date_subtract_days         (GDate       *date, 
2338                                            guint        n_days);
2339
2340 /* If you add/sub months while day > 28, the day might change */
2341 void         g_date_add_months            (GDate       *date,
2342                                            guint        n_months);
2343 void         g_date_subtract_months       (GDate       *date,
2344                                            guint        n_months);
2345
2346 /* If it's feb 29, changing years can move you to the 28th */
2347 void         g_date_add_years             (GDate       *date,
2348                                            guint        n_years);
2349 void         g_date_subtract_years        (GDate       *date,
2350                                            guint        n_years);
2351 gboolean     g_date_is_leap_year          (GDateYear    year);
2352 guint8       g_date_days_in_month         (GDateMonth   month, 
2353                                            GDateYear    year);
2354 guint8       g_date_monday_weeks_in_year  (GDateYear    year);
2355 guint8       g_date_sunday_weeks_in_year  (GDateYear    year);
2356
2357 /* qsort-friendly (with a cast...) */
2358 gint         g_date_compare               (GDate       *lhs,
2359                                            GDate       *rhs);
2360 void         g_date_to_struct_tm          (GDate       *date,
2361                                            struct tm   *tm);
2362
2363 /* Just like strftime() except you can only use date-related formats.
2364  *   Using a time format is undefined.
2365  */
2366 gsize        g_date_strftime              (gchar       *s,
2367                                            gsize        slen,
2368                                            const gchar *format,
2369                                            GDate       *date);
2370
2371
2372 /* GRelation
2373  *
2374  * Indexed Relations.  Imagine a really simple table in a
2375  * database.  Relations are not ordered.  This data type is meant for
2376  * maintaining a N-way mapping.
2377  *
2378  * g_relation_new() creates a relation with FIELDS fields
2379  *
2380  * g_relation_destroy() frees all resources
2381  * g_tuples_destroy() frees the result of g_relation_select()
2382  *
2383  * g_relation_index() indexes relation FIELD with the provided
2384  *   equality and hash functions.  this must be done before any
2385  *   calls to insert are made.
2386  *
2387  * g_relation_insert() inserts a new tuple.  you are expected to
2388  *   provide the right number of fields.
2389  *
2390  * g_relation_delete() deletes all relations with KEY in FIELD
2391  * g_relation_select() returns ...
2392  * g_relation_count() counts ...
2393  */
2394
2395 GRelation* g_relation_new     (gint         fields);
2396 void       g_relation_destroy (GRelation   *relation);
2397 void       g_relation_index   (GRelation   *relation,
2398                                gint         field,
2399                                GHashFunc    hash_func,
2400                                GCompareFunc key_compare_func);
2401 void       g_relation_insert  (GRelation   *relation,
2402                                ...);
2403 gint       g_relation_delete  (GRelation   *relation,
2404                                gconstpointer  key,
2405                                gint         field);
2406 GTuples*   g_relation_select  (GRelation   *relation,
2407                                gconstpointer  key,
2408                                gint         field);
2409 gint       g_relation_count   (GRelation   *relation,
2410                                gconstpointer  key,
2411                                gint         field);
2412 gboolean   g_relation_exists  (GRelation   *relation,
2413                                ...);
2414 void       g_relation_print   (GRelation   *relation);
2415
2416 void       g_tuples_destroy   (GTuples     *tuples);
2417 gpointer   g_tuples_index     (GTuples     *tuples,
2418                                gint         index,
2419                                gint         field);
2420
2421
2422 /* GRand - a good and fast random number generator: Mersenne Twister 
2423  * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2424  * The range functions return a value in the intervall [min,max).
2425  * int          -> [0..2^32-1]
2426  * int_range    -> [min..max-1]
2427  * double       -> [0..1)
2428  * double_range -> [min..max)
2429  */
2430
2431 GRand*  g_rand_new_with_seed   (guint32     seed);
2432 GRand*  g_rand_new             (void);
2433 void    g_rand_free            (GRand      *rand);
2434
2435 void    g_rand_set_seed        (GRand      *rand, 
2436                                 guint32     seed);
2437 guint32 g_rand_int             (GRand      *rand);
2438 gint32  g_rand_int_range       (GRand      *rand, 
2439                                 gint32      min, 
2440                                 gint32      max);
2441 gdouble g_rand_double          (GRand      *rand);
2442 gdouble g_rand_double_range    (GRand      *rand, 
2443                                 gdouble     min, 
2444                                 gdouble     max);
2445
2446 void    g_random_set_seed      (guint32     seed);
2447 guint32 g_random_int           (void);
2448 gint32  g_random_int_range     (gint32      min, 
2449                                 gint32      max);
2450 gdouble g_random_double        (void);
2451 gdouble g_random_double_range  (gdouble     min, 
2452                                 gdouble     max);
2453  
2454
2455 /* Prime numbers.
2456  */
2457
2458 /* This function returns prime numbers spaced by approximately 1.5-2.0
2459  * and is for use in resizing data structures which prefer
2460  * prime-valued sizes.  The closest spaced prime function returns the
2461  * next largest prime, or the highest it knows about which is about
2462  * MAXINT/4.
2463  */
2464 guint      g_spaced_primes_closest (guint num);
2465
2466
2467 /* GIOChannel
2468  */
2469
2470 typedef struct _GIOFuncs GIOFuncs;
2471 typedef enum
2472 {
2473   G_IO_ERROR_NONE,
2474   G_IO_ERROR_AGAIN,
2475   G_IO_ERROR_INVAL,
2476   G_IO_ERROR_UNKNOWN
2477 } GIOError;
2478 typedef enum
2479 {
2480   G_SEEK_CUR,
2481   G_SEEK_SET,
2482   G_SEEK_END
2483 } GSeekType;
2484 typedef enum
2485 {
2486   G_IO_IN       GLIB_SYSDEF_POLLIN,
2487   G_IO_OUT      GLIB_SYSDEF_POLLOUT,
2488   G_IO_PRI      GLIB_SYSDEF_POLLPRI,
2489   G_IO_ERR      GLIB_SYSDEF_POLLERR,
2490   G_IO_HUP      GLIB_SYSDEF_POLLHUP,
2491   G_IO_NVAL     GLIB_SYSDEF_POLLNVAL
2492 } GIOCondition;
2493
2494 struct _GIOChannel
2495 {
2496   guint channel_flags;
2497   guint ref_count;
2498   GIOFuncs *funcs;
2499 };
2500
2501 typedef gboolean (*GIOFunc) (GIOChannel   *source,
2502                              GIOCondition  condition,
2503                              gpointer      data);
2504 struct _GIOFuncs
2505 {
2506   GIOError (*io_read)   (GIOChannel     *channel, 
2507                          gchar          *buf, 
2508                          guint           count,
2509                          guint          *bytes_read);
2510   GIOError (*io_write)  (GIOChannel     *channel, 
2511                          gchar          *buf, 
2512                          guint           count,
2513                          guint          *bytes_written);
2514   GIOError (*io_seek)   (GIOChannel     *channel, 
2515                          gint            offset, 
2516                          GSeekType       type);
2517   void (*io_close)      (GIOChannel     *channel);
2518   guint (*io_add_watch) (GIOChannel     *channel,
2519                          gint            priority,
2520                          GIOCondition    condition,
2521                          GIOFunc         func,
2522                          gpointer        user_data,
2523                          GDestroyNotify  notify);
2524   void (*io_free)       (GIOChannel     *channel);
2525 };
2526
2527 void        g_io_channel_init   (GIOChannel    *channel);
2528 void        g_io_channel_ref    (GIOChannel    *channel);
2529 void        g_io_channel_unref  (GIOChannel    *channel);
2530 GIOError    g_io_channel_read   (GIOChannel    *channel, 
2531                                  gchar         *buf, 
2532                                  guint          count,
2533                                  guint         *bytes_read);
2534 GIOError  g_io_channel_write    (GIOChannel    *channel, 
2535                                  gchar         *buf, 
2536                                  guint          count,
2537                                  guint         *bytes_written);
2538 GIOError  g_io_channel_seek     (GIOChannel    *channel,
2539                                  gint           offset, 
2540                                  GSeekType      type);
2541 void      g_io_channel_close    (GIOChannel    *channel);
2542 guint     g_io_add_watch_full   (GIOChannel    *channel,
2543                                  gint           priority,
2544                                  GIOCondition   condition,
2545                                  GIOFunc        func,
2546                                  gpointer       user_data,
2547                                  GDestroyNotify notify);
2548 guint    g_io_add_watch         (GIOChannel    *channel,
2549                                  GIOCondition   condition,
2550                                  GIOFunc        func,
2551                                  gpointer       user_data);
2552
2553
2554 /* Main loop
2555  */
2556 typedef struct _GTimeVal        GTimeVal;
2557 typedef struct _GSourceFuncs    GSourceFuncs;
2558 typedef struct _GMainLoop       GMainLoop;      /* Opaque */
2559
2560 struct _GTimeVal
2561 {
2562   glong tv_sec;
2563   glong tv_usec;
2564 };
2565 struct _GSourceFuncs
2566 {
2567   gboolean (*prepare)  (gpointer  source_data, 
2568                         GTimeVal *current_time,
2569                         gint     *timeout,
2570                         gpointer  user_data);
2571   gboolean (*check)    (gpointer  source_data,
2572                         GTimeVal *current_time,
2573                         gpointer  user_data);
2574   gboolean (*dispatch) (gpointer  source_data, 
2575                         GTimeVal *current_time,
2576                         gpointer  user_data);
2577   GDestroyNotify destroy;
2578 };
2579
2580 /* Standard priorities */
2581
2582 #define G_PRIORITY_HIGH            -100
2583 #define G_PRIORITY_DEFAULT          0
2584 #define G_PRIORITY_HIGH_IDLE        100
2585 #define G_PRIORITY_DEFAULT_IDLE     200
2586 #define G_PRIORITY_LOW              300
2587
2588 typedef gboolean (*GSourceFunc) (gpointer data);
2589
2590 /* Hooks for adding to the main loop */
2591 guint    g_source_add                        (gint           priority, 
2592                                               gboolean       can_recurse,
2593                                               GSourceFuncs  *funcs,
2594                                               gpointer       source_data, 
2595                                               gpointer       user_data,
2596                                               GDestroyNotify notify);
2597 gboolean g_source_remove                     (guint          tag);
2598 gboolean g_source_remove_by_user_data        (gpointer       user_data);
2599 gboolean g_source_remove_by_source_data      (gpointer       source_data);
2600 gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
2601                                               gpointer       user_data);
2602
2603 void g_get_current_time             (GTimeVal      *result);
2604
2605 /* Running the main loop */
2606 GMainLoop*      g_main_new              (gboolean        is_running);
2607 void            g_main_run              (GMainLoop      *loop);
2608 void            g_main_quit             (GMainLoop      *loop);
2609 void            g_main_destroy          (GMainLoop      *loop);
2610 gboolean        g_main_is_running       (GMainLoop      *loop);
2611
2612 /* Run a single iteration of the mainloop. If block is FALSE,
2613  * will never block
2614  */
2615 gboolean        g_main_iteration        (gboolean       may_block);
2616
2617 /* See if any events are pending */
2618 gboolean        g_main_pending          (void);
2619
2620 /* Idles and timeouts */
2621 guint           g_timeout_add_full      (gint           priority,
2622                                          guint          interval, 
2623                                          GSourceFunc    function,
2624                                          gpointer       data,
2625                                          GDestroyNotify notify);
2626 guint           g_timeout_add           (guint          interval,
2627                                          GSourceFunc    function,
2628                                          gpointer       data);
2629 guint           g_idle_add              (GSourceFunc    function,
2630                                          gpointer       data);
2631 guint           g_idle_add_full         (gint           priority,
2632                                          GSourceFunc    function,
2633                                          gpointer       data,
2634                                          GDestroyNotify destroy);
2635 gboolean        g_idle_remove_by_data   (gpointer       data);
2636
2637 /* GPollFD
2638  *
2639  * System-specific IO and main loop calls
2640  *
2641  * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2642  * descriptor as provided by the C runtime) that can be used by
2643  * MsgWaitForMultipleObjects. This does *not* include file handles
2644  * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2645  * WSAEventSelect to signal events when a SOCKET is readable).
2646  *
2647  * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2648  * indicate polling for messages. These message queue GPollFDs should
2649  * be added with the g_main_poll_win32_msg_add function.
2650  *
2651  * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2652  * (GTK) programs, as GDK itself wants to read messages and convert them
2653  * to GDK events.
2654  *
2655  * So, unless you really know what you are doing, it's best not to try
2656  * to use the main loop polling stuff for your own needs on
2657  * Win32. It's really only written for the GIMP's needs so
2658  * far.
2659  */
2660
2661 typedef struct _GPollFD GPollFD;
2662 typedef gint    (*GPollFunc)    (GPollFD *ufds,
2663                                  guint    nfsd,
2664                                  gint     timeout);
2665 struct _GPollFD
2666 {
2667   gint          fd;
2668   gushort       events;
2669   gushort       revents;
2670 };
2671
2672 void        g_main_add_poll          (GPollFD    *fd,
2673                                       gint        priority);
2674 void        g_main_remove_poll       (GPollFD    *fd);
2675 void        g_main_set_poll_func     (GPollFunc   func);
2676
2677 /* On Unix, IO channels created with this function for any file
2678  * descriptor or socket.
2679  *
2680  * On Win32, use this only for plain files opened with the MSVCRT (the
2681  * Microsoft run-time C library) _open(), including file descriptors
2682  * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2683  * Actually, don't do even that, this code isn't done yet.
2684  *
2685  * The term file descriptor as used in the context of Win32 refers to
2686  * the emulated Unix-like file descriptors MSVCRT provides.
2687  */
2688 GIOChannel* g_io_channel_unix_new    (int         fd);
2689 gint        g_io_channel_unix_get_fd (GIOChannel *channel);
2690
2691 #ifdef NATIVE_WIN32
2692
2693 GUTILS_C_VAR guint g_pipe_readable_msg;
2694
2695 #define G_WIN32_MSG_HANDLE 19981206
2696
2697 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2698  * should *not* use this. (In fact, I can't think of any program that
2699  * would want to use this, but it's here just for completeness's sake.
2700  */
2701 void        g_main_poll_win32_msg_add(gint        priority,
2702                                       GPollFD    *fd,
2703                                       guint       hwnd);
2704
2705 /* An IO channel for Windows messages for window handle hwnd. */
2706 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2707
2708 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2709  * _pipe(), with no mechanism for the writer to tell the reader when
2710  * there is data in the pipe.
2711  *
2712  * This is not really implemented yet.
2713  */
2714 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2715
2716 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2717  * Windows user messages used to signal data in the pipe for the
2718  * reader.
2719  *
2720  * fd is the file descriptor. For the write end, peer is the thread id
2721  * of the reader, and peer_fd is his file descriptor for the read end
2722  * of the pipe.
2723  *
2724  * This is used by the GIMP, and works.
2725  */
2726 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int   fd,
2727                                                       guint peer,
2728                                                       int   peer_fd);
2729
2730 void        g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2731                                                      guint       peer,
2732                                                      int         peer_fd);
2733
2734 void        g_io_channel_win32_pipe_readable (int   fd,
2735                                               guint offset);
2736
2737 /* Get the C runtime file descriptor of a channel. */
2738 gint        g_io_channel_win32_get_fd (GIOChannel *channel);
2739
2740 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2741  * actually a SOCKET.
2742  */
2743 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2744
2745 #endif
2746
2747 /* Windows emulation stubs for common Unix functions
2748  */
2749 #ifdef NATIVE_WIN32
2750 #  define MAXPATHLEN 1024
2751
2752 #ifdef _MSC_VER
2753 typedef int pid_t;
2754 #endif
2755
2756 /* These POSIXish functions are available in the Microsoft C library
2757  * prefixed with underscore (which of course technically speaking is
2758  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2759  * prevents Microsoft from practically requiring you to include
2760  * <windows.h> every now and then...).
2761  *
2762  * You still need to include the appropriate headers to get the
2763  * prototypes, like <stdio.h>, <io.h>, <direct.h> or <process.h>.
2764  *
2765  * For some functions, we provide emulators in glib, which are prefixed
2766  * with gwin_.
2767  */
2768 #    define getcwd              _getcwd
2769 #    define getpid              _getpid
2770 #    define access              _access
2771 #ifdef __GNUC__
2772 #    define stat                _stat
2773 #    define fileno              _fileno
2774 #endif
2775 #    define fstat               _fstat
2776 #    define unlink              _unlink
2777 #    define open                _open
2778 #    define read                _read
2779 #    define write               _write
2780 #    define lseek               _lseek
2781 #    define close               _close
2782 #    define rmdir               _rmdir
2783 #    define pipe(phandles)      _pipe (phandles, 4096, _O_BINARY)
2784 #    define popen               _popen
2785 #    define pclose              _pclose
2786 #    define fdopen              _fdopen
2787 #    define hypot               _hypot
2788 #    define ftruncate(fd, size) gwin_ftruncate (fd, size)
2789 #    define opendir             gwin_opendir
2790 #    define readdir             gwin_readdir
2791 #    define rewinddir           gwin_rewinddir
2792 #    define closedir            gwin_closedir
2793 #    define NAME_MAX 255
2794 struct DIR
2795 {
2796   gchar    *dir_name;
2797   gboolean  just_opened;
2798   guint     find_file_handle;
2799   gpointer  find_file_data;
2800 };
2801 typedef struct DIR DIR;
2802 struct dirent
2803 {
2804   gchar  d_name[NAME_MAX + 1];
2805 };
2806 /* emulation functions */
2807 extern int      gwin_ftruncate  (gint            f,
2808                                  guint           size);
2809 DIR*            gwin_opendir    (const gchar    *dirname);
2810 struct dirent*  gwin_readdir    (DIR            *dir);
2811 void            gwin_rewinddir  (DIR            *dir);
2812 gint            gwin_closedir   (DIR            *dir);
2813 #endif   /* NATIVE_WIN32 */
2814
2815
2816 /* GLib Thread support
2817  */
2818
2819 typedef void            (*GThreadFunc)          (gpointer       value);
2820
2821 typedef enum
2822 {
2823     G_THREAD_PRIORITY_LOW,
2824     G_THREAD_PRIORITY_NORMAL,
2825     G_THREAD_PRIORITY_HIGH,
2826     G_THREAD_PRIORITY_URGENT
2827 } GThreadPriority;
2828
2829 typedef struct _GThread         GThread;
2830 struct  _GThread
2831 {
2832   GThreadPriority priority;
2833   gboolean bound;
2834   gboolean joinable;
2835 };
2836
2837 typedef struct _GMutex          GMutex;
2838 typedef struct _GCond           GCond;
2839 typedef struct _GPrivate        GPrivate;
2840 typedef struct _GStaticPrivate  GStaticPrivate;
2841
2842 typedef struct _GThreadFunctions GThreadFunctions;
2843 struct _GThreadFunctions
2844 {
2845   GMutex*  (*mutex_new)           (void);
2846   void     (*mutex_lock)          (GMutex               *mutex);
2847   gboolean (*mutex_trylock)       (GMutex               *mutex);
2848   void     (*mutex_unlock)        (GMutex               *mutex);
2849   void     (*mutex_free)          (GMutex               *mutex);
2850   GCond*   (*cond_new)            (void);
2851   void     (*cond_signal)         (GCond                *cond);
2852   void     (*cond_broadcast)      (GCond                *cond);
2853   void     (*cond_wait)           (GCond                *cond,
2854                                    GMutex               *mutex);
2855   gboolean (*cond_timed_wait)     (GCond                *cond,
2856                                    GMutex               *mutex, 
2857                                    GTimeVal             *end_time);
2858   void      (*cond_free)          (GCond                *cond);
2859   GPrivate* (*private_new)        (GDestroyNotify        destructor);
2860   gpointer  (*private_get)        (GPrivate             *private_key);
2861   void      (*private_set)        (GPrivate             *private_key,
2862                                    gpointer              data);
2863   gpointer  (*thread_create)      (GThreadFunc           thread_func,
2864                                    gpointer              arg,
2865                                    gulong                stack_size,
2866                                    gboolean              joinable,
2867                                    gboolean              bound,
2868                                    GThreadPriority       priority);
2869   void      (*thread_yield)       (void);
2870   void      (*thread_join)        (gpointer              thread);
2871   void      (*thread_exit)        (void);
2872   void      (*thread_set_priority)(gpointer              thread, 
2873                                    GThreadPriority       priority);
2874   gpointer  (*thread_self)        (void);
2875 };
2876
2877 GUTILS_C_VAR GThreadFunctions   g_thread_functions_for_glib_use;
2878 GUTILS_C_VAR gboolean           g_thread_use_default_impl;
2879 GUTILS_C_VAR gboolean           g_threads_got_initialized;
2880
2881 /* initializes the mutex/cond/private implementation for glib, might
2882  * only be called once, and must not be called directly or indirectly
2883  * from another glib-function, e.g. as a callback.
2884  */
2885 void    g_thread_init   (GThreadFunctions       *vtable);
2886
2887 /* internal function for fallback static mutex implementation */
2888 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
2889
2890 /* shorthands for conditional and unconditional function calls */
2891 #define G_THREAD_UF(name, arglist) \
2892     (*g_thread_functions_for_glib_use . name) arglist
2893 #define G_THREAD_CF(name, fail, arg) \
2894     (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2895 /* keep in mind, all those mutexes and static mutexes are not 
2896  * recursive in general, don't rely on that
2897  */
2898 #define g_thread_supported()    (g_threads_got_initialized)
2899 #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
2900 #define g_mutex_lock(mutex)      G_THREAD_CF (mutex_lock,     (void)0, (mutex))
2901 #define g_mutex_trylock(mutex)   G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
2902 #define g_mutex_unlock(mutex)    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
2903 #define g_mutex_free(mutex)      G_THREAD_CF (mutex_free,     (void)0, (mutex))
2904 #define g_cond_new()             G_THREAD_UF (cond_new,       ())
2905 #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
2906 #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
2907 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait,      (void)0, (cond, \
2908                                                                         mutex))
2909 #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
2910 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2911                                                               TRUE, \
2912                                                               (cond, mutex, \
2913                                                                abs_time))
2914 #define g_private_new(destructor)         G_THREAD_UF (private_new, (destructor))
2915 #define g_private_get(private_key)        G_THREAD_CF (private_get, \
2916                                                        ((gpointer)private_key), \
2917                                                        (private_key))
2918 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2919                                                        (void) (private_key = \
2920                                                         (GPrivate*) (value)), \
2921                                                        (private_key, value))
2922 #define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())
2923 #define g_thread_exit()               G_THREAD_CF (thread_exit, (void)0, ())
2924
2925 GThread* g_thread_create (GThreadFunc            thread_func,
2926                           gpointer               arg,
2927                           gulong                 stack_size,
2928                           gboolean               joinable,
2929                           gboolean               bound,
2930                           GThreadPriority        priority);
2931 GThread* g_thread_self ();
2932 void g_thread_join (GThread* thread);
2933 void g_thread_set_priority (GThread* thread, 
2934                             GThreadPriority priority);
2935
2936 /* GStaticMutexes can be statically initialized with the value
2937  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2938  * much easier, than having to explicitly allocate the mutex before
2939  * use
2940  */
2941 #define g_static_mutex_lock(mutex) \
2942     g_mutex_lock (g_static_mutex_get_mutex (mutex))
2943 #define g_static_mutex_trylock(mutex) \
2944     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2945 #define g_static_mutex_unlock(mutex) \
2946     g_mutex_unlock (g_static_mutex_get_mutex (mutex)) 
2947
2948 struct _GStaticPrivate
2949 {
2950   guint index;
2951 };
2952 #define G_STATIC_PRIVATE_INIT { 0 }
2953 gpointer g_static_private_get (GStaticPrivate   *private_key);
2954 void     g_static_private_set (GStaticPrivate   *private_key, 
2955                                gpointer          data,
2956                                GDestroyNotify    notify);
2957 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
2958                                           GThread        *thread);
2959 void g_static_private_set_for_thread (GStaticPrivate *private_key, 
2960                                       GThread        *thread,
2961                                       gpointer        data,
2962                                       GDestroyNotify  notify);
2963 #ifndef G_STATIC_REC_MUTEX_INIT
2964 /* if GStaticRecMutex is not just a differently initialized GStaticMutex, 
2965  * the following is done:
2966  * This can't be done in glibconfig.h, as GStaticPrivate and gboolean
2967  * are not yet known there 
2968  */
2969 typedef struct _GStaticRecMutex GStaticRecMutex;
2970 struct _GStaticRecMutex
2971 {
2972   GStaticMutex mutex;
2973   GStaticPrivate counter; 
2974 };
2975 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT, G_STATIC_PRIVATE_INIT }
2976 void     g_static_rec_mutex_lock    (GStaticRecMutex* mutex);
2977 gboolean g_static_rec_mutex_trylock (GStaticRecMutex* mutex);
2978 void     g_static_rec_mutex_unlock  (GStaticRecMutex* mutex);
2979 #define  g_static_rec_mutex_get_mutex(mutex) ((mutex)->mutex)
2980 #endif /* G_STATIC_REC_MUTEX_INIT */
2981
2982 typedef struct _GStaticRWLock GStaticRWLock;
2983 struct _GStaticRWLock
2984 {
2985   GStaticMutex mutex; 
2986   GCond *read_cond;
2987   GCond *write_cond;
2988   guint read_counter;
2989   gboolean write;
2990   guint want_to_write;
2991 };
2992
2993 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
2994
2995 void      g_static_rw_lock_reader_lock    (GStaticRWLock* lock);
2996 gboolean  g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
2997 void      g_static_rw_lock_reader_unlock  (GStaticRWLock* lock);
2998 void      g_static_rw_lock_writer_lock    (GStaticRWLock* lock);
2999 gboolean  g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3000 void      g_static_rw_lock_writer_unlock  (GStaticRWLock* lock);
3001 void      g_static_rw_lock_free (GStaticRWLock* lock);
3002
3003 /* these are some convenience macros that expand to nothing if GLib
3004  * was configured with --disable-threads. for using StaticMutexes,
3005  * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3006  * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3007  * declare such an globally defined lock. name is a unique identifier
3008  * for the protected varibale or code portion. locking, testing and
3009  * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3010  * G_TRYLOCK() respectively.  
3011  */
3012 extern void glib_dummy_decl (void);
3013 #define G_LOCK_NAME(name)               (g__ ## name ## _lock)
3014 #ifdef  G_THREADS_ENABLED
3015 #  define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
3016 #  define G_LOCK_DEFINE(name)           \
3017     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT 
3018 #  define G_LOCK_EXTERN(name)           extern GStaticMutex G_LOCK_NAME (name)
3019
3020 #  ifdef G_DEBUG_LOCKS
3021 #    define G_LOCK(name)                G_STMT_START{             \
3022         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
3023                "file %s: line %d (%s): locking: %s ",             \
3024                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3025                #name);                                            \
3026         g_static_mutex_lock (&G_LOCK_NAME (name));                \
3027      }G_STMT_END
3028 #    define G_UNLOCK(name)              G_STMT_START{             \
3029         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
3030                "file %s: line %d (%s): unlocking: %s ",           \
3031                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3032                #name);                                            \
3033        g_static_mutex_unlock (&G_LOCK_NAME (name));               \
3034      }G_STMT_END
3035 #    define G_TRYLOCK(name)             G_STMT_START{             \
3036         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
3037                "file %s: line %d (%s): try locking: %s ",         \
3038                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3039                #name);                                            \
3040      }G_STMT_END,       g_static_mutex_trylock (&G_LOCK_NAME (name))
3041 #  else  /* !G_DEBUG_LOCKS */
3042 #    define G_LOCK(name) g_static_mutex_lock       (&G_LOCK_NAME (name)) 
3043 #    define G_UNLOCK(name) g_static_mutex_unlock   (&G_LOCK_NAME (name))
3044 #    define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3045 #  endif /* !G_DEBUG_LOCKS */
3046 #else   /* !G_THREADS_ENABLED */
3047 #  define G_LOCK_DEFINE_STATIC(name)    extern void glib_dummy_decl (void)
3048 #  define G_LOCK_DEFINE(name)           extern void glib_dummy_decl (void)
3049 #  define G_LOCK_EXTERN(name)           extern void glib_dummy_decl (void)
3050 #  define G_LOCK(name)
3051 #  define G_UNLOCK(name)
3052 #  define G_TRYLOCK(name)               (FALSE)
3053 #endif  /* !G_THREADS_ENABLED */
3054
3055 #ifdef __cplusplus
3056 }
3057 #endif /* __cplusplus */
3058
3059
3060 #endif /* __G_LIB_H__ */