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