This is Josh, commiting as Manish. This is completely new, and
[platform/upstream/glib.git] / glib / glib.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #ifndef __G_LIB_H__
20 #define __G_LIB_H__
21
22 /* system specific config file
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 _GArray          GArray;
579 typedef struct _GByteArray      GByteArray;
580 typedef struct _GCache          GCache;
581 typedef struct _GCompletion     GCompletion;
582 typedef struct _GData           GData;
583 typedef struct _GDebugKey       GDebugKey;
584 typedef struct _GHashTable      GHashTable;
585 typedef struct _GList           GList;
586 typedef struct _GListAllocator  GListAllocator;
587 typedef struct _GMemChunk       GMemChunk;
588 typedef struct _GNode           GNode;
589 typedef struct _GPtrArray       GPtrArray;
590 typedef struct _GRelation       GRelation;
591 typedef struct _GScanner        GScanner;
592 typedef struct _GScannerConfig  GScannerConfig;
593 typedef struct _GSList          GSList;
594 typedef struct _GString         GString;
595 typedef struct _GStringChunk    GStringChunk;
596 typedef struct _GTimer          GTimer;
597 typedef struct _GTree           GTree;
598 typedef struct _GTuples         GTuples;
599 typedef union  _GValue          GValue;
600
601
602 typedef enum
603 {
604   G_TRAVERSE_LEAFS      = 1 << 0,
605   G_TRAVERSE_NON_LEAFS  = 1 << 1,
606   G_TRAVERSE_ALL        = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
607   G_TRAVERSE_MASK       = 0x03
608 } GTraverseFlags;
609
610 typedef enum
611 {
612   G_IN_ORDER,
613   G_PRE_ORDER,
614   G_POST_ORDER,
615   G_LEVEL_ORDER
616 } GTraverseType;
617
618 /* Log level shift offset for user defined
619  * log levels (0-7 are used by GLib).
620  */
621 #define G_LOG_LEVEL_USER_SHIFT  (8)
622
623 /* Glib log levels and flags.
624  */
625 typedef enum
626 {
627   /* log flags */
628   G_LOG_FLAG_RECURSION          = 1 << 0,
629   G_LOG_FLAG_FATAL              = 1 << 1,
630
631   /* GLib log levels */
632   G_LOG_LEVEL_ERROR             = 1 << 2,       /* always fatal */
633   G_LOG_LEVEL_CRITICAL          = 1 << 3,
634   G_LOG_LEVEL_WARNING           = 1 << 4,
635   G_LOG_LEVEL_MESSAGE           = 1 << 5,
636   G_LOG_LEVEL_INFO              = 1 << 6,
637   G_LOG_LEVEL_DEBUG             = 1 << 7,
638
639   G_LOG_LEVEL_MASK              = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
640 } GLogLevelFlags;
641
642 /* GLib log levels that are considered fatal by default */
643 #define G_LOG_FATAL_MASK        (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
644
645
646 typedef gpointer        (*GCacheNewFunc)        (gpointer       key);
647 typedef gpointer        (*GCacheDupFunc)        (gpointer       value);
648 typedef void            (*GCacheDestroyFunc)    (gpointer       value);
649 typedef gint            (*GCompareFunc)         (gconstpointer  a,
650                                                  gconstpointer  b);
651 typedef gchar*          (*GCompletionFunc)      (gpointer);
652 typedef void            (*GDestroyNotify)       (gpointer       data);
653 typedef void            (*GDataForeachFunc)     (GQuark         key_id,
654                                                  gpointer       data,
655                                                  gpointer       user_data);
656 typedef void            (*GFunc)                (gpointer       data,
657                                                  gpointer       user_data);
658 typedef guint           (*GHashFunc)            (gconstpointer  key);
659 typedef void            (*GHFunc)               (gpointer       key,
660                                                  gpointer       value,
661                                                  gpointer       user_data);
662 typedef gboolean        (*GHRFunc)              (gpointer       key,
663                                                  gpointer       value,
664                                                  gpointer       user_data);
665 typedef void            (*GLogFunc)             (const gchar   *log_domain,
666                                                  GLogLevelFlags log_level,
667                                                  const gchar   *message,
668                                                  gpointer       user_data);
669 typedef gboolean        (*GNodeTraverseFunc)    (GNode         *node,
670                                                  gpointer       data);
671 typedef void            (*GNodeForeachFunc)     (GNode         *node,
672                                                  gpointer       data);
673 typedef gint            (*GSearchFunc)          (gpointer       key,
674                                                  gpointer       data);
675 typedef void            (*GScannerMsgFunc)      (GScanner      *scanner,
676                                                  gchar         *message,
677                                                  gint           error);
678 typedef gint            (*GTraverseFunc)        (gpointer       key,
679                                                  gpointer       value,
680                                                  gpointer       data);
681 typedef void            (*GVoidFunc)            (void);
682
683
684 struct _GList
685 {
686   gpointer data;
687   GList *next;
688   GList *prev;
689 };
690
691 struct _GSList
692 {
693   gpointer data;
694   GSList *next;
695 };
696
697 struct _GString
698 {
699   gchar *str;
700   gint len;
701 };
702
703 struct _GArray
704 {
705   gchar *data;
706   guint len;
707 };
708
709 struct _GByteArray
710 {
711   guint8 *data;
712   guint   len;
713 };
714
715 struct _GPtrArray
716 {
717   gpointer *pdata;
718   guint     len;
719 };
720
721 struct _GTuples
722 {
723   guint len;
724 };
725
726 struct _GDebugKey
727 {
728   gchar *key;
729   guint  value;
730 };
731
732 struct _GCache { gint dummy; };
733 struct _GTree { gint dummy; };
734 struct _GTimer { gint dummy; };
735 struct _GMemChunk { gint dummy; };
736 struct _GListAllocator { gint dummy; };
737 struct _GStringChunk { gint dummy; };
738
739
740 /* Doubly linked lists
741  */
742 GList* g_list_alloc             (void);
743 void   g_list_free              (GList          *list);
744 void   g_list_free_1            (GList          *list);
745 GList* g_list_append            (GList          *list,
746                                  gpointer        data);
747 GList* g_list_prepend           (GList          *list,
748                                  gpointer        data);
749 GList* g_list_insert            (GList          *list,
750                                  gpointer        data,
751                                  gint            position);
752 GList* g_list_insert_sorted     (GList          *list,
753                                  gpointer        data,
754                                  GCompareFunc    func);
755 GList* g_list_concat            (GList          *list1,
756                                  GList          *list2);
757 GList* g_list_remove            (GList          *list,
758                                  gpointer        data);
759 GList* g_list_remove_link       (GList          *list,
760                                  GList          *llink);
761 GList* g_list_reverse           (GList          *list);
762 GList* g_list_nth               (GList          *list,
763                                  guint           n);
764 GList* g_list_find              (GList          *list,
765                                  gpointer        data);
766 GList* g_list_find_custom       (GList          *list,
767                                  gpointer        data,
768                                  GCompareFunc    func);
769 gint   g_list_position          (GList          *list,
770                                  GList          *llink);
771 gint   g_list_index             (GList          *list,
772                                  gpointer        data);
773 GList* g_list_last              (GList          *list);
774 GList* g_list_first             (GList          *list);
775 guint  g_list_length            (GList          *list);
776 void   g_list_foreach           (GList          *list,
777                                  GFunc           func,
778                                  gpointer        user_data);
779 gpointer g_list_nth_data        (GList          *list,
780                                  guint           n);
781 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
782 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
783
784
785 /* Singly linked lists
786  */
787 GSList* g_slist_alloc           (void);
788 void    g_slist_free            (GSList         *list);
789 void    g_slist_free_1          (GSList         *list);
790 GSList* g_slist_append          (GSList         *list,
791                                  gpointer        data);
792 GSList* g_slist_prepend         (GSList         *list,
793                                  gpointer        data);
794 GSList* g_slist_insert          (GSList         *list,
795                                  gpointer        data,
796                                  gint            position);
797 GSList* g_slist_insert_sorted   (GSList         *list,
798                                  gpointer        data,
799                                  GCompareFunc    func);
800 GSList* g_slist_concat          (GSList         *list1,
801                                  GSList         *list2);
802 GSList* g_slist_remove          (GSList         *list,
803                                  gpointer        data);
804 GSList* g_slist_remove_link     (GSList         *list,
805                                  GSList         *llink);
806 GSList* g_slist_reverse         (GSList         *list);
807 GSList* g_slist_nth             (GSList         *list,
808                                  guint           n);
809 GSList* g_slist_find            (GSList         *list,
810                                  gpointer        data);
811 GSList* g_slist_find_custom     (GSList         *list,
812                                  gpointer        data,
813                                  GCompareFunc    func);
814 gint    g_slist_position        (GSList         *list,
815                                  GSList         *llink);
816 gint    g_slist_index           (GSList         *list,
817                                  gpointer        data);
818 GSList* g_slist_last            (GSList         *list);
819 guint   g_slist_length          (GSList         *list);
820 void    g_slist_foreach         (GSList         *list,
821                                  GFunc           func,
822                                  gpointer        user_data);
823 gpointer g_slist_nth_data       (GSList         *list,
824                                  guint           n);
825 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
826
827
828 /* List Allocators
829  */
830 GListAllocator* g_list_allocator_new  (void);
831 void            g_list_allocator_free (GListAllocator* allocator);
832 GListAllocator* g_slist_set_allocator (GListAllocator* allocator);
833 GListAllocator* g_list_set_allocator  (GListAllocator* allocator);
834
835
836 /* Hash tables
837  */
838 GHashTable* g_hash_table_new            (GHashFunc       hash_func,
839                                          GCompareFunc    key_compare_func);
840 void        g_hash_table_destroy        (GHashTable     *hash_table);
841 void        g_hash_table_insert         (GHashTable     *hash_table,
842                                          gpointer        key,
843                                          gpointer        value);
844 void        g_hash_table_remove         (GHashTable     *hash_table,
845                                          gconstpointer   key);
846 gpointer    g_hash_table_lookup         (GHashTable     *hash_table,
847                                          gconstpointer   key);
848 gboolean    g_hash_table_lookup_extended(GHashTable     *hash_table,
849                                          gconstpointer   lookup_key,
850                                          gpointer       *orig_key,
851                                          gpointer       *value);
852 void        g_hash_table_freeze         (GHashTable     *hash_table);
853 void        g_hash_table_thaw           (GHashTable     *hash_table);
854 void        g_hash_table_foreach        (GHashTable     *hash_table,
855                                          GHFunc          func,
856                                          gpointer        user_data);
857 gint        g_hash_table_foreach_remove (GHashTable     *hash_table,
858                                          GHRFunc         func,
859                                          gpointer        user_data);
860 gint        g_hash_table_size           (GHashTable     *hash_table);
861
862
863 /* Caches
864  */
865 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
866                                 GCacheDestroyFunc  value_destroy_func,
867                                 GCacheDupFunc      key_dup_func,
868                                 GCacheDestroyFunc  key_destroy_func,
869                                 GHashFunc          hash_key_func,
870                                 GHashFunc          hash_value_func,
871                                 GCompareFunc       key_compare_func);
872 void     g_cache_destroy       (GCache            *cache);
873 gpointer g_cache_insert        (GCache            *cache,
874                                 gpointer           key);
875 void     g_cache_remove        (GCache            *cache,
876                                 gpointer           value);
877 void     g_cache_key_foreach   (GCache            *cache,
878                                 GHFunc             func,
879                                 gpointer           user_data);
880 void     g_cache_value_foreach (GCache            *cache,
881                                 GHFunc             func,
882                                 gpointer           user_data);
883
884
885 /* Balanced binary trees
886  */
887 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
888 void     g_tree_destroy  (GTree         *tree);
889 void     g_tree_insert   (GTree         *tree,
890                           gpointer       key,
891                           gpointer       value);
892 void     g_tree_remove   (GTree         *tree,
893                           gpointer       key);
894 gpointer g_tree_lookup   (GTree         *tree,
895                           gpointer       key);
896 void     g_tree_traverse (GTree         *tree,
897                           GTraverseFunc  traverse_func,
898                           GTraverseType  traverse_type,
899                           gpointer       data);
900 gpointer g_tree_search   (GTree         *tree,
901                           GSearchFunc    search_func,
902                           gpointer       data);
903 gint     g_tree_height   (GTree         *tree);
904 gint     g_tree_nnodes   (GTree         *tree);
905
906
907
908 /* N-way tree implementation
909  */
910 struct _GNode
911 {
912   gpointer data;
913   GNode   *next;
914   GNode   *prev;
915   GNode   *parent;
916   GNode   *children;
917 };
918
919 #define  G_NODE_IS_ROOT(node)   (((GNode*) (node))->parent == NULL && \
920                                  ((GNode*) (node))->prev == NULL && \
921                                  ((GNode*) (node))->next == NULL)
922 #define  G_NODE_IS_LEAF(node)   (((GNode*) (node))->children == NULL)
923
924 GNode*   g_node_new             (gpointer          data);
925 void     g_node_destroy         (GNode            *root);
926 void     g_node_unlink          (GNode            *node);
927 GNode*   g_node_insert          (GNode            *parent,
928                                  gint              position,
929                                  GNode            *node);
930 GNode*   g_node_insert_before   (GNode            *parent,
931                                  GNode            *sibling,
932                                  GNode            *node);
933 GNode*   g_node_prepend         (GNode            *parent,
934                                  GNode            *node);
935 guint    g_node_n_nodes         (GNode            *root,
936                                  GTraverseFlags    flags);
937 GNode*   g_node_get_root        (GNode            *node);
938 gboolean g_node_is_ancestor     (GNode            *node,
939                                  GNode            *descendant);
940 guint    g_node_depth           (GNode            *node);
941 GNode*   g_node_find            (GNode            *root,
942                                  GTraverseType     order,
943                                  GTraverseFlags    flags,
944                                  gpointer          data);
945
946 /* convenience macros */
947 #define g_node_append(parent, node)                             \
948      g_node_insert_before ((parent), NULL, (node))
949 #define g_node_insert_data(parent, position, data)              \
950      g_node_insert ((parent), (position), g_node_new (data))
951 #define g_node_insert_data_before(parent, sibling, data)        \
952      g_node_insert_before ((parent), (sibling), g_node_new (data))
953 #define g_node_prepend_data(parent, data)                       \
954      g_node_prepend ((parent), g_node_new (data))
955 #define g_node_append_data(parent, data)                        \
956      g_node_insert_before ((parent), NULL, g_node_new (data))
957
958 /* traversal function, assumes that `node' is root
959  * (only traverses `node' and its subtree).
960  * this function is just a high level interface to
961  * low level traversal functions, optimized for speed.
962  */
963 void     g_node_traverse        (GNode            *root,
964                                  GTraverseType     order,
965                                  GTraverseFlags    flags,
966                                  gint              max_depth,
967                                  GNodeTraverseFunc func,
968                                  gpointer          data);
969
970 /* return the maximum tree height starting with `node', this is an expensive
971  * operation, since we need to visit all nodes. this could be shortened by
972  * adding `guint height' to struct _GNode, but then again, this is not very
973  * often needed, and would make g_node_insert() more time consuming.
974  */
975 guint    g_node_max_height       (GNode *root);
976
977 void     g_node_children_foreach (GNode           *node,
978                                   GTraverseFlags   flags,
979                                   GNodeForeachFunc func,
980                                   gpointer         data);
981 void     g_node_reverse_children (GNode           *node);
982 guint    g_node_n_children       (GNode           *node);
983 GNode*   g_node_nth_child        (GNode           *node,
984                                   guint            n);
985 GNode*   g_node_last_child       (GNode           *node);
986 GNode*   g_node_find_child       (GNode           *node,
987                                   GTraverseFlags   flags,
988                                   gpointer         data);
989 gint     g_node_child_position   (GNode           *node,
990                                   GNode           *child);
991 gint     g_node_child_index      (GNode           *node,
992                                   gpointer         data);
993
994 GNode*   g_node_first_sibling    (GNode           *node);
995 GNode*   g_node_last_sibling     (GNode           *node);
996
997 #define  g_node_prev_sibling(node)      ((node) ? \
998                                          ((GNode*) (node))->prev : NULL)
999 #define  g_node_next_sibling(node)      ((node) ? \
1000                                          ((GNode*) (node))->next : NULL)
1001 #define  g_node_first_child(node)       ((node) ? \
1002                                          ((GNode*) (node))->children : NULL)
1003
1004
1005 /* Fatal error handlers.
1006  * g_on_error_query() will prompt the user to either
1007  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1008  * g_on_error_stack_trace() invokes gdb, which attaches to the current
1009  * process and shows a stack trace.
1010  * These function may cause different actions on non-unix platforms.
1011  * The prg_name arg is required by gdb to find the executable, if it is
1012  * passed as NULL, g_on_error_query() will try g_get_prgname().
1013  */
1014 void g_on_error_query (const gchar *prg_name);
1015 void g_on_error_stack_trace (const gchar *prg_name);
1016
1017
1018 /* Logging mechanism
1019  */
1020 extern const gchar                      *g_log_domain_glib;
1021 guint           g_log_set_handler       (const gchar    *log_domain,
1022                                          GLogLevelFlags  log_levels,
1023                                          GLogFunc        log_func,
1024                                          gpointer        user_data);
1025 void            g_log_remove_handler    (const gchar    *log_domain,
1026                                          guint           handler_id);
1027 void            g_log_default_handler   (const gchar    *log_domain,
1028                                          GLogLevelFlags  log_level,
1029                                          const gchar    *message,
1030                                          gpointer        unused_data);
1031 void            g_log                   (const gchar    *log_domain,
1032                                          GLogLevelFlags  log_level,
1033                                          const gchar    *format,
1034                                          ...) G_GNUC_PRINTF (3, 4);
1035 void            g_logv                  (const gchar    *log_domain,
1036                                          GLogLevelFlags  log_level,
1037                                          const gchar    *format,
1038                                          va_list         args);
1039 GLogLevelFlags  g_log_set_fatal_mask    (const gchar    *log_domain,
1040                                          GLogLevelFlags  fatal_mask);
1041 GLogLevelFlags  g_log_set_always_fatal  (GLogLevelFlags  fatal_mask);
1042 #ifndef G_LOG_DOMAIN
1043 #define G_LOG_DOMAIN    (NULL)
1044 #endif  /* G_LOG_DOMAIN */
1045 #ifdef  __GNUC__
1046 #define g_error(format, args...)        g_log (G_LOG_DOMAIN, \
1047                                                G_LOG_LEVEL_ERROR, \
1048                                                format, ##args)
1049 #define g_message(format, args...)      g_log (G_LOG_DOMAIN, \
1050                                                G_LOG_LEVEL_MESSAGE, \
1051                                                format, ##args)
1052 #define g_warning(format, args...)      g_log (G_LOG_DOMAIN, \
1053                                                G_LOG_LEVEL_WARNING, \
1054                                                format, ##args)
1055 #else   /* !__GNUC__ */
1056 static inline void
1057 g_error (const gchar *format,
1058          ...)
1059 {
1060   va_list args;
1061   va_start (args, format);
1062   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1063   va_end (args);
1064 }
1065 static inline void
1066 g_message (const gchar *format,
1067            ...)
1068 {
1069   va_list args;
1070   va_start (args, format);
1071   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1072   va_end (args);
1073 }
1074 static inline void
1075 g_warning (const gchar *format,
1076            ...)
1077 {
1078   va_list args;
1079   va_start (args, format);
1080   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1081   va_end (args);
1082 }
1083 #endif  /* !__GNUC__ */
1084
1085 typedef void    (*GPrintFunc)           (const gchar    *string);
1086 void            g_print                 (const gchar    *format,
1087                                          ...) G_GNUC_PRINTF (1, 2);
1088 GPrintFunc      g_set_print_handler     (GPrintFunc      func);
1089 void            g_printerr              (const gchar    *format,
1090                                          ...) G_GNUC_PRINTF (1, 2);
1091 GPrintFunc      g_set_printerr_handler  (GPrintFunc      func);
1092
1093 /* deprecated compatibility functions, use g_log_set_handler() instead */
1094 typedef void            (*GErrorFunc)           (const gchar *str);
1095 typedef void            (*GWarningFunc)         (const gchar *str);
1096 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
1097 GWarningFunc g_set_warning_handler (GWarningFunc func);
1098 GPrintFunc   g_set_message_handler (GPrintFunc func);
1099
1100
1101 /* Memory allocation and debugging
1102  */
1103 #ifdef USE_DMALLOC
1104
1105 #define g_malloc(size)       ((gpointer) MALLOC (size))
1106 #define g_malloc0(size)      ((gpointer) CALLOC (char, size))
1107 #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
1108 #define g_free(mem)          FREE (mem)
1109
1110 #else /* !USE_DMALLOC */
1111
1112 gpointer g_malloc      (gulong    size);
1113 gpointer g_malloc0     (gulong    size);
1114 gpointer g_realloc     (gpointer  mem,
1115                         gulong    size);
1116 void     g_free        (gpointer  mem);
1117
1118 #endif /* !USE_DMALLOC */
1119
1120 void     g_mem_profile (void);
1121 void     g_mem_check   (gpointer  mem);
1122
1123
1124 /* "g_mem_chunk_new" creates a new memory chunk.
1125  * Memory chunks are used to allocate pieces of memory which are
1126  *  always the same size. Lists are a good example of such a data type.
1127  * The memory chunk allocates and frees blocks of memory as needed.
1128  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1129  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
1130  *  fault...somewhere).
1131  *
1132  * Oh yeah, GMemChunk is an opaque data type. (You don't really
1133  *  want to know what's going on inside do you?)
1134  */
1135
1136 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1137  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1138  *  atom. (They are also useful for lists which use MemChunk to allocate
1139  *  memory but are also part of the MemChunk implementation).
1140  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1141  */
1142
1143 #define G_ALLOC_ONLY      1
1144 #define G_ALLOC_AND_FREE  2
1145
1146 GMemChunk* g_mem_chunk_new     (gchar     *name,
1147                                 gint       atom_size,
1148                                 gulong     area_size,
1149                                 gint       type);
1150 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
1151 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
1152 gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
1153 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
1154                                 gpointer   mem);
1155 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
1156 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
1157 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
1158 void       g_mem_chunk_info    (void);
1159
1160 /* Ah yes...we have a "g_blow_chunks" function.
1161  * "g_blow_chunks" simply compresses all the chunks. This operation
1162  *  consists of freeing every memory area that should be freed (but
1163  *  which we haven't gotten around to doing yet). And, no,
1164  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
1165  *  much better name than "g_mem_chunk_clean_all" or something
1166  *  similar.
1167  */
1168 void g_blow_chunks (void);
1169
1170
1171 /* Timer
1172  */
1173 GTimer* g_timer_new     (void);
1174 void    g_timer_destroy (GTimer  *timer);
1175 void    g_timer_start   (GTimer  *timer);
1176 void    g_timer_stop    (GTimer  *timer);
1177 void    g_timer_reset   (GTimer  *timer);
1178 gdouble g_timer_elapsed (GTimer  *timer,
1179                          gulong  *microseconds);
1180
1181
1182 /* String utility functions
1183  */
1184 #define  G_STR_DELIMITERS       "_-|> <."
1185 void     g_strdelimit           (gchar       *string,
1186                                  const gchar *delimiters,
1187                                  gchar        new_delimiter);
1188 gchar*   g_strdup               (const gchar *str);
1189 gchar*   g_strdup_printf        (const gchar *format,
1190                                  ...) G_GNUC_PRINTF (1, 2);
1191 gchar*   g_strdup_vprintf       (const gchar *format,
1192                                  va_list      args);
1193 gchar*   g_strndup              (const gchar *str,
1194                                  guint        n);
1195 gchar*   g_strnfill             (guint        length,
1196                                  gchar        fill_char);
1197 gchar*   g_strconcat            (const gchar *string1,
1198                                  ...); /* NULL terminated */
1199 gdouble  g_strtod               (const gchar *nptr,
1200                                  gchar      **endptr);
1201 gchar*   g_strerror             (gint         errnum);
1202 gchar*   g_strsignal            (gint         signum);
1203 gint     g_strcasecmp           (const gchar *s1,
1204                                  const gchar *s2);
1205 void     g_strdown              (gchar       *string);
1206 void     g_strup                (gchar       *string);
1207 void     g_strreverse           (gchar       *string);
1208 gpointer g_memdup               (gconstpointer mem,
1209                                  guint         byte_size);
1210
1211 /* calculate a string size, guarranteed to fit format + args.
1212  */
1213 guint   g_printf_string_upper_bound (const gchar* format,
1214                                      va_list      args);
1215
1216
1217 /* Retrive static string info
1218  */
1219 gchar*  g_get_user_name         (void);
1220 gchar*  g_get_real_name         (void);
1221 gchar*  g_get_home_dir          (void);
1222 gchar*  g_get_tmp_dir           (void);
1223 gchar*  g_get_prgname           (void);
1224 void    g_set_prgname           (const gchar *prgname);
1225
1226
1227 /* Miscellaneous utility functions
1228  */
1229 guint   g_parse_debug_string    (const gchar *string,
1230                                  GDebugKey   *keys,
1231                                  guint        nkeys);
1232 gint    g_snprintf              (gchar       *string,
1233                                  gulong       n,
1234                                  gchar const *format,
1235                                  ...) G_GNUC_PRINTF (3, 4);
1236 gint    g_vsnprintf             (gchar       *string,
1237                                  gulong       n,
1238                                  gchar const *format,
1239                                  va_list      args);
1240 gchar*  g_basename              (const gchar *file_name);
1241
1242 /* strings are newly allocated with g_malloc() */
1243 gchar*  g_dirname               (const gchar *file_name);
1244 gchar*  g_get_current_dir       (void);
1245
1246 /* We make the assumption that if memmove isn't available, then
1247  * bcopy will do the job. This isn't safe everywhere. (bcopy can't
1248  * necessarily handle overlapping copies).
1249  * Either way, g_memmove() will not return a value.
1250  */
1251 #ifdef HAVE_MEMMOVE
1252 #define g_memmove(dest, src, size)      G_STMT_START {  \
1253      memmove ((dest), (src), (size));                   \
1254 } G_STMT_END
1255 #else
1256 #define g_memmove(dest, src, size)      G_STMT_START {  \
1257      bcopy ((src), (dest), (size));                     \
1258 } G_STMT_END
1259 #endif
1260
1261 /* we use a GLib function as a replacement for ATEXIT, so
1262  * the programmer is not required to check the return value
1263  * (if there is any in the implementation) and doesn't encounter
1264  * missing include files.
1265  */
1266 void    g_atexit                (GVoidFunc    func);
1267
1268
1269 /* Bit tests
1270  */
1271 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
1272                                        gint    nth_bit);
1273 #ifdef  G_CAN_INLINE
1274 G_INLINE_FUNC gint
1275 g_bit_nth_lsf (guint32 mask,
1276                gint    nth_bit)
1277 {
1278   do
1279     {
1280       nth_bit++;
1281       if (mask & (1 << (guint) nth_bit))
1282         return nth_bit;
1283     }
1284   while (nth_bit < 32);
1285   return -1;
1286 }
1287 #endif  /* G_CAN_INLINE */
1288
1289 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
1290                                        gint    nth_bit);
1291 #ifdef G_CAN_INLINE
1292 G_INLINE_FUNC gint
1293 g_bit_nth_msf (guint32 mask,
1294                gint    nth_bit)
1295 {
1296   if (nth_bit < 0)
1297     nth_bit = 33;
1298   do
1299     {
1300       nth_bit--;
1301       if (mask & (1 << (guint) nth_bit))
1302         return nth_bit;
1303     }
1304   while (nth_bit > 0);
1305   return -1;
1306 }
1307 #endif  /* G_CAN_INLINE */
1308
1309 G_INLINE_FUNC guint     g_bit_storage (guint number);
1310 #ifdef G_CAN_INLINE
1311 G_INLINE_FUNC guint
1312 g_bit_storage (guint number)
1313 {
1314   register guint n_bits = 0;
1315
1316   do
1317     {
1318       n_bits++;
1319       number >>= 1;
1320     }
1321   while (number);
1322   return n_bits;
1323 }
1324 #endif  /* G_CAN_INLINE */
1325
1326
1327 /* String Chunks
1328  */
1329 GStringChunk* g_string_chunk_new           (gint size);
1330 void          g_string_chunk_free          (GStringChunk *chunk);
1331 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
1332                                             const gchar  *string);
1333 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
1334                                             const gchar  *string);
1335
1336
1337 /* Strings
1338  */
1339 GString* g_string_new       (const gchar *init);
1340 GString* g_string_sized_new (guint        dfl_size);
1341 void     g_string_free      (GString     *string,
1342                              gint         free_segment);
1343 GString* g_string_assign    (GString     *lval,
1344                              const gchar *rval);
1345 GString* g_string_truncate  (GString     *string,
1346                              gint         len);
1347 GString* g_string_append    (GString     *string,
1348                              const gchar *val);
1349 GString* g_string_append_c  (GString     *string,
1350                              gchar        c);
1351 GString* g_string_prepend   (GString     *string,
1352                              const gchar *val);
1353 GString* g_string_prepend_c (GString     *string,
1354                              gchar        c);
1355 GString* g_string_insert    (GString     *string,
1356                              gint         pos,
1357                              const gchar *val);
1358 GString* g_string_insert_c  (GString     *string,
1359                              gint         pos,
1360                              gchar        c);
1361 GString* g_string_erase     (GString     *string,
1362                              gint         pos,
1363                              gint         len);
1364 GString* g_string_down      (GString     *string);
1365 GString* g_string_up        (GString     *string);
1366 void     g_string_sprintf   (GString     *string,
1367                              const gchar *format,
1368                              ...) G_GNUC_PRINTF (2, 3);
1369 void     g_string_sprintfa  (GString     *string,
1370                              const gchar *format,
1371                              ...) G_GNUC_PRINTF (2, 3);
1372
1373
1374 /* Resizable arrays
1375  */
1376
1377 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1378 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1379 #define g_array_index(a,t,i) (((t*)a->data)[i])
1380
1381 GArray* g_array_new          (gboolean      zero_terminated,
1382                               gboolean      clear,
1383                               guint         element_size);
1384 void    g_array_free         (GArray       *array,
1385                               gboolean      free_segment);
1386 GArray* g_array_append_vals  (GArray       *array,
1387                               gconstpointer data,
1388                               guint         len);
1389 GArray* g_array_prepend_vals (GArray       *array,
1390                               gconstpointer data,
1391                               guint         len);
1392 GArray* g_array_set_size     (GArray       *array,
1393                               guint         length);
1394
1395 /* Resizable pointer array.  This interface is much less complicated
1396  * than the above.  Add appends appends a pointer.  Remove fills any
1397  * cleared spot and shortens the array.
1398  */
1399 #define     g_ptr_array_index(array,index) (array->pdata)[index]
1400 GPtrArray*  g_ptr_array_new                (void);
1401 void        g_ptr_array_free               (GPtrArray   *array,
1402                                             gboolean     free_seg);
1403 void        g_ptr_array_set_size           (GPtrArray   *array,
1404                                             gint         length);
1405 gpointer    g_ptr_array_remove_index       (GPtrArray   *array,
1406                                             gint         index);
1407 gboolean    g_ptr_array_remove             (GPtrArray   *array,
1408                                             gpointer     data);
1409 void        g_ptr_array_add                (GPtrArray   *array,
1410                                             gpointer     data);
1411
1412 /* Byte arrays, an array of guint8.  Implemented as a GArray,
1413  * but type-safe.
1414  */
1415
1416 GByteArray* g_byte_array_new      (void);
1417 void        g_byte_array_free     (GByteArray   *array,
1418                                    gboolean      free_segment);
1419 GByteArray* g_byte_array_append   (GByteArray   *array,
1420                                    const guint8 *data,
1421                                    guint         len);
1422 GByteArray* g_byte_array_prepend  (GByteArray   *array,
1423                                    const guint8 *data,
1424                                    guint         len);
1425 GByteArray* g_byte_array_set_size (GByteArray   *array,
1426                                    guint         length);
1427
1428
1429 /* Hash Functions
1430  */
1431 gint  g_str_equal (gconstpointer   v,
1432                    gconstpointer   v2);
1433 guint g_str_hash  (gconstpointer   v);
1434
1435 gint  g_int_equal (gconstpointer   v,
1436                    gconstpointer   v2);
1437 guint g_int_hash  (gconstpointer   v);
1438
1439 /* This "hash" function will just return the key's adress as an
1440  * unsigned integer. Useful for hashing on plain adresses or
1441  * simple integer values.
1442  */
1443 guint g_direct_hash  (gconstpointer v);
1444 gint  g_direct_equal (gconstpointer v,
1445                       gconstpointer v2);
1446
1447
1448 /* Quarks (string<->id association)
1449  */
1450 GQuark    g_quark_try_string            (const gchar    *string);
1451 GQuark    g_quark_from_static_string    (const gchar    *string);
1452 GQuark    g_quark_from_string           (const gchar    *string);
1453 gchar*    g_quark_to_string             (GQuark          quark);
1454
1455
1456 /* Keyed Data List
1457  */
1458 void      g_datalist_init               (GData          **datalist);
1459 void      g_datalist_clear              (GData          **datalist);
1460 gpointer  g_datalist_id_get_data        (GData          **datalist,
1461                                          GQuark           key_id);
1462 void      g_datalist_id_set_data_full   (GData          **datalist,
1463                                          GQuark           key_id,
1464                                          gpointer         data,
1465                                          GDestroyNotify   destroy_func);
1466 void      g_datalist_id_set_destroy     (GData          **datalist,
1467                                          GQuark           key_id,
1468                                          GDestroyNotify   destroy_func);
1469 void      g_datalist_foreach            (GData          **datalist,
1470                                          GDataForeachFunc func,
1471                                          gpointer         user_data);
1472 #define   g_datalist_id_set_data(dl, q, d)      \
1473      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1474 #define   g_datalist_id_remove_data(dl, q)      \
1475      g_datalist_id_set_data ((dl), (q), NULL)
1476 #define   g_datalist_get_data(dl, k)            \
1477      (g_datalist_id_get_data ((dl), g_quark_try_string ((k))))
1478 #define   g_datalist_set_data_full(dl, k, d, f) \
1479      g_datalist_id_set_data_full ((dl), g_quark_from_string ((k)), (d), (f))
1480 #define   g_datalist_set_destroy(dl, k, f)      \
1481      g_datalist_id_set_destroy ((dl), g_quark_try_string ((k)), (f))
1482 #define   g_datalist_set_data(dl, k, d)         \
1483      g_datalist_set_data_full ((dl), (k), (d), NULL)
1484 #define   g_datalist_remove_data(dl, k)         \
1485      g_datalist_id_set_data ((dl), g_quark_try_string ((k)), NULL)
1486
1487
1488 /* Location Associated Keyed Data
1489  */
1490 void      g_dataset_destroy             (gconstpointer    dataset_location);
1491 gpointer  g_dataset_id_get_data         (gconstpointer    dataset_location,
1492                                          GQuark           key_id);
1493 void      g_dataset_id_set_data_full    (gconstpointer    dataset_location,
1494                                          GQuark           key_id,
1495                                          gpointer         data,
1496                                          GDestroyNotify   destroy_func);
1497 void      g_dataset_id_set_destroy      (gconstpointer    dataset_location,
1498                                          GQuark           key_id,
1499                                          GDestroyNotify   destroy_func);
1500 void      g_dataset_foreach             (gconstpointer    dataset_location,
1501                                          GDataForeachFunc func,
1502                                          gpointer         user_data);
1503 #define   g_dataset_id_set_data(l, k, d)        \
1504      g_dataset_id_set_data_full ((l), (k), (d), NULL)
1505 #define   g_dataset_id_remove_data(l, k)        \
1506      g_dataset_id_set_data ((l), (k), NULL)
1507 #define   g_dataset_get_data(l, k)              \
1508      (g_dataset_id_get_data ((l), g_quark_try_string ((k))))
1509 #define   g_dataset_set_data_full(l, k, d, f)   \
1510      g_dataset_id_set_data_full ((l), g_quark_from_string ((k)), (d), (f))
1511 #define   g_dataset_set_destroy(l, k, f)        \
1512      g_dataset_id_set_destroy ((l), g_quark_try_string ((k)), (f))
1513 #define   g_dataset_set_data(l, k, d)           \
1514      g_dataset_set_data_full ((l), (k), (d), NULL)
1515 #define   g_dataset_remove_data(l, k)           \
1516      g_dataset_id_set_data ((l), g_quark_try_string ((k)), NULL)
1517
1518
1519 /* GScanner: Flexible lexical scanner for general purpose.
1520  */
1521
1522 /* Character sets */
1523 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1524 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
1525 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
1526                         "\307\310\311\312\313\314\315\316\317\320"\
1527                         "\321\322\323\324\325\326"\
1528                         "\330\331\332\333\334\335\336"
1529 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
1530                         "\347\350\351\352\353\354\355\356\357\360"\
1531                         "\361\362\363\364\365\366"\
1532                         "\370\371\372\373\374\375\376\377"
1533
1534 /* Error types */
1535 typedef enum
1536 {
1537   G_ERR_UNKNOWN,
1538   G_ERR_UNEXP_EOF,
1539   G_ERR_UNEXP_EOF_IN_STRING,
1540   G_ERR_UNEXP_EOF_IN_COMMENT,
1541   G_ERR_NON_DIGIT_IN_CONST,
1542   G_ERR_DIGIT_RADIX,
1543   G_ERR_FLOAT_RADIX,
1544   G_ERR_FLOAT_MALFORMED
1545 } GErrorType;
1546
1547 /* Token types */
1548 typedef enum
1549 {
1550   G_TOKEN_EOF                   =   0,
1551
1552   G_TOKEN_LEFT_PAREN            = '(',
1553   G_TOKEN_RIGHT_PAREN           = ')',
1554   G_TOKEN_LEFT_CURLY            = '{',
1555   G_TOKEN_RIGHT_CURLY           = '}',
1556   G_TOKEN_LEFT_BRACE            = '[',
1557   G_TOKEN_RIGHT_BRACE           = ']',
1558   G_TOKEN_EQUAL_SIGN            = '=',
1559   G_TOKEN_COMMA                 = ',',
1560
1561   G_TOKEN_NONE                  = 256,
1562
1563   G_TOKEN_ERROR,
1564
1565   G_TOKEN_CHAR,
1566   G_TOKEN_BINARY,
1567   G_TOKEN_OCTAL,
1568   G_TOKEN_INT,
1569   G_TOKEN_HEX,
1570   G_TOKEN_FLOAT,
1571   G_TOKEN_STRING,
1572
1573   G_TOKEN_SYMBOL,
1574   G_TOKEN_IDENTIFIER,
1575   G_TOKEN_IDENTIFIER_NULL,
1576
1577   G_TOKEN_COMMENT_SINGLE,
1578   G_TOKEN_COMMENT_MULTI,
1579   G_TOKEN_LAST
1580 } GTokenType;
1581
1582 union   _GValue
1583 {
1584   gpointer      v_symbol;
1585   gchar         *v_identifier;
1586   gulong        v_binary;
1587   gulong        v_octal;
1588   gulong        v_int;
1589   gdouble       v_float;
1590   gulong        v_hex;
1591   gchar         *v_string;
1592   gchar         *v_comment;
1593   guchar        v_char;
1594   guint         v_error;
1595 };
1596
1597 struct  _GScannerConfig
1598 {
1599   /* Character sets
1600    */
1601   gchar         *cset_skip_characters;          /* default: " \t\n" */
1602   gchar         *cset_identifier_first;
1603   gchar         *cset_identifier_nth;
1604   gchar         *cpair_comment_single;          /* default: "#\n" */
1605
1606   /* Should symbol lookup work case sensitive?
1607    */
1608   guint         case_sensitive : 1;
1609
1610   /* Boolean values to be adjusted "on the fly"
1611    * to configure scanning behaviour.
1612    */
1613   guint         skip_comment_multi : 1;         /* C like comment */
1614   guint         skip_comment_single : 1;        /* single line comment */
1615   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1616   guint         scan_identifier : 1;
1617   guint         scan_identifier_1char : 1;
1618   guint         scan_identifier_NULL : 1;
1619   guint         scan_symbols : 1;
1620   guint         scan_binary : 1;
1621   guint         scan_octal : 1;
1622   guint         scan_float : 1;
1623   guint         scan_hex : 1;                   /* `0x0ff0' */
1624   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1625   guint         scan_string_sq : 1;             /* string: 'anything' */
1626   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1627   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1628   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1629   guint         identifier_2_string : 1;
1630   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1631   guint         symbol_2_token : 1;
1632   guint         scope_0_fallback : 1;           /* try scope 0 on lookups? */
1633 };
1634
1635 struct  _GScanner
1636 {
1637   /* unused fields */
1638   gpointer              user_data;
1639   guint                 max_parse_errors;
1640
1641   /* g_scanner_error() increments this field */
1642   guint                 parse_errors;
1643
1644   /* name of input stream, featured by the default message handler */
1645   const gchar           *input_name;
1646
1647   /* data pointer for derived structures */
1648   gpointer              derived_data;
1649
1650   /* link into the scanner configuration */
1651   GScannerConfig        *config;
1652
1653   /* fields filled in after g_scanner_get_next_token() */
1654   GTokenType            token;
1655   GValue                value;
1656   guint                 line;
1657   guint                 position;
1658
1659   /* fields filled in after g_scanner_peek_next_token() */
1660   GTokenType            next_token;
1661   GValue                next_value;
1662   guint                 next_line;
1663   guint                 next_position;
1664
1665   /* to be considered private */
1666   GHashTable            *symbol_table;
1667   const gchar           *text;
1668   guint                 text_len;
1669   gint                  input_fd;
1670   gint                  peeked_char;
1671   guint                 scope_id;
1672
1673   /* handler function for _warn and _error */
1674   GScannerMsgFunc       msg_handler;
1675 };
1676
1677 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
1678 void            g_scanner_destroy               (GScanner       *scanner);
1679 void            g_scanner_input_file            (GScanner       *scanner,
1680                                                  gint           input_fd);
1681 void            g_scanner_input_text            (GScanner       *scanner,
1682                                                  const  gchar   *text,
1683                                                  guint          text_len);
1684 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
1685 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
1686 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
1687 GValue          g_scanner_cur_value             (GScanner       *scanner);
1688 guint           g_scanner_cur_line              (GScanner       *scanner);
1689 guint           g_scanner_cur_position          (GScanner       *scanner);
1690 gboolean        g_scanner_eof                   (GScanner       *scanner);
1691 guint           g_scanner_set_scope             (GScanner       *scanner,
1692                                                  guint           scope_id);
1693 void            g_scanner_scope_add_symbol      (GScanner       *scanner,
1694                                                  guint           scope_id,
1695                                                  const gchar    *symbol,
1696                                                  gpointer       value);
1697 void            g_scanner_scope_remove_symbol   (GScanner       *scanner,
1698                                                  guint           scope_id,
1699                                                  const gchar    *symbol);
1700 gpointer        g_scanner_scope_lookup_symbol   (GScanner       *scanner,
1701                                                  guint           scope_id,
1702                                                  const gchar    *symbol);
1703 void            g_scanner_scope_foreach_symbol  (GScanner       *scanner,
1704                                                  guint           scope_id,
1705                                                  GHFunc          func,
1706                                                  gpointer        func_data);
1707 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
1708                                                  const gchar    *symbol);
1709 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
1710 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
1711 void            g_scanner_unexp_token           (GScanner       *scanner,
1712                                                  GTokenType     expected_token,
1713                                                  const gchar    *identifier_spec,
1714                                                  const gchar    *symbol_spec,
1715                                                  const gchar    *symbol_name,
1716                                                  const gchar    *message,
1717                                                  gint            is_error);
1718 void            g_scanner_error                 (GScanner       *scanner,
1719                                                  const gchar    *format,
1720                                                  ...) G_GNUC_PRINTF (2,3);
1721 void            g_scanner_warn                  (GScanner       *scanner,
1722                                                  const gchar    *format,
1723                                                  ...) G_GNUC_PRINTF (2,3);
1724 gint            g_scanner_stat_mode             (const gchar    *filename);
1725 /* keep downward source compatibility */
1726 #define         g_scanner_add_symbol( scanner, symbol, value )  G_STMT_START { \
1727   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
1728 } G_STMT_END
1729 #define         g_scanner_remove_symbol( scanner, symbol )      G_STMT_START { \
1730   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
1731 } G_STMT_END
1732 #define         g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
1733   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
1734 } G_STMT_END
1735
1736
1737 /* Completion */
1738
1739 struct _GCompletion
1740 {
1741   GList* items;
1742   GCompletionFunc func;
1743
1744   gchar* prefix;
1745   GList* cache;
1746 };
1747
1748 GCompletion* g_completion_new          (GCompletionFunc func);
1749 void         g_completion_add_items    (GCompletion*    cmp,
1750                                         GList*          items);
1751 void         g_completion_remove_items (GCompletion*    cmp,
1752                                         GList*          items);
1753 void         g_completion_clear_items  (GCompletion*    cmp);
1754 GList*       g_completion_complete     (GCompletion*    cmp,
1755                                         gchar*          prefix,
1756                                         gchar**         new_prefix);
1757 void         g_completion_free         (GCompletion*    cmp);
1758
1759
1760 /* GRelation: Indexed Relations.  Imagine a really simple table in a
1761  * database.  Relations are not ordered.  This data type is meant for
1762  * maintaining a N-way mapping.
1763  *
1764  * g_relation_new() creates a relation with FIELDS fields
1765  *
1766  * g_relation_destroy() frees all resources
1767  * g_tuples_destroy() frees the result of g_relation_select()
1768  *
1769  * g_relation_index() indexes relation FIELD with the provided
1770  *   equality and hash functions.  this must be done before any
1771  *   calls to insert are made.
1772  *
1773  * g_relation_insert() inserts a new tuple.  you are expected to
1774  *   provide the right number of fields.
1775  *
1776  * g_relation_delete() deletes all relations with KEY in FIELD
1777  * g_relation_select() returns ...
1778  * g_relation_count() counts ...
1779  */
1780
1781 GRelation* g_relation_new     (gint         fields);
1782 void       g_relation_destroy (GRelation   *relation);
1783 void       g_relation_index   (GRelation   *relation,
1784                                gint         field,
1785                                GHashFunc    hash_func,
1786                                GCompareFunc key_compare_func);
1787 void       g_relation_insert  (GRelation   *relation,
1788                                ...);
1789 gint       g_relation_delete  (GRelation   *relation,
1790                                gconstpointer  key,
1791                                gint         field);
1792 GTuples*   g_relation_select  (GRelation   *relation,
1793                                gconstpointer  key,
1794                                gint         field);
1795 gint       g_relation_count   (GRelation   *relation,
1796                                gconstpointer  key,
1797                                gint         field);
1798 gboolean   g_relation_exists  (GRelation   *relation,
1799                                ...);
1800 void       g_relation_print   (GRelation   *relation);
1801
1802 void       g_tuples_destroy   (GTuples     *tuples);
1803 gpointer   g_tuples_index     (GTuples     *tuples,
1804                                gint         index,
1805                                gint         field);
1806
1807
1808 /* Prime numbers.
1809  */
1810
1811 /* This function returns prime numbers spaced by approximately 1.5-2.0
1812  * and is for use in resizing data structures which prefer
1813  * prime-valued sizes.  The closest spaced prime function returns the
1814  * next largest prime, or the highest it knows about which is about
1815  * MAXINT/4.
1816  */
1817
1818 guint      g_spaced_primes_closest (guint num);
1819
1820 /* Glib version.
1821  */
1822 extern const guint glib_major_version;
1823 extern const guint glib_minor_version;
1824 extern const guint glib_micro_version;
1825
1826 #ifdef __cplusplus
1827 }
1828 #endif /* __cplusplus */
1829
1830
1831 #endif /* __G_LIB_H__ */