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