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