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