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