added GFreeFunc and g_hash_table_set_key_freefunc() prototype. added
[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 _MSC_VER
93 /* Make MSVC more pedantic, this is a recommended pragma list
94  * from _Win32_Programming_ by Rector and Newcomer.
95  */
96 #pragma warning(error:4002)
97 #pragma warning(error:4003)
98 #pragma warning(1:4010)
99 #pragma warning(error:4013)
100 #pragma warning(1:4016)
101 #pragma warning(error:4020)
102 #pragma warning(error:4021)
103 #pragma warning(error:4027)
104 #pragma warning(error:4029)
105 #pragma warning(error:4033)
106 #pragma warning(error:4035)
107 #pragma warning(error:4045)
108 #pragma warning(error:4047)
109 #pragma warning(error:4049)
110 #pragma warning(error:4053)
111 #pragma warning(error:4071)
112 #pragma warning(disable:4101)
113 #pragma warning(error:4150)
114
115 #pragma warning(disable:4244)   /* No possible loss of data warnings, please */
116 #endif /* _MSC_VER */
117
118
119 #ifdef __cplusplus
120 extern "C" {
121 #endif /* __cplusplus */
122
123
124 /* Provide definitions for some commonly used macros.
125  *  Some of them are only provided if they haven't already
126  *  been defined. It is assumed that if they are already
127  *  defined then the current definition is correct.
128  */
129 #ifndef NULL
130 #define NULL    ((void*) 0)
131 #endif
132
133 #ifndef FALSE
134 #define FALSE   (0)
135 #endif
136
137 #ifndef TRUE
138 #define TRUE    (!FALSE)
139 #endif
140
141 #undef  MAX
142 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
143
144 #undef  MIN
145 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
146
147 #undef  ABS
148 #define ABS(a)     (((a) < 0) ? -(a) : (a))
149
150 #undef  CLAMP
151 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
152
153
154 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
155  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
156  */
157 #if !defined (G_VA_COPY)
158 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
159 #  define G_VA_COPY(ap1, ap2)     (*(ap1) = *(ap2))
160 #  elif defined (G_VA_COPY_AS_ARRAY)
161 #  define G_VA_COPY(ap1, ap2)     g_memmove ((ap1), (ap2), sizeof (va_list))
162 #  else /* va_list is a pointer */
163 #  define G_VA_COPY(ap1, ap2)     ((ap1) = (ap2))
164 #  endif /* va_list is a pointer */
165 #endif /* !G_VA_COPY */
166
167
168 /* Provide convenience macros for handling structure
169  * fields through their offsets.
170  */
171 #define G_STRUCT_OFFSET(struct_type, member)    \
172     ((gulong) ((gchar*) &((struct_type*) 0)->member))
173 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
174     ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
175 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
176     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
177
178
179 /* inlining hassle. for compilers that don't allow the `inline' keyword,
180  * mostly because of strict ANSI C compliance or dumbness, we try to fall
181  * back to either `__inline__' or `__inline'.
182  * we define G_CAN_INLINE, if the compiler seems to be actually
183  * *capable* to do function inlining, in which case inline function bodys
184  * do make sense. we also define G_INLINE_FUNC to properly export the
185  * function prototypes if no inlinig can be performed.
186  * we special case most of the stuff, so inline functions can have a normal
187  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
188  */
189 #ifndef G_INLINE_FUNC
190 #  define G_CAN_INLINE 1
191 #endif
192 #ifdef G_HAVE_INLINE
193 #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
194 #    undef inline
195 #    define inline __inline__
196 #  endif
197 #else /* !G_HAVE_INLINE */
198 #  undef inline
199 #  if defined (G_HAVE___INLINE__)
200 #    define inline __inline__
201 #  else /* !inline && !__inline__ */
202 #    if defined (G_HAVE___INLINE)
203 #      define inline __inline
204 #    else /* !inline && !__inline__ && !__inline */
205 #      define inline /* don't inline, then */
206 #      ifndef G_INLINE_FUNC
207 #        undef G_CAN_INLINE
208 #      endif
209 #    endif
210 #  endif
211 #endif
212 #ifndef G_INLINE_FUNC
213 #  ifdef __GNUC__
214 #    ifdef __OPTIMIZE__
215 #      define G_INLINE_FUNC extern inline
216 #    else
217 #      undef G_CAN_INLINE
218 #      define G_INLINE_FUNC extern
219 #    endif
220 #  else /* !__GNUC__ */
221 #    ifdef G_CAN_INLINE
222 #      define G_INLINE_FUNC static inline
223 #    else
224 #      define G_INLINE_FUNC extern
225 #    endif
226 #  endif /* !__GNUC__ */
227 #endif /* !G_INLINE_FUNC */
228
229
230 /* Provide simple macro statement wrappers (adapted from Perl):
231  *  G_STMT_START { statements; } G_STMT_END;
232  *  can be used as a single statement, as in
233  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
234  *
235  *  For gcc we will wrap the statements within `({' and `})' braces.
236  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
237  *  and otherwise within `do' and `while (0)'.
238  */
239 #if !(defined (G_STMT_START) && defined (G_STMT_END))
240 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
241 #    define G_STMT_START        (void)(
242 #    define G_STMT_END          )
243 #  else
244 #    if (defined (sun) || defined (__sun__))
245 #      define G_STMT_START      if (1)
246 #      define G_STMT_END        else (void)0
247 #    else
248 #      define G_STMT_START      do
249 #      define G_STMT_END        while (0)
250 #    endif
251 #  endif
252 #endif
253
254
255 /* Provide macros to feature the GCC function attribute.
256  */
257 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
258 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
259   __attribute__((format (printf, format_idx, arg_idx)))
260 #define G_GNUC_SCANF( format_idx, arg_idx )     \
261   __attribute__((format (scanf, format_idx, arg_idx)))
262 #define G_GNUC_FORMAT( arg_idx )                \
263   __attribute__((format_arg (arg_idx)))
264 #define G_GNUC_NORETURN                         \
265   __attribute__((noreturn))
266 #define G_GNUC_CONST                            \
267   __attribute__((const))
268 #define G_GNUC_UNUSED                           \
269   __attribute__((unused))
270 #else   /* !__GNUC__ */
271 #define G_GNUC_PRINTF( format_idx, arg_idx )
272 #define G_GNUC_SCANF( format_idx, arg_idx )
273 #define G_GNUC_FORMAT( arg_idx )
274 #define G_GNUC_NORETURN
275 #define G_GNUC_CONST
276 #define G_GNUC_UNUSED
277 #endif  /* !__GNUC__ */
278
279
280 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
281  * macros, so we can refer to them as strings unconditionally.
282  */
283 #ifdef  __GNUC__
284 #define G_GNUC_FUNCTION         (__FUNCTION__)
285 #define G_GNUC_PRETTY_FUNCTION  (__PRETTY_FUNCTION__)
286 #else   /* !__GNUC__ */
287 #define G_GNUC_FUNCTION         ("")
288 #define G_GNUC_PRETTY_FUNCTION  ("")
289 #endif  /* !__GNUC__ */
290
291 /* we try to provide a usefull equivalent for ATEXIT if it is
292  * not defined, but use is actually abandoned. people should
293  * use g_atexit() instead.
294  */
295 #ifndef ATEXIT
296 # define ATEXIT(proc)   g_ATEXIT(proc)
297 #else
298 # define G_NATIVE_ATEXIT
299 #endif /* ATEXIT */
300
301 /* Hacker macro to place breakpoints for elected machines.
302  * Actual use is strongly deprecated of course ;)
303  */
304 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
305 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
306 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
307 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
308 #else   /* !__i386__ && !__alpha__ */
309 #define G_BREAKPOINT()
310 #endif  /* __i386__ */
311
312
313 /* Provide macros for easily allocating memory. The macros
314  *  will cast the allocated memory to the specified type
315  *  in order to avoid compiler warnings. (Makes the code neater).
316  */
317
318 #ifdef __DMALLOC_H__
319 #  define g_new(type, count)            (ALLOC (type, count))
320 #  define g_new0(type, count)           (CALLOC (type, count))
321 #  define g_renew(type, mem, count)     (REALLOC (mem, type, count))
322 #else /* __DMALLOC_H__ */
323 #  define g_new(type, count)      \
324       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
325 #  define g_new0(type, count)     \
326       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
327 #  define g_renew(type, mem, count)       \
328       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
329 #endif /* __DMALLOC_H__ */
330
331 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
332   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
333                    sizeof (type), \
334                    sizeof (type) * (pre_alloc), \
335                    (alloc_type)) \
336 )
337 #define g_chunk_new(type, chunk)        ( \
338   (type *) g_mem_chunk_alloc (chunk) \
339 )
340 #define g_chunk_new0(type, chunk)       ( \
341   (type *) g_mem_chunk_alloc0 (chunk) \
342 )
343 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
344   g_mem_chunk_free ((mem_chunk), (mem)); \
345 } G_STMT_END
346
347
348 #define g_string(x) #x
349
350
351 /* Provide macros for error handling. The "assert" macros will
352  *  exit on failure. The "return" macros will exit the current
353  *  function. Two different definitions are given for the macros
354  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
355  *  __PRETTY_FUNCTION__ capability.
356  */
357
358 #ifdef G_DISABLE_ASSERT
359
360 #define g_assert(expr)
361 #define g_assert_not_reached()
362
363 #else /* !G_DISABLE_ASSERT */
364
365 #ifdef __GNUC__
366
367 #define g_assert(expr)                  G_STMT_START{           \
368      if (!(expr))                                               \
369        g_log (G_LOG_DOMAIN,                                     \
370               G_LOG_LEVEL_ERROR,                                \
371               "file %s: line %d (%s): assertion failed: (%s)",  \
372               __FILE__,                                         \
373               __LINE__,                                         \
374               __PRETTY_FUNCTION__,                              \
375               #expr);                   }G_STMT_END
376
377 #define g_assert_not_reached()          G_STMT_START{           \
378      g_log (G_LOG_DOMAIN,                                       \
379             G_LOG_LEVEL_ERROR,                                  \
380             "file %s: line %d (%s): should not be reached",     \
381             __FILE__,                                           \
382             __LINE__,                                           \
383             __PRETTY_FUNCTION__);       }G_STMT_END
384
385 #else /* !__GNUC__ */
386
387 #define g_assert(expr)                  G_STMT_START{           \
388      if (!(expr))                                               \
389        g_log (G_LOG_DOMAIN,                                     \
390               G_LOG_LEVEL_ERROR,                                \
391               "file %s: line %d: assertion failed: (%s)",       \
392               __FILE__,                                         \
393               __LINE__,                                         \
394               #expr);                   }G_STMT_END
395
396 #define g_assert_not_reached()          G_STMT_START{   \
397      g_log (G_LOG_DOMAIN,                               \
398             G_LOG_LEVEL_ERROR,                          \
399             "file %s: line %d: should not be reached",  \
400             __FILE__,                                   \
401             __LINE__);          }G_STMT_END
402
403 #endif /* __GNUC__ */
404
405 #endif /* !G_DISABLE_ASSERT */
406
407
408 #ifdef G_DISABLE_CHECKS
409
410 #define g_return_if_fail(expr)
411 #define g_return_val_if_fail(expr,val)
412
413 #else /* !G_DISABLE_CHECKS */
414
415 #ifdef __GNUC__
416
417 #define g_return_if_fail(expr)          G_STMT_START{                   \
418      if (!(expr))                                                       \
419        {                                                                \
420          g_log (G_LOG_DOMAIN,                                           \
421                 G_LOG_LEVEL_CRITICAL,                                   \
422                 "file %s: line %d (%s): assertion `%s' failed.",        \
423                 __FILE__,                                               \
424                 __LINE__,                                               \
425                 __PRETTY_FUNCTION__,                                    \
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 (%s): assertion `%s' failed.",        \
436                 __FILE__,                                               \
437                 __LINE__,                                               \
438                 __PRETTY_FUNCTION__,                                    \
439                 #expr);                                                 \
440          return val;                                                    \
441        };                               }G_STMT_END
442
443 #else /* !__GNUC__ */
444
445 #define g_return_if_fail(expr)          G_STMT_START{           \
446      if (!(expr))                                               \
447        {                                                        \
448          g_log (G_LOG_DOMAIN,                                   \
449                 G_LOG_LEVEL_CRITICAL,                           \
450                 "file %s: line %d: assertion `%s' failed.",     \
451                 __FILE__,                                       \
452                 __LINE__,                                       \
453                 #expr);                                         \
454          return;                                                \
455        };                               }G_STMT_END
456
457 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
458      if (!(expr))                                               \
459        {                                                        \
460          g_log (G_LOG_DOMAIN,                                   \
461                 G_LOG_LEVEL_CRITICAL,                           \
462                 "file %s: line %d: assertion `%s' failed.",     \
463                 __FILE__,                                       \
464                 __LINE__,                                       \
465                 #expr);                                         \
466          return val;                                            \
467        };                               }G_STMT_END
468
469 #endif /* !__GNUC__ */
470
471 #endif /* !G_DISABLE_CHECKS */
472
473
474 /* Provide type definitions for commonly used types.
475  *  These are useful because a "gint8" can be adjusted
476  *  to be 1 byte (8 bits) on all platforms. Similarly and
477  *  more importantly, "gint32" can be adjusted to be
478  *  4 bytes (32 bits) on all platforms.
479  */
480
481 typedef char   gchar;
482 typedef short  gshort;
483 typedef long   glong;
484 typedef int    gint;
485 typedef gint   gboolean;
486
487 typedef unsigned char   guchar;
488 typedef unsigned short  gushort;
489 typedef unsigned long   gulong;
490 typedef unsigned int    guint;
491
492 typedef float   gfloat;
493 typedef double  gdouble;
494
495 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
496  * Since gldouble isn't used anywhere, just disable it for now */
497
498 #if 0
499 #ifdef HAVE_LONG_DOUBLE
500 typedef long double gldouble;
501 #else /* HAVE_LONG_DOUBLE */
502 typedef double gldouble;
503 #endif /* HAVE_LONG_DOUBLE */
504 #endif /* 0 */
505
506 typedef void* gpointer;
507 typedef const void *gconstpointer;
508
509
510 typedef gint32  gssize;
511 typedef guint32 gsize;
512 typedef guint32 GQuark;
513 typedef gint32  GTime;
514
515
516 /* Portable endian checks and conversions
517  */
518
519 #define G_LITTLE_ENDIAN 1234
520 #define G_BIG_ENDIAN    4321
521 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
522
523 /* Basic bit swapping functions
524  */
525 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
526     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
527     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
528 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
529     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
530     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
531     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
532     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
533
534 /* Intel specific stuff for speed
535  */
536 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
537
538 #  define GUINT16_SWAP_LE_BE_X86(val) \
539      (__extension__                                     \
540       ({ register guint16 __v;                          \
541          if (__builtin_constant_p (val))                \
542            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
543          else                                           \
544            __asm__ __const__ ("rorw $8, %w0"            \
545                               : "=r" (__v)              \
546                               : "0" ((guint16) (val))); \
547         __v; }))
548
549 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
550
551 #  if !defined(__i486__) && !defined(__i586__) \
552       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
553 #     define GUINT32_SWAP_LE_BE_X86(val) \
554         (__extension__                                          \
555          ({ register guint32 __v;                               \
556             if (__builtin_constant_p (val))                     \
557               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
558           else                                                  \
559             __asm__ __const__ ("rorw $8, %w0\n\t"               \
560                                "rorl $16, %0\n\t"               \
561                                "rorw $8, %w0"                   \
562                                : "=r" (__v)                     \
563                                : "0" ((guint32) (val)));        \
564         __v; }))
565
566 #  else /* 486 and higher has bswap */
567 #     define GUINT32_SWAP_LE_BE_X86(val) \
568         (__extension__                                          \
569          ({ register guint32 __v;                               \
570             if (__builtin_constant_p (val))                     \
571               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
572           else                                                  \
573             __asm__ __const__ ("bswap %0"                       \
574                                : "=r" (__v)                     \
575                                : "0" ((guint32) (val)));        \
576         __v; }))
577 #  endif /* processor specific 32-bit stuff */
578
579 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
580
581 #else /* !__i386__ */
582 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
583 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
584 #endif /* __i386__ */
585
586 #ifdef G_HAVE_GINT64
587 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
588       (((guint64) (val) &                                               \
589         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
590       (((guint64) (val) &                                               \
591         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
592       (((guint64) (val) &                                               \
593         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
594       (((guint64) (val) &                                               \
595         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
596       (((guint64) (val) &                                               \
597         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
598       (((guint64) (val) &                                               \
599         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
600       (((guint64) (val) &                                               \
601         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
602       (((guint64) (val) &                                               \
603         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
604
605 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
606 #    define GUINT64_SWAP_LE_BE_X86(val) \
607         (__extension__                                          \
608          ({ union { guint64 __ll;                               \
609                     guint32 __l[2]; } __r;                      \
610             if (__builtin_constant_p (val))                     \
611               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
612             else                                                \
613               {                                                 \
614                 union { guint64 __ll;                           \
615                         guint32 __l[2]; } __w;                  \
616                 __w.__ll = ((guint64) val);                     \
617                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
618                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
619               }                                                 \
620           __r.__ll; }))
621
622 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
623
624 #  else /* !__i386__ */
625 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
626 #  endif
627 #endif
628
629 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
630 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
631 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
632     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
633     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
634 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
635     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
636     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
637
638 /* The TO_?E stuff is defined in glibconfig.h. The transformation is symmetric,
639    so the FROM just maps to the TO.
640  */
641 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
642 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
643 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
644 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
645 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
646 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
647 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
648 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
649
650 #ifdef G_HAVE_GINT64
651 #define GINT64_FROM_LE(val)     (GINT32_TO_LE (val))
652 #define GUINT64_FROM_LE(val)    (GUINT32_TO_LE (val))
653 #define GINT64_FROM_BE(val)     (GINT32_TO_BE (val))
654 #define GUINT64_FROM_BE(val)    (GUINT32_TO_BE (val))
655 #endif
656
657 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
658 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
659 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
660 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
661
662 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
663 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
664 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
665 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
666
667 /* Portable versions of host-network order stuff
668  */
669 #define g_ntohl(val) (GUINT32_FROM_BE (val))
670 #define g_ntohs(val) (GUINT16_FROM_BE (val))
671 #define g_htonl(val) (GUINT32_TO_BE (val))
672 #define g_htons(val) (GUINT16_TO_BE (val))
673
674
675 /* Glib version.
676  * we prefix variable declarations so they can
677  * properly get exported in windows dlls.
678  */
679 #ifdef NATIVE_WIN32
680 #  ifdef GLIB_COMPILATION
681 #    define GUTILS_C_VAR __declspec(dllexport)
682 #  else /* !GLIB_COMPILATION */
683 #    define GUTILS_C_VAR __declspec(dllimport)
684 #  endif /* !GLIB_COMPILATION */
685 #else /* !NATIVE_WIN32 */
686 #  define GUTILS_C_VAR extern
687 #endif /* !NATIVE_WIN32 */
688
689 GUTILS_C_VAR const guint glib_major_version;
690 GUTILS_C_VAR const guint glib_minor_version;
691 GUTILS_C_VAR const guint glib_micro_version;
692 GUTILS_C_VAR const guint glib_interface_age;
693 GUTILS_C_VAR const guint glib_binary_age;
694
695 /* Forward declarations of glib types.
696  */
697
698 typedef struct _GAllocator      GAllocator;
699 typedef struct _GArray          GArray;
700 typedef struct _GByteArray      GByteArray;
701 typedef struct _GCache          GCache;
702 typedef struct _GCompletion     GCompletion;
703 typedef struct _GData           GData;
704 typedef struct _GDebugKey       GDebugKey;
705 typedef struct _GHashTable      GHashTable;
706 typedef struct _GHook           GHook;
707 typedef struct _GHookList       GHookList;
708 typedef struct _GList           GList;
709 typedef struct _GMemChunk       GMemChunk;
710 typedef struct _GNode           GNode;
711 typedef struct _GPtrArray       GPtrArray;
712 typedef struct _GRelation       GRelation;
713 typedef struct _GScanner        GScanner;
714 typedef struct _GScannerConfig  GScannerConfig;
715 typedef struct _GSList          GSList;
716 typedef struct _GString         GString;
717 typedef struct _GStringChunk    GStringChunk;
718 typedef struct _GTimer          GTimer;
719 typedef struct _GTree           GTree;
720 typedef struct _GTuples         GTuples;
721 typedef union  _GTokenValue     GTokenValue;
722 typedef struct _GIOChannel      GIOChannel;
723
724
725 typedef enum
726 {
727   G_TRAVERSE_LEAFS      = 1 << 0,
728   G_TRAVERSE_NON_LEAFS  = 1 << 1,
729   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
730   G_TRAVERSE_MASK       = 0x03
731 } GTraverseFlags;
732
733 typedef enum
734 {
735   G_IN_ORDER,
736   G_PRE_ORDER,
737   G_POST_ORDER,
738   G_LEVEL_ORDER
739 } GTraverseType;
740
741 /* Log level shift offset for user defined
742  * log levels (0-7 are used by GLib).
743  */
744 #define G_LOG_LEVEL_USER_SHIFT  (8)
745
746 /* Glib log levels and flags.
747  */
748 typedef enum
749 {
750   /* log flags */
751   G_LOG_FLAG_RECURSION          = 1 << 0,
752   G_LOG_FLAG_FATAL              = 1 << 1,
753   
754   /* GLib log levels */
755   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
756   G_LOG_LEVEL_CRITICAL          = 1 << 3,
757   G_LOG_LEVEL_WARNING           = 1 << 4,
758   G_LOG_LEVEL_MESSAGE           = 1 << 5,
759   G_LOG_LEVEL_INFO              = 1 << 6,
760   G_LOG_LEVEL_DEBUG             = 1 << 7,
761   
762   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
763 } GLogLevelFlags;
764
765 /* GLib log levels that are considered fatal by default */
766 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
767
768
769 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
770 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
771 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
772 typedef gint            (*GCompareFunc)         (gconstpointer  a,
773                                                  gconstpointer  b);
774 typedef gchar*          (*GCompletionFunc)      (gpointer);
775 typedef void            (*GDestroyNotify)       (gpointer       data);
776 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
777                                                  gpointer       data,
778                                                  gpointer       user_data);
779 typedef void            (*GFunc)                (gpointer       data,
780                                                  gpointer       user_data);
781 typedef guint           (*GHashFunc)            (gconstpointer  key);
782 typedef void            (*GFreeFunc)            (gpointer       data);
783 typedef void            (*GHFunc)               (gpointer       key,
784                                                  gpointer       value,
785                                                  gpointer       user_data);
786 typedef gboolean        (*GHRFunc)              (gpointer       key,
787                                                  gpointer       value,
788                                                  gpointer       user_data);
789 typedef gint            (*GHookCompareFunc)     (GHook          *new_hook,
790                                                  GHook          *sibling);
791 typedef gboolean        (*GHookFindFunc)        (GHook          *hook,
792                                                  gpointer        data);
793 typedef void            (*GHookMarshaller)      (GHook          *hook,
794                                                  gpointer        data);
795 typedef void            (*GHookFunc)            (gpointer        data);
796 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
797 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
798                                                  GHook          *hook);
799 typedef void            (*GLogFunc)             (const gchar   *log_domain,
800                                                  GLogLevelFlags log_level,
801                                                  const gchar   *message,
802                                                  gpointer       user_data);
803 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
804                                                  gpointer       data);
805 typedef void            (*GNodeForeachFunc)     (GNode         *node,
806                                                  gpointer       data);
807 typedef gint            (*GSearchFunc)          (gpointer       key,
808                                                  gpointer       data);
809 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
810                                                  gchar         *message,
811                                                  gint           error);
812 typedef gint            (*GTraverseFunc)        (gpointer       key,
813                                                  gpointer       value,
814                                                  gpointer       data);
815 typedef void            (*GVoidFunc)            (void);
816
817
818 struct _GList
819 {
820   gpointer data;
821   GList *next;
822   GList *prev;
823 };
824
825 struct _GSList
826 {
827   gpointer data;
828   GSList *next;
829 };
830
831 struct _GString
832 {
833   gchar *str;
834   gint len;
835 };
836
837 struct _GArray
838 {
839   gchar *data;
840   guint len;
841 };
842
843 struct _GByteArray
844 {
845   guint8 *data;
846   guint   len;
847 };
848
849 struct _GPtrArray
850 {
851   gpointer *pdata;
852   guint     len;
853 };
854
855 struct _GTuples
856 {
857   guint len;
858 };
859
860 struct _GDebugKey
861 {
862   gchar *key;
863   guint  value;
864 };
865
866
867 /* Doubly linked lists
868  */
869 void   g_list_push_allocator    (GAllocator     *allocator);
870 void   g_list_pop_allocator     (void);
871 GList* g_list_alloc             (void);
872 void   g_list_free              (GList          *list);
873 void   g_list_free_1            (GList          *list);
874 GList* g_list_append            (GList          *list,
875                                  gpointer        data);
876 GList* g_list_prepend           (GList          *list,
877                                  gpointer        data);
878 GList* g_list_insert            (GList          *list,
879                                  gpointer        data,
880                                  gint            position);
881 GList* g_list_insert_sorted     (GList          *list,
882                                  gpointer        data,
883                                  GCompareFunc    func);
884 GList* g_list_concat            (GList          *list1,
885                                  GList          *list2);
886 GList* g_list_remove            (GList          *list,
887                                  gpointer        data);
888 GList* g_list_remove_link       (GList          *list,
889                                  GList          *llink);
890 GList* g_list_reverse           (GList          *list);
891 GList* g_list_copy              (GList          *list);
892 GList* g_list_nth               (GList          *list,
893                                  guint           n);
894 GList* g_list_find              (GList          *list,
895                                  gpointer        data);
896 GList* g_list_find_custom       (GList          *list,
897                                  gpointer        data,
898                                  GCompareFunc    func);
899 gint   g_list_position          (GList          *list,
900                                  GList          *llink);
901 gint   g_list_index             (GList          *list,
902                                  gpointer        data);
903 GList* g_list_last              (GList          *list);
904 GList* g_list_first             (GList          *list);
905 guint  g_list_length            (GList          *list);
906 void   g_list_foreach           (GList          *list,
907                                  GFunc           func,
908                                  gpointer        user_data);
909 GList* g_list_sort              (GList          *list,
910                                  GCompareFunc    compare_func);
911 gpointer g_list_nth_data        (GList          *list,
912                                  guint           n);
913 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
914 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
915
916
917 /* Singly linked lists
918  */
919 void    g_slist_push_allocator  (GAllocator     *allocator);
920 void    g_slist_pop_allocator   (void);
921 GSList* g_slist_alloc           (void);
922 void    g_slist_free            (GSList         *list);
923 void    g_slist_free_1          (GSList         *list);
924 GSList* g_slist_append          (GSList         *list,
925                                  gpointer        data);
926 GSList* g_slist_prepend         (GSList         *list,
927                                  gpointer        data);
928 GSList* g_slist_insert          (GSList         *list,
929                                  gpointer        data,
930                                  gint            position);
931 GSList* g_slist_insert_sorted   (GSList         *list,
932                                  gpointer        data,
933                                  GCompareFunc    func);
934 GSList* g_slist_concat          (GSList         *list1,
935                                  GSList         *list2);
936 GSList* g_slist_remove          (GSList         *list,
937                                  gpointer        data);
938 GSList* g_slist_remove_link     (GSList         *list,
939                                  GSList         *llink);
940 GSList* g_slist_reverse         (GSList         *list);
941 GSList* g_slist_copy            (GSList         *list);
942 GSList* g_slist_nth             (GSList         *list,
943                                  guint           n);
944 GSList* g_slist_find            (GSList         *list,
945                                  gpointer        data);
946 GSList* g_slist_find_custom     (GSList         *list,
947                                  gpointer        data,
948                                  GCompareFunc    func);
949 gint    g_slist_position        (GSList         *list,
950                                  GSList         *llink);
951 gint    g_slist_index           (GSList         *list,
952                                  gpointer        data);
953 GSList* g_slist_last            (GSList         *list);
954 guint   g_slist_length          (GSList         *list);
955 void    g_slist_foreach         (GSList         *list,
956                                  GFunc           func,
957                                  gpointer        user_data);
958 GSList*  g_slist_sort           (GSList          *list,
959                                  GCompareFunc    compare_func);
960 gpointer g_slist_nth_data       (GSList         *list,
961                                  guint           n);
962 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
963
964
965 /* Hash tables
966  */
967 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
968                                          GCompareFunc    key_compare_func);
969 void        g_hash_table_destroy        (GHashTable     *hash_table);
970 void        g_hash_table_insert         (GHashTable     *hash_table,
971                                          gpointer        key,
972                                          gpointer        value);
973 void        g_hash_table_remove         (GHashTable     *hash_table,
974                                          gconstpointer   key);
975 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
976                                          gconstpointer   key);
977 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
978                                          gconstpointer   lookup_key,
979                                          gpointer       *orig_key,
980                                          gpointer       *value);
981 void        g_hash_table_freeze         (GHashTable     *hash_table);
982 void        g_hash_table_thaw           (GHashTable     *hash_table);
983 void        g_hash_table_foreach        (GHashTable     *hash_table,
984                                          GHFunc          func,
985                                          gpointer        user_data);
986 gint        g_hash_table_foreach_remove (GHashTable     *hash_table,
987                                          GHRFunc         func,
988                                          gpointer        user_data);
989 gint        g_hash_table_size           (GHashTable     *hash_table);
990 void        g_hash_table_set_key_freefunc (GHashTable   *hash_table,
991                                          GFreeFunc      *free_func);
992
993
994 /* Caches
995  */
996 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
997                                 GCacheDestroyFunc  value_destroy_func,
998                                 GCacheDupFunc      key_dup_func,
999                                 GCacheDestroyFunc  key_destroy_func,
1000                                 GHashFunc          hash_key_func,
1001                                 GHashFunc          hash_value_func,
1002                                 GCompareFunc       key_compare_func);
1003 void     g_cache_destroy       (GCache            *cache);
1004 gpointer g_cache_insert        (GCache            *cache,
1005                                 gpointer           key);
1006 void     g_cache_remove        (GCache            *cache,
1007                                 gpointer           value);
1008 void     g_cache_key_foreach   (GCache            *cache,
1009                                 GHFunc             func,
1010                                 gpointer           user_data);
1011 void     g_cache_value_foreach (GCache            *cache,
1012                                 GHFunc             func,
1013                                 gpointer           user_data);
1014
1015
1016 /* Balanced binary trees
1017  */
1018 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
1019 void     g_tree_destroy  (GTree         *tree);
1020 void     g_tree_insert   (GTree         *tree,
1021                           gpointer       key,
1022                           gpointer       value);
1023 void     g_tree_remove   (GTree         *tree,
1024                           gpointer       key);
1025 gpointer g_tree_lookup   (GTree         *tree,
1026                           gpointer       key);
1027 void     g_tree_traverse (GTree         *tree,
1028                           GTraverseFunc  traverse_func,
1029                           GTraverseType  traverse_type,
1030                           gpointer       data);
1031 gpointer g_tree_search   (GTree         *tree,
1032                           GSearchFunc    search_func,
1033                           gpointer       data);
1034 gint     g_tree_height   (GTree         *tree);
1035 gint     g_tree_nnodes   (GTree         *tree);
1036
1037
1038
1039 /* N-way tree implementation
1040  */
1041 struct _GNode
1042 {
1043   gpointer data;
1044   GNode   *next;
1045   GNode   *prev;
1046   GNode   *parent;
1047   GNode   *children;
1048 };
1049
1050 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1051                                  ((GNode*) (node))->prev == NULL && \
1052                                  ((GNode*) (node))->next == NULL)
1053 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1054
1055 void     g_node_push_allocator  (GAllocator       *allocator);
1056 void     g_node_pop_allocator   (void);
1057 GNode*   g_node_new             (gpointer          data);
1058 void     g_node_destroy         (GNode            *root);
1059 void     g_node_unlink          (GNode            *node);
1060 GNode*   g_node_insert          (GNode            *parent,
1061                                  gint              position,
1062                                  GNode            *node);
1063 GNode*   g_node_insert_before   (GNode            *parent,
1064                                  GNode            *sibling,
1065                                  GNode            *node);
1066 GNode*   g_node_prepend         (GNode            *parent,
1067                                  GNode            *node);
1068 guint    g_node_n_nodes         (GNode            *root,
1069                                  GTraverseFlags    flags);
1070 GNode*   g_node_get_root        (GNode            *node);
1071 gboolean g_node_is_ancestor     (GNode            *node,
1072                                  GNode            *descendant);
1073 guint    g_node_depth           (GNode            *node);
1074 GNode*   g_node_find            (GNode            *root,
1075                                  GTraverseType     order,
1076                                  GTraverseFlags    flags,
1077                                  gpointer          data);
1078
1079 /* convenience macros */
1080 #define g_node_append(parent, node)                             \
1081      g_node_insert_before ((parent), NULL, (node))
1082 #define g_node_insert_data(parent, position, data)              \
1083      g_node_insert ((parent), (position), g_node_new (data))
1084 #define g_node_insert_data_before(parent, sibling, data)        \
1085      g_node_insert_before ((parent), (sibling), g_node_new (data))
1086 #define g_node_prepend_data(parent, data)                       \
1087      g_node_prepend ((parent), g_node_new (data))
1088 #define g_node_append_data(parent, data)                        \
1089      g_node_insert_before ((parent), NULL, g_node_new (data))
1090
1091 /* traversal function, assumes that `node' is root
1092  * (only traverses `node' and its subtree).
1093  * this function is just a high level interface to
1094  * low level traversal functions, optimized for speed.
1095  */
1096 void     g_node_traverse        (GNode            *root,
1097                                  GTraverseType     order,
1098                                  GTraverseFlags    flags,
1099                                  gint              max_depth,
1100                                  GNodeTraverseFunc func,
1101                                  gpointer          data);
1102
1103 /* return the maximum tree height starting with `node', this is an expensive
1104  * operation, since we need to visit all nodes. this could be shortened by
1105  * adding `guint height' to struct _GNode, but then again, this is not very
1106  * often needed, and would make g_node_insert() more time consuming.
1107  */
1108 guint    g_node_max_height       (GNode *root);
1109
1110 void     g_node_children_foreach (GNode           *node,
1111                                   GTraverseFlags   flags,
1112                                   GNodeForeachFunc func,
1113                                   gpointer         data);
1114 void     g_node_reverse_children (GNode           *node);
1115 guint    g_node_n_children       (GNode           *node);
1116 GNode*   g_node_nth_child        (GNode           *node,
1117                                   guint            n);
1118 GNode*   g_node_last_child       (GNode           *node);
1119 GNode*   g_node_find_child       (GNode           *node,
1120                                   GTraverseFlags   flags,
1121                                   gpointer         data);
1122 gint     g_node_child_position   (GNode           *node,
1123                                   GNode           *child);
1124 gint     g_node_child_index      (GNode           *node,
1125                                   gpointer         data);
1126
1127 GNode*   g_node_first_sibling    (GNode           *node);
1128 GNode*   g_node_last_sibling     (GNode           *node);
1129
1130 #define  g_node_prev_sibling(node)      ((node) ? \
1131                                          ((GNode*) (node))->prev : NULL)
1132 #define  g_node_next_sibling(node)      ((node) ? \
1133                                          ((GNode*) (node))->next : NULL)
1134 #define  g_node_first_child(node)       ((node) ? \
1135                                          ((GNode*) (node))->children : NULL)
1136
1137
1138 /* Callback maintenance functions
1139  */
1140 #define G_HOOK_FLAG_USER_SHIFT  (4)
1141 typedef enum
1142 {
1143   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1144   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1145   G_HOOK_FLAG_MASK      = 0x0f
1146 } GHookFlagMask;
1147
1148 struct _GHookList
1149 {
1150   guint          seq_id;
1151   guint          hook_size;
1152   guint          is_setup : 1;
1153   GHook         *hooks;
1154   GMemChunk     *hook_memchunk;
1155   GHookFreeFunc  hook_free; /* virtual function */
1156 };
1157
1158 struct _GHook
1159 {
1160   gpointer       data;
1161   GHook         *next;
1162   GHook         *prev;
1163   guint          ref_count;
1164   guint          hook_id;
1165   guint          flags;
1166   gpointer       func;
1167   GDestroyNotify destroy;
1168 };
1169
1170 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1171                                           G_HOOK_FLAG_ACTIVE) != 0)
1172 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1173                                           G_HOOK_FLAG_IN_CALL) != 0)
1174 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1175                                          G_HOOK_ACTIVE (hook))
1176 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1177                                          ((GHook*) hook)->prev == NULL && \
1178                                          ((GHook*) hook)->hook_id == 0 && \
1179                                          ((GHook*) hook)->ref_count == 0)
1180
1181 void     g_hook_list_init               (GHookList              *hook_list,
1182                                          guint                   hook_size);
1183 void     g_hook_list_clear              (GHookList              *hook_list);
1184 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1185 void     g_hook_free                    (GHookList              *hook_list,
1186                                          GHook                  *hook);
1187 void     g_hook_ref                     (GHookList              *hook_list,
1188                                          GHook                  *hook);
1189 void     g_hook_unref                   (GHookList              *hook_list,
1190                                          GHook                  *hook);
1191 gboolean g_hook_destroy                 (GHookList              *hook_list,
1192                                          guint                   hook_id);
1193 void     g_hook_destroy_link            (GHookList              *hook_list,
1194                                          GHook                  *hook);
1195 void     g_hook_prepend                 (GHookList              *hook_list,
1196                                          GHook                  *hook);
1197 void     g_hook_insert_before           (GHookList              *hook_list,
1198                                          GHook                  *sibling,
1199                                          GHook                  *hook);
1200 void     g_hook_insert_sorted           (GHookList              *hook_list,
1201                                          GHook                  *hook,
1202                                          GHookCompareFunc        func);
1203 GHook*   g_hook_get                     (GHookList              *hook_list,
1204                                          guint                   hook_id);
1205 GHook*   g_hook_find                    (GHookList              *hook_list,
1206                                          gboolean                need_valids,
1207                                          GHookFindFunc           func,
1208                                          gpointer                data);
1209 GHook*   g_hook_find_data               (GHookList              *hook_list,
1210                                          gboolean                need_valids,
1211                                          gpointer                data);
1212 GHook*   g_hook_find_func               (GHookList              *hook_list,
1213                                          gboolean                need_valids,
1214                                          gpointer                func);
1215 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1216                                          gboolean                need_valids,
1217                                          gpointer                func,
1218                                          gpointer                data);
1219 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1220                                          gboolean                may_be_in_call);
1221 GHook*   g_hook_next_valid              (GHook                  *hook,
1222                                          gboolean                may_be_in_call);
1223
1224 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1225 gint     g_hook_compare_ids             (GHook                  *new_hook,
1226                                          GHook                  *sibling);
1227
1228 /* convenience macros */
1229 #define  g_hook_append( hook_list, hook )  \
1230      g_hook_insert_before ((hook_list), NULL, (hook))
1231
1232 /* invoke all valid hooks with the (*GHookFunc) signature.
1233  */
1234 void     g_hook_list_invoke             (GHookList              *hook_list,
1235                                          gboolean                may_recurse);
1236 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1237  * and destroy the hook if FALSE is returned.
1238  */
1239 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1240                                          gboolean                may_recurse);
1241 /* invoke a marshaller on all valid hooks.
1242  */
1243 void     g_hook_list_marshal            (GHookList              *hook_list,
1244                                          gboolean                may_recurse,
1245                                          GHookMarshaller         marshaller,
1246                                          gpointer                data);
1247
1248
1249 /* Fatal error handlers.
1250  * g_on_error_query() will prompt the user to either
1251  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1252  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1253  * process and shows a stack trace.
1254  * These function may cause different actions on non-unix platforms.
1255  * The prg_name arg is required by gdb to find the executable, if it is
1256  * passed as NULL, g_on_error_query() will try g_get_prgname().
1257  */
1258 void g_on_error_query (const gchar *prg_name);
1259 void g_on_error_stack_trace (const gchar *prg_name);
1260
1261
1262 /* Logging mechanism
1263  */
1264 extern          const gchar             *g_log_domain_glib;
1265 guint           g_log_set_handler       (const gchar    *log_domain,
1266                                          GLogLevelFlags  log_levels,
1267                                          GLogFunc        log_func,
1268                                          gpointer        user_data);
1269 void            g_log_remove_handler    (const gchar    *log_domain,
1270                                          guint           handler_id);
1271 void            g_log_default_handler   (const gchar    *log_domain,
1272                                          GLogLevelFlags  log_level,
1273                                          const gchar    *message,
1274                                          gpointer        unused_data);
1275 void            g_log                   (const gchar    *log_domain,
1276                                          GLogLevelFlags  log_level,
1277                                          const gchar    *format,
1278                                          ...) G_GNUC_PRINTF (3, 4);
1279 void            g_logv                  (const gchar    *log_domain,
1280                                          GLogLevelFlags  log_level,
1281                                          const gchar    *format,
1282                                          va_list         args);
1283 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1284                                          GLogLevelFlags  fatal_mask);
1285 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1286 #ifndef G_LOG_DOMAIN
1287 #define G_LOG_DOMAIN    (NULL)
1288 #endif  /* G_LOG_DOMAIN */
1289 #ifdef  __GNUC__
1290 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1291                                                G_LOG_LEVEL_ERROR, \
1292                                                format, ##args)
1293 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1294                                                G_LOG_LEVEL_MESSAGE, \
1295                                                format, ##args)
1296 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1297                                                G_LOG_LEVEL_WARNING, \
1298                                                format, ##args)
1299 #else   /* !__GNUC__ */
1300 static inline void
1301 g_error (const gchar *format,
1302          ...)
1303 {
1304   va_list args;
1305   va_start (args, format);
1306   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1307   va_end (args);
1308 }
1309 static inline void
1310 g_message (const gchar *format,
1311            ...)
1312 {
1313   va_list args;
1314   va_start (args, format);
1315   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1316   va_end (args);
1317 }
1318 static inline void
1319 g_warning (const gchar *format,
1320            ...)
1321 {
1322   va_list args;
1323   va_start (args, format);
1324   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1325   va_end (args);
1326 }
1327 #endif  /* !__GNUC__ */
1328
1329 typedef void    (*GPrintFunc)           (const gchar    *string);
1330 void            g_print                 (const gchar    *format,
1331                                          ...) G_GNUC_PRINTF (1, 2);
1332 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1333 void            g_printerr              (const gchar    *format,
1334                                          ...) G_GNUC_PRINTF (1, 2);
1335 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1336
1337 /* deprecated compatibility functions, use g_log_set_handler() instead */
1338 typedef void            (*GErrorFunc)           (const gchar *str);
1339 typedef void            (*GWarningFunc)         (const gchar *str);
1340 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1341 GWarningFunc g_set_warning_handler (GWarningFunc func);
1342 GPrintFunc   g_set_message_handler (GPrintFunc func);
1343
1344
1345 /* Memory allocation and debugging
1346  */
1347 #ifdef USE_DMALLOC
1348
1349 #define g_malloc(size)       ((gpointer) MALLOC (size))
1350 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1351 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1352 #define g_free(mem)          FREE (mem)
1353
1354 #else /* !USE_DMALLOC */
1355
1356 gpointer g_malloc      (gulong    size);
1357 gpointer g_malloc0     (gulong    size);
1358 gpointer g_realloc     (gpointer  mem,
1359                         gulong    size);
1360 void     g_free        (gpointer  mem);
1361
1362 #endif /* !USE_DMALLOC */
1363
1364 void     g_mem_profile (void);
1365 void     g_mem_check   (gpointer  mem);
1366
1367 /* Generic allocators
1368  */
1369 GAllocator* g_allocator_new   (const gchar  *name,
1370                                guint         n_preallocs);
1371 void        g_allocator_free  (GAllocator   *allocator);
1372
1373 #define G_ALLOCATOR_LIST        (1)
1374 #define G_ALLOCATOR_SLIST       (2)
1375 #define G_ALLOCATOR_NODE        (3)
1376
1377
1378 /* "g_mem_chunk_new" creates a new memory chunk.
1379  * Memory chunks are used to allocate pieces of memory which are
1380  *  always the same size. Lists are a good example of such a data type.
1381  * The memory chunk allocates and frees blocks of memory as needed.
1382  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1383  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1384  *  fault...somewhere).
1385  *
1386  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1387  *  want to know what's going on inside do you?)
1388  */
1389
1390 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1391  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1392  *  atom. (They are also useful for lists which use MemChunk to allocate
1393  *  memory but are also part of the MemChunk implementation).
1394  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1395  */
1396
1397 #define G_ALLOC_ONLY      1
1398 #define G_ALLOC_AND_FREE  2
1399
1400 GMemChunk* g_mem_chunk_new     (gchar     *name,
1401                                 gint       atom_size,
1402                                 gulong     area_size,
1403                                 gint       type);
1404 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1405 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1406 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1407 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1408                                 gpointer   mem);
1409 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1410 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1411 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1412 void       g_mem_chunk_info    (void);
1413
1414 /* Ah yes...we have a "g_blow_chunks" function.
1415  * "g_blow_chunks" simply compresses all the chunks. This operation
1416  *  consists of freeing every memory area that should be freed (but
1417  *  which we haven't gotten around to doing yet). And, no,
1418  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1419  *  much better name than "g_mem_chunk_clean_all" or something
1420  *  similar.
1421  */
1422 void g_blow_chunks (void);
1423
1424
1425 /* Timer
1426  */
1427 GTimer* g_timer_new     (void);
1428 void    g_timer_destroy (GTimer  *timer);
1429 void    g_timer_start   (GTimer  *timer);
1430 void    g_timer_stop    (GTimer  *timer);
1431 void    g_timer_reset   (GTimer  *timer);
1432 gdouble g_timer_elapsed (GTimer  *timer,
1433                          gulong  *microseconds);
1434
1435
1436 /* String utility functions that modify a string argument or
1437  * return a constant string that must not be freed.
1438  */
1439 #define  G_STR_DELIMITERS       "_-|> <."
1440 gchar*   g_strdelimit           (gchar       *string,
1441                                  const gchar *delimiters,
1442                                  gchar        new_delimiter);
1443 gdouble  g_strtod               (const gchar *nptr,
1444                                  gchar      **endptr);
1445 gchar*   g_strerror             (gint         errnum);
1446 gchar*   g_strsignal            (gint         signum);
1447 gint     g_strcasecmp           (const gchar *s1,
1448                                  const gchar *s2);
1449 gint     g_strncasecmp          (const gchar *s1,
1450                                  const gchar *s2,
1451                                  guint        n);
1452 void     g_strdown              (gchar       *string);
1453 void     g_strup                (gchar       *string);
1454 void     g_strreverse           (gchar       *string);
1455 /* removes leading spaces */
1456 gchar*   g_strchug              (gchar        *string);
1457 /* removes trailing spaces */
1458 gchar*  g_strchomp              (gchar        *string);
1459 /* removes leading & trailing spaces */
1460 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1461
1462 /* String utility functions that return a newly allocated string which
1463  * ought to be freed from the caller at some point.
1464  */
1465 gchar*   g_strdup               (const gchar *str);
1466 gchar*   g_strdup_printf        (const gchar *format,
1467                                  ...) G_GNUC_PRINTF (1, 2);
1468 gchar*   g_strdup_vprintf       (const gchar *format,
1469                                  va_list      args);
1470 gchar*   g_strndup              (const gchar *str,
1471                                  guint        n);
1472 gchar*   g_strnfill             (guint        length,
1473                                  gchar        fill_char);
1474 gchar*   g_strconcat            (const gchar *string1,
1475                                  ...); /* NULL terminated */
1476 gchar*   g_strjoin              (const gchar  *separator,
1477                                  ...); /* NULL terminated */
1478 gchar*   g_strescape            (gchar        *string);
1479 gpointer g_memdup               (gconstpointer mem,
1480                                  guint         byte_size);
1481
1482 /* NULL terminated string arrays.
1483  * g_strsplit() splits up string into max_tokens tokens at delim and
1484  * returns a newly allocated string array.
1485  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1486  * optional separator, the returned string is newly allocated.
1487  * g_strfreev() frees the array itself and all of its strings.
1488  */
1489 gchar**  g_strsplit             (const gchar  *string,
1490                                  const gchar  *delimiter,
1491                                  gint          max_tokens);
1492 gchar*   g_strjoinv             (const gchar  *separator,
1493                                  gchar       **str_array);
1494 void     g_strfreev             (gchar       **str_array);
1495
1496
1497
1498 /* calculate a string size, guarranteed to fit format + args.
1499  */
1500 guint   g_printf_string_upper_bound (const gchar* format,
1501                                      va_list      args);
1502
1503
1504 /* Retrive static string info
1505  */
1506 gchar*  g_get_user_name         (void);
1507 gchar*  g_get_real_name         (void);
1508 gchar*  g_get_home_dir          (void);
1509 gchar*  g_get_tmp_dir           (void);
1510 gchar*  g_get_prgname           (void);
1511 void    g_set_prgname           (const gchar *prgname);
1512
1513
1514 /* Miscellaneous utility functions
1515  */
1516 guint   g_parse_debug_string    (const gchar *string,
1517                                  GDebugKey   *keys,
1518                                  guint        nkeys);
1519 gint    g_snprintf              (gchar       *string,
1520                                  gulong       n,
1521                                  gchar const *format,
1522                                  ...) G_GNUC_PRINTF (3, 4);
1523 gint    g_vsnprintf             (gchar       *string,
1524                                  gulong       n,
1525                                  gchar const *format,
1526                                  va_list      args);
1527 gchar*  g_basename              (const gchar *file_name);
1528 /* Check if a file name is an absolute path */
1529 gboolean g_path_is_absolute     (const gchar *file_name);
1530 /* In case of absolute paths, skip the root part */
1531 gchar*  g_path_skip_root        (gchar       *file_name);
1532
1533 /* strings are newly allocated with g_malloc() */
1534 gchar*  g_dirname               (const gchar *file_name);
1535 gchar*  g_get_current_dir       (void);
1536 gchar*  g_getenv                (const gchar *variable);
1537
1538
1539 /* we use a GLib function as a replacement for ATEXIT, so
1540  * the programmer is not required to check the return value
1541  * (if there is any in the implementation) and doesn't encounter
1542  * missing include files.
1543  */
1544 void    g_atexit                (GVoidFunc    func);
1545
1546
1547 /* Bit tests
1548  */
1549 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1550                                        gint    nth_bit);
1551 #ifdef  G_CAN_INLINE
1552 G_INLINE_FUNC gint
1553 g_bit_nth_lsf (guint32 mask,
1554                gint    nth_bit)
1555 {
1556   do
1557     {
1558       nth_bit++;
1559       if (mask & (1 << (guint) nth_bit))
1560         return nth_bit;
1561     }
1562   while (nth_bit < 32);
1563   return -1;
1564 }
1565 #endif  /* G_CAN_INLINE */
1566
1567 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1568                                        gint    nth_bit);
1569 #ifdef G_CAN_INLINE
1570 G_INLINE_FUNC gint
1571 g_bit_nth_msf (guint32 mask,
1572                gint    nth_bit)
1573 {
1574   if (nth_bit < 0)
1575     nth_bit = 32;
1576   do
1577     {
1578       nth_bit--;
1579       if (mask & (1 << (guint) nth_bit))
1580         return nth_bit;
1581     }
1582   while (nth_bit > 0);
1583   return -1;
1584 }
1585 #endif  /* G_CAN_INLINE */
1586
1587 G_INLINE_FUNC guint     g_bit_storage (guint number);
1588 #ifdef G_CAN_INLINE
1589 G_INLINE_FUNC guint
1590 g_bit_storage (guint number)
1591 {
1592   register guint n_bits = 0;
1593   
1594   do
1595     {
1596       n_bits++;
1597       number >>= 1;
1598     }
1599   while (number);
1600   return n_bits;
1601 }
1602 #endif  /* G_CAN_INLINE */
1603
1604 /* String Chunks
1605  */
1606 GStringChunk* g_string_chunk_new           (gint size);
1607 void          g_string_chunk_free          (GStringChunk *chunk);
1608 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1609                                             const gchar  *string);
1610 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1611                                             const gchar  *string);
1612
1613
1614 /* Strings
1615  */
1616 GString* g_string_new       (const gchar *init);
1617 GString* g_string_sized_new (guint        dfl_size);
1618 void     g_string_free      (GString     *string,
1619                              gint         free_segment);
1620 GString* g_string_assign    (GString     *lval,
1621                              const gchar *rval);
1622 GString* g_string_truncate  (GString     *string,
1623                              gint         len);
1624 GString* g_string_append    (GString     *string,
1625                              const gchar *val);
1626 GString* g_string_append_c  (GString     *string,
1627                              gchar        c);
1628 GString* g_string_prepend   (GString     *string,
1629                              const gchar *val);
1630 GString* g_string_prepend_c (GString     *string,
1631                              gchar        c);
1632 GString* g_string_insert    (GString     *string,
1633                              gint         pos,
1634                              const gchar *val);
1635 GString* g_string_insert_c  (GString     *string,
1636                              gint         pos,
1637                              gchar        c);
1638 GString* g_string_erase     (GString     *string,
1639                              gint         pos,
1640                              gint         len);
1641 GString* g_string_down      (GString     *string);
1642 GString* g_string_up        (GString     *string);
1643 void     g_string_sprintf   (GString     *string,
1644                              const gchar *format,
1645                              ...) G_GNUC_PRINTF (2, 3);
1646 void     g_string_sprintfa  (GString     *string,
1647                              const gchar *format,
1648                              ...) G_GNUC_PRINTF (2, 3);
1649
1650
1651 /* Resizable arrays, remove fills any cleared spot and shortens the
1652  * array, while preserving the order. remove_fast will distort the
1653  * order by moving the last element to the position of the removed 
1654  */
1655
1656 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1657 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1658 #define g_array_insert_val(a,i,v) g_array_prepend_vals(a,i,&v,1)
1659 #define g_array_index(a,t,i) (((t*)a->data)[i])
1660
1661 GArray* g_array_new               (gboolean         zero_terminated,
1662                                    gboolean         clear,
1663                                    guint            element_size);
1664 void    g_array_free              (GArray          *array,
1665                                    gboolean         free_segment);
1666 GArray* g_array_append_vals       (GArray          *array,
1667                                    gconstpointer    data,
1668                                    guint            len);
1669 GArray* g_array_prepend_vals      (GArray          *array,
1670                                    gconstpointer    data,
1671                                    guint            len);
1672 GArray* g_array_insert_vals       (GArray          *array,
1673                                    guint            index,
1674                                    gconstpointer    data,
1675                                    guint            len);
1676 GArray* g_array_set_size          (GArray          *array,
1677                                    guint            length);
1678 GArray* g_array_remove_index      (GArray          *array,
1679                                    guint            index);
1680 GArray* g_array_remove_index_fast (GArray          *array,
1681                                    guint            index);
1682
1683 /* Resizable pointer array.  This interface is much less complicated
1684  * than the above.  Add appends appends a pointer.  Remove fills any
1685  * cleared spot and shortens the array. remove_fast will again distort
1686  * order.  
1687  */
1688 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1689 GPtrArray*  g_ptr_array_new                (void);
1690 void        g_ptr_array_free               (GPtrArray   *array,
1691                                             gboolean     free_seg);
1692 void        g_ptr_array_set_size           (GPtrArray   *array,
1693                                             gint         length);
1694 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1695                                             guint        index);
1696 gpointer    g_ptr_array_remove_index_fast  (GPtrArray   *array,
1697                                             guint        index);
1698 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1699                                             gpointer     data);
1700 gboolean    g_ptr_array_remove_fast        (GPtrArray   *array,
1701                                             gpointer     data);
1702 void        g_ptr_array_add                (GPtrArray   *array,
1703                                             gpointer     data);
1704
1705 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1706  * but type-safe.
1707  */
1708
1709 GByteArray* g_byte_array_new               (void);
1710 void        g_byte_array_free              (GByteArray   *array,
1711                                             gboolean      free_segment);
1712 GByteArray* g_byte_array_append            (GByteArray   *array,
1713                                             const guint8 *data,
1714                                             guint         len);
1715 GByteArray* g_byte_array_prepend           (GByteArray   *array,
1716                                             const guint8 *data,
1717                                             guint         len);
1718 GByteArray* g_byte_array_set_size          (GByteArray   *array,
1719                                             guint         length);
1720 GByteArray* g_byte_array_remove_index      (GByteArray   *array,
1721                                             guint         index);
1722 GByteArray* g_byte_array_remove_index_fast (GByteArray   *array,
1723                                             guint         index);
1724
1725
1726 /* Hash Functions
1727  */
1728 gint  g_str_equal (gconstpointer   v,
1729                    gconstpointer   v2);
1730 guint g_str_hash  (gconstpointer   v);
1731
1732 gint  g_int_equal (gconstpointer   v,
1733                    gconstpointer   v2);
1734 guint g_int_hash  (gconstpointer   v);
1735
1736 /* This "hash" function will just return the key's adress as an
1737  * unsigned integer. Useful for hashing on plain adresses or
1738  * simple integer values.
1739  */
1740 guint g_direct_hash  (gconstpointer v);
1741 gint  g_direct_equal (gconstpointer v,
1742                       gconstpointer v2);
1743
1744
1745 /* Quarks (string<->id association)
1746  */
1747 GQuark    g_quark_try_string            (const gchar    *string);
1748 GQuark    g_quark_from_static_string    (const gchar    *string);
1749 GQuark    g_quark_from_string           (const gchar    *string);
1750 gchar*    g_quark_to_string             (GQuark          quark);
1751
1752
1753 /* Keyed Data List
1754  */
1755 void      g_datalist_init                (GData          **datalist);
1756 void      g_datalist_clear               (GData          **datalist);
1757 gpointer  g_datalist_id_get_data         (GData          **datalist,
1758                                           GQuark           key_id);
1759 void      g_datalist_id_set_data_full    (GData          **datalist,
1760                                           GQuark           key_id,
1761                                           gpointer         data,
1762                                           GDestroyNotify   destroy_func);
1763 void      g_datalist_id_remove_no_notify (GData          **datalist,
1764                                           GQuark           key_id);
1765 void      g_datalist_foreach             (GData          **datalist,
1766                                           GDataForeachFunc func,
1767                                           gpointer         user_data);
1768 #define   g_datalist_id_set_data(dl, q, d)      \
1769      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1770 #define   g_datalist_id_remove_data(dl, q)      \
1771      g_datalist_id_set_data ((dl), (q), NULL)
1772 #define   g_datalist_get_data(dl, k)            \
1773      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1774 #define   g_datalist_set_data_full(dl, k, d, f) \
1775      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1776 #define   g_datalist_remove_no_notify(dl, k)    \
1777      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1778 #define   g_datalist_set_data(dl, k, d)         \
1779      g_datalist_set_data_full ((dl), (k), (d), NULL)
1780 #define   g_datalist_remove_data(dl, k)         \
1781      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1782
1783
1784 /* Location Associated Keyed Data
1785  */
1786 void      g_dataset_destroy             (gconstpointer    dataset_location);
1787 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
1788                                          GQuark           key_id);
1789 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
1790                                          GQuark           key_id,
1791                                          gpointer         data,
1792                                          GDestroyNotify   destroy_func);
1793 void      g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
1794                                          GQuark           key_id);
1795 void      g_dataset_foreach             (gconstpointer    dataset_location,
1796                                          GDataForeachFunc func,
1797                                          gpointer         user_data);
1798 #define   g_dataset_id_set_data(l, k, d)        \
1799      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1800 #define   g_dataset_id_remove_data(l, k)        \
1801      g_dataset_id_set_data ((l), (k), NULL)
1802 #define   g_dataset_get_data(l, k)              \
1803      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1804 #define   g_dataset_set_data_full(l, k, d, f)   \
1805      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1806 #define   g_dataset_remove_no_notify(l, k)      \
1807      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1808 #define   g_dataset_set_data(l, k, d)           \
1809      g_dataset_set_data_full ((l), (k), (d), NULL)
1810 #define   g_dataset_remove_data(l, k)           \
1811      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1812
1813
1814 /* GScanner: Flexible lexical scanner for general purpose.
1815  */
1816
1817 /* Character sets */
1818 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1819 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1820 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1821                         "\307\310\311\312\313\314\315\316\317\320"\
1822                         "\321\322\323\324\325\326"\
1823                         "\330\331\332\333\334\335\336"
1824 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1825                         "\347\350\351\352\353\354\355\356\357\360"\
1826                         "\361\362\363\364\365\366"\
1827                         "\370\371\372\373\374\375\376\377"
1828
1829 /* Error types */
1830 typedef enum
1831 {
1832   G_ERR_UNKNOWN,
1833   G_ERR_UNEXP_EOF,
1834   G_ERR_UNEXP_EOF_IN_STRING,
1835   G_ERR_UNEXP_EOF_IN_COMMENT,
1836   G_ERR_NON_DIGIT_IN_CONST,
1837   G_ERR_DIGIT_RADIX,
1838   G_ERR_FLOAT_RADIX,
1839   G_ERR_FLOAT_MALFORMED
1840 } GErrorType;
1841
1842 /* Token types */
1843 typedef enum
1844 {
1845   G_TOKEN_EOF                   =   0,
1846   
1847   G_TOKEN_LEFT_PAREN            = '(',
1848   G_TOKEN_RIGHT_PAREN           = ')',
1849   G_TOKEN_LEFT_CURLY            = '{',
1850   G_TOKEN_RIGHT_CURLY           = '}',
1851   G_TOKEN_LEFT_BRACE            = '[',
1852   G_TOKEN_RIGHT_BRACE           = ']',
1853   G_TOKEN_EQUAL_SIGN            = '=',
1854   G_TOKEN_COMMA                 = ',',
1855   
1856   G_TOKEN_NONE                  = 256,
1857   
1858   G_TOKEN_ERROR,
1859   
1860   G_TOKEN_CHAR,
1861   G_TOKEN_BINARY,
1862   G_TOKEN_OCTAL,
1863   G_TOKEN_INT,
1864   G_TOKEN_HEX,
1865   G_TOKEN_FLOAT,
1866   G_TOKEN_STRING,
1867   
1868   G_TOKEN_SYMBOL,
1869   G_TOKEN_IDENTIFIER,
1870   G_TOKEN_IDENTIFIER_NULL,
1871   
1872   G_TOKEN_COMMENT_SINGLE,
1873   G_TOKEN_COMMENT_MULTI,
1874   G_TOKEN_LAST
1875 } GTokenType;
1876
1877 union   _GTokenValue
1878 {
1879   gpointer      v_symbol;
1880   gchar         *v_identifier;
1881   gulong        v_binary;
1882   gulong        v_octal;
1883   gulong        v_int;
1884   gdouble       v_float;
1885   gulong        v_hex;
1886   gchar         *v_string;
1887   gchar         *v_comment;
1888   guchar        v_char;
1889   guint         v_error;
1890 };
1891
1892 struct  _GScannerConfig
1893 {
1894   /* Character sets
1895    */
1896   gchar         *cset_skip_characters;          /* default: " \t\n" */
1897   gchar         *cset_identifier_first;
1898   gchar         *cset_identifier_nth;
1899   gchar         *cpair_comment_single;          /* default: "#\n" */
1900   
1901   /* Should symbol lookup work case sensitive?
1902    */
1903   guint         case_sensitive : 1;
1904   
1905   /* Boolean values to be adjusted "on the fly"
1906    * to configure scanning behaviour.
1907    */
1908   guint         skip_comment_multi : 1;         /* C like comment */
1909   guint         skip_comment_single : 1;        /* single line comment */
1910   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1911   guint         scan_identifier : 1;
1912   guint         scan_identifier_1char : 1;
1913   guint         scan_identifier_NULL : 1;
1914   guint         scan_symbols : 1;
1915   guint         scan_binary : 1;
1916   guint         scan_octal : 1;
1917   guint         scan_float : 1;
1918   guint         scan_hex : 1;                   /* `0x0ff0' */
1919   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1920   guint         scan_string_sq : 1;             /* string: 'anything' */
1921   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1922   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1923   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1924   guint         identifier_2_string : 1;
1925   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1926   guint         symbol_2_token : 1;
1927   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
1928 };
1929
1930 struct  _GScanner
1931 {
1932   /* unused fields */
1933   gpointer              user_data;
1934   guint                 max_parse_errors;
1935   
1936   /* g_scanner_error() increments this field */
1937   guint                 parse_errors;
1938   
1939   /* name of input stream, featured by the default message handler */
1940   const gchar           *input_name;
1941   
1942   /* data pointer for derived structures */
1943   gpointer              derived_data;
1944   
1945   /* link into the scanner configuration */
1946   GScannerConfig        *config;
1947   
1948   /* fields filled in after g_scanner_get_next_token() */
1949   GTokenType            token;
1950   GTokenValue           value;
1951   guint                 line;
1952   guint                 position;
1953   
1954   /* fields filled in after g_scanner_peek_next_token() */
1955   GTokenType            next_token;
1956   GTokenValue           next_value;
1957   guint                 next_line;
1958   guint                 next_position;
1959   
1960   /* to be considered private */
1961   GHashTable            *symbol_table;
1962   gint                  input_fd;
1963   const gchar           *text;
1964   const gchar           *text_end;
1965   gchar                 *buffer;
1966   guint                 scope_id;
1967   
1968   /* handler function for _warn and _error */
1969   GScannerMsgFunc       msg_handler;
1970 };
1971
1972 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
1973 void            g_scanner_destroy               (GScanner       *scanner);
1974 void            g_scanner_input_file            (GScanner       *scanner,
1975                                                  gint           input_fd);
1976 void            g_scanner_sync_file_offset      (GScanner       *scanner);
1977 void            g_scanner_input_text            (GScanner       *scanner,
1978                                                  const  gchar   *text,
1979                                                  guint          text_len);
1980 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
1981 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
1982 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
1983 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
1984 guint           g_scanner_cur_line              (GScanner       *scanner);
1985 guint           g_scanner_cur_position          (GScanner       *scanner);
1986 gboolean        g_scanner_eof                   (GScanner       *scanner);
1987 guint           g_scanner_set_scope             (GScanner       *scanner,
1988                                                  guint           scope_id);
1989 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
1990                                                  guint           scope_id,
1991                                                  const gchar    *symbol,
1992                                                  gpointer       value);
1993 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
1994                                                  guint           scope_id,
1995                                                  const gchar    *symbol);
1996 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
1997                                                  guint           scope_id,
1998                                                  const gchar    *symbol);
1999 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
2000                                                  guint           scope_id,
2001                                                  GHFunc          func,
2002                                                  gpointer        func_data);
2003 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
2004                                                  const gchar    *symbol);
2005 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
2006 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
2007 void            g_scanner_unexp_token           (GScanner       *scanner,
2008                                                  GTokenType     expected_token,
2009                                                  const gchar    *identifier_spec,
2010                                                  const gchar    *symbol_spec,
2011                                                  const gchar    *symbol_name,
2012                                                  const gchar    *message,
2013                                                  gint            is_error);
2014 void            g_scanner_error                 (GScanner       *scanner,
2015                                                  const gchar    *format,
2016                                                  ...) G_GNUC_PRINTF (2,3);
2017 void            g_scanner_warn                  (GScanner       *scanner,
2018                                                  const gchar    *format,
2019                                                  ...) G_GNUC_PRINTF (2,3);
2020 gint            g_scanner_stat_mode             (const gchar    *filename);
2021 /* keep downward source compatibility */
2022 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
2023   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2024 } G_STMT_END
2025 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
2026   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2027 } G_STMT_END
2028 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2029   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2030 } G_STMT_END
2031
2032
2033 /* Completion */
2034
2035 struct _GCompletion
2036 {
2037   GList* items;
2038   GCompletionFunc func;
2039   
2040   gchar* prefix;
2041   GList* cache;
2042 };
2043
2044 GCompletion* g_completion_new          (GCompletionFunc func);
2045 void         g_completion_add_items    (GCompletion*    cmp,
2046                                         GList*          items);
2047 void         g_completion_remove_items (GCompletion*    cmp,
2048                                         GList*          items);
2049 void         g_completion_clear_items  (GCompletion*    cmp);
2050 GList*       g_completion_complete     (GCompletion*    cmp,
2051                                         gchar*          prefix,
2052                                         gchar**         new_prefix);
2053 void         g_completion_free         (GCompletion*    cmp);
2054
2055
2056 /* GRelation: Indexed Relations.  Imagine a really simple table in a
2057  * database.  Relations are not ordered.  This data type is meant for
2058  * maintaining a N-way mapping.
2059  *
2060  * g_relation_new() creates a relation with FIELDS fields
2061  *
2062  * g_relation_destroy() frees all resources
2063  * g_tuples_destroy() frees the result of g_relation_select()
2064  *
2065  * g_relation_index() indexes relation FIELD with the provided
2066  *   equality and hash functions.  this must be done before any
2067  *   calls to insert are made.
2068  *
2069  * g_relation_insert() inserts a new tuple.  you are expected to
2070  *   provide the right number of fields.
2071  *
2072  * g_relation_delete() deletes all relations with KEY in FIELD
2073  * g_relation_select() returns ...
2074  * g_relation_count() counts ...
2075  */
2076
2077 GRelation* g_relation_new     (gint         fields);
2078 void       g_relation_destroy (GRelation   *relation);
2079 void       g_relation_index   (GRelation   *relation,
2080                                gint         field,
2081                                GHashFunc    hash_func,
2082                                GCompareFunc key_compare_func);
2083 void       g_relation_insert  (GRelation   *relation,
2084                                ...);
2085 gint       g_relation_delete  (GRelation   *relation,
2086                                gconstpointer  key,
2087                                gint         field);
2088 GTuples*   g_relation_select  (GRelation   *relation,
2089                                gconstpointer  key,
2090                                gint         field);
2091 gint       g_relation_count   (GRelation   *relation,
2092                                gconstpointer  key,
2093                                gint         field);
2094 gboolean   g_relation_exists  (GRelation   *relation,
2095                                ...);
2096 void       g_relation_print   (GRelation   *relation);
2097
2098 void       g_tuples_destroy   (GTuples     *tuples);
2099 gpointer   g_tuples_index     (GTuples     *tuples,
2100                                gint         index,
2101                                gint         field);
2102
2103
2104 /* Prime numbers.
2105  */
2106
2107 /* This function returns prime numbers spaced by approximately 1.5-2.0
2108  * and is for use in resizing data structures which prefer
2109  * prime-valued sizes.  The closest spaced prime function returns the
2110  * next largest prime, or the highest it knows about which is about
2111  * MAXINT/4.
2112  */
2113 guint      g_spaced_primes_closest (guint num);
2114
2115
2116 /* IO Channels.
2117  * These are used for plug-in communication in the GIMP, for instance.
2118  * On Unix, it's simply an encapsulated file descriptor (a pipe).
2119  * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2120  * of the writable end) a thread id to post a message to when you have written
2121  * stuff.
2122  */
2123 struct _GIOChannel
2124 {
2125   gint fd;                      /* file handle (pseudo such in Win32) */
2126 #ifdef NATIVE_WIN32
2127   guint peer;                   /* thread to post message to */
2128   guint peer_fd;                /* read handle (in the other process) */
2129   guint offset;                 /* counter of accumulated bytes, to
2130                                  * be included in the message posted
2131                                  * so we keep in sync.
2132                                  */
2133   guint peer_offset;            /* in input channels where the writer's
2134                                  * offset is, so we don't try to read too much
2135                                  */
2136 #endif
2137 };
2138
2139 GIOChannel *g_iochannel_new            (gint        fd);
2140 void        g_iochannel_free           (GIOChannel *channel);
2141 void        g_iochannel_close_and_free (GIOChannel *channel);
2142 void        g_iochannel_wakeup_peer    (GIOChannel *channel);
2143 #ifndef NATIVE_WIN32
2144 #  define   g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2145 #endif
2146
2147
2148 /* Windows emulation stubs for common unix functions
2149  */
2150 #ifdef NATIVE_WIN32
2151
2152 #define MAXPATHLEN 1024
2153
2154 #ifdef _MSC_VER
2155 typedef int pid_t;
2156
2157 /* These POSIXish functions are available in the Microsoft C library
2158  * prefixed with underscore (which of course technically speaking is
2159  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2160  * prevents Microsoft from practically requiring you to include
2161  * <windows.h> every now and then...).
2162  *
2163  * You still need to include the appropriate headers to get the
2164  * prototypes, <io.h> or <direct.h>.
2165  *
2166  * For some functions, we provide emulators in glib, which are prefixed
2167  * with gwin_.
2168  */
2169 #define getcwd                  _getcwd
2170 #define getpid                  _getpid
2171 #define access                  _access
2172 #define open                    _open
2173 #define read                    _read
2174 #define write                   _write
2175 #define lseek                   _lseek
2176 #define close                   _close
2177 #define pipe(phandles)          _pipe (phandles, 4096, _O_BINARY)
2178 #define popen                   _popen
2179 #define pclose                  _pclose
2180 #define fdopen                  _fdopen
2181 #define ftruncate(fd, size)     gwin_ftruncate (fd, size)
2182 #define opendir                 gwin_opendir
2183 #define readdir                 gwin_readdir
2184 #define rewinddir               gwin_rewinddir
2185 #define closedir                gwin_closedir
2186
2187 #define NAME_MAX 255
2188
2189 struct DIR
2190 {
2191   gchar    *dir_name;
2192
2193   gboolean  just_opened;
2194   guint     find_file_handle;
2195   gpointer  find_file_data;
2196 };
2197 typedef struct DIR DIR;
2198 struct dirent
2199 {
2200   gchar  d_name[NAME_MAX + 1];
2201 };
2202
2203 /* emulation functions */
2204 extern int      gwin_ftruncate  (gint            f,
2205                                  guint           size);
2206 DIR*            gwin_opendir    (const gchar    *dirname);
2207 struct dirent*  gwin_readdir    (DIR            *dir);
2208 void            gwin_rewinddir  (DIR            *dir);
2209 gint            gwin_closedir   (DIR            *dir);
2210
2211 #endif /* _MSC_VER */
2212
2213 #endif /* NATIVE_WIN32 */
2214
2215
2216 #ifdef __cplusplus
2217 }
2218 #endif /* __cplusplus */
2219
2220
2221 #endif /* __G_LIB_H__ */