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