reverted the g_hash_table_set_key_freefunc() addition, since it's to
[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 inlinig 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 void            (*GHookFunc)            (gpointer        data);
769 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
770 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
771                                                  GHook          *hook);
772 typedef void            (*GLogFunc)             (const gchar   *log_domain,
773                                                  GLogLevelFlags log_level,
774                                                  const gchar   *message,
775                                                  gpointer       user_data);
776 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
777                                                  gpointer       data);
778 typedef void            (*GNodeForeachFunc)     (GNode         *node,
779                                                  gpointer       data);
780 typedef gint            (*GSearchFunc)          (gpointer       key,
781                                                  gpointer       data);
782 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
783                                                  gchar         *message,
784                                                  gint           error);
785 typedef gint            (*GTraverseFunc)        (gpointer       key,
786                                                  gpointer       value,
787                                                  gpointer       data);
788 typedef void            (*GVoidFunc)            (void);
789
790
791 struct _GList
792 {
793   gpointer data;
794   GList *next;
795   GList *prev;
796 };
797
798 struct _GSList
799 {
800   gpointer data;
801   GSList *next;
802 };
803
804 struct _GString
805 {
806   gchar *str;
807   gint len;
808 };
809
810 struct _GArray
811 {
812   gchar *data;
813   guint len;
814 };
815
816 struct _GByteArray
817 {
818   guint8 *data;
819   guint   len;
820 };
821
822 struct _GPtrArray
823 {
824   gpointer *pdata;
825   guint     len;
826 };
827
828 struct _GTuples
829 {
830   guint len;
831 };
832
833 struct _GDebugKey
834 {
835   gchar *key;
836   guint  value;
837 };
838
839
840 /* Doubly linked lists
841  */
842 void   g_list_push_allocator    (GAllocator     *allocator);
843 void   g_list_pop_allocator     (void);
844 GList* g_list_alloc             (void);
845 void   g_list_free              (GList          *list);
846 void   g_list_free_1            (GList          *list);
847 GList* g_list_append            (GList          *list,
848                                  gpointer        data);
849 GList* g_list_prepend           (GList          *list,
850                                  gpointer        data);
851 GList* g_list_insert            (GList          *list,
852                                  gpointer        data,
853                                  gint            position);
854 GList* g_list_insert_sorted     (GList          *list,
855                                  gpointer        data,
856                                  GCompareFunc    func);
857 GList* g_list_concat            (GList          *list1,
858                                  GList          *list2);
859 GList* g_list_remove            (GList          *list,
860                                  gpointer        data);
861 GList* g_list_remove_link       (GList          *list,
862                                  GList          *llink);
863 GList* g_list_reverse           (GList          *list);
864 GList* g_list_copy              (GList          *list);
865 GList* g_list_nth               (GList          *list,
866                                  guint           n);
867 GList* g_list_find              (GList          *list,
868                                  gpointer        data);
869 GList* g_list_find_custom       (GList          *list,
870                                  gpointer        data,
871                                  GCompareFunc    func);
872 gint   g_list_position          (GList          *list,
873                                  GList          *llink);
874 gint   g_list_index             (GList          *list,
875                                  gpointer        data);
876 GList* g_list_last              (GList          *list);
877 GList* g_list_first             (GList          *list);
878 guint  g_list_length            (GList          *list);
879 void   g_list_foreach           (GList          *list,
880                                  GFunc           func,
881                                  gpointer        user_data);
882 GList* g_list_sort              (GList          *list,
883                                  GCompareFunc    compare_func);
884 gpointer g_list_nth_data        (GList          *list,
885                                  guint           n);
886 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
887 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
888
889
890 /* Singly linked lists
891  */
892 void    g_slist_push_allocator  (GAllocator     *allocator);
893 void    g_slist_pop_allocator   (void);
894 GSList* g_slist_alloc           (void);
895 void    g_slist_free            (GSList         *list);
896 void    g_slist_free_1          (GSList         *list);
897 GSList* g_slist_append          (GSList         *list,
898                                  gpointer        data);
899 GSList* g_slist_prepend         (GSList         *list,
900                                  gpointer        data);
901 GSList* g_slist_insert          (GSList         *list,
902                                  gpointer        data,
903                                  gint            position);
904 GSList* g_slist_insert_sorted   (GSList         *list,
905                                  gpointer        data,
906                                  GCompareFunc    func);
907 GSList* g_slist_concat          (GSList         *list1,
908                                  GSList         *list2);
909 GSList* g_slist_remove          (GSList         *list,
910                                  gpointer        data);
911 GSList* g_slist_remove_link     (GSList         *list,
912                                  GSList         *llink);
913 GSList* g_slist_reverse         (GSList         *list);
914 GSList* g_slist_copy            (GSList         *list);
915 GSList* g_slist_nth             (GSList         *list,
916                                  guint           n);
917 GSList* g_slist_find            (GSList         *list,
918                                  gpointer        data);
919 GSList* g_slist_find_custom     (GSList         *list,
920                                  gpointer        data,
921                                  GCompareFunc    func);
922 gint    g_slist_position        (GSList         *list,
923                                  GSList         *llink);
924 gint    g_slist_index           (GSList         *list,
925                                  gpointer        data);
926 GSList* g_slist_last            (GSList         *list);
927 guint   g_slist_length          (GSList         *list);
928 void    g_slist_foreach         (GSList         *list,
929                                  GFunc           func,
930                                  gpointer        user_data);
931 GSList*  g_slist_sort           (GSList          *list,
932                                  GCompareFunc    compare_func);
933 gpointer g_slist_nth_data       (GSList         *list,
934                                  guint           n);
935 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
936
937
938 /* Hash tables
939  */
940 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
941                                          GCompareFunc    key_compare_func);
942 void        g_hash_table_destroy        (GHashTable     *hash_table);
943 void        g_hash_table_insert         (GHashTable     *hash_table,
944                                          gpointer        key,
945                                          gpointer        value);
946 void        g_hash_table_remove         (GHashTable     *hash_table,
947                                          gconstpointer   key);
948 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
949                                          gconstpointer   key);
950 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
951                                          gconstpointer   lookup_key,
952                                          gpointer       *orig_key,
953                                          gpointer       *value);
954 void        g_hash_table_freeze         (GHashTable     *hash_table);
955 void        g_hash_table_thaw           (GHashTable     *hash_table);
956 void        g_hash_table_foreach        (GHashTable     *hash_table,
957                                          GHFunc          func,
958                                          gpointer        user_data);
959 gint        g_hash_table_foreach_remove (GHashTable     *hash_table,
960                                          GHRFunc         func,
961                                          gpointer        user_data);
962 gint        g_hash_table_size           (GHashTable     *hash_table);
963
964
965 /* Caches
966  */
967 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
968                                 GCacheDestroyFunc  value_destroy_func,
969                                 GCacheDupFunc      key_dup_func,
970                                 GCacheDestroyFunc  key_destroy_func,
971                                 GHashFunc          hash_key_func,
972                                 GHashFunc          hash_value_func,
973                                 GCompareFunc       key_compare_func);
974 void     g_cache_destroy       (GCache            *cache);
975 gpointer g_cache_insert        (GCache            *cache,
976                                 gpointer           key);
977 void     g_cache_remove        (GCache            *cache,
978                                 gpointer           value);
979 void     g_cache_key_foreach   (GCache            *cache,
980                                 GHFunc             func,
981                                 gpointer           user_data);
982 void     g_cache_value_foreach (GCache            *cache,
983                                 GHFunc             func,
984                                 gpointer           user_data);
985
986
987 /* Balanced binary trees
988  */
989 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
990 void     g_tree_destroy  (GTree         *tree);
991 void     g_tree_insert   (GTree         *tree,
992                           gpointer       key,
993                           gpointer       value);
994 void     g_tree_remove   (GTree         *tree,
995                           gpointer       key);
996 gpointer g_tree_lookup   (GTree         *tree,
997                           gpointer       key);
998 void     g_tree_traverse (GTree         *tree,
999                           GTraverseFunc  traverse_func,
1000                           GTraverseType  traverse_type,
1001                           gpointer       data);
1002 gpointer g_tree_search   (GTree         *tree,
1003                           GSearchFunc    search_func,
1004                           gpointer       data);
1005 gint     g_tree_height   (GTree         *tree);
1006 gint     g_tree_nnodes   (GTree         *tree);
1007
1008
1009
1010 /* N-way tree implementation
1011  */
1012 struct _GNode
1013 {
1014   gpointer data;
1015   GNode   *next;
1016   GNode   *prev;
1017   GNode   *parent;
1018   GNode   *children;
1019 };
1020
1021 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1022                                  ((GNode*) (node))->prev == NULL && \
1023                                  ((GNode*) (node))->next == NULL)
1024 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1025
1026 void     g_node_push_allocator  (GAllocator       *allocator);
1027 void     g_node_pop_allocator   (void);
1028 GNode*   g_node_new             (gpointer          data);
1029 void     g_node_destroy         (GNode            *root);
1030 void     g_node_unlink          (GNode            *node);
1031 GNode*   g_node_insert          (GNode            *parent,
1032                                  gint              position,
1033                                  GNode            *node);
1034 GNode*   g_node_insert_before   (GNode            *parent,
1035                                  GNode            *sibling,
1036                                  GNode            *node);
1037 GNode*   g_node_prepend         (GNode            *parent,
1038                                  GNode            *node);
1039 guint    g_node_n_nodes         (GNode            *root,
1040                                  GTraverseFlags    flags);
1041 GNode*   g_node_get_root        (GNode            *node);
1042 gboolean g_node_is_ancestor     (GNode            *node,
1043                                  GNode            *descendant);
1044 guint    g_node_depth           (GNode            *node);
1045 GNode*   g_node_find            (GNode            *root,
1046                                  GTraverseType     order,
1047                                  GTraverseFlags    flags,
1048                                  gpointer          data);
1049
1050 /* convenience macros */
1051 #define g_node_append(parent, node)                             \
1052      g_node_insert_before ((parent), NULL, (node))
1053 #define g_node_insert_data(parent, position, data)              \
1054      g_node_insert ((parent), (position), g_node_new (data))
1055 #define g_node_insert_data_before(parent, sibling, data)        \
1056      g_node_insert_before ((parent), (sibling), g_node_new (data))
1057 #define g_node_prepend_data(parent, data)                       \
1058      g_node_prepend ((parent), g_node_new (data))
1059 #define g_node_append_data(parent, data)                        \
1060      g_node_insert_before ((parent), NULL, g_node_new (data))
1061
1062 /* traversal function, assumes that `node' is root
1063  * (only traverses `node' and its subtree).
1064  * this function is just a high level interface to
1065  * low level traversal functions, optimized for speed.
1066  */
1067 void     g_node_traverse        (GNode            *root,
1068                                  GTraverseType     order,
1069                                  GTraverseFlags    flags,
1070                                  gint              max_depth,
1071                                  GNodeTraverseFunc func,
1072                                  gpointer          data);
1073
1074 /* return the maximum tree height starting with `node', this is an expensive
1075  * operation, since we need to visit all nodes. this could be shortened by
1076  * adding `guint height' to struct _GNode, but then again, this is not very
1077  * often needed, and would make g_node_insert() more time consuming.
1078  */
1079 guint    g_node_max_height       (GNode *root);
1080
1081 void     g_node_children_foreach (GNode           *node,
1082                                   GTraverseFlags   flags,
1083                                   GNodeForeachFunc func,
1084                                   gpointer         data);
1085 void     g_node_reverse_children (GNode           *node);
1086 guint    g_node_n_children       (GNode           *node);
1087 GNode*   g_node_nth_child        (GNode           *node,
1088                                   guint            n);
1089 GNode*   g_node_last_child       (GNode           *node);
1090 GNode*   g_node_find_child       (GNode           *node,
1091                                   GTraverseFlags   flags,
1092                                   gpointer         data);
1093 gint     g_node_child_position   (GNode           *node,
1094                                   GNode           *child);
1095 gint     g_node_child_index      (GNode           *node,
1096                                   gpointer         data);
1097
1098 GNode*   g_node_first_sibling    (GNode           *node);
1099 GNode*   g_node_last_sibling     (GNode           *node);
1100
1101 #define  g_node_prev_sibling(node)      ((node) ? \
1102                                          ((GNode*) (node))->prev : NULL)
1103 #define  g_node_next_sibling(node)      ((node) ? \
1104                                          ((GNode*) (node))->next : NULL)
1105 #define  g_node_first_child(node)       ((node) ? \
1106                                          ((GNode*) (node))->children : NULL)
1107
1108
1109 /* Callback maintenance functions
1110  */
1111 #define G_HOOK_FLAG_USER_SHIFT  (4)
1112 typedef enum
1113 {
1114   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1115   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1116   G_HOOK_FLAG_MASK      = 0x0f
1117 } GHookFlagMask;
1118
1119 struct _GHookList
1120 {
1121   guint          seq_id;
1122   guint          hook_size;
1123   guint          is_setup : 1;
1124   GHook         *hooks;
1125   GMemChunk     *hook_memchunk;
1126   GHookFreeFunc  hook_free; /* virtual function */
1127 };
1128
1129 struct _GHook
1130 {
1131   gpointer       data;
1132   GHook         *next;
1133   GHook         *prev;
1134   guint          ref_count;
1135   guint          hook_id;
1136   guint          flags;
1137   gpointer       func;
1138   GDestroyNotify destroy;
1139 };
1140
1141 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1142                                           G_HOOK_FLAG_ACTIVE) != 0)
1143 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1144                                           G_HOOK_FLAG_IN_CALL) != 0)
1145 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1146                                          G_HOOK_ACTIVE (hook))
1147 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1148                                          ((GHook*) hook)->prev == NULL && \
1149                                          ((GHook*) hook)->hook_id == 0 && \
1150                                          ((GHook*) hook)->ref_count == 0)
1151
1152 void     g_hook_list_init               (GHookList              *hook_list,
1153                                          guint                   hook_size);
1154 void     g_hook_list_clear              (GHookList              *hook_list);
1155 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1156 void     g_hook_free                    (GHookList              *hook_list,
1157                                          GHook                  *hook);
1158 void     g_hook_ref                     (GHookList              *hook_list,
1159                                          GHook                  *hook);
1160 void     g_hook_unref                   (GHookList              *hook_list,
1161                                          GHook                  *hook);
1162 gboolean g_hook_destroy                 (GHookList              *hook_list,
1163                                          guint                   hook_id);
1164 void     g_hook_destroy_link            (GHookList              *hook_list,
1165                                          GHook                  *hook);
1166 void     g_hook_prepend                 (GHookList              *hook_list,
1167                                          GHook                  *hook);
1168 void     g_hook_insert_before           (GHookList              *hook_list,
1169                                          GHook                  *sibling,
1170                                          GHook                  *hook);
1171 void     g_hook_insert_sorted           (GHookList              *hook_list,
1172                                          GHook                  *hook,
1173                                          GHookCompareFunc        func);
1174 GHook*   g_hook_get                     (GHookList              *hook_list,
1175                                          guint                   hook_id);
1176 GHook*   g_hook_find                    (GHookList              *hook_list,
1177                                          gboolean                need_valids,
1178                                          GHookFindFunc           func,
1179                                          gpointer                data);
1180 GHook*   g_hook_find_data               (GHookList              *hook_list,
1181                                          gboolean                need_valids,
1182                                          gpointer                data);
1183 GHook*   g_hook_find_func               (GHookList              *hook_list,
1184                                          gboolean                need_valids,
1185                                          gpointer                func);
1186 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1187                                          gboolean                need_valids,
1188                                          gpointer                func,
1189                                          gpointer                data);
1190 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1191                                          gboolean                may_be_in_call);
1192 GHook*   g_hook_next_valid              (GHook                  *hook,
1193                                          gboolean                may_be_in_call);
1194
1195 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1196 gint     g_hook_compare_ids             (GHook                  *new_hook,
1197                                          GHook                  *sibling);
1198
1199 /* convenience macros */
1200 #define  g_hook_append( hook_list, hook )  \
1201      g_hook_insert_before ((hook_list), NULL, (hook))
1202
1203 /* invoke all valid hooks with the (*GHookFunc) signature.
1204  */
1205 void     g_hook_list_invoke             (GHookList              *hook_list,
1206                                          gboolean                may_recurse);
1207 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1208  * and destroy the hook if FALSE is returned.
1209  */
1210 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1211                                          gboolean                may_recurse);
1212 /* invoke a marshaller on all valid hooks.
1213  */
1214 void     g_hook_list_marshal            (GHookList              *hook_list,
1215                                          gboolean                may_recurse,
1216                                          GHookMarshaller         marshaller,
1217                                          gpointer                data);
1218
1219
1220 /* Fatal error handlers.
1221  * g_on_error_query() will prompt the user to either
1222  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1223  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1224  * process and shows a stack trace.
1225  * These function may cause different actions on non-unix platforms.
1226  * The prg_name arg is required by gdb to find the executable, if it is
1227  * passed as NULL, g_on_error_query() will try g_get_prgname().
1228  */
1229 void g_on_error_query (const gchar *prg_name);
1230 void g_on_error_stack_trace (const gchar *prg_name);
1231
1232
1233 /* Logging mechanism
1234  */
1235 extern          const gchar             *g_log_domain_glib;
1236 guint           g_log_set_handler       (const gchar    *log_domain,
1237                                          GLogLevelFlags  log_levels,
1238                                          GLogFunc        log_func,
1239                                          gpointer        user_data);
1240 void            g_log_remove_handler    (const gchar    *log_domain,
1241                                          guint           handler_id);
1242 void            g_log_default_handler   (const gchar    *log_domain,
1243                                          GLogLevelFlags  log_level,
1244                                          const gchar    *message,
1245                                          gpointer        unused_data);
1246 void            g_log                   (const gchar    *log_domain,
1247                                          GLogLevelFlags  log_level,
1248                                          const gchar    *format,
1249                                          ...) G_GNUC_PRINTF (3, 4);
1250 void            g_logv                  (const gchar    *log_domain,
1251                                          GLogLevelFlags  log_level,
1252                                          const gchar    *format,
1253                                          va_list         args);
1254 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1255                                          GLogLevelFlags  fatal_mask);
1256 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1257 #ifndef G_LOG_DOMAIN
1258 #define G_LOG_DOMAIN    (NULL)
1259 #endif  /* G_LOG_DOMAIN */
1260 #ifdef  __GNUC__
1261 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1262                                                G_LOG_LEVEL_ERROR, \
1263                                                format, ##args)
1264 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1265                                                G_LOG_LEVEL_MESSAGE, \
1266                                                format, ##args)
1267 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1268                                                G_LOG_LEVEL_WARNING, \
1269                                                format, ##args)
1270 #else   /* !__GNUC__ */
1271 static inline void
1272 g_error (const gchar *format,
1273          ...)
1274 {
1275   va_list args;
1276   va_start (args, format);
1277   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1278   va_end (args);
1279 }
1280 static inline void
1281 g_message (const gchar *format,
1282            ...)
1283 {
1284   va_list args;
1285   va_start (args, format);
1286   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1287   va_end (args);
1288 }
1289 static inline void
1290 g_warning (const gchar *format,
1291            ...)
1292 {
1293   va_list args;
1294   va_start (args, format);
1295   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1296   va_end (args);
1297 }
1298 #endif  /* !__GNUC__ */
1299
1300 typedef void    (*GPrintFunc)           (const gchar    *string);
1301 void            g_print                 (const gchar    *format,
1302                                          ...) G_GNUC_PRINTF (1, 2);
1303 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1304 void            g_printerr              (const gchar    *format,
1305                                          ...) G_GNUC_PRINTF (1, 2);
1306 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1307
1308 /* deprecated compatibility functions, use g_log_set_handler() instead */
1309 typedef void            (*GErrorFunc)           (const gchar *str);
1310 typedef void            (*GWarningFunc)         (const gchar *str);
1311 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1312 GWarningFunc g_set_warning_handler (GWarningFunc func);
1313 GPrintFunc   g_set_message_handler (GPrintFunc func);
1314
1315
1316 /* Memory allocation and debugging
1317  */
1318 #ifdef USE_DMALLOC
1319
1320 #define g_malloc(size)       ((gpointer) MALLOC (size))
1321 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1322 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1323 #define g_free(mem)          FREE (mem)
1324
1325 #else /* !USE_DMALLOC */
1326
1327 gpointer g_malloc      (gulong    size);
1328 gpointer g_malloc0     (gulong    size);
1329 gpointer g_realloc     (gpointer  mem,
1330                         gulong    size);
1331 void     g_free        (gpointer  mem);
1332
1333 #endif /* !USE_DMALLOC */
1334
1335 void     g_mem_profile (void);
1336 void     g_mem_check   (gpointer  mem);
1337
1338 /* Generic allocators
1339  */
1340 GAllocator* g_allocator_new   (const gchar  *name,
1341                                guint         n_preallocs);
1342 void        g_allocator_free  (GAllocator   *allocator);
1343
1344 #define G_ALLOCATOR_LIST        (1)
1345 #define G_ALLOCATOR_SLIST       (2)
1346 #define G_ALLOCATOR_NODE        (3)
1347
1348
1349 /* "g_mem_chunk_new" creates a new memory chunk.
1350  * Memory chunks are used to allocate pieces of memory which are
1351  *  always the same size. Lists are a good example of such a data type.
1352  * The memory chunk allocates and frees blocks of memory as needed.
1353  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1354  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1355  *  fault...somewhere).
1356  *
1357  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1358  *  want to know what's going on inside do you?)
1359  */
1360
1361 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1362  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1363  *  atom. (They are also useful for lists which use MemChunk to allocate
1364  *  memory but are also part of the MemChunk implementation).
1365  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1366  */
1367
1368 #define G_ALLOC_ONLY      1
1369 #define G_ALLOC_AND_FREE  2
1370
1371 GMemChunk* g_mem_chunk_new     (gchar     *name,
1372                                 gint       atom_size,
1373                                 gulong     area_size,
1374                                 gint       type);
1375 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1376 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1377 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1378 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1379                                 gpointer   mem);
1380 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1381 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1382 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1383 void       g_mem_chunk_info    (void);
1384
1385 /* Ah yes...we have a "g_blow_chunks" function.
1386  * "g_blow_chunks" simply compresses all the chunks. This operation
1387  *  consists of freeing every memory area that should be freed (but
1388  *  which we haven't gotten around to doing yet). And, no,
1389  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1390  *  much better name than "g_mem_chunk_clean_all" or something
1391  *  similar.
1392  */
1393 void g_blow_chunks (void);
1394
1395
1396 /* Timer
1397  */
1398 GTimer* g_timer_new     (void);
1399 void    g_timer_destroy (GTimer  *timer);
1400 void    g_timer_start   (GTimer  *timer);
1401 void    g_timer_stop    (GTimer  *timer);
1402 void    g_timer_reset   (GTimer  *timer);
1403 gdouble g_timer_elapsed (GTimer  *timer,
1404                          gulong  *microseconds);
1405
1406
1407 /* String utility functions that modify a string argument or
1408  * return a constant string that must not be freed.
1409  */
1410 #define  G_STR_DELIMITERS       "_-|> <."
1411 gchar*   g_strdelimit           (gchar       *string,
1412                                  const gchar *delimiters,
1413                                  gchar        new_delimiter);
1414 gdouble  g_strtod               (const gchar *nptr,
1415                                  gchar      **endptr);
1416 gchar*   g_strerror             (gint         errnum);
1417 gchar*   g_strsignal            (gint         signum);
1418 gint     g_strcasecmp           (const gchar *s1,
1419                                  const gchar *s2);
1420 gint     g_strncasecmp          (const gchar *s1,
1421                                  const gchar *s2,
1422                                  guint        n);
1423 void     g_strdown              (gchar       *string);
1424 void     g_strup                (gchar       *string);
1425 void     g_strreverse           (gchar       *string);
1426 /* removes leading spaces */
1427 gchar*   g_strchug              (gchar        *string);
1428 /* removes trailing spaces */
1429 gchar*  g_strchomp              (gchar        *string);
1430 /* removes leading & trailing spaces */
1431 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1432
1433 /* String utility functions that return a newly allocated string which
1434  * ought to be freed from the caller at some point.
1435  */
1436 gchar*   g_strdup               (const gchar *str);
1437 gchar*   g_strdup_printf        (const gchar *format,
1438                                  ...) G_GNUC_PRINTF (1, 2);
1439 gchar*   g_strdup_vprintf       (const gchar *format,
1440                                  va_list      args);
1441 gchar*   g_strndup              (const gchar *str,
1442                                  guint        n);
1443 gchar*   g_strnfill             (guint        length,
1444                                  gchar        fill_char);
1445 gchar*   g_strconcat            (const gchar *string1,
1446                                  ...); /* NULL terminated */
1447 gchar*   g_strjoin              (const gchar  *separator,
1448                                  ...); /* NULL terminated */
1449 gchar*   g_strescape            (gchar        *string);
1450 gpointer g_memdup               (gconstpointer mem,
1451                                  guint         byte_size);
1452
1453 /* NULL terminated string arrays.
1454  * g_strsplit() splits up string into max_tokens tokens at delim and
1455  * returns a newly allocated string array.
1456  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1457  * optional separator, the returned string is newly allocated.
1458  * g_strfreev() frees the array itself and all of its strings.
1459  */
1460 gchar**  g_strsplit             (const gchar  *string,
1461                                  const gchar  *delimiter,
1462                                  gint          max_tokens);
1463 gchar*   g_strjoinv             (const gchar  *separator,
1464                                  gchar       **str_array);
1465 void     g_strfreev             (gchar       **str_array);
1466
1467
1468
1469 /* calculate a string size, guarranteed to fit format + args.
1470  */
1471 guint   g_printf_string_upper_bound (const gchar* format,
1472                                      va_list      args);
1473
1474
1475 /* Retrive static string info
1476  */
1477 gchar*  g_get_user_name         (void);
1478 gchar*  g_get_real_name         (void);
1479 gchar*  g_get_home_dir          (void);
1480 gchar*  g_get_tmp_dir           (void);
1481 gchar*  g_get_prgname           (void);
1482 void    g_set_prgname           (const gchar *prgname);
1483
1484
1485 /* Miscellaneous utility functions
1486  */
1487 guint   g_parse_debug_string    (const gchar *string,
1488                                  GDebugKey   *keys,
1489                                  guint        nkeys);
1490 gint    g_snprintf              (gchar       *string,
1491                                  gulong       n,
1492                                  gchar const *format,
1493                                  ...) G_GNUC_PRINTF (3, 4);
1494 gint    g_vsnprintf             (gchar       *string,
1495                                  gulong       n,
1496                                  gchar const *format,
1497                                  va_list      args);
1498 gchar*  g_basename              (const gchar *file_name);
1499 /* Check if a file name is an absolute path */
1500 gboolean g_path_is_absolute     (const gchar *file_name);
1501 /* In case of absolute paths, skip the root part */
1502 gchar*  g_path_skip_root        (gchar       *file_name);
1503
1504 /* strings are newly allocated with g_malloc() */
1505 gchar*  g_dirname               (const gchar *file_name);
1506 gchar*  g_get_current_dir       (void);
1507 gchar*  g_getenv                (const gchar *variable);
1508
1509
1510 /* we use a GLib function as a replacement for ATEXIT, so
1511  * the programmer is not required to check the return value
1512  * (if there is any in the implementation) and doesn't encounter
1513  * missing include files.
1514  */
1515 void    g_atexit                (GVoidFunc    func);
1516
1517
1518 /* Bit tests
1519  */
1520 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1521                                        gint    nth_bit);
1522 #ifdef  G_CAN_INLINE
1523 G_INLINE_FUNC gint
1524 g_bit_nth_lsf (guint32 mask,
1525                gint    nth_bit)
1526 {
1527   do
1528     {
1529       nth_bit++;
1530       if (mask & (1 << (guint) nth_bit))
1531         return nth_bit;
1532     }
1533   while (nth_bit < 32);
1534   return -1;
1535 }
1536 #endif  /* G_CAN_INLINE */
1537
1538 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1539                                        gint    nth_bit);
1540 #ifdef G_CAN_INLINE
1541 G_INLINE_FUNC gint
1542 g_bit_nth_msf (guint32 mask,
1543                gint    nth_bit)
1544 {
1545   if (nth_bit < 0)
1546     nth_bit = 32;
1547   do
1548     {
1549       nth_bit--;
1550       if (mask & (1 << (guint) nth_bit))
1551         return nth_bit;
1552     }
1553   while (nth_bit > 0);
1554   return -1;
1555 }
1556 #endif  /* G_CAN_INLINE */
1557
1558 G_INLINE_FUNC guint     g_bit_storage (guint number);
1559 #ifdef G_CAN_INLINE
1560 G_INLINE_FUNC guint
1561 g_bit_storage (guint number)
1562 {
1563   register guint n_bits = 0;
1564   
1565   do
1566     {
1567       n_bits++;
1568       number >>= 1;
1569     }
1570   while (number);
1571   return n_bits;
1572 }
1573 #endif  /* G_CAN_INLINE */
1574
1575 /* String Chunks
1576  */
1577 GStringChunk* g_string_chunk_new           (gint size);
1578 void          g_string_chunk_free          (GStringChunk *chunk);
1579 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1580                                             const gchar  *string);
1581 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1582                                             const gchar  *string);
1583
1584
1585 /* Strings
1586  */
1587 GString* g_string_new       (const gchar *init);
1588 GString* g_string_sized_new (guint        dfl_size);
1589 void     g_string_free      (GString     *string,
1590                              gint         free_segment);
1591 GString* g_string_assign    (GString     *lval,
1592                              const gchar *rval);
1593 GString* g_string_truncate  (GString     *string,
1594                              gint         len);
1595 GString* g_string_append    (GString     *string,
1596                              const gchar *val);
1597 GString* g_string_append_c  (GString     *string,
1598                              gchar        c);
1599 GString* g_string_prepend   (GString     *string,
1600                              const gchar *val);
1601 GString* g_string_prepend_c (GString     *string,
1602                              gchar        c);
1603 GString* g_string_insert    (GString     *string,
1604                              gint         pos,
1605                              const gchar *val);
1606 GString* g_string_insert_c  (GString     *string,
1607                              gint         pos,
1608                              gchar        c);
1609 GString* g_string_erase     (GString     *string,
1610                              gint         pos,
1611                              gint         len);
1612 GString* g_string_down      (GString     *string);
1613 GString* g_string_up        (GString     *string);
1614 void     g_string_sprintf   (GString     *string,
1615                              const gchar *format,
1616                              ...) G_GNUC_PRINTF (2, 3);
1617 void     g_string_sprintfa  (GString     *string,
1618                              const gchar *format,
1619                              ...) G_GNUC_PRINTF (2, 3);
1620
1621
1622 /* Resizable arrays, remove fills any cleared spot and shortens the
1623  * array, while preserving the order. remove_fast will distort the
1624  * order by moving the last element to the position of the removed 
1625  */
1626
1627 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1628 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1629 #define g_array_insert_val(a,i,v) g_array_prepend_vals(a,i,&v,1)
1630 #define g_array_index(a,t,i) (((t*)a->data)[i])
1631
1632 GArray* g_array_new               (gboolean         zero_terminated,
1633                                    gboolean         clear,
1634                                    guint            element_size);
1635 void    g_array_free              (GArray          *array,
1636                                    gboolean         free_segment);
1637 GArray* g_array_append_vals       (GArray          *array,
1638                                    gconstpointer    data,
1639                                    guint            len);
1640 GArray* g_array_prepend_vals      (GArray          *array,
1641                                    gconstpointer    data,
1642                                    guint            len);
1643 GArray* g_array_insert_vals       (GArray          *array,
1644                                    guint            index,
1645                                    gconstpointer    data,
1646                                    guint            len);
1647 GArray* g_array_set_size          (GArray          *array,
1648                                    guint            length);
1649 GArray* g_array_remove_index      (GArray          *array,
1650                                    guint            index);
1651 GArray* g_array_remove_index_fast (GArray          *array,
1652                                    guint            index);
1653
1654 /* Resizable pointer array.  This interface is much less complicated
1655  * than the above.  Add appends appends a pointer.  Remove fills any
1656  * cleared spot and shortens the array. remove_fast will again distort
1657  * order.  
1658  */
1659 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1660 GPtrArray*  g_ptr_array_new                (void);
1661 void        g_ptr_array_free               (GPtrArray   *array,
1662                                             gboolean     free_seg);
1663 void        g_ptr_array_set_size           (GPtrArray   *array,
1664                                             gint         length);
1665 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1666                                             guint        index);
1667 gpointer    g_ptr_array_remove_index_fast  (GPtrArray   *array,
1668                                             guint        index);
1669 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1670                                             gpointer     data);
1671 gboolean    g_ptr_array_remove_fast        (GPtrArray   *array,
1672                                             gpointer     data);
1673 void        g_ptr_array_add                (GPtrArray   *array,
1674                                             gpointer     data);
1675
1676 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1677  * but type-safe.
1678  */
1679
1680 GByteArray* g_byte_array_new               (void);
1681 void        g_byte_array_free              (GByteArray   *array,
1682                                             gboolean      free_segment);
1683 GByteArray* g_byte_array_append            (GByteArray   *array,
1684                                             const guint8 *data,
1685                                             guint         len);
1686 GByteArray* g_byte_array_prepend           (GByteArray   *array,
1687                                             const guint8 *data,
1688                                             guint         len);
1689 GByteArray* g_byte_array_set_size          (GByteArray   *array,
1690                                             guint         length);
1691 GByteArray* g_byte_array_remove_index      (GByteArray   *array,
1692                                             guint         index);
1693 GByteArray* g_byte_array_remove_index_fast (GByteArray   *array,
1694                                             guint         index);
1695
1696
1697 /* Hash Functions
1698  */
1699 gint  g_str_equal (gconstpointer   v,
1700                    gconstpointer   v2);
1701 guint g_str_hash  (gconstpointer   v);
1702
1703 gint  g_int_equal (gconstpointer   v,
1704                    gconstpointer   v2);
1705 guint g_int_hash  (gconstpointer   v);
1706
1707 /* This "hash" function will just return the key's adress as an
1708  * unsigned integer. Useful for hashing on plain adresses or
1709  * simple integer values.
1710  */
1711 guint g_direct_hash  (gconstpointer v);
1712 gint  g_direct_equal (gconstpointer v,
1713                       gconstpointer v2);
1714
1715
1716 /* Quarks (string<->id association)
1717  */
1718 GQuark    g_quark_try_string            (const gchar    *string);
1719 GQuark    g_quark_from_static_string    (const gchar    *string);
1720 GQuark    g_quark_from_string           (const gchar    *string);
1721 gchar*    g_quark_to_string             (GQuark          quark);
1722
1723
1724 /* Keyed Data List
1725  */
1726 void      g_datalist_init                (GData          **datalist);
1727 void      g_datalist_clear               (GData          **datalist);
1728 gpointer  g_datalist_id_get_data         (GData          **datalist,
1729                                           GQuark           key_id);
1730 void      g_datalist_id_set_data_full    (GData          **datalist,
1731                                           GQuark           key_id,
1732                                           gpointer         data,
1733                                           GDestroyNotify   destroy_func);
1734 void      g_datalist_id_remove_no_notify (GData          **datalist,
1735                                           GQuark           key_id);
1736 void      g_datalist_foreach             (GData          **datalist,
1737                                           GDataForeachFunc func,
1738                                           gpointer         user_data);
1739 #define   g_datalist_id_set_data(dl, q, d)      \
1740      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1741 #define   g_datalist_id_remove_data(dl, q)      \
1742      g_datalist_id_set_data ((dl), (q), NULL)
1743 #define   g_datalist_get_data(dl, k)            \
1744      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1745 #define   g_datalist_set_data_full(dl, k, d, f) \
1746      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1747 #define   g_datalist_remove_no_notify(dl, k)    \
1748      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1749 #define   g_datalist_set_data(dl, k, d)         \
1750      g_datalist_set_data_full ((dl), (k), (d), NULL)
1751 #define   g_datalist_remove_data(dl, k)         \
1752      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1753
1754
1755 /* Location Associated Keyed Data
1756  */
1757 void      g_dataset_destroy             (gconstpointer    dataset_location);
1758 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
1759                                          GQuark           key_id);
1760 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
1761                                          GQuark           key_id,
1762                                          gpointer         data,
1763                                          GDestroyNotify   destroy_func);
1764 void      g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
1765                                          GQuark           key_id);
1766 void      g_dataset_foreach             (gconstpointer    dataset_location,
1767                                          GDataForeachFunc func,
1768                                          gpointer         user_data);
1769 #define   g_dataset_id_set_data(l, k, d)        \
1770      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1771 #define   g_dataset_id_remove_data(l, k)        \
1772      g_dataset_id_set_data ((l), (k), NULL)
1773 #define   g_dataset_get_data(l, k)              \
1774      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1775 #define   g_dataset_set_data_full(l, k, d, f)   \
1776      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1777 #define   g_dataset_remove_no_notify(l, k)      \
1778      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1779 #define   g_dataset_set_data(l, k, d)           \
1780      g_dataset_set_data_full ((l), (k), (d), NULL)
1781 #define   g_dataset_remove_data(l, k)           \
1782      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1783
1784
1785 /* GScanner: Flexible lexical scanner for general purpose.
1786  */
1787
1788 /* Character sets */
1789 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1790 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1791 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1792                         "\307\310\311\312\313\314\315\316\317\320"\
1793                         "\321\322\323\324\325\326"\
1794                         "\330\331\332\333\334\335\336"
1795 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1796                         "\347\350\351\352\353\354\355\356\357\360"\
1797                         "\361\362\363\364\365\366"\
1798                         "\370\371\372\373\374\375\376\377"
1799
1800 /* Error types */
1801 typedef enum
1802 {
1803   G_ERR_UNKNOWN,
1804   G_ERR_UNEXP_EOF,
1805   G_ERR_UNEXP_EOF_IN_STRING,
1806   G_ERR_UNEXP_EOF_IN_COMMENT,
1807   G_ERR_NON_DIGIT_IN_CONST,
1808   G_ERR_DIGIT_RADIX,
1809   G_ERR_FLOAT_RADIX,
1810   G_ERR_FLOAT_MALFORMED
1811 } GErrorType;
1812
1813 /* Token types */
1814 typedef enum
1815 {
1816   G_TOKEN_EOF                   =   0,
1817   
1818   G_TOKEN_LEFT_PAREN            = '(',
1819   G_TOKEN_RIGHT_PAREN           = ')',
1820   G_TOKEN_LEFT_CURLY            = '{',
1821   G_TOKEN_RIGHT_CURLY           = '}',
1822   G_TOKEN_LEFT_BRACE            = '[',
1823   G_TOKEN_RIGHT_BRACE           = ']',
1824   G_TOKEN_EQUAL_SIGN            = '=',
1825   G_TOKEN_COMMA                 = ',',
1826   
1827   G_TOKEN_NONE                  = 256,
1828   
1829   G_TOKEN_ERROR,
1830   
1831   G_TOKEN_CHAR,
1832   G_TOKEN_BINARY,
1833   G_TOKEN_OCTAL,
1834   G_TOKEN_INT,
1835   G_TOKEN_HEX,
1836   G_TOKEN_FLOAT,
1837   G_TOKEN_STRING,
1838   
1839   G_TOKEN_SYMBOL,
1840   G_TOKEN_IDENTIFIER,
1841   G_TOKEN_IDENTIFIER_NULL,
1842   
1843   G_TOKEN_COMMENT_SINGLE,
1844   G_TOKEN_COMMENT_MULTI,
1845   G_TOKEN_LAST
1846 } GTokenType;
1847
1848 union   _GTokenValue
1849 {
1850   gpointer      v_symbol;
1851   gchar         *v_identifier;
1852   gulong        v_binary;
1853   gulong        v_octal;
1854   gulong        v_int;
1855   gdouble       v_float;
1856   gulong        v_hex;
1857   gchar         *v_string;
1858   gchar         *v_comment;
1859   guchar        v_char;
1860   guint         v_error;
1861 };
1862
1863 struct  _GScannerConfig
1864 {
1865   /* Character sets
1866    */
1867   gchar         *cset_skip_characters;          /* default: " \t\n" */
1868   gchar         *cset_identifier_first;
1869   gchar         *cset_identifier_nth;
1870   gchar         *cpair_comment_single;          /* default: "#\n" */
1871   
1872   /* Should symbol lookup work case sensitive?
1873    */
1874   guint         case_sensitive : 1;
1875   
1876   /* Boolean values to be adjusted "on the fly"
1877    * to configure scanning behaviour.
1878    */
1879   guint         skip_comment_multi : 1;         /* C like comment */
1880   guint         skip_comment_single : 1;        /* single line comment */
1881   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1882   guint         scan_identifier : 1;
1883   guint         scan_identifier_1char : 1;
1884   guint         scan_identifier_NULL : 1;
1885   guint         scan_symbols : 1;
1886   guint         scan_binary : 1;
1887   guint         scan_octal : 1;
1888   guint         scan_float : 1;
1889   guint         scan_hex : 1;                   /* `0x0ff0' */
1890   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1891   guint         scan_string_sq : 1;             /* string: 'anything' */
1892   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1893   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1894   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1895   guint         identifier_2_string : 1;
1896   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1897   guint         symbol_2_token : 1;
1898   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
1899 };
1900
1901 struct  _GScanner
1902 {
1903   /* unused fields */
1904   gpointer              user_data;
1905   guint                 max_parse_errors;
1906   
1907   /* g_scanner_error() increments this field */
1908   guint                 parse_errors;
1909   
1910   /* name of input stream, featured by the default message handler */
1911   const gchar           *input_name;
1912   
1913   /* data pointer for derived structures */
1914   gpointer              derived_data;
1915   
1916   /* link into the scanner configuration */
1917   GScannerConfig        *config;
1918   
1919   /* fields filled in after g_scanner_get_next_token() */
1920   GTokenType            token;
1921   GTokenValue           value;
1922   guint                 line;
1923   guint                 position;
1924   
1925   /* fields filled in after g_scanner_peek_next_token() */
1926   GTokenType            next_token;
1927   GTokenValue           next_value;
1928   guint                 next_line;
1929   guint                 next_position;
1930   
1931   /* to be considered private */
1932   GHashTable            *symbol_table;
1933   gint                  input_fd;
1934   const gchar           *text;
1935   const gchar           *text_end;
1936   gchar                 *buffer;
1937   guint                 scope_id;
1938   
1939   /* handler function for _warn and _error */
1940   GScannerMsgFunc       msg_handler;
1941 };
1942
1943 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
1944 void            g_scanner_destroy               (GScanner       *scanner);
1945 void            g_scanner_input_file            (GScanner       *scanner,
1946                                                  gint           input_fd);
1947 void            g_scanner_sync_file_offset      (GScanner       *scanner);
1948 void            g_scanner_input_text            (GScanner       *scanner,
1949                                                  const  gchar   *text,
1950                                                  guint          text_len);
1951 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
1952 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
1953 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
1954 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
1955 guint           g_scanner_cur_line              (GScanner       *scanner);
1956 guint           g_scanner_cur_position          (GScanner       *scanner);
1957 gboolean        g_scanner_eof                   (GScanner       *scanner);
1958 guint           g_scanner_set_scope             (GScanner       *scanner,
1959                                                  guint           scope_id);
1960 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
1961                                                  guint           scope_id,
1962                                                  const gchar    *symbol,
1963                                                  gpointer       value);
1964 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
1965                                                  guint           scope_id,
1966                                                  const gchar    *symbol);
1967 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
1968                                                  guint           scope_id,
1969                                                  const gchar    *symbol);
1970 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
1971                                                  guint           scope_id,
1972                                                  GHFunc          func,
1973                                                  gpointer        func_data);
1974 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
1975                                                  const gchar    *symbol);
1976 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
1977 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
1978 void            g_scanner_unexp_token           (GScanner       *scanner,
1979                                                  GTokenType     expected_token,
1980                                                  const gchar    *identifier_spec,
1981                                                  const gchar    *symbol_spec,
1982                                                  const gchar    *symbol_name,
1983                                                  const gchar    *message,
1984                                                  gint            is_error);
1985 void            g_scanner_error                 (GScanner       *scanner,
1986                                                  const gchar    *format,
1987                                                  ...) G_GNUC_PRINTF (2,3);
1988 void            g_scanner_warn                  (GScanner       *scanner,
1989                                                  const gchar    *format,
1990                                                  ...) G_GNUC_PRINTF (2,3);
1991 gint            g_scanner_stat_mode             (const gchar    *filename);
1992 /* keep downward source compatibility */
1993 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
1994   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
1995 } G_STMT_END
1996 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
1997   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
1998 } G_STMT_END
1999 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2000   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2001 } G_STMT_END
2002
2003
2004 /* Completion */
2005
2006 struct _GCompletion
2007 {
2008   GList* items;
2009   GCompletionFunc func;
2010   
2011   gchar* prefix;
2012   GList* cache;
2013 };
2014
2015 GCompletion* g_completion_new          (GCompletionFunc func);
2016 void         g_completion_add_items    (GCompletion*    cmp,
2017                                         GList*          items);
2018 void         g_completion_remove_items (GCompletion*    cmp,
2019                                         GList*          items);
2020 void         g_completion_clear_items  (GCompletion*    cmp);
2021 GList*       g_completion_complete     (GCompletion*    cmp,
2022                                         gchar*          prefix,
2023                                         gchar**         new_prefix);
2024 void         g_completion_free         (GCompletion*    cmp);
2025
2026
2027 /* GRelation: Indexed Relations.  Imagine a really simple table in a
2028  * database.  Relations are not ordered.  This data type is meant for
2029  * maintaining a N-way mapping.
2030  *
2031  * g_relation_new() creates a relation with FIELDS fields
2032  *
2033  * g_relation_destroy() frees all resources
2034  * g_tuples_destroy() frees the result of g_relation_select()
2035  *
2036  * g_relation_index() indexes relation FIELD with the provided
2037  *   equality and hash functions.  this must be done before any
2038  *   calls to insert are made.
2039  *
2040  * g_relation_insert() inserts a new tuple.  you are expected to
2041  *   provide the right number of fields.
2042  *
2043  * g_relation_delete() deletes all relations with KEY in FIELD
2044  * g_relation_select() returns ...
2045  * g_relation_count() counts ...
2046  */
2047
2048 GRelation* g_relation_new     (gint         fields);
2049 void       g_relation_destroy (GRelation   *relation);
2050 void       g_relation_index   (GRelation   *relation,
2051                                gint         field,
2052                                GHashFunc    hash_func,
2053                                GCompareFunc key_compare_func);
2054 void       g_relation_insert  (GRelation   *relation,
2055                                ...);
2056 gint       g_relation_delete  (GRelation   *relation,
2057                                gconstpointer  key,
2058                                gint         field);
2059 GTuples*   g_relation_select  (GRelation   *relation,
2060                                gconstpointer  key,
2061                                gint         field);
2062 gint       g_relation_count   (GRelation   *relation,
2063                                gconstpointer  key,
2064                                gint         field);
2065 gboolean   g_relation_exists  (GRelation   *relation,
2066                                ...);
2067 void       g_relation_print   (GRelation   *relation);
2068
2069 void       g_tuples_destroy   (GTuples     *tuples);
2070 gpointer   g_tuples_index     (GTuples     *tuples,
2071                                gint         index,
2072                                gint         field);
2073
2074
2075 /* Prime numbers.
2076  */
2077
2078 /* This function returns prime numbers spaced by approximately 1.5-2.0
2079  * and is for use in resizing data structures which prefer
2080  * prime-valued sizes.  The closest spaced prime function returns the
2081  * next largest prime, or the highest it knows about which is about
2082  * MAXINT/4.
2083  */
2084 guint      g_spaced_primes_closest (guint num);
2085
2086
2087 /* IO Channels.
2088  * These are used for plug-in communication in the GIMP, for instance.
2089  * On Unix, it's simply an encapsulated file descriptor (a pipe).
2090  * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2091  * of the writable end) a thread id to post a message to when you have written
2092  * stuff.
2093  */
2094 struct _GIOChannel
2095 {
2096   gint fd;                      /* file handle (pseudo such in Win32) */
2097 #ifdef NATIVE_WIN32
2098   guint peer;                   /* thread to post message to */
2099   guint peer_fd;                /* read handle (in the other process) */
2100   guint offset;                 /* counter of accumulated bytes, to
2101                                  * be included in the message posted
2102                                  * so we keep in sync.
2103                                  */
2104   guint need_wakeups;           /* in output channels whether the reader
2105                                  * needs wakeups
2106                                  */
2107 #endif
2108 };
2109
2110 GIOChannel *g_iochannel_new            (gint        fd);
2111 void        g_iochannel_free           (GIOChannel *channel);
2112 void        g_iochannel_close_and_free (GIOChannel *channel);
2113 void        g_iochannel_wakeup_peer    (GIOChannel *channel);
2114 #ifndef NATIVE_WIN32
2115 #  define   g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2116 #endif
2117
2118
2119 /* Windows emulation stubs for common unix functions
2120  */
2121 #ifdef NATIVE_WIN32
2122
2123 #define MAXPATHLEN 1024
2124
2125 #ifdef _MSC_VER
2126 typedef int pid_t;
2127
2128 /* These POSIXish functions are available in the Microsoft C library
2129  * prefixed with underscore (which of course technically speaking is
2130  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2131  * prevents Microsoft from practically requiring you to include
2132  * <windows.h> every now and then...).
2133  *
2134  * You still need to include the appropriate headers to get the
2135  * prototypes, <io.h> or <direct.h>.
2136  *
2137  * For some functions, we provide emulators in glib, which are prefixed
2138  * with gwin_.
2139  */
2140 #define getcwd                  _getcwd
2141 #define getpid                  _getpid
2142 #define access                  _access
2143 #define open                    _open
2144 #define read                    _read
2145 #define write                   _write
2146 #define lseek                   _lseek
2147 #define close                   _close
2148 #define pipe(phandles)          _pipe (phandles, 4096, _O_BINARY)
2149 #define popen                   _popen
2150 #define pclose                  _pclose
2151 #define fdopen                  _fdopen
2152 #define ftruncate(fd, size)     gwin_ftruncate (fd, size)
2153 #define opendir                 gwin_opendir
2154 #define readdir                 gwin_readdir
2155 #define rewinddir               gwin_rewinddir
2156 #define closedir                gwin_closedir
2157
2158 #define NAME_MAX 255
2159
2160 struct DIR
2161 {
2162   gchar    *dir_name;
2163
2164   gboolean  just_opened;
2165   guint     find_file_handle;
2166   gpointer  find_file_data;
2167 };
2168 typedef struct DIR DIR;
2169 struct dirent
2170 {
2171   gchar  d_name[NAME_MAX + 1];
2172 };
2173
2174 /* emulation functions */
2175 extern int      gwin_ftruncate  (gint            f,
2176                                  guint           size);
2177 DIR*            gwin_opendir    (const gchar    *dirname);
2178 struct dirent*  gwin_readdir    (DIR            *dir);
2179 void            gwin_rewinddir  (DIR            *dir);
2180 gint            gwin_closedir   (DIR            *dir);
2181
2182 #endif /* _MSC_VER */
2183
2184 #endif /* NATIVE_WIN32 */
2185
2186
2187 #ifdef __cplusplus
2188 }
2189 #endif /* __cplusplus */
2190
2191
2192 #endif /* __G_LIB_H__ */