include <stdlib.h> and <stddef.h> if STDC_HEADERS is defined.
[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
20 /*
21  * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #ifndef __G_LIB_H__
28 #define __G_LIB_H__
29
30 /* Here we provide G_GNUC_EXTENSION as an alias for __extension__,
31  * where this is valid. This allows for warningless compilation of
32  * "long long" types even in the presence of '-ansi -pedantic'. This
33  * of course should be with the other GCC-isms below, but then
34  * glibconfig.h wouldn't load cleanly and it is better to have that
35  * here, than in glibconfig.h.  
36  */
37 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
38 #  define G_GNUC_EXTENSION __extension__
39 #else
40 #  define G_GNUC_EXTENSION
41 #endif
42
43 /* system specific config file glibconfig.h provides definitions for
44  * the extrema of many of the standard types. These are:
45  *
46  *  G_MINSHORT, G_MAXSHORT
47  *  G_MININT, G_MAXINT
48  *  G_MINLONG, G_MAXLONG
49  *  G_MINFLOAT, G_MAXFLOAT
50  *  G_MINDOUBLE, G_MAXDOUBLE
51  *
52  * It also provides the following typedefs:
53  *
54  *  gint8, guint8
55  *  gint16, guint16
56  *  gint32, guint32
57  *  gint64, guint64
58  *  gssize, gsize
59  *
60  * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
61  * this file). 
62  *
63  * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
64  * This is useful to pass an integer instead of a pointer to a callback.
65  *
66  *  GINT_TO_POINTER (i), GUINT_TO_POINTER (i)
67  *  GPOINTER_TO_INT (p), GPOINTER_TO_UINT (p)
68  *
69  * Finally, it provides the following wrappers to STDC functions:
70  *
71  *  void g_memmove (gpointer dest, gconstpointer void *src, gulong count);
72  *    A wrapper for STDC memmove, or an implementation, if memmove doesn't
73  *    exist.  The prototype looks like the above, give or take a const,
74  *    or size_t.
75  */
76 #include <glibconfig.h>
77
78 /* Define some mathematical constants that aren't available
79  * symbolically in some strict ISO C implementations.
80  */
81 #define G_E     2.7182818284590452354E0
82 #define G_LN2   6.9314718055994530942E-1
83 #define G_LN10  2.3025850929940456840E0
84 #define G_PI    3.14159265358979323846E0
85 #define G_PI_2  1.57079632679489661923E0
86 #define G_PI_4  0.78539816339744830962E0
87 #define G_SQRT2 1.4142135623730950488E0
88
89 /* include varargs functions for assertment macros
90  */
91 #include <stdarg.h>
92
93 /* optionally feature DMALLOC memory allocation debugger
94  */
95 #ifdef USE_DMALLOC
96 #include "dmalloc.h"
97 #endif
98
99
100 #ifdef G_OS_WIN32
101
102 /* On native Win32, directory separator is the backslash, and search path
103  * separator is the semicolon.
104  */
105 #define G_DIR_SEPARATOR '\\'
106 #define G_DIR_SEPARATOR_S "\\"
107 #define G_SEARCHPATH_SEPARATOR ';'
108 #define G_SEARCHPATH_SEPARATOR_S ";"
109
110 #else  /* !G_OS_WIN32 */
111
112 /* Unix */
113
114 #define G_DIR_SEPARATOR '/'
115 #define G_DIR_SEPARATOR_S "/"
116 #define G_SEARCHPATH_SEPARATOR ':'
117 #define G_SEARCHPATH_SEPARATOR_S ":"
118
119 #endif /* !G_OS_WIN32 */
120
121 #ifdef __cplusplus
122 extern "C" {
123 #endif /* __cplusplus */
124
125
126 /* Provide definitions for some commonly used macros.
127  *  Some of them are only provided if they haven't already
128  *  been defined. It is assumed that if they are already
129  *  defined then the current definition is correct.
130  */
131 #ifndef NULL
132 #  ifdef __cplusplus
133 #    define NULL        (0L)
134 #  else /* !__cplusplus */
135 #    define NULL        ((void*) 0)
136 #  endif /* !__cplusplus */
137 #endif
138
139 #ifndef FALSE
140 #define FALSE   (0)
141 #endif
142
143 #ifndef TRUE
144 #define TRUE    (!FALSE)
145 #endif
146
147 #undef  MAX
148 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
149
150 #undef  MIN
151 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
152
153 #undef  ABS
154 #define ABS(a)     (((a) < 0) ? -(a) : (a))
155
156 #undef  CLAMP
157 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
158
159 #define G_STRINGIFY(macro_or_string)    G_STRINGIFY_ARG (macro_or_string)
160 #define G_STRINGIFY_ARG(contents)       #contents
161
162 /* provide a string identifying the current code position */
163 #ifdef  __GNUC__
164 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__) ":" __PRETTY_FUNCTION__ "()"
165 #else
166 #  define G_STRLOC      __FILE__ ":" G_STRINGIFY (__LINE__)
167 #endif
168
169
170 /* Count the number of elements in an array. The array must be defined
171  * as such; using this with a dynamically allocated array will give
172  * incorrect results.
173  */
174 #define G_N_ELEMENTS(arr)               (sizeof (arr) / sizeof ((arr)[0]))
175
176 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
177  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
178  */
179 #if !defined (G_VA_COPY)
180 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
181 #  define G_VA_COPY(ap1, ap2)     (*(ap1) = *(ap2))
182 #  elif defined (G_VA_COPY_AS_ARRAY)
183 #  define G_VA_COPY(ap1, ap2)     g_memmove ((ap1), (ap2), sizeof (va_list))
184 #  else /* va_list is a pointer */
185 #  define G_VA_COPY(ap1, ap2)     ((ap1) = (ap2))
186 #  endif /* va_list is a pointer */
187 #endif /* !G_VA_COPY */
188
189
190 /* Provide convenience macros for handling structure
191  * fields through their offsets.
192  */
193 #define G_STRUCT_OFFSET(struct_type, member)    \
194     ((glong) ((guint8*) &((struct_type*) 0)->member))
195 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
196     ((gpointer) ((guint8*) (struct_p) + (glong) (struct_offset)))
197 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
198     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
199
200
201 /* inlining hassle. for compilers that don't allow the `inline' keyword,
202  * mostly because of strict ANSI C compliance or dumbness, we try to fall
203  * back to either `__inline__' or `__inline'.
204  * we define G_CAN_INLINE, if the compiler seems to be actually
205  * *capable* to do function inlining, in which case inline function bodys
206  * do make sense. we also define G_INLINE_FUNC to properly export the
207  * function prototypes if no inlining can be performed.
208  * we special case most of the stuff, so inline functions can have a normal
209  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
210  */
211 #ifndef G_INLINE_FUNC
212 #  define G_CAN_INLINE 1
213 #endif
214 #ifdef G_HAVE_INLINE
215 #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
216 #    undef inline
217 #    define inline __inline__
218 #  endif
219 #else /* !G_HAVE_INLINE */
220 #  undef inline
221 #  if defined (G_HAVE___INLINE__)
222 #    define inline __inline__
223 #  else /* !inline && !__inline__ */
224 #    if defined (G_HAVE___INLINE)
225 #      define inline __inline
226 #    else /* !inline && !__inline__ && !__inline */
227 #      define inline /* don't inline, then */
228 #      ifndef G_INLINE_FUNC
229 #        undef G_CAN_INLINE
230 #      endif
231 #    endif
232 #  endif
233 #endif
234 #ifndef G_INLINE_FUNC
235 #  ifdef __GNUC__
236 #    ifdef __OPTIMIZE__
237 #      define G_INLINE_FUNC extern inline
238 #    else
239 #      undef G_CAN_INLINE
240 #      define G_INLINE_FUNC extern
241 #    endif
242 #  else /* !__GNUC__ */
243 #    ifdef G_CAN_INLINE
244 #      define G_INLINE_FUNC static inline
245 #    else
246 #      define G_INLINE_FUNC extern
247 #    endif
248 #  endif /* !__GNUC__ */
249 #endif /* !G_INLINE_FUNC */
250
251
252 /* Provide simple macro statement wrappers (adapted from Perl):
253  *  G_STMT_START { statements; } G_STMT_END;
254  *  can be used as a single statement, as in
255  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
256  *
257  *  For gcc we will wrap the statements within `({' and `})' braces.
258  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
259  *  and otherwise within `do' and `while (0)'.
260  */
261 #if !(defined (G_STMT_START) && defined (G_STMT_END))
262 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
263 #    define G_STMT_START        (void)(
264 #    define G_STMT_END          )
265 #  else
266 #    if (defined (sun) || defined (__sun__))
267 #      define G_STMT_START      if (1)
268 #      define G_STMT_END        else (void)0
269 #    else
270 #      define G_STMT_START      do
271 #      define G_STMT_END        while (0)
272 #    endif
273 #  endif
274 #endif
275
276
277 /* Provide macros to feature the GCC function attribute.
278  */
279 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
280 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
281   __attribute__((format (printf, format_idx, arg_idx)))
282 #define G_GNUC_SCANF( format_idx, arg_idx )     \
283   __attribute__((format (scanf, format_idx, arg_idx)))
284 #define G_GNUC_FORMAT( arg_idx )                \
285   __attribute__((format_arg (arg_idx)))
286 #define G_GNUC_NORETURN                         \
287   __attribute__((noreturn))
288 #define G_GNUC_CONST                            \
289   __attribute__((const))
290 #define G_GNUC_UNUSED                           \
291   __attribute__((unused))
292 #else   /* !__GNUC__ */
293 #define G_GNUC_PRINTF( format_idx, arg_idx )
294 #define G_GNUC_SCANF( format_idx, arg_idx )
295 #define G_GNUC_FORMAT( arg_idx )
296 #define G_GNUC_NORETURN
297 #define G_GNUC_CONST
298 #define G_GNUC_UNUSED
299 #endif  /* !__GNUC__ */
300
301 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
302  * macros, so we can refer to them as strings unconditionally.
303  */
304 #ifdef  __GNUC__
305 #define G_GNUC_FUNCTION         __FUNCTION__
306 #define G_GNUC_PRETTY_FUNCTION  __PRETTY_FUNCTION__
307 #else   /* !__GNUC__ */
308 #define G_GNUC_FUNCTION         ""
309 #define G_GNUC_PRETTY_FUNCTION  ""
310 #endif  /* !__GNUC__ */
311
312 /* we try to provide a usefull equivalent for ATEXIT if it is
313  * not defined, but use is actually abandoned. people should
314  * use g_atexit() instead.
315  */
316 #ifndef ATEXIT
317 # define ATEXIT(proc)   g_ATEXIT(proc)
318 #else
319 # define G_NATIVE_ATEXIT
320 #endif /* ATEXIT */
321
322 /* Hacker macro to place breakpoints for elected machines.
323  * Actual use is strongly deprecated of course ;)
324  */
325 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
326 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
327 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
328 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
329 #else   /* !__i386__ && !__alpha__ */
330 #define G_BREAKPOINT()
331 #endif  /* __i386__ */
332
333
334 /* Provide macros for easily allocating memory. The macros
335  *  will cast the allocated memory to the specified type
336  *  in order to avoid compiler warnings. (Makes the code neater).
337  */
338
339 #ifdef __DMALLOC_H__
340 #  define g_new(type, count)            (ALLOC (type, count))
341 #  define g_new0(type, count)           (CALLOC (type, count))
342 #  define g_renew(type, mem, count)     (REALLOC (mem, type, count))
343 #else /* __DMALLOC_H__ */
344 #  define g_new(type, count)      \
345       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
346 #  define g_new0(type, count)     \
347       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
348 #  define g_renew(type, mem, count)       \
349       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
350 #endif /* __DMALLOC_H__ */
351
352 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
353   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
354                    sizeof (type), \
355                    sizeof (type) * (pre_alloc), \
356                    (alloc_type)) \
357 )
358 #define g_chunk_new(type, chunk)        ( \
359   (type *) g_mem_chunk_alloc (chunk) \
360 )
361 #define g_chunk_new0(type, chunk)       ( \
362   (type *) g_mem_chunk_alloc0 (chunk) \
363 )
364 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
365   g_mem_chunk_free ((mem_chunk), (mem)); \
366 } G_STMT_END
367
368
369 /* Provide macros for error handling. The "assert" macros will
370  *  exit on failure. The "return" macros will exit the current
371  *  function. Two different definitions are given for the macros
372  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
373  *  __PRETTY_FUNCTION__ capability.
374  */
375
376 #ifdef G_DISABLE_ASSERT
377
378 #define g_assert(expr)
379 #define g_assert_not_reached()
380
381 #else /* !G_DISABLE_ASSERT */
382
383 #ifdef __GNUC__
384
385 #define g_assert(expr)                  G_STMT_START{           \
386      if (!(expr))                                               \
387        g_log (G_LOG_DOMAIN,                                     \
388               G_LOG_LEVEL_ERROR,                                \
389               "file %s: line %d (%s): assertion failed: (%s)",  \
390               __FILE__,                                         \
391               __LINE__,                                         \
392               __PRETTY_FUNCTION__,                              \
393               #expr);                   }G_STMT_END
394
395 #define g_assert_not_reached()          G_STMT_START{           \
396      g_log (G_LOG_DOMAIN,                                       \
397             G_LOG_LEVEL_ERROR,                                  \
398             "file %s: line %d (%s): should not be reached",     \
399             __FILE__,                                           \
400             __LINE__,                                           \
401             __PRETTY_FUNCTION__);       }G_STMT_END
402
403 #else /* !__GNUC__ */
404
405 #define g_assert(expr)                  G_STMT_START{           \
406      if (!(expr))                                               \
407        g_log (G_LOG_DOMAIN,                                     \
408               G_LOG_LEVEL_ERROR,                                \
409               "file %s: line %d: assertion failed: (%s)",       \
410               __FILE__,                                         \
411               __LINE__,                                         \
412               #expr);                   }G_STMT_END
413
414 #define g_assert_not_reached()          G_STMT_START{   \
415      g_log (G_LOG_DOMAIN,                               \
416             G_LOG_LEVEL_ERROR,                          \
417             "file %s: line %d: should not be reached",  \
418             __FILE__,                                   \
419             __LINE__);          }G_STMT_END
420
421 #endif /* __GNUC__ */
422
423 #endif /* !G_DISABLE_ASSERT */
424
425
426 #ifdef G_DISABLE_CHECKS
427
428 #define g_return_if_fail(expr)
429 #define g_return_val_if_fail(expr,val)
430 #define g_return_if_reached() return
431 #define g_return_val_if_reached(val) return (val)
432
433 #else /* !G_DISABLE_CHECKS */
434
435 #ifdef __GNUC__
436
437 #define g_return_if_fail(expr)          G_STMT_START{                   \
438      if (!(expr))                                                       \
439        {                                                                \
440          g_log (G_LOG_DOMAIN,                                           \
441                 G_LOG_LEVEL_CRITICAL,                                   \
442                 "file %s: line %d (%s): assertion `%s' failed",         \
443                 __FILE__,                                               \
444                 __LINE__,                                               \
445                 __PRETTY_FUNCTION__,                                    \
446                 #expr);                                                 \
447          return;                                                        \
448        };                               }G_STMT_END
449
450 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
451      if (!(expr))                                                       \
452        {                                                                \
453          g_log (G_LOG_DOMAIN,                                           \
454                 G_LOG_LEVEL_CRITICAL,                                   \
455                 "file %s: line %d (%s): assertion `%s' failed",         \
456                 __FILE__,                                               \
457                 __LINE__,                                               \
458                 __PRETTY_FUNCTION__,                                    \
459                 #expr);                                                 \
460          return (val);                                                  \
461        };                               }G_STMT_END
462
463 #define g_return_if_reached()           G_STMT_START{                   \
464      g_log (G_LOG_DOMAIN,                                               \
465             G_LOG_LEVEL_CRITICAL,                                       \
466             "file %s: line %d (%s): should not be reached",             \
467             __FILE__,                                                   \
468             __LINE__,                                                   \
469             __PRETTY_FUNCTION__);                                       \
470      return;                            }G_STMT_END
471
472 #define g_return_val_if_reached(val)    G_STMT_START{                   \
473      g_log (G_LOG_DOMAIN,                                               \
474             G_LOG_LEVEL_CRITICAL,                                       \
475             "file %s: line %d (%s): should not be reached",             \
476             __FILE__,                                                   \
477             __LINE__,                                                   \
478             __PRETTY_FUNCTION__);                                       \
479      return (val);                      }G_STMT_END
480
481 #else /* !__GNUC__ */
482
483 #define g_return_if_fail(expr)          G_STMT_START{           \
484      if (!(expr))                                               \
485        {                                                        \
486          g_log (G_LOG_DOMAIN,                                   \
487                 G_LOG_LEVEL_CRITICAL,                           \
488                 "file %s: line %d: assertion `%s' failed",      \
489                 __FILE__,                                       \
490                 __LINE__,                                       \
491                 #expr);                                         \
492          return;                                                \
493        };                               }G_STMT_END
494
495 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
496      if (!(expr))                                               \
497        {                                                        \
498          g_log (G_LOG_DOMAIN,                                   \
499                 G_LOG_LEVEL_CRITICAL,                           \
500                 "file %s: line %d: assertion `%s' failed",      \
501                 __FILE__,                                       \
502                 __LINE__,                                       \
503                 #expr);                                         \
504          return (val);                                          \
505        };                               }G_STMT_END
506
507 #define g_return_if_reached()           G_STMT_START{           \
508      g_log (G_LOG_DOMAIN,                                       \
509             G_LOG_LEVEL_CRITICAL,                               \
510             "file %s: line %d: should not be reached",          \
511             __FILE__,                                           \
512             __LINE__);                                          \
513      return;                            }G_STMT_END
514
515 #define g_return_val_if_reached(val)    G_STMT_START{           \
516      g_log (G_LOG_DOMAIN,                                       \
517             G_LOG_LEVEL_CRITICAL,                               \
518             "file %s: line %d: should not be reached",          \
519             __FILE__,                                           \
520             __LINE__);                                          \
521      return (val);                      }G_STMT_END
522
523 #endif /* !__GNUC__ */
524
525 #endif /* !G_DISABLE_CHECKS */
526
527
528 /* Provide type definitions for commonly used types.
529  *  These are useful because a "gint8" can be adjusted
530  *  to be 1 byte (8 bits) on all platforms. Similarly and
531  *  more importantly, "gint32" can be adjusted to be
532  *  4 bytes (32 bits) on all platforms.
533  */
534
535 typedef char   gchar;
536 typedef short  gshort;
537 typedef long   glong;
538 typedef int    gint;
539 typedef gint   gboolean;
540 typedef gchar* gstring;
541
542 typedef unsigned char   guchar;
543 typedef unsigned short  gushort;
544 typedef unsigned long   gulong;
545 typedef unsigned int    guint;
546
547 #define G_GSHORT_FORMAT  "hi"
548 #define G_GUSHORT_FORMAT "hu"
549 #define G_GINT_FORMAT    "i"
550 #define G_GUINT_FORMAT   "u"
551 #define G_GLONG_FORMAT   "li"
552 #define G_GULONG_FORMAT  "lu"
553
554 typedef float   gfloat;
555 typedef double  gdouble;
556
557 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
558  * Since gldouble isn't used anywhere, just disable it for now */
559
560 #if 0
561 #ifdef HAVE_LONG_DOUBLE
562 typedef long double gldouble;
563 #else /* HAVE_LONG_DOUBLE */
564 typedef double gldouble;
565 #endif /* HAVE_LONG_DOUBLE */
566 #endif /* 0 */
567
568 typedef void* gpointer;
569 typedef const void *gconstpointer;
570
571
572 typedef guint32 GQuark;
573 typedef gint32  GTime;
574
575
576 /* Portable endian checks and conversions
577  *
578  * glibconfig.h defines G_BYTE_ORDER which expands to one of
579  * the below macros.
580  */
581 #define G_LITTLE_ENDIAN 1234
582 #define G_BIG_ENDIAN    4321
583 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
584
585
586 /* Basic bit swapping functions
587  */
588 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
589     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
590     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
591 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
592     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
593     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
594     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
595     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
596
597 /* Intel specific stuff for speed
598  */
599 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
600 #  define GUINT16_SWAP_LE_BE_X86(val) \
601      (__extension__                                     \
602       ({ register guint16 __v;                          \
603          if (__builtin_constant_p (val))                \
604            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
605          else                                           \
606            __asm__ __const__ ("rorw $8, %w0"            \
607                               : "=r" (__v)              \
608                               : "0" ((guint16) (val))); \
609         __v; }))
610 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
611 #  if !defined(__i486__) && !defined(__i586__) \
612       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
613 #     define GUINT32_SWAP_LE_BE_X86(val) \
614         (__extension__                                          \
615          ({ register guint32 __v;                               \
616             if (__builtin_constant_p (val))                     \
617               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
618           else                                                  \
619             __asm__ __const__ ("rorw $8, %w0\n\t"               \
620                                "rorl $16, %0\n\t"               \
621                                "rorw $8, %w0"                   \
622                                : "=r" (__v)                     \
623                                : "0" ((guint32) (val)));        \
624         __v; }))
625 #  else /* 486 and higher has bswap */
626 #     define GUINT32_SWAP_LE_BE_X86(val) \
627         (__extension__                                          \
628          ({ register guint32 __v;                               \
629             if (__builtin_constant_p (val))                     \
630               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
631           else                                                  \
632             __asm__ __const__ ("bswap %0"                       \
633                                : "=r" (__v)                     \
634                                : "0" ((guint32) (val)));        \
635         __v; }))
636 #  endif /* processor specific 32-bit stuff */
637 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
638 #else /* !__i386__ */
639 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
640 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
641 #endif /* __i386__ */
642
643 #ifdef G_HAVE_GINT64
644 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
645       (((guint64) (val) &                                               \
646         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
647       (((guint64) (val) &                                               \
648         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
649       (((guint64) (val) &                                               \
650         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
651       (((guint64) (val) &                                               \
652         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
653       (((guint64) (val) &                                               \
654         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
655       (((guint64) (val) &                                               \
656         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
657       (((guint64) (val) &                                               \
658         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
659       (((guint64) (val) &                                               \
660         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
661 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
662 #    define GUINT64_SWAP_LE_BE_X86(val) \
663         (__extension__                                          \
664          ({ union { guint64 __ll;                               \
665                     guint32 __l[2]; } __r;                      \
666             if (__builtin_constant_p (val))                     \
667               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
668             else                                                \
669               {                                                 \
670                 union { guint64 __ll;                           \
671                         guint32 __l[2]; } __w;                  \
672                 __w.__ll = ((guint64) val);                     \
673                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
674                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
675               }                                                 \
676           __r.__ll; }))
677 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
678 #  else /* !__i386__ */
679 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
680 #  endif
681 #endif
682
683 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
684 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
685 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
686     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
687     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
688 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
689     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
690     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
691
692 /* The G*_TO_?E() macros are defined in glibconfig.h.
693  * The transformation is symmetric, so the FROM just maps to the TO.
694  */
695 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
696 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
697 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
698 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
699 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
700 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
701 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
702 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
703
704 #ifdef G_HAVE_GINT64
705 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
706 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
707 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
708 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
709 #endif
710
711 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
712 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
713 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
714 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
715
716 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
717 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
718 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
719 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
720
721
722 /* Portable versions of host-network order stuff
723  */
724 #define g_ntohl(val) (GUINT32_FROM_BE (val))
725 #define g_ntohs(val) (GUINT16_FROM_BE (val))
726 #define g_htonl(val) (GUINT32_TO_BE (val))
727 #define g_htons(val) (GUINT16_TO_BE (val))
728
729
730 /* Glib version.
731  * we prefix variable declarations so they can
732  * properly get exported in windows dlls.
733  */
734 #ifdef G_OS_WIN32
735 #  ifdef GLIB_COMPILATION
736 #    define GLIB_VAR __declspec(dllexport)
737 #  else /* !GLIB_COMPILATION */
738 #    define GLIB_VAR extern __declspec(dllimport)
739 #  endif /* !GLIB_COMPILATION */
740 #else /* !G_OS_WIN32 */
741 #  define GLIB_VAR extern
742 #endif /* !G_OS_WIN32 */
743
744 GLIB_VAR const guint glib_major_version;
745 GLIB_VAR const guint glib_minor_version;
746 GLIB_VAR const guint glib_micro_version;
747 GLIB_VAR const guint glib_interface_age;
748 GLIB_VAR const guint glib_binary_age;
749
750 #define GLIB_CHECK_VERSION(major,minor,micro)    \
751     (GLIB_MAJOR_VERSION > (major) || \
752      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
753      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
754       GLIB_MICRO_VERSION >= (micro)))
755
756 /* Forward declarations of glib types.
757  */
758 typedef struct _GAllocator      GAllocator;
759 typedef struct _GArray          GArray;
760 typedef struct _GByteArray      GByteArray;
761 typedef struct _GCache          GCache;
762 typedef struct _GCompletion     GCompletion;
763 typedef struct _GData           GData;
764 typedef struct _GDebugKey       GDebugKey;
765 typedef union  _GDoubleIEEE754  GDoubleIEEE754;
766 typedef union  _GFloatIEEE754   GFloatIEEE754;
767 typedef struct _GHashTable      GHashTable;
768 typedef struct _GHook           GHook;
769 typedef struct _GHookList       GHookList;
770 typedef struct _GList           GList;
771 typedef struct _GMemChunk       GMemChunk;
772 typedef struct _GNode           GNode;
773 typedef struct _GPtrArray       GPtrArray;
774 typedef struct _GQueue          GQueue;
775 typedef struct _GRand           GRand;
776 typedef struct _GRelation       GRelation;
777 typedef struct _GScanner        GScanner;
778 typedef struct _GScannerConfig  GScannerConfig;
779 typedef struct _GSList          GSList;
780 typedef struct _GString         GString;
781 typedef struct _GStringChunk    GStringChunk;
782 typedef struct _GTimer          GTimer;
783 typedef struct _GTrashStack     GTrashStack;
784 typedef struct _GTree           GTree;
785 typedef struct _GTuples         GTuples;
786 typedef union  _GTokenValue     GTokenValue;
787 typedef struct _GIOChannel      GIOChannel;
788
789 /* Tree traverse flags */
790 typedef enum
791 {
792   G_TRAVERSE_LEAFS      = 1 << 0,
793   G_TRAVERSE_NON_LEAFS  = 1 << 1,
794   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
795   G_TRAVERSE_MASK       = 0x03
796 } GTraverseFlags;
797
798 /* Tree traverse orders */
799 typedef enum
800 {
801   G_IN_ORDER,
802   G_PRE_ORDER,
803   G_POST_ORDER,
804   G_LEVEL_ORDER
805 } GTraverseType;
806
807 /* Log level shift offset for user defined
808  * log levels (0-7 are used by GLib).
809  */
810 #define G_LOG_LEVEL_USER_SHIFT  (8)
811
812 /* Glib log levels and flags.
813  */
814 typedef enum
815 {
816   /* log flags */
817   G_LOG_FLAG_RECURSION          = 1 << 0,
818   G_LOG_FLAG_FATAL              = 1 << 1,
819   
820   /* GLib log levels */
821   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
822   G_LOG_LEVEL_CRITICAL          = 1 << 3,
823   G_LOG_LEVEL_WARNING           = 1 << 4,
824   G_LOG_LEVEL_MESSAGE           = 1 << 5,
825   G_LOG_LEVEL_INFO              = 1 << 6,
826   G_LOG_LEVEL_DEBUG             = 1 << 7,
827   
828   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
829 } GLogLevelFlags;
830
831 /* GLib log levels that are considered fatal by default */
832 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
833
834
835 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
836 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
837 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
838 typedef gint            (*GCompareFunc)         (gconstpointer  a,
839                                                  gconstpointer  b);
840 typedef gchar*          (*GCompletionFunc)      (gpointer);
841 typedef void            (*GDestroyNotify)       (gpointer       data);
842 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
843                                                  gpointer       data,
844                                                  gpointer       user_data);
845 typedef void            (*GFunc)                (gpointer       data,
846                                                  gpointer       user_data);
847 typedef guint           (*GHashFunc)            (gconstpointer  key);
848 typedef void            (*GFreeFunc)            (gpointer       data);
849 typedef void            (*GHFunc)               (gpointer       key,
850                                                  gpointer       value,
851                                                  gpointer       user_data);
852 typedef gboolean        (*GHRFunc)              (gpointer       key,
853                                                  gpointer       value,
854                                                  gpointer       user_data);
855 typedef gint            (*GHookCompareFunc)     (GHook          *new_hook,
856                                                  GHook          *sibling);
857 typedef gboolean        (*GHookFindFunc)        (GHook          *hook,
858                                                  gpointer        data);
859 typedef void            (*GHookMarshaller)      (GHook          *hook,
860                                                  gpointer        data);
861 typedef gboolean        (*GHookCheckMarshaller) (GHook          *hook,
862                                                  gpointer        data);
863 typedef void            (*GHookFunc)            (gpointer        data);
864 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
865 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
866                                                  GHook          *hook);
867 typedef void            (*GLogFunc)             (const gchar   *log_domain,
868                                                  GLogLevelFlags log_level,
869                                                  const gchar   *message,
870                                                  gpointer       user_data);
871 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
872                                                  gpointer       data);
873 typedef void            (*GNodeForeachFunc)     (GNode         *node,
874                                                  gpointer       data);
875 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
876                                                  gchar         *message,
877                                                  gint           error);
878 typedef gint            (*GTraverseFunc)        (gpointer       key,
879                                                  gpointer       value,
880                                                  gpointer       data);
881 typedef void            (*GVoidFunc)            (void);
882
883
884 struct _GArray
885 {
886   gchar *data;
887   guint len;
888 };
889
890 struct _GByteArray
891 {
892   guint8 *data;
893   guint   len;
894 };
895
896 struct _GDebugKey
897 {
898   gchar *key;
899   guint  value;
900 };
901
902 struct _GList
903 {
904   gpointer data;
905   GList *next;
906   GList *prev;
907 };
908
909 struct _GPtrArray
910 {
911   gpointer *pdata;
912   guint     len;
913 };
914
915 struct _GQueue
916 {
917   GList *head;
918   GList *tail;
919   guint  length;
920 };
921
922 struct _GSList
923 {
924   gpointer data;
925   GSList *next;
926 };
927
928 struct _GString
929 {
930   gchar *str;
931   gint len;
932 };
933
934 struct _GTrashStack
935 {
936   GTrashStack *next;
937 };
938
939 struct _GTuples
940 {
941   guint len;
942 };
943
944
945 /* IEEE Standard 754 Single Precision Storage Format (gfloat):
946  *
947  *        31 30           23 22            0
948  * +--------+---------------+---------------+
949  * | s 1bit | e[30:23] 8bit | f[22:0] 23bit |
950  * +--------+---------------+---------------+
951  * B0------------------->B1------->B2-->B3-->
952  *
953  * IEEE Standard 754 Double Precision Storage Format (gdouble):
954  *
955  *        63 62            52 51            32   31            0
956  * +--------+----------------+----------------+ +---------------+
957  * | s 1bit | e[62:52] 11bit | f[51:32] 20bit | | f[31:0] 32bit |
958  * +--------+----------------+----------------+ +---------------+
959  * B0--------------->B1---------->B2--->B3---->  B4->B5->B6->B7->
960  */
961 /* subtract from biased_exponent to form base2 exponent (normal numbers) */
962 #define G_IEEE754_FLOAT_BIAS    (127)
963 #define G_IEEE754_DOUBLE_BIAS   (1023)
964 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
965 #define G_LOG_2_BASE_10         (0.30102999566398119521)
966 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
967 union _GFloatIEEE754
968 {
969   gfloat v_float;
970   struct {
971     guint mantissa : 23;
972     guint biased_exponent : 8;
973     guint sign : 1;
974   } mpn;
975 };
976 union _GDoubleIEEE754
977 {
978   gdouble v_double;
979   struct {
980     guint mantissa_low : 32;
981     guint mantissa_high : 20;
982     guint biased_exponent : 11;
983     guint sign : 1;
984   } mpn;
985 };
986 #elif G_BYTE_ORDER == G_BIG_ENDIAN
987 union _GFloatIEEE754
988 {
989   gfloat v_float;
990   struct {
991     guint sign : 1;
992     guint biased_exponent : 8;
993     guint mantissa : 23;
994   } mpn;
995 };
996 union _GDoubleIEEE754
997 {
998   gdouble v_double;
999   struct {
1000     guint sign : 1;
1001     guint biased_exponent : 11;
1002     guint mantissa_high : 20;
1003     guint mantissa_low : 32;
1004   } mpn;
1005 };
1006 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1007 #error unknown ENDIAN type
1008 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
1009
1010
1011 /* Doubly linked lists
1012  */
1013 void   g_list_push_allocator    (GAllocator     *allocator);
1014 void   g_list_pop_allocator     (void);
1015 GList* g_list_alloc             (void);
1016 void   g_list_free              (GList          *list);
1017 void   g_list_free_1            (GList          *list);
1018 GList* g_list_append            (GList          *list,
1019                                  gpointer        data);
1020 GList* g_list_prepend           (GList          *list,
1021                                  gpointer        data);
1022 GList* g_list_insert            (GList          *list,
1023                                  gpointer        data,
1024                                  gint            position);
1025 GList* g_list_insert_sorted     (GList          *list,
1026                                  gpointer        data,
1027                                  GCompareFunc    func);
1028 GList* g_list_concat            (GList          *list1,
1029                                  GList          *list2);
1030 GList* g_list_remove            (GList          *list,
1031                                  gconstpointer   data);
1032 GList* g_list_remove_link       (GList          *list,
1033                                  GList          *llink);
1034 GList* g_list_delete_link       (GList          *list,
1035                                  GList          *link);
1036 GList* g_list_reverse           (GList          *list);
1037 GList* g_list_copy              (GList          *list);
1038 GList* g_list_nth               (GList          *list,
1039                                  guint           n);
1040 GList* g_list_find              (GList          *list,
1041                                  gconstpointer   data);
1042 GList* g_list_find_custom       (GList          *list,
1043                                  gconstpointer   data,
1044                                  GCompareFunc    func);
1045 gint   g_list_position          (GList          *list,
1046                                  GList          *llink);
1047 gint   g_list_index             (GList          *list,
1048                                  gconstpointer   data);
1049 GList* g_list_last              (GList          *list);
1050 GList* g_list_first             (GList          *list);
1051 guint  g_list_length            (GList          *list);
1052 void   g_list_foreach           (GList          *list,
1053                                  GFunc           func,
1054                                  gpointer        user_data);
1055 GList* g_list_sort              (GList          *list,
1056                                  GCompareFunc    compare_func);
1057 gpointer g_list_nth_data        (GList          *list,
1058                                  guint           n);
1059 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
1060 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
1061
1062
1063 /* Singly linked lists
1064  */
1065 void    g_slist_push_allocator  (GAllocator     *allocator);
1066 void    g_slist_pop_allocator   (void);
1067 GSList* g_slist_alloc           (void);
1068 void    g_slist_free            (GSList         *list);
1069 void    g_slist_free_1          (GSList         *list);
1070 GSList* g_slist_append          (GSList         *list,
1071                                  gpointer        data);
1072 GSList* g_slist_prepend         (GSList         *list,
1073                                  gpointer        data);
1074 GSList* g_slist_insert          (GSList         *list,
1075                                  gpointer        data,
1076                                  gint            position);
1077 GSList* g_slist_insert_sorted   (GSList         *list,
1078                                  gpointer        data,
1079                                  GCompareFunc    func);
1080 GSList* g_slist_insert_before   (GSList         *slist,
1081                                  GSList         *sibling,
1082                                  gpointer        data);
1083 GSList* g_slist_concat          (GSList         *list1,
1084                                  GSList         *list2);
1085 GSList* g_slist_remove          (GSList         *list,
1086                                  gconstpointer   data);
1087 GSList* g_slist_remove_link     (GSList         *list,
1088                                  GSList         *link);
1089 GSList* g_slist_delete_link     (GSList         *list,
1090                                  GSList         *link);
1091 GSList* g_slist_reverse         (GSList         *list);
1092 GSList* g_slist_copy            (GSList         *list);
1093 GSList* g_slist_nth             (GSList         *list,
1094                                  guint           n);
1095 GSList* g_slist_find            (GSList         *list,
1096                                  gconstpointer   data);
1097 GSList* g_slist_find_custom     (GSList         *list,
1098                                  gconstpointer   data,
1099                                  GCompareFunc    func);
1100 gint    g_slist_position        (GSList         *list,
1101                                  GSList         *llink);
1102 gint    g_slist_index           (GSList         *list,
1103                                  gconstpointer   data);
1104 GSList* g_slist_last            (GSList         *list);
1105 guint   g_slist_length          (GSList         *list);
1106 void    g_slist_foreach         (GSList         *list,
1107                                  GFunc           func,
1108                                  gpointer        user_data);
1109 GSList*  g_slist_sort           (GSList          *list,
1110                                  GCompareFunc    compare_func);
1111 gpointer g_slist_nth_data       (GSList         *list,
1112                                  guint           n);
1113 #define  g_slist_next(slist)    ((slist) ? (((GSList *)(slist))->next) : NULL)
1114
1115
1116 /* Queues
1117  */
1118 GQueue*  g_queue_new            (void);
1119 void     g_queue_free           (GQueue  *queue);
1120 void     g_queue_push_head      (GQueue  *queue,
1121                                  gpointer data);
1122 void     g_queue_push_tail      (GQueue  *queue,
1123                                  gpointer data);
1124 gpointer g_queue_pop_head       (GQueue  *queue);
1125 gpointer g_queue_pop_tail       (GQueue  *queue);
1126 gboolean g_queue_is_empty       (GQueue  *queue);
1127 gpointer g_queue_peek_head      (GQueue  *queue);
1128 gpointer g_queue_peek_tail      (GQueue  *queue);
1129 void     g_queue_push_head_link (GQueue  *queue,
1130                                  GList   *link);
1131 void     g_queue_push_tail_link (GQueue  *queue,
1132                                  GList   *link);
1133 GList*   g_queue_pop_head_link  (GQueue  *queue);
1134 GList*   g_queue_pop_tail_link  (GQueue  *queue);
1135
1136 /* Hash tables
1137  */
1138 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
1139                                          GCompareFunc    key_compare_func);
1140 void        g_hash_table_destroy        (GHashTable     *hash_table);
1141 void        g_hash_table_insert         (GHashTable     *hash_table,
1142                                          gpointer        key,
1143                                          gpointer        value);
1144 void        g_hash_table_remove         (GHashTable     *hash_table,
1145                                          gconstpointer   key);
1146 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
1147                                          gconstpointer   key);
1148 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
1149                                          gconstpointer   lookup_key,
1150                                          gpointer       *orig_key,
1151                                          gpointer       *value);
1152 void        g_hash_table_foreach        (GHashTable     *hash_table,
1153                                          GHFunc          func,
1154                                          gpointer        user_data);
1155 guint       g_hash_table_foreach_remove (GHashTable     *hash_table,
1156                                          GHRFunc         func,
1157                                          gpointer        user_data);
1158 guint       g_hash_table_size           (GHashTable     *hash_table);
1159
1160 /* The following two functions are deprecated and will be removed in
1161  * the next major release. They do no good. */
1162 void        g_hash_table_freeze         (GHashTable     *hash_table);
1163 void        g_hash_table_thaw           (GHashTable     *hash_table);
1164
1165 /* Caches
1166  */
1167 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
1168                                 GCacheDestroyFunc  value_destroy_func,
1169                                 GCacheDupFunc      key_dup_func,
1170                                 GCacheDestroyFunc  key_destroy_func,
1171                                 GHashFunc          hash_key_func,
1172                                 GHashFunc          hash_value_func,
1173                                 GCompareFunc       key_compare_func);
1174 void     g_cache_destroy       (GCache            *cache);
1175 gpointer g_cache_insert        (GCache            *cache,
1176                                 gpointer           key);
1177 void     g_cache_remove        (GCache            *cache,
1178                                 gconstpointer      value);
1179 void     g_cache_key_foreach   (GCache            *cache,
1180                                 GHFunc             func,
1181                                 gpointer           user_data);
1182 void     g_cache_value_foreach (GCache            *cache,
1183                                 GHFunc             func,
1184                                 gpointer           user_data);
1185
1186
1187 /* Balanced binary trees
1188  */
1189 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
1190 void     g_tree_destroy  (GTree         *tree);
1191 void     g_tree_insert   (GTree         *tree,
1192                           gpointer       key,
1193                           gpointer       value);
1194 void     g_tree_remove   (GTree         *tree,
1195                           gconstpointer  key);
1196 gpointer g_tree_lookup   (GTree         *tree,
1197                           gconstpointer  key);
1198 void     g_tree_traverse (GTree         *tree,
1199                           GTraverseFunc  traverse_func,
1200                           GTraverseType  traverse_type,
1201                           gpointer       data);
1202 gpointer g_tree_search   (GTree         *tree,
1203                           GCompareFunc   search_func,
1204                           gconstpointer  data);
1205 gint     g_tree_height   (GTree         *tree);
1206 gint     g_tree_nnodes   (GTree         *tree);
1207
1208
1209
1210 /* N-way tree implementation
1211  */
1212 struct _GNode
1213 {
1214   gpointer data;
1215   GNode   *next;
1216   GNode   *prev;
1217   GNode   *parent;
1218   GNode   *children;
1219 };
1220
1221 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1222                                  ((GNode*) (node))->prev == NULL && \
1223                                  ((GNode*) (node))->next == NULL)
1224 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1225
1226 void     g_node_push_allocator  (GAllocator       *allocator);
1227 void     g_node_pop_allocator   (void);
1228 GNode*   g_node_new             (gpointer          data);
1229 void     g_node_destroy         (GNode            *root);
1230 void     g_node_unlink          (GNode            *node);
1231 GNode*   g_node_copy            (GNode            *node);
1232 GNode*   g_node_insert          (GNode            *parent,
1233                                  gint              position,
1234                                  GNode            *node);
1235 GNode*   g_node_insert_before   (GNode            *parent,
1236                                  GNode            *sibling,
1237                                  GNode            *node);
1238 GNode*   g_node_prepend         (GNode            *parent,
1239                                  GNode            *node);
1240 guint    g_node_n_nodes         (GNode            *root,
1241                                  GTraverseFlags    flags);
1242 GNode*   g_node_get_root        (GNode            *node);
1243 gboolean g_node_is_ancestor     (GNode            *node,
1244                                  GNode            *descendant);
1245 guint    g_node_depth           (GNode            *node);
1246 GNode*   g_node_find            (GNode            *root,
1247                                  GTraverseType     order,
1248                                  GTraverseFlags    flags,
1249                                  gpointer          data);
1250
1251 /* convenience macros */
1252 #define g_node_append(parent, node)                             \
1253      g_node_insert_before ((parent), NULL, (node))
1254 #define g_node_insert_data(parent, position, data)              \
1255      g_node_insert ((parent), (position), g_node_new (data))
1256 #define g_node_insert_data_before(parent, sibling, data)        \
1257      g_node_insert_before ((parent), (sibling), g_node_new (data))
1258 #define g_node_prepend_data(parent, data)                       \
1259      g_node_prepend ((parent), g_node_new (data))
1260 #define g_node_append_data(parent, data)                        \
1261      g_node_insert_before ((parent), NULL, g_node_new (data))
1262
1263 /* traversal function, assumes that `node' is root
1264  * (only traverses `node' and its subtree).
1265  * this function is just a high level interface to
1266  * low level traversal functions, optimized for speed.
1267  */
1268 void     g_node_traverse        (GNode            *root,
1269                                  GTraverseType     order,
1270                                  GTraverseFlags    flags,
1271                                  gint              max_depth,
1272                                  GNodeTraverseFunc func,
1273                                  gpointer          data);
1274
1275 /* return the maximum tree height starting with `node', this is an expensive
1276  * operation, since we need to visit all nodes. this could be shortened by
1277  * adding `guint height' to struct _GNode, but then again, this is not very
1278  * often needed, and would make g_node_insert() more time consuming.
1279  */
1280 guint    g_node_max_height       (GNode *root);
1281
1282 void     g_node_children_foreach (GNode           *node,
1283                                   GTraverseFlags   flags,
1284                                   GNodeForeachFunc func,
1285                                   gpointer         data);
1286 void     g_node_reverse_children (GNode           *node);
1287 guint    g_node_n_children       (GNode           *node);
1288 GNode*   g_node_nth_child        (GNode           *node,
1289                                   guint            n);
1290 GNode*   g_node_last_child       (GNode           *node);
1291 GNode*   g_node_find_child       (GNode           *node,
1292                                   GTraverseFlags   flags,
1293                                   gpointer         data);
1294 gint     g_node_child_position   (GNode           *node,
1295                                   GNode           *child);
1296 gint     g_node_child_index      (GNode           *node,
1297                                   gpointer         data);
1298
1299 GNode*   g_node_first_sibling    (GNode           *node);
1300 GNode*   g_node_last_sibling     (GNode           *node);
1301
1302 #define  g_node_prev_sibling(node)      ((node) ? \
1303                                          ((GNode*) (node))->prev : NULL)
1304 #define  g_node_next_sibling(node)      ((node) ? \
1305                                          ((GNode*) (node))->next : NULL)
1306 #define  g_node_first_child(node)       ((node) ? \
1307                                          ((GNode*) (node))->children : NULL)
1308
1309
1310 /* Callback maintenance functions
1311  */
1312 #define G_HOOK_FLAG_USER_SHIFT  (4)
1313 typedef enum
1314 {
1315   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1316   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1317   G_HOOK_FLAG_MASK      = 0x0f
1318 } GHookFlagMask;
1319
1320 #define G_HOOK_DEFERRED_DESTROY ((GHookFreeFunc) 0x01)
1321
1322 struct _GHookList
1323 {
1324   guint          seq_id;
1325   guint          hook_size;
1326   guint          is_setup : 1;
1327   GHook         *hooks;
1328   GMemChunk     *hook_memchunk;
1329   GHookFreeFunc  hook_free; /* virtual function */
1330   GHookFreeFunc  hook_destroy; /* virtual function */
1331 };
1332
1333 struct _GHook
1334 {
1335   gpointer       data;
1336   GHook         *next;
1337   GHook         *prev;
1338   guint          ref_count;
1339   guint          hook_id;
1340   guint          flags;
1341   gpointer       func;
1342   GDestroyNotify destroy;
1343 };
1344
1345 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1346                                           G_HOOK_FLAG_ACTIVE) != 0)
1347 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1348                                           G_HOOK_FLAG_IN_CALL) != 0)
1349 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1350                                          G_HOOK_ACTIVE (hook))
1351 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1352                                          ((GHook*) hook)->prev == NULL && \
1353                                          ((GHook*) hook)->hook_id == 0 && \
1354                                          ((GHook*) hook)->ref_count == 0)
1355
1356 void     g_hook_list_init               (GHookList              *hook_list,
1357                                          guint                   hook_size);
1358 void     g_hook_list_clear              (GHookList              *hook_list);
1359 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1360 void     g_hook_free                    (GHookList              *hook_list,
1361                                          GHook                  *hook);
1362 void     g_hook_ref                     (GHookList              *hook_list,
1363                                          GHook                  *hook);
1364 void     g_hook_unref                   (GHookList              *hook_list,
1365                                          GHook                  *hook);
1366 gboolean g_hook_destroy                 (GHookList              *hook_list,
1367                                          guint                   hook_id);
1368 void     g_hook_destroy_link            (GHookList              *hook_list,
1369                                          GHook                  *hook);
1370 void     g_hook_prepend                 (GHookList              *hook_list,
1371                                          GHook                  *hook);
1372 void     g_hook_insert_before           (GHookList              *hook_list,
1373                                          GHook                  *sibling,
1374                                          GHook                  *hook);
1375 void     g_hook_insert_sorted           (GHookList              *hook_list,
1376                                          GHook                  *hook,
1377                                          GHookCompareFunc        func);
1378 GHook*   g_hook_get                     (GHookList              *hook_list,
1379                                          guint                   hook_id);
1380 GHook*   g_hook_find                    (GHookList              *hook_list,
1381                                          gboolean                need_valids,
1382                                          GHookFindFunc           func,
1383                                          gpointer                data);
1384 GHook*   g_hook_find_data               (GHookList              *hook_list,
1385                                          gboolean                need_valids,
1386                                          gpointer                data);
1387 GHook*   g_hook_find_func               (GHookList              *hook_list,
1388                                          gboolean                need_valids,
1389                                          gpointer                func);
1390 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1391                                          gboolean                need_valids,
1392                                          gpointer                func,
1393                                          gpointer                data);
1394 /* return the first valid hook, and increment its reference count */
1395 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1396                                          gboolean                may_be_in_call);
1397 /* return the next valid hook with incremented reference count, and
1398  * decrement the reference count of the original hook
1399  */
1400 GHook*   g_hook_next_valid              (GHookList              *hook_list,
1401                                          GHook                  *hook,
1402                                          gboolean                may_be_in_call);
1403
1404 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1405 gint     g_hook_compare_ids             (GHook                  *new_hook,
1406                                          GHook                  *sibling);
1407
1408 /* convenience macros */
1409 #define  g_hook_append( hook_list, hook )  \
1410      g_hook_insert_before ((hook_list), NULL, (hook))
1411
1412 /* invoke all valid hooks with the (*GHookFunc) signature.
1413  */
1414 void     g_hook_list_invoke             (GHookList              *hook_list,
1415                                          gboolean                may_recurse);
1416 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1417  * and destroy the hook if FALSE is returned.
1418  */
1419 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1420                                          gboolean                may_recurse);
1421 /* invoke a marshaller on all valid hooks.
1422  */
1423 void     g_hook_list_marshal            (GHookList              *hook_list,
1424                                          gboolean                may_recurse,
1425                                          GHookMarshaller         marshaller,
1426                                          gpointer                data);
1427 void     g_hook_list_marshal_check      (GHookList              *hook_list,
1428                                          gboolean                may_recurse,
1429                                          GHookCheckMarshaller    marshaller,
1430                                          gpointer                data);
1431
1432
1433 /* Fatal error handlers.
1434  * g_on_error_query() will prompt the user to either
1435  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1436  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1437  * process and shows a stack trace.
1438  * These function may cause different actions on non-unix platforms.
1439  * The prg_name arg is required by gdb to find the executable, if it is
1440  * passed as NULL, g_on_error_query() will try g_get_prgname().
1441  */
1442 void g_on_error_query (const gchar *prg_name);
1443 void g_on_error_stack_trace (const gchar *prg_name);
1444
1445
1446 /* Logging mechanism
1447  */
1448 extern          const gchar             *g_log_domain_glib;
1449 guint           g_log_set_handler       (const gchar    *log_domain,
1450                                          GLogLevelFlags  log_levels,
1451                                          GLogFunc        log_func,
1452                                          gpointer        user_data);
1453 void            g_log_remove_handler    (const gchar    *log_domain,
1454                                          guint           handler_id);
1455 void            g_log_default_handler   (const gchar    *log_domain,
1456                                          GLogLevelFlags  log_level,
1457                                          const gchar    *message,
1458                                          gpointer        unused_data);
1459 void            g_log                   (const gchar    *log_domain,
1460                                          GLogLevelFlags  log_level,
1461                                          const gchar    *format,
1462                                          ...) G_GNUC_PRINTF (3, 4);
1463 void            g_logv                  (const gchar    *log_domain,
1464                                          GLogLevelFlags  log_level,
1465                                          const gchar    *format,
1466                                          va_list         args);
1467 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1468                                          GLogLevelFlags  fatal_mask);
1469 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1470 #ifndef G_LOG_DOMAIN
1471 #define G_LOG_DOMAIN    ((gchar*) 0)
1472 #endif  /* G_LOG_DOMAIN */
1473 #ifdef  __GNUC__
1474 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1475                                                G_LOG_LEVEL_ERROR, \
1476                                                format, ##args)
1477 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1478                                                G_LOG_LEVEL_MESSAGE, \
1479                                                format, ##args)
1480 #define g_critical(format, args...)     g_log (G_LOG_DOMAIN, \
1481                                                G_LOG_LEVEL_CRITICAL, \
1482                                                format, ##args)
1483 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1484                                                G_LOG_LEVEL_WARNING, \
1485                                                format, ##args)
1486 #else   /* !__GNUC__ */
1487 static void
1488 g_error (const gchar *format,
1489          ...)
1490 {
1491   va_list args;
1492   va_start (args, format);
1493   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1494   va_end (args);
1495 }
1496 static void
1497 g_message (const gchar *format,
1498            ...)
1499 {
1500   va_list args;
1501   va_start (args, format);
1502   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1503   va_end (args);
1504 }
1505 static void
1506 g_critical (const gchar *format,
1507             ...)
1508 {
1509   va_list args;
1510   va_start (args, format);
1511   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, format, args);
1512   va_end (args);
1513 }
1514 static void
1515 g_warning (const gchar *format,
1516            ...)
1517 {
1518   va_list args;
1519   va_start (args, format);
1520   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1521   va_end (args);
1522 }
1523 #endif  /* !__GNUC__ */
1524
1525 typedef void    (*GPrintFunc)           (const gchar    *string);
1526 void            g_print                 (const gchar    *format,
1527                                          ...) G_GNUC_PRINTF (1, 2);
1528 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1529 void            g_printerr              (const gchar    *format,
1530                                          ...) G_GNUC_PRINTF (1, 2);
1531 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1532
1533 /* deprecated compatibility functions, use g_log_set_handler() instead */
1534 typedef void            (*GErrorFunc)           (const gchar *str);
1535 typedef void            (*GWarningFunc)         (const gchar *str);
1536 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1537 GWarningFunc g_set_warning_handler (GWarningFunc func);
1538 GPrintFunc   g_set_message_handler (GPrintFunc func);
1539
1540
1541 /* Memory allocation and debugging
1542  */
1543 #ifdef USE_DMALLOC
1544
1545 #define g_malloc(size)       ((gpointer) MALLOC (size))
1546 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1547 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1548 #define g_free(mem)          FREE (mem)
1549
1550 #else /* !USE_DMALLOC */
1551
1552 gpointer g_malloc      (gulong    size);
1553 gpointer g_malloc0     (gulong    size);
1554 gpointer g_realloc     (gpointer  mem,
1555                         gulong    size);
1556 void     g_free        (gpointer  mem);
1557
1558 #endif /* !USE_DMALLOC */
1559
1560 void     g_mem_profile (void);
1561 void     g_mem_check   (gpointer  mem);
1562
1563 /* Generic allocators
1564  */
1565 GAllocator* g_allocator_new   (const gchar  *name,
1566                                guint         n_preallocs);
1567 void        g_allocator_free  (GAllocator   *allocator);
1568
1569 #define G_ALLOCATOR_LIST        (1)
1570 #define G_ALLOCATOR_SLIST       (2)
1571 #define G_ALLOCATOR_NODE        (3)
1572
1573
1574 /* "g_mem_chunk_new" creates a new memory chunk.
1575  * Memory chunks are used to allocate pieces of memory which are
1576  *  always the same size. Lists are a good example of such a data type.
1577  * The memory chunk allocates and frees blocks of memory as needed.
1578  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1579  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1580  *  fault...somewhere).
1581  *
1582  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1583  *  want to know what's going on inside do you?)
1584  */
1585
1586 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1587  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1588  *  atom. (They are also useful for lists which use MemChunk to allocate
1589  *  memory but are also part of the MemChunk implementation).
1590  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1591  */
1592
1593 #define G_ALLOC_ONLY      1
1594 #define G_ALLOC_AND_FREE  2
1595
1596 GMemChunk* g_mem_chunk_new     (gchar     *name,
1597                                 gint       atom_size,
1598                                 gulong     area_size,
1599                                 gint       type);
1600 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1601 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1602 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1603 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1604                                 gpointer   mem);
1605 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1606 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1607 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1608 void       g_mem_chunk_info    (void);
1609
1610 /* Ah yes...we have a "g_blow_chunks" function.
1611  * "g_blow_chunks" simply compresses all the chunks. This operation
1612  *  consists of freeing every memory area that should be freed (but
1613  *  which we haven't gotten around to doing yet). And, no,
1614  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1615  *  much better name than "g_mem_chunk_clean_all" or something
1616  *  similar.
1617  */
1618 void g_blow_chunks (void);
1619
1620
1621 /* Timer
1622  */
1623
1624 #define G_MICROSEC 1000000
1625
1626 GTimer* g_timer_new     (void);
1627 void    g_timer_destroy (GTimer  *timer);
1628 void    g_timer_start   (GTimer  *timer);
1629 void    g_timer_stop    (GTimer  *timer);
1630 void    g_timer_reset   (GTimer  *timer);
1631 gdouble g_timer_elapsed (GTimer  *timer,
1632                          gulong  *microseconds);
1633 void    g_usleep        (gulong microseconds);
1634
1635 /* String utility functions that modify a string argument or
1636  * return a constant string that must not be freed.
1637  */
1638 #define  G_STR_DELIMITERS       "_-|> <."
1639 gchar*   g_strdelimit           (gchar       *string,
1640                                  const gchar *delimiters,
1641                                  gchar        new_delimiter);
1642 gchar*   g_strcanon             (gchar       *string,
1643                                  const gchar *valid_chars,
1644                                  gchar        subsitutor);
1645 gdouble  g_strtod               (const gchar *nptr,
1646                                  gchar      **endptr);
1647 gchar*   g_strerror             (gint         errnum);
1648 gchar*   g_strsignal            (gint         signum);
1649 gint     g_strcasecmp           (const gchar *s1,
1650                                  const gchar *s2);
1651 gint     g_strncasecmp          (const gchar *s1,
1652                                  const gchar *s2,
1653                                  guint        n);
1654 gchar*   g_strdown              (gchar       *string);
1655 gchar*   g_strup                (gchar       *string);
1656 gchar*   g_strreverse           (gchar       *string);
1657 /* removes leading spaces */
1658 gchar*   g_strchug              (gchar        *string);
1659 /* removes trailing spaces */
1660 gchar*  g_strchomp              (gchar        *string);
1661 /* removes leading & trailing spaces */
1662 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1663
1664 /* String utility functions that return a newly allocated string which
1665  * ought to be freed with g_free from the caller at some point.
1666  */
1667 gchar*   g_strdup               (const gchar *str);
1668 gchar*   g_strdup_printf        (const gchar *format,
1669                                  ...) G_GNUC_PRINTF (1, 2);
1670 gchar*   g_strdup_vprintf       (const gchar *format,
1671                                  va_list      args);
1672 gchar*   g_strndup              (const gchar *str,
1673                                  guint        n);
1674 gchar*   g_strnfill             (guint        length,
1675                                  gchar        fill_char);
1676 gchar*   g_strconcat            (const gchar *string1,
1677                                  ...); /* NULL terminated */
1678 gchar*   g_strjoin              (const gchar  *separator,
1679                                  ...); /* NULL terminated */
1680 /* Make a copy of a string interpreting C string -style escape
1681  * sequences. Inverse of g_strescape. The recognized sequences are \b
1682  * \f \n \r \t \\ \" and the octal format.
1683  */
1684 gchar*   g_strcompress          (const gchar *source);
1685
1686 /* Convert between the operating system (or C runtime)
1687  * representation of file names and UTF-8.
1688  */
1689 gchar*   g_filename_to_utf8 (const gchar *opsysstring);
1690 gchar*   g_filename_from_utf8 (const gchar *utf8string);
1691
1692 /* Copy a string escaping nonprintable characters like in C strings.
1693  * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
1694  * to a string containing characters that are not to be escaped.
1695  *
1696  * Deprecated API: gchar* g_strescape (const gchar *source);
1697  * Luckily this function wasn't used much, using NULL as second parameter
1698  * provides mostly identical semantics.
1699  */
1700 gchar*   g_strescape            (const gchar *source,
1701                                  const gchar *exceptions);
1702
1703 gpointer g_memdup               (gconstpointer mem,
1704                                  guint         byte_size);
1705
1706 /* NULL terminated string arrays.
1707  * g_strsplit() splits up string into max_tokens tokens at delim and
1708  * returns a newly allocated string array.
1709  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1710  * optional separator, the returned string is newly allocated.
1711  * g_strfreev() frees the array itself and all of its strings.
1712  */
1713 gchar**  g_strsplit             (const gchar  *string,
1714                                  const gchar  *delimiter,
1715                                  gint          max_tokens);
1716 gchar*   g_strjoinv             (const gchar  *separator,
1717                                  gchar       **str_array);
1718 void     g_strfreev             (gchar       **str_array);
1719
1720
1721
1722 /* calculate a string size, guarranteed to fit format + args.
1723  */
1724 guint   g_printf_string_upper_bound (const gchar* format,
1725                                      va_list      args);
1726
1727
1728 /* Retrive static string info
1729  */
1730 gchar*  g_get_user_name         (void);
1731 gchar*  g_get_real_name         (void);
1732 gchar*  g_get_home_dir          (void);
1733 gchar*  g_get_tmp_dir           (void);
1734 gchar*  g_get_prgname           (void);
1735 void    g_set_prgname           (const gchar *prgname);
1736
1737
1738 /* Miscellaneous utility functions
1739  */
1740 guint   g_parse_debug_string    (const gchar *string,
1741                                  GDebugKey   *keys,
1742                                  guint        nkeys);
1743 gint    g_snprintf              (gchar       *string,
1744                                  gulong       n,
1745                                  gchar const *format,
1746                                  ...) G_GNUC_PRINTF (3, 4);
1747 gint    g_vsnprintf             (gchar       *string,
1748                                  gulong       n,
1749                                  gchar const *format,
1750                                  va_list      args);
1751 /* Check if a file name is an absolute path */
1752 gboolean g_path_is_absolute     (const gchar *file_name);
1753 /* In case of absolute paths, skip the root part */
1754 gchar*  g_path_skip_root        (gchar       *file_name);
1755
1756 /* These two functions are deprecated and will be removed in the next
1757  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
1758  * instead. Whatch out! The string returned by g_path_get_basename
1759  * must be g_freed, while the string returned by g_basename must not.*/
1760 gchar*  g_basename              (const gchar *file_name);
1761 gchar*  g_dirname               (const gchar *file_name);
1762
1763 /* The returned strings are newly allocated with g_malloc() */
1764 gchar*  g_get_current_dir       (void);
1765 gchar*  g_path_get_basename     (const gchar *file_name);
1766 gchar*  g_path_get_dirname      (const gchar *file_name);
1767
1768 /* Get the codeset for the current locale */
1769 /* gchar * g_get_codeset    (void); */
1770
1771 /* return the environment string for the variable. The returned memory
1772  * must not be freed. */
1773 gchar*  g_getenv                (const gchar *variable);
1774
1775 /* we use a GLib function as a replacement for ATEXIT, so
1776  * the programmer is not required to check the return value
1777  * (if there is any in the implementation) and doesn't encounter
1778  * missing include files.
1779  */
1780 void    g_atexit                (GVoidFunc    func);
1781
1782
1783 /* Bit tests
1784  */
1785 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1786                                        gint    nth_bit);
1787 #ifdef  G_CAN_INLINE
1788 G_INLINE_FUNC gint
1789 g_bit_nth_lsf (guint32 mask,
1790                gint    nth_bit)
1791 {
1792   do
1793     {
1794       nth_bit++;
1795       if (mask & (1 << (guint) nth_bit))
1796         return nth_bit;
1797     }
1798   while (nth_bit < 32);
1799   return -1;
1800 }
1801 #endif  /* G_CAN_INLINE */
1802
1803 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1804                                        gint    nth_bit);
1805 #ifdef G_CAN_INLINE
1806 G_INLINE_FUNC gint
1807 g_bit_nth_msf (guint32 mask,
1808                gint    nth_bit)
1809 {
1810   if (nth_bit < 0)
1811     nth_bit = 32;
1812   do
1813     {
1814       nth_bit--;
1815       if (mask & (1 << (guint) nth_bit))
1816         return nth_bit;
1817     }
1818   while (nth_bit > 0);
1819   return -1;
1820 }
1821 #endif  /* G_CAN_INLINE */
1822
1823 G_INLINE_FUNC guint     g_bit_storage (guint number);
1824 #ifdef G_CAN_INLINE
1825 G_INLINE_FUNC guint
1826 g_bit_storage (guint number)
1827 {
1828   register guint n_bits = 0;
1829   
1830   do
1831     {
1832       n_bits++;
1833       number >>= 1;
1834     }
1835   while (number);
1836   return n_bits;
1837 }
1838 #endif  /* G_CAN_INLINE */
1839
1840
1841 /* Trash Stacks
1842  * elements need to be >= sizeof (gpointer)
1843  */
1844 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
1845                                                  gpointer      data_p);
1846 #ifdef G_CAN_INLINE
1847 G_INLINE_FUNC void
1848 g_trash_stack_push (GTrashStack **stack_p,
1849                     gpointer      data_p)
1850 {
1851   GTrashStack *data = (GTrashStack *) data_p;
1852
1853   data->next = *stack_p;
1854   *stack_p = data;
1855 }
1856 #endif  /* G_CAN_INLINE */
1857
1858 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
1859 #ifdef G_CAN_INLINE
1860 G_INLINE_FUNC gpointer
1861 g_trash_stack_pop (GTrashStack **stack_p)
1862 {
1863   GTrashStack *data;
1864
1865   data = *stack_p;
1866   if (data)
1867     {
1868       *stack_p = data->next;
1869       /* NULLify private pointer here, most platforms store NULL as
1870        * subsequent 0 bytes
1871        */
1872       data->next = NULL;
1873     }
1874
1875   return data;
1876 }
1877 #endif  /* G_CAN_INLINE */
1878
1879 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
1880 #ifdef G_CAN_INLINE
1881 G_INLINE_FUNC gpointer
1882 g_trash_stack_peek (GTrashStack **stack_p)
1883 {
1884   GTrashStack *data;
1885
1886   data = *stack_p;
1887
1888   return data;
1889 }
1890 #endif  /* G_CAN_INLINE */
1891
1892 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
1893 #ifdef G_CAN_INLINE
1894 G_INLINE_FUNC guint
1895 g_trash_stack_height (GTrashStack **stack_p)
1896 {
1897   GTrashStack *data;
1898   guint i = 0;
1899
1900   for (data = *stack_p; data; data = data->next)
1901     i++;
1902
1903   return i;
1904 }
1905 #endif  /* G_CAN_INLINE */
1906
1907
1908 /* String Chunks
1909  */
1910 GStringChunk* g_string_chunk_new           (gint size);
1911 void          g_string_chunk_free          (GStringChunk *chunk);
1912 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1913                                             const gchar  *string);
1914 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1915                                             const gchar  *string);
1916
1917
1918 /* Strings
1919  */
1920 GString*     g_string_new               (const gchar     *init);
1921 GString*     g_string_sized_new         (guint            dfl_size);
1922 void         g_string_free              (GString         *string,
1923                                          gboolean         free_segment);
1924 gboolean     g_string_equal             (const GString   *v,
1925                                          const GString   *v2);
1926 guint        g_string_hash              (const GString   *str);
1927 GString*     g_string_assign            (GString         *string,
1928                                          const gchar     *rval);
1929 GString*     g_string_truncate          (GString         *string,
1930                                          guint            len);
1931 GString*     g_string_insert_len        (GString         *string,
1932                                          gint             pos,
1933                                          const gchar     *val,
1934                                          gint             len);
1935 GString*     g_string_append            (GString         *string,
1936                                          const gchar     *val);
1937 GString*     g_string_append_len        (GString         *string,
1938                                          const gchar     *val,
1939                                          gint             len);
1940 GString*     g_string_append_c          (GString         *string,
1941                                          gchar            c);
1942 GString*     g_string_prepend           (GString         *string,
1943                                          const gchar     *val);
1944 GString*     g_string_prepend_c         (GString         *string,
1945                                          gchar            c);
1946 GString*     g_string_prepend_len       (GString         *string,
1947                                          const gchar     *val,
1948                                          gint             len);
1949 GString*     g_string_insert            (GString         *string,
1950                                          gint             pos,
1951                                          const gchar     *val);
1952 GString*     g_string_insert_c          (GString         *string,
1953                                          gint             pos,
1954                                          gchar            c);
1955 GString*     g_string_erase             (GString         *string,
1956                                          gint             pos,
1957                                          gint             len);
1958 GString*     g_string_down              (GString         *string);
1959 GString*     g_string_up                (GString         *string);
1960 void         g_string_sprintf           (GString         *string,
1961                                          const gchar     *format,
1962                                          ...) G_GNUC_PRINTF (2, 3);
1963 void         g_string_sprintfa          (GString         *string,
1964                                          const gchar     *format,
1965                                          ...) G_GNUC_PRINTF (2, 3);
1966
1967
1968 /* Resizable arrays, remove fills any cleared spot and shortens the
1969  * array, while preserving the order. remove_fast will distort the
1970  * order by moving the last element to the position of the removed 
1971  */
1972
1973 #define g_array_append_val(a,v)   g_array_append_vals (a, &v, 1)
1974 #define g_array_prepend_val(a,v)  g_array_prepend_vals (a, &v, 1)
1975 #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
1976 #define g_array_index(a,t,i)      (((t*) (a)->data) [(i)])
1977
1978 GArray* g_array_new               (gboolean         zero_terminated,
1979                                    gboolean         clear,
1980                                    guint            element_size);
1981 GArray* g_array_sized_new         (gboolean         zero_terminated,
1982                                    gboolean         clear,
1983                                    guint            element_size,
1984                                    guint            reserved_size);
1985 void    g_array_free              (GArray          *array,
1986                                    gboolean         free_segment);
1987 GArray* g_array_append_vals       (GArray          *array,
1988                                    gconstpointer    data,
1989                                    guint            len);
1990 GArray* g_array_prepend_vals      (GArray          *array,
1991                                    gconstpointer    data,
1992                                    guint            len);
1993 GArray* g_array_insert_vals       (GArray          *array,
1994                                    guint            index,
1995                                    gconstpointer    data,
1996                                    guint            len);
1997 GArray* g_array_set_size          (GArray          *array,
1998                                    guint            length);
1999 GArray* g_array_remove_index      (GArray          *array,
2000                                    guint            index);
2001 GArray* g_array_remove_index_fast (GArray          *array,
2002                                    guint            index);
2003
2004 /* Resizable pointer array.  This interface is much less complicated
2005  * than the above.  Add appends appends a pointer.  Remove fills any
2006  * cleared spot and shortens the array. remove_fast will again distort
2007  * order.  
2008  */
2009 #define     g_ptr_array_index(array,index) (array->pdata)[index]
2010 GPtrArray*  g_ptr_array_new                (void);
2011 GPtrArray*  g_ptr_array_sized_new          (guint        reserved_size);
2012 void        g_ptr_array_free               (GPtrArray   *array,
2013                                             gboolean     free_seg);
2014 void        g_ptr_array_set_size           (GPtrArray   *array,
2015                                             gint         length);
2016 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
2017                                             guint        index);
2018 gpointer    g_ptr_array_remove_index_fast  (GPtrArray   *array,
2019                                             guint        index);
2020 gboolean    g_ptr_array_remove             (GPtrArray   *array,
2021                                             gpointer     data);
2022 gboolean    g_ptr_array_remove_fast        (GPtrArray   *array,
2023                                             gpointer     data);
2024 void        g_ptr_array_add                (GPtrArray   *array,
2025                                             gpointer     data);
2026
2027 /* Byte arrays, an array of guint8.  Implemented as a GArray,
2028  * but type-safe.
2029  */
2030
2031 GByteArray* g_byte_array_new               (void);
2032 GByteArray* g_byte_array_sized_new         (guint        reserved_size);
2033 void        g_byte_array_free              (GByteArray   *array,
2034                                             gboolean      free_segment);
2035 GByteArray* g_byte_array_append            (GByteArray   *array,
2036                                             const guint8 *data,
2037                                             guint         len);
2038 GByteArray* g_byte_array_prepend           (GByteArray   *array,
2039                                             const guint8 *data,
2040                                             guint         len);
2041 GByteArray* g_byte_array_set_size          (GByteArray   *array,
2042                                             guint         length);
2043 GByteArray* g_byte_array_remove_index      (GByteArray   *array,
2044                                             guint         index);
2045 GByteArray* g_byte_array_remove_index_fast (GByteArray   *array,
2046                                             guint         index);
2047
2048
2049 /* Hash Functions
2050  */
2051 gboolean g_str_equal (gconstpointer   v,
2052                       gconstpointer   v2);
2053 guint    g_str_hash  (gconstpointer   v);
2054
2055 gint     g_int_equal (gconstpointer   v,
2056                       gconstpointer   v2);
2057 guint    g_int_hash  (gconstpointer   v);
2058
2059 /* This "hash" function will just return the key's adress as an
2060  * unsigned integer. Useful for hashing on plain adresses or
2061  * simple integer values.
2062  * passing NULL into g_hash_table_new() as GHashFunc has the
2063  * same effect as passing g_direct_hash().
2064  */
2065 guint g_direct_hash  (gconstpointer v);
2066 gint  g_direct_equal (gconstpointer v,
2067                       gconstpointer v2);
2068
2069
2070 /* Quarks (string<->id association)
2071  */
2072 GQuark    g_quark_try_string            (const gchar    *string);
2073 GQuark    g_quark_from_static_string    (const gchar    *string);
2074 GQuark    g_quark_from_string           (const gchar    *string);
2075 gchar*    g_quark_to_string             (GQuark          quark);
2076
2077
2078 /* Keyed Data List
2079  */
2080 void      g_datalist_init                (GData          **datalist);
2081 void      g_datalist_clear               (GData          **datalist);
2082 gpointer  g_datalist_id_get_data         (GData          **datalist,
2083                                           GQuark           key_id);
2084 void      g_datalist_id_set_data_full    (GData          **datalist,
2085                                           GQuark           key_id,
2086                                           gpointer         data,
2087                                           GDestroyNotify   destroy_func);
2088 gpointer  g_datalist_id_remove_no_notify (GData          **datalist,
2089                                           GQuark           key_id);
2090 void      g_datalist_foreach             (GData          **datalist,
2091                                           GDataForeachFunc func,
2092                                           gpointer         user_data);
2093 #define   g_datalist_id_set_data(dl, q, d)      \
2094      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
2095 #define   g_datalist_id_remove_data(dl, q)      \
2096      g_datalist_id_set_data ((dl), (q), NULL)
2097 #define   g_datalist_get_data(dl, k)            \
2098      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
2099 #define   g_datalist_set_data_full(dl, k, d, f) \
2100      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
2101 #define   g_datalist_remove_no_notify(dl, k)    \
2102      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
2103 #define   g_datalist_set_data(dl, k, d)         \
2104      g_datalist_set_data_full ((dl), (k), (d), NULL)
2105 #define   g_datalist_remove_data(dl, k)         \
2106      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
2107
2108
2109 /* Location Associated Keyed Data
2110  */
2111 void      g_dataset_destroy             (gconstpointer    dataset_location);
2112 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
2113                                          GQuark           key_id);
2114 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
2115                                          GQuark           key_id,
2116                                          gpointer         data,
2117                                          GDestroyNotify   destroy_func);
2118 gpointer  g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
2119                                          GQuark           key_id);
2120 void      g_dataset_foreach             (gconstpointer    dataset_location,
2121                                          GDataForeachFunc func,
2122                                          gpointer         user_data);
2123 #define   g_dataset_id_set_data(l, k, d)        \
2124      g_dataset_id_set_data_full ((l), (k), (d), NULL)
2125 #define   g_dataset_id_remove_data(l, k)        \
2126      g_dataset_id_set_data ((l), (k), NULL)
2127 #define   g_dataset_get_data(l, k)              \
2128      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
2129 #define   g_dataset_set_data_full(l, k, d, f)   \
2130      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
2131 #define   g_dataset_remove_no_notify(l, k)      \
2132      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
2133 #define   g_dataset_set_data(l, k, d)           \
2134      g_dataset_set_data_full ((l), (k), (d), NULL)
2135 #define   g_dataset_remove_data(l, k)           \
2136      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
2137
2138
2139 /* GScanner: Flexible lexical scanner for general purpose.
2140  */
2141
2142 /* Character sets */
2143 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2144 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
2145 #define G_CSET_DIGITS   "0123456789"
2146 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
2147                         "\307\310\311\312\313\314\315\316\317\320"\
2148                         "\321\322\323\324\325\326"\
2149                         "\330\331\332\333\334\335\336"
2150 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
2151                         "\347\350\351\352\353\354\355\356\357\360"\
2152                         "\361\362\363\364\365\366"\
2153                         "\370\371\372\373\374\375\376\377"
2154
2155 /* Error types */
2156 typedef enum
2157 {
2158   G_ERR_UNKNOWN,
2159   G_ERR_UNEXP_EOF,
2160   G_ERR_UNEXP_EOF_IN_STRING,
2161   G_ERR_UNEXP_EOF_IN_COMMENT,
2162   G_ERR_NON_DIGIT_IN_CONST,
2163   G_ERR_DIGIT_RADIX,
2164   G_ERR_FLOAT_RADIX,
2165   G_ERR_FLOAT_MALFORMED
2166 } GErrorType;
2167
2168 /* Token types */
2169 typedef enum
2170 {
2171   G_TOKEN_EOF                   =   0,
2172   
2173   G_TOKEN_LEFT_PAREN            = '(',
2174   G_TOKEN_RIGHT_PAREN           = ')',
2175   G_TOKEN_LEFT_CURLY            = '{',
2176   G_TOKEN_RIGHT_CURLY           = '}',
2177   G_TOKEN_LEFT_BRACE            = '[',
2178   G_TOKEN_RIGHT_BRACE           = ']',
2179   G_TOKEN_EQUAL_SIGN            = '=',
2180   G_TOKEN_COMMA                 = ',',
2181   
2182   G_TOKEN_NONE                  = 256,
2183   
2184   G_TOKEN_ERROR,
2185   
2186   G_TOKEN_CHAR,
2187   G_TOKEN_BINARY,
2188   G_TOKEN_OCTAL,
2189   G_TOKEN_INT,
2190   G_TOKEN_HEX,
2191   G_TOKEN_FLOAT,
2192   G_TOKEN_STRING,
2193   
2194   G_TOKEN_SYMBOL,
2195   G_TOKEN_IDENTIFIER,
2196   G_TOKEN_IDENTIFIER_NULL,
2197   
2198   G_TOKEN_COMMENT_SINGLE,
2199   G_TOKEN_COMMENT_MULTI,
2200   G_TOKEN_LAST
2201 } GTokenType;
2202
2203 union   _GTokenValue
2204 {
2205   gpointer      v_symbol;
2206   gchar         *v_identifier;
2207   gulong        v_binary;
2208   gulong        v_octal;
2209   gulong        v_int;
2210   gdouble       v_float;
2211   gulong        v_hex;
2212   gchar         *v_string;
2213   gchar         *v_comment;
2214   guchar        v_char;
2215   guint         v_error;
2216 };
2217
2218 struct  _GScannerConfig
2219 {
2220   /* Character sets
2221    */
2222   gchar         *cset_skip_characters;          /* default: " \t\n" */
2223   gchar         *cset_identifier_first;
2224   gchar         *cset_identifier_nth;
2225   gchar         *cpair_comment_single;          /* default: "#\n" */
2226   
2227   /* Should symbol lookup work case sensitive?
2228    */
2229   guint         case_sensitive : 1;
2230   
2231   /* Boolean values to be adjusted "on the fly"
2232    * to configure scanning behaviour.
2233    */
2234   guint         skip_comment_multi : 1;         /* C like comment */
2235   guint         skip_comment_single : 1;        /* single line comment */
2236   guint         scan_comment_multi : 1;         /* scan multi line comments? */
2237   guint         scan_identifier : 1;
2238   guint         scan_identifier_1char : 1;
2239   guint         scan_identifier_NULL : 1;
2240   guint         scan_symbols : 1;
2241   guint         scan_binary : 1;
2242   guint         scan_octal : 1;
2243   guint         scan_float : 1;
2244   guint         scan_hex : 1;                   /* `0x0ff0' */
2245   guint         scan_hex_dollar : 1;            /* `$0ff0' */
2246   guint         scan_string_sq : 1;             /* string: 'anything' */
2247   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
2248   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
2249   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
2250   guint         identifier_2_string : 1;
2251   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
2252   guint         symbol_2_token : 1;
2253   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
2254 };
2255
2256 struct  _GScanner
2257 {
2258   /* unused fields */
2259   gpointer              user_data;
2260   guint                 max_parse_errors;
2261   
2262   /* g_scanner_error() increments this field */
2263   guint                 parse_errors;
2264   
2265   /* name of input stream, featured by the default message handler */
2266   const gchar           *input_name;
2267   
2268   /* data pointer for derived structures */
2269   gpointer              derived_data;
2270   
2271   /* link into the scanner configuration */
2272   GScannerConfig        *config;
2273   
2274   /* fields filled in after g_scanner_get_next_token() */
2275   GTokenType            token;
2276   GTokenValue           value;
2277   guint                 line;
2278   guint                 position;
2279   
2280   /* fields filled in after g_scanner_peek_next_token() */
2281   GTokenType            next_token;
2282   GTokenValue           next_value;
2283   guint                 next_line;
2284   guint                 next_position;
2285   
2286   /* to be considered private */
2287   GHashTable            *symbol_table;
2288   gint                  input_fd;
2289   const gchar           *text;
2290   const gchar           *text_end;
2291   gchar                 *buffer;
2292   guint                 scope_id;
2293   
2294   /* handler function for _warn and _error */
2295   GScannerMsgFunc       msg_handler;
2296 };
2297
2298 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
2299 void            g_scanner_destroy               (GScanner       *scanner);
2300 void            g_scanner_input_file            (GScanner       *scanner,
2301                                                  gint           input_fd);
2302 void            g_scanner_sync_file_offset      (GScanner       *scanner);
2303 void            g_scanner_input_text            (GScanner       *scanner,
2304                                                  const  gchar   *text,
2305                                                  guint          text_len);
2306 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
2307 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
2308 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
2309 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
2310 guint           g_scanner_cur_line              (GScanner       *scanner);
2311 guint           g_scanner_cur_position          (GScanner       *scanner);
2312 gboolean        g_scanner_eof                   (GScanner       *scanner);
2313 guint           g_scanner_set_scope             (GScanner       *scanner,
2314                                                  guint           scope_id);
2315 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
2316                                                  guint           scope_id,
2317                                                  const gchar    *symbol,
2318                                                  gpointer       value);
2319 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
2320                                                  guint           scope_id,
2321                                                  const gchar    *symbol);
2322 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
2323                                                  guint           scope_id,
2324                                                  const gchar    *symbol);
2325 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
2326                                                  guint           scope_id,
2327                                                  GHFunc          func,
2328                                                  gpointer        user_data);
2329 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
2330                                                  const gchar    *symbol);
2331 void            g_scanner_unexp_token           (GScanner       *scanner,
2332                                                  GTokenType     expected_token,
2333                                                  const gchar    *identifier_spec,
2334                                                  const gchar    *symbol_spec,
2335                                                  const gchar    *symbol_name,
2336                                                  const gchar    *message,
2337                                                  gint            is_error);
2338 void            g_scanner_error                 (GScanner       *scanner,
2339                                                  const gchar    *format,
2340                                                  ...) G_GNUC_PRINTF (2,3);
2341 void            g_scanner_warn                  (GScanner       *scanner,
2342                                                  const gchar    *format,
2343                                                  ...) G_GNUC_PRINTF (2,3);
2344 gint            g_scanner_stat_mode             (const gchar    *filename);
2345 /* keep downward source compatibility */
2346 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
2347   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2348 } G_STMT_END
2349 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
2350   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2351 } G_STMT_END
2352 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2353   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2354 } G_STMT_END
2355
2356 /* The following two functions are deprecated and will be removed in
2357  * the next major release. They do no good. */
2358 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
2359 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
2360
2361 /* GCompletion
2362  */
2363
2364 struct _GCompletion
2365 {
2366   GList* items;
2367   GCompletionFunc func;
2368   
2369   gchar* prefix;
2370   GList* cache;
2371 };
2372
2373 GCompletion* g_completion_new          (GCompletionFunc func);
2374 void         g_completion_add_items    (GCompletion*    cmp,
2375                                         GList*          items);
2376 void         g_completion_remove_items (GCompletion*    cmp,
2377                                         GList*          items);
2378 void         g_completion_clear_items  (GCompletion*    cmp);
2379 GList*       g_completion_complete     (GCompletion*    cmp,
2380                                         gchar*          prefix,
2381                                         gchar**         new_prefix);
2382 void         g_completion_free         (GCompletion*    cmp);
2383
2384
2385 /* GDate
2386  *
2387  * Date calculations (not time for now, to be resolved). These are a
2388  * mutant combination of Steffen Beyer's DateCalc routines
2389  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2390  * date routines (written for in-house software).  Written by Havoc
2391  * Pennington <hp@pobox.com> 
2392  */
2393
2394 typedef guint16 GDateYear;
2395 typedef guint8  GDateDay;   /* day of the month */
2396 typedef struct _GDate GDate;
2397 /* make struct tm known without having to include time.h */
2398 struct tm;
2399
2400 /* enum used to specify order of appearance in parsed date strings */
2401 typedef enum
2402 {
2403   G_DATE_DAY   = 0,
2404   G_DATE_MONTH = 1,
2405   G_DATE_YEAR  = 2
2406 } GDateDMY;
2407
2408 /* actual week and month values */
2409 typedef enum
2410 {
2411   G_DATE_BAD_WEEKDAY  = 0,
2412   G_DATE_MONDAY       = 1,
2413   G_DATE_TUESDAY      = 2,
2414   G_DATE_WEDNESDAY    = 3,
2415   G_DATE_THURSDAY     = 4,
2416   G_DATE_FRIDAY       = 5,
2417   G_DATE_SATURDAY     = 6,
2418   G_DATE_SUNDAY       = 7
2419 } GDateWeekday;
2420 typedef enum
2421 {
2422   G_DATE_BAD_MONTH = 0,
2423   G_DATE_JANUARY   = 1,
2424   G_DATE_FEBRUARY  = 2,
2425   G_DATE_MARCH     = 3,
2426   G_DATE_APRIL     = 4,
2427   G_DATE_MAY       = 5,
2428   G_DATE_JUNE      = 6,
2429   G_DATE_JULY      = 7,
2430   G_DATE_AUGUST    = 8,
2431   G_DATE_SEPTEMBER = 9,
2432   G_DATE_OCTOBER   = 10,
2433   G_DATE_NOVEMBER  = 11,
2434   G_DATE_DECEMBER  = 12
2435 } GDateMonth;
2436
2437 #define G_DATE_BAD_JULIAN 0U
2438 #define G_DATE_BAD_DAY    0U
2439 #define G_DATE_BAD_YEAR   0U
2440
2441 /* Note: directly manipulating structs is generally a bad idea, but
2442  * in this case it's an *incredibly* bad idea, because all or part
2443  * of this struct can be invalid at any given time. Use the functions,
2444  * or you will get hosed, I promise.
2445  */
2446 struct _GDate
2447
2448   guint julian_days : 32; /* julian days representation - we use a
2449                            *  bitfield hoping that 64 bit platforms
2450                            *  will pack this whole struct in one big
2451                            *  int 
2452                            */
2453
2454   guint julian : 1;    /* julian is valid */
2455   guint dmy    : 1;    /* dmy is valid */
2456
2457   /* DMY representation */
2458   guint day    : 6;  
2459   guint month  : 4; 
2460   guint year   : 16; 
2461 };
2462
2463 /* g_date_new() returns an invalid date, you then have to _set() stuff 
2464  * to get a usable object. You can also allocate a GDate statically,
2465  * then call g_date_clear() to initialize.
2466  */
2467 GDate*       g_date_new                   (void);
2468 GDate*       g_date_new_dmy               (GDateDay     day, 
2469                                            GDateMonth   month, 
2470                                            GDateYear    year);
2471 GDate*       g_date_new_julian            (guint32      julian_day);
2472 void         g_date_free                  (GDate       *date);
2473
2474 /* check g_date_valid() after doing an operation that might fail, like
2475  * _parse.  Almost all g_date operations are undefined on invalid
2476  * dates (the exceptions are the mutators, since you need those to
2477  * return to validity).  
2478  */
2479 gboolean     g_date_valid                 (GDate       *date);
2480 gboolean     g_date_valid_day             (GDateDay     day);
2481 gboolean     g_date_valid_month           (GDateMonth   month);
2482 gboolean     g_date_valid_year            (GDateYear    year);
2483 gboolean     g_date_valid_weekday         (GDateWeekday weekday);
2484 gboolean     g_date_valid_julian          (guint32      julian_date);
2485 gboolean     g_date_valid_dmy             (GDateDay     day,
2486                                            GDateMonth   month,
2487                                            GDateYear    year);
2488
2489 GDateWeekday g_date_weekday               (GDate       *date);
2490 GDateMonth   g_date_month                 (GDate       *date);
2491 GDateYear    g_date_year                  (GDate       *date);
2492 GDateDay     g_date_day                   (GDate       *date);
2493 guint32      g_date_julian                (GDate       *date);
2494 guint        g_date_day_of_year           (GDate       *date);
2495
2496 /* First monday/sunday is the start of week 1; if we haven't reached
2497  * that day, return 0. These are not ISO weeks of the year; that
2498  * routine needs to be added.
2499  * these functions return the number of weeks, starting on the
2500  * corrsponding day
2501  */
2502 guint        g_date_monday_week_of_year   (GDate      *date);
2503 guint        g_date_sunday_week_of_year   (GDate      *date);
2504
2505 /* If you create a static date struct you need to clear it to get it
2506  * in a sane state before use. You can clear a whole array at
2507  * once with the ndates argument.
2508  */
2509 void         g_date_clear                 (GDate       *date, 
2510                                            guint        n_dates);
2511
2512 /* The parse routine is meant for dates typed in by a user, so it
2513  * permits many formats but tries to catch common typos. If your data
2514  * needs to be strictly validated, it is not an appropriate function.
2515  */
2516 void         g_date_set_parse             (GDate       *date,
2517                                            const gchar *str);
2518 void         g_date_set_time              (GDate       *date, 
2519                                            GTime        time);
2520 void         g_date_set_month             (GDate       *date, 
2521                                            GDateMonth   month);
2522 void         g_date_set_day               (GDate       *date, 
2523                                            GDateDay     day);
2524 void         g_date_set_year              (GDate       *date,
2525                                            GDateYear    year);
2526 void         g_date_set_dmy               (GDate       *date,
2527                                            GDateDay     day,
2528                                            GDateMonth   month,
2529                                            GDateYear    y);
2530 void         g_date_set_julian            (GDate       *date,
2531                                            guint32      julian_date);
2532 gboolean     g_date_is_first_of_month     (GDate       *date);
2533 gboolean     g_date_is_last_of_month      (GDate       *date);
2534
2535 /* To go forward by some number of weeks just go forward weeks*7 days */
2536 void         g_date_add_days              (GDate       *date, 
2537                                            guint        n_days);
2538 void         g_date_subtract_days         (GDate       *date, 
2539                                            guint        n_days);
2540
2541 /* If you add/sub months while day > 28, the day might change */
2542 void         g_date_add_months            (GDate       *date,
2543                                            guint        n_months);
2544 void         g_date_subtract_months       (GDate       *date,
2545                                            guint        n_months);
2546
2547 /* If it's feb 29, changing years can move you to the 28th */
2548 void         g_date_add_years             (GDate       *date,
2549                                            guint        n_years);
2550 void         g_date_subtract_years        (GDate       *date,
2551                                            guint        n_years);
2552 gboolean     g_date_is_leap_year          (GDateYear    year);
2553 guint8       g_date_days_in_month         (GDateMonth   month, 
2554                                            GDateYear    year);
2555 guint8       g_date_monday_weeks_in_year  (GDateYear    year);
2556 guint8       g_date_sunday_weeks_in_year  (GDateYear    year);
2557
2558 /* qsort-friendly (with a cast...) */
2559 gint         g_date_compare               (GDate       *lhs,
2560                                            GDate       *rhs);
2561 void         g_date_to_struct_tm          (GDate       *date,
2562                                            struct tm   *tm);
2563
2564 /* Just like strftime() except you can only use date-related formats.
2565  *   Using a time format is undefined.
2566  */
2567 gsize        g_date_strftime              (gchar       *s,
2568                                            gsize        slen,
2569                                            const gchar *format,
2570                                            GDate       *date);
2571
2572
2573 /* GRelation
2574  *
2575  * Indexed Relations.  Imagine a really simple table in a
2576  * database.  Relations are not ordered.  This data type is meant for
2577  * maintaining a N-way mapping.
2578  *
2579  * g_relation_new() creates a relation with FIELDS fields
2580  *
2581  * g_relation_destroy() frees all resources
2582  * g_tuples_destroy() frees the result of g_relation_select()
2583  *
2584  * g_relation_index() indexes relation FIELD with the provided
2585  *   equality and hash functions.  this must be done before any
2586  *   calls to insert are made.
2587  *
2588  * g_relation_insert() inserts a new tuple.  you are expected to
2589  *   provide the right number of fields.
2590  *
2591  * g_relation_delete() deletes all relations with KEY in FIELD
2592  * g_relation_select() returns ...
2593  * g_relation_count() counts ...
2594  */
2595
2596 GRelation* g_relation_new     (gint         fields);
2597 void       g_relation_destroy (GRelation   *relation);
2598 void       g_relation_index   (GRelation   *relation,
2599                                gint         field,
2600                                GHashFunc    hash_func,
2601                                GCompareFunc key_compare_func);
2602 void       g_relation_insert  (GRelation   *relation,
2603                                ...);
2604 gint       g_relation_delete  (GRelation   *relation,
2605                                gconstpointer  key,
2606                                gint         field);
2607 GTuples*   g_relation_select  (GRelation   *relation,
2608                                gconstpointer  key,
2609                                gint         field);
2610 gint       g_relation_count   (GRelation   *relation,
2611                                gconstpointer  key,
2612                                gint         field);
2613 gboolean   g_relation_exists  (GRelation   *relation,
2614                                ...);
2615 void       g_relation_print   (GRelation   *relation);
2616
2617 void       g_tuples_destroy   (GTuples     *tuples);
2618 gpointer   g_tuples_index     (GTuples     *tuples,
2619                                gint         index,
2620                                gint         field);
2621
2622
2623 /* GRand - a good and fast random number generator: Mersenne Twister 
2624  * see http://www.math.keio.ac.jp/~matumoto/emt.html for more info.
2625  * The range functions return a value in the intervall [min,max).
2626  * int          -> [0..2^32-1]
2627  * int_range    -> [min..max-1]
2628  * double       -> [0..1)
2629  * double_range -> [min..max)
2630  */
2631
2632 GRand*  g_rand_new_with_seed   (guint32     seed);
2633 GRand*  g_rand_new             (void);
2634 void    g_rand_free            (GRand      *rand);
2635
2636 void    g_rand_set_seed        (GRand      *rand, 
2637                                 guint32     seed);
2638 guint32 g_rand_int             (GRand      *rand);
2639 gint32  g_rand_int_range       (GRand      *rand, 
2640                                 gint32      min, 
2641                                 gint32      max);
2642 gdouble g_rand_double          (GRand      *rand);
2643 gdouble g_rand_double_range    (GRand      *rand, 
2644                                 gdouble     min, 
2645                                 gdouble     max);
2646
2647 void    g_random_set_seed      (guint32     seed);
2648 guint32 g_random_int           (void);
2649 gint32  g_random_int_range     (gint32      min, 
2650                                 gint32      max);
2651 gdouble g_random_double        (void);
2652 gdouble g_random_double_range  (gdouble     min, 
2653                                 gdouble     max);
2654  
2655
2656 /* Prime numbers.
2657  */
2658
2659 /* This function returns prime numbers spaced by approximately 1.5-2.0
2660  * and is for use in resizing data structures which prefer
2661  * prime-valued sizes.  The closest spaced prime function returns the
2662  * next largest prime, or the highest it knows about which is about
2663  * MAXINT/4.
2664  */
2665 guint      g_spaced_primes_closest (guint num);
2666
2667
2668 /* GIOChannel
2669  */
2670
2671 typedef struct _GIOFuncs GIOFuncs;
2672 typedef enum
2673 {
2674   G_IO_ERROR_NONE,
2675   G_IO_ERROR_AGAIN,
2676   G_IO_ERROR_INVAL,
2677   G_IO_ERROR_UNKNOWN
2678 } GIOError;
2679 typedef enum
2680 {
2681   G_SEEK_CUR,
2682   G_SEEK_SET,
2683   G_SEEK_END
2684 } GSeekType;
2685 typedef enum
2686 {
2687   G_IO_IN       GLIB_SYSDEF_POLLIN,
2688   G_IO_OUT      GLIB_SYSDEF_POLLOUT,
2689   G_IO_PRI      GLIB_SYSDEF_POLLPRI,
2690   G_IO_ERR      GLIB_SYSDEF_POLLERR,
2691   G_IO_HUP      GLIB_SYSDEF_POLLHUP,
2692   G_IO_NVAL     GLIB_SYSDEF_POLLNVAL
2693 } GIOCondition;
2694
2695 struct _GIOChannel
2696 {
2697   guint channel_flags;
2698   guint ref_count;
2699   GIOFuncs *funcs;
2700 };
2701
2702 typedef gboolean (*GIOFunc) (GIOChannel   *source,
2703                              GIOCondition  condition,
2704                              gpointer      data);
2705 struct _GIOFuncs
2706 {
2707   GIOError (*io_read)   (GIOChannel     *channel, 
2708                          gchar          *buf, 
2709                          guint           count,
2710                          guint          *bytes_read);
2711   GIOError (*io_write)  (GIOChannel     *channel, 
2712                          gchar          *buf, 
2713                          guint           count,
2714                          guint          *bytes_written);
2715   GIOError (*io_seek)   (GIOChannel     *channel, 
2716                          gint            offset, 
2717                          GSeekType       type);
2718   void (*io_close)      (GIOChannel     *channel);
2719   guint (*io_add_watch) (GIOChannel     *channel,
2720                          gint            priority,
2721                          GIOCondition    condition,
2722                          GIOFunc         func,
2723                          gpointer        user_data,
2724                          GDestroyNotify  notify);
2725   void (*io_free)       (GIOChannel     *channel);
2726 };
2727
2728 void        g_io_channel_init   (GIOChannel    *channel);
2729 void        g_io_channel_ref    (GIOChannel    *channel);
2730 void        g_io_channel_unref  (GIOChannel    *channel);
2731 GIOError    g_io_channel_read   (GIOChannel    *channel, 
2732                                  gchar         *buf, 
2733                                  guint          count,
2734                                  guint         *bytes_read);
2735 GIOError  g_io_channel_write    (GIOChannel    *channel, 
2736                                  gchar         *buf, 
2737                                  guint          count,
2738                                  guint         *bytes_written);
2739 GIOError  g_io_channel_seek     (GIOChannel    *channel,
2740                                  gint           offset, 
2741                                  GSeekType      type);
2742 void      g_io_channel_close    (GIOChannel    *channel);
2743 guint     g_io_add_watch_full   (GIOChannel    *channel,
2744                                  gint           priority,
2745                                  GIOCondition   condition,
2746                                  GIOFunc        func,
2747                                  gpointer       user_data,
2748                                  GDestroyNotify notify);
2749 guint    g_io_add_watch         (GIOChannel    *channel,
2750                                  GIOCondition   condition,
2751                                  GIOFunc        func,
2752                                  gpointer       user_data);
2753
2754
2755 /* Main loop
2756  */
2757 typedef struct _GTimeVal        GTimeVal;
2758 typedef struct _GSourceFuncs    GSourceFuncs;
2759 typedef struct _GMainLoop       GMainLoop;      /* Opaque */
2760
2761 struct _GTimeVal
2762 {
2763   glong tv_sec;
2764   glong tv_usec;
2765 };
2766 struct _GSourceFuncs
2767 {
2768   gboolean (*prepare)  (gpointer  source_data, 
2769                         GTimeVal *current_time,
2770                         gint     *timeout,
2771                         gpointer  user_data);
2772   gboolean (*check)    (gpointer  source_data,
2773                         GTimeVal *current_time,
2774                         gpointer  user_data);
2775   gboolean (*dispatch) (gpointer  source_data, 
2776                         GTimeVal *dispatch_time,
2777                         gpointer  user_data);
2778   GDestroyNotify destroy;
2779 };
2780
2781 /* Standard priorities */
2782
2783 #define G_PRIORITY_HIGH            -100
2784 #define G_PRIORITY_DEFAULT          0
2785 #define G_PRIORITY_HIGH_IDLE        100
2786 #define G_PRIORITY_DEFAULT_IDLE     200
2787 #define G_PRIORITY_LOW              300
2788
2789 typedef gboolean (*GSourceFunc) (gpointer data);
2790
2791 /* Hooks for adding to the main loop */
2792 guint    g_source_add                        (gint           priority, 
2793                                               gboolean       can_recurse,
2794                                               GSourceFuncs  *funcs,
2795                                               gpointer       source_data, 
2796                                               gpointer       user_data,
2797                                               GDestroyNotify notify);
2798 gboolean g_source_remove                     (guint          tag);
2799 gboolean g_source_remove_by_user_data        (gpointer       user_data);
2800 gboolean g_source_remove_by_source_data      (gpointer       source_data);
2801 gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
2802                                               gpointer       user_data);
2803
2804 void g_get_current_time                 (GTimeVal       *result);
2805
2806 /* Running the main loop */
2807 GMainLoop*      g_main_new              (gboolean        is_running);
2808 void            g_main_run              (GMainLoop      *loop);
2809 void            g_main_quit             (GMainLoop      *loop);
2810 void            g_main_destroy          (GMainLoop      *loop);
2811 gboolean        g_main_is_running       (GMainLoop      *loop);
2812
2813 /* Run a single iteration of the mainloop. If block is FALSE,
2814  * will never block
2815  */
2816 gboolean        g_main_iteration        (gboolean       may_block);
2817
2818 /* See if any events are pending */
2819 gboolean        g_main_pending          (void);
2820
2821 /* Idles and timeouts */
2822 guint           g_timeout_add_full      (gint           priority,
2823                                          guint          interval, 
2824                                          GSourceFunc    function,
2825                                          gpointer       data,
2826                                          GDestroyNotify notify);
2827 guint           g_timeout_add           (guint          interval,
2828                                          GSourceFunc    function,
2829                                          gpointer       data);
2830 guint           g_idle_add              (GSourceFunc    function,
2831                                          gpointer       data);
2832 guint           g_idle_add_full         (gint           priority,
2833                                          GSourceFunc    function,
2834                                          gpointer       data,
2835                                          GDestroyNotify destroy);
2836 gboolean        g_idle_remove_by_data   (gpointer       data);
2837
2838 /* GPollFD
2839  *
2840  * System-specific IO and main loop calls
2841  *
2842  * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
2843  * descriptor as provided by the C runtime) that can be used by
2844  * MsgWaitForMultipleObjects. This does *not* include file handles
2845  * from CreateFile, SOCKETs, nor pipe handles. (But you can use
2846  * WSAEventSelect to signal events when a SOCKET is readable).
2847  *
2848  * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
2849  * indicate polling for messages. These message queue GPollFDs should
2850  * be added with the g_main_poll_win32_msg_add function.
2851  *
2852  * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
2853  * (GTK) programs, as GDK itself wants to read messages and convert them
2854  * to GDK events.
2855  *
2856  * So, unless you really know what you are doing, it's best not to try
2857  * to use the main loop polling stuff for your own needs on
2858  * Win32. It's really only written for the GIMP's needs so
2859  * far.
2860  */
2861
2862 typedef struct _GPollFD GPollFD;
2863 typedef gint    (*GPollFunc)    (GPollFD *ufds,
2864                                  guint    nfsd,
2865                                  gint     timeout);
2866 struct _GPollFD
2867 {
2868   gint          fd;
2869   gushort       events;
2870   gushort       revents;
2871 };
2872
2873 void        g_main_add_poll          (GPollFD    *fd,
2874                                       gint        priority);
2875 void        g_main_remove_poll       (GPollFD    *fd);
2876 void        g_main_set_poll_func     (GPollFunc   func);
2877
2878 /* On Unix, IO channels created with this function for any file
2879  * descriptor or socket.
2880  *
2881  * On Win32, use this only for plain files opened with the MSVCRT (the
2882  * Microsoft run-time C library) _open(), including file descriptors
2883  * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
2884  * Actually, don't do even that, this code isn't done yet.
2885  *
2886  * The term file descriptor as used in the context of Win32 refers to
2887  * the emulated Unix-like file descriptors MSVCRT provides.
2888  */
2889 GIOChannel* g_io_channel_unix_new    (int         fd);
2890 gint        g_io_channel_unix_get_fd (GIOChannel *channel);
2891
2892 #ifdef G_OS_WIN32
2893
2894 GLIB_VAR guint g_pipe_readable_msg;
2895
2896 #define G_WIN32_MSG_HANDLE 19981206
2897
2898 /* This is used to add polling for Windows messages. GDK (GTk+) programs
2899  * should *not* use this. (In fact, I can't think of any program that
2900  * would want to use this, but it's here just for completeness's sake.
2901  */
2902 void        g_main_poll_win32_msg_add(gint        priority,
2903                                       GPollFD    *fd,
2904                                       guint       hwnd);
2905
2906 /* An IO channel for Windows messages for window handle hwnd. */
2907 GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
2908
2909 /* An IO channel for an anonymous pipe as returned from the MSVCRT
2910  * _pipe(), with no mechanism for the writer to tell the reader when
2911  * there is data in the pipe.
2912  *
2913  * This is not really implemented yet.
2914  */
2915 GIOChannel *g_io_channel_win32_new_pipe (int fd);
2916
2917 /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
2918  * Windows user messages used to signal data in the pipe for the
2919  * reader.
2920  *
2921  * fd is the file descriptor. For the write end, peer is the thread id
2922  * of the reader, and peer_fd is his file descriptor for the read end
2923  * of the pipe.
2924  *
2925  * This is used by the GIMP, and works.
2926  */
2927 GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int   fd,
2928                                                       guint peer,
2929                                                       int   peer_fd);
2930
2931 void        g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
2932                                                      guint       peer,
2933                                                      int         peer_fd);
2934
2935 void        g_io_channel_win32_pipe_readable (int   fd,
2936                                               guint offset);
2937
2938 /* Get the C runtime file descriptor of a channel. */
2939 gint        g_io_channel_win32_get_fd (GIOChannel *channel);
2940
2941 /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
2942  * actually a SOCKET.
2943  */
2944 GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
2945
2946 #endif
2947
2948 /* Windows emulation stubs for common Unix functions
2949  */
2950 #ifdef G_OS_WIN32
2951 #  define MAXPATHLEN 1024
2952
2953 #ifdef _MSC_VER
2954 typedef int pid_t;
2955 #endif
2956
2957 /*
2958  * To get prototypes for the following POSIXish functions, you have to
2959  * include the indicated non-POSIX headers. The functions are defined
2960  * in OLDNAMES.LIB (MSVC) or -lmoldname-msvc (mingw32).
2961  *
2962  * getcwd: <direct.h> (MSVC), <io.h> (mingw32)
2963  * getpid: <process.h>
2964  * access: <io.h>
2965  * unlink: <stdio.h> or <io.h>
2966  * open, read, write, lseek, close: <io.h>
2967  * rmdir: <direct.h>
2968  * pipe: <direct.h>
2969  */
2970
2971 /* pipe is not in OLDNAMES.LIB or -lmoldname-msvc. */
2972 #define pipe(phandles)  _pipe (phandles, 4096, _O_BINARY)
2973
2974 /* For some POSIX functions that are not provided by the MS runtime,
2975  * we provide emulators in glib, which are prefixed with g_win32_.
2976  */
2977 #    define ftruncate(fd, size) g_win32_ftruncate (fd, size)
2978
2979 /* -lmingw32 also has emulations for these, but we need our own
2980  * for MSVC anyhow, so we might aswell use them always.
2981  */
2982 #    define opendir             g_win32_opendir
2983 #    define readdir             g_win32_readdir
2984 #    define rewinddir           g_win32_rewinddir
2985 #    define closedir            g_win32_closedir
2986 #    define NAME_MAX 255
2987
2988 struct DIR
2989 {
2990   gchar    *dir_name;
2991   gboolean  just_opened;
2992   guint     find_file_handle;
2993   gpointer  find_file_data;
2994 };
2995 typedef struct DIR DIR;
2996 struct dirent
2997 {
2998   gchar  d_name[NAME_MAX + 1];
2999 };
3000 /* emulation functions */
3001 extern int      g_win32_ftruncate       (gint            f,
3002                                          guint           size);
3003 DIR*            g_win32_opendir         (const gchar    *dirname);
3004 struct dirent*  g_win32_readdir         (DIR            *dir);
3005 void            g_win32_rewinddir       (DIR            *dir);
3006 gint            g_win32_closedir        (DIR            *dir);
3007
3008 /* The MS setlocale uses locale names of the form "English_United
3009  * States.1252" etc. We want the Unixish standard form "en", "zh_TW"
3010  * etc. This function gets the current thread locale from Windows and
3011  * returns it as a string of the above form for use in forming file
3012  * names etc. The returned string should be deallocated with g_free().
3013  */
3014 gchar *         g_win32_getlocale  (void);
3015
3016 /* Translate a Win32 error code (as returned by GetLastError()) into
3017  * the corresponding message. The returned string should be deallocated
3018  * with g_free().
3019  */
3020 gchar *         g_win32_error_message (gint error);
3021
3022 #endif   /* G_OS_WIN32 */
3023
3024
3025 /* GLib Thread support
3026  */
3027
3028 typedef void            (*GThreadFunc)          (gpointer       value);
3029
3030 typedef enum
3031 {
3032     G_THREAD_PRIORITY_LOW,
3033     G_THREAD_PRIORITY_NORMAL,
3034     G_THREAD_PRIORITY_HIGH,
3035     G_THREAD_PRIORITY_URGENT
3036 } GThreadPriority;
3037
3038 typedef struct _GThread         GThread;
3039 struct  _GThread
3040 {
3041   GThreadPriority priority;
3042   gboolean bound;
3043   gboolean joinable;
3044 };
3045
3046 typedef struct _GMutex          GMutex;
3047 typedef struct _GCond           GCond;
3048 typedef struct _GPrivate        GPrivate;
3049 typedef struct _GStaticPrivate  GStaticPrivate;
3050 typedef struct _GAsyncQueue     GAsyncQueue;
3051 typedef struct _GThreadPool     GThreadPool;
3052
3053 typedef struct _GThreadFunctions GThreadFunctions;
3054 struct _GThreadFunctions
3055 {
3056   GMutex*  (*mutex_new)           (void);
3057   void     (*mutex_lock)          (GMutex               *mutex);
3058   gboolean (*mutex_trylock)       (GMutex               *mutex);
3059   void     (*mutex_unlock)        (GMutex               *mutex);
3060   void     (*mutex_free)          (GMutex               *mutex);
3061   GCond*   (*cond_new)            (void);
3062   void     (*cond_signal)         (GCond                *cond);
3063   void     (*cond_broadcast)      (GCond                *cond);
3064   void     (*cond_wait)           (GCond                *cond,
3065                                    GMutex               *mutex);
3066   gboolean (*cond_timed_wait)     (GCond                *cond,
3067                                    GMutex               *mutex, 
3068                                    GTimeVal             *end_time);
3069   void      (*cond_free)          (GCond                *cond);
3070   GPrivate* (*private_new)        (GDestroyNotify        destructor);
3071   gpointer  (*private_get)        (GPrivate             *private_key);
3072   void      (*private_set)        (GPrivate             *private_key,
3073                                    gpointer              data);
3074   void      (*thread_create)      (GThreadFunc           thread_func,
3075                                    gpointer              arg,
3076                                    gulong                stack_size,
3077                                    gboolean              joinable,
3078                                    gboolean              bound,
3079                                    GThreadPriority       priority,
3080                                    gpointer              thread);
3081   void      (*thread_yield)       (void);
3082   void      (*thread_join)        (gpointer              thread);
3083   void      (*thread_exit)        (void);
3084   void      (*thread_set_priority)(gpointer              thread, 
3085                                    GThreadPriority       priority);
3086   void      (*thread_self)        (gpointer              thread);
3087 };
3088
3089 GLIB_VAR GThreadFunctions       g_thread_functions_for_glib_use;
3090 GLIB_VAR gboolean               g_thread_use_default_impl;
3091 GLIB_VAR gboolean               g_threads_got_initialized;
3092
3093 /* initializes the mutex/cond/private implementation for glib, might
3094  * only be called once, and must not be called directly or indirectly
3095  * from another glib-function, e.g. as a callback.
3096  */
3097 void    g_thread_init   (GThreadFunctions       *vtable);
3098
3099 /* internal function for fallback static mutex implementation */
3100 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
3101
3102 /* shorthands for conditional and unconditional function calls */
3103 #define G_THREAD_UF(name, arglist) \
3104     (*g_thread_functions_for_glib_use . name) arglist
3105 #define G_THREAD_CF(name, fail, arg) \
3106     (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
3107 /* keep in mind, all those mutexes and static mutexes are not 
3108  * recursive in general, don't rely on that
3109  */
3110 #define g_thread_supported()    (g_threads_got_initialized)
3111 #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
3112 #define g_mutex_lock(mutex)      G_THREAD_CF (mutex_lock,     (void)0, (mutex))
3113 #define g_mutex_trylock(mutex)   G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
3114 #define g_mutex_unlock(mutex)    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
3115 #define g_mutex_free(mutex)      G_THREAD_CF (mutex_free,     (void)0, (mutex))
3116 #define g_cond_new()             G_THREAD_UF (cond_new,       ())
3117 #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
3118 #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
3119 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait,      (void)0, (cond, \
3120                                                                         mutex))
3121 #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
3122 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
3123                                                               TRUE, \
3124                                                               (cond, mutex, \
3125                                                                abs_time))
3126 #define g_private_new(destructor)         G_THREAD_UF (private_new, (destructor))
3127 #define g_private_get(private_key)        G_THREAD_CF (private_get, \
3128                                                        ((gpointer)private_key), \
3129                                                        (private_key))
3130 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
3131                                                        (void) (private_key = \
3132                                                         (GPrivate*) (value)), \
3133                                                        (private_key, value))
3134 #define g_thread_yield()              G_THREAD_CF (thread_yield, (void)0, ())
3135 #define g_thread_exit()               G_THREAD_CF (thread_exit, (void)0, ())
3136
3137 GThread* g_thread_create (GThreadFunc            thread_func,
3138                           gpointer               arg,
3139                           gulong                 stack_size,
3140                           gboolean               joinable,
3141                           gboolean               bound,
3142                           GThreadPriority        priority);
3143 GThread* g_thread_self ();
3144 void g_thread_join (GThread* thread);
3145 void g_thread_set_priority (GThread* thread, 
3146                             GThreadPriority priority);
3147
3148 /* GStaticMutexes can be statically initialized with the value
3149  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
3150  * much easier, than having to explicitly allocate the mutex before
3151  * use
3152  */
3153 #define g_static_mutex_lock(mutex) \
3154     g_mutex_lock (g_static_mutex_get_mutex (mutex))
3155 #define g_static_mutex_trylock(mutex) \
3156     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
3157 #define g_static_mutex_unlock(mutex) \
3158     g_mutex_unlock (g_static_mutex_get_mutex (mutex)) 
3159
3160 struct _GStaticPrivate
3161 {
3162   guint index;
3163 };
3164 #define G_STATIC_PRIVATE_INIT { 0 }
3165 gpointer g_static_private_get (GStaticPrivate   *private_key);
3166 void     g_static_private_set (GStaticPrivate   *private_key, 
3167                                gpointer          data,
3168                                GDestroyNotify    notify);
3169 gpointer g_static_private_get_for_thread (GStaticPrivate *private_key,
3170                                           GThread        *thread);
3171 void g_static_private_set_for_thread (GStaticPrivate *private_key, 
3172                                       GThread        *thread,
3173                                       gpointer        data,
3174                                       GDestroyNotify  notify);
3175
3176 typedef struct _GStaticRecMutex GStaticRecMutex;
3177 struct _GStaticRecMutex
3178 {
3179   GStaticMutex mutex;
3180   unsigned int depth;
3181   GSystemThread owner;
3182 };
3183
3184 #define G_STATIC_REC_MUTEX_INIT { G_STATIC_MUTEX_INIT }
3185 void     g_static_rec_mutex_lock        (GStaticRecMutex *mutex);
3186 gboolean g_static_rec_mutex_trylock     (GStaticRecMutex *mutex);
3187 void     g_static_rec_mutex_unlock      (GStaticRecMutex *mutex);
3188 void     g_static_rec_mutex_lock_full   (GStaticRecMutex *mutex,
3189                                          guint            depth);
3190 guint    g_static_rec_mutex_unlock_full (GStaticRecMutex *mutex);
3191
3192 typedef struct _GStaticRWLock GStaticRWLock;
3193 struct _GStaticRWLock
3194 {
3195   GStaticMutex mutex; 
3196   GCond *read_cond;
3197   GCond *write_cond;
3198   guint read_counter;
3199   gboolean write;
3200   guint want_to_write;
3201 };
3202
3203 #define G_STATIC_RW_LOCK_INIT { G_STATIC_MUTEX_INIT, NULL, NULL, 0, FALSE, FALSE }
3204
3205 void      g_static_rw_lock_reader_lock    (GStaticRWLock* lock);
3206 gboolean  g_static_rw_lock_reader_trylock (GStaticRWLock* lock);
3207 void      g_static_rw_lock_reader_unlock  (GStaticRWLock* lock);
3208 void      g_static_rw_lock_writer_lock    (GStaticRWLock* lock);
3209 gboolean  g_static_rw_lock_writer_trylock (GStaticRWLock* lock);
3210 void      g_static_rw_lock_writer_unlock  (GStaticRWLock* lock);
3211 void      g_static_rw_lock_free (GStaticRWLock* lock);
3212
3213 /* these are some convenience macros that expand to nothing if GLib
3214  * was configured with --disable-threads. for using StaticMutexes,
3215  * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
3216  * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
3217  * declare such an globally defined lock. name is a unique identifier
3218  * for the protected varibale or code portion. locking, testing and
3219  * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
3220  * G_TRYLOCK() respectively.  
3221  */
3222 extern void glib_dummy_decl (void);
3223 #define G_LOCK_NAME(name)               g__ ## name ## _lock
3224 #ifdef  G_THREADS_ENABLED
3225 #  define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
3226 #  define G_LOCK_DEFINE(name)           \
3227     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT 
3228 #  define G_LOCK_EXTERN(name)           extern GStaticMutex G_LOCK_NAME (name)
3229
3230 #  ifdef G_DEBUG_LOCKS
3231 #    define G_LOCK(name)                G_STMT_START{             \
3232         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
3233                "file %s: line %d (%s): locking: %s ",             \
3234                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3235                #name);                                            \
3236         g_static_mutex_lock (&G_LOCK_NAME (name));                \
3237      }G_STMT_END
3238 #    define G_UNLOCK(name)              G_STMT_START{             \
3239         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
3240                "file %s: line %d (%s): unlocking: %s ",           \
3241                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3242                #name);                                            \
3243        g_static_mutex_unlock (&G_LOCK_NAME (name));               \
3244      }G_STMT_END
3245 #    define G_TRYLOCK(name)                                       \
3246         (g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                  \
3247                "file %s: line %d (%s): try locking: %s ",         \
3248                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
3249                #name), g_static_mutex_trylock (&G_LOCK_NAME (name)))
3250 #  else  /* !G_DEBUG_LOCKS */
3251 #    define G_LOCK(name) g_static_mutex_lock       (&G_LOCK_NAME (name)) 
3252 #    define G_UNLOCK(name) g_static_mutex_unlock   (&G_LOCK_NAME (name))
3253 #    define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
3254 #  endif /* !G_DEBUG_LOCKS */
3255 #else   /* !G_THREADS_ENABLED */
3256 #  define G_LOCK_DEFINE_STATIC(name)    extern void glib_dummy_decl (void)
3257 #  define G_LOCK_DEFINE(name)           extern void glib_dummy_decl (void)
3258 #  define G_LOCK_EXTERN(name)           extern void glib_dummy_decl (void)
3259 #  define G_LOCK(name)
3260 #  define G_UNLOCK(name)
3261 #  define G_TRYLOCK(name)               (TRUE)
3262 #endif  /* !G_THREADS_ENABLED */
3263
3264 /* Asyncronous Queues, can be used to communicate between threads
3265  */
3266
3267 /* Get a new GAsyncQueue with the ref_count 1 */
3268 GAsyncQueue*  g_async_queue_new                (void);
3269
3270 /* Lock and unlock an GAsyncQueue, all functions lock the queue for
3271  * themselves, but in certain cirumstances you want to hold the lock longer,
3272  * thus you lock the queue, call the *_unlocked functions and unlock it again
3273  */
3274 void          g_async_queue_lock               (GAsyncQueue *queue);
3275 void          g_async_queue_unlock             (GAsyncQueue *queue);
3276
3277 /* Ref and unref the GAsyncQueue. g_async_queue_unref_unlocked makes
3278  * no sense, as after the unreffing the Queue might be gone and can't
3279  * be unlocked. So you have a function to call, if you don't hold the
3280  * lock (g_async_queue_unref) and one to call, when you already hold
3281  * the lock (g_async_queue_unref_and_unlock). After that however, you
3282  * don't hold the lock anymore and the Queue might in fact be
3283  * destroyed, if you unrefed to zero */
3284 void          g_async_queue_ref                (GAsyncQueue *queue);
3285 void          g_async_queue_ref_unlocked       (GAsyncQueue *queue);
3286 void          g_async_queue_unref              (GAsyncQueue *queue);
3287 void          g_async_queue_unref_and_unlock   (GAsyncQueue *queue);
3288
3289 /* Push data into the async queue. Must not be NULL */
3290 void          g_async_queue_push               (GAsyncQueue *queue,
3291                                                 gpointer     data);
3292 void          g_async_queue_push_unlocked      (GAsyncQueue *queue,
3293                                                 gpointer     data);
3294
3295 /* Pop data from the async queue, when no data is there, the thread is blocked
3296  * until data arrives */
3297 gpointer      g_async_queue_pop                (GAsyncQueue *queue);
3298 gpointer      g_async_queue_pop_unlocked       (GAsyncQueue *queue);
3299
3300 /* Try to pop data, NULL is returned in case of empty queue */
3301 gpointer      g_async_queue_try_pop            (GAsyncQueue *queue);
3302 gpointer      g_async_queue_try_pop_unlocked   (GAsyncQueue *queue);
3303
3304 /* Wait for data until at maximum until end_time is reached, NULL is returned
3305  * in case of empty queue*/
3306 gpointer      g_async_queue_timed_pop          (GAsyncQueue *queue, 
3307                                                 GTimeVal    *end_time);
3308 gpointer      g_async_queue_timed_pop_unlocked (GAsyncQueue *queue, 
3309                                                 GTimeVal    *end_time);
3310
3311 /* Return the length of the queue, negative values mean, that threads
3312  * are waiting, positve values mean, that there are entries in the
3313  * queue. Actually this function returns the length of the queue minus
3314  * the number of waiting threads, g_async_queue_length == 0 could also
3315  * mean 'n' entries in the queue and 'n' thread waiting, such can
3316  * happen due to locking of the queue or due to scheduling. */
3317 gint          g_async_queue_length             (GAsyncQueue *queue);
3318 gint          g_async_queue_length_unlocked    (GAsyncQueue *queue);
3319
3320 /* Thread Pools
3321  */
3322
3323 /* The real GThreadPool is bigger, so you may only create a thread
3324  * pool with the constructor function */
3325 struct _GThreadPool
3326 {
3327   GFunc thread_func;
3328   gulong stack_size;
3329   gboolean bound; 
3330   GThreadPriority priority;
3331   gboolean exclusive;
3332   gpointer user_data;
3333 };
3334
3335 /* Get a thread pool with the function thread_func, at most max_threads may
3336  * run at a time (max_threads == -1 means no limit), stack_size, bound,
3337  * priority like in g_thread_create, exclusive == TRUE means, that the threads
3338  * shouldn't be shared and that they will be prestarted (otherwise they are
3339  * started, as needed) user_data is the 2nd argument to the thread_func */
3340 GThreadPool*    g_thread_pool_new             (GFunc            thread_func,
3341                                                gint             max_threads,
3342                                                gulong           stack_size,
3343                                                gboolean         bound,
3344                                                GThreadPriority  priority,
3345                                                gboolean         exclusive,
3346                                                gpointer         user_data);
3347
3348 /* Push new data into the thread pool. This task is assigned to a thread later
3349  * (when the maximal number of threads is reached for that pool) or now
3350  * (otherwise). If necessary a new thread will be started. The function
3351  * returns immediatly */
3352 void            g_thread_pool_push            (GThreadPool     *pool,
3353                                                gpointer         data);
3354
3355 /* Set the number of threads, which can run concurrently for that pool, -1
3356  * means no limit. 0 means has the effect, that the pool won't process
3357  * requests until the limit is set higher again */
3358 void            g_thread_pool_set_max_threads (GThreadPool     *pool,
3359                                                gint             max_threads);
3360 gint            g_thread_pool_get_max_threads (GThreadPool     *pool);
3361
3362 /* Get the number of threads assigned to that pool. This number doesn't
3363  * necessarily represent the number of working threads in that pool */
3364 guint           g_thread_pool_get_num_threads (GThreadPool     *pool);
3365
3366 /* Get the number of unprocessed items in the pool */
3367 guint           g_thread_pool_unprocessed     (GThreadPool     *pool);
3368
3369 /* Free the pool, immediate means, that all unprocessed items in the queue
3370  * wont be processed, wait means, that the function doesn't return immediatly,
3371  * but after all threads in the pool are ready processing items. immediate
3372  * does however not mean, that threads are killed. */
3373 void            g_thread_pool_free            (GThreadPool     *pool,
3374                                                gboolean         immediate,
3375                                                gboolean         wait);
3376
3377 /* Set the maximal number of unused threads before threads will be stopped by
3378  * GLib, -1 means no limit */
3379 void            g_thread_pool_set_max_unused_threads (gint      max_threads);
3380 gint            g_thread_pool_get_max_unused_threads (void);
3381 guint           g_thread_pool_get_num_unused_threads (void);
3382
3383 /* Stop all currently unused threads, but leave the limit untouched */
3384 void            g_thread_pool_stop_unused_threads    (void);
3385
3386 #ifdef __cplusplus
3387 }
3388 #endif /* __cplusplus */
3389
3390 #include <gunicode.h>
3391 #include <gerror.h>
3392
3393 #endif /* __G_LIB_H__ */