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