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