adjusted the g_strdup_a, g_strndup_a and g_strconcat3_a macros so their
[platform/upstream/glib.git] / glib.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU 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 __cplusplus
93 extern "C" {
94 #endif /* __cplusplus */
95
96
97 /* Provide definitions for some commonly used macros.
98  *  Some of them are only provided if they haven't already
99  *  been defined. It is assumed that if they are already
100  *  defined then the current definition is correct.
101  */
102 #ifndef NULL
103 #define NULL    ((void*) 0)
104 #endif
105
106 #ifndef FALSE
107 #define FALSE   (0)
108 #endif
109
110 #ifndef TRUE
111 #define TRUE    (!FALSE)
112 #endif
113
114 #undef  MAX
115 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
116
117 #undef  MIN
118 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
119
120 #undef  ABS
121 #define ABS(a)     (((a) < 0) ? -(a) : (a))
122
123 #undef  CLAMP
124 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
125
126
127 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
128  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
129  */
130 #if !defined (G_VA_COPY)
131 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
132 #  define G_VA_COPY(ap1, ap2)     (*(ap1) = *(ap2))
133 #  elif defined (G_VA_COPY_AS_ARRAY)
134 #  define G_VA_COPY(ap1, ap2)     g_memmove ((ap1), (ap2), sizeof (va_list))
135 #  else /* va_list is a pointer */
136 #  define G_VA_COPY(ap1, ap2)     ((ap1) = (ap2))
137 #  endif /* va_list is a pointer */
138 #endif /* !G_VA_COPY */
139
140
141 /* Provide convenience macros for handling structure
142  * fields through their offsets.
143  */
144 #define G_STRUCT_OFFSET(struct_type, member)    \
145     ((gulong) ((gchar*) &((struct_type*) 0)->member))
146 #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
147     ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
148 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
149     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
150
151
152 /* inlining hassle. for compilers that don't allow the `inline' keyword,
153  * mostly because of strict ANSI C compliance or dumbness, we try to fall
154  * back to either `__inline__' or `__inline'.
155  * we define G_CAN_INLINE, if the compiler seems to be actually
156  * *capable* to do function inlining, in which case inline function bodys
157  * do make sense. we also define G_INLINE_FUNC to properly export the
158  * function prototypes if no inlining can be performed.
159  * we special case most of the stuff, so inline functions can have a normal
160  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
161  */
162 #ifndef G_INLINE_FUNC
163 #  define G_CAN_INLINE 1
164 #endif
165 #ifdef G_HAVE_INLINE
166 #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
167 #    undef inline
168 #    define inline __inline__
169 #  endif
170 #else /* !G_HAVE_INLINE */
171 #  undef inline
172 #  if defined (G_HAVE___INLINE__)
173 #    define inline __inline__
174 #  else /* !inline && !__inline__ */
175 #    if defined (G_HAVE___INLINE)
176 #      define inline __inline
177 #    else /* !inline && !__inline__ && !__inline */
178 #      define inline /* don't inline, then */
179 #      ifndef G_INLINE_FUNC
180 #        undef G_CAN_INLINE
181 #      endif
182 #    endif
183 #  endif
184 #endif
185 #ifndef G_INLINE_FUNC
186 #  ifdef __GNUC__
187 #    ifdef __OPTIMIZE__
188 #      define G_INLINE_FUNC extern inline
189 #    else
190 #      undef G_CAN_INLINE
191 #      define G_INLINE_FUNC extern
192 #    endif
193 #  else /* !__GNUC__ */
194 #    ifdef G_CAN_INLINE
195 #      define G_INLINE_FUNC static inline
196 #    else
197 #      define G_INLINE_FUNC extern
198 #    endif
199 #  endif /* !__GNUC__ */
200 #endif /* !G_INLINE_FUNC */
201
202
203 /* Provide simple macro statement wrappers (adapted from Perl):
204  *  G_STMT_START { statements; } G_STMT_END;
205  *  can be used as a single statement, as in
206  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
207  *
208  *  For gcc we will wrap the statements within `({' and `})' braces.
209  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
210  *  and otherwise within `do' and `while (0)'.
211  */
212 #if !(defined (G_STMT_START) && defined (G_STMT_END))
213 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
214 #    define G_STMT_START        (void)(
215 #    define G_STMT_END          )
216 #  else
217 #    if (defined (sun) || defined (__sun__))
218 #      define G_STMT_START      if (1)
219 #      define G_STMT_END        else (void)0
220 #    else
221 #      define G_STMT_START      do
222 #      define G_STMT_END        while (0)
223 #    endif
224 #  endif
225 #endif
226
227
228 /* Provide macros to feature the GCC function attribute.
229  */
230 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
231 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
232   __attribute__((format (printf, format_idx, arg_idx)))
233 #define G_GNUC_SCANF( format_idx, arg_idx )     \
234   __attribute__((format (scanf, format_idx, arg_idx)))
235 #define G_GNUC_FORMAT( arg_idx )                \
236   __attribute__((format_arg (arg_idx)))
237 #define G_GNUC_NORETURN                         \
238   __attribute__((noreturn))
239 #define G_GNUC_CONST                            \
240   __attribute__((const))
241 #define G_GNUC_UNUSED                           \
242   __attribute__((unused))
243 #else   /* !__GNUC__ */
244 #define G_GNUC_PRINTF( format_idx, arg_idx )
245 #define G_GNUC_SCANF( format_idx, arg_idx )
246 #define G_GNUC_FORMAT( arg_idx )
247 #define G_GNUC_NORETURN
248 #define G_GNUC_CONST
249 #define G_GNUC_UNUSED
250 #endif  /* !__GNUC__ */
251
252
253 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
254  * macros, so we can refer to them as strings unconditionally.
255  */
256 #ifdef  __GNUC__
257 #define G_GNUC_FUNCTION         (__FUNCTION__)
258 #define G_GNUC_PRETTY_FUNCTION  (__PRETTY_FUNCTION__)
259 #else   /* !__GNUC__ */
260 #define G_GNUC_FUNCTION         ("")
261 #define G_GNUC_PRETTY_FUNCTION  ("")
262 #endif  /* !__GNUC__ */
263
264 /* we try to provide a usefull equivalent for ATEXIT if it is
265  * not defined, but use is actually abandoned. people should
266  * use g_atexit() instead.
267  */
268 #ifndef ATEXIT
269 # define ATEXIT(proc)   g_ATEXIT(proc)
270 #else
271 # define G_NATIVE_ATEXIT
272 #endif /* ATEXIT */
273
274 /* Hacker macro to place breakpoints for elected machines.
275  * Actual use is strongly deprecated of course ;)
276  */
277 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
278 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
279 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
280 #define G_BREAKPOINT()          G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
281 #else   /* !__i386__ && !__alpha__ */
282 #define G_BREAKPOINT()
283 #endif  /* __i386__ */
284
285
286 /* Provide macros for easily allocating memory. The macros
287  *  will cast the allocated memory to the specified type
288  *  in order to avoid compiler warnings. (Makes the code neater).
289  */
290
291 #ifdef __DMALLOC_H__
292 #  define g_new(type, count)            (ALLOC (type, count))
293 #  define g_new0(type, count)           (CALLOC (type, count))
294 #  define g_renew(type, mem, count)     (REALLOC (mem, type, count))
295 #else /* __DMALLOC_H__ */
296 #  define g_new(type, count)      \
297       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
298 #  define g_new0(type, count)     \
299       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
300 #  define g_renew(type, mem, count)       \
301       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
302 #endif /* __DMALLOC_H__ */
303
304 /* alloca-based counterparts of the above routines */
305 #if G_HAVE_ALLOCA
306 #  define g_alloca alloca
307 #  define g_new_a(type, count)    \
308       ((type *) alloca ((unsigned) sizeof (type) * (count)))
309 #  define g_new0_a(type, count)   \
310       ((type *) memset (alloca ((unsigned) sizeof (type) * (count)), 0, \
311                         ((unsigned) sizeof (type) * (count))))
312 #endif /* G_HAVE_ALLOCA */
313
314 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
315   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
316                    sizeof (type), \
317                    sizeof (type) * (pre_alloc), \
318                    (alloc_type)) \
319 )
320 #define g_chunk_new(type, chunk)        ( \
321   (type *) g_mem_chunk_alloc (chunk) \
322 )
323 #define g_chunk_new0(type, chunk)       ( \
324   (type *) g_mem_chunk_alloc0 (chunk) \
325 )
326 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
327   g_mem_chunk_free ((mem_chunk), (mem)); \
328 } G_STMT_END
329
330
331 #define g_string(x) #x
332
333
334 /* Provide macros for error handling. The "assert" macros will
335  *  exit on failure. The "return" macros will exit the current
336  *  function. Two different definitions are given for the macros
337  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
338  *  __PRETTY_FUNCTION__ capability.
339  */
340
341 #ifdef G_DISABLE_ASSERT
342
343 #define g_assert(expr)
344 #define g_assert_not_reached()
345
346 #else /* !G_DISABLE_ASSERT */
347
348 #ifdef __GNUC__
349
350 #define g_assert(expr)                  G_STMT_START{           \
351      if (!(expr))                                               \
352        g_log (G_LOG_DOMAIN,                                     \
353               G_LOG_LEVEL_ERROR,                                \
354               "file %s: line %d (%s): assertion failed: (%s)",  \
355               __FILE__,                                         \
356               __LINE__,                                         \
357               __PRETTY_FUNCTION__,                              \
358               #expr);                   }G_STMT_END
359
360 #define g_assert_not_reached()          G_STMT_START{           \
361      g_log (G_LOG_DOMAIN,                                       \
362             G_LOG_LEVEL_ERROR,                                  \
363             "file %s: line %d (%s): should not be reached",     \
364             __FILE__,                                           \
365             __LINE__,                                           \
366             __PRETTY_FUNCTION__);       }G_STMT_END
367
368 #else /* !__GNUC__ */
369
370 #define g_assert(expr)                  G_STMT_START{           \
371      if (!(expr))                                               \
372        g_log (G_LOG_DOMAIN,                                     \
373               G_LOG_LEVEL_ERROR,                                \
374               "file %s: line %d: assertion failed: (%s)",       \
375               __FILE__,                                         \
376               __LINE__,                                         \
377               #expr);                   }G_STMT_END
378
379 #define g_assert_not_reached()          G_STMT_START{   \
380      g_log (G_LOG_DOMAIN,                               \
381             G_LOG_LEVEL_ERROR,                          \
382             "file %s: line %d: should not be reached",  \
383             __FILE__,                                   \
384             __LINE__);          }G_STMT_END
385
386 #endif /* __GNUC__ */
387
388 #endif /* !G_DISABLE_ASSERT */
389
390
391 #ifdef G_DISABLE_CHECKS
392
393 #define g_return_if_fail(expr)
394 #define g_return_val_if_fail(expr,val)
395
396 #else /* !G_DISABLE_CHECKS */
397
398 #ifdef __GNUC__
399
400 #define g_return_if_fail(expr)          G_STMT_START{                   \
401      if (!(expr))                                                       \
402        {                                                                \
403          g_log (G_LOG_DOMAIN,                                           \
404                 G_LOG_LEVEL_CRITICAL,                                   \
405                 "file %s: line %d (%s): assertion `%s' failed.",        \
406                 __FILE__,                                               \
407                 __LINE__,                                               \
408                 __PRETTY_FUNCTION__,                                    \
409                 #expr);                                                 \
410          return;                                                        \
411        };                               }G_STMT_END
412
413 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
414      if (!(expr))                                                       \
415        {                                                                \
416          g_log (G_LOG_DOMAIN,                                           \
417                 G_LOG_LEVEL_CRITICAL,                                   \
418                 "file %s: line %d (%s): assertion `%s' failed.",        \
419                 __FILE__,                                               \
420                 __LINE__,                                               \
421                 __PRETTY_FUNCTION__,                                    \
422                 #expr);                                                 \
423          return val;                                                    \
424        };                               }G_STMT_END
425
426 #else /* !__GNUC__ */
427
428 #define g_return_if_fail(expr)          G_STMT_START{           \
429      if (!(expr))                                               \
430        {                                                        \
431          g_log (G_LOG_DOMAIN,                                   \
432                 G_LOG_LEVEL_CRITICAL,                           \
433                 "file %s: line %d: assertion `%s' failed.",     \
434                 __FILE__,                                       \
435                 __LINE__,                                       \
436                 #expr);                                         \
437          return;                                                \
438        };                               }G_STMT_END
439
440 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
441      if (!(expr))                                               \
442        {                                                        \
443          g_log (G_LOG_DOMAIN,                                   \
444                 G_LOG_LEVEL_CRITICAL,                           \
445                 "file %s: line %d: assertion `%s' failed.",     \
446                 __FILE__,                                       \
447                 __LINE__,                                       \
448                 #expr);                                         \
449          return val;                                            \
450        };                               }G_STMT_END
451
452 #endif /* !__GNUC__ */
453
454 #endif /* !G_DISABLE_CHECKS */
455
456
457 /* Provide type definitions for commonly used types.
458  *  These are useful because a "gint8" can be adjusted
459  *  to be 1 byte (8 bits) on all platforms. Similarly and
460  *  more importantly, "gint32" can be adjusted to be
461  *  4 bytes (32 bits) on all platforms.
462  */
463
464 typedef char   gchar;
465 typedef short  gshort;
466 typedef long   glong;
467 typedef int    gint;
468 typedef gint   gboolean;
469
470 typedef unsigned char   guchar;
471 typedef unsigned short  gushort;
472 typedef unsigned long   gulong;
473 typedef unsigned int    guint;
474
475 typedef float   gfloat;
476 typedef double  gdouble;
477
478 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
479  * Since gldouble isn't used anywhere, just disable it for now */
480
481 #if 0
482 #ifdef HAVE_LONG_DOUBLE
483 typedef long double gldouble;
484 #else /* HAVE_LONG_DOUBLE */
485 typedef double gldouble;
486 #endif /* HAVE_LONG_DOUBLE */
487 #endif /* 0 */
488
489 typedef void* gpointer;
490 typedef const void *gconstpointer;
491
492
493 typedef gint32  gssize;
494 typedef guint32 gsize;
495 typedef guint32 GQuark;
496 typedef gint32  GTime;
497
498
499 /* Portable endian checks and conversions
500  *
501  * glibconfig.h defines G_BYTE_ORDER which expands to one of
502  * the below macros.
503  */
504 #define G_LITTLE_ENDIAN 1234
505 #define G_BIG_ENDIAN    4321
506 #define G_PDP_ENDIAN    3412            /* unused, need specific PDP check */   
507
508
509 /* Basic bit swapping functions
510  */
511 #define GUINT16_SWAP_LE_BE_CONSTANT(val)        ((guint16) ( \
512     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
513     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
514 #define GUINT32_SWAP_LE_BE_CONSTANT(val)        ((guint32) ( \
515     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
516     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
517     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
518     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
519
520 /* Intel specific stuff for speed
521  */
522 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
523 #  define GUINT16_SWAP_LE_BE_X86(val) \
524      (__extension__                                     \
525       ({ register guint16 __v;                          \
526          if (__builtin_constant_p (val))                \
527            __v = GUINT16_SWAP_LE_BE_CONSTANT (val);     \
528          else                                           \
529            __asm__ __const__ ("rorw $8, %w0"            \
530                               : "=r" (__v)              \
531                               : "0" ((guint16) (val))); \
532         __v; }))
533 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
534 #  if !defined(__i486__) && !defined(__i586__) \
535       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
536 #     define GUINT32_SWAP_LE_BE_X86(val) \
537         (__extension__                                          \
538          ({ register guint32 __v;                               \
539             if (__builtin_constant_p (val))                     \
540               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
541           else                                                  \
542             __asm__ __const__ ("rorw $8, %w0\n\t"               \
543                                "rorl $16, %0\n\t"               \
544                                "rorw $8, %w0"                   \
545                                : "=r" (__v)                     \
546                                : "0" ((guint32) (val)));        \
547         __v; }))
548 #  else /* 486 and higher has bswap */
549 #     define GUINT32_SWAP_LE_BE_X86(val) \
550         (__extension__                                          \
551          ({ register guint32 __v;                               \
552             if (__builtin_constant_p (val))                     \
553               __v = GUINT32_SWAP_LE_BE_CONSTANT (val);          \
554           else                                                  \
555             __asm__ __const__ ("bswap %0"                       \
556                                : "=r" (__v)                     \
557                                : "0" ((guint32) (val)));        \
558         __v; }))
559 #  endif /* processor specific 32-bit stuff */
560 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
561 #else /* !__i386__ */
562 #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
563 #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
564 #endif /* __i386__ */
565
566 #ifdef G_HAVE_GINT64
567 #  define GUINT64_SWAP_LE_BE_CONSTANT(val)      ((guint64) ( \
568       (((guint64) (val) &                                               \
569         (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |      \
570       (((guint64) (val) &                                               \
571         (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |      \
572       (((guint64) (val) &                                               \
573         (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |      \
574       (((guint64) (val) &                                               \
575         (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |      \
576       (((guint64) (val) &                                               \
577         (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |      \
578       (((guint64) (val) &                                               \
579         (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |      \
580       (((guint64) (val) &                                               \
581         (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |      \
582       (((guint64) (val) &                                               \
583         (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
584 #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
585 #    define GUINT64_SWAP_LE_BE_X86(val) \
586         (__extension__                                          \
587          ({ union { guint64 __ll;                               \
588                     guint32 __l[2]; } __r;                      \
589             if (__builtin_constant_p (val))                     \
590               __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);     \
591             else                                                \
592               {                                                 \
593                 union { guint64 __ll;                           \
594                         guint32 __l[2]; } __w;                  \
595                 __w.__ll = ((guint64) val);                     \
596                 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);   \
597                 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);   \
598               }                                                 \
599           __r.__ll; }))
600 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
601 #  else /* !__i386__ */
602 #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
603 #  endif
604 #endif
605
606 #define GUINT16_SWAP_LE_PDP(val)        ((guint16) (val))
607 #define GUINT16_SWAP_BE_PDP(val)        (GUINT16_SWAP_LE_BE (val))
608 #define GUINT32_SWAP_LE_PDP(val)        ((guint32) ( \
609     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
610     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
611 #define GUINT32_SWAP_BE_PDP(val)        ((guint32) ( \
612     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
613     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
614
615 /* The G*_TO_?E() macros are defined in glibconfig.h.
616  * The transformation is symmetric, so the FROM just maps to the TO.
617  */
618 #define GINT16_FROM_LE(val)     (GINT16_TO_LE (val))
619 #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
620 #define GINT16_FROM_BE(val)     (GINT16_TO_BE (val))
621 #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
622 #define GINT32_FROM_LE(val)     (GINT32_TO_LE (val))
623 #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
624 #define GINT32_FROM_BE(val)     (GINT32_TO_BE (val))
625 #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
626
627 #ifdef G_HAVE_GINT64
628 #define GINT64_FROM_LE(val)     (GINT64_TO_LE (val))
629 #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
630 #define GINT64_FROM_BE(val)     (GINT64_TO_BE (val))
631 #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
632 #endif
633
634 #define GLONG_FROM_LE(val)      (GLONG_TO_LE (val))
635 #define GULONG_FROM_LE(val)     (GULONG_TO_LE (val))
636 #define GLONG_FROM_BE(val)      (GLONG_TO_BE (val))
637 #define GULONG_FROM_BE(val)     (GULONG_TO_BE (val))
638
639 #define GINT_FROM_LE(val)       (GINT_TO_LE (val))
640 #define GUINT_FROM_LE(val)      (GUINT_TO_LE (val))
641 #define GINT_FROM_BE(val)       (GINT_TO_BE (val))
642 #define GUINT_FROM_BE(val)      (GUINT_TO_BE (val))
643
644
645 /* Portable versions of host-network order stuff
646  */
647 #define g_ntohl(val) (GUINT32_FROM_BE (val))
648 #define g_ntohs(val) (GUINT16_FROM_BE (val))
649 #define g_htonl(val) (GUINT32_TO_BE (val))
650 #define g_htons(val) (GUINT16_TO_BE (val))
651
652
653 /* Glib version.
654  * we prefix variable declarations so they can
655  * properly get exported in windows dlls.
656  */
657 #ifdef NATIVE_WIN32
658 #  ifdef GLIB_COMPILATION
659 #    define GUTILS_C_VAR __declspec(dllexport)
660 #  else /* !GLIB_COMPILATION */
661 #    define GUTILS_C_VAR extern __declspec(dllimport)
662 #  endif /* !GLIB_COMPILATION */
663 #else /* !NATIVE_WIN32 */
664 #  define GUTILS_C_VAR extern
665 #endif /* !NATIVE_WIN32 */
666
667 GUTILS_C_VAR const guint glib_major_version;
668 GUTILS_C_VAR const guint glib_minor_version;
669 GUTILS_C_VAR const guint glib_micro_version;
670 GUTILS_C_VAR const guint glib_interface_age;
671 GUTILS_C_VAR const guint glib_binary_age;
672
673
674 /* Forward declarations of glib types.
675  */
676 typedef struct _GAllocator      GAllocator;
677 typedef struct _GArray          GArray;
678 typedef struct _GByteArray      GByteArray;
679 typedef struct _GCache          GCache;
680 typedef struct _GCompletion     GCompletion;
681 typedef struct _GData           GData;
682 typedef struct _GDebugKey       GDebugKey;
683 typedef struct _GHashTable      GHashTable;
684 typedef struct _GHook           GHook;
685 typedef struct _GHookList       GHookList;
686 typedef struct _GList           GList;
687 typedef struct _GMemChunk       GMemChunk;
688 typedef struct _GNode           GNode;
689 typedef struct _GPtrArray       GPtrArray;
690 typedef struct _GRelation       GRelation;
691 typedef struct _GScanner        GScanner;
692 typedef struct _GScannerConfig  GScannerConfig;
693 typedef struct _GSList          GSList;
694 typedef struct _GString         GString;
695 typedef struct _GStringChunk    GStringChunk;
696 typedef struct _GTimer          GTimer;
697 typedef struct _GTree           GTree;
698 typedef struct _GTuples         GTuples;
699 typedef union  _GTokenValue     GTokenValue;
700 typedef struct _GIOChannel      GIOChannel;
701
702 typedef enum
703 {
704   G_TRAVERSE_LEAFS      = 1 << 0,
705   G_TRAVERSE_NON_LEAFS  = 1 << 1,
706   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
707   G_TRAVERSE_MASK       = 0x03
708 } GTraverseFlags;
709
710 typedef enum
711 {
712   G_IN_ORDER,
713   G_PRE_ORDER,
714   G_POST_ORDER,
715   G_LEVEL_ORDER
716 } GTraverseType;
717
718 /* Log level shift offset for user defined
719  * log levels (0-7 are used by GLib).
720  */
721 #define G_LOG_LEVEL_USER_SHIFT  (8)
722
723 /* Glib log levels and flags.
724  */
725 typedef enum
726 {
727   /* log flags */
728   G_LOG_FLAG_RECURSION          = 1 << 0,
729   G_LOG_FLAG_FATAL              = 1 << 1,
730   
731   /* GLib log levels */
732   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
733   G_LOG_LEVEL_CRITICAL          = 1 << 3,
734   G_LOG_LEVEL_WARNING           = 1 << 4,
735   G_LOG_LEVEL_MESSAGE           = 1 << 5,
736   G_LOG_LEVEL_INFO              = 1 << 6,
737   G_LOG_LEVEL_DEBUG             = 1 << 7,
738   
739   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
740 } GLogLevelFlags;
741
742 /* GLib log levels that are considered fatal by default */
743 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
744
745
746 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
747 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
748 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
749 typedef gint            (*GCompareFunc)         (gconstpointer  a,
750                                                  gconstpointer  b);
751 typedef gchar*          (*GCompletionFunc)      (gpointer);
752 typedef void            (*GDestroyNotify)       (gpointer       data);
753 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
754                                                  gpointer       data,
755                                                  gpointer       user_data);
756 typedef void            (*GFunc)                (gpointer       data,
757                                                  gpointer       user_data);
758 typedef guint           (*GHashFunc)            (gconstpointer  key);
759 typedef void            (*GFreeFunc)            (gpointer       data);
760 typedef void            (*GHFunc)               (gpointer       key,
761                                                  gpointer       value,
762                                                  gpointer       user_data);
763 typedef gboolean        (*GHRFunc)              (gpointer       key,
764                                                  gpointer       value,
765                                                  gpointer       user_data);
766 typedef gint            (*GHookCompareFunc)     (GHook          *new_hook,
767                                                  GHook          *sibling);
768 typedef gboolean        (*GHookFindFunc)        (GHook          *hook,
769                                                  gpointer        data);
770 typedef void            (*GHookMarshaller)      (GHook          *hook,
771                                                  gpointer        data);
772 typedef gboolean        (*GHookCheckMarshaller) (GHook          *hook,
773                                                  gpointer        data);
774 typedef void            (*GHookFunc)            (gpointer        data);
775 typedef gboolean        (*GHookCheckFunc)       (gpointer        data);
776 typedef void            (*GHookFreeFunc)        (GHookList      *hook_list,
777                                                  GHook          *hook);
778 typedef void            (*GLogFunc)             (const gchar   *log_domain,
779                                                  GLogLevelFlags log_level,
780                                                  const gchar   *message,
781                                                  gpointer       user_data);
782 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
783                                                  gpointer       data);
784 typedef void            (*GNodeForeachFunc)     (GNode         *node,
785                                                  gpointer       data);
786 typedef gint            (*GSearchFunc)          (gpointer       key,
787                                                  gpointer       data);
788 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
789                                                  gchar         *message,
790                                                  gint           error);
791 typedef gint            (*GTraverseFunc)        (gpointer       key,
792                                                  gpointer       value,
793                                                  gpointer       data);
794 typedef void            (*GVoidFunc)            (void);
795
796
797 struct _GList
798 {
799   gpointer data;
800   GList *next;
801   GList *prev;
802 };
803
804 struct _GSList
805 {
806   gpointer data;
807   GSList *next;
808 };
809
810 struct _GString
811 {
812   gchar *str;
813   gint len;
814 };
815
816 struct _GArray
817 {
818   gchar *data;
819   guint len;
820 };
821
822 struct _GByteArray
823 {
824   guint8 *data;
825   guint   len;
826 };
827
828 struct _GPtrArray
829 {
830   gpointer *pdata;
831   guint     len;
832 };
833
834 struct _GTuples
835 {
836   guint len;
837 };
838
839 struct _GDebugKey
840 {
841   gchar *key;
842   guint  value;
843 };
844
845
846 /* Doubly linked lists
847  */
848 void   g_list_push_allocator    (GAllocator     *allocator);
849 void   g_list_pop_allocator     (void);
850 GList* g_list_alloc             (void);
851 void   g_list_free              (GList          *list);
852 void   g_list_free_1            (GList          *list);
853 GList* g_list_append            (GList          *list,
854                                  gpointer        data);
855 GList* g_list_prepend           (GList          *list,
856                                  gpointer        data);
857 GList* g_list_insert            (GList          *list,
858                                  gpointer        data,
859                                  gint            position);
860 GList* g_list_insert_sorted     (GList          *list,
861                                  gpointer        data,
862                                  GCompareFunc    func);
863 GList* g_list_concat            (GList          *list1,
864                                  GList          *list2);
865 GList* g_list_remove            (GList          *list,
866                                  gpointer        data);
867 GList* g_list_remove_link       (GList          *list,
868                                  GList          *llink);
869 GList* g_list_reverse           (GList          *list);
870 GList* g_list_copy              (GList          *list);
871 GList* g_list_nth               (GList          *list,
872                                  guint           n);
873 GList* g_list_find              (GList          *list,
874                                  gpointer        data);
875 GList* g_list_find_custom       (GList          *list,
876                                  gpointer        data,
877                                  GCompareFunc    func);
878 gint   g_list_position          (GList          *list,
879                                  GList          *llink);
880 gint   g_list_index             (GList          *list,
881                                  gpointer        data);
882 GList* g_list_last              (GList          *list);
883 GList* g_list_first             (GList          *list);
884 guint  g_list_length            (GList          *list);
885 void   g_list_foreach           (GList          *list,
886                                  GFunc           func,
887                                  gpointer        user_data);
888 GList* g_list_sort              (GList          *list,
889                                  GCompareFunc    compare_func);
890 gpointer g_list_nth_data        (GList          *list,
891                                  guint           n);
892 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
893 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
894
895
896 /* Singly linked lists
897  */
898 void    g_slist_push_allocator  (GAllocator     *allocator);
899 void    g_slist_pop_allocator   (void);
900 GSList* g_slist_alloc           (void);
901 void    g_slist_free            (GSList         *list);
902 void    g_slist_free_1          (GSList         *list);
903 GSList* g_slist_append          (GSList         *list,
904                                  gpointer        data);
905 GSList* g_slist_prepend         (GSList         *list,
906                                  gpointer        data);
907 GSList* g_slist_insert          (GSList         *list,
908                                  gpointer        data,
909                                  gint            position);
910 GSList* g_slist_insert_sorted   (GSList         *list,
911                                  gpointer        data,
912                                  GCompareFunc    func);
913 GSList* g_slist_concat          (GSList         *list1,
914                                  GSList         *list2);
915 GSList* g_slist_remove          (GSList         *list,
916                                  gpointer        data);
917 GSList* g_slist_remove_link     (GSList         *list,
918                                  GSList         *llink);
919 GSList* g_slist_reverse         (GSList         *list);
920 GSList* g_slist_copy            (GSList         *list);
921 GSList* g_slist_nth             (GSList         *list,
922                                  guint           n);
923 GSList* g_slist_find            (GSList         *list,
924                                  gpointer        data);
925 GSList* g_slist_find_custom     (GSList         *list,
926                                  gpointer        data,
927                                  GCompareFunc    func);
928 gint    g_slist_position        (GSList         *list,
929                                  GSList         *llink);
930 gint    g_slist_index           (GSList         *list,
931                                  gpointer        data);
932 GSList* g_slist_last            (GSList         *list);
933 guint   g_slist_length          (GSList         *list);
934 void    g_slist_foreach         (GSList         *list,
935                                  GFunc           func,
936                                  gpointer        user_data);
937 GSList*  g_slist_sort           (GSList          *list,
938                                  GCompareFunc    compare_func);
939 gpointer g_slist_nth_data       (GSList         *list,
940                                  guint           n);
941 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
942
943
944 /* Hash tables
945  */
946 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
947                                          GCompareFunc    key_compare_func);
948 void        g_hash_table_destroy        (GHashTable     *hash_table);
949 void        g_hash_table_insert         (GHashTable     *hash_table,
950                                          gpointer        key,
951                                          gpointer        value);
952 void        g_hash_table_remove         (GHashTable     *hash_table,
953                                          gconstpointer   key);
954 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
955                                          gconstpointer   key);
956 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
957                                          gconstpointer   lookup_key,
958                                          gpointer       *orig_key,
959                                          gpointer       *value);
960 void        g_hash_table_freeze         (GHashTable     *hash_table);
961 void        g_hash_table_thaw           (GHashTable     *hash_table);
962 void        g_hash_table_foreach        (GHashTable     *hash_table,
963                                          GHFunc          func,
964                                          gpointer        user_data);
965 gint        g_hash_table_foreach_remove (GHashTable     *hash_table,
966                                          GHRFunc         func,
967                                          gpointer        user_data);
968 gint        g_hash_table_size           (GHashTable     *hash_table);
969
970
971 /* Caches
972  */
973 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
974                                 GCacheDestroyFunc  value_destroy_func,
975                                 GCacheDupFunc      key_dup_func,
976                                 GCacheDestroyFunc  key_destroy_func,
977                                 GHashFunc          hash_key_func,
978                                 GHashFunc          hash_value_func,
979                                 GCompareFunc       key_compare_func);
980 void     g_cache_destroy       (GCache            *cache);
981 gpointer g_cache_insert        (GCache            *cache,
982                                 gpointer           key);
983 void     g_cache_remove        (GCache            *cache,
984                                 gpointer           value);
985 void     g_cache_key_foreach   (GCache            *cache,
986                                 GHFunc             func,
987                                 gpointer           user_data);
988 void     g_cache_value_foreach (GCache            *cache,
989                                 GHFunc             func,
990                                 gpointer           user_data);
991
992
993 /* Balanced binary trees
994  */
995 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
996 void     g_tree_destroy  (GTree         *tree);
997 void     g_tree_insert   (GTree         *tree,
998                           gpointer       key,
999                           gpointer       value);
1000 void     g_tree_remove   (GTree         *tree,
1001                           gpointer       key);
1002 gpointer g_tree_lookup   (GTree         *tree,
1003                           gpointer       key);
1004 void     g_tree_traverse (GTree         *tree,
1005                           GTraverseFunc  traverse_func,
1006                           GTraverseType  traverse_type,
1007                           gpointer       data);
1008 gpointer g_tree_search   (GTree         *tree,
1009                           GSearchFunc    search_func,
1010                           gpointer       data);
1011 gint     g_tree_height   (GTree         *tree);
1012 gint     g_tree_nnodes   (GTree         *tree);
1013
1014
1015
1016 /* N-way tree implementation
1017  */
1018 struct _GNode
1019 {
1020   gpointer data;
1021   GNode   *next;
1022   GNode   *prev;
1023   GNode   *parent;
1024   GNode   *children;
1025 };
1026
1027 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
1028                                  ((GNode*) (node))->prev == NULL && \
1029                                  ((GNode*) (node))->next == NULL)
1030 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
1031
1032 void     g_node_push_allocator  (GAllocator       *allocator);
1033 void     g_node_pop_allocator   (void);
1034 GNode*   g_node_new             (gpointer          data);
1035 void     g_node_destroy         (GNode            *root);
1036 void     g_node_unlink          (GNode            *node);
1037 GNode*   g_node_insert          (GNode            *parent,
1038                                  gint              position,
1039                                  GNode            *node);
1040 GNode*   g_node_insert_before   (GNode            *parent,
1041                                  GNode            *sibling,
1042                                  GNode            *node);
1043 GNode*   g_node_prepend         (GNode            *parent,
1044                                  GNode            *node);
1045 guint    g_node_n_nodes         (GNode            *root,
1046                                  GTraverseFlags    flags);
1047 GNode*   g_node_get_root        (GNode            *node);
1048 gboolean g_node_is_ancestor     (GNode            *node,
1049                                  GNode            *descendant);
1050 guint    g_node_depth           (GNode            *node);
1051 GNode*   g_node_find            (GNode            *root,
1052                                  GTraverseType     order,
1053                                  GTraverseFlags    flags,
1054                                  gpointer          data);
1055
1056 /* convenience macros */
1057 #define g_node_append(parent, node)                             \
1058      g_node_insert_before ((parent), NULL, (node))
1059 #define g_node_insert_data(parent, position, data)              \
1060      g_node_insert ((parent), (position), g_node_new (data))
1061 #define g_node_insert_data_before(parent, sibling, data)        \
1062      g_node_insert_before ((parent), (sibling), g_node_new (data))
1063 #define g_node_prepend_data(parent, data)                       \
1064      g_node_prepend ((parent), g_node_new (data))
1065 #define g_node_append_data(parent, data)                        \
1066      g_node_insert_before ((parent), NULL, g_node_new (data))
1067
1068 /* traversal function, assumes that `node' is root
1069  * (only traverses `node' and its subtree).
1070  * this function is just a high level interface to
1071  * low level traversal functions, optimized for speed.
1072  */
1073 void     g_node_traverse        (GNode            *root,
1074                                  GTraverseType     order,
1075                                  GTraverseFlags    flags,
1076                                  gint              max_depth,
1077                                  GNodeTraverseFunc func,
1078                                  gpointer          data);
1079
1080 /* return the maximum tree height starting with `node', this is an expensive
1081  * operation, since we need to visit all nodes. this could be shortened by
1082  * adding `guint height' to struct _GNode, but then again, this is not very
1083  * often needed, and would make g_node_insert() more time consuming.
1084  */
1085 guint    g_node_max_height       (GNode *root);
1086
1087 void     g_node_children_foreach (GNode           *node,
1088                                   GTraverseFlags   flags,
1089                                   GNodeForeachFunc func,
1090                                   gpointer         data);
1091 void     g_node_reverse_children (GNode           *node);
1092 guint    g_node_n_children       (GNode           *node);
1093 GNode*   g_node_nth_child        (GNode           *node,
1094                                   guint            n);
1095 GNode*   g_node_last_child       (GNode           *node);
1096 GNode*   g_node_find_child       (GNode           *node,
1097                                   GTraverseFlags   flags,
1098                                   gpointer         data);
1099 gint     g_node_child_position   (GNode           *node,
1100                                   GNode           *child);
1101 gint     g_node_child_index      (GNode           *node,
1102                                   gpointer         data);
1103
1104 GNode*   g_node_first_sibling    (GNode           *node);
1105 GNode*   g_node_last_sibling     (GNode           *node);
1106
1107 #define  g_node_prev_sibling(node)      ((node) ? \
1108                                          ((GNode*) (node))->prev : NULL)
1109 #define  g_node_next_sibling(node)      ((node) ? \
1110                                          ((GNode*) (node))->next : NULL)
1111 #define  g_node_first_child(node)       ((node) ? \
1112                                          ((GNode*) (node))->children : NULL)
1113
1114
1115 /* Callback maintenance functions
1116  */
1117 #define G_HOOK_FLAG_USER_SHIFT  (4)
1118 typedef enum
1119 {
1120   G_HOOK_FLAG_ACTIVE    = 1 << 0,
1121   G_HOOK_FLAG_IN_CALL   = 1 << 1,
1122   G_HOOK_FLAG_MASK      = 0x0f
1123 } GHookFlagMask;
1124
1125 struct _GHookList
1126 {
1127   guint          seq_id;
1128   guint          hook_size;
1129   guint          is_setup : 1;
1130   GHook         *hooks;
1131   GMemChunk     *hook_memchunk;
1132   GHookFreeFunc  hook_free; /* virtual function */
1133 };
1134
1135 struct _GHook
1136 {
1137   gpointer       data;
1138   GHook         *next;
1139   GHook         *prev;
1140   guint          ref_count;
1141   guint          hook_id;
1142   guint          flags;
1143   gpointer       func;
1144   GDestroyNotify destroy;
1145 };
1146
1147 #define G_HOOK_ACTIVE(hook)             ((((GHook*) hook)->flags & \
1148                                           G_HOOK_FLAG_ACTIVE) != 0)
1149 #define G_HOOK_IN_CALL(hook)            ((((GHook*) hook)->flags & \
1150                                           G_HOOK_FLAG_IN_CALL) != 0)
1151 #define G_HOOK_IS_VALID(hook)           (((GHook*) hook)->hook_id != 0 && \
1152                                          G_HOOK_ACTIVE (hook))
1153 #define G_HOOK_IS_UNLINKED(hook)        (((GHook*) hook)->next == NULL && \
1154                                          ((GHook*) hook)->prev == NULL && \
1155                                          ((GHook*) hook)->hook_id == 0 && \
1156                                          ((GHook*) hook)->ref_count == 0)
1157
1158 void     g_hook_list_init               (GHookList              *hook_list,
1159                                          guint                   hook_size);
1160 void     g_hook_list_clear              (GHookList              *hook_list);
1161 GHook*   g_hook_alloc                   (GHookList              *hook_list);
1162 void     g_hook_free                    (GHookList              *hook_list,
1163                                          GHook                  *hook);
1164 void     g_hook_ref                     (GHookList              *hook_list,
1165                                          GHook                  *hook);
1166 void     g_hook_unref                   (GHookList              *hook_list,
1167                                          GHook                  *hook);
1168 gboolean g_hook_destroy                 (GHookList              *hook_list,
1169                                          guint                   hook_id);
1170 void     g_hook_destroy_link            (GHookList              *hook_list,
1171                                          GHook                  *hook);
1172 void     g_hook_prepend                 (GHookList              *hook_list,
1173                                          GHook                  *hook);
1174 void     g_hook_insert_before           (GHookList              *hook_list,
1175                                          GHook                  *sibling,
1176                                          GHook                  *hook);
1177 void     g_hook_insert_sorted           (GHookList              *hook_list,
1178                                          GHook                  *hook,
1179                                          GHookCompareFunc        func);
1180 GHook*   g_hook_get                     (GHookList              *hook_list,
1181                                          guint                   hook_id);
1182 GHook*   g_hook_find                    (GHookList              *hook_list,
1183                                          gboolean                need_valids,
1184                                          GHookFindFunc           func,
1185                                          gpointer                data);
1186 GHook*   g_hook_find_data               (GHookList              *hook_list,
1187                                          gboolean                need_valids,
1188                                          gpointer                data);
1189 GHook*   g_hook_find_func               (GHookList              *hook_list,
1190                                          gboolean                need_valids,
1191                                          gpointer                func);
1192 GHook*   g_hook_find_func_data          (GHookList              *hook_list,
1193                                          gboolean                need_valids,
1194                                          gpointer                func,
1195                                          gpointer                data);
1196 /* return the first valid hook, and increment its reference count */
1197 GHook*   g_hook_first_valid             (GHookList              *hook_list,
1198                                          gboolean                may_be_in_call);
1199 /* return the next valid hook with incremented reference count, and
1200  * decrement the reference count of the original hook
1201  */
1202 GHook*   g_hook_next_valid              (GHookList              *hook_list,
1203                                          GHook                  *hook,
1204                                          gboolean                may_be_in_call);
1205
1206 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1207 gint     g_hook_compare_ids             (GHook                  *new_hook,
1208                                          GHook                  *sibling);
1209
1210 /* convenience macros */
1211 #define  g_hook_append( hook_list, hook )  \
1212      g_hook_insert_before ((hook_list), NULL, (hook))
1213
1214 /* invoke all valid hooks with the (*GHookFunc) signature.
1215  */
1216 void     g_hook_list_invoke             (GHookList              *hook_list,
1217                                          gboolean                may_recurse);
1218 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1219  * and destroy the hook if FALSE is returned.
1220  */
1221 void     g_hook_list_invoke_check       (GHookList              *hook_list,
1222                                          gboolean                may_recurse);
1223 /* invoke a marshaller on all valid hooks.
1224  */
1225 void     g_hook_list_marshal            (GHookList              *hook_list,
1226                                          gboolean                may_recurse,
1227                                          GHookMarshaller         marshaller,
1228                                          gpointer                data);
1229 void     g_hook_list_marshal_check      (GHookList              *hook_list,
1230                                          gboolean                may_recurse,
1231                                          GHookCheckMarshaller    marshaller,
1232                                          gpointer                data);
1233
1234
1235 /* Fatal error handlers.
1236  * g_on_error_query() will prompt the user to either
1237  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1238  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1239  * process and shows a stack trace.
1240  * These function may cause different actions on non-unix platforms.
1241  * The prg_name arg is required by gdb to find the executable, if it is
1242  * passed as NULL, g_on_error_query() will try g_get_prgname().
1243  */
1244 void g_on_error_query (const gchar *prg_name);
1245 void g_on_error_stack_trace (const gchar *prg_name);
1246
1247
1248 /* Logging mechanism
1249  */
1250 extern          const gchar             *g_log_domain_glib;
1251 guint           g_log_set_handler       (const gchar    *log_domain,
1252                                          GLogLevelFlags  log_levels,
1253                                          GLogFunc        log_func,
1254                                          gpointer        user_data);
1255 void            g_log_remove_handler    (const gchar    *log_domain,
1256                                          guint           handler_id);
1257 void            g_log_default_handler   (const gchar    *log_domain,
1258                                          GLogLevelFlags  log_level,
1259                                          const gchar    *message,
1260                                          gpointer        unused_data);
1261 void            g_log                   (const gchar    *log_domain,
1262                                          GLogLevelFlags  log_level,
1263                                          const gchar    *format,
1264                                          ...) G_GNUC_PRINTF (3, 4);
1265 void            g_logv                  (const gchar    *log_domain,
1266                                          GLogLevelFlags  log_level,
1267                                          const gchar    *format,
1268                                          va_list         args);
1269 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1270                                          GLogLevelFlags  fatal_mask);
1271 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1272 #ifndef G_LOG_DOMAIN
1273 #define G_LOG_DOMAIN    (NULL)
1274 #endif  /* G_LOG_DOMAIN */
1275 #ifdef  __GNUC__
1276 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1277                                                G_LOG_LEVEL_ERROR, \
1278                                                format, ##args)
1279 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1280                                                G_LOG_LEVEL_MESSAGE, \
1281                                                format, ##args)
1282 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1283                                                G_LOG_LEVEL_WARNING, \
1284                                                format, ##args)
1285 #else   /* !__GNUC__ */
1286 static inline void
1287 g_error (const gchar *format,
1288          ...)
1289 {
1290   va_list args;
1291   va_start (args, format);
1292   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1293   va_end (args);
1294 }
1295 static inline void
1296 g_message (const gchar *format,
1297            ...)
1298 {
1299   va_list args;
1300   va_start (args, format);
1301   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1302   va_end (args);
1303 }
1304 static inline void
1305 g_warning (const gchar *format,
1306            ...)
1307 {
1308   va_list args;
1309   va_start (args, format);
1310   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1311   va_end (args);
1312 }
1313 #endif  /* !__GNUC__ */
1314
1315 typedef void    (*GPrintFunc)           (const gchar    *string);
1316 void            g_print                 (const gchar    *format,
1317                                          ...) G_GNUC_PRINTF (1, 2);
1318 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1319 void            g_printerr              (const gchar    *format,
1320                                          ...) G_GNUC_PRINTF (1, 2);
1321 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1322
1323 /* deprecated compatibility functions, use g_log_set_handler() instead */
1324 typedef void            (*GErrorFunc)           (const gchar *str);
1325 typedef void            (*GWarningFunc)         (const gchar *str);
1326 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1327 GWarningFunc g_set_warning_handler (GWarningFunc func);
1328 GPrintFunc   g_set_message_handler (GPrintFunc func);
1329
1330
1331 /* Memory allocation and debugging
1332  */
1333 #ifdef USE_DMALLOC
1334
1335 #define g_malloc(size)       ((gpointer) MALLOC (size))
1336 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1337 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1338 #define g_free(mem)          FREE (mem)
1339
1340 #else /* !USE_DMALLOC */
1341
1342 gpointer g_malloc      (gulong    size);
1343 gpointer g_malloc0     (gulong    size);
1344 gpointer g_realloc     (gpointer  mem,
1345                         gulong    size);
1346 void     g_free        (gpointer  mem);
1347
1348 #endif /* !USE_DMALLOC */
1349
1350 void     g_mem_profile (void);
1351 void     g_mem_check   (gpointer  mem);
1352
1353 /* Generic allocators
1354  */
1355 GAllocator* g_allocator_new   (const gchar  *name,
1356                                guint         n_preallocs);
1357 void        g_allocator_free  (GAllocator   *allocator);
1358
1359 #define G_ALLOCATOR_LIST        (1)
1360 #define G_ALLOCATOR_SLIST       (2)
1361 #define G_ALLOCATOR_NODE        (3)
1362
1363
1364 /* "g_mem_chunk_new" creates a new memory chunk.
1365  * Memory chunks are used to allocate pieces of memory which are
1366  *  always the same size. Lists are a good example of such a data type.
1367  * The memory chunk allocates and frees blocks of memory as needed.
1368  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1369  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1370  *  fault...somewhere).
1371  *
1372  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1373  *  want to know what's going on inside do you?)
1374  */
1375
1376 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1377  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1378  *  atom. (They are also useful for lists which use MemChunk to allocate
1379  *  memory but are also part of the MemChunk implementation).
1380  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1381  */
1382
1383 #define G_ALLOC_ONLY      1
1384 #define G_ALLOC_AND_FREE  2
1385
1386 GMemChunk* g_mem_chunk_new     (gchar     *name,
1387                                 gint       atom_size,
1388                                 gulong     area_size,
1389                                 gint       type);
1390 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1391 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1392 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1393 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1394                                 gpointer   mem);
1395 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1396 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1397 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1398 void       g_mem_chunk_info    (void);
1399
1400 /* Ah yes...we have a "g_blow_chunks" function.
1401  * "g_blow_chunks" simply compresses all the chunks. This operation
1402  *  consists of freeing every memory area that should be freed (but
1403  *  which we haven't gotten around to doing yet). And, no,
1404  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1405  *  much better name than "g_mem_chunk_clean_all" or something
1406  *  similar.
1407  */
1408 void g_blow_chunks (void);
1409
1410
1411 /* Timer
1412  */
1413 GTimer* g_timer_new     (void);
1414 void    g_timer_destroy (GTimer  *timer);
1415 void    g_timer_start   (GTimer  *timer);
1416 void    g_timer_stop    (GTimer  *timer);
1417 void    g_timer_reset   (GTimer  *timer);
1418 gdouble g_timer_elapsed (GTimer  *timer,
1419                          gulong  *microseconds);
1420
1421
1422 /* String utility functions that modify a string argument or
1423  * return a constant string that must not be freed.
1424  */
1425 #define  G_STR_DELIMITERS       "_-|> <."
1426 gchar*   g_strdelimit           (gchar       *string,
1427                                  const gchar *delimiters,
1428                                  gchar        new_delimiter);
1429 gdouble  g_strtod               (const gchar *nptr,
1430                                  gchar      **endptr);
1431 gchar*   g_strerror             (gint         errnum);
1432 gchar*   g_strsignal            (gint         signum);
1433 gint     g_strcasecmp           (const gchar *s1,
1434                                  const gchar *s2);
1435 gint     g_strncasecmp          (const gchar *s1,
1436                                  const gchar *s2,
1437                                  guint        n);
1438 void     g_strdown              (gchar       *string);
1439 void     g_strup                (gchar       *string);
1440 void     g_strreverse           (gchar       *string);
1441 /* removes leading spaces */
1442 gchar*   g_strchug              (gchar        *string);
1443 /* removes trailing spaces */
1444 gchar*  g_strchomp              (gchar        *string);
1445 /* removes leading & trailing spaces */
1446 #define g_strstrip( string )    g_strchomp (g_strchug (string))
1447
1448 /* String utility functions that return a newly allocated string which
1449  * ought to be freed from the caller at some point.
1450  */
1451 gchar*   g_strdup               (const gchar *str);
1452 gchar*   g_strdup_printf        (const gchar *format,
1453                                  ...) G_GNUC_PRINTF (1, 2);
1454 gchar*   g_strdup_vprintf       (const gchar *format,
1455                                  va_list      args);
1456 gchar*   g_strndup              (const gchar *str,
1457                                  guint        n);
1458 gchar*   g_strnfill             (guint        length,
1459                                  gchar        fill_char);
1460 gchar*   g_strconcat            (const gchar *string1,
1461                                  ...); /* NULL terminated */
1462 gchar*   g_strjoin              (const gchar  *separator,
1463                                  ...); /* NULL terminated */
1464 gchar*   g_strescape            (gchar        *string);
1465 gpointer g_memdup               (gconstpointer mem,
1466                                  guint         byte_size);
1467
1468 /* Macros for dynamic strings via fast stack allocation
1469  * All macros take a special first argument: the target gchar* string
1470  */
1471 #if G_HAVE_ALLOCA
1472 #  define g_strdup_a(newstr,str) G_STMT_START { \
1473           gchar *__new;                         \
1474           const gchar *__old = (str);           \
1475           if (__old)                            \
1476           {                                     \
1477             size_t __len = strlen (__old) + 1;  \
1478             __new = alloca (__len);             \
1479             memcpy (__new, __old, __len);       \
1480           }                                     \
1481           else                                  \
1482             __new = NULL;                       \
1483           (newstr) = __new;                     \
1484    } G_STMT_END
1485 #  define g_strndup_a(newstr,str,n) G_STMT_START { \
1486           gchar *__new;                         \
1487           const gchar *__old = (str);           \
1488           if (__old)                            \
1489           {                                     \
1490             guint __n = (n);                    \
1491             size_t __len = strlen (__old);      \
1492             if (__len > (__n))                  \
1493               __len = (__n);                    \
1494             __new = alloca (__len + 1);         \
1495             memcpy (__new, __old, __len);       \
1496             __new[__len] = 0;                   \
1497           }                                     \
1498           else                                  \
1499             __new = NULL;                       \
1500           (newstr) = __new;                     \
1501    } G_STMT_END
1502 #  define g_strconcat3_a(newstr,str1,str2,str3) G_STMT_START { \
1503           const gchar *__str1 = (str1);                         \
1504           const gchar *__str2 = (str2);                         \
1505           const gchar *__str3 = (str3);                         \
1506           gchar *__new;                                         \
1507           if (__str1) {                                         \
1508             size_t __len1 = strlen (__str1);                    \
1509             if (__str2) {                                       \
1510               size_t __len2 = strlen (__str2);                  \
1511               if (__str3) {                                     \
1512                 size_t __len3 = strlen (__str3);                \
1513                 __new = alloca (__len1 + __len2 + __len3 + 1);  \
1514                 __new[__len1 + __len2 + __len3] = 0;            \
1515                 memcpy (__new + __len1 + __len2, __str3, __len3); \
1516               } else {                                          \
1517                 __new = alloca (__len1 + __len2 + 1);           \
1518                 __new[__len1 + __len2] = 0;                     \
1519               }                                                 \
1520               memcpy (__new + __len1, __str2, __len2);          \
1521             } else {                                            \
1522               __new = alloca (__len1 + 1);                      \
1523               __new[__len1] = 0;                                \
1524             }                                                   \
1525             memcpy (__new, __str1, __len1);                     \
1526           } else                                                \
1527             __new = NULL;                                       \
1528           (newstr) = __new;                                     \
1529    } G_STMT_END
1530 #endif /* G_HAVE_ALLOCA */
1531
1532
1533 /* NULL terminated string arrays.
1534  * g_strsplit() splits up string into max_tokens tokens at delim and
1535  * returns a newly allocated string array.
1536  * g_strjoinv() concatenates all of str_array's strings, sliding in an
1537  * optional separator, the returned string is newly allocated.
1538  * g_strfreev() frees the array itself and all of its strings.
1539  */
1540 gchar**  g_strsplit             (const gchar  *string,
1541                                  const gchar  *delimiter,
1542                                  gint          max_tokens);
1543 gchar*   g_strjoinv             (const gchar  *separator,
1544                                  gchar       **str_array);
1545 void     g_strfreev             (gchar       **str_array);
1546
1547
1548
1549 /* calculate a string size, guarranteed to fit format + args.
1550  */
1551 guint   g_printf_string_upper_bound (const gchar* format,
1552                                      va_list      args);
1553
1554
1555 /* Retrive static string info
1556  */
1557 gchar*  g_get_user_name         (void);
1558 gchar*  g_get_real_name         (void);
1559 gchar*  g_get_home_dir          (void);
1560 gchar*  g_get_tmp_dir           (void);
1561 gchar*  g_get_prgname           (void);
1562 void    g_set_prgname           (const gchar *prgname);
1563
1564
1565 /* Miscellaneous utility functions
1566  */
1567 guint   g_parse_debug_string    (const gchar *string,
1568                                  GDebugKey   *keys,
1569                                  guint        nkeys);
1570 gint    g_snprintf              (gchar       *string,
1571                                  gulong       n,
1572                                  gchar const *format,
1573                                  ...) G_GNUC_PRINTF (3, 4);
1574 gint    g_vsnprintf             (gchar       *string,
1575                                  gulong       n,
1576                                  gchar const *format,
1577                                  va_list      args);
1578 gchar*  g_basename              (const gchar *file_name);
1579 /* Check if a file name is an absolute path */
1580 gboolean g_path_is_absolute     (const gchar *file_name);
1581 /* In case of absolute paths, skip the root part */
1582 gchar*  g_path_skip_root        (gchar       *file_name);
1583
1584 /* strings are newly allocated with g_malloc() */
1585 gchar*  g_dirname               (const gchar *file_name);
1586 gchar*  g_get_current_dir       (void);
1587 gchar*  g_getenv                (const gchar *variable);
1588
1589
1590 /* we use a GLib function as a replacement for ATEXIT, so
1591  * the programmer is not required to check the return value
1592  * (if there is any in the implementation) and doesn't encounter
1593  * missing include files.
1594  */
1595 void    g_atexit                (GVoidFunc    func);
1596
1597
1598 /* Bit tests
1599  */
1600 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1601                                        gint    nth_bit);
1602 #ifdef  G_CAN_INLINE
1603 G_INLINE_FUNC gint
1604 g_bit_nth_lsf (guint32 mask,
1605                gint    nth_bit)
1606 {
1607   do
1608     {
1609       nth_bit++;
1610       if (mask & (1 << (guint) nth_bit))
1611         return nth_bit;
1612     }
1613   while (nth_bit < 32);
1614   return -1;
1615 }
1616 #endif  /* G_CAN_INLINE */
1617
1618 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1619                                        gint    nth_bit);
1620 #ifdef G_CAN_INLINE
1621 G_INLINE_FUNC gint
1622 g_bit_nth_msf (guint32 mask,
1623                gint    nth_bit)
1624 {
1625   if (nth_bit < 0)
1626     nth_bit = 32;
1627   do
1628     {
1629       nth_bit--;
1630       if (mask & (1 << (guint) nth_bit))
1631         return nth_bit;
1632     }
1633   while (nth_bit > 0);
1634   return -1;
1635 }
1636 #endif  /* G_CAN_INLINE */
1637
1638 G_INLINE_FUNC guint     g_bit_storage (guint number);
1639 #ifdef G_CAN_INLINE
1640 G_INLINE_FUNC guint
1641 g_bit_storage (guint number)
1642 {
1643   register guint n_bits = 0;
1644   
1645   do
1646     {
1647       n_bits++;
1648       number >>= 1;
1649     }
1650   while (number);
1651   return n_bits;
1652 }
1653 #endif  /* G_CAN_INLINE */
1654
1655 /* String Chunks
1656  */
1657 GStringChunk* g_string_chunk_new           (gint size);
1658 void          g_string_chunk_free          (GStringChunk *chunk);
1659 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1660                                             const gchar  *string);
1661 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1662                                             const gchar  *string);
1663
1664
1665 /* Strings
1666  */
1667 GString* g_string_new       (const gchar *init);
1668 GString* g_string_sized_new (guint        dfl_size);
1669 void     g_string_free      (GString     *string,
1670                              gint         free_segment);
1671 GString* g_string_assign    (GString     *lval,
1672                              const gchar *rval);
1673 GString* g_string_truncate  (GString     *string,
1674                              gint         len);
1675 GString* g_string_append    (GString     *string,
1676                              const gchar *val);
1677 GString* g_string_append_c  (GString     *string,
1678                              gchar        c);
1679 GString* g_string_prepend   (GString     *string,
1680                              const gchar *val);
1681 GString* g_string_prepend_c (GString     *string,
1682                              gchar        c);
1683 GString* g_string_insert    (GString     *string,
1684                              gint         pos,
1685                              const gchar *val);
1686 GString* g_string_insert_c  (GString     *string,
1687                              gint         pos,
1688                              gchar        c);
1689 GString* g_string_erase     (GString     *string,
1690                              gint         pos,
1691                              gint         len);
1692 GString* g_string_down      (GString     *string);
1693 GString* g_string_up        (GString     *string);
1694 void     g_string_sprintf   (GString     *string,
1695                              const gchar *format,
1696                              ...) G_GNUC_PRINTF (2, 3);
1697 void     g_string_sprintfa  (GString     *string,
1698                              const gchar *format,
1699                              ...) G_GNUC_PRINTF (2, 3);
1700
1701
1702 /* Resizable arrays, remove fills any cleared spot and shortens the
1703  * array, while preserving the order. remove_fast will distort the
1704  * order by moving the last element to the position of the removed 
1705  */
1706
1707 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1708 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1709 #define g_array_insert_val(a,i,v) g_array_prepend_vals(a,i,&v,1)
1710 #define g_array_index(a,t,i) (((t*)a->data)[i])
1711
1712 GArray* g_array_new               (gboolean         zero_terminated,
1713                                    gboolean         clear,
1714                                    guint            element_size);
1715 void    g_array_free              (GArray          *array,
1716                                    gboolean         free_segment);
1717 GArray* g_array_append_vals       (GArray          *array,
1718                                    gconstpointer    data,
1719                                    guint            len);
1720 GArray* g_array_prepend_vals      (GArray          *array,
1721                                    gconstpointer    data,
1722                                    guint            len);
1723 GArray* g_array_insert_vals       (GArray          *array,
1724                                    guint            index,
1725                                    gconstpointer    data,
1726                                    guint            len);
1727 GArray* g_array_set_size          (GArray          *array,
1728                                    guint            length);
1729 GArray* g_array_remove_index      (GArray          *array,
1730                                    guint            index);
1731 GArray* g_array_remove_index_fast (GArray          *array,
1732                                    guint            index);
1733
1734 /* Resizable pointer array.  This interface is much less complicated
1735  * than the above.  Add appends appends a pointer.  Remove fills any
1736  * cleared spot and shortens the array. remove_fast will again distort
1737  * order.  
1738  */
1739 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1740 GPtrArray*  g_ptr_array_new                (void);
1741 void        g_ptr_array_free               (GPtrArray   *array,
1742                                             gboolean     free_seg);
1743 void        g_ptr_array_set_size           (GPtrArray   *array,
1744                                             gint         length);
1745 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1746                                             guint        index);
1747 gpointer    g_ptr_array_remove_index_fast  (GPtrArray   *array,
1748                                             guint        index);
1749 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1750                                             gpointer     data);
1751 gboolean    g_ptr_array_remove_fast        (GPtrArray   *array,
1752                                             gpointer     data);
1753 void        g_ptr_array_add                (GPtrArray   *array,
1754                                             gpointer     data);
1755
1756 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1757  * but type-safe.
1758  */
1759
1760 GByteArray* g_byte_array_new               (void);
1761 void        g_byte_array_free              (GByteArray   *array,
1762                                             gboolean      free_segment);
1763 GByteArray* g_byte_array_append            (GByteArray   *array,
1764                                             const guint8 *data,
1765                                             guint         len);
1766 GByteArray* g_byte_array_prepend           (GByteArray   *array,
1767                                             const guint8 *data,
1768                                             guint         len);
1769 GByteArray* g_byte_array_set_size          (GByteArray   *array,
1770                                             guint         length);
1771 GByteArray* g_byte_array_remove_index      (GByteArray   *array,
1772                                             guint         index);
1773 GByteArray* g_byte_array_remove_index_fast (GByteArray   *array,
1774                                             guint         index);
1775
1776
1777 /* Hash Functions
1778  */
1779 gint  g_str_equal (gconstpointer   v,
1780                    gconstpointer   v2);
1781 guint g_str_hash  (gconstpointer   v);
1782
1783 gint  g_int_equal (gconstpointer   v,
1784                    gconstpointer   v2);
1785 guint g_int_hash  (gconstpointer   v);
1786
1787 /* This "hash" function will just return the key's adress as an
1788  * unsigned integer. Useful for hashing on plain adresses or
1789  * simple integer values.
1790  */
1791 guint g_direct_hash  (gconstpointer v);
1792 gint  g_direct_equal (gconstpointer v,
1793                       gconstpointer v2);
1794
1795
1796 /* Quarks (string<->id association)
1797  */
1798 GQuark    g_quark_try_string            (const gchar    *string);
1799 GQuark    g_quark_from_static_string    (const gchar    *string);
1800 GQuark    g_quark_from_string           (const gchar    *string);
1801 gchar*    g_quark_to_string             (GQuark          quark);
1802
1803
1804 /* Keyed Data List
1805  */
1806 void      g_datalist_init                (GData          **datalist);
1807 void      g_datalist_clear               (GData          **datalist);
1808 gpointer  g_datalist_id_get_data         (GData          **datalist,
1809                                           GQuark           key_id);
1810 void      g_datalist_id_set_data_full    (GData          **datalist,
1811                                           GQuark           key_id,
1812                                           gpointer         data,
1813                                           GDestroyNotify   destroy_func);
1814 void      g_datalist_id_remove_no_notify (GData          **datalist,
1815                                           GQuark           key_id);
1816 void      g_datalist_foreach             (GData          **datalist,
1817                                           GDataForeachFunc func,
1818                                           gpointer         user_data);
1819 #define   g_datalist_id_set_data(dl, q, d)      \
1820      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1821 #define   g_datalist_id_remove_data(dl, q)      \
1822      g_datalist_id_set_data ((dl), (q), NULL)
1823 #define   g_datalist_get_data(dl, k)            \
1824      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1825 #define   g_datalist_set_data_full(dl, k, d, f) \
1826      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1827 #define   g_datalist_remove_no_notify(dl, k)    \
1828      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1829 #define   g_datalist_set_data(dl, k, d)         \
1830      g_datalist_set_data_full ((dl), (k), (d), NULL)
1831 #define   g_datalist_remove_data(dl, k)         \
1832      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1833
1834
1835 /* Location Associated Keyed Data
1836  */
1837 void      g_dataset_destroy             (gconstpointer    dataset_location);
1838 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
1839                                          GQuark           key_id);
1840 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
1841                                          GQuark           key_id,
1842                                          gpointer         data,
1843                                          GDestroyNotify   destroy_func);
1844 void      g_dataset_id_remove_no_notify (gconstpointer    dataset_location,
1845                                          GQuark           key_id);
1846 void      g_dataset_foreach             (gconstpointer    dataset_location,
1847                                          GDataForeachFunc func,
1848                                          gpointer         user_data);
1849 #define   g_dataset_id_set_data(l, k, d)        \
1850      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1851 #define   g_dataset_id_remove_data(l, k)        \
1852      g_dataset_id_set_data ((l), (k), NULL)
1853 #define   g_dataset_get_data(l, k)              \
1854      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1855 #define   g_dataset_set_data_full(l, k, d, f)   \
1856      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1857 #define   g_dataset_remove_no_notify(l, k)      \
1858      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1859 #define   g_dataset_set_data(l, k, d)           \
1860      g_dataset_set_data_full ((l), (k), (d), NULL)
1861 #define   g_dataset_remove_data(l, k)           \
1862      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1863
1864
1865 /* GScanner: Flexible lexical scanner for general purpose.
1866  */
1867
1868 /* Character sets */
1869 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1870 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1871 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1872                         "\307\310\311\312\313\314\315\316\317\320"\
1873                         "\321\322\323\324\325\326"\
1874                         "\330\331\332\333\334\335\336"
1875 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1876                         "\347\350\351\352\353\354\355\356\357\360"\
1877                         "\361\362\363\364\365\366"\
1878                         "\370\371\372\373\374\375\376\377"
1879
1880 /* Error types */
1881 typedef enum
1882 {
1883   G_ERR_UNKNOWN,
1884   G_ERR_UNEXP_EOF,
1885   G_ERR_UNEXP_EOF_IN_STRING,
1886   G_ERR_UNEXP_EOF_IN_COMMENT,
1887   G_ERR_NON_DIGIT_IN_CONST,
1888   G_ERR_DIGIT_RADIX,
1889   G_ERR_FLOAT_RADIX,
1890   G_ERR_FLOAT_MALFORMED
1891 } GErrorType;
1892
1893 /* Token types */
1894 typedef enum
1895 {
1896   G_TOKEN_EOF                   =   0,
1897   
1898   G_TOKEN_LEFT_PAREN            = '(',
1899   G_TOKEN_RIGHT_PAREN           = ')',
1900   G_TOKEN_LEFT_CURLY            = '{',
1901   G_TOKEN_RIGHT_CURLY           = '}',
1902   G_TOKEN_LEFT_BRACE            = '[',
1903   G_TOKEN_RIGHT_BRACE           = ']',
1904   G_TOKEN_EQUAL_SIGN            = '=',
1905   G_TOKEN_COMMA                 = ',',
1906   
1907   G_TOKEN_NONE                  = 256,
1908   
1909   G_TOKEN_ERROR,
1910   
1911   G_TOKEN_CHAR,
1912   G_TOKEN_BINARY,
1913   G_TOKEN_OCTAL,
1914   G_TOKEN_INT,
1915   G_TOKEN_HEX,
1916   G_TOKEN_FLOAT,
1917   G_TOKEN_STRING,
1918   
1919   G_TOKEN_SYMBOL,
1920   G_TOKEN_IDENTIFIER,
1921   G_TOKEN_IDENTIFIER_NULL,
1922   
1923   G_TOKEN_COMMENT_SINGLE,
1924   G_TOKEN_COMMENT_MULTI,
1925   G_TOKEN_LAST
1926 } GTokenType;
1927
1928 union   _GTokenValue
1929 {
1930   gpointer      v_symbol;
1931   gchar         *v_identifier;
1932   gulong        v_binary;
1933   gulong        v_octal;
1934   gulong        v_int;
1935   gdouble       v_float;
1936   gulong        v_hex;
1937   gchar         *v_string;
1938   gchar         *v_comment;
1939   guchar        v_char;
1940   guint         v_error;
1941 };
1942
1943 struct  _GScannerConfig
1944 {
1945   /* Character sets
1946    */
1947   gchar         *cset_skip_characters;          /* default: " \t\n" */
1948   gchar         *cset_identifier_first;
1949   gchar         *cset_identifier_nth;
1950   gchar         *cpair_comment_single;          /* default: "#\n" */
1951   
1952   /* Should symbol lookup work case sensitive?
1953    */
1954   guint         case_sensitive : 1;
1955   
1956   /* Boolean values to be adjusted "on the fly"
1957    * to configure scanning behaviour.
1958    */
1959   guint         skip_comment_multi : 1;         /* C like comment */
1960   guint         skip_comment_single : 1;        /* single line comment */
1961   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1962   guint         scan_identifier : 1;
1963   guint         scan_identifier_1char : 1;
1964   guint         scan_identifier_NULL : 1;
1965   guint         scan_symbols : 1;
1966   guint         scan_binary : 1;
1967   guint         scan_octal : 1;
1968   guint         scan_float : 1;
1969   guint         scan_hex : 1;                   /* `0x0ff0' */
1970   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1971   guint         scan_string_sq : 1;             /* string: 'anything' */
1972   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1973   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1974   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1975   guint         identifier_2_string : 1;
1976   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1977   guint         symbol_2_token : 1;
1978   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
1979 };
1980
1981 struct  _GScanner
1982 {
1983   /* unused fields */
1984   gpointer              user_data;
1985   guint                 max_parse_errors;
1986   
1987   /* g_scanner_error() increments this field */
1988   guint                 parse_errors;
1989   
1990   /* name of input stream, featured by the default message handler */
1991   const gchar           *input_name;
1992   
1993   /* data pointer for derived structures */
1994   gpointer              derived_data;
1995   
1996   /* link into the scanner configuration */
1997   GScannerConfig        *config;
1998   
1999   /* fields filled in after g_scanner_get_next_token() */
2000   GTokenType            token;
2001   GTokenValue           value;
2002   guint                 line;
2003   guint                 position;
2004   
2005   /* fields filled in after g_scanner_peek_next_token() */
2006   GTokenType            next_token;
2007   GTokenValue           next_value;
2008   guint                 next_line;
2009   guint                 next_position;
2010   
2011   /* to be considered private */
2012   GHashTable            *symbol_table;
2013   gint                  input_fd;
2014   const gchar           *text;
2015   const gchar           *text_end;
2016   gchar                 *buffer;
2017   guint                 scope_id;
2018   
2019   /* handler function for _warn and _error */
2020   GScannerMsgFunc       msg_handler;
2021 };
2022
2023 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
2024 void            g_scanner_destroy               (GScanner       *scanner);
2025 void            g_scanner_input_file            (GScanner       *scanner,
2026                                                  gint           input_fd);
2027 void            g_scanner_sync_file_offset      (GScanner       *scanner);
2028 void            g_scanner_input_text            (GScanner       *scanner,
2029                                                  const  gchar   *text,
2030                                                  guint          text_len);
2031 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
2032 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
2033 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
2034 GTokenValue     g_scanner_cur_value             (GScanner       *scanner);
2035 guint           g_scanner_cur_line              (GScanner       *scanner);
2036 guint           g_scanner_cur_position          (GScanner       *scanner);
2037 gboolean        g_scanner_eof                   (GScanner       *scanner);
2038 guint           g_scanner_set_scope             (GScanner       *scanner,
2039                                                  guint           scope_id);
2040 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
2041                                                  guint           scope_id,
2042                                                  const gchar    *symbol,
2043                                                  gpointer       value);
2044 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
2045                                                  guint           scope_id,
2046                                                  const gchar    *symbol);
2047 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
2048                                                  guint           scope_id,
2049                                                  const gchar    *symbol);
2050 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
2051                                                  guint           scope_id,
2052                                                  GHFunc          func,
2053                                                  gpointer        user_data);
2054 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
2055                                                  const gchar    *symbol);
2056 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
2057 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
2058 void            g_scanner_unexp_token           (GScanner       *scanner,
2059                                                  GTokenType     expected_token,
2060                                                  const gchar    *identifier_spec,
2061                                                  const gchar    *symbol_spec,
2062                                                  const gchar    *symbol_name,
2063                                                  const gchar    *message,
2064                                                  gint            is_error);
2065 void            g_scanner_error                 (GScanner       *scanner,
2066                                                  const gchar    *format,
2067                                                  ...) G_GNUC_PRINTF (2,3);
2068 void            g_scanner_warn                  (GScanner       *scanner,
2069                                                  const gchar    *format,
2070                                                  ...) G_GNUC_PRINTF (2,3);
2071 gint            g_scanner_stat_mode             (const gchar    *filename);
2072 /* keep downward source compatibility */
2073 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
2074   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2075 } G_STMT_END
2076 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
2077   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2078 } G_STMT_END
2079 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2080   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2081 } G_STMT_END
2082
2083
2084 /* GCompletion
2085  */
2086
2087 struct _GCompletion
2088 {
2089   GList* items;
2090   GCompletionFunc func;
2091   
2092   gchar* prefix;
2093   GList* cache;
2094 };
2095
2096 GCompletion* g_completion_new          (GCompletionFunc func);
2097 void         g_completion_add_items    (GCompletion*    cmp,
2098                                         GList*          items);
2099 void         g_completion_remove_items (GCompletion*    cmp,
2100                                         GList*          items);
2101 void         g_completion_clear_items  (GCompletion*    cmp);
2102 GList*       g_completion_complete     (GCompletion*    cmp,
2103                                         gchar*          prefix,
2104                                         gchar**         new_prefix);
2105 void         g_completion_free         (GCompletion*    cmp);
2106
2107
2108 /* GDate
2109  *
2110  * Date calculations (not time for now, to be resolved). These are a
2111  * mutant combination of Steffen Beyer's DateCalc routines
2112  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
2113  * date routines (written for in-house software).  Written by Havoc
2114  * Pennington <hp@pobox.com> 
2115  */
2116
2117 typedef guint16 GDateYear;
2118 typedef guint8  GDateDay;   /* day of the month */
2119 typedef struct _GDate GDate;
2120 /* make struct tm known without having to include time.h */
2121 struct tm;
2122
2123 /* enum used to specify order of appearance in parsed date strings */
2124 typedef enum
2125 {
2126   G_DATE_DAY   = 0,
2127   G_DATE_MONTH = 1,
2128   G_DATE_YEAR  = 2
2129 } GDateDMY;
2130
2131 /* actual week and month values */
2132 typedef enum
2133 {
2134   G_DATE_BAD_WEEKDAY  = 0,
2135   G_DATE_MONDAY       = 1,
2136   G_DATE_TUESDAY      = 2,
2137   G_DATE_WEDNESDAY    = 3,
2138   G_DATE_THURSDAY     = 4,
2139   G_DATE_FRIDAY       = 5,
2140   G_DATE_SATURDAY     = 6,
2141   G_DATE_SUNDAY       = 7
2142 } GDateWeekday;
2143 typedef enum
2144 {
2145   G_DATE_BAD_MONTH = 0,
2146   G_DATE_JANUARY   = 1,
2147   G_DATE_FEBRUARY  = 2,
2148   G_DATE_MARCH     = 3,
2149   G_DATE_APRIL     = 4,
2150   G_DATE_MAY       = 5,
2151   G_DATE_JUNE      = 6,
2152   G_DATE_JULY      = 7,
2153   G_DATE_AUGUST    = 8,
2154   G_DATE_SEPTEMBER = 9,
2155   G_DATE_OCTOBER   = 10,
2156   G_DATE_NOVEMBER  = 11,
2157   G_DATE_DECEMBER  = 12
2158 } GDateMonth;
2159
2160 #define G_DATE_BAD_JULIAN 0U
2161 #define G_DATE_BAD_DAY    0U
2162 #define G_DATE_BAD_YEAR   0U
2163
2164 /* Note: directly manipulating structs is generally a bad idea, but
2165  * in this case it's an *incredibly* bad idea, because all or part
2166  * of this struct can be invalid at any given time. Use the functions,
2167  * or you will get hosed, I promise.
2168  */
2169 struct _GDate
2170
2171   guint julian_days : 32; /* julian days representation - we use a
2172                            *  bitfield hoping that 64 bit platforms
2173                            *  will pack this whole struct in one big
2174                            *  int 
2175                            */
2176
2177   guint julian : 1;    /* julian is valid */
2178   guint dmy    : 1;    /* dmy is valid */
2179
2180   /* DMY representation */
2181   guint day    : 6;  
2182   guint month  : 4; 
2183   guint year   : 16; 
2184 };
2185
2186 /* g_date_new() returns an invalid date, you then have to _set() stuff 
2187  * to get a usable object. You can also allocate a GDate statically,
2188  * then call g_date_clear() to initialize.
2189  */
2190 GDate*       g_date_new                   (void);
2191 GDate*       g_date_new_dmy               (GDateDay     day, 
2192                                            GDateMonth   month, 
2193                                            GDateYear    year);
2194 GDate*       g_date_new_julian            (guint32      julian_day);
2195 void         g_date_free                  (GDate       *date);
2196
2197 /* check g_date_valid() after doing an operation that might fail, like
2198  * _parse.  Almost all g_date operations are undefined on invalid
2199  * dates (the exceptions are the mutators, since you need those to
2200  * return to validity).  
2201  */
2202 gboolean     g_date_valid                 (GDate       *date);
2203 gboolean     g_date_valid_day             (GDateDay     day);
2204 gboolean     g_date_valid_month           (GDateMonth   month);
2205 gboolean     g_date_valid_year            (GDateYear    year);
2206 gboolean     g_date_valid_weekday         (GDateWeekday weekday);
2207 gboolean     g_date_valid_julian          (guint32      julian_date);
2208 gboolean     g_date_valid_dmy             (GDateDay     day,
2209                                            GDateMonth   month,
2210                                            GDateYear    year);
2211
2212 GDateWeekday g_date_weekday               (GDate       *date);
2213 GDateMonth   g_date_month                 (GDate       *date);
2214 GDateYear    g_date_year                  (GDate       *date);
2215 GDateDay     g_date_day                   (GDate       *date);
2216 guint32      g_date_julian                (GDate       *date);
2217 guint        g_date_day_of_year           (GDate       *date);
2218
2219 /* First monday/sunday is the start of week 1; if we haven't reached
2220  * that day, return 0. These are not ISO weeks of the year; that
2221  * routine needs to be added.
2222  * these functions return the number of weeks, starting on the
2223  * corrsponding day
2224  */
2225 guint        g_date_monday_week_of_year   (GDate      *date);
2226 guint        g_date_sunday_week_of_year   (GDate      *date);
2227
2228 /* If you create a static date struct you need to clear it to get it
2229  * in a sane state before use. You can clear a whole array at
2230  * once with the ndates argument.
2231  */
2232 void         g_date_clear                 (GDate       *date, 
2233                                            guint        n_dates);
2234
2235 /* The parse routine is meant for dates typed in by a user, so it
2236  * permits many formats but tries to catch common typos. If your data
2237  * needs to be strictly validated, it is not an appropriate function.
2238  */
2239 void         g_date_set_parse             (GDate       *date,
2240                                            const gchar *str);
2241 void         g_date_set_time              (GDate       *date, 
2242                                            GTime        time);
2243 void         g_date_set_month             (GDate       *date, 
2244                                            GDateMonth   month);
2245 void         g_date_set_day               (GDate       *date, 
2246                                            GDateDay     day);
2247 void         g_date_set_year              (GDate       *date,
2248                                            GDateYear    year);
2249 void         g_date_set_dmy               (GDate       *date,
2250                                            GDateDay     day,
2251                                            GDateMonth   month,
2252                                            GDateYear    y);
2253 void         g_date_set_julian            (GDate       *date,
2254                                            guint32      julian_date);
2255 gboolean     g_date_is_first_of_month     (GDate       *date);
2256 gboolean     g_date_is_last_of_month      (GDate       *date);
2257
2258 /* To go forward by some number of weeks just go forward weeks*7 days */
2259 void         g_date_add_days              (GDate       *date, 
2260                                            guint        n_days);
2261 void         g_date_subtract_days         (GDate       *date, 
2262                                            guint        n_days);
2263
2264 /* If you add/sub months while day > 28, the day might change */
2265 void         g_date_add_months            (GDate       *date,
2266                                            guint        n_months);
2267 void         g_date_subtract_months       (GDate       *date,
2268                                            guint        n_months);
2269
2270 /* If it's feb 29, changing years can move you to the 28th */
2271 void         g_date_add_years             (GDate       *date,
2272                                            guint        n_years);
2273 void         g_date_subtract_years        (GDate       *date,
2274                                            guint        n_years);
2275 gboolean     g_date_is_leap_year          (GDateYear    year);
2276 guint8       g_date_days_in_month         (GDateMonth   month, 
2277                                            GDateYear    year);
2278 guint8       g_date_monday_weeks_in_year  (GDateYear    year);
2279 guint8       g_date_sunday_weeks_in_year  (GDateYear    year);
2280
2281 /* qsort-friendly (with a cast...) */
2282 gint         g_date_compare               (GDate       *lhs,
2283                                            GDate       *rhs);
2284 void         g_date_to_struct_tm          (GDate       *date,
2285                                            struct tm   *tm);
2286
2287 /* Just like strftime() except you can only use date-related formats.
2288  *   Using a time format is undefined.
2289  */
2290 gsize        g_date_strftime              (gchar       *s,
2291                                            gsize        slen,
2292                                            const gchar *format,
2293                                            GDate       *date);
2294
2295
2296 /* GRelation
2297  *
2298  * Indexed Relations.  Imagine a really simple table in a
2299  * database.  Relations are not ordered.  This data type is meant for
2300  * maintaining a N-way mapping.
2301  *
2302  * g_relation_new() creates a relation with FIELDS fields
2303  *
2304  * g_relation_destroy() frees all resources
2305  * g_tuples_destroy() frees the result of g_relation_select()
2306  *
2307  * g_relation_index() indexes relation FIELD with the provided
2308  *   equality and hash functions.  this must be done before any
2309  *   calls to insert are made.
2310  *
2311  * g_relation_insert() inserts a new tuple.  you are expected to
2312  *   provide the right number of fields.
2313  *
2314  * g_relation_delete() deletes all relations with KEY in FIELD
2315  * g_relation_select() returns ...
2316  * g_relation_count() counts ...
2317  */
2318
2319 GRelation* g_relation_new     (gint         fields);
2320 void       g_relation_destroy (GRelation   *relation);
2321 void       g_relation_index   (GRelation   *relation,
2322                                gint         field,
2323                                GHashFunc    hash_func,
2324                                GCompareFunc key_compare_func);
2325 void       g_relation_insert  (GRelation   *relation,
2326                                ...);
2327 gint       g_relation_delete  (GRelation   *relation,
2328                                gconstpointer  key,
2329                                gint         field);
2330 GTuples*   g_relation_select  (GRelation   *relation,
2331                                gconstpointer  key,
2332                                gint         field);
2333 gint       g_relation_count   (GRelation   *relation,
2334                                gconstpointer  key,
2335                                gint         field);
2336 gboolean   g_relation_exists  (GRelation   *relation,
2337                                ...);
2338 void       g_relation_print   (GRelation   *relation);
2339
2340 void       g_tuples_destroy   (GTuples     *tuples);
2341 gpointer   g_tuples_index     (GTuples     *tuples,
2342                                gint         index,
2343                                gint         field);
2344
2345
2346 /* Prime numbers.
2347  */
2348
2349 /* This function returns prime numbers spaced by approximately 1.5-2.0
2350  * and is for use in resizing data structures which prefer
2351  * prime-valued sizes.  The closest spaced prime function returns the
2352  * next largest prime, or the highest it knows about which is about
2353  * MAXINT/4.
2354  */
2355 guint      g_spaced_primes_closest (guint num);
2356
2357
2358 /* GIOChannel
2359  */
2360
2361 typedef struct _GIOFuncs GIOFuncs;
2362 typedef enum
2363 {
2364   G_IO_ERROR_NONE,
2365   G_IO_ERROR_AGAIN,
2366   G_IO_ERROR_INVAL,
2367   G_IO_ERROR_UNKNOWN
2368 } GIOError;
2369 typedef enum
2370 {
2371   G_SEEK_CUR,
2372   G_SEEK_SET,
2373   G_SEEK_END
2374 } GSeekType;
2375 typedef enum
2376 {
2377   G_IO_IN       GLIB_SYSDEF_POLLIN,
2378   G_IO_OUT      GLIB_SYSDEF_POLLOUT,
2379   G_IO_PRI      GLIB_SYSDEF_POLLPRI,
2380   G_IO_ERR      GLIB_SYSDEF_POLLERR,
2381   G_IO_HUP      GLIB_SYSDEF_POLLHUP,
2382   G_IO_NVAL     GLIB_SYSDEF_POLLNVAL
2383 } GIOCondition;
2384
2385 struct _GIOChannel
2386 {
2387   guint channel_flags;
2388   guint ref_count;
2389   GIOFuncs *funcs;
2390 };
2391
2392 typedef gboolean (*GIOFunc) (GIOChannel   *source,
2393                              GIOCondition  condition,
2394                              gpointer      data);
2395 struct _GIOFuncs
2396 {
2397   GIOError (*io_read)   (GIOChannel     *channel, 
2398                          gchar          *buf, 
2399                          guint           count,
2400                          guint          *bytes_read);
2401   GIOError (*io_write)  (GIOChannel     *channel, 
2402                          gchar          *buf, 
2403                          guint           count,
2404                          guint          *bytes_written);
2405   GIOError (*io_seek)   (GIOChannel     *channel, 
2406                          gint            offset, 
2407                          GSeekType       type);
2408   void (*io_close)      (GIOChannel     *channel);
2409   guint (*io_add_watch) (GIOChannel     *channel,
2410                          gint            priority,
2411                          GIOCondition    condition,
2412                          GIOFunc         func,
2413                          gpointer        user_data,
2414                          GDestroyNotify  notify);
2415   void (*io_free)       (GIOChannel     *channel);
2416 };
2417
2418 void        g_io_channel_init   (GIOChannel    *channel);
2419 void        g_io_channel_ref    (GIOChannel    *channel);
2420 void        g_io_channel_unref  (GIOChannel    *channel);
2421 GIOError    g_io_channel_read   (GIOChannel    *channel, 
2422                                  gchar         *buf, 
2423                                  guint          count,
2424                                  guint         *bytes_read);
2425 GIOError  g_io_channel_write    (GIOChannel    *channel, 
2426                                  gchar         *buf, 
2427                                  guint          count,
2428                                  guint         *bytes_written);
2429 GIOError  g_io_channel_seek     (GIOChannel    *channel,
2430                                  gint           offset, 
2431                                  GSeekType      type);
2432 void      g_io_channel_close    (GIOChannel    *channel);
2433 guint     g_io_add_watch_full   (GIOChannel    *channel,
2434                                  gint           priority,
2435                                  GIOCondition   condition,
2436                                  GIOFunc        func,
2437                                  gpointer       user_data,
2438                                  GDestroyNotify notify);
2439 guint    g_io_add_watch         (GIOChannel    *channel,
2440                                  GIOCondition   condition,
2441                                  GIOFunc        func,
2442                                  gpointer       user_data);
2443
2444
2445 /* Main loop
2446  */
2447 typedef struct _GTimeVal        GTimeVal;
2448 typedef struct _GSourceFuncs    GSourceFuncs;
2449 typedef struct _GMainLoop       GMainLoop;      /* Opaque */
2450
2451 struct _GTimeVal
2452 {
2453   glong tv_sec;
2454   glong tv_usec;
2455 };
2456 struct _GSourceFuncs
2457 {
2458   gboolean (*prepare)  (gpointer  source_data, 
2459                         GTimeVal *current_time,
2460                         gint     *timeout);
2461   gboolean (*check)    (gpointer  source_data,
2462                         GTimeVal *current_time);
2463   gboolean (*dispatch) (gpointer  source_data, 
2464                         GTimeVal *current_time,
2465                         gpointer  user_data);
2466   GDestroyNotify destroy;
2467 };
2468
2469 /* Standard priorities */
2470
2471 #define G_PRIORITY_HIGH            -100
2472 #define G_PRIORITY_DEFAULT          0
2473 #define G_PRIORITY_HIGH_IDLE        100
2474 #define G_PRIORITY_DEFAULT_IDLE     200
2475 #define G_PRIORITY_LOW              300
2476
2477 typedef gboolean (*GSourceFunc) (gpointer data);
2478
2479 /* Hooks for adding to the main loop */
2480 guint g_source_add                  (gint           priority, 
2481                                      gboolean       can_recurse,
2482                                      GSourceFuncs  *funcs,
2483                                      gpointer       source_data, 
2484                                      gpointer       user_data,
2485                                      GDestroyNotify notify);
2486 void g_source_remove                (guint          tag);
2487 void g_source_remove_by_user_data   (gpointer       user_data);
2488 void g_source_remove_by_source_data (gpointer       source_data);
2489
2490 void g_get_current_time             (GTimeVal      *result);
2491
2492 /* Running the main loop */
2493 GMainLoop*      g_main_new              (gboolean        is_running);
2494 void            g_main_run              (GMainLoop      *loop);
2495 void            g_main_quit             (GMainLoop      *loop);
2496 void            g_main_destroy          (GMainLoop      *loop);
2497 gboolean        g_main_is_running       (GMainLoop      *loop);
2498
2499 /* Run a single iteration of the mainloop. If block is FALSE,
2500  * will never block
2501  */
2502 gboolean        g_main_iteration        (gboolean       may_block);
2503
2504 /* See if any events are pending */
2505 gboolean        g_main_pending          (void);
2506
2507 /* Idles and timeouts */
2508 guint           g_timeout_add_full      (gint           priority,
2509                                          guint          interval, 
2510                                          GSourceFunc    function,
2511                                          gpointer       data,
2512                                          GDestroyNotify notify);
2513 guint           g_timeout_add           (guint          interval,
2514                                          GSourceFunc    function,
2515                                          gpointer       data);
2516 guint           g_idle_add              (GSourceFunc    function,
2517                                          gpointer       data);
2518 guint           g_idle_add_full         (gint           priority,
2519                                          GSourceFunc    function,
2520                                          gpointer       data,
2521                                          GDestroyNotify destroy);
2522
2523 /* GPollFD
2524  *
2525  * Unix-specific IO and main loop calls
2526  */
2527
2528 typedef struct _GPollFD GPollFD;
2529 typedef gint    (*GPollFunc)    (GPollFD *ufds,
2530                                  guint    nfsd,
2531                                  gint     timeout);
2532 struct _GPollFD
2533 {
2534   gint          fd;
2535   gushort       events;
2536   gushort       revents;
2537 };
2538
2539 void        g_main_add_poll          (GPollFD    *fd,
2540                                       gint        priority);
2541 void        g_main_remove_poll       (GPollFD    *fd);
2542 void        g_main_set_poll_func     (GPollFunc   func);
2543
2544 GIOChannel* g_io_channel_unix_new    (int         fd);
2545 gint        g_io_channel_unix_get_fd (GIOChannel *channel);
2546
2547
2548 /* old IO Channels */
2549 #if 0
2550 /* IO Channels.
2551  * These are used for plug-in communication in the GIMP, for instance.
2552  * On Unix, it's simply an encapsulated file descriptor (a pipe).
2553  * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2554  * of the writable end) a thread id to post a message to when you have written
2555  * stuff.
2556  */
2557 struct _GIOChannel
2558 {
2559   gint fd;                      /* file handle (pseudo such in Win32) */
2560 #ifdef NATIVE_WIN32
2561   guint peer;                   /* thread to post message to */
2562   guint peer_fd;                /* read handle (in the other process) */
2563   guint offset;                 /* counter of accumulated bytes, to
2564                                  * be included in the message posted
2565                                  * so we keep in sync.
2566                                  */
2567   guint need_wakeups;           /* in output channels whether the reader
2568                                  * needs wakeups
2569                                  */
2570 #endif
2571 };
2572 GIOChannel *g_iochannel_new            (gint        fd);
2573 void        g_iochannel_free           (GIOChannel *channel);
2574 void        g_iochannel_close_and_free (GIOChannel *channel);
2575 void        g_iochannel_wakeup_peer    (GIOChannel *channel);
2576 #ifndef NATIVE_WIN32
2577 #  define   g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2578 #endif
2579 #endif  /* old IO Channels */
2580
2581
2582 /* Windows emulation stubs for common unix functions
2583  */
2584 #ifdef NATIVE_WIN32
2585 #  define MAXPATHLEN 1024
2586 #  ifdef _MSC_VER
2587 typedef int pid_t;
2588
2589 /* These POSIXish functions are available in the Microsoft C library
2590  * prefixed with underscore (which of course technically speaking is
2591  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2592  * prevents Microsoft from practically requiring you to include
2593  * <windows.h> every now and then...).
2594  *
2595  * You still need to include the appropriate headers to get the
2596  * prototypes, <io.h> or <direct.h>.
2597  *
2598  * For some functions, we provide emulators in glib, which are prefixed
2599  * with gwin_.
2600  */
2601 #    define getcwd              _getcwd
2602 #    define getpid              _getpid
2603 #    define access              _access
2604 #    define open                _open
2605 #    define read                _read
2606 #    define write               _write
2607 #    define lseek               _lseek
2608 #    define close               _close
2609 #    define pipe(phandles)      _pipe (phandles, 4096, _O_BINARY)
2610 #    define popen               _popen
2611 #    define pclose              _pclose
2612 #    define fdopen              _fdopen
2613 #    define ftruncate(fd, size) gwin_ftruncate (fd, size)
2614 #    define opendir             gwin_opendir
2615 #    define readdir             gwin_readdir
2616 #    define rewinddir           gwin_rewinddir
2617 #    define closedir            gwin_closedir
2618 #    define NAME_MAX 255
2619 struct DIR
2620 {
2621   gchar    *dir_name;
2622   gboolean  just_opened;
2623   guint     find_file_handle;
2624   gpointer  find_file_data;
2625 };
2626 typedef struct DIR DIR;
2627 struct dirent
2628 {
2629   gchar  d_name[NAME_MAX + 1];
2630 };
2631 /* emulation functions */
2632 extern int      gwin_ftruncate  (gint            f,
2633                                  guint           size);
2634 DIR*            gwin_opendir    (const gchar    *dirname);
2635 struct dirent*  gwin_readdir    (DIR            *dir);
2636 void            gwin_rewinddir  (DIR            *dir);
2637 gint            gwin_closedir   (DIR            *dir);
2638 #  endif /* _MSC_VER */
2639 #endif   /* NATIVE_WIN32 */
2640
2641
2642 /* GLib Thread support
2643  */
2644 typedef struct _GMutex          GMutex;
2645 typedef struct _GCond           GCond;
2646 typedef struct _GPrivate        GPrivate;
2647 typedef struct _GStaticPrivate  GStaticPrivate;
2648 typedef struct _GThreadFunctions GThreadFunctions;
2649 struct _GThreadFunctions
2650 {
2651   GMutex*  (*mutex_new)       (void);
2652   void     (*mutex_lock)      (GMutex           *mutex);
2653   gboolean (*mutex_trylock)   (GMutex           *mutex);
2654   void     (*mutex_unlock)    (GMutex           *mutex);
2655   void     (*mutex_free)      (GMutex           *mutex);
2656   GCond*   (*cond_new)        (void);
2657   void     (*cond_signal)     (GCond            *cond);
2658   void     (*cond_broadcast)  (GCond            *cond);
2659   void     (*cond_wait)       (GCond            *cond,
2660                                GMutex           *mutex);
2661   gboolean (*cond_timed_wait) (GCond            *cond,
2662                                GMutex           *mutex, 
2663                                GTimeVal         *end_time);
2664   void      (*cond_free)      (GCond            *cond);
2665   GPrivate* (*private_new)    (GDestroyNotify    destructor);
2666   gpointer  (*private_get)    (GPrivate         *private_key);
2667   void      (*private_set)    (GPrivate         *private_key,
2668                                gpointer          data);
2669 };
2670
2671 GUTILS_C_VAR GThreadFunctions   g_thread_functions_for_glib_use;
2672 GUTILS_C_VAR gboolean           g_thread_use_default_impl;
2673 GUTILS_C_VAR gboolean           g_threads_got_initialized;
2674
2675 /* initializes the mutex/cond/private implementation for glib, might
2676  * only be called once, and must not be called directly or indirectly
2677  * from another glib-function, e.g. as a callback.
2678  */
2679 void    g_thread_init   (GThreadFunctions       *vtable);
2680
2681 /* internal function for fallback static mutex implementation */
2682 GMutex* g_static_mutex_get_mutex_impl   (GMutex **mutex);
2683
2684 /* shorthands for conditional and unconditional function calls */
2685 #define G_THREAD_UF(name, arglist) \
2686     (*g_thread_functions_for_glib_use . name) arglist
2687 #define G_THREAD_CF(name, fail, arg) \
2688     (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
2689 /* keep in mind, all those mutexes and static mutexes are not 
2690  * recursive in general, don't rely on that
2691  */
2692 #define g_thread_supported()    (g_threads_got_initialized)
2693 #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
2694 #define g_mutex_lock(mutex)      G_THREAD_CF (mutex_lock,     (void)0, (mutex))
2695 #define g_mutex_trylock(mutex)   G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
2696 #define g_mutex_unlock(mutex)    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
2697 #define g_mutex_free(mutex)      G_THREAD_CF (mutex_free,     (void)0, (mutex))
2698 #define g_cond_new()             G_THREAD_UF (cond_new,       ())
2699 #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
2700 #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
2701 #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait,      (void)0, (cond, \
2702                                                                         mutex))
2703 #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
2704 #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
2705                                                               TRUE, \
2706                                                               (cond, mutex, \
2707                                                                abs_time))
2708 #define g_private_new(destructor)         G_THREAD_UF (private_new, (destructor))
2709 #define g_private_get(private_key)        G_THREAD_CF (private_get, \
2710                                                        ((gpointer)private_key), \
2711                                                        (private_key))
2712 #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
2713                                                        (void) (private_key = \
2714                                                         (GPrivate*) (value)), \
2715                                                        (private_key, value))
2716 /* GStaticMutexes can be statically initialized with the value
2717  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
2718  * much easier, than having to explicitly allocate the mutex before
2719  * use
2720  */
2721 #define g_static_mutex_lock(mutex) \
2722     g_mutex_lock (g_static_mutex_get_mutex (mutex))
2723 #define g_static_mutex_trylock(mutex) \
2724     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
2725 #define g_static_mutex_unlock(mutex) \
2726     g_mutex_unlock (g_static_mutex_get_mutex (mutex)) 
2727 struct _GStaticPrivate
2728 {
2729   guint index;
2730 };
2731 #define G_STATIC_PRIVATE_INIT { 0 }
2732 gpointer g_static_private_get (GStaticPrivate   *private_key);
2733 void     g_static_private_set (GStaticPrivate   *private_key, 
2734                                gpointer          data,
2735                                GDestroyNotify    notify);
2736
2737 /* these are some convenience macros that expand to nothing if GLib was
2738  * configured with --deisable-threads. for using StaticMutexes, you
2739  * declare them with G_LOCK_DECLARE_STATIC (name) or G_LOCK_DECLARE (name)
2740  * if you need to export the mutex. name is a unique identifier for the
2741  * protected varibale or code portion. locking, testing and unlocking of
2742  * such mutexes can be done with G_LOCK(), G_UNLOCK() and G_TRYLOCK()
2743  * respectively.
2744  */
2745 extern void glib_dummy_decl (void);
2746 #define G_LOCK_NAME(name)               (g__ ## name ## _lock)
2747 #ifdef  G_THREADS_ENABLED
2748 #  define G_LOCK_DECLARE_STATIC(name)   static G_LOCK_DECLARE (name)
2749 #  define G_LOCK_DECLARE(name)          \
2750     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT 
2751
2752 #  ifdef G_DEBUG_LOCKS
2753 #    define G_LOCK(name)                G_STMT_START{             \
2754         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
2755                "file %s: line %d (%s): locking: %s ",             \
2756                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
2757                #name);                                            \
2758         g_static_mutex_lock (G_LOCK_NAME (name));                 \
2759      }G_STMT_END
2760 #    define G_UNLOCK(name)              G_STMT_START{             \
2761         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
2762                "file %s: line %d (%s): unlocking: %s ",           \
2763                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
2764                #name);                                            \
2765        g_static_mutex_unlock (G_LOCK_NAME (name));                \
2766      }G_STMT_END
2767 #    define G_TRYLOCK(name)             G_STMT_START{             \
2768         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,                   \
2769                "file %s: line %d (%s): try locking: %s ",         \
2770                __FILE__,        __LINE__, G_GNUC_PRETTY_FUNCTION, \
2771                #name);                                            \
2772      }G_STMT_END,       g_static_mutex_trylock (G_LOCK_NAME (name))
2773 #  else  /* !G_DEBUG_LOCKS */
2774 #    define G_LOCK(name) g_static_mutex_lock       (G_LOCK_NAME (name)) 
2775 #    define G_UNLOCK(name) g_static_mutex_unlock   (G_LOCK_NAME (name))
2776 #    define G_TRYLOCK(name) g_static_mutex_trylock (G_LOCK_NAME (name))
2777 #  endif /* !G_DEBUG_LOCKS */
2778 #else   /* !G_THREADS_ENABLED */
2779 #  define G_LOCK_DECLARE_STATIC(name)   extern void glib_dummy_decl (void)
2780 #  define G_LOCK_DECLARE(name)          extern void glib_dummy_decl (void)
2781 #  define G_LOCK(name)
2782 #  define G_UNLOCK(name)
2783 #  define G_TRYLOCK(name)               (FALSE)
2784 #endif  /* !G_THREADS_ENABLED */
2785
2786 #ifdef __cplusplus
2787 }
2788 #endif /* __cplusplus */
2789
2790
2791 #endif /* __G_LIB_H__ */