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