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