4ce67979bd9d5bf5f1445ebbaedadde3feb6f127
[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
23  */
24 #include <glibconfig.h>
25
26 /* include varargs functions for assertment macros
27  */
28 #include <stdarg.h>
29
30 /* optionally feature DMALLOC memory allocation debugger
31  */
32 #ifdef USE_DMALLOC
33 #include "dmalloc.h"
34 #endif
35
36
37 /* glib provides definitions for the extrema of many
38  *  of the standard types. These are:
39  * G_MINFLOAT
40  * G_MAXFLOAT
41  * G_MINDOUBLE
42  * G_MAXDOUBLE
43  * G_MINSHORT
44  * G_MAXSHORT
45  * G_MININT
46  * G_MAXINT
47  * G_MINLONG
48  * G_MAXLONG
49  */
50
51 #ifdef HAVE_FLOAT_H
52
53 #include <float.h>
54
55 #define G_MINFLOAT   FLT_MIN
56 #define G_MAXFLOAT   FLT_MAX
57 #define G_MINDOUBLE  DBL_MIN
58 #define G_MAXDOUBLE  DBL_MAX
59
60 #elif HAVE_VALUES_H
61
62 #include <values.h>
63
64 #define G_MINFLOAT  MINFLOAT
65 #define G_MAXFLOAT  MAXFLOAT
66 #define G_MINDOUBLE MINDOUBLE
67 #define G_MAXDOUBLE MAXDOUBLE
68
69 #endif /* HAVE_VALUES_H */
70
71 #ifdef HAVE_LIMITS_H
72
73 #include <limits.h>
74
75 #define G_MINSHORT  SHRT_MIN
76 #define G_MAXSHORT  SHRT_MAX
77 #define G_MININT    INT_MIN
78 #define G_MAXINT    INT_MAX
79 #define G_MINLONG   LONG_MIN
80 #define G_MAXLONG   LONG_MAX
81
82 #elif HAVE_VALUES_H
83
84 #ifdef HAVE_FLOAT_H
85 #include <values.h>
86 #endif /* HAVE_FLOAT_H */
87
88 #define G_MINSHORT  MINSHORT
89 #define G_MAXSHORT  MAXSHORT
90 #define G_MININT    MININT
91 #define G_MAXINT    MAXINT
92 #define G_MINLONG   MINLONG
93 #define G_MAXLONG   MAXLONG
94
95 #endif /* HAVE_VALUES_H */
96
97
98 /* the #pragma } statment is used to fix up emacs' c-mode which gets
99  * confused by extern "C" {. the ansi standard says that compilers
100  * have to ignore #pragma directives that they don't know about,
101  * so we should be save in using this.
102  */
103 #ifdef __cplusplus
104 extern "C" {
105 #pragma }
106 #endif /* __cplusplus */
107
108
109 /* Provide definitions for some commonly used macros.
110  *  Some of them are only provided if they haven't already
111  *  been defined. It is assumed that if they are already
112  *  defined then the current definition is correct.
113  */
114 #ifndef NULL
115 #define NULL    ((void*) 0)
116 #endif
117
118 #ifndef FALSE
119 #define FALSE   (0)
120 #endif
121
122 #ifndef TRUE
123 #define TRUE    (!FALSE)
124 #endif
125
126 #undef  MAX
127 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
128
129 #undef  MIN
130 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
131
132 #undef  ABS
133 #define ABS(a)     (((a) < 0) ? -(a) : (a))
134
135 #undef  CLAMP
136 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
137
138
139 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
140  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
141  */
142 #if !defined (G_VA_COPY)
143 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
144 #  define G_VA_COPY(ap1, ap2)     (*(ap1) = *(ap2))
145 #  elif defined (G_VA_COPY_AS_ARRAY)
146 #  define G_VA_COPY(ap1, ap2)     g_memmove ((ap1), (ap2), sizeof (va_list))
147 #  else /* va_list is a pointer */
148 #  define G_VA_COPY(ap1, ap2)     ((ap1) = (ap2))
149 #  endif /* va_list is a pointer */
150 #endif /* !G_VA_COPY */
151
152
153 /* Provide simple enum value macro wrappers that ease automated
154  * enum value stringification code. [abandoned]
155  */
156 #if     !defined (G_CODE_GENERATION)
157 #define G_ENUM( EnumerationName )               EnumerationName
158 #define G_FLAGS( EnumerationName )              EnumerationName
159 #define G_NV( VALUE_NAME , value_nick, VALUE)   VALUE_NAME = (VALUE)
160 #define G_SV( VALUE_NAME, value_nick )          VALUE_NAME
161 #else   /* G_CODE_GENERATION */
162 #define G_ENUM( EnumerationName )               G_ENUM_E + EnumerationName +
163 #define G_FLAGS( EnumerationName )              G_ENUM_F + EnumerationName +
164 #define G_NV( VALUE_NAME , value_nick, VALUE)   G_ENUM_V + VALUE_NAME + value_nick +
165 #define G_SV( VALUE_NAME, value_nick )          G_ENUM_V + VALUE_NAME + value_nick +
166 #endif  /* G_CODE_GENERATION */
167
168
169 /* inlining hassle. for compilers that don't allow the `inline' keyword,
170  * mostly because of strict ANSI C compliance or dumbness, we try to fall
171  * back to either `__inline__' or `__inline'.
172  * we define G_CAN_INLINE, if the compiler seems to be actually
173  * *capable* to do function inlining, in which case inline function bodys
174  * do make sense. we also define G_INLINE_FUNC to properly export the
175  * function prototypes if no inlinig can be performed.
176  * we special case most of the stuff, so inline functions can have a normal
177  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
178  */
179 #ifndef G_INLINE_FUNC
180 #  define G_CAN_INLINE 1
181 #endif
182 #ifdef G_HAVE_INLINE
183 #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
184 #    undef inline
185 #    define inline __inline__
186 #  endif
187 #else /* !G_HAVE_INLINE */
188 #  undef inline
189 #  if defined (G_HAVE___INLINE__)
190 #    define inline __inline__
191 #  else /* !inline && !__inline__ */
192 #    if defined (G_HAVE___INLINE)
193 #      define inline __inline
194 #    else /* !inline && !__inline__ && !__inline */
195 #      define inline /* don't inline, then */
196 #      ifndef G_INLINE_FUNC
197 #        undef G_CAN_INLINE
198 #      endif
199 #    endif
200 #  endif
201 #endif
202 #ifndef G_INLINE_FUNC
203 #  ifdef __GNUC__
204 #    ifdef __OPTIMIZE__
205 #      define G_INLINE_FUNC extern inline
206 #    else
207 #      undef G_CAN_INLINE
208 #      define G_INLINE_FUNC extern
209 #    endif
210 #  else /* !__GNUC__ */
211 #    ifdef G_CAN_INLINE
212 #      define G_INLINE_FUNC static inline
213 #    else
214 #      define G_INLINE_FUNC extern
215 #    endif
216 #  endif /* !__GNUC__ */
217 #endif /* !G_INLINE_FUNC */
218
219
220 /* Provide simple macro statement wrappers (adapted from Perl):
221  *  G_STMT_START { statements; } G_STMT_END;
222  *  can be used as a single statement, as in
223  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
224  *
225  *  For gcc we will wrap the statements within `({' and `})' braces.
226  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
227  *  and otherwise within `do' and `while (0)'.
228  */
229 #if !(defined (G_STMT_START) && defined (G_STMT_END))
230 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
231 #    define G_STMT_START        (void)(
232 #    define G_STMT_END          )
233 #  else
234 #    if (defined (sun) || defined (__sun__))
235 #      define G_STMT_START      if (1)
236 #      define G_STMT_END        else (void)0
237 #    else
238 #      define G_STMT_START      do
239 #      define G_STMT_END        while (0)
240 #    endif
241 #  endif
242 #endif
243
244
245 /* Provide macros to feature the GCC function attribute.
246  */
247 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
248 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
249   __attribute__((format (printf, format_idx, arg_idx)))
250 #define G_GNUC_SCANF( format_idx, arg_idx )     \
251   __attribute__((format (scanf, format_idx, arg_idx)))
252 #define G_GNUC_FORMAT( arg_idx )                \
253   __attribute__((format_arg (arg_idx)))
254 #define G_GNUC_NORETURN                         \
255   __attribute__((noreturn))
256 #define G_GNUC_CONST                            \
257   __attribute__((const))
258 #else   /* !__GNUC__ */
259 #define G_GNUC_PRINTF( format_idx, arg_idx )
260 #define G_GNUC_SCANF( format_idx, arg_idx )
261 #define G_GNUC_FORMAT( arg_idx )
262 #define G_GNUC_NORETURN
263 #define G_GNUC_CONST
264 #endif  /* !__GNUC__ */
265
266
267 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
268  * macros, so we can refer to them as strings unconditionally.
269  */
270 #ifdef  __GNUC__
271 #define G_GNUC_FUNCTION         (__FUNCTION__)
272 #define G_GNUC_PRETTY_FUNCTION  (__PRETTY_FUNCTION__)
273 #else   /* !__GNUC__ */
274 #define G_GNUC_FUNCTION         ("")
275 #define G_GNUC_PRETTY_FUNCTION  ("")
276 #endif  /* !__GNUC__ */
277
278
279 /* we try to provide a usefull equivalent for ATEXIT if it is
280  * not defined, but use is actually abandoned. people should
281  * use g_atexit() instead.
282  * keep this in sync with gutils.c.
283  */
284 #ifndef ATEXIT
285 #  ifdef HAVE_ATEXIT
286 #    ifdef NeXT /* @#%@! NeXTStep */
287 #      define ATEXIT(proc)   (!atexit (proc))
288 #    else /* !NeXT */
289 #      define ATEXIT(proc)   (atexit (proc))
290 #    endif /* !NeXT */
291 #  elif defined (HAVE_ON_EXIT)
292 #    define ATEXIT(proc)   (on_exit ((void (*)(int, void *))(proc), NULL))
293 #  else
294 #  error Could not determine proper atexit() implementation
295 #  endif
296 #else
297 #  define G_NATIVE_ATEXIT
298 #endif /* ATEXIT */
299
300 /* Hacker macro to place breakpoints for x86 machines.
301  * Actual use is strongly deprecated of course ;)
302  */
303 #if     defined (__i386__) && defined (__GNUC__)
304 #define G_BREAKPOINT()          G_STMT_START{ __asm__ volatile ("int $03"); }G_STMT_END
305 #else   /* !__i386__ */
306 #define G_BREAKPOINT()
307 #endif  /* __i386__ */
308
309
310 /* Provide macros for easily allocating memory. The macros
311  *  will cast the allocated memory to the specified type
312  *  in order to avoid compiler warnings. (Makes the code neater).
313  */
314
315 #ifdef __DMALLOC_H__
316
317 #define g_new(type, count)       (ALLOC (type, count))
318 #define g_new0(type, count)      (CALLOC (type, count))
319
320 #else /* __DMALLOC_H__ */
321
322 #define g_new(type, count)        \
323     ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
324 #define g_new0(type, count)       \
325     ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
326 #endif /* __DMALLOC_H__ */
327
328 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
329   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
330                    sizeof (type), \
331                    sizeof (type) * (pre_alloc), \
332                    (alloc_type)) \
333 )
334 #define g_chunk_new(type, chunk)        ( \
335   (type *) g_mem_chunk_alloc (chunk) \
336 )
337 #define g_chunk_new0(type, chunk)       ( \
338   (type *) g_mem_chunk_alloc0 (chunk) \
339 )
340 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
341   g_mem_chunk_free ((mem_chunk), (mem)); \
342 } G_STMT_END
343
344
345 #define g_string(x) #x
346
347
348 /* Provide macros for error handling. The "assert" macros will
349  *  exit on failure. The "return" macros will exit the current
350  *  function. Two different definitions are given for the macros
351  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
352  *  __PRETTY_FUNCTION__ capability.
353  */
354
355 #ifdef G_DISABLE_ASSERT
356
357 #define g_assert(expr)
358 #define g_assert_not_reached()
359
360 #else /* !G_DISABLE_ASSERT */
361
362 #ifdef __GNUC__
363
364 #define g_assert(expr)                  G_STMT_START{           \
365      if (!(expr))                                               \
366        g_log (G_LOG_DOMAIN,                                     \
367               G_LOG_LEVEL_ERROR,                                \
368               "file %s: line %d (%s): assertion failed: (%s)",  \
369               __FILE__,                                         \
370               __LINE__,                                         \
371               __PRETTY_FUNCTION__,                              \
372               #expr);                   }G_STMT_END
373
374 #define g_assert_not_reached()          G_STMT_START{           \
375      g_log (G_LOG_DOMAIN,                                       \
376             G_LOG_LEVEL_ERROR,                                  \
377             "file %s: line %d (%s): should not be reached",     \
378             __FILE__,                                           \
379             __LINE__,                                           \
380             __PRETTY_FUNCTION__);       }G_STMT_END
381
382 #else /* !__GNUC__ */
383
384 #define g_assert(expr)                  G_STMT_START{           \
385      if (!(expr))                                               \
386        g_log (G_LOG_DOMAIN,                                     \
387               G_LOG_LEVEL_ERROR,                                \
388               "file %s: line %d: assertion failed: (%s)",       \
389               __FILE__,                                         \
390               __LINE__,                                         \
391               #expr);                   }G_STMT_END
392
393 #define g_assert_not_reached()          G_STMT_START{   \
394      g_log (G_LOG_DOMAIN,                               \
395             G_LOG_LEVEL_ERROR,                          \
396             "file %s: line %d: should not be reached",  \
397             __FILE__,                                   \
398             __LINE__);          }G_STMT_END
399
400 #endif /* __GNUC__ */
401
402 #endif /* !G_DISABLE_ASSERT */
403
404
405 #ifdef G_DISABLE_CHECKS
406
407 #define g_return_if_fail(expr)
408 #define g_return_val_if_fail(expr,val)
409
410 #else /* !G_DISABLE_CHECKS */
411
412 #ifdef __GNUC__
413
414 #define g_return_if_fail(expr)          G_STMT_START{                   \
415      if (!(expr))                                                       \
416        {                                                                \
417          g_log (G_LOG_DOMAIN,                                           \
418                 G_LOG_LEVEL_CRITICAL,                                   \
419                 "file %s: line %d (%s): assertion `%s' failed.",        \
420                 __FILE__,                                               \
421                 __LINE__,                                               \
422                 __PRETTY_FUNCTION__,                                    \
423                 #expr);                                                 \
424          return;                                                        \
425        };                               }G_STMT_END
426
427 #define g_return_val_if_fail(expr,val)  G_STMT_START{                   \
428      if (!(expr))                                                       \
429        {                                                                \
430          g_log (G_LOG_DOMAIN,                                           \
431                 G_LOG_LEVEL_CRITICAL,                                   \
432                 "file %s: line %d (%s): assertion `%s' failed.",        \
433                 __FILE__,                                               \
434                 __LINE__,                                               \
435                 __PRETTY_FUNCTION__,                                    \
436                 #expr);                                                 \
437          return val;                                                    \
438        };                               }G_STMT_END
439
440 #else /* !__GNUC__ */
441
442 #define g_return_if_fail(expr)          G_STMT_START{           \
443      if (!(expr))                                               \
444        {                                                        \
445          g_log (G_LOG_DOMAIN,                                   \
446                 G_LOG_LEVEL_CRITICAL,                           \
447                 "file %s: line %d: assertion `%s' failed.",     \
448                 __FILE__,                                       \
449                 __LINE__,                                       \
450                 #expr);                                         \
451          return;                                                \
452        };                               }G_STMT_END
453
454 #define g_return_val_if_fail(expr, val) G_STMT_START{           \
455      if (!(expr))                                               \
456        {                                                        \
457          g_log (G_LOG_DOMAIN,                                   \
458                 G_LOG_LEVEL_CRITICAL,                           \
459                 "file %s: line %d: assertion `%s' failed.",     \
460                 __FILE__,                                       \
461                 __LINE__,                                       \
462                 #expr);                                         \
463          return val;                                            \
464        };                               }G_STMT_END
465
466 #endif /* !__GNUC__ */
467
468 #endif /* !G_DISABLE_CHECKS */
469
470
471 /* Provide type definitions for commonly used types.
472  *  These are useful because a "gint8" can be adjusted
473  *  to be 1 byte (8 bits) on all platforms. Similarly and
474  *  more importantly, "gint32" can be adjusted to be
475  *  4 bytes (32 bits) on all platforms.
476  */
477
478 typedef char   gchar;
479 typedef short  gshort;
480 typedef long   glong;
481 typedef int    gint;
482 typedef gint   gboolean;
483
484 typedef unsigned char   guchar;
485 typedef unsigned short  gushort;
486 typedef unsigned long   gulong;
487 typedef unsigned int    guint;
488
489 typedef float   gfloat;
490 typedef double  gdouble;
491
492 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
493  * Since gldouble isn't used anywhere, just disable it for now */
494
495 #if 0
496 #ifdef HAVE_LONG_DOUBLE
497 typedef long double gldouble;
498 #else /* HAVE_LONG_DOUBLE */
499 typedef double gldouble;
500 #endif /* HAVE_LONG_DOUBLE */
501 #endif /* 0 */
502
503 typedef void* gpointer;
504 typedef const void *gconstpointer;
505
506 #if (SIZEOF_CHAR == 1)
507 typedef signed char     gint8;
508 typedef unsigned char   guint8;
509 #endif /* SIZEOF_CHAR */
510
511 #if (SIZEOF_SHORT == 2)
512 typedef signed short    gint16;
513 typedef unsigned short  guint16;
514 #endif /* SIZEOF_SHORT */
515
516 #if (SIZEOF_INT == 4)
517 typedef signed int      gint32;
518 typedef unsigned int    guint32;
519 #elif (SIZEOF_LONG == 4)
520 typedef signed long     gint32;
521 typedef unsigned long   guint32;
522 #endif /* SIZEOF_INT */
523
524 #if (SIZEOF_LONG == 8)
525 #define HAVE_GINT64 1
526 typedef signed long gint64;
527 typedef unsigned long guint64;
528 #elif (SIZEOF_LONG_LONG == 8)
529 #define HAVE_GINT64 1
530 typedef signed long long gint64;
531 typedef unsigned long long guint64;
532 #else
533 /* No gint64 */
534 #undef HAVE_GINT64
535 #endif
536
537
538 /* Define macros for storing integers inside pointers
539  */
540 #if (SIZEOF_INT == SIZEOF_VOID_P)
541
542 #define GPOINTER_TO_INT(p) ((gint)(p))
543 #define GPOINTER_TO_UINT(p) ((guint)(p))
544
545 #define GINT_TO_POINTER(i) ((gpointer)(i))
546 #define GUINT_TO_POINTER(u) ((gpointer)(u))
547
548 #elif (SIZEOF_LONG == SIZEOF_VOID_P)
549
550 #define GPOINTER_TO_INT(p) ((gint)(glong)(p))
551 #define GPOINTER_TO_UINT(p) ((guint)(gulong)(p))
552
553 #define GINT_TO_POINTER(i) ((gpointer)(glong)(i))
554 #define GUINT_TO_POINTER(u) ((gpointer)(gulong)(u))
555
556 #else
557 #error SIZEOF_VOID_P unknown - This should never happen
558 #endif
559
560 typedef gint32  gssize;
561 typedef guint32 gsize;
562 typedef guint32 GQuark;
563 typedef gint32  GTime;
564
565
566 /* Glib version.
567  */
568 extern const guint glib_major_version;
569 extern const guint glib_minor_version;
570 extern const guint glib_micro_version;
571 extern const guint glib_interface_age;
572 extern const guint glib_binary_age;
573
574
575 /* Forward declarations of glib types.
576  */
577
578 typedef struct _GList           GList;
579 typedef struct _GSList          GSList;
580 typedef struct _GHashTable      GHashTable;
581 typedef struct _GCache          GCache;
582 typedef struct _GTree           GTree;
583 typedef struct _GTimer          GTimer;
584 typedef struct _GMemChunk       GMemChunk;
585 typedef struct _GListAllocator  GListAllocator;
586 typedef struct _GStringChunk    GStringChunk;
587 typedef struct _GString         GString;
588 typedef struct _GArray          GArray;
589 typedef struct _GPtrArray       GPtrArray;
590 typedef struct _GByteArray      GByteArray;
591 typedef struct _GDebugKey       GDebugKey;
592 typedef struct _GScannerConfig  GScannerConfig;
593 typedef struct _GScanner        GScanner;
594 typedef union  _GValue          GValue;
595 typedef struct _GCompletion     GCompletion;
596 typedef struct _GRelation       GRelation;
597 typedef struct _GTuples         GTuples;
598 typedef struct _GNode           GNode;
599
600
601 typedef enum
602 {
603   G_TRAVERSE_LEAFS      = 1 << 0,
604   G_TRAVERSE_NON_LEAFS  = 1 << 1,
605   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
606   G_TRAVERSE_MASK       = 0x03
607 } GTraverseFlags;
608
609 typedef enum
610 {
611   G_IN_ORDER,
612   G_PRE_ORDER,
613   G_POST_ORDER,
614   G_LEVEL_ORDER
615 } GTraverseType;
616
617 /* Log level shift offset for user defined
618  * log levels (0-7 are used by GLib).
619  */
620 #define G_LOG_LEVEL_USER_SHIFT  (8)
621
622 /* Glib log levels and flags.
623  */
624 typedef enum
625 {
626   /* log flags */
627   G_LOG_FLAG_RECURSION          = 1 << 0,
628   G_LOG_FLAG_FATAL              = 1 << 1,
629
630   /* GLib log levels */
631   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
632   G_LOG_LEVEL_CRITICAL          = 1 << 3,
633   G_LOG_LEVEL_WARNING           = 1 << 4,
634   G_LOG_LEVEL_MESSAGE           = 1 << 5,
635   G_LOG_LEVEL_INFO              = 1 << 6,
636   G_LOG_LEVEL_DEBUG             = 1 << 7,
637
638   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
639 } GLogLevelFlags;
640
641 /* GLib log levels that are considered fatal by default */
642 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
643
644
645 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
646 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
647 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
648 typedef gint            (*GCompareFunc)         (gconstpointer  a,
649                                                  gconstpointer  b);
650 typedef gchar*          (*GCompletionFunc)      (gpointer);
651 typedef void            (*GDestroyNotify)       (gpointer       data);
652 typedef void            (*GFunc)                (gpointer       data,
653                                                  gpointer       user_data);
654 typedef guint           (*GHashFunc)            (gconstpointer  key);
655 typedef void            (*GHFunc)               (gpointer       key,
656                                                  gpointer       value,
657                                                  gpointer       user_data);
658 typedef void            (*GLogFunc)             (const gchar   *log_domain,
659                                                  GLogLevelFlags log_level,
660                                                  const gchar   *message,
661                                                  gpointer       user_data);
662 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
663                                                  gpointer       data);
664 typedef void            (*GNodeForeachFunc)     (GNode         *node,
665                                                  gpointer       data);
666 typedef gint            (*GSearchFunc)          (gpointer       key,
667                                                  gpointer       data);
668 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
669                                                  gchar         *message,
670                                                  gint           error);
671 typedef gint            (*GTraverseFunc)        (gpointer       key,
672                                                  gpointer       value,
673                                                  gpointer       data);
674 typedef void            (*GVoidFunc)            (void);
675
676
677 struct _GList
678 {
679   gpointer data;
680   GList *next;
681   GList *prev;
682 };
683
684 struct _GSList
685 {
686   gpointer data;
687   GSList *next;
688 };
689
690 struct _GString
691 {
692   gchar *str;
693   gint len;
694 };
695
696 struct _GArray
697 {
698   gchar *data;
699   guint len;
700 };
701
702 struct _GByteArray
703 {
704   guint8 *data;
705   guint   len;
706 };
707
708 struct _GPtrArray
709 {
710   gpointer *pdata;
711   guint     len;
712 };
713
714 struct _GTuples
715 {
716   guint len;
717 };
718
719 struct _GDebugKey
720 {
721   gchar *key;
722   guint  value;
723 };
724
725 struct _GCache { gint dummy; };
726 struct _GTree { gint dummy; };
727 struct _GTimer { gint dummy; };
728 struct _GMemChunk { gint dummy; };
729 struct _GListAllocator { gint dummy; };
730 struct _GStringChunk { gint dummy; };
731
732
733 /* Doubly linked lists
734  */
735 GList* g_list_alloc             (void);
736 void   g_list_free              (GList          *list);
737 void   g_list_free_1            (GList          *list);
738 GList* g_list_append            (GList          *list,
739                                  gpointer        data);
740 GList* g_list_prepend           (GList          *list,
741                                  gpointer        data);
742 GList* g_list_insert            (GList          *list,
743                                  gpointer        data,
744                                  gint            position);
745 GList* g_list_insert_sorted     (GList          *list,
746                                  gpointer        data,
747                                  GCompareFunc    func);
748 GList* g_list_concat            (GList          *list1,
749                                  GList          *list2);
750 GList* g_list_remove            (GList          *list,
751                                  gpointer        data);
752 GList* g_list_remove_link       (GList          *list,
753                                  GList          *llink);
754 GList* g_list_reverse           (GList          *list);
755 GList* g_list_nth               (GList          *list,
756                                  guint           n);
757 GList* g_list_find              (GList          *list,
758                                  gpointer        data);
759 GList* g_list_find_custom       (GList          *list,
760                                  gpointer        data,
761                                  GCompareFunc    func);
762 gint   g_list_position          (GList          *list,
763                                  GList          *llink);
764 gint   g_list_index             (GList          *list,
765                                  gpointer        data);
766 GList* g_list_last              (GList          *list);
767 GList* g_list_first             (GList          *list);
768 guint  g_list_length            (GList          *list);
769 void   g_list_foreach           (GList          *list,
770                                  GFunc           func,
771                                  gpointer        user_data);
772 gpointer g_list_nth_data        (GList          *list,
773                                  guint           n);
774 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
775 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
776
777
778 /* Singly linked lists
779  */
780 GSList* g_slist_alloc           (void);
781 void    g_slist_free            (GSList         *list);
782 void    g_slist_free_1          (GSList         *list);
783 GSList* g_slist_append          (GSList         *list,
784                                  gpointer        data);
785 GSList* g_slist_prepend         (GSList         *list,
786                                  gpointer        data);
787 GSList* g_slist_insert          (GSList         *list,
788                                  gpointer        data,
789                                  gint            position);
790 GSList* g_slist_insert_sorted   (GSList         *list,
791                                  gpointer        data,
792                                  GCompareFunc    func);
793 GSList* g_slist_concat          (GSList         *list1,
794                                  GSList         *list2);
795 GSList* g_slist_remove          (GSList         *list,
796                                  gpointer        data);
797 GSList* g_slist_remove_link     (GSList         *list,
798                                  GSList         *llink);
799 GSList* g_slist_reverse         (GSList         *list);
800 GSList* g_slist_nth             (GSList         *list,
801                                  guint           n);
802 GSList* g_slist_find            (GSList         *list,
803                                  gpointer        data);
804 GSList* g_slist_find_custom     (GSList         *list,
805                                  gpointer        data,
806                                  GCompareFunc    func);
807 gint    g_slist_position        (GSList         *list,
808                                  GSList         *llink);
809 gint    g_slist_index           (GSList         *list,
810                                  gpointer        data);
811 GSList* g_slist_last            (GSList         *list);
812 guint   g_slist_length          (GSList         *list);
813 void    g_slist_foreach         (GSList         *list,
814                                  GFunc           func,
815                                  gpointer        user_data);
816 gpointer g_slist_nth_data       (GSList         *list,
817                                  guint           n);
818 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
819
820
821 /* List Allocators
822  */
823 GListAllocator* g_list_allocator_new  (void);
824 void            g_list_allocator_free (GListAllocator* allocator);
825 GListAllocator* g_slist_set_allocator (GListAllocator* allocator);
826 GListAllocator* g_list_set_allocator  (GListAllocator* allocator);
827
828
829 /* Hash tables
830  */
831 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
832                                          GCompareFunc    key_compare_func);
833 void        g_hash_table_destroy        (GHashTable     *hash_table);
834 void        g_hash_table_insert         (GHashTable     *hash_table,
835                                          gpointer        key,
836                                          gpointer        value);
837 void        g_hash_table_remove         (GHashTable     *hash_table,
838                                          gconstpointer   key);
839 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
840                                          gconstpointer   key);
841 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
842                                          gconstpointer   lookup_key,
843                                          gpointer       *orig_key,
844                                          gpointer       *value);
845 void        g_hash_table_freeze         (GHashTable     *hash_table);
846 void        g_hash_table_thaw           (GHashTable     *hash_table);
847 void        g_hash_table_foreach        (GHashTable     *hash_table,
848                                          GHFunc          func,
849                                          gpointer        user_data);
850 gint        g_hash_table_size           (GHashTable     *hash_table);
851
852
853 /* Caches
854  */
855 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
856                                 GCacheDestroyFunc  value_destroy_func,
857                                 GCacheDupFunc      key_dup_func,
858                                 GCacheDestroyFunc  key_destroy_func,
859                                 GHashFunc          hash_key_func,
860                                 GHashFunc          hash_value_func,
861                                 GCompareFunc       key_compare_func);
862 void     g_cache_destroy       (GCache            *cache);
863 gpointer g_cache_insert        (GCache            *cache,
864                                 gpointer           key);
865 void     g_cache_remove        (GCache            *cache,
866                                 gpointer           value);
867 void     g_cache_key_foreach   (GCache            *cache,
868                                 GHFunc             func,
869                                 gpointer           user_data);
870 void     g_cache_value_foreach (GCache            *cache,
871                                 GHFunc             func,
872                                 gpointer           user_data);
873
874
875 /* Balanced binary trees
876  */
877 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
878 void     g_tree_destroy  (GTree         *tree);
879 void     g_tree_insert   (GTree         *tree,
880                           gpointer       key,
881                           gpointer       value);
882 void     g_tree_remove   (GTree         *tree,
883                           gpointer       key);
884 gpointer g_tree_lookup   (GTree         *tree,
885                           gpointer       key);
886 void     g_tree_traverse (GTree         *tree,
887                           GTraverseFunc  traverse_func,
888                           GTraverseType  traverse_type,
889                           gpointer       data);
890 gpointer g_tree_search   (GTree         *tree,
891                           GSearchFunc    search_func,
892                           gpointer       data);
893 gint     g_tree_height   (GTree         *tree);
894 gint     g_tree_nnodes   (GTree         *tree);
895
896
897
898 /* N-way tree implementation
899  */
900 struct _GNode
901 {
902   gpointer data;
903   GNode   *next;
904   GNode   *prev;
905   GNode   *parent;
906   GNode   *children;
907 };
908
909 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
910                                  ((GNode*) (node))->prev == NULL && \
911                                  ((GNode*) (node))->next == NULL)
912 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
913
914 GNode*   g_node_new             (gpointer          data);
915 void     g_node_destroy         (GNode            *root);
916 void     g_node_unlink          (GNode            *node);
917 GNode*   g_node_insert          (GNode            *parent,
918                                  gint              position,
919                                  GNode            *node);
920 GNode*   g_node_insert_before   (GNode            *parent,
921                                  GNode            *sibling,
922                                  GNode            *node);
923 GNode*   g_node_prepend         (GNode            *parent,
924                                  GNode            *node);
925 guint    g_node_n_nodes         (GNode            *root,
926                                  GTraverseFlags    flags);
927 GNode*   g_node_get_root        (GNode            *node);
928 gboolean g_node_is_ancestor     (GNode            *node,
929                                  GNode            *descendant);
930 guint    g_node_depth           (GNode            *node);
931 GNode*   g_node_find            (GNode            *root,
932                                  GTraverseType     order,
933                                  GTraverseFlags    flags,
934                                  gpointer          data);
935
936 /* convenience macros */
937 #define g_node_append(parent, node)                             \
938      g_node_insert_before ((parent), NULL, (node))
939 #define g_node_insert_data(parent, position, data)              \
940      g_node_insert ((parent), (position), g_node_new (data))
941 #define g_node_insert_data_before(parent, sibling, data)        \
942      g_node_insert_before ((parent), (sibling), g_node_new (data))
943 #define g_node_prepend_data(parent, data)                       \
944      g_node_prepend ((parent), g_node_new (data))
945 #define g_node_append_data(parent, data)                        \
946      g_node_insert_before ((parent), NULL, g_node_new (data))
947
948 /* traversal function, assumes that `node' is root
949  * (only traverses `node' and its subtree).
950  * this function is just a high level interface to
951  * low level traversal functions, optimized for speed.
952  */
953 void     g_node_traverse        (GNode            *root,
954                                  GTraverseType     order,
955                                  GTraverseFlags    flags,
956                                  gint              max_depth,
957                                  GNodeTraverseFunc func,
958                                  gpointer          data);
959
960 /* return the maximum tree height starting with `node', this is an expensive
961  * operation, since we need to visit all nodes. this could be shortened by
962  * adding `guint height' to struct _GNode, but then again, this is not very
963  * often needed, and would make g_node_insert() more time consuming.
964  */
965 guint    g_node_max_height       (GNode *root);
966
967 void     g_node_children_foreach (GNode           *node,
968                                   GTraverseFlags   flags,
969                                   GNodeForeachFunc func,
970                                   gpointer         data);
971 void     g_node_reverse_children (GNode           *node);
972 guint    g_node_n_children       (GNode           *node);
973 GNode*   g_node_nth_child        (GNode           *node,
974                                   guint            n);
975 GNode*   g_node_last_child       (GNode           *node);
976 GNode*   g_node_find_child       (GNode           *node,
977                                   GTraverseFlags   flags,
978                                   gpointer         data);
979 gint     g_node_child_position   (GNode           *node,
980                                   GNode           *child);
981 gint     g_node_child_index      (GNode           *node,
982                                   gpointer         data);
983
984 GNode*   g_node_first_sibling    (GNode           *node);
985 GNode*   g_node_last_sibling     (GNode           *node);
986
987 #define  g_node_prev_sibling(node)      ((node) ? \
988                                          ((GNode*) (node))->prev : NULL)
989 #define  g_node_next_sibling(node)      ((node) ? \
990                                          ((GNode*) (node))->next : NULL)
991 #define  g_node_first_child(node)       ((node) ? \
992                                          ((GNode*) (node))->children : NULL)
993
994
995 /* Fatal error handlers.
996  * g_on_error_query() will prompt the user to either
997  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
998  * g_on_error_stack_trace() invokes gdb, which attaches to the current
999  * process and shows a stack trace.
1000  * These function may cause different actions on non-unix platforms.
1001  * The prg_name arg is required by gdb to find the executable, if it is
1002  * passed as NULL, g_on_error_query() will try g_get_prgname().
1003  */
1004 void g_on_error_query (const gchar *prg_name);
1005 void g_on_error_stack_trace (const gchar *prg_name);
1006
1007
1008 /* Logging mechanism
1009  */
1010 extern const gchar                      *g_log_domain_glib;
1011 guint           g_log_set_handler       (const gchar    *log_domain,
1012                                          GLogLevelFlags  log_levels,
1013                                          GLogFunc        log_func,
1014                                          gpointer        user_data);
1015 void            g_log_remove_handler    (const gchar    *log_domain,
1016                                          guint           handler_id);
1017 void            g_log_default_handler   (const gchar    *log_domain,
1018                                          GLogLevelFlags  log_level,
1019                                          const gchar    *message,
1020                                          gpointer        unused_data);
1021 void            g_log                   (const gchar    *log_domain,
1022                                          GLogLevelFlags  log_level,
1023                                          const gchar    *format,
1024                                          ...) G_GNUC_PRINTF (3, 4);
1025 void            g_logv                  (const gchar    *log_domain,
1026                                          GLogLevelFlags  log_level,
1027                                          const gchar    *format,
1028                                          va_list         args);
1029 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1030                                          GLogLevelFlags  fatal_mask);
1031 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1032 #ifndef G_LOG_DOMAIN
1033 #define G_LOG_DOMAIN    (NULL)
1034 #endif  /* G_LOG_DOMAIN */
1035 #ifdef  __GNUC__
1036 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1037                                                G_LOG_LEVEL_ERROR, \
1038                                                format, ##args)
1039 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1040                                                G_LOG_LEVEL_MESSAGE, \
1041                                                format, ##args)
1042 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1043                                                G_LOG_LEVEL_WARNING, \
1044                                                format, ##args)
1045 #else   /* !__GNUC__ */
1046 static inline void
1047 g_error (const gchar *format,
1048          ...)
1049 {
1050   va_list args;
1051   va_start (args, format);
1052   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1053   va_end (args);
1054 }
1055 static inline void
1056 g_message (const gchar *format,
1057            ...)
1058 {
1059   va_list args;
1060   va_start (args, format);
1061   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1062   va_end (args);
1063 }
1064 static inline void
1065 g_warning (const gchar *format,
1066            ...)
1067 {
1068   va_list args;
1069   va_start (args, format);
1070   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1071   va_end (args);
1072 }
1073 #endif  /* !__GNUC__ */
1074
1075 typedef void    (*GPrintFunc)           (const gchar    *string);
1076 void            g_print                 (const gchar    *format,
1077                                          ...) G_GNUC_PRINTF (1, 2);
1078 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1079 void            g_printerr              (const gchar    *format,
1080                                          ...) G_GNUC_PRINTF (1, 2);
1081 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1082
1083 /* deprecated compatibility functions, use g_log_set_handler() instead */
1084 typedef void            (*GErrorFunc)           (const gchar *str);
1085 typedef void            (*GWarningFunc)         (const gchar *str);
1086 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1087 GWarningFunc g_set_warning_handler (GWarningFunc func);
1088 GPrintFunc   g_set_message_handler (GPrintFunc func);
1089
1090
1091 /* Memory allocation and debugging
1092  */
1093 #ifdef USE_DMALLOC
1094
1095 #define g_malloc(size)       ((gpointer) MALLOC (size))
1096 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1097 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1098 #define g_free(mem)          FREE (mem)
1099
1100 #else /* !USE_DMALLOC */
1101
1102 gpointer g_malloc      (gulong    size);
1103 gpointer g_malloc0     (gulong    size);
1104 gpointer g_realloc     (gpointer  mem,
1105                         gulong    size);
1106 void     g_free        (gpointer  mem);
1107
1108 #endif /* !USE_DMALLOC */
1109
1110 void     g_mem_profile (void);
1111 void     g_mem_check   (gpointer  mem);
1112
1113
1114 /* "g_mem_chunk_new" creates a new memory chunk.
1115  * Memory chunks are used to allocate pieces of memory which are
1116  *  always the same size. Lists are a good example of such a data type.
1117  * The memory chunk allocates and frees blocks of memory as needed.
1118  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1119  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1120  *  fault...somewhere).
1121  *
1122  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1123  *  want to know what's going on inside do you?)
1124  */
1125
1126 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1127  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1128  *  atom. (They are also useful for lists which use MemChunk to allocate
1129  *  memory but are also part of the MemChunk implementation).
1130  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1131  */
1132
1133 #define G_ALLOC_ONLY      1
1134 #define G_ALLOC_AND_FREE  2
1135
1136 GMemChunk* g_mem_chunk_new     (gchar     *name,
1137                                 gint       atom_size,
1138                                 gulong     area_size,
1139                                 gint       type);
1140 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1141 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1142 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1143 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1144                                 gpointer   mem);
1145 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1146 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1147 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1148 void       g_mem_chunk_info    (void);
1149
1150 /* Ah yes...we have a "g_blow_chunks" function.
1151  * "g_blow_chunks" simply compresses all the chunks. This operation
1152  *  consists of freeing every memory area that should be freed (but
1153  *  which we haven't gotten around to doing yet). And, no,
1154  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1155  *  much better name than "g_mem_chunk_clean_all" or something
1156  *  similar.
1157  */
1158 void g_blow_chunks (void);
1159
1160
1161 /* Timer
1162  */
1163 GTimer* g_timer_new     (void);
1164 void    g_timer_destroy (GTimer  *timer);
1165 void    g_timer_start   (GTimer  *timer);
1166 void    g_timer_stop    (GTimer  *timer);
1167 void    g_timer_reset   (GTimer  *timer);
1168 gdouble g_timer_elapsed (GTimer  *timer,
1169                          gulong  *microseconds);
1170
1171
1172 /* String utility functions
1173  */
1174 #define G_STR_DELIMITERS        "_-|> <."
1175 void    g_strdelimit            (gchar       *string,
1176                                  const gchar *delimiters,
1177                                  gchar        new_delimiter);
1178 gchar*  g_strdup                (const gchar *str);
1179 gchar*  g_strdup_printf         (const gchar *format,
1180                                  ...) G_GNUC_PRINTF (1, 2);
1181 gchar*  g_strdup_vprintf        (const gchar *format,
1182                                  va_list      args);
1183 gchar*  g_strndup               (const gchar *str,
1184                                  guint        n);
1185 gchar*  g_strnfill              (guint        length,
1186                                  gchar        fill_char);
1187 gchar*  g_strconcat             (const gchar *string1,
1188                                  ...); /* NULL terminated */
1189 gdouble g_strtod                (const gchar *nptr,
1190                                  gchar      **endptr);
1191 gchar*  g_strerror              (gint         errnum);
1192 gchar*  g_strsignal             (gint         signum);
1193 gint    g_strcasecmp            (const gchar *s1,
1194                                  const gchar *s2);
1195 void    g_strdown               (gchar       *string);
1196 void    g_strup                 (gchar       *string);
1197 void    g_strreverse            (gchar       *string);
1198
1199 /* calculate a string size, guarranteed to fit format + args.
1200  */
1201 guint   g_printf_string_upper_bound (const gchar* format,
1202                                      va_list      args);
1203
1204
1205 /* Retrive static string info
1206  */
1207 gchar*  g_get_user_name         (void);
1208 gchar*  g_get_real_name         (void);
1209 gchar*  g_get_home_dir          (void);
1210 gchar*  g_get_tmp_dir           (void);
1211 gchar*  g_get_prgname           (void);
1212 void    g_set_prgname           (const gchar *prgname);
1213
1214
1215 /* Miscellaneous utility functions
1216  */
1217 guint   g_parse_debug_string    (const gchar *string,
1218                                  GDebugKey   *keys,
1219                                  guint        nkeys);
1220 gint    g_snprintf              (gchar       *string,
1221                                  gulong       n,
1222                                  gchar const *format,
1223                                  ...) G_GNUC_PRINTF (3, 4);
1224 gint    g_vsnprintf             (gchar       *string,
1225                                  gulong       n,
1226                                  gchar const *format,
1227                                  va_list      args);
1228 gchar*  g_basename              (const gchar *file_name);
1229
1230 /* strings are newly allocated with g_malloc() */
1231 gchar*  g_dirname               (const gchar *file_name);
1232 gchar*  g_get_current_dir       (void);
1233
1234 /* We make the assumption that if memmove isn't available, then
1235  * bcopy will do the job. This isn't safe everywhere. (bcopy can't
1236  * necessarily handle overlapping copies).
1237  * Either way, g_memmove() will not return a value.
1238  */
1239 #ifdef HAVE_MEMMOVE
1240 #define g_memmove(dest, src, size)      G_STMT_START {  \
1241      memmove ((dest), (src), (size));                   \
1242 } G_STMT_END
1243 #else
1244 #define g_memmove(dest, src, size)      G_STMT_START {  \
1245      bcopy ((src), (dest), (size));                     \
1246 } G_STMT_END
1247 #endif
1248
1249 /* we use a GLib function as a replacement for ATEXIT, so
1250  * the programmer is not required to check the return value
1251  * (if there is any in the implementation) and doesn't encounter
1252  * missing include files.
1253  */
1254 void    g_atexit                (GVoidFunc    func);
1255
1256
1257 /* Bit tests
1258  */
1259 G_INLINE_FUNC gint
1260 g_bit_nth_lsf (guint32 mask,
1261                gint    nth_bit)
1262 #ifdef G_CAN_INLINE
1263 {
1264   do
1265     {
1266       nth_bit++;
1267       if (mask & (1 << (guint) nth_bit))
1268         return nth_bit;
1269     }
1270   while (nth_bit < 32);
1271   return -1;
1272 }
1273 #else
1274 ;
1275 #endif
1276 G_INLINE_FUNC gint
1277 g_bit_nth_msf (guint32 mask,
1278                gint    nth_bit)
1279 #ifdef G_CAN_INLINE
1280 {
1281   if (nth_bit < 0)
1282     nth_bit = 33;
1283   do
1284     {
1285       nth_bit--;
1286       if (mask & (1 << (guint) nth_bit))
1287         return nth_bit;
1288     }
1289   while (nth_bit > 0);
1290   return -1;
1291 }
1292 #else
1293 ;
1294 #endif
1295 G_INLINE_FUNC guint
1296 g_bit_storage (guint number)
1297 #ifdef G_CAN_INLINE
1298 {
1299   register guint n_bits = 0;
1300
1301   do
1302     {
1303       n_bits++;
1304       number = number >> 1;
1305     } while (number);
1306   return n_bits;
1307 }
1308 #else
1309 ;
1310 #endif
1311
1312
1313 /* String Chunks
1314  */
1315 GStringChunk* g_string_chunk_new           (gint size);
1316 void          g_string_chunk_free          (GStringChunk *chunk);
1317 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1318                                             const gchar  *string);
1319 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1320                                             const gchar  *string);
1321
1322
1323 /* Strings
1324  */
1325 GString* g_string_new       (const gchar *init);
1326 GString* g_string_sized_new (guint        dfl_size);
1327 void     g_string_free      (GString     *string,
1328                              gint         free_segment);
1329 GString* g_string_assign    (GString     *lval,
1330                              const gchar *rval);
1331 GString* g_string_truncate  (GString     *string,
1332                              gint         len);
1333 GString* g_string_append    (GString     *string,
1334                              const gchar *val);
1335 GString* g_string_append_c  (GString     *string,
1336                              gchar        c);
1337 GString* g_string_prepend   (GString     *string,
1338                              const gchar *val);
1339 GString* g_string_prepend_c (GString     *string,
1340                              gchar        c);
1341 GString* g_string_insert    (GString     *string,
1342                              gint         pos,
1343                              const gchar *val);
1344 GString* g_string_insert_c  (GString     *string,
1345                              gint         pos,
1346                              gchar        c);
1347 GString* g_string_erase     (GString     *string,
1348                              gint         pos,
1349                              gint         len);
1350 GString* g_string_down      (GString     *string);
1351 GString* g_string_up        (GString     *string);
1352 void     g_string_sprintf   (GString     *string,
1353                              const gchar *format,
1354                              ...) G_GNUC_PRINTF (2, 3);
1355 void     g_string_sprintfa  (GString     *string,
1356                              const gchar *format,
1357                              ...) G_GNUC_PRINTF (2, 3);
1358
1359
1360 /* Resizable arrays
1361  */
1362
1363 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1364 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1365 #define g_array_index(a,t,i) (((t*)a->data)[i])
1366
1367 GArray* g_array_new          (gboolean      zero_terminated,
1368                               gboolean      clear,
1369                               guint         element_size);
1370 void    g_array_free         (GArray       *array,
1371                               gboolean      free_segment);
1372 GArray* g_array_append_vals  (GArray       *array,
1373                               gconstpointer data,
1374                               guint         len);
1375 GArray* g_array_prepend_vals (GArray       *array,
1376                               gconstpointer data,
1377                               guint         len);
1378 GArray* g_array_set_size     (GArray       *array,
1379                               guint         length);
1380
1381 /* Resizable pointer array.  This interface is much less complicated
1382  * than the above.  Add appends appends a pointer.  Remove fills any
1383  * cleared spot and shortens the array.
1384  */
1385 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1386 GPtrArray*  g_ptr_array_new                (void);
1387 void        g_ptr_array_free               (GPtrArray   *array,
1388                                             gboolean     free_seg);
1389 void        g_ptr_array_set_size           (GPtrArray   *array,
1390                                             gint         length);
1391 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1392                                             gint         index);
1393 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1394                                             gpointer     data);
1395 void        g_ptr_array_add                (GPtrArray   *array,
1396                                             gpointer     data);
1397
1398 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1399  * but type-safe.
1400  */
1401
1402 GByteArray* g_byte_array_new      (void);
1403 void        g_byte_array_free     (GByteArray   *array,
1404                                    gboolean      free_segment);
1405 GByteArray* g_byte_array_append   (GByteArray   *array,
1406                                    const guint8 *data,
1407                                    guint         len);
1408 GByteArray* g_byte_array_prepend  (GByteArray   *array,
1409                                    const guint8 *data,
1410                                    guint         len);
1411 GByteArray* g_byte_array_set_size (GByteArray   *array,
1412                                    guint         length);
1413
1414
1415 /* Hash Functions
1416  */
1417 gint  g_str_equal (gconstpointer   v,
1418                    gconstpointer   v2);
1419 guint g_str_hash  (gconstpointer   v);
1420
1421 gint  g_int_equal (gconstpointer   v,
1422                    gconstpointer   v2);
1423 guint g_int_hash  (gconstpointer   v);
1424
1425 /* This "hash" function will just return the key's adress as an
1426  * unsigned integer. Useful for hashing on plain adresses or
1427  * simple integer values.
1428  */
1429 guint g_direct_hash  (gconstpointer v);
1430 gint  g_direct_equal (gconstpointer v,
1431                       gconstpointer v2);
1432
1433
1434 /* Quarks (string<->id association)
1435  */
1436 GQuark    g_quark_try_string            (const gchar    *string);
1437 GQuark    g_quark_from_static_string    (const gchar    *string);
1438 GQuark    g_quark_from_string           (const gchar    *string);
1439 gchar*    g_quark_to_string             (GQuark          quark);
1440
1441
1442 /* Location Associated Data
1443  */
1444 void      g_dataset_destroy             (gconstpointer   dataset_location);
1445 gpointer  g_dataset_id_get_data         (gconstpointer   dataset_location,
1446                                          GQuark          key_id);
1447 void      g_dataset_id_set_data_full    (gconstpointer   dataset_location,
1448                                          GQuark          key_id,
1449                                          gpointer        data,
1450                                          GDestroyNotify  destroy_func);
1451 void      g_dataset_id_set_destroy      (gconstpointer   dataset_location,
1452                                          GQuark          key_id,
1453                                          GDestroyNotify  destroy_func);
1454 #define   g_dataset_id_set_data(l, k, d)        \
1455      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1456 #define   g_dataset_id_remove_data(l, k)        \
1457      g_dataset_id_set_data ((l), (k), NULL)
1458 #define   g_dataset_get_data(l, k)              \
1459      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1460 #define   g_dataset_set_data_full(l, k, d, f)   \
1461      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1462 #define   g_dataset_set_destroy(l, k, f)        \
1463      g_dataset_id_set_destroy ((l), g_quark_from_string (k), (f))
1464 #define   g_dataset_set_data(l, k, d)           \
1465      g_dataset_set_data_full ((l), (k), (d), NULL)
1466 #define   g_dataset_remove_data(l,k)            \
1467      g_dataset_set_data ((l), (k), NULL)
1468
1469
1470 /* GScanner: Flexible lexical scanner for general purpose.
1471  */
1472
1473 /* Character sets */
1474 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1475 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1476 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1477                         "\307\310\311\312\313\314\315\316\317\320"\
1478                         "\321\322\323\324\325\326"\
1479                         "\330\331\332\333\334\335\336"
1480 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1481                         "\347\350\351\352\353\354\355\356\357\360"\
1482                         "\361\362\363\364\365\366"\
1483                         "\370\371\372\373\374\375\376\377"
1484
1485 /* Error types */
1486 typedef enum
1487 {
1488   G_ERR_UNKNOWN,
1489   G_ERR_UNEXP_EOF,
1490   G_ERR_UNEXP_EOF_IN_STRING,
1491   G_ERR_UNEXP_EOF_IN_COMMENT,
1492   G_ERR_NON_DIGIT_IN_CONST,
1493   G_ERR_DIGIT_RADIX,
1494   G_ERR_FLOAT_RADIX,
1495   G_ERR_FLOAT_MALFORMED
1496 } GErrorType;
1497
1498 /* Token types */
1499 typedef enum
1500 {
1501   G_TOKEN_EOF                   =   0,
1502
1503   G_TOKEN_LEFT_PAREN            = '(',
1504   G_TOKEN_RIGHT_PAREN           = ')',
1505   G_TOKEN_LEFT_CURLY            = '{',
1506   G_TOKEN_RIGHT_CURLY           = '}',
1507   G_TOKEN_LEFT_BRACE            = '[',
1508   G_TOKEN_RIGHT_BRACE           = ']',
1509   G_TOKEN_EQUAL_SIGN            = '=',
1510   G_TOKEN_COMMA                 = ',',
1511
1512   G_TOKEN_NONE                  = 256,
1513
1514   G_TOKEN_ERROR,
1515
1516   G_TOKEN_CHAR,
1517   G_TOKEN_BINARY,
1518   G_TOKEN_OCTAL,
1519   G_TOKEN_INT,
1520   G_TOKEN_HEX,
1521   G_TOKEN_FLOAT,
1522   G_TOKEN_STRING,
1523
1524   G_TOKEN_SYMBOL,
1525   G_TOKEN_IDENTIFIER,
1526   G_TOKEN_IDENTIFIER_NULL,
1527
1528   G_TOKEN_COMMENT_SINGLE,
1529   G_TOKEN_COMMENT_MULTI,
1530   G_TOKEN_LAST
1531 } GTokenType;
1532
1533 union   _GValue
1534 {
1535   gpointer      v_symbol;
1536   gchar         *v_identifier;
1537   gulong        v_binary;
1538   gulong        v_octal;
1539   gulong        v_int;
1540   gdouble       v_float;
1541   gulong        v_hex;
1542   gchar         *v_string;
1543   gchar         *v_comment;
1544   guchar        v_char;
1545   guint         v_error;
1546 };
1547
1548 struct  _GScannerConfig
1549 {
1550   /* Character sets
1551    */
1552   gchar         *cset_skip_characters;          /* default: " \t\n" */
1553   gchar         *cset_identifier_first;
1554   gchar         *cset_identifier_nth;
1555   gchar         *cpair_comment_single;          /* default: "#\n" */
1556
1557   /* Should symbol lookup work case sensitive?
1558    */
1559   guint         case_sensitive : 1;
1560
1561   /* Boolean values to be adjusted "on the fly"
1562    * to configure scanning behaviour.
1563    */
1564   guint         skip_comment_multi : 1;         /* C like comment */
1565   guint         skip_comment_single : 1;        /* single line comment */
1566   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1567   guint         scan_identifier : 1;
1568   guint         scan_identifier_1char : 1;
1569   guint         scan_identifier_NULL : 1;
1570   guint         scan_symbols : 1;
1571   guint         scan_binary : 1;
1572   guint         scan_octal : 1;
1573   guint         scan_float : 1;
1574   guint         scan_hex : 1;                   /* `0x0ff0' */
1575   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1576   guint         scan_string_sq : 1;             /* string: 'anything' */
1577   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1578   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1579   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1580   guint         identifier_2_string : 1;
1581   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1582   guint         symbol_2_token : 1;
1583   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
1584 };
1585
1586 struct  _GScanner
1587 {
1588   /* unused fields */
1589   gpointer              user_data;
1590   guint                 max_parse_errors;
1591
1592   /* g_scanner_error() increments this field */
1593   guint                 parse_errors;
1594
1595   /* name of input stream, featured by the default message handler */
1596   const gchar           *input_name;
1597
1598   /* data pointer for derived structures */
1599   gpointer              derived_data;
1600
1601   /* link into the scanner configuration */
1602   GScannerConfig        *config;
1603
1604   /* fields filled in after g_scanner_get_next_token() */
1605   GTokenType            token;
1606   GValue                value;
1607   guint                 line;
1608   guint                 position;
1609
1610   /* fields filled in after g_scanner_peek_next_token() */
1611   GTokenType            next_token;
1612   GValue                next_value;
1613   guint                 next_line;
1614   guint                 next_position;
1615
1616   /* to be considered private */
1617   GHashTable            *symbol_table;
1618   const gchar           *text;
1619   guint                 text_len;
1620   gint                  input_fd;
1621   gint                  peeked_char;
1622   guint                 scope_id;
1623
1624   /* handler function for _warn and _error */
1625   GScannerMsgFunc       msg_handler;
1626 };
1627
1628 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
1629 void            g_scanner_destroy               (GScanner       *scanner);
1630 void            g_scanner_input_file            (GScanner       *scanner,
1631                                                  gint           input_fd);
1632 void            g_scanner_input_text            (GScanner       *scanner,
1633                                                  const  gchar   *text,
1634                                                  guint          text_len);
1635 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
1636 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
1637 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
1638 GValue          g_scanner_cur_value             (GScanner       *scanner);
1639 guint           g_scanner_cur_line              (GScanner       *scanner);
1640 guint           g_scanner_cur_position          (GScanner       *scanner);
1641 gboolean        g_scanner_eof                   (GScanner       *scanner);
1642 guint           g_scanner_set_scope             (GScanner       *scanner,
1643                                                  guint           scope_id);
1644 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
1645                                                  guint           scope_id,
1646                                                  const gchar    *symbol,
1647                                                  gpointer       value);
1648 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
1649                                                  guint           scope_id,
1650                                                  const gchar    *symbol);
1651 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
1652                                                  guint           scope_id,
1653                                                  const gchar    *symbol);
1654 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
1655                                                  guint           scope_id,
1656                                                  GHFunc          func,
1657                                                  gpointer        func_data);
1658 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
1659                                                  const gchar    *symbol);
1660 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
1661 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
1662 void            g_scanner_unexp_token           (GScanner       *scanner,
1663                                                  GTokenType     expected_token,
1664                                                  const gchar    *identifier_spec,
1665                                                  const gchar    *symbol_spec,
1666                                                  const gchar    *symbol_name,
1667                                                  const gchar    *message,
1668                                                  gint            is_error);
1669 void            g_scanner_error                 (GScanner       *scanner,
1670                                                  const gchar    *format,
1671                                                  ...) G_GNUC_PRINTF (2,3);
1672 void            g_scanner_warn                  (GScanner       *scanner,
1673                                                  const gchar    *format,
1674                                                  ...) G_GNUC_PRINTF (2,3);
1675 gint            g_scanner_stat_mode             (const gchar    *filename);
1676 /* keep downward source compatibility */
1677 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
1678   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
1679 } G_STMT_END
1680 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
1681   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
1682 } G_STMT_END
1683 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
1684   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
1685 } G_STMT_END
1686
1687
1688 /* Completion */
1689
1690 struct _GCompletion
1691 {
1692   GList* items;
1693   GCompletionFunc func;
1694
1695   gchar* prefix;
1696   GList* cache;
1697 };
1698
1699 GCompletion* g_completion_new          (GCompletionFunc func);
1700 void         g_completion_add_items    (GCompletion*    cmp,
1701                                         GList*          items);
1702 void         g_completion_remove_items (GCompletion*    cmp,
1703                                         GList*          items);
1704 void         g_completion_clear_items  (GCompletion*    cmp);
1705 GList*       g_completion_complete     (GCompletion*    cmp,
1706                                         gchar*          prefix,
1707                                         gchar**         new_prefix);
1708 void         g_completion_free         (GCompletion*    cmp);
1709
1710
1711 /* GRelation: Indexed Relations.  Imagine a really simple table in a
1712  * database.  Relations are not ordered.  This data type is meant for
1713  * maintaining a N-way mapping.
1714  *
1715  * g_relation_new() creates a relation with FIELDS fields
1716  *
1717  * g_relation_destroy() frees all resources
1718  * g_tuples_destroy() frees the result of g_relation_select()
1719  *
1720  * g_relation_index() indexes relation FIELD with the provided
1721  *   equality and hash functions.  this must be done before any
1722  *   calls to insert are made.
1723  *
1724  * g_relation_insert() inserts a new tuple.  you are expected to
1725  *   provide the right number of fields.
1726  *
1727  * g_relation_delete() deletes all relations with KEY in FIELD
1728  * g_relation_select() returns ...
1729  * g_relation_count() counts ...
1730  */
1731
1732 GRelation* g_relation_new     (gint         fields);
1733 void       g_relation_destroy (GRelation   *relation);
1734 void       g_relation_index   (GRelation   *relation,
1735                                gint         field,
1736                                GHashFunc    hash_func,
1737                                GCompareFunc key_compare_func);
1738 void       g_relation_insert  (GRelation   *relation,
1739                                ...);
1740 gint       g_relation_delete  (GRelation   *relation,
1741                                gconstpointer  key,
1742                                gint         field);
1743 GTuples*   g_relation_select  (GRelation   *relation,
1744                                gconstpointer  key,
1745                                gint         field);
1746 gint       g_relation_count   (GRelation   *relation,
1747                                gconstpointer  key,
1748                                gint         field);
1749 gboolean   g_relation_exists  (GRelation   *relation,
1750                                ...);
1751 void       g_relation_print   (GRelation   *relation);
1752
1753 void       g_tuples_destroy   (GTuples     *tuples);
1754 gpointer   g_tuples_index     (GTuples     *tuples,
1755                                gint         index,
1756                                gint         field);
1757
1758
1759 /* Prime numbers.
1760  */
1761
1762 /* This function returns prime numbers spaced by approximately 1.5-2.0
1763  * and is for use in resizing data structures which prefer
1764  * prime-valued sizes.  The closest spaced prime function returns the
1765  * next largest prime, or the highest it knows about which is about
1766  * MAXINT/4.
1767  */
1768
1769 guint      g_spaced_primes_closest (guint num);
1770
1771 /* Glib version.
1772  */
1773 extern const guint glib_major_version;
1774 extern const guint glib_minor_version;
1775 extern const guint glib_micro_version;
1776
1777 #ifdef __cplusplus
1778 }
1779 #endif /* __cplusplus */
1780
1781
1782 #endif /* __G_LIB_H__ */