glib.h endian macros defined using the glibconfig.h mechanism now
[platform/upstream/glib.git] / glib / glib.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #ifndef __G_LIB_H__
20 #define __G_LIB_H__
21
22 /* system specific config file glibconfig.h provides definitions for
23  * the extrema of many of the standard types. These are:
24  *
25  *  G_MINSHORT, G_MAXSHORT
26  *  G_MININT, G_MAXINT
27  *  G_MINLONG, G_MAXLONG
28  *  G_MINFLOAT, G_MAXFLOAT
29  *  G_MINDOUBLE, G_MAXDOUBLE
30  *
31  * It also provides the following typedefs:
32  *
33  *  gint8, guint8
34  *  gint16, guint16
35  *  gint32, guint32
36  *  gint64, guint64
37  *
38  * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
39  * this file). 
40  *
41  * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
42  * This is useful to pass an integer instead of a pointer to a callback.
43  *
44  *  GINT_TO_POINTER(i), GUINT_TO_POINTER(i)
45  *  GPOINTER_TO_INT(p), GPOINTER_TO_UINT(p)
46  *
47  * Finally, it provide the following wrappers to STDC functions:
48  *
49  *  g_ATEXIT
50  *    To register hooks which are executed on exit().
51  *    Usually a wrapper for STDC atexit.
52  *
53  *  void *g_memmove(void *dest, const void *src, guint count);
54  *    A wrapper for STDC memmove, or an implementation, if memmove doesn't
55  *    exist.  The prototype looks like the above, give or take a const,
56  *    or size_t.
57  */
58 #include <glibconfig.h>
59
60 /* include varargs functions for assertment macros
61  */
62 #include <stdarg.h>
63
64 /* optionally feature DMALLOC memory allocation debugger
65  */
66 #ifdef USE_DMALLOC
67 #include "dmalloc.h"
68 #endif
69
70
71 #ifdef NATIVE_WIN32
72
73 /* On native Win32, directory separator is the backslash, and search path
74  * separator is the semicolon.
75  */
76 #define G_DIR_SEPARATOR '\\'
77 #define G_DIR_SEPARATOR_S "\\"
78 #define G_SEARCHPATH_SEPARATOR ';'
79 #define G_SEARCHPATH_SEPARATOR_S ";"
80
81 #else  /* !NATIVE_WIN32 */
82
83 /* Unix */
84
85 #define G_DIR_SEPARATOR '/'
86 #define G_DIR_SEPARATOR_S "/"
87 #define G_SEARCHPATH_SEPARATOR ':'
88 #define G_SEARCHPATH_SEPARATOR_S ":"
89
90 #endif /* !NATIVE_WIN32 */
91
92 #ifdef _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 x86 machines.
302  * Actual use is strongly deprecated of course ;)
303  */
304 #if defined (__i386__) && defined (__GNUC__)
305 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
306 #elif defined (__alpha__) && defined (__GNUC__)
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__)
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__ __volatile__ ("rorw $8, %w0"                 \
545                                  : "=r" (__v)                   \
546                                  : "0" ((guint16) (val))        \
547                                  : "cc");                       \
548         __v; }))
549
550 #  define GUINT16_SWAP_LE_BE(val) \
551      ((guint16) GUINT16_SWAP_LE_BE_X86 ((guint16) (val)))
552
553 #  if !defined(__i486__) && !defined(__i586__) \
554       && !defined(__pentium__) && !defined(__pentiumpro__) && !defined(__i686__)
555 #     define GUINT32_SWAP_LE_BE_X86(val) \
556         (__extension__                                          \
557          ({ register guint32 __v;                               \
558             if (__builtin_constant_p (val))                     \
559               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
560           else                                                  \
561             __asm__ __volatile__ ("rorw $8, %w0\n\t"            \
562                                   "rorl $16, %0\n\t"            \
563                                   "rorw $8, %w0"                \
564                                   : "=r" (__v)                  \
565                                   : "0" ((guint32) (val))       \
566                                   : "cc");                      \
567         __v; }))
568
569 #  else /* 486 and higher has bswap */
570 #     define GUINT32_SWAP_LE_BE_X86(val) \
571         (__extension__                                          \
572          ({ register guint32 __v;                               \
573             if (__builtin_constant_p (val))                     \
574               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
575           else                                                  \
576             __asm__ __volatile__ ("bswap %0"                    \
577                                   : "=r" (__v)                  \
578                                   : "0" ((guint32) (val)));     \
579         __v; }))
580 #  endif /* processor specific 32-bit stuff */
581
582 #  define GUINT32_SWAP_LE_BE(val) \
583      ((guint32) GUINT32_SWAP_LE_BE_X86 ((guint32) (val)))
584
585 #else /* !__i386__ */
586 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
587 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
588 #endif /* __i386__ */
589
590 #ifdef G_HAVE_GINT64
591 #define GUINT64_SWAP_LE_BE(val)         ((guint64) ( \
592     (((guint64) (val) & (guint64) 0x00000000000000ffU) << 56) | \
593     (((guint64) (val) & (guint64) 0x000000000000ff00U) << 40) | \
594     (((guint64) (val) & (guint64) 0x0000000000ff0000U) << 24) | \
595     (((guint64) (val) & (guint64) 0x00000000ff000000U) <<  8) | \
596     (((guint64) (val) & (guint64) 0x000000ff00000000U) >>  8) | \
597     (((guint64) (val) & (guint64) 0x0000ff0000000000U) >> 24) | \
598     (((guint64) (val) & (guint64) 0x00ff000000000000U) >> 40) | \
599     (((guint64) (val) & (guint64) 0xff00000000000000U) >> 56)))
600 #endif
601
602 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
603 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
604 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
605     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
606     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
607 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
608     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
609     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
610
611 /* The TO_?E stuff is defined in glibconfig.h. The transformation is symmetric,
612    so the FROM just maps to the TO.
613  */
614 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
615 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
616 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
617 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
618 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
619 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
620 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
621 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
622
623 #ifdef G_HAVE_GINT64
624 #define GINT64_FROM_LE(val)     (GINT32_TO_LE (val))
625 #define GUINT64_FROM_LE(val)    (GUINT32_TO_LE (val))
626 #define GINT64_FROM_BE(val)     (GINT32_TO_BE (val))
627 #define GUINT64_FROM_BE(val)    (GUINT32_TO_BE (val))
628 #endif
629
630 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
631 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
632 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
633 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
634
635 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
636 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
637 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
638 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
639
640 /* Portable versions of host-network order stuff
641  */
642 #define g_ntohl(val) (GUINT32_FROM_BE (val))
643 #define g_ntohs(val) (GUINT16_FROM_BE (val))
644 #define g_htonl(val) (GUINT32_TO_BE (val))
645 #define g_htons(val) (GUINT16_TO_BE (val))
646
647
648 /* Glib version.
649  * we prefix variable declarations so they can
650  * properly get exported in windows dlls.
651  */
652 #ifdef NATIVE_WIN32
653 #  ifdef GLIB_COMPILATION
654 #    define GUTILS_C_VAR __declspec(dllexport)
655 #  else /* !GLIB_COMPILATION */
656 #    define GUTILS_C_VAR __declspec(dllimport)
657 #  endif /* !GLIB_COMPILATION */
658 #else /* !NATIVE_WIN32 */
659 #  define GUTILS_C_VAR extern
660 #endif /* !NATIVE_WIN32 */
661
662 GUTILS_C_VAR const guint glib_major_version;
663 GUTILS_C_VAR const guint glib_minor_version;
664 GUTILS_C_VAR const guint glib_micro_version;
665 GUTILS_C_VAR const guint glib_interface_age;
666 GUTILS_C_VAR const guint glib_binary_age;
667
668 /* Forward declarations of glib types.
669  */
670
671 typedef struct _GArray          GArray;
672 typedef struct _GByteArray      GByteArray;
673 typedef struct _GCache          GCache;
674 typedef struct _GCompletion     GCompletion;
675 typedef struct _GData           GData;
676 typedef struct _GDebugKey       GDebugKey;
677 typedef struct _GHashTable      GHashTable;
678 typedef struct _GHook           GHook;
679 typedef struct _GHookList       GHookList;
680 typedef struct _GList           GList;
681 typedef struct _GListAllocator  GListAllocator;
682 typedef struct _GMemChunk       GMemChunk;
683 typedef struct _GNode           GNode;
684 typedef struct _GPtrArray       GPtrArray;
685 typedef struct _GRelation       GRelation;
686 typedef struct _GScanner        GScanner;
687 typedef struct _GScannerConfig  GScannerConfig;
688 typedef struct _GSList          GSList;
689 typedef struct _GString         GString;
690 typedef struct _GStringChunk    GStringChunk;
691 typedef struct _GTimer          GTimer;
692 typedef struct _GTree           GTree;
693 typedef struct _GTuples         GTuples;
694 typedef union  _GTokenValue     GTokenValue;
695 typedef struct _GIOChannel      GIOChannel;
696
697
698 typedef enum
699 {
700   G_TRAVERSE_LEAFS      = 1 << 0,
701   G_TRAVERSE_NON_LEAFS  = 1 << 1,
702   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
703   G_TRAVERSE_MASK       = 0x03
704 } GTraverseFlags;
705
706 typedef enum
707 {
708   G_IN_ORDER,
709   G_PRE_ORDER,
710   G_POST_ORDER,
711   G_LEVEL_ORDER
712 } GTraverseType;
713
714 /* Log level shift offset for user defined
715  * log levels (0-7 are used by GLib).
716  */
717 #define G_LOG_LEVEL_USER_SHIFT  (8)
718
719 /* Glib log levels and flags.
720  */
721 typedef enum
722 {
723   /* log flags */
724   G_LOG_FLAG_RECURSION          = 1 << 0,
725   G_LOG_FLAG_FATAL              = 1 << 1,
726   
727   /* GLib log levels */
728   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
729   G_LOG_LEVEL_CRITICAL          = 1 << 3,
730   G_LOG_LEVEL_WARNING           = 1 << 4,
731   G_LOG_LEVEL_MESSAGE           = 1 << 5,
732   G_LOG_LEVEL_INFO              = 1 << 6,
733   G_LOG_LEVEL_DEBUG             = 1 << 7,
734   
735   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
736 } GLogLevelFlags;
737
738 /* GLib log levels that are considered fatal by default */
739 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
740
741
742 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
743 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
744 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
745 typedef gint            (*GCompareFunc)         (gconstpointer  a,
746                                                  gconstpointer  b);
747 typedef gchar*          (*GCompletionFunc)      (gpointer);
748 typedef void            (*GDestroyNotify)       (gpointer       data);
749 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
750                                                  gpointer       data,
751                                                  gpointer       user_data);
752 typedef void            (*GFunc)                (gpointer       data,
753                                                  gpointer       user_data);
754 typedef guint           (*GHashFunc)            (gconstpointer  key);
755 typedef void            (*GHFunc)               (gpointer       key,
756                                                  gpointer       value,
757                                                  gpointer       user_data);
758 typedef gboolean        (*GHRFunc)              (gpointer       key,
759                                                  gpointer       value,
760                                                  gpointer       user_data);
761 typedef gint            (*GHookCompareFunc)     (GHook          *new_hook,
762                                                  GHook          *sibling);
763 typedef gboolean        (*GHookFindFunc)        (GHook          *hook,
764                                                  gpointer        data);
765 typedef void            (*GHookMarshaller)      (GHook          *hook,
766                                                  gpointer        data);
767 typedef void            (*GHookFunc)            (gpointer        data);
768 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
769 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
770                                                  GHook          *hook);
771 typedef void            (*GLogFunc)             (const gchar   *log_domain,
772                                                  GLogLevelFlags log_level,
773                                                  const gchar   *message,
774                                                  gpointer       user_data);
775 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
776                                                  gpointer       data);
777 typedef void            (*GNodeForeachFunc)     (GNode         *node,
778                                                  gpointer       data);
779 typedef gint            (*GSearchFunc)          (gpointer       key,
780                                                  gpointer       data);
781 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
782                                                  gchar         *message,
783                                                  gint           error);
784 typedef gint            (*GTraverseFunc)        (gpointer       key,
785                                                  gpointer       value,
786                                                  gpointer       data);
787 typedef void            (*GVoidFunc)            (void);
788
789
790 struct _GList
791 {
792   gpointer data;
793   GList *next;
794   GList *prev;
795 };
796
797 struct _GSList
798 {
799   gpointer data;
800   GSList *next;
801 };
802
803 struct _GString
804 {
805   gchar *str;
806   gint len;
807 };
808
809 struct _GArray
810 {
811   gchar *data;
812   guint len;
813 };
814
815 struct _GByteArray
816 {
817   guint8 *data;
818   guint   len;
819 };
820
821 struct _GPtrArray
822 {
823   gpointer *pdata;
824   guint     len;
825 };
826
827 struct _GTuples
828 {
829   guint len;
830 };
831
832 struct _GDebugKey
833 {
834   gchar *key;
835   guint  value;
836 };
837
838
839 /* Doubly linked lists
840  */
841 GList* g_list_alloc             (void);
842 void   g_list_free              (GList          *list);
843 void   g_list_free_1            (GList          *list);
844 GList* g_list_append            (GList          *list,
845                                  gpointer        data);
846 GList* g_list_prepend           (GList          *list,
847                                  gpointer        data);
848 GList* g_list_insert            (GList          *list,
849                                  gpointer        data,
850                                  gint            position);
851 GList* g_list_insert_sorted     (GList          *list,
852                                  gpointer        data,
853                                  GCompareFunc    func);
854 GList* g_list_concat            (GList          *list1,
855                                  GList          *list2);
856 GList* g_list_remove            (GList          *list,
857                                  gpointer        data);
858 GList* g_list_remove_link       (GList          *list,
859                                  GList          *llink);
860 GList* g_list_reverse           (GList          *list);
861 GList* g_list_nth               (GList          *list,
862                                  guint           n);
863 GList* g_list_find              (GList          *list,
864                                  gpointer        data);
865 GList* g_list_find_custom       (GList          *list,
866                                  gpointer        data,
867                                  GCompareFunc    func);
868 gint   g_list_position          (GList          *list,
869                                  GList          *llink);
870 gint   g_list_index             (GList          *list,
871                                  gpointer        data);
872 GList* g_list_last              (GList          *list);
873 GList* g_list_first             (GList          *list);
874 guint  g_list_length            (GList          *list);
875 void   g_list_foreach           (GList          *list,
876                                  GFunc           func,
877                                  gpointer        user_data);
878 gpointer g_list_nth_data        (GList          *list,
879                                  guint           n);
880 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
881 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
882
883
884 /* Singly linked lists
885  */
886 GSList* g_slist_alloc           (void);
887 void    g_slist_free            (GSList         *list);
888 void    g_slist_free_1          (GSList         *list);
889 GSList* g_slist_append          (GSList         *list,
890                                  gpointer        data);
891 GSList* g_slist_prepend         (GSList         *list,
892                                  gpointer        data);
893 GSList* g_slist_insert          (GSList         *list,
894                                  gpointer        data,
895                                  gint            position);
896 GSList* g_slist_insert_sorted   (GSList         *list,
897                                  gpointer        data,
898                                  GCompareFunc    func);
899 GSList* g_slist_concat          (GSList         *list1,
900                                  GSList         *list2);
901 GSList* g_slist_remove          (GSList         *list,
902                                  gpointer        data);
903 GSList* g_slist_remove_link     (GSList         *list,
904                                  GSList         *llink);
905 GSList* g_slist_reverse         (GSList         *list);
906 GSList* g_slist_nth             (GSList         *list,
907                                  guint           n);
908 GSList* g_slist_find            (GSList         *list,
909                                  gpointer        data);
910 GSList* g_slist_find_custom     (GSList         *list,
911                                  gpointer        data,
912                                  GCompareFunc    func);
913 gint    g_slist_position        (GSList         *list,
914                                  GSList         *llink);
915 gint    g_slist_index           (GSList         *list,
916                                  gpointer        data);
917 GSList* g_slist_last            (GSList         *list);
918 guint   g_slist_length          (GSList         *list);
919 void    g_slist_foreach         (GSList         *list,
920                                  GFunc           func,
921                                  gpointer        user_data);
922 gpointer g_slist_nth_data       (GSList         *list,
923                                  guint           n);
924 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
925
926
927 /* List Allocators
928  */
929 GListAllocator* g_list_allocator_new  (void);
930 void            g_list_allocator_free (GListAllocator* allocator);
931 GListAllocator* g_slist_set_allocator (GListAllocator* allocator);
932 GListAllocator* g_list_set_allocator  (GListAllocator* allocator);
933
934
935 /* Hash tables
936  */
937 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
938                                          GCompareFunc    key_compare_func);
939 void        g_hash_table_destroy        (GHashTable     *hash_table);
940 void        g_hash_table_insert         (GHashTable     *hash_table,
941                                          gpointer        key,
942                                          gpointer        value);
943 void        g_hash_table_remove         (GHashTable     *hash_table,
944                                          gconstpointer   key);
945 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
946                                          gconstpointer   key);
947 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
948                                          gconstpointer   lookup_key,
949                                          gpointer       *orig_key,
950                                          gpointer       *value);
951 void        g_hash_table_freeze         (GHashTable     *hash_table);
952 void        g_hash_table_thaw           (GHashTable     *hash_table);
953 void        g_hash_table_foreach        (GHashTable     *hash_table,
954                                          GHFunc          func,
955                                          gpointer        user_data);
956 gint        g_hash_table_foreach_remove (GHashTable     *hash_table,
957                                          GHRFunc         func,
958                                          gpointer        user_data);
959 gint        g_hash_table_size           (GHashTable     *hash_table);
960
961
962 /* Caches
963  */
964 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
965                                 GCacheDestroyFunc  value_destroy_func,
966                                 GCacheDupFunc      key_dup_func,
967                                 GCacheDestroyFunc  key_destroy_func,
968                                 GHashFunc          hash_key_func,
969                                 GHashFunc          hash_value_func,
970                                 GCompareFunc       key_compare_func);
971 void     g_cache_destroy       (GCache            *cache);
972 gpointer g_cache_insert        (GCache            *cache,
973                                 gpointer           key);
974 void     g_cache_remove        (GCache            *cache,
975                                 gpointer           value);
976 void     g_cache_key_foreach   (GCache            *cache,
977                                 GHFunc             func,
978                                 gpointer           user_data);
979 void     g_cache_value_foreach (GCache            *cache,
980                                 GHFunc             func,
981                                 gpointer           user_data);
982
983
984 /* Balanced binary trees
985  */
986 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
987 void     g_tree_destroy  (GTree         *tree);
988 void     g_tree_insert   (GTree         *tree,
989                           gpointer       key,
990                           gpointer       value);
991 void     g_tree_remove   (GTree         *tree,
992                           gpointer       key);
993 gpointer g_tree_lookup   (GTree         *tree,
994                           gpointer       key);
995 void     g_tree_traverse (GTree         *tree,
996                           GTraverseFunc  traverse_func,
997                           GTraverseType  traverse_type,
998                           gpointer       data);
999 gpointer g_tree_search   (GTree         *tree,
1000                           GSearchFunc    search_func,
1001                           gpointer       data);
1002 gint     g_tree_height   (GTree         *tree);
1003 gint     g_tree_nnodes   (GTree         *tree);
1004
1005
1006
1007 /* N-way tree implementation
1008  */
1009 struct _GNode
1010 {
1011   gpointer data;
1012   GNode   *next;
1013   GNode   *prev;
1014   GNode   *parent;
1015   GNode   *children;
1016 };
1017
1018 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1019                                  ((GNode*) (node))->prev == NULL && \
1020                                  ((GNode*) (node))->next == NULL)
1021 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1022
1023 GNode*   g_node_new             (gpointer          data);
1024 void     g_node_destroy         (GNode            *root);
1025 void     g_node_unlink          (GNode            *node);
1026 GNode*   g_node_insert          (GNode            *parent,
1027                                  gint              position,
1028                                  GNode            *node);
1029 GNode*   g_node_insert_before   (GNode            *parent,
1030                                  GNode            *sibling,
1031                                  GNode            *node);
1032 GNode*   g_node_prepend         (GNode            *parent,
1033                                  GNode            *node);
1034 guint    g_node_n_nodes         (GNode            *root,
1035                                  GTraverseFlags    flags);
1036 GNode*   g_node_get_root        (GNode            *node);
1037 gboolean g_node_is_ancestor     (GNode            *node,
1038                                  GNode            *descendant);
1039 guint    g_node_depth           (GNode            *node);
1040 GNode*   g_node_find            (GNode            *root,
1041                                  GTraverseType     order,
1042                                  GTraverseFlags    flags,
1043                                  gpointer          data);
1044
1045 /* convenience macros */
1046 #define g_node_append(parent, node)                             \
1047      g_node_insert_before ((parent), NULL, (node))
1048 #define g_node_insert_data(parent, position, data)              \
1049      g_node_insert ((parent), (position), g_node_new (data))
1050 #define g_node_insert_data_before(parent, sibling, data)        \
1051      g_node_insert_before ((parent), (sibling), g_node_new (data))
1052 #define g_node_prepend_data(parent, data)                       \
1053      g_node_prepend ((parent), g_node_new (data))
1054 #define g_node_append_data(parent, data)                        \
1055      g_node_insert_before ((parent), NULL, g_node_new (data))
1056
1057 /* traversal function, assumes that `node' is root
1058  * (only traverses `node' and its subtree).
1059  * this function is just a high level interface to
1060  * low level traversal functions, optimized for speed.
1061  */
1062 void     g_node_traverse        (GNode            *root,
1063                                  GTraverseType     order,
1064                                  GTraverseFlags    flags,
1065                                  gint              max_depth,
1066                                  GNodeTraverseFunc func,
1067                                  gpointer          data);
1068
1069 /* return the maximum tree height starting with `node', this is an expensive
1070  * operation, since we need to visit all nodes. this could be shortened by
1071  * adding `guint height' to struct _GNode, but then again, this is not very
1072  * often needed, and would make g_node_insert() more time consuming.
1073  */
1074 guint    g_node_max_height       (GNode *root);
1075
1076 void     g_node_children_foreach (GNode           *node,
1077                                   GTraverseFlags   flags,
1078                                   GNodeForeachFunc func,
1079                                   gpointer         data);
1080 void     g_node_reverse_children (GNode           *node);
1081 guint    g_node_n_children       (GNode           *node);
1082 GNode*   g_node_nth_child        (GNode           *node,
1083                                   guint            n);
1084 GNode*   g_node_last_child       (GNode           *node);
1085 GNode*   g_node_find_child       (GNode           *node,
1086                                   GTraverseFlags   flags,
1087                                   gpointer         data);
1088 gint     g_node_child_position   (GNode           *node,
1089                                   GNode           *child);
1090 gint     g_node_child_index      (GNode           *node,
1091                                   gpointer         data);
1092
1093 GNode*   g_node_first_sibling    (GNode           *node);
1094 GNode*   g_node_last_sibling     (GNode           *node);
1095
1096 #define  g_node_prev_sibling(node)      ((node) ? \
1097                                          ((GNode*) (node))->prev : NULL)
1098 #define  g_node_next_sibling(node)      ((node) ? \
1099                                          ((GNode*) (node))->next : NULL)
1100 #define  g_node_first_child(node)       ((node) ? \
1101                                          ((GNode*) (node))->children : NULL)
1102
1103
1104 /* Callback maintenance functions
1105  */
1106 #define G_HOOK_FLAG_USER_SHIFT  (4)
1107 typedef enum
1108 {
1109   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1110   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1111   G_HOOK_FLAG_MASK      = 0x0f
1112 } GHookFlagMask;
1113
1114 struct _GHookList
1115 {
1116   guint          seq_id;
1117   guint          hook_size;
1118   guint          is_setup : 1;
1119   GHook         *hooks;
1120   GMemChunk     *hook_memchunk;
1121   GHookFreeFunc  hook_free; /* virtual function */
1122 };
1123
1124 struct _GHook
1125 {
1126   gpointer       data;
1127   GHook         *next;
1128   GHook         *prev;
1129   guint          ref_count;
1130   guint          hook_id;
1131   guint          flags;
1132   gpointer       func;
1133   GDestroyNotify destroy;
1134 };
1135
1136 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1137                                           G_HOOK_FLAG_ACTIVE) != 0)
1138 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1139                                           G_HOOK_FLAG_IN_CALL) != 0)
1140 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1141                                          G_HOOK_ACTIVE (hook))
1142 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1143                                          ((GHook*) hook)->prev == NULL && \
1144                                          ((GHook*) hook)->hook_id == 0 && \
1145                                          ((GHook*) hook)->ref_count == 0)
1146
1147 void     g_hook_list_init               (GHookList              *hook_list,
1148                                          guint                   hook_size);
1149 void     g_hook_list_clear              (GHookList              *hook_list);
1150 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1151 void     g_hook_free                    (GHookList              *hook_list,
1152                                          GHook                  *hook);
1153 void     g_hook_ref                     (GHookList              *hook_list,
1154                                          GHook                  *hook);
1155 void     g_hook_unref                   (GHookList              *hook_list,
1156                                          GHook                  *hook);
1157 gboolean g_hook_destroy                 (GHookList              *hook_list,
1158                                          guint                   hook_id);
1159 void     g_hook_destroy_link            (GHookList              *hook_list,
1160                                          GHook                  *hook);
1161 void     g_hook_prepend                 (GHookList              *hook_list,
1162                                          GHook                  *hook);
1163 void     g_hook_insert_before           (GHookList              *hook_list,
1164                                          GHook                  *sibling,
1165                                          GHook                  *hook);
1166 void     g_hook_insert_sorted           (GHookList              *hook_list,
1167                                          GHook                  *hook,
1168                                          GHookCompareFunc        func);
1169 GHook*   g_hook_get                     (GHookList              *hook_list,
1170                                          guint                   hook_id);
1171 GHook*   g_hook_find                    (GHookList              *hook_list,
1172                                          gboolean                need_valids,
1173                                          GHookFindFunc           func,
1174                                          gpointer                data);
1175 GHook*   g_hook_find_data               (GHookList              *hook_list,
1176                                          gboolean                need_valids,
1177                                          gpointer                data);
1178 GHook*   g_hook_find_func               (GHookList              *hook_list,
1179                                          gboolean                need_valids,
1180                                          gpointer                func);
1181 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1182                                          gboolean                need_valids,
1183                                          gpointer                func,
1184                                          gpointer                data);
1185 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1186                                          gboolean                may_be_in_call);
1187 GHook*   g_hook_next_valid              (GHook                  *hook,
1188                                          gboolean                may_be_in_call);
1189
1190 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1191 gint     g_hook_compare_ids             (GHook                  *new_hook,
1192                                          GHook                  *sibling);
1193
1194 /* convenience macros */
1195 #define  g_hook_append( hook_list, hook )  \
1196      g_hook_insert_before ((hook_list), NULL, (hook))
1197
1198 /* invoke all valid hooks with the (*GHookFunc) signature.
1199  */
1200 void     g_hook_list_invoke             (GHookList              *hook_list,
1201                                          gboolean                may_recurse);
1202 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1203  * and destroy the hook if FALSE is returned.
1204  */
1205 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1206                                          gboolean                may_recurse);
1207 /* invoke a marshaller on all valid hooks.
1208  */
1209 void     g_hook_list_marshal            (GHookList              *hook_list,
1210                                          gboolean                may_recurse,
1211                                          GHookMarshaller         marshaller,
1212                                          gpointer                data);
1213
1214
1215 /* Fatal error handlers.
1216  * g_on_error_query() will prompt the user to either
1217  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1218  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1219  * process and shows a stack trace.
1220  * These function may cause different actions on non-unix platforms.
1221  * The prg_name arg is required by gdb to find the executable, if it is
1222  * passed as NULL, g_on_error_query() will try g_get_prgname().
1223  */
1224 void g_on_error_query (const gchar *prg_name);
1225 void g_on_error_stack_trace (const gchar *prg_name);
1226
1227
1228 /* Logging mechanism
1229  */
1230 extern          const gchar             *g_log_domain_glib;
1231 guint           g_log_set_handler       (const gchar    *log_domain,
1232                                          GLogLevelFlags  log_levels,
1233                                          GLogFunc        log_func,
1234                                          gpointer        user_data);
1235 void            g_log_remove_handler    (const gchar    *log_domain,
1236                                          guint           handler_id);
1237 void            g_log_default_handler   (const gchar    *log_domain,
1238                                          GLogLevelFlags  log_level,
1239                                          const gchar    *message,
1240                                          gpointer        unused_data);
1241 void            g_log                   (const gchar    *log_domain,
1242                                          GLogLevelFlags  log_level,
1243                                          const gchar    *format,
1244                                          ...) G_GNUC_PRINTF (3, 4);
1245 void            g_logv                  (const gchar    *log_domain,
1246                                          GLogLevelFlags  log_level,
1247                                          const gchar    *format,
1248                                          va_list         args);
1249 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1250                                          GLogLevelFlags  fatal_mask);
1251 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1252 #ifndef G_LOG_DOMAIN
1253 #define G_LOG_DOMAIN    (NULL)
1254 #endif  /* G_LOG_DOMAIN */
1255 #ifdef  __GNUC__
1256 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1257                                                G_LOG_LEVEL_ERROR, \
1258                                                format, ##args)
1259 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1260                                                G_LOG_LEVEL_MESSAGE, \
1261                                                format, ##args)
1262 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1263                                                G_LOG_LEVEL_WARNING, \
1264                                                format, ##args)
1265 #else   /* !__GNUC__ */
1266 static inline void
1267 g_error (const gchar *format,
1268          ...)
1269 {
1270   va_list args;
1271   va_start (args, format);
1272   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1273   va_end (args);
1274 }
1275 static inline void
1276 g_message (const gchar *format,
1277            ...)
1278 {
1279   va_list args;
1280   va_start (args, format);
1281   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1282   va_end (args);
1283 }
1284 static inline void
1285 g_warning (const gchar *format,
1286            ...)
1287 {
1288   va_list args;
1289   va_start (args, format);
1290   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1291   va_end (args);
1292 }
1293 #endif  /* !__GNUC__ */
1294
1295 typedef void    (*GPrintFunc)           (const gchar    *string);
1296 void            g_print                 (const gchar    *format,
1297                                          ...) G_GNUC_PRINTF (1, 2);
1298 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1299 void            g_printerr              (const gchar    *format,
1300                                          ...) G_GNUC_PRINTF (1, 2);
1301 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1302
1303 /* deprecated compatibility functions, use g_log_set_handler() instead */
1304 typedef void            (*GErrorFunc)           (const gchar *str);
1305 typedef void            (*GWarningFunc)         (const gchar *str);
1306 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1307 GWarningFunc g_set_warning_handler (GWarningFunc func);
1308 GPrintFunc   g_set_message_handler (GPrintFunc func);
1309
1310
1311 /* Memory allocation and debugging
1312  */
1313 #ifdef USE_DMALLOC
1314
1315 #define g_malloc(size)       ((gpointer) MALLOC (size))
1316 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1317 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1318 #define g_free(mem)          FREE (mem)
1319
1320 #else /* !USE_DMALLOC */
1321
1322 gpointer g_malloc      (gulong    size);
1323 gpointer g_malloc0     (gulong    size);
1324 gpointer g_realloc     (gpointer  mem,
1325                         gulong    size);
1326 void     g_free        (gpointer  mem);
1327
1328 #endif /* !USE_DMALLOC */
1329
1330 void     g_mem_profile (void);
1331 void     g_mem_check   (gpointer  mem);
1332
1333
1334 /* "g_mem_chunk_new" creates a new memory chunk.
1335  * Memory chunks are used to allocate pieces of memory which are
1336  *  always the same size. Lists are a good example of such a data type.
1337  * The memory chunk allocates and frees blocks of memory as needed.
1338  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1339  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1340  *  fault...somewhere).
1341  *
1342  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1343  *  want to know what's going on inside do you?)
1344  */
1345
1346 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1347  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1348  *  atom. (They are also useful for lists which use MemChunk to allocate
1349  *  memory but are also part of the MemChunk implementation).
1350  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1351  */
1352
1353 #define G_ALLOC_ONLY      1
1354 #define G_ALLOC_AND_FREE  2
1355
1356 GMemChunk* g_mem_chunk_new     (gchar     *name,
1357                                 gint       atom_size,
1358                                 gulong     area_size,
1359                                 gint       type);
1360 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1361 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1362 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1363 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1364                                 gpointer   mem);
1365 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1366 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1367 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1368 void       g_mem_chunk_info    (void);
1369
1370 /* Ah yes...we have a "g_blow_chunks" function.
1371  * "g_blow_chunks" simply compresses all the chunks. This operation
1372  *  consists of freeing every memory area that should be freed (but
1373  *  which we haven't gotten around to doing yet). And, no,
1374  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1375  *  much better name than "g_mem_chunk_clean_all" or something
1376  *  similar.
1377  */
1378 void g_blow_chunks (void);
1379
1380
1381 /* Timer
1382  */
1383 GTimer* g_timer_new     (void);
1384 void    g_timer_destroy (GTimer  *timer);
1385 void    g_timer_start   (GTimer  *timer);
1386 void    g_timer_stop    (GTimer  *timer);
1387 void    g_timer_reset   (GTimer  *timer);
1388 gdouble g_timer_elapsed (GTimer  *timer,
1389                          gulong  *microseconds);
1390
1391
1392 /* String utility functions that modify a string argument or
1393  * return a constant string that must not be freed.
1394  */
1395 #define  G_STR_DELIMITERS       "_-|> <."
1396 gchar*   g_strdelimit           (gchar       *string,
1397                                  const gchar *delimiters,
1398                                  gchar        new_delimiter);
1399 gdouble  g_strtod               (const gchar *nptr,
1400                                  gchar      **endptr);
1401 gchar*   g_strerror             (gint         errnum);
1402 gchar*   g_strsignal            (gint         signum);
1403 gint     g_strcasecmp           (const gchar *s1,
1404                                  const gchar *s2);
1405 void     g_strdown              (gchar       *string);
1406 void     g_strup                (gchar       *string);
1407 void     g_strreverse           (gchar       *string);
1408 /* removes leading spaces */
1409 gchar*   g_strchug              (gchar        *string);
1410 /* removes trailing spaces */
1411 gchar*  g_strchomp              (gchar        *string);
1412 /* removes leading & trailing spaces */
1413 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1414
1415 /* String utility functions that return a newly allocated string which
1416  * ought to be freed from the caller at some point.
1417  */
1418 gchar*   g_strdup               (const gchar *str);
1419 gchar*   g_strdup_printf        (const gchar *format,
1420                                  ...) G_GNUC_PRINTF (1, 2);
1421 gchar*   g_strdup_vprintf       (const gchar *format,
1422                                  va_list      args);
1423 gchar*   g_strndup              (const gchar *str,
1424                                  guint        n);
1425 gchar*   g_strnfill             (guint        length,
1426                                  gchar        fill_char);
1427 gchar*   g_strconcat            (const gchar *string1,
1428                                  ...); /* NULL terminated */
1429 gchar*   g_strjoin              (const gchar  *separator,
1430                                  ...); /* NULL terminated */
1431 gchar*   g_strescape            (gchar        *string);
1432 gpointer g_memdup               (gconstpointer mem,
1433                                  guint         byte_size);
1434
1435 /* NULL terminated string arrays.
1436  * g_strsplit() splits up string into max_tokens tokens at delim and
1437  * returns a newly allocated string array.
1438  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1439  * optional separator, the returned string is newly allocated.
1440  * g_strfreev() frees the array itself and all of its strings.
1441  */
1442 gchar**  g_strsplit             (const gchar  *string,
1443                                  const gchar  *delimiter,
1444                                  gint          max_tokens);
1445 gchar*   g_strjoinv             (const gchar  *separator,
1446                                  gchar       **str_array);
1447 void     g_strfreev             (gchar       **str_array);
1448
1449
1450
1451 /* calculate a string size, guarranteed to fit format + args.
1452  */
1453 guint   g_printf_string_upper_bound (const gchar* format,
1454                                      va_list      args);
1455
1456
1457 /* Retrive static string info
1458  */
1459 gchar*  g_get_user_name         (void);
1460 gchar*  g_get_real_name         (void);
1461 gchar*  g_get_home_dir          (void);
1462 gchar*  g_get_tmp_dir           (void);
1463 gchar*  g_get_prgname           (void);
1464 void    g_set_prgname           (const gchar *prgname);
1465
1466
1467 /* Miscellaneous utility functions
1468  */
1469 guint   g_parse_debug_string    (const gchar *string,
1470                                  GDebugKey   *keys,
1471                                  guint        nkeys);
1472 gint    g_snprintf              (gchar       *string,
1473                                  gulong       n,
1474                                  gchar const *format,
1475                                  ...) G_GNUC_PRINTF (3, 4);
1476 gint    g_vsnprintf             (gchar       *string,
1477                                  gulong       n,
1478                                  gchar const *format,
1479                                  va_list      args);
1480 gchar*  g_basename              (const gchar *file_name);
1481 /* Check if a file name is an absolute path */
1482 gboolean g_path_is_absolute     (const gchar *file_name);
1483 /* In case of absolute paths, skip the root part */
1484 gchar*  g_path_skip_root        (gchar       *file_name);
1485
1486 /* strings are newly allocated with g_malloc() */
1487 gchar*  g_dirname               (const gchar *file_name);
1488 gchar*  g_get_current_dir       (void);
1489 gchar*  g_getenv                (const gchar *variable);
1490
1491
1492 /* we use a GLib function as a replacement for ATEXIT, so
1493  * the programmer is not required to check the return value
1494  * (if there is any in the implementation) and doesn't encounter
1495  * missing include files.
1496  */
1497 void    g_atexit                (GVoidFunc    func);
1498
1499
1500 /* Bit tests
1501  */
1502 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1503                                        gint    nth_bit);
1504 #ifdef  G_CAN_INLINE
1505 G_INLINE_FUNC gint
1506 g_bit_nth_lsf (guint32 mask,
1507                gint    nth_bit)
1508 {
1509   do
1510     {
1511       nth_bit++;
1512       if (mask & (1 << (guint) nth_bit))
1513         return nth_bit;
1514     }
1515   while (nth_bit < 32);
1516   return -1;
1517 }
1518 #endif  /* G_CAN_INLINE */
1519
1520 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1521                                        gint    nth_bit);
1522 #ifdef G_CAN_INLINE
1523 G_INLINE_FUNC gint
1524 g_bit_nth_msf (guint32 mask,
1525                gint    nth_bit)
1526 {
1527   if (nth_bit < 0)
1528     nth_bit = 33;
1529   do
1530     {
1531       nth_bit--;
1532       if (mask & (1 << (guint) nth_bit))
1533         return nth_bit;
1534     }
1535   while (nth_bit > 0);
1536   return -1;
1537 }
1538 #endif  /* G_CAN_INLINE */
1539
1540 G_INLINE_FUNC guint     g_bit_storage (guint number);
1541 #ifdef G_CAN_INLINE
1542 G_INLINE_FUNC guint
1543 g_bit_storage (guint number)
1544 {
1545   register guint n_bits = 0;
1546   
1547   do
1548     {
1549       n_bits++;
1550       number >>= 1;
1551     }
1552   while (number);
1553   return n_bits;
1554 }
1555 #endif  /* G_CAN_INLINE */
1556
1557 /* String Chunks
1558  */
1559 GStringChunk* g_string_chunk_new           (gint size);
1560 void          g_string_chunk_free          (GStringChunk *chunk);
1561 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1562                                             const gchar  *string);
1563 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1564                                             const gchar  *string);
1565
1566
1567 /* Strings
1568  */
1569 GString* g_string_new       (const gchar *init);
1570 GString* g_string_sized_new (guint        dfl_size);
1571 void     g_string_free      (GString     *string,
1572                              gint         free_segment);
1573 GString* g_string_assign    (GString     *lval,
1574                              const gchar *rval);
1575 GString* g_string_truncate  (GString     *string,
1576                              gint         len);
1577 GString* g_string_append    (GString     *string,
1578                              const gchar *val);
1579 GString* g_string_append_c  (GString     *string,
1580                              gchar        c);
1581 GString* g_string_prepend   (GString     *string,
1582                              const gchar *val);
1583 GString* g_string_prepend_c (GString     *string,
1584                              gchar        c);
1585 GString* g_string_insert    (GString     *string,
1586                              gint         pos,
1587                              const gchar *val);
1588 GString* g_string_insert_c  (GString     *string,
1589                              gint         pos,
1590                              gchar        c);
1591 GString* g_string_erase     (GString     *string,
1592                              gint         pos,
1593                              gint         len);
1594 GString* g_string_down      (GString     *string);
1595 GString* g_string_up        (GString     *string);
1596 void     g_string_sprintf   (GString     *string,
1597                              const gchar *format,
1598                              ...) G_GNUC_PRINTF (2, 3);
1599 void     g_string_sprintfa  (GString     *string,
1600                              const gchar *format,
1601                              ...) G_GNUC_PRINTF (2, 3);
1602
1603
1604 /* Resizable arrays
1605  */
1606
1607 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1608 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1609 #define g_array_index(a,t,i) (((t*)a->data)[i])
1610
1611 GArray* g_array_new          (gboolean      zero_terminated,
1612                               gboolean      clear,
1613                               guint         element_size);
1614 void    g_array_free         (GArray       *array,
1615                               gboolean      free_segment);
1616 GArray* g_array_append_vals  (GArray       *array,
1617                               gconstpointer data,
1618                               guint         len);
1619 GArray* g_array_prepend_vals (GArray       *array,
1620                               gconstpointer data,
1621                               guint         len);
1622 GArray* g_array_set_size     (GArray       *array,
1623                               guint         length);
1624
1625 /* Resizable pointer array.  This interface is much less complicated
1626  * than the above.  Add appends appends a pointer.  Remove fills any
1627  * cleared spot and shortens the array.
1628  */
1629 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1630 GPtrArray*  g_ptr_array_new                (void);
1631 void        g_ptr_array_free               (GPtrArray   *array,
1632                                             gboolean     free_seg);
1633 void        g_ptr_array_set_size           (GPtrArray   *array,
1634                                             gint         length);
1635 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1636                                             gint         index);
1637 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1638                                             gpointer     data);
1639 void        g_ptr_array_add                (GPtrArray   *array,
1640                                             gpointer     data);
1641
1642 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1643  * but type-safe.
1644  */
1645
1646 GByteArray* g_byte_array_new      (void);
1647 void        g_byte_array_free     (GByteArray   *array,
1648                                    gboolean      free_segment);
1649 GByteArray* g_byte_array_append   (GByteArray   *array,
1650                                    const guint8 *data,
1651                                    guint         len);
1652 GByteArray* g_byte_array_prepend  (GByteArray   *array,
1653                                    const guint8 *data,
1654                                    guint         len);
1655 GByteArray* g_byte_array_set_size (GByteArray   *array,
1656                                    guint         length);
1657
1658
1659 /* Hash Functions
1660  */
1661 gint  g_str_equal (gconstpointer   v,
1662                    gconstpointer   v2);
1663 guint g_str_hash  (gconstpointer   v);
1664
1665 gint  g_int_equal (gconstpointer   v,
1666                    gconstpointer   v2);
1667 guint g_int_hash  (gconstpointer   v);
1668
1669 /* This "hash" function will just return the key's adress as an
1670  * unsigned integer. Useful for hashing on plain adresses or
1671  * simple integer values.
1672  */
1673 guint g_direct_hash  (gconstpointer v);
1674 gint  g_direct_equal (gconstpointer v,
1675                       gconstpointer v2);
1676
1677
1678 /* Quarks (string<->id association)
1679  */
1680 GQuark    g_quark_try_string            (const gchar    *string);
1681 GQuark    g_quark_from_static_string    (const gchar    *string);
1682 GQuark    g_quark_from_string           (const gchar    *string);
1683 gchar*    g_quark_to_string             (GQuark          quark);
1684
1685
1686 /* Keyed Data List
1687  */
1688 void      g_datalist_init                (GData          **datalist);
1689 void      g_datalist_clear               (GData          **datalist);
1690 gpointer  g_datalist_id_get_data         (GData          **datalist,
1691                                           GQuark           key_id);
1692 void      g_datalist_id_set_data_full    (GData          **datalist,
1693                                           GQuark           key_id,
1694                                           gpointer         data,
1695                                           GDestroyNotify   destroy_func);
1696 void      g_datalist_id_remove_no_notify (GData          **datalist,
1697                                           GQuark           key_id);
1698 void      g_datalist_foreach             (GData          **datalist,
1699                                           GDataForeachFunc func,
1700                                           gpointer         user_data);
1701 #define   g_datalist_id_set_data(dl, q, d)      \
1702      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1703 #define   g_datalist_id_remove_data(dl, q)      \
1704      g_datalist_id_set_data ((dl), (q), NULL)
1705 #define   g_datalist_get_data(dl, k)            \
1706      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1707 #define   g_datalist_set_data_full(dl, k, d, f) \
1708      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1709 #define   g_datalist_remove_no_notify(dl, k)    \
1710      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1711 #define   g_datalist_set_data(dl, k, d)         \
1712      g_datalist_set_data_full ((dl), (k), (d), NULL)
1713 #define   g_datalist_remove_data(dl, k)         \
1714      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1715
1716
1717 /* Location Associated Keyed Data
1718  */
1719 void      g_dataset_destroy             (gconstpointer    dataset_location);
1720 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
1721                                          GQuark           key_id);
1722 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
1723                                          GQuark           key_id,
1724                                          gpointer         data,
1725                                          GDestroyNotify   destroy_func);
1726 void      g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
1727                                          GQuark           key_id);
1728 void      g_dataset_foreach             (gconstpointer    dataset_location,
1729                                          GDataForeachFunc func,
1730                                          gpointer         user_data);
1731 #define   g_dataset_id_set_data(l, k, d)        \
1732      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1733 #define   g_dataset_id_remove_data(l, k)        \
1734      g_dataset_id_set_data ((l), (k), NULL)
1735 #define   g_dataset_get_data(l, k)              \
1736      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1737 #define   g_dataset_set_data_full(l, k, d, f)   \
1738      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1739 #define   g_dataset_remove_no_notify(l, k)      \
1740      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1741 #define   g_dataset_set_data(l, k, d)           \
1742      g_dataset_set_data_full ((l), (k), (d), NULL)
1743 #define   g_dataset_remove_data(l, k)           \
1744      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1745
1746
1747 /* GScanner: Flexible lexical scanner for general purpose.
1748  */
1749
1750 /* Character sets */
1751 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1752 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1753 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1754                         "\307\310\311\312\313\314\315\316\317\320"\
1755                         "\321\322\323\324\325\326"\
1756                         "\330\331\332\333\334\335\336"
1757 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1758                         "\347\350\351\352\353\354\355\356\357\360"\
1759                         "\361\362\363\364\365\366"\
1760                         "\370\371\372\373\374\375\376\377"
1761
1762 /* Error types */
1763 typedef enum
1764 {
1765   G_ERR_UNKNOWN,
1766   G_ERR_UNEXP_EOF,
1767   G_ERR_UNEXP_EOF_IN_STRING,
1768   G_ERR_UNEXP_EOF_IN_COMMENT,
1769   G_ERR_NON_DIGIT_IN_CONST,
1770   G_ERR_DIGIT_RADIX,
1771   G_ERR_FLOAT_RADIX,
1772   G_ERR_FLOAT_MALFORMED
1773 } GErrorType;
1774
1775 /* Token types */
1776 typedef enum
1777 {
1778   G_TOKEN_EOF                   =   0,
1779   
1780   G_TOKEN_LEFT_PAREN            = '(',
1781   G_TOKEN_RIGHT_PAREN           = ')',
1782   G_TOKEN_LEFT_CURLY            = '{',
1783   G_TOKEN_RIGHT_CURLY           = '}',
1784   G_TOKEN_LEFT_BRACE            = '[',
1785   G_TOKEN_RIGHT_BRACE           = ']',
1786   G_TOKEN_EQUAL_SIGN            = '=',
1787   G_TOKEN_COMMA                 = ',',
1788   
1789   G_TOKEN_NONE                  = 256,
1790   
1791   G_TOKEN_ERROR,
1792   
1793   G_TOKEN_CHAR,
1794   G_TOKEN_BINARY,
1795   G_TOKEN_OCTAL,
1796   G_TOKEN_INT,
1797   G_TOKEN_HEX,
1798   G_TOKEN_FLOAT,
1799   G_TOKEN_STRING,
1800   
1801   G_TOKEN_SYMBOL,
1802   G_TOKEN_IDENTIFIER,
1803   G_TOKEN_IDENTIFIER_NULL,
1804   
1805   G_TOKEN_COMMENT_SINGLE,
1806   G_TOKEN_COMMENT_MULTI,
1807   G_TOKEN_LAST
1808 } GTokenType;
1809
1810 union   _GTokenValue
1811 {
1812   gpointer      v_symbol;
1813   gchar         *v_identifier;
1814   gulong        v_binary;
1815   gulong        v_octal;
1816   gulong        v_int;
1817   gdouble       v_float;
1818   gulong        v_hex;
1819   gchar         *v_string;
1820   gchar         *v_comment;
1821   guchar        v_char;
1822   guint         v_error;
1823 };
1824
1825 struct  _GScannerConfig
1826 {
1827   /* Character sets
1828    */
1829   gchar         *cset_skip_characters;          /* default: " \t\n" */
1830   gchar         *cset_identifier_first;
1831   gchar         *cset_identifier_nth;
1832   gchar         *cpair_comment_single;          /* default: "#\n" */
1833   
1834   /* Should symbol lookup work case sensitive?
1835    */
1836   guint         case_sensitive : 1;
1837   
1838   /* Boolean values to be adjusted "on the fly"
1839    * to configure scanning behaviour.
1840    */
1841   guint         skip_comment_multi : 1;         /* C like comment */
1842   guint         skip_comment_single : 1;        /* single line comment */
1843   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1844   guint         scan_identifier : 1;
1845   guint         scan_identifier_1char : 1;
1846   guint         scan_identifier_NULL : 1;
1847   guint         scan_symbols : 1;
1848   guint         scan_binary : 1;
1849   guint         scan_octal : 1;
1850   guint         scan_float : 1;
1851   guint         scan_hex : 1;                   /* `0x0ff0' */
1852   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1853   guint         scan_string_sq : 1;             /* string: 'anything' */
1854   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1855   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1856   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1857   guint         identifier_2_string : 1;
1858   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1859   guint         symbol_2_token : 1;
1860   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
1861 };
1862
1863 struct  _GScanner
1864 {
1865   /* unused fields */
1866   gpointer              user_data;
1867   guint                 max_parse_errors;
1868   
1869   /* g_scanner_error() increments this field */
1870   guint                 parse_errors;
1871   
1872   /* name of input stream, featured by the default message handler */
1873   const gchar           *input_name;
1874   
1875   /* data pointer for derived structures */
1876   gpointer              derived_data;
1877   
1878   /* link into the scanner configuration */
1879   GScannerConfig        *config;
1880   
1881   /* fields filled in after g_scanner_get_next_token() */
1882   GTokenType            token;
1883   GTokenValue           value;
1884   guint                 line;
1885   guint                 position;
1886   
1887   /* fields filled in after g_scanner_peek_next_token() */
1888   GTokenType            next_token;
1889   GTokenValue           next_value;
1890   guint                 next_line;
1891   guint                 next_position;
1892   
1893   /* to be considered private */
1894   GHashTable            *symbol_table;
1895   gint                  input_fd;
1896   const gchar           *text;
1897   const gchar           *text_end;
1898   gchar                 *buffer;
1899   guint                 scope_id;
1900   
1901   /* handler function for _warn and _error */
1902   GScannerMsgFunc       msg_handler;
1903 };
1904
1905 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
1906 void            g_scanner_destroy               (GScanner       *scanner);
1907 void            g_scanner_input_file            (GScanner       *scanner,
1908                                                  gint           input_fd);
1909 void            g_scanner_sync_file_offset      (GScanner       *scanner);
1910 void            g_scanner_input_text            (GScanner       *scanner,
1911                                                  const  gchar   *text,
1912                                                  guint          text_len);
1913 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
1914 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
1915 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
1916 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
1917 guint           g_scanner_cur_line              (GScanner       *scanner);
1918 guint           g_scanner_cur_position          (GScanner       *scanner);
1919 gboolean        g_scanner_eof                   (GScanner       *scanner);
1920 guint           g_scanner_set_scope             (GScanner       *scanner,
1921                                                  guint           scope_id);
1922 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
1923                                                  guint           scope_id,
1924                                                  const gchar    *symbol,
1925                                                  gpointer       value);
1926 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
1927                                                  guint           scope_id,
1928                                                  const gchar    *symbol);
1929 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
1930                                                  guint           scope_id,
1931                                                  const gchar    *symbol);
1932 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
1933                                                  guint           scope_id,
1934                                                  GHFunc          func,
1935                                                  gpointer        func_data);
1936 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
1937                                                  const gchar    *symbol);
1938 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
1939 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
1940 void            g_scanner_unexp_token           (GScanner       *scanner,
1941                                                  GTokenType     expected_token,
1942                                                  const gchar    *identifier_spec,
1943                                                  const gchar    *symbol_spec,
1944                                                  const gchar    *symbol_name,
1945                                                  const gchar    *message,
1946                                                  gint            is_error);
1947 void            g_scanner_error                 (GScanner       *scanner,
1948                                                  const gchar    *format,
1949                                                  ...) G_GNUC_PRINTF (2,3);
1950 void            g_scanner_warn                  (GScanner       *scanner,
1951                                                  const gchar    *format,
1952                                                  ...) G_GNUC_PRINTF (2,3);
1953 gint            g_scanner_stat_mode             (const gchar    *filename);
1954 /* keep downward source compatibility */
1955 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
1956   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
1957 } G_STMT_END
1958 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
1959   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
1960 } G_STMT_END
1961 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
1962   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
1963 } G_STMT_END
1964
1965
1966 /* Completion */
1967
1968 struct _GCompletion
1969 {
1970   GList* items;
1971   GCompletionFunc func;
1972   
1973   gchar* prefix;
1974   GList* cache;
1975 };
1976
1977 GCompletion* g_completion_new          (GCompletionFunc func);
1978 void         g_completion_add_items    (GCompletion*    cmp,
1979                                         GList*          items);
1980 void         g_completion_remove_items (GCompletion*    cmp,
1981                                         GList*          items);
1982 void         g_completion_clear_items  (GCompletion*    cmp);
1983 GList*       g_completion_complete     (GCompletion*    cmp,
1984                                         gchar*          prefix,
1985                                         gchar**         new_prefix);
1986 void         g_completion_free         (GCompletion*    cmp);
1987
1988
1989 /* GRelation: Indexed Relations.  Imagine a really simple table in a
1990  * database.  Relations are not ordered.  This data type is meant for
1991  * maintaining a N-way mapping.
1992  *
1993  * g_relation_new() creates a relation with FIELDS fields
1994  *
1995  * g_relation_destroy() frees all resources
1996  * g_tuples_destroy() frees the result of g_relation_select()
1997  *
1998  * g_relation_index() indexes relation FIELD with the provided
1999  *   equality and hash functions.  this must be done before any
2000  *   calls to insert are made.
2001  *
2002  * g_relation_insert() inserts a new tuple.  you are expected to
2003  *   provide the right number of fields.
2004  *
2005  * g_relation_delete() deletes all relations with KEY in FIELD
2006  * g_relation_select() returns ...
2007  * g_relation_count() counts ...
2008  */
2009
2010 GRelation* g_relation_new     (gint         fields);
2011 void       g_relation_destroy (GRelation   *relation);
2012 void       g_relation_index   (GRelation   *relation,
2013                                gint         field,
2014                                GHashFunc    hash_func,
2015                                GCompareFunc key_compare_func);
2016 void       g_relation_insert  (GRelation   *relation,
2017                                ...);
2018 gint       g_relation_delete  (GRelation   *relation,
2019                                gconstpointer  key,
2020                                gint         field);
2021 GTuples*   g_relation_select  (GRelation   *relation,
2022                                gconstpointer  key,
2023                                gint         field);
2024 gint       g_relation_count   (GRelation   *relation,
2025                                gconstpointer  key,
2026                                gint         field);
2027 gboolean   g_relation_exists  (GRelation   *relation,
2028                                ...);
2029 void       g_relation_print   (GRelation   *relation);
2030
2031 void       g_tuples_destroy   (GTuples     *tuples);
2032 gpointer   g_tuples_index     (GTuples     *tuples,
2033                                gint         index,
2034                                gint         field);
2035
2036
2037 /* Prime numbers.
2038  */
2039
2040 /* This function returns prime numbers spaced by approximately 1.5-2.0
2041  * and is for use in resizing data structures which prefer
2042  * prime-valued sizes.  The closest spaced prime function returns the
2043  * next largest prime, or the highest it knows about which is about
2044  * MAXINT/4.
2045  */
2046 guint      g_spaced_primes_closest (guint num);
2047
2048
2049 /* IO Channels.
2050  * These are used for plug-in communication in the GIMP, for instance.
2051  * On Unix, it's simply an encapsulated file descriptor (a pipe).
2052  * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2053  * of the writable end) a thread id to post a message to when you have written
2054  * stuff.
2055  */
2056 struct _GIOChannel
2057 {
2058   gint fd;                      /* file handle (pseudo such in Win32) */
2059 #ifdef NATIVE_WIN32
2060   guint peer;                   /* thread to post message to */
2061   guint peer_fd;                /* read handle (in the other process) */
2062   guint offset;                 /* counter of accumulated bytes, to
2063                                  * be included in the message posted
2064                                  * so we keep in sync.
2065                                  */
2066   guint peer_offset;            /* in input channels where the writer's
2067                                  * offset is, so we don't try to read too much
2068                                  */
2069 #endif
2070 };
2071
2072 GIOChannel *g_iochannel_new            (gint        fd);
2073 void        g_iochannel_free           (GIOChannel *channel);
2074 void        g_iochannel_close_and_free (GIOChannel *channel);
2075 void        g_iochannel_wakeup_peer    (GIOChannel *channel);
2076 #ifndef NATIVE_WIN32
2077 #  define   g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2078 #endif
2079
2080
2081 /* Windows emulation stubs for common unix functions
2082  */
2083 #ifdef NATIVE_WIN32
2084
2085 #define MAXPATHLEN 1024
2086
2087 #ifdef _MSC_VER
2088 typedef int pid_t;
2089
2090 /* These POSIXish functions are available in the Microsoft C library
2091  * prefixed with underscore (which of course technically speaking is
2092  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2093  * prevents Microsoft from practically requiring you to include
2094  * <windows.h> every now and then...).
2095  *
2096  * You still need to include the appropriate headers to get the
2097  * prototypes, <io.h> or <direct.h>.
2098  *
2099  * For some functions, we provide emulators in glib, which are prefixed
2100  * with gwin_.
2101  */
2102 #define getcwd                  _getcwd
2103 #define getpid                  _getpid
2104 #define access                  _access
2105 #define open                    _open
2106 #define read                    _read
2107 #define write                   _write
2108 #define lseek                   _lseek
2109 #define close                   _close
2110 #define pipe(phandles)          _pipe (phandles, 4096, _O_BINARY)
2111 #define popen                   _popen
2112 #define pclose                  _pclose
2113 #define fdopen                  _fdopen
2114 #define ftruncate(fd, size)     gwin_ftruncate (fd, size)
2115 #define opendir                 gwin_opendir
2116 #define readdir                 gwin_readdir
2117 #define rewinddir               gwin_rewinddir
2118 #define closedir                gwin_closedir
2119
2120 #define NAME_MAX 255
2121
2122 struct DIR
2123 {
2124   gchar    *dir_name;
2125
2126   gboolean  just_opened;
2127   guint     find_file_handle;
2128   gpointer  find_file_data;
2129 };
2130 typedef struct DIR DIR;
2131 struct dirent
2132 {
2133   gchar  d_name[NAME_MAX + 1];
2134 };
2135
2136 /* emulation functions */
2137 extern int      gwin_ftruncate  (gint            f,
2138                                  guint           size);
2139 DIR*            gwin_opendir    (const gchar    *dirname);
2140 struct dirent*  gwin_readdir    (DIR            *dir);
2141 void            gwin_rewinddir  (DIR            *dir);
2142 gint            gwin_closedir   (DIR            *dir);
2143
2144 #endif /* _MSC_VER */
2145
2146 #endif /* NATIVE_WIN32 */
2147
2148
2149 #ifdef __cplusplus
2150 }
2151 #endif /* __cplusplus */
2152
2153
2154 #endif /* __G_LIB_H__ */