new function g_dataset_retrive_key. adjusted prealloc sizes, to take up
[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 #include <glibconfig.h>
23
24 #ifdef USE_DMALLOC
25 #include "dmalloc.h"
26 #endif
27
28
29 /* glib provides definitions for the extrema of many
30  *  of the standard types. These are:
31  * G_MINFLOAT
32  * G_MAXFLOAT
33  * G_MINDOUBLE
34  * G_MAXDOUBLE
35  * G_MINSHORT
36  * G_MAXSHORT
37  * G_MININT
38  * G_MAXINT
39  * G_MINLONG
40  * G_MAXLONG
41  */
42
43 #ifdef HAVE_FLOAT_H
44
45 #include <float.h>
46
47 #define G_MINFLOAT   FLT_MIN
48 #define G_MAXFLOAT   FLT_MAX
49 #define G_MINDOUBLE  DBL_MIN
50 #define G_MAXDOUBLE  DBL_MAX
51
52 #elif HAVE_VALUES_H
53
54 #include <values.h>
55
56 #define G_MINFLOAT  MINFLOAT
57 #define G_MAXFLOAT  MAXFLOAT
58 #define G_MINDOUBLE MINDOUBLE
59 #define G_MAXDOUBLE MAXDOUBLE
60
61 #endif /* HAVE_VALUES_H */
62
63
64 #ifdef HAVE_LIMITS_H
65
66 #include <limits.h>
67
68 #define G_MINSHORT  SHRT_MIN
69 #define G_MAXSHORT  SHRT_MAX
70 #define G_MININT    INT_MIN
71 #define G_MAXINT    INT_MAX
72 #define G_MINLONG   LONG_MIN
73 #define G_MAXLONG   LONG_MAX
74
75 #elif HAVE_VALUES_H
76
77 #ifdef HAVE_FLOAT_H
78 #include <values.h>
79 #endif /* HAVE_FLOAT_H */
80
81 #define G_MINSHORT  MINSHORT
82 #define G_MAXSHORT  MAXSHORT
83 #define G_MININT    MININT
84 #define G_MAXINT    MAXINT
85 #define G_MINLONG   MINLONG
86 #define G_MAXLONG   MAXLONG
87
88 #endif /* HAVE_VALUES_H */
89
90
91 /* Provide definitions for some commonly used macros.
92  *  Some of them are only provided if they haven't already
93  *  been defined. It is assumed that if they are already
94  *  defined then the current definition is correct.
95  */
96 #ifndef NULL
97 #define NULL    ((void*) 0)
98 #endif
99
100 #ifndef FALSE
101 #define FALSE   (0)
102 #endif
103
104 #ifndef TRUE
105 #define TRUE    (!FALSE)
106 #endif
107
108 #undef  MAX
109 #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
110
111 #undef  MIN
112 #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
113
114 #undef  ABS
115 #define ABS(a)     (((a) < 0) ? -(a) : (a))
116
117 #undef  CLAMP
118 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
119
120
121 /* Provide simple enum value macro wrappers that ease automated enum value
122  * stringification code.
123  */
124 #if     !defined (G_CODE_GENERATION)
125 #define G_ENUM( EnumerationName )               EnumerationName
126 #define G_FLAGS( EnumerationName )              EnumerationName
127 #define G_NV( VALUE_NAME , value_nick, VALUE)   VALUE_NAME = (VALUE)
128 #define G_SV( VALUE_NAME, value_nick )          VALUE_NAME
129 #else   /* G_CODE_GENERATION */
130 #define G_ENUM( EnumerationName )               G_ENUM_E + EnumerationName +
131 #define G_FLAGS( EnumerationName )              G_ENUM_F + EnumerationName +
132 #define G_NV( VALUE_NAME , value_nick, VALUE)   G_ENUM_V + VALUE_NAME + value_nick +
133 #define G_SV( VALUE_NAME, value_nick )          G_ENUM_V + VALUE_NAME + value_nick +
134 #endif  /* G_CODE_GENERATION */
135
136
137 /* Provide simple macro statement wrappers (adapted from Pearl):
138  *  G_STMT_START { statements; } G_STMT_END;
139  *  can be used as a single statement, as in
140  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
141  *
142  *  For gcc we will wrap the statements within `({' and `})' braces.
143  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
144  *  and otherwise within `do' and `while (0)'.
145  */
146 #if !(defined (G_STMT_START) && defined (G_STMT_END))
147 #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
148 #    define G_STMT_START        (void)(
149 #    define G_STMT_END          )
150 #  else
151 #    if (defined (sun) || defined (__sun__))
152 #      define G_STMT_START      if (1)
153 #      define G_STMT_END        else (void)0
154 #    else
155 #      define G_STMT_START      do
156 #      define G_STMT_END        while (0)
157 #    endif
158 #  endif
159 #endif
160
161 /* Provide macros to feature the GCC function attribute.
162  */
163 #if     __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
164 #define G_GNUC_PRINTF( format_idx, arg_idx )    \
165   __attribute__((format (printf, format_idx, arg_idx)))
166 #define G_GNUC_SCANF( format_idx, arg_idx )     \
167   __attribute__((format (scanf, format_idx, arg_idx)))
168 #define G_GNUC_FORMAT( arg_idx )                \
169   __attribute__((format_arg (arg_idx)))
170 #define G_GNUC_NORETURN                         \
171   __attribute__((noreturn))
172 #define G_GNUC_CONST                            \
173   __attribute__((const))
174 #else   /* !__GNUC__ */
175 #define G_GNUC_PRINTF( format_idx, arg_idx )
176 #define G_GNUC_SCANF( format_idx, arg_idx )
177 #define G_GNUC_FORMAT( arg_idx )
178 #define G_GNUC_NORETURN
179 #define G_GNUC_CONST
180 #endif  /* !__GNUC__ */
181
182 /* Hacker macro to place breakpoints for x86 machines.
183  * Actuall use is strongly deprecated of course ;)
184  */
185 #if     defined (__i386__)
186 #define G_BREAKPOINT()          G_STMT_START{ __asm__ ("int $03"); }G_STMT_END
187 #else   /* !__i386__ */
188 #define G_BREAKPOINT()
189 #endif  /* __i386__ */
190
191 /* Wrap the __PRETTY_FUNCTION__ and __FUNCTION__ variables with macros,
192  * so we can refer to them as strings unconditionally.
193  */
194 #ifdef  __GNUC__
195 #define G_GNUC_FUNCTION         (__FUNCTION__)
196 #define G_GNUC_PRETTY_FUNCTION  (__PRETTY_FUNCTION__)
197 #else   /* !__GNUC__ */
198 #define G_GNUC_FUNCTION         ("")
199 #define G_GNUC_PRETTY_FUNCTION  ("")
200 #endif  /* !__GNUC__ */
201
202
203 #ifndef ATEXIT
204 #  ifdef HAVE_ATEXIT
205 #    define ATEXIT(proc)   (atexit (proc))
206 #  elif defined (HAVE_ON_EXIT)
207 #    define ATEXIT(proc)   (on_exit ((void (*)(int, void *))(proc), NULL))
208 #  endif    
209 #endif /* ATEXIT */
210
211
212 /* Provide macros for easily allocating memory. The macros
213  *  will cast the allocated memory to the specified type
214  *  in order to avoid compiler warnings. (Makes the code neater).
215  */
216
217 #ifdef __DMALLOC_H__
218
219 #define g_new(type,count)        ALLOC(type,count)
220 #define g_new0(type,count)       CALLOC(type,count)
221
222 #else /* __DMALLOC_H__ */
223
224 #define g_new(type, count)        \
225     ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
226 #define g_new0(type, count)       \
227     ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
228 #endif /* __DMALLOC_H__ */
229
230 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
231   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
232                    sizeof (type), \
233                    sizeof (type) * (pre_alloc), \
234                    (alloc_type)) \
235 )
236 #define g_chunk_new(type, chunk)        ( \
237   (type *) g_mem_chunk_alloc (chunk) \
238 )
239 #define g_chunk_new0(type, chunk)       ( \
240   (type *) memset (g_mem_chunk_alloc (chunk), 0, sizeof (type)) \
241 )
242 #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
243   g_mem_chunk_free ((mem_chunk), (mem)); \
244 } G_STMT_END
245
246 #define g_string(x) #x
247
248
249 /* Provide macros for error handling. The "assert" macros will
250  *  exit on failure. The "return" macros will exit the current
251  *  function. Two different definitions are given for the macros
252  *  in order to support gcc's __PRETTY_FUNCTION__ capability.
253  */
254
255 #ifdef G_DISABLE_ASSERT
256
257 #define g_assert(expr)
258 #define g_assert_not_reached()
259
260 #else /* !G_DISABLE_ASSERT */
261
262 #ifdef __GNUC__
263
264 #define g_assert(expr)                  G_STMT_START{\
265      if (!(expr))                                    \
266        g_error ("file %s: line %d (%s): \"%s\"",     \
267                 __FILE__,                            \
268                 __LINE__,                            \
269                 __PRETTY_FUNCTION__,                 \
270                 #expr);                 }G_STMT_END
271
272 #define g_assert_not_reached()          G_STMT_START{                 \
273      g_error ("file %s: line %d (%s): \"should not be reached\"",     \
274               __FILE__,                                               \
275               __LINE__,                                               \
276               __PRETTY_FUNCTION__);     }G_STMT_END
277
278 #else /* !__GNUC__ */
279
280 #define g_assert(expr)                  G_STMT_START{\
281      if (!(expr))                                    \
282        g_error ("file %s: line %d: \"%s\"",          \
283                 __FILE__,                            \
284                 __LINE__,                            \
285                 #expr);                 }G_STMT_END
286
287 #define g_assert_not_reached()          G_STMT_START{                 \
288      g_error ("file %s: line %d: \"should not be reached\"",          \
289               __FILE__,                                               \
290               __LINE__);                }G_STMT_END
291
292 #endif /* __GNUC__ */
293
294 #endif /* G_DISABLE_ASSERT */
295
296 #ifdef G_DISABLE_CHECKS
297
298 #define g_return_if_fail(expr)
299 #define g_return_val_if_fail(expr,val)
300
301 #else /* !G_DISABLE_CHECKS */
302
303 #ifdef __GNUC__
304
305 #define g_return_if_fail(expr)          G_STMT_START{          \
306      if (!(expr))                                              \
307        {                                                       \
308          g_warning ("file %s: line %d (%s): \"%s\"",           \
309                     __FILE__,                                  \
310                     __LINE__,                                  \
311                     __PRETTY_FUNCTION__,                       \
312                     #expr);                                    \
313          return;                                               \
314        };                               }G_STMT_END
315
316 #define g_return_val_if_fail(expr,val)  G_STMT_START{          \
317      if (!(expr))                                              \
318        {                                                       \
319          g_warning ("file %s: line %d (%s): \"%s\"",           \
320                     __FILE__,                                  \
321                     __LINE__,                                  \
322                     __PRETTY_FUNCTION__,                       \
323                     #expr);                                    \
324          return val;                                           \
325        };                               }G_STMT_END
326
327 #else /* !__GNUC__ */
328
329 #define g_return_if_fail(expr)          G_STMT_START{    \
330      if (!(expr))                                        \
331        {                                                 \
332          g_warning ("file %s: line %d: \"%s\"",          \
333                     __FILE__,                            \
334                     __LINE__,                            \
335                     #expr);                              \
336          return;                                         \
337        };                               }G_STMT_END
338
339 #define g_return_val_if_fail(expr, val) G_STMT_START{    \
340      if (!(expr))                                        \
341        {                                                 \
342          g_warning ("file %s: line %d: \"%s\"",          \
343                     __FILE__,                            \
344                     __LINE__,                            \
345                     #expr);                              \
346          return val;                                     \
347        };                               }G_STMT_END
348
349 #endif /* !__GNUC__ */
350
351 #endif /* G_DISABLE_CHECKS */
352
353
354 #ifdef __cplusplus
355 extern "C" {
356 #pragma }
357 #endif /* __cplusplus */
358
359 /* Provide type definitions for commonly used types.
360  *  These are useful because a "gint8" can be adjusted
361  *  to be 1 byte (8 bits) on all platforms. Similarly and
362  *  more importantly, "gint32" can be adjusted to be
363  *  4 bytes (32 bits) on all platforms.
364  */
365
366 typedef char   gchar;
367 typedef short  gshort;
368 typedef long   glong;
369 typedef int    gint;
370 typedef gint   gboolean;
371
372 typedef unsigned char   guchar;
373 typedef unsigned short  gushort;
374 typedef unsigned long   gulong;
375 typedef unsigned int    guint;
376
377 typedef float   gfloat;
378 typedef double  gdouble;
379
380 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms. 
381  * Since gldouble isn't used anywhere, just disable it for now */
382
383 #if 0
384 #ifdef HAVE_LONG_DOUBLE
385 typedef long double gldouble;
386 #else /* HAVE_LONG_DOUBLE */
387 typedef double gldouble;
388 #endif /* HAVE_LONG_DOUBLE */
389 #endif /* 0 */
390
391 typedef void* gpointer;
392 typedef const void *gconstpointer;
393
394 #if (SIZEOF_CHAR == 1)
395 typedef signed char     gint8;
396 typedef unsigned char   guint8;
397 #endif /* SIZEOF_CHAR */
398
399
400 #if (SIZEOF_SHORT == 2)
401 typedef signed short    gint16;
402 typedef unsigned short  guint16;
403 #endif /* SIZEOF_SHORT */
404
405
406 #if (SIZEOF_INT == 4)
407 typedef signed int      gint32;
408 typedef unsigned int    guint32;
409 #elif (SIZEOF_LONG == 4)
410 typedef signed long     gint32;
411 typedef unsigned long   guint32;
412 #endif /* SIZEOF_INT */
413
414 /* Define macros for storing integers inside pointers */
415
416 #if (SIZEOF_INT == SIZEOF_VOID_P)
417
418 #define GPOINTER_TO_INT(p) ((gint)(p))
419 #define GPOINTER_TO_UINT(p) ((guint)(p))
420
421 #define GINT_TO_POINTER(i) ((gpointer)(i))
422 #define GUINT_TO_POINTER(u) ((gpointer)(u))
423
424 #elif (SIZEOF_LONG == SIZEOF_VOID_P)
425
426 #define GPOINTER_TO_INT(p) ((gint)(glong)(p))
427 #define GPOINTER_TO_UINT(p) ((guint)(gulong)(p))
428
429 #define GINT_TO_POINTER(i) ((gpointer)(glong)(i))
430 #define GUINT_TO_POINTER(u) ((gpointer)(gulong)(u))
431
432 #else
433 /* This should never happen */
434 #endif
435
436
437 typedef struct _GList           GList;
438 typedef struct _GSList          GSList;
439 typedef struct _GHashTable      GHashTable;
440 typedef struct _GCache          GCache;
441 typedef struct _GTree           GTree;
442 typedef struct _GTimer          GTimer;
443 typedef struct _GMemChunk       GMemChunk;
444 typedef struct _GListAllocator  GListAllocator;
445 typedef struct _GStringChunk    GStringChunk;
446 typedef struct _GString         GString;
447 typedef struct _GArray          GArray;
448 typedef struct _GDebugKey       GDebugKey;
449 typedef struct _GScannerConfig  GScannerConfig;
450 typedef struct _GScanner        GScanner;
451 typedef union  _GValue          GValue;
452
453
454 typedef void            (*GFunc)                (gpointer  data,
455                                                  gpointer  user_data);
456 typedef void            (*GHFunc)               (gpointer  key,
457                                                  gpointer  value,
458                                                  gpointer  user_data);
459 typedef gpointer        (*GCacheNewFunc)        (gpointer  key);
460 typedef gpointer        (*GCacheDupFunc)        (gpointer  value);
461 typedef void            (*GCacheDestroyFunc)    (gpointer  value);
462 typedef gint            (*GTraverseFunc)        (gpointer  key,
463                                                  gpointer  value,
464                                                  gpointer  data);
465 typedef gint            (*GSearchFunc)          (gpointer  key,
466                                                  gpointer  data);
467 typedef void            (*GErrorFunc)           (gchar    *str);
468 typedef void            (*GWarningFunc)         (gchar    *str);
469 typedef void            (*GPrintFunc)           (gchar    *str);
470 typedef void            (*GScannerMsgFunc)      (GScanner *scanner,
471                                                  gchar    *message,
472                                                  gint      error);
473 typedef void            (*GDestroyNotify)       (gpointer  data);
474
475 typedef guint           (*GHashFunc)            (gconstpointer    key);
476 typedef gint            (*GCompareFunc)         (gconstpointer    a,
477                                                  gconstpointer    b);
478
479 struct _GList
480 {
481   gpointer data;
482   GList *next;
483   GList *prev;
484 };
485
486 struct _GSList
487 {
488   gpointer data;
489   GSList *next;
490 };
491
492 struct _GString
493 {
494   gchar *str;
495   gint len;
496 };
497
498 struct _GArray
499 {
500   gchar *data;
501   guint len;
502 };
503
504 struct _GDebugKey
505 {
506   gchar *key;
507   guint  value;
508 };
509
510 struct _GHashTable { gint dummy; };
511 struct _GCache { gint dummy; };
512 struct _GTree { gint dummy; };
513 struct _GTimer { gint dummy; };
514 struct _GMemChunk { gint dummy; };
515 struct _GListAllocator { gint dummy; };
516 struct _GStringChunk { gint dummy; };
517
518 typedef enum
519 {
520   G_IN_ORDER,
521   G_PRE_ORDER,
522   G_POST_ORDER
523 } GTraverseType;
524
525 /* Doubly linked lists
526  */
527 GList* g_list_alloc             (void);
528 void   g_list_free              (GList          *list);
529 void   g_list_free_1            (GList          *list);
530 GList* g_list_append            (GList          *list,
531                                  gpointer        data);
532 GList* g_list_prepend           (GList          *list,
533                                  gpointer        data);
534 GList* g_list_insert            (GList          *list,
535                                  gpointer        data,
536                                  gint            position);
537 GList* g_list_insert_sorted     (GList          *list,
538                                  gpointer        data,
539                                  GCompareFunc    func);                     
540 GList* g_list_concat            (GList          *list1, 
541                                  GList          *list2);
542 GList* g_list_remove            (GList          *list,
543                                  gpointer        data);
544 GList* g_list_remove_link       (GList          *list,
545                                  GList          *link);
546 GList* g_list_reverse           (GList          *list);
547 GList* g_list_nth               (GList          *list,
548                                  guint           n);
549 GList* g_list_find              (GList          *list,
550                                  gpointer        data);
551 GList* g_list_find_custom       (GList          *list,
552                                  gpointer        data,
553                                  GCompareFunc    func);
554 gint   g_list_position          (GList          *list,
555                                  GList          *link);
556 gint   g_list_index             (GList          *list,
557                                  gpointer        data);
558 GList* g_list_last              (GList          *list);
559 GList* g_list_first             (GList          *list);
560 guint  g_list_length            (GList          *list);
561 void   g_list_foreach           (GList          *list,
562                                  GFunc           func,
563                                  gpointer        user_data);
564 gpointer g_list_nth_data        (GList          *list,
565                                  guint           n);
566
567 #define g_list_previous(list)   ((list) ? (((GList *)(list))->prev) : NULL)
568 #define g_list_next(list)       ((list) ? (((GList *)(list))->next) : NULL)
569
570 /* Singly linked lists
571  */
572 GSList* g_slist_alloc           (void);
573 void    g_slist_free            (GSList         *list);
574 void    g_slist_free_1          (GSList         *list);
575 GSList* g_slist_append          (GSList         *list,
576                                  gpointer        data);
577 GSList* g_slist_prepend         (GSList         *list,
578                                  gpointer        data);
579 GSList* g_slist_insert          (GSList         *list,
580                                  gpointer        data,
581                                  gint            position);
582 GSList* g_slist_insert_sorted   (GSList         *list,
583                                  gpointer        data,
584                                  GCompareFunc    func);                     
585 GSList* g_slist_concat          (GSList         *list1, 
586                                  GSList         *list2);
587 GSList* g_slist_remove          (GSList         *list,
588                                  gpointer        data);
589 GSList* g_slist_remove_link     (GSList         *list,
590                                  GSList         *link);
591 GSList* g_slist_reverse         (GSList         *list);
592 GSList* g_slist_nth             (GSList         *list,
593                                  guint           n);
594 GSList* g_slist_find            (GSList         *list,
595                                  gpointer        data);
596 GSList* g_slist_find_custom     (GSList         *list,
597                                  gpointer        data,
598                                  GCompareFunc    func);
599 gint    g_slist_position        (GSList         *list,
600                                  GSList         *link);
601 gint    g_slist_index           (GSList         *list,
602                                  gpointer        data);
603 GSList* g_slist_last            (GSList         *list);
604 guint   g_slist_length          (GSList         *list);
605 void    g_slist_foreach         (GSList         *list,
606                                  GFunc           func,
607                                  gpointer        user_data);
608 gpointer g_slist_nth_data       (GSList         *list,
609                                  guint           n);
610
611 #define g_slist_next(slist)     ((slist) ? (((GSList *)(slist))->next) : NULL)
612
613 /* List Allocators
614  */
615 GListAllocator* g_list_allocator_new  (void);
616 void            g_list_allocator_free (GListAllocator* allocator);
617 GListAllocator* g_slist_set_allocator (GListAllocator* allocator);
618 GListAllocator* g_list_set_allocator  (GListAllocator* allocator);
619
620
621 /* Hash tables
622  */
623 GHashTable* g_hash_table_new     (GHashFunc       hash_func,
624                                   GCompareFunc    key_compare_func);
625 void        g_hash_table_destroy (GHashTable     *hash_table);
626 void        g_hash_table_insert  (GHashTable     *hash_table,
627                                   gpointer        key,
628                                   gpointer        value);
629 void        g_hash_table_remove  (GHashTable     *hash_table,
630                                   gconstpointer   key);
631 gpointer    g_hash_table_lookup  (GHashTable     *hash_table,
632                                   gconstpointer    key);
633 void        g_hash_table_freeze  (GHashTable     *hash_table);
634 void        g_hash_table_thaw    (GHashTable     *hash_table);
635 void        g_hash_table_foreach (GHashTable     *hash_table,
636                                   GHFunc          func,
637                                   gpointer        user_data);
638
639
640 /* Caches
641  */
642 GCache*  g_cache_new           (GCacheNewFunc      value_new_func,
643                                 GCacheDestroyFunc  value_destroy_func,
644                                 GCacheDupFunc      key_dup_func,
645                                 GCacheDestroyFunc  key_destroy_func,
646                                 GHashFunc          hash_key_func,
647                                 GHashFunc          hash_value_func,
648                                 GCompareFunc       key_compare_func);
649 void     g_cache_destroy       (GCache            *cache);
650 gpointer g_cache_insert        (GCache            *cache,
651                                 gpointer           key);
652 void     g_cache_remove        (GCache            *cache,
653                                 gpointer           value);
654 void     g_cache_key_foreach   (GCache            *cache,
655                                 GHFunc             func,
656                                 gpointer           user_data);
657 void     g_cache_value_foreach (GCache            *cache,
658                                 GHFunc             func,
659                                 gpointer           user_data);
660
661
662 /* Trees
663  */
664 GTree*   g_tree_new      (GCompareFunc   key_compare_func);
665 void     g_tree_destroy  (GTree         *tree);
666 void     g_tree_insert   (GTree         *tree,
667                           gpointer       key,
668                           gpointer       value);
669 void     g_tree_remove   (GTree         *tree,
670                           gpointer       key);
671 gpointer g_tree_lookup   (GTree         *tree,
672                           gpointer       key);
673 void     g_tree_traverse (GTree         *tree,
674                           GTraverseFunc  traverse_func,
675                           GTraverseType  traverse_type,
676                           gpointer       data);
677 gpointer g_tree_search   (GTree         *tree,
678                           GSearchFunc    search_func,
679                           gpointer       data);
680 gint     g_tree_height   (GTree         *tree);
681 gint     g_tree_nnodes   (GTree         *tree);
682
683
684 /* Memory
685  */
686
687 #ifdef USE_DMALLOC
688
689 #define g_malloc(size)       (gpointer) MALLOC(size)
690 #define g_malloc0(size)      (gpointer) CALLOC(char,size)
691 #define g_realloc(mem,size)  (gpointer) REALLOC(mem,char,size)
692 #define g_free(mem)          FREE(mem)
693
694 #else /* USE_DMALLOC */
695
696 gpointer g_malloc      (gulong    size);
697 gpointer g_malloc0     (gulong    size);
698 gpointer g_realloc     (gpointer  mem,
699                         gulong    size);
700 void     g_free        (gpointer  mem);
701
702 #endif /* USE_DMALLOC */
703
704 void     g_mem_profile (void);
705 void     g_mem_check   (gpointer  mem);
706
707
708 /* "g_mem_chunk_new" creates a new memory chunk.
709  * Memory chunks are used to allocate pieces of memory which are
710  *  always the same size. Lists are a good example of such a data type.
711  * The memory chunk allocates and frees blocks of memory as needed.
712  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
713  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
714  *  fault...somewhere).
715  *
716  * Oh yeah, GMemChunk is an opaque data type. (You don't really
717  *  want to know what's going on inside do you?)
718  */
719
720 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
721  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
722  *  atom. (They are also useful for lists which use MemChunk to allocate
723  *  memory but are also part of the MemChunk implementation).
724  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
725  */
726
727 #define G_ALLOC_ONLY      1
728 #define G_ALLOC_AND_FREE  2
729
730 GMemChunk* g_mem_chunk_new     (gchar     *name,
731                                 gint       atom_size,
732                                 gulong     area_size,
733                                 gint       type);
734 void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
735 gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
736 void       g_mem_chunk_free    (GMemChunk *mem_chunk,
737                                 gpointer   mem);
738 void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
739 void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
740 void       g_mem_chunk_print   (GMemChunk *mem_chunk);
741 void       g_mem_chunk_info    (void);
742
743 /* Ah yes...we have a "g_blow_chunks" function.
744  * "g_blow_chunks" simply compresses all the chunks. This operation
745  *  consists of freeing every memory area that should be freed (but
746  *  which we haven't gotten around to doing yet). And, no,
747  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
748  *  much better name than "g_mem_chunk_clean_all" or something
749  *  similar.
750  */
751 void g_blow_chunks (void);
752
753
754 /* Timer
755  */
756 GTimer* g_timer_new     (void);
757 void    g_timer_destroy (GTimer  *timer);
758 void    g_timer_start   (GTimer  *timer);
759 void    g_timer_stop    (GTimer  *timer);
760 void    g_timer_reset   (GTimer  *timer);
761 gdouble g_timer_elapsed (GTimer  *timer,
762                          gulong  *microseconds);
763
764
765 /* Output
766  */
767 void g_error   (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
768 void g_warning (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
769 void g_message (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
770 void g_print   (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
771
772 /* Utility functions
773  */
774 #define G_STR_DELIMITERS     "_-|> <."
775 void    g_strdelimit            (gchar       *string,
776                                  const gchar *delimiters,
777                                  gchar        new_delimiter);
778 gchar*  g_strdup                (const gchar *str);
779 gchar*  g_strconcat             (const gchar *string1,
780                                  ...); /* NULL terminated */
781 gdouble g_strtod                (const gchar *nptr,
782                                  gchar      **endptr);
783 gchar*  g_strerror              (gint         errnum);
784 gchar*  g_strsignal             (gint         signum);
785 gint    g_strcasecmp            (const gchar *s1,
786                                  const gchar *s2);
787 void    g_strdown               (gchar       *string);
788 void    g_strup                 (gchar       *string);
789 void    g_strreverse            (gchar       *string);
790 guint   g_parse_debug_string    (const gchar *string, 
791                                  GDebugKey   *keys, 
792                                  guint        nkeys);
793 gint    g_snprintf              (gchar       *string,
794                                  gulong       n,
795                                  gchar const *format,
796                                  ...) G_GNUC_PRINTF (3, 4);
797
798 /* We make the assumption that if memmove isn't available, then
799  * bcopy will do the job. This isn't safe everywhere. (bcopy can't
800  * necessarily handle overlapping copies) */
801 #ifdef HAVE_MEMMOVE
802 #define g_memmove memmove
803 #else 
804 #define g_memmove(a,b,c) bcopy(b,a,c)
805 #endif
806
807 /* Errors
808  */
809 GErrorFunc   g_set_error_handler   (GErrorFunc   func);
810 GWarningFunc g_set_warning_handler (GWarningFunc func);
811 GPrintFunc   g_set_message_handler (GPrintFunc func);
812 GPrintFunc   g_set_print_handler   (GPrintFunc func);
813
814 void g_debug          (const gchar *progname);
815 void g_attach_process (const gchar *progname,
816                        gint         query);
817 void g_stack_trace    (const gchar *progname,
818                        gint         query);
819
820
821 /* String Chunks
822  */
823 GStringChunk* g_string_chunk_new           (gint size);
824 void          g_string_chunk_free          (GStringChunk *chunk);
825 gchar*        g_string_chunk_insert        (GStringChunk *chunk,
826                                             const gchar  *string);
827 gchar*        g_string_chunk_insert_const  (GStringChunk *chunk,
828                                             const gchar  *string);
829
830 /* Strings
831  */
832 GString* g_string_new       (const gchar *init);
833 GString* g_string_sized_new (guint        dfl_size);
834 void     g_string_free      (GString     *string,
835                              gint         free_segment);
836 GString* g_string_assign    (GString     *lval,
837                              const gchar *rval);
838 GString* g_string_truncate  (GString     *string,
839                              gint         len);
840 GString* g_string_append    (GString     *string,
841                              const gchar *val);
842 GString* g_string_append_c  (GString     *string,
843                              gchar        c);
844 GString* g_string_prepend   (GString     *string,
845                              const gchar *val);
846 GString* g_string_prepend_c (GString     *string,
847                              gchar        c);
848 GString* g_string_insert    (GString     *string, 
849                              gint         pos, 
850                              const gchar *val);
851 GString* g_string_insert_c  (GString     *string, 
852                              gint         pos, 
853                              gchar        c);
854 GString* g_string_erase     (GString     *string, 
855                              gint         pos, 
856                              gint         len);
857 GString* g_string_down      (GString     *string);
858 GString* g_string_up        (GString     *string);
859 void     g_string_sprintf   (GString     *string,
860                              const gchar *format,
861                              ...) G_GNUC_PRINTF (2, 3);
862 void     g_string_sprintfa  (GString     *string,
863                              const gchar *format,
864                              ...) G_GNUC_PRINTF (2, 3);
865
866 /* Resizable arrays
867  */
868 #define g_array_append_val(array,type,val) \
869      g_rarray_append (array, (gpointer) &val, sizeof (type))
870 #define g_array_append_vals(array,type,vals,nvals) \
871      g_rarray_append (array, (gpointer) vals, sizeof (type) * nvals)
872 #define g_array_prepend_val(array,type,val) \
873      g_rarray_prepend (array, (gpointer) &val, sizeof (type))
874 #define g_array_prepend_vals(array,type,vals,nvals) \
875      g_rarray_prepend (array, (gpointer) vals, sizeof (type) * nvals)
876 #define g_array_truncate(array,type,length) \
877      g_rarray_truncate (array, length, sizeof (type))
878 #define g_array_index(array,type,index) \
879      ((type*) array->data)[index]
880
881 GArray* g_array_new       (gint      zero_terminated);
882 void    g_array_free      (GArray   *array,
883                            gint      free_segment);
884 GArray* g_rarray_append   (GArray   *array,
885                            gpointer  data,
886                            gint      size);
887 GArray* g_rarray_prepend  (GArray   *array,
888                            gpointer  data,
889                            gint      size);
890 GArray* g_rarray_truncate (GArray   *array,
891                            gint      length,
892                            gint      size);
893
894 /* Hash Functions
895  */
896 gint  g_str_equal (gconstpointer   v,
897                    gconstpointer   v2);
898 guint g_str_hash  (gconstpointer v);
899
900 /* This "hash" function will just return the key's adress as an
901  * unsigned integer. Useful for hashing on plain adresses or
902  * simple integer values.
903  */
904 guint g_direct_hash (gconstpointer key);
905
906
907 /* Location Associated Data
908  */
909 void      g_dataset_destroy             (gconstpointer   dataset_location);
910 guint     g_dataset_try_key             (const gchar    *key);
911 guint     g_dataset_force_id            (const gchar    *key);
912 gchar*    g_dataset_retrive_key         (guint           key_id);
913 gpointer  g_dataset_id_get_data         (gconstpointer   dataset_location,
914                                          guint           key_id);
915 void      g_dataset_id_set_data_full    (gconstpointer   dataset_location,
916                                          guint           key_id,
917                                          gpointer        data,
918                                          GDestroyNotify  destroy_func);
919 void      g_dataset_id_set_destroy      (gconstpointer   dataset_location,
920                                          guint           key_id,
921                                          GDestroyNotify  destroy_func);
922
923 #define   g_dataset_id_set_data(l,k,d)  G_STMT_START{g_dataset_id_set_data_full((l),(k),(d),NULL);}G_STMT_END
924 #define   g_dataset_id_remove_data(l,k) G_STMT_START{g_dataset_id_set_data((l),(k),NULL);}G_STMT_END
925 #define   g_dataset_get_data(l,k)       (g_dataset_id_get_data((l),g_dataset_try_key(k)))
926 #define   g_dataset_set_data_full(l,k,d,f) G_STMT_START{g_dataset_id_set_data_full((l),g_dataset_force_id(k),(d),(f));}G_STMT_END
927 #define   g_dataset_set_destroy(l,k,f)  G_STMT_START{g_dataset_id_set_destroy((l),g_dataset_force_id(k),(f));}G_STMT_END
928 #define   g_dataset_set_data(l,k,d)     G_STMT_START{g_dataset_set_data_full((l),(k),(d),NULL);}G_STMT_END
929 #define   g_dataset_remove_data(l,k)    G_STMT_START{g_dataset_set_data((l),(k),NULL);}G_STMT_END
930
931
932 /* GScanner: Flexible lexical scanner for general purpose.
933  */
934
935 /* Character sets */
936 #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
937 #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
938 #define G_CSET_LATINC   "\300\301\302\303\304\305\306"\
939                         "\307\310\311\312\313\314\315\316\317\320"\
940                         "\321\322\323\324\325\326"\
941                         "\330\331\332\333\334\335\336"
942 #define G_CSET_LATINS   "\337\340\341\342\343\344\345\346"\
943                         "\347\350\351\352\353\354\355\356\357\360"\
944                         "\361\362\363\364\365\366"\
945                         "\370\371\372\373\374\375\376\377"
946
947 /* Error types */
948 typedef enum
949 {
950   G_ERR_UNKNOWN,
951   G_ERR_UNEXP_EOF,
952   G_ERR_UNEXP_EOF_IN_STRING,
953   G_ERR_UNEXP_EOF_IN_COMMENT,
954   G_ERR_NON_DIGIT_IN_CONST,
955   G_ERR_DIGIT_RADIX,
956   G_ERR_FLOAT_RADIX,
957   G_ERR_FLOAT_MALFORMED
958 } GErrorType;
959
960 /* Token types */
961 typedef enum
962 {
963   G_TOKEN_EOF                   =   0,
964   
965   G_TOKEN_LEFT_PAREN            = '(',
966   G_TOKEN_RIGHT_PAREN           = ')',
967   G_TOKEN_LEFT_CURLY            = '{',
968   G_TOKEN_RIGHT_CURLY           = '}',
969   G_TOKEN_LEFT_BRACE            = '[',
970   G_TOKEN_RIGHT_BRACE           = ']',
971   G_TOKEN_EQUAL_SIGN            = '=',
972   G_TOKEN_COMMA                 = ',',
973   
974   G_TOKEN_NONE                  = 256,
975   
976   G_TOKEN_ERROR,
977   
978   G_TOKEN_CHAR,
979   G_TOKEN_BINARY,
980   G_TOKEN_OCTAL,
981   G_TOKEN_INT,
982   G_TOKEN_HEX,
983   G_TOKEN_FLOAT,
984   G_TOKEN_STRING,
985   
986   G_TOKEN_SYMBOL,
987   G_TOKEN_IDENTIFIER,
988   G_TOKEN_IDENTIFIER_NULL,
989   
990   G_TOKEN_COMMENT_SINGLE,
991   G_TOKEN_COMMENT_MULTI,
992   G_TOKEN_LAST
993 } GTokenType;
994
995 union   _GValue
996 {
997   gpointer      v_symbol;
998   gchar         *v_identifier;
999   gulong        v_binary;
1000   gulong        v_octal;
1001   gulong        v_int;
1002   gdouble       v_float;
1003   gulong        v_hex;
1004   gchar         *v_string;
1005   gchar         *v_comment;
1006   guchar        v_char;
1007   guint         v_error;
1008 };
1009
1010 struct  _GScannerConfig
1011 {
1012   /* Character sets
1013    */
1014   gchar         *cset_skip_characters;          /* default: " \t\n" */
1015   gchar         *cset_identifier_first;
1016   gchar         *cset_identifier_nth;
1017   gchar         *cpair_comment_single;          /* default: "#\n" */
1018   
1019   /* Should symbol lookup work case sensitive?
1020    */
1021   guint         case_sensitive : 1;
1022   
1023   /* Boolean values to be adjusted "on the fly"
1024    * to configure scanning behaviour.
1025    */
1026   guint         skip_comment_multi : 1;         /* C like comment */
1027   guint         skip_comment_single : 1;        /* single line comment */
1028   guint         scan_comment_multi : 1;         /* scan multi line comments? */
1029   guint         scan_identifier : 1;
1030   guint         scan_identifier_1char : 1;
1031   guint         scan_identifier_NULL : 1;
1032   guint         scan_symbols : 1;
1033   guint         scan_binary : 1;
1034   guint         scan_octal : 1;
1035   guint         scan_float : 1;
1036   guint         scan_hex : 1;                   /* `0x0ff0' */
1037   guint         scan_hex_dollar : 1;            /* `$0ff0' */
1038   guint         scan_string_sq : 1;             /* string: 'anything' */
1039   guint         scan_string_dq : 1;             /* string: "\\-escapes!\n" */
1040   guint         numbers_2_int : 1;              /* bin, octal, hex => int */
1041   guint         int_2_float : 1;                /* int => G_TOKEN_FLOAT? */
1042   guint         identifier_2_string : 1;
1043   guint         char_2_token : 1;               /* return G_TOKEN_CHAR? */
1044   guint         symbol_2_token : 1;
1045 };
1046
1047 struct  _GScanner
1048 {
1049   /* unused portions */
1050   gpointer              user_data;
1051   const gchar           *input_name;
1052   guint                 parse_errors;
1053   guint                 max_parse_errors;
1054   
1055   /* maintained/used by the g_scanner_*() functions */
1056   GScannerConfig        *config;
1057   GTokenType            token;
1058   GValue                value;
1059   guint                 line;
1060   guint                 position;
1061   
1062   /* to be considered private */
1063   GTokenType            next_token;
1064   GValue                next_value;
1065   guint                 next_line;
1066   guint                 next_position;
1067   GHashTable            *symbol_table;
1068   const gchar           *text;
1069   guint                 text_len;
1070   gint                  input_fd;
1071   gint                  peeked_char;
1072
1073   GScannerMsgFunc       msg_handler;
1074 };
1075
1076 GScanner*       g_scanner_new                   (GScannerConfig *config_templ);
1077 void            g_scanner_destroy               (GScanner       *scanner);
1078 void            g_scanner_input_file            (GScanner       *scanner,
1079                                                  gint           input_fd);
1080 void            g_scanner_input_text            (GScanner       *scanner,
1081                                                  const  gchar   *text,
1082                                                  guint          text_len);
1083 GTokenType      g_scanner_get_next_token        (GScanner       *scanner);
1084 GTokenType      g_scanner_peek_next_token       (GScanner       *scanner);
1085 GTokenType      g_scanner_cur_token             (GScanner       *scanner);
1086 GValue          g_scanner_cur_value             (GScanner       *scanner);
1087 guint           g_scanner_cur_line              (GScanner       *scanner);
1088 guint           g_scanner_cur_position          (GScanner       *scanner);
1089 gboolean        g_scanner_eof                   (GScanner       *scanner);
1090 void            g_scanner_add_symbol            (GScanner       *scanner,
1091                                                  const gchar    *symbol,
1092                                                  gpointer       value);
1093 gpointer        g_scanner_lookup_symbol         (GScanner       *scanner,
1094                                                  const gchar    *symbol);
1095 void            g_scanner_foreach_symbol        (GScanner       *scanner,
1096                                                  GHFunc          func,
1097                                                  gpointer        func_data);
1098 void            g_scanner_remove_symbol         (GScanner       *scanner,
1099                                                  const gchar    *symbol);
1100 void            g_scanner_freeze_symbol_table   (GScanner       *scanner);
1101 void            g_scanner_thaw_symbol_table     (GScanner       *scanner);
1102 void            g_scanner_unexp_token           (GScanner       *scanner,
1103                                                  GTokenType     expected_token,
1104                                                  const gchar    *identifier_spec,
1105                                                  const gchar    *symbol_spec,
1106                                                  const gchar    *symbol_name,
1107                                                  const gchar    *message,
1108                                                  gint            is_error);
1109 void            g_scanner_error                 (GScanner       *scanner,
1110                                                  const gchar    *format,
1111                                                  ...) G_GNUC_PRINTF (2,3);
1112 void            g_scanner_warn                  (GScanner       *scanner,
1113                                                  const gchar    *format,
1114                                                  ...) G_GNUC_PRINTF (2,3);
1115 gint            g_scanner_stat_mode             (const gchar    *filename);
1116
1117
1118 /* Completion */
1119
1120 typedef gchar* (*GCompletionFunc)(gpointer);
1121
1122 typedef struct _GCompletion GCompletion;
1123
1124 struct _GCompletion {
1125         GList* items;
1126         GCompletionFunc func;
1127
1128         gchar* prefix;
1129         GList* cache;
1130
1131 };
1132
1133 GCompletion* g_completion_new          (GCompletionFunc func);
1134 void         g_completion_add_items    (GCompletion* cmp, 
1135                                         GList* items);
1136 void         g_completion_remove_items (GCompletion* cmp, 
1137                                         GList* items);
1138 void         g_completion_clear_items  (GCompletion* cmp);
1139 GList*       g_completion_complete     (GCompletion* cmp, 
1140                                         gchar* prefix,
1141                                         gchar** new_prefix);
1142 void         g_completion_free         (GCompletion* cmp);
1143
1144 /* Glib version.
1145  */
1146 extern const guint glib_major_version;
1147 extern const guint glib_minor_version;
1148 extern const guint glib_micro_version;
1149
1150 #ifdef __cplusplus
1151 }
1152 #endif /* __cplusplus */
1153
1154
1155 #endif /* __G_LIB_H__ */