1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
22 /* system specific config file
24 #include <glibconfig.h>
26 /* support standard arg inline functions for assertment macros
30 /* optionally feature DMALLOC memory allocation debugger
37 /* glib provides definitions for the extrema of many
38 * of the standard types. These are:
55 #define G_MINFLOAT FLT_MIN
56 #define G_MAXFLOAT FLT_MAX
57 #define G_MINDOUBLE DBL_MIN
58 #define G_MAXDOUBLE DBL_MAX
64 #define G_MINFLOAT MINFLOAT
65 #define G_MAXFLOAT MAXFLOAT
66 #define G_MINDOUBLE MINDOUBLE
67 #define G_MAXDOUBLE MAXDOUBLE
69 #endif /* HAVE_VALUES_H */
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
86 #endif /* HAVE_FLOAT_H */
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
95 #endif /* HAVE_VALUES_H */
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.
104 #define NULL ((void*) 0)
112 #define TRUE (!FALSE)
116 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
119 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
122 #define ABS(a) (((a) < 0) ? -(a) : (a))
125 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
128 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
129 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
131 #if !defined (G_VA_COPY)
132 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
133 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
134 # elif defined (G_VA_COPY_AS_ARRAY)
135 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
136 # else /* va_list is a pointer */
137 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
138 # endif /* va_list is a pointer */
139 #endif /* !G_VA_COPY */
142 /* Provide simple enum value macro wrappers that ease automated
143 * enum value stringification code. [abandoned]
145 #if !defined (G_CODE_GENERATION)
146 #define G_ENUM( EnumerationName ) EnumerationName
147 #define G_FLAGS( EnumerationName ) EnumerationName
148 #define G_NV( VALUE_NAME , value_nick, VALUE) VALUE_NAME = (VALUE)
149 #define G_SV( VALUE_NAME, value_nick ) VALUE_NAME
150 #else /* G_CODE_GENERATION */
151 #define G_ENUM( EnumerationName ) G_ENUM_E + EnumerationName +
152 #define G_FLAGS( EnumerationName ) G_ENUM_F + EnumerationName +
153 #define G_NV( VALUE_NAME , value_nick, VALUE) G_ENUM_V + VALUE_NAME + value_nick +
154 #define G_SV( VALUE_NAME, value_nick ) G_ENUM_V + VALUE_NAME + value_nick +
155 #endif /* G_CODE_GENERATION */
158 /* Provide simple macro statement wrappers (adapted from Perl):
159 * G_STMT_START { statements; } G_STMT_END;
160 * can be used as a single statement, as in
161 * if (x) G_STMT_START { ... } G_STMT_END; else ...
163 * For gcc we will wrap the statements within `({' and `})' braces.
164 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
165 * and otherwise within `do' and `while (0)'.
167 #if !(defined (G_STMT_START) && defined (G_STMT_END))
168 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
169 # define G_STMT_START (void)(
170 # define G_STMT_END )
172 # if (defined (sun) || defined (__sun__))
173 # define G_STMT_START if (1)
174 # define G_STMT_END else (void)0
176 # define G_STMT_START do
177 # define G_STMT_END while (0)
183 /* ANSI does not permit the keyword `inline'.
185 #if defined (__STRICT_ANSI__)
188 # define inline __inline__
189 # else /* !__GNUC__ */
190 # define inline /* don't inline */
191 # endif /* !__GNUC__ */
192 #endif /* __STRICT_ANSI__ */
195 /* Provide macros to feature the GCC function attribute.
197 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
198 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
199 __attribute__((format (printf, format_idx, arg_idx)))
200 #define G_GNUC_SCANF( format_idx, arg_idx ) \
201 __attribute__((format (scanf, format_idx, arg_idx)))
202 #define G_GNUC_FORMAT( arg_idx ) \
203 __attribute__((format_arg (arg_idx)))
204 #define G_GNUC_NORETURN \
205 __attribute__((noreturn))
206 #define G_GNUC_CONST \
207 __attribute__((const))
208 #else /* !__GNUC__ */
209 #define G_GNUC_PRINTF( format_idx, arg_idx )
210 #define G_GNUC_SCANF( format_idx, arg_idx )
211 #define G_GNUC_FORMAT( arg_idx )
212 #define G_GNUC_NORETURN
214 #endif /* !__GNUC__ */
217 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
218 * macros, so we can refer to them as strings unconditionally.
221 #define G_GNUC_FUNCTION (__FUNCTION__)
222 #define G_GNUC_PRETTY_FUNCTION (__PRETTY_FUNCTION__)
223 #else /* !__GNUC__ */
224 #define G_GNUC_FUNCTION ("")
225 #define G_GNUC_PRETTY_FUNCTION ("")
226 #endif /* !__GNUC__ */
229 /* Hacker macro to place breakpoints for x86 machines.
230 * Actual use is strongly deprecated of course ;)
232 #if defined (__i386__)
233 #define G_BREAKPOINT() G_STMT_START{ __asm__ ("int $03"); }G_STMT_END
234 #else /* !__i386__ */
235 #define G_BREAKPOINT()
236 #endif /* __i386__ */
241 # define ATEXIT(proc) (atexit (proc))
242 # elif defined (HAVE_ON_EXIT)
243 # define ATEXIT(proc) (on_exit ((void (*)(int, void *))(proc), NULL))
248 /* Provide macros for easily allocating memory. The macros
249 * will cast the allocated memory to the specified type
250 * in order to avoid compiler warnings. (Makes the code neater).
255 #define g_new(type, count) (ALLOC (type, count))
256 #define g_new0(type, count) (CALLOC (type, count))
258 #else /* __DMALLOC_H__ */
260 #define g_new(type, count) \
261 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
262 #define g_new0(type, count) \
263 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
264 #endif /* __DMALLOC_H__ */
266 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
267 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
269 sizeof (type) * (pre_alloc), \
272 #define g_chunk_new(type, chunk) ( \
273 (type *) g_mem_chunk_alloc (chunk) \
275 #define g_chunk_new0(type, chunk) ( \
276 (type *) memset (g_mem_chunk_alloc (chunk), 0, sizeof (type)) \
278 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
279 g_mem_chunk_free ((mem_chunk), (mem)); \
283 #define g_string(x) #x
286 /* Provide macros for error handling. The "assert" macros will
287 * exit on failure. The "return" macros will exit the current
288 * function. Two different definitions are given for the macros
289 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
290 * __PRETTY_FUNCTION__ capability.
293 #ifdef G_DISABLE_ASSERT
295 #define g_assert(expr)
296 #define g_assert_not_reached()
298 #else /* !G_DISABLE_ASSERT */
302 #define g_assert(expr) G_STMT_START{ \
304 g_log (G_LOG_DOMAIN, \
306 "file %s: line %d (%s): assertion failed: (%s)", \
309 __PRETTY_FUNCTION__, \
312 #define g_assert_not_reached() G_STMT_START{ \
313 g_log (G_LOG_DOMAIN, \
315 "file %s: line %d (%s): should not be reached", \
318 __PRETTY_FUNCTION__); }G_STMT_END
320 #else /* !__GNUC__ */
322 #define g_assert(expr) G_STMT_START{ \
324 g_log (G_LOG_DOMAIN, \
326 "file %s: line %d: assertion failed: (%s)", \
331 #define g_assert_not_reached() G_STMT_START{ \
332 g_log (G_LOG_DOMAIN, \
334 "file %s: line %d: should not be reached", \
336 __LINE__); }G_STMT_END
338 #endif /* __GNUC__ */
340 #endif /* !G_DISABLE_ASSERT */
343 #ifdef G_DISABLE_CHECKS
345 #define g_return_if_fail(expr)
346 #define g_return_val_if_fail(expr,val)
348 #else /* !G_DISABLE_CHECKS */
352 #define g_return_if_fail(expr) G_STMT_START{ \
355 g_log (G_LOG_DOMAIN, \
356 G_LOG_LEVEL_CRITICAL, \
357 "file %s: line %d (%s): assertion `%s' failed.", \
360 __PRETTY_FUNCTION__, \
365 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
368 g_log (G_LOG_DOMAIN, \
369 G_LOG_LEVEL_CRITICAL, \
370 "file %s: line %d (%s): assertion `%s' failed.", \
373 __PRETTY_FUNCTION__, \
378 #else /* !__GNUC__ */
380 #define g_return_if_fail(expr) G_STMT_START{ \
383 g_log (G_LOG_DOMAIN, \
384 G_LOG_LEVEL_CRITICAL, \
385 "file %s: line %d: assertion `%s' failed.", \
392 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
395 g_log (G_LOG_DOMAIN, \
396 G_LOG_LEVEL_CRITICAL, \
397 "file %s: line %d: assertion `%s' failed.", \
404 #endif /* !__GNUC__ */
406 #endif /* !G_DISABLE_CHECKS */
410 /* the #pragma } statment is used to fix up emacs' c-mode which gets
411 * confused by extern "C" {. the ansi standard says that compilers
412 * have to ignore #pragma directives that they don't know about,
413 * so we should be save in using this.
417 #endif /* __cplusplus */
420 /* Provide type definitions for commonly used types.
421 * These are useful because a "gint8" can be adjusted
422 * to be 1 byte (8 bits) on all platforms. Similarly and
423 * more importantly, "gint32" can be adjusted to be
424 * 4 bytes (32 bits) on all platforms.
428 typedef short gshort;
431 typedef gint gboolean;
433 typedef unsigned char guchar;
434 typedef unsigned short gushort;
435 typedef unsigned long gulong;
436 typedef unsigned int guint;
438 typedef float gfloat;
439 typedef double gdouble;
441 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
442 * Since gldouble isn't used anywhere, just disable it for now
445 #ifdef HAVE_LONG_DOUBLE
446 typedef long double gldouble;
447 #else /* HAVE_LONG_DOUBLE */
448 typedef double gldouble;
449 #endif /* HAVE_LONG_DOUBLE */
452 typedef void* gpointer;
453 typedef const void *gconstpointer;
455 #if (SIZEOF_CHAR == 1)
456 typedef signed char gint8;
457 typedef unsigned char guint8;
458 #endif /* SIZEOF_CHAR */
460 #if (SIZEOF_SHORT == 2)
461 typedef signed short gint16;
462 typedef unsigned short guint16;
463 #endif /* SIZEOF_SHORT */
465 #if (SIZEOF_INT == 4)
466 typedef signed int gint32;
467 typedef unsigned int guint32;
468 #elif (SIZEOF_LONG == 4)
469 typedef signed long gint32;
470 typedef unsigned long guint32;
471 #endif /* SIZEOF_INT */
473 #if (SIZEOF_LONG == 8)
474 #define HAVE_GINT64 1
475 typedef signed long gint64;
476 typedef unsigned long guint64;
477 #elif (SIZEOF_LONG_LONG == 8)
478 #define HAVE_GINT64 1
479 typedef signed long long gint64;
480 typedef unsigned long long guint64;
487 /* Define macros for storing integers inside pointers
489 #if (SIZEOF_INT == SIZEOF_VOID_P)
491 #define GPOINTER_TO_INT(p) ((gint)(p))
492 #define GPOINTER_TO_UINT(p) ((guint)(p))
494 #define GINT_TO_POINTER(i) ((gpointer)(i))
495 #define GUINT_TO_POINTER(u) ((gpointer)(u))
497 #elif (SIZEOF_LONG == SIZEOF_VOID_P)
499 #define GPOINTER_TO_INT(p) ((gint)(glong)(p))
500 #define GPOINTER_TO_UINT(p) ((guint)(gulong)(p))
502 #define GINT_TO_POINTER(i) ((gpointer)(glong)(i))
503 #define GUINT_TO_POINTER(u) ((gpointer)(gulong)(u))
506 #error SIZEOF_VOID_P unknown - This should never happen
509 typedef gint32 gssize;
510 typedef guint32 gsize;
511 typedef guint32 GQuark;
512 typedef gint32 GTime;
517 extern const guint glib_major_version;
518 extern const guint glib_minor_version;
519 extern const guint glib_micro_version;
520 extern const guint glib_interface_age;
521 extern const guint glib_binary_age;
524 /* Forward declarations of glib types.
527 typedef struct _GList GList;
528 typedef struct _GSList GSList;
529 typedef struct _GHashTable GHashTable;
530 typedef struct _GCache GCache;
531 typedef struct _GTree GTree;
532 typedef struct _GTimer GTimer;
533 typedef struct _GMemChunk GMemChunk;
534 typedef struct _GListAllocator GListAllocator;
535 typedef struct _GStringChunk GStringChunk;
536 typedef struct _GString GString;
537 typedef struct _GArray GArray;
538 typedef struct _GPtrArray GPtrArray;
539 typedef struct _GByteArray GByteArray;
540 typedef struct _GDebugKey GDebugKey;
541 typedef struct _GScannerConfig GScannerConfig;
542 typedef struct _GScanner GScanner;
543 typedef union _GValue GValue;
544 typedef struct _GCompletion GCompletion;
545 typedef struct _GRelation GRelation;
546 typedef struct _GTuples GTuples;
547 typedef struct _GNode GNode;
552 G_TRAVERSE_LEAFS = 1 << 0,
553 G_TRAVERSE_NON_LEAFS = 1 << 1,
554 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
555 G_TRAVERSE_MASK = 0x03
566 /* Log level shift offset for user defined
567 * log levels (0-7 are used by GLib).
569 #define G_LOG_LEVEL_USER_SHIFT (8)
571 /* Glib log levels and flags.
576 G_LOG_FLAG_RECURSION = 1 << 0,
577 G_LOG_FLAG_FATAL = 1 << 1,
579 /* GLib log levels */
580 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
581 G_LOG_LEVEL_CRITICAL = 1 << 3,
582 G_LOG_LEVEL_WARNING = 1 << 4,
583 G_LOG_LEVEL_MESSAGE = 1 << 5,
584 G_LOG_LEVEL_INFO = 1 << 6,
585 G_LOG_LEVEL_DEBUG = 1 << 7,
587 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
590 /* GLib log levels that are considered fatal by default */
591 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
594 typedef gpointer (*GCacheNewFunc) (gpointer key);
595 typedef gpointer (*GCacheDupFunc) (gpointer value);
596 typedef void (*GCacheDestroyFunc) (gpointer value);
597 typedef gint (*GCompareFunc) (gconstpointer a,
599 typedef gchar* (*GCompletionFunc) (gpointer);
600 typedef void (*GDestroyNotify) (gpointer data);
601 typedef void (*GFunc) (gpointer data,
603 typedef guint (*GHashFunc) (gconstpointer key);
604 typedef void (*GHFunc) (gpointer key,
607 typedef void (*GLogFunc) (const gchar *log_domain,
608 GLogLevelFlags log_level,
609 const gchar *message,
611 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
613 typedef void (*GNodeForeachFunc) (GNode *node,
615 typedef gint (*GSearchFunc) (gpointer key,
617 typedef void (*GScannerMsgFunc) (GScanner *scanner,
620 typedef gint (*GTraverseFunc) (gpointer key,
673 struct _GCache { gint dummy; };
674 struct _GTree { gint dummy; };
675 struct _GTimer { gint dummy; };
676 struct _GMemChunk { gint dummy; };
677 struct _GListAllocator { gint dummy; };
678 struct _GStringChunk { gint dummy; };
681 /* Doubly linked lists
683 GList* g_list_alloc (void);
684 void g_list_free (GList *list);
685 void g_list_free_1 (GList *list);
686 GList* g_list_append (GList *list,
688 GList* g_list_prepend (GList *list,
690 GList* g_list_insert (GList *list,
693 GList* g_list_insert_sorted (GList *list,
696 GList* g_list_concat (GList *list1,
698 GList* g_list_remove (GList *list,
700 GList* g_list_remove_link (GList *list,
702 GList* g_list_reverse (GList *list);
703 GList* g_list_nth (GList *list,
705 GList* g_list_find (GList *list,
707 GList* g_list_find_custom (GList *list,
710 gint g_list_position (GList *list,
712 gint g_list_index (GList *list,
714 GList* g_list_last (GList *list);
715 GList* g_list_first (GList *list);
716 guint g_list_length (GList *list);
717 void g_list_foreach (GList *list,
720 gpointer g_list_nth_data (GList *list,
722 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
723 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
726 /* Singly linked lists
728 GSList* g_slist_alloc (void);
729 void g_slist_free (GSList *list);
730 void g_slist_free_1 (GSList *list);
731 GSList* g_slist_append (GSList *list,
733 GSList* g_slist_prepend (GSList *list,
735 GSList* g_slist_insert (GSList *list,
738 GSList* g_slist_insert_sorted (GSList *list,
741 GSList* g_slist_concat (GSList *list1,
743 GSList* g_slist_remove (GSList *list,
745 GSList* g_slist_remove_link (GSList *list,
747 GSList* g_slist_reverse (GSList *list);
748 GSList* g_slist_nth (GSList *list,
750 GSList* g_slist_find (GSList *list,
752 GSList* g_slist_find_custom (GSList *list,
755 gint g_slist_position (GSList *list,
757 gint g_slist_index (GSList *list,
759 GSList* g_slist_last (GSList *list);
760 guint g_slist_length (GSList *list);
761 void g_slist_foreach (GSList *list,
764 gpointer g_slist_nth_data (GSList *list,
766 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
771 GListAllocator* g_list_allocator_new (void);
772 void g_list_allocator_free (GListAllocator* allocator);
773 GListAllocator* g_slist_set_allocator (GListAllocator* allocator);
774 GListAllocator* g_list_set_allocator (GListAllocator* allocator);
779 GHashTable* g_hash_table_new (GHashFunc hash_func,
780 GCompareFunc key_compare_func);
781 void g_hash_table_destroy (GHashTable *hash_table);
782 void g_hash_table_insert (GHashTable *hash_table,
785 void g_hash_table_remove (GHashTable *hash_table,
787 gpointer g_hash_table_lookup (GHashTable *hash_table,
789 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
790 gconstpointer lookup_key,
793 void g_hash_table_freeze (GHashTable *hash_table);
794 void g_hash_table_thaw (GHashTable *hash_table);
795 void g_hash_table_foreach (GHashTable *hash_table,
798 gint g_hash_table_size (GHashTable *hash_table);
803 GCache* g_cache_new (GCacheNewFunc value_new_func,
804 GCacheDestroyFunc value_destroy_func,
805 GCacheDupFunc key_dup_func,
806 GCacheDestroyFunc key_destroy_func,
807 GHashFunc hash_key_func,
808 GHashFunc hash_value_func,
809 GCompareFunc key_compare_func);
810 void g_cache_destroy (GCache *cache);
811 gpointer g_cache_insert (GCache *cache,
813 void g_cache_remove (GCache *cache,
815 void g_cache_key_foreach (GCache *cache,
818 void g_cache_value_foreach (GCache *cache,
823 /* Balanced binary trees
825 GTree* g_tree_new (GCompareFunc key_compare_func);
826 void g_tree_destroy (GTree *tree);
827 void g_tree_insert (GTree *tree,
830 void g_tree_remove (GTree *tree,
832 gpointer g_tree_lookup (GTree *tree,
834 void g_tree_traverse (GTree *tree,
835 GTraverseFunc traverse_func,
836 GTraverseType traverse_type,
838 gpointer g_tree_search (GTree *tree,
839 GSearchFunc search_func,
841 gint g_tree_height (GTree *tree);
842 gint g_tree_nnodes (GTree *tree);
846 /* N-way tree implementation
857 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
858 ((GNode*) (node))->prev == NULL && \
859 ((GNode*) (node))->next == NULL)
860 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
862 GNode* g_node_new (gpointer data);
863 void g_node_destroy (GNode *root);
864 void g_node_unlink (GNode *node);
865 GNode* g_node_insert (GNode *parent,
868 GNode* g_node_insert_before (GNode *parent,
871 GNode* g_node_prepend (GNode *parent,
873 guint g_node_n_nodes (GNode *root,
874 GTraverseFlags flags);
875 GNode* g_node_get_root (GNode *node);
876 gboolean g_node_is_ancestor (GNode *node,
878 guint g_node_depth (GNode *node);
879 GNode* g_node_find (GNode *root,
881 GTraverseFlags flags,
884 /* convenience macros */
885 #define g_node_append(parent, node) \
886 g_node_insert_before ((parent), NULL, (node))
887 #define g_node_insert_data(parent, position, data) \
888 g_node_insert ((parent), (position), g_node_new (data))
889 #define g_node_insert_data_before(parent, sibling, data) \
890 g_node_insert_before ((parent), (sibling), g_node_new (data))
891 #define g_node_prepend_data(parent, data) \
892 g_node_prepend ((parent), g_node_new (data))
893 #define g_node_append_data(parent, data) \
894 g_node_insert_before ((parent), NULL, g_node_new (data))
896 /* traversal function, assumes that `node' is root
897 * (only traverses `node' and its subtree).
898 * this function is just a high level interface to
899 * low level traversal functions, optimized for speed.
901 void g_node_traverse (GNode *root,
903 GTraverseFlags flags,
905 GNodeTraverseFunc func,
908 /* return the maximum tree height starting with `node', this is an expensive
909 * operation, since we need to visit all nodes. this could be shortened by
910 * adding `guint height' to struct _GNode, but then again, this is not very
911 * often needed, and would make g_node_insert() more time consuming.
913 guint g_node_max_height (GNode *root);
915 void g_node_children_foreach (GNode *node,
916 GTraverseFlags flags,
917 GNodeForeachFunc func,
919 void g_node_reverse_children (GNode *node);
920 guint g_node_n_children (GNode *node);
921 GNode* g_node_nth_child (GNode *node,
923 GNode* g_node_last_child (GNode *node);
924 GNode* g_node_find_child (GNode *node,
925 GTraverseFlags flags,
927 gint g_node_child_position (GNode *node,
929 gint g_node_child_index (GNode *node,
932 GNode* g_node_first_sibling (GNode *node);
933 GNode* g_node_last_sibling (GNode *node);
935 #define g_node_prev_sibling(node) ((node) ? \
936 ((GNode*) (node))->prev : NULL)
937 #define g_node_next_sibling(node) ((node) ? \
938 ((GNode*) (node))->next : NULL)
939 #define g_node_first_child(node) ((node) ? \
940 ((GNode*) (node))->children : NULL)
943 /* Fatal error handlers.
944 * g_on_error_query() will prompt the user to either
945 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
946 * g_on_error_stack_trace() invokes gdb, which attaches to the current
947 * process and shows a stack trace.
948 * These function may cause different actions on non-unix platforms.
949 * The prg_name arg is required by gdb to find the executable, if it is
950 * passed as NULL, g_on_error_query() will try g_get_prgname().
952 void g_on_error_query (const gchar *prg_name);
953 void g_on_error_stack_trace (const gchar *prg_name);
958 extern const gchar *g_log_domain_glib;
959 guint g_log_set_handler (const gchar *log_domain,
960 GLogLevelFlags log_levels,
963 void g_log_remove_handler (const gchar *log_domain,
965 void g_log_default_handler (const gchar *log_domain,
966 GLogLevelFlags log_level,
967 const gchar *message,
968 gpointer unused_data);
969 void g_log (const gchar *log_domain,
970 GLogLevelFlags log_level,
972 ...) G_GNUC_PRINTF (3, 4);
973 void g_logv (const gchar *log_domain,
974 GLogLevelFlags log_level,
977 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
978 GLogLevelFlags fatal_mask);
979 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
981 #define G_LOG_DOMAIN (NULL)
982 #endif /* G_LOG_DOMAIN */
984 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
987 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
988 G_LOG_LEVEL_MESSAGE, \
990 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
991 G_LOG_LEVEL_WARNING, \
993 #else /* !__GNUC__ */
995 g_error (const gchar *format,
999 va_start (args, format);
1000 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1004 g_message (const gchar *format,
1008 va_start (args, format);
1009 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1013 g_warning (const gchar *format,
1017 va_start (args, format);
1018 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1021 #endif /* !__GNUC__ */
1023 typedef void (*GPrintFunc) (const gchar *string);
1024 void g_print (const gchar *format,
1025 ...) G_GNUC_PRINTF (1, 2);
1026 GPrintFunc g_set_print_handler (GPrintFunc func);
1027 void g_printerr (const gchar *format,
1028 ...) G_GNUC_PRINTF (1, 2);
1029 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1031 /* deprecated compatibility functions, use g_log_set_handler() instead */
1032 typedef void (*GErrorFunc) (const gchar *str);
1033 typedef void (*GWarningFunc) (const gchar *str);
1034 GErrorFunc g_set_error_handler (GErrorFunc func);
1035 GWarningFunc g_set_warning_handler (GWarningFunc func);
1036 GPrintFunc g_set_message_handler (GPrintFunc func);
1039 /* Memory allocation and debugging
1043 #define g_malloc(size) ((gpointer) MALLOC (size))
1044 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1045 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1046 #define g_free(mem) FREE (mem)
1048 #else /* !USE_DMALLOC */
1050 gpointer g_malloc (gulong size);
1051 gpointer g_malloc0 (gulong size);
1052 gpointer g_realloc (gpointer mem,
1054 void g_free (gpointer mem);
1056 #endif /* !USE_DMALLOC */
1058 void g_mem_profile (void);
1059 void g_mem_check (gpointer mem);
1062 /* "g_mem_chunk_new" creates a new memory chunk.
1063 * Memory chunks are used to allocate pieces of memory which are
1064 * always the same size. Lists are a good example of such a data type.
1065 * The memory chunk allocates and frees blocks of memory as needed.
1066 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1067 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1068 * fault...somewhere).
1070 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1071 * want to know what's going on inside do you?)
1074 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1075 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1076 * atom. (They are also useful for lists which use MemChunk to allocate
1077 * memory but are also part of the MemChunk implementation).
1078 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1081 #define G_ALLOC_ONLY 1
1082 #define G_ALLOC_AND_FREE 2
1084 GMemChunk* g_mem_chunk_new (gchar *name,
1088 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1089 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1090 void g_mem_chunk_free (GMemChunk *mem_chunk,
1092 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1093 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1094 void g_mem_chunk_print (GMemChunk *mem_chunk);
1095 void g_mem_chunk_info (void);
1097 /* Ah yes...we have a "g_blow_chunks" function.
1098 * "g_blow_chunks" simply compresses all the chunks. This operation
1099 * consists of freeing every memory area that should be freed (but
1100 * which we haven't gotten around to doing yet). And, no,
1101 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1102 * much better name than "g_mem_chunk_clean_all" or something
1105 void g_blow_chunks (void);
1110 GTimer* g_timer_new (void);
1111 void g_timer_destroy (GTimer *timer);
1112 void g_timer_start (GTimer *timer);
1113 void g_timer_stop (GTimer *timer);
1114 void g_timer_reset (GTimer *timer);
1115 gdouble g_timer_elapsed (GTimer *timer,
1116 gulong *microseconds);
1119 /* String utility functions
1121 #define G_STR_DELIMITERS "_-|> <."
1122 void g_strdelimit (gchar *string,
1123 const gchar *delimiters,
1124 gchar new_delimiter);
1125 gchar* g_strdup (const gchar *str);
1126 gchar* g_strdup_printf (const gchar *format,
1127 ...) G_GNUC_PRINTF (1, 2);
1128 gchar* g_strdup_vprintf (const gchar *format,
1130 gchar* g_strndup (const gchar *str,
1132 gchar* g_strconcat (const gchar *string1,
1133 ...); /* NULL terminated */
1134 gdouble g_strtod (const gchar *nptr,
1136 gchar* g_strerror (gint errnum);
1137 gchar* g_strsignal (gint signum);
1138 gint g_strcasecmp (const gchar *s1,
1140 void g_strdown (gchar *string);
1141 void g_strup (gchar *string);
1142 void g_strreverse (gchar *string);
1144 /* calculate a string size, guarranteed to fit format + args.
1146 guint g_printf_string_upper_bound (const gchar* format,
1150 /* Retrive static string info
1152 gchar* g_get_user_name (void);
1153 gchar* g_get_real_name (void);
1154 gchar* g_get_home_dir (void);
1155 gchar* g_get_tmp_dir (void);
1156 gchar* g_get_prgname (void);
1157 void g_set_prgname (const gchar *prgname);
1160 /* Miscellaneous utility functions
1162 guint g_parse_debug_string (const gchar *string,
1165 gint g_snprintf (gchar *string,
1167 gchar const *format,
1168 ...) G_GNUC_PRINTF (3, 4);
1169 gint g_vsnprintf (gchar *string,
1171 gchar const *format,
1173 gchar* g_basename (const gchar *file_name);
1175 /* strings are newly allocated with g_malloc() */
1176 gchar* g_dirname (const gchar *file_name);
1177 gchar* g_get_current_dir (void);
1179 /* We make the assumption that if memmove isn't available, then
1180 * bcopy will do the job. This isn't safe everywhere. (bcopy can't
1181 * necessarily handle overlapping copies).
1182 * Either way, g_memmove() will not return a value.
1185 #define g_memmove(dest, src, size) G_STMT_START { \
1186 memmove ((dest), (src), (size)); \
1189 #define g_memmove(dest, src, size) G_STMT_START { \
1190 bcopy ((src), (dest), (size)); \
1198 g_bit_nth_lsf (guint32 mask,
1204 if (mask & (1 << (guint) nth_bit))
1207 while (nth_bit < 32);
1211 g_bit_nth_msf (guint32 mask,
1219 if (mask & (1 << (guint) nth_bit))
1222 while (nth_bit > 0);
1226 g_bit_storage (guint number)
1228 register guint n_bits = 0;
1233 number = number >> 1;
1241 GStringChunk* g_string_chunk_new (gint size);
1242 void g_string_chunk_free (GStringChunk *chunk);
1243 gchar* g_string_chunk_insert (GStringChunk *chunk,
1244 const gchar *string);
1245 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1246 const gchar *string);
1251 GString* g_string_new (const gchar *init);
1252 GString* g_string_sized_new (guint dfl_size);
1253 void g_string_free (GString *string,
1255 GString* g_string_assign (GString *lval,
1257 GString* g_string_truncate (GString *string,
1259 GString* g_string_append (GString *string,
1261 GString* g_string_append_c (GString *string,
1263 GString* g_string_prepend (GString *string,
1265 GString* g_string_prepend_c (GString *string,
1267 GString* g_string_insert (GString *string,
1270 GString* g_string_insert_c (GString *string,
1273 GString* g_string_erase (GString *string,
1276 GString* g_string_down (GString *string);
1277 GString* g_string_up (GString *string);
1278 void g_string_sprintf (GString *string,
1279 const gchar *format,
1280 ...) G_GNUC_PRINTF (2, 3);
1281 void g_string_sprintfa (GString *string,
1282 const gchar *format,
1283 ...) G_GNUC_PRINTF (2, 3);
1288 #define g_array_length(array,type) \
1289 (((array)->len)/sizeof(type))
1290 #define g_array_append_val(array,type,val) \
1291 g_rarray_append (array, (gpointer) &val, sizeof (type))
1292 #define g_array_append_vals(array,type,vals,nvals) \
1293 g_rarray_append (array, (gpointer) vals, sizeof (type) * nvals)
1294 #define g_array_prepend_val(array,type,val) \
1295 g_rarray_prepend (array, (gpointer) &val, sizeof (type))
1296 #define g_array_prepend_vals(array,type,vals,nvals) \
1297 g_rarray_prepend (array, (gpointer) vals, sizeof (type) * nvals)
1298 #define g_array_truncate(array,type,length) \
1299 g_rarray_truncate (array, length, sizeof (type))
1300 #define g_array_index(array,type,index) \
1301 ((type*) array->data)[index]
1303 GArray* g_array_new (gboolean zero_terminated);
1304 void g_array_free (GArray *array,
1305 gboolean free_segment);
1306 GArray* g_rarray_append (GArray *array,
1309 GArray* g_rarray_prepend (GArray *array,
1312 GArray* g_rarray_truncate (GArray *array,
1317 /* Resizable pointer array. This interface is much less complicated
1318 * than the above. Add appends appends a pointer. Remove fills any
1319 * cleared spot and shortens the array.
1321 #define g_ptr_array_index(array,index) (array->pdata)[index]
1322 GPtrArray* g_ptr_array_new (void);
1323 void g_ptr_array_free (GPtrArray *array,
1325 void g_ptr_array_set_size (GPtrArray *array,
1327 void g_ptr_array_remove_index (GPtrArray *array,
1329 gboolean g_ptr_array_remove (GPtrArray *array,
1331 void g_ptr_array_add (GPtrArray *array,
1335 /* Byte arrays, an array of guint8. Implemented as a GArray,
1338 GByteArray* g_byte_array_new (void);
1339 void g_byte_array_free (GByteArray *array,
1341 GByteArray* g_byte_array_append (GByteArray *array,
1345 GByteArray* g_byte_array_prepend (GByteArray *array,
1349 GByteArray* g_byte_array_truncate (GByteArray *array,
1355 gint g_str_equal (gconstpointer v,
1357 guint g_str_hash (gconstpointer v);
1359 gint g_int_equal (gconstpointer v,
1361 guint g_int_hash (gconstpointer v);
1363 /* This "hash" function will just return the key's adress as an
1364 * unsigned integer. Useful for hashing on plain adresses or
1365 * simple integer values.
1367 guint g_direct_hash (gconstpointer v);
1368 gint g_direct_equal (gconstpointer v,
1372 /* Quarks (string<->id association)
1374 GQuark g_quark_try_string (const gchar *string);
1375 GQuark g_quark_from_static_string (const gchar *string);
1376 GQuark g_quark_from_string (const gchar *string);
1377 gchar* g_quark_to_string (GQuark quark);
1380 /* Location Associated Data
1382 void g_dataset_destroy (gconstpointer dataset_location);
1383 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1385 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1388 GDestroyNotify destroy_func);
1389 void g_dataset_id_set_destroy (gconstpointer dataset_location,
1391 GDestroyNotify destroy_func);
1392 #define g_dataset_id_set_data(l, k, d) \
1393 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1394 #define g_dataset_id_remove_data(l, k) \
1395 g_dataset_id_set_data ((l), (k), NULL)
1396 #define g_dataset_get_data(l, k) \
1397 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1398 #define g_dataset_set_data_full(l, k, d, f) \
1399 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1400 #define g_dataset_set_destroy(l, k, f) \
1401 g_dataset_id_set_destroy ((l), g_quark_from_string (k), (f))
1402 #define g_dataset_set_data(l, k, d) \
1403 g_dataset_set_data_full ((l), (k), (d), NULL)
1404 #define g_dataset_remove_data(l,k) \
1405 g_dataset_set_data ((l), (k), NULL)
1408 /* GScanner: Flexible lexical scanner for general purpose.
1411 /* Character sets */
1412 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1413 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1414 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1415 "\307\310\311\312\313\314\315\316\317\320"\
1416 "\321\322\323\324\325\326"\
1417 "\330\331\332\333\334\335\336"
1418 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1419 "\347\350\351\352\353\354\355\356\357\360"\
1420 "\361\362\363\364\365\366"\
1421 "\370\371\372\373\374\375\376\377"
1428 G_ERR_UNEXP_EOF_IN_STRING,
1429 G_ERR_UNEXP_EOF_IN_COMMENT,
1430 G_ERR_NON_DIGIT_IN_CONST,
1433 G_ERR_FLOAT_MALFORMED
1441 G_TOKEN_LEFT_PAREN = '(',
1442 G_TOKEN_RIGHT_PAREN = ')',
1443 G_TOKEN_LEFT_CURLY = '{',
1444 G_TOKEN_RIGHT_CURLY = '}',
1445 G_TOKEN_LEFT_BRACE = '[',
1446 G_TOKEN_RIGHT_BRACE = ']',
1447 G_TOKEN_EQUAL_SIGN = '=',
1448 G_TOKEN_COMMA = ',',
1464 G_TOKEN_IDENTIFIER_NULL,
1466 G_TOKEN_COMMENT_SINGLE,
1467 G_TOKEN_COMMENT_MULTI,
1474 gchar *v_identifier;
1486 struct _GScannerConfig
1490 gchar *cset_skip_characters; /* default: " \t\n" */
1491 gchar *cset_identifier_first;
1492 gchar *cset_identifier_nth;
1493 gchar *cpair_comment_single; /* default: "#\n" */
1495 /* Should symbol lookup work case sensitive?
1497 guint case_sensitive : 1;
1499 /* Boolean values to be adjusted "on the fly"
1500 * to configure scanning behaviour.
1502 guint skip_comment_multi : 1; /* C like comment */
1503 guint skip_comment_single : 1; /* single line comment */
1504 guint scan_comment_multi : 1; /* scan multi line comments? */
1505 guint scan_identifier : 1;
1506 guint scan_identifier_1char : 1;
1507 guint scan_identifier_NULL : 1;
1508 guint scan_symbols : 1;
1509 guint scan_binary : 1;
1510 guint scan_octal : 1;
1511 guint scan_float : 1;
1512 guint scan_hex : 1; /* `0x0ff0' */
1513 guint scan_hex_dollar : 1; /* `$0ff0' */
1514 guint scan_string_sq : 1; /* string: 'anything' */
1515 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
1516 guint numbers_2_int : 1; /* bin, octal, hex => int */
1517 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
1518 guint identifier_2_string : 1;
1519 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
1520 guint symbol_2_token : 1;
1521 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
1528 guint max_parse_errors;
1530 /* g_scanner_error() increments this field */
1533 /* name of input stream, featured by the default message handler */
1534 const gchar *input_name;
1536 /* data pointer for derived structures */
1537 gpointer derived_data;
1539 /* link into the scanner configuration */
1540 GScannerConfig *config;
1542 /* fields filled in after g_scanner_get_next_token() */
1548 /* fields filled in after g_scanner_peek_next_token() */
1549 GTokenType next_token;
1552 guint next_position;
1554 /* to be considered private */
1555 GHashTable *symbol_table;
1562 /* handler function for _warn and _error */
1563 GScannerMsgFunc msg_handler;
1566 GScanner* g_scanner_new (GScannerConfig *config_templ);
1567 void g_scanner_destroy (GScanner *scanner);
1568 void g_scanner_input_file (GScanner *scanner,
1570 void g_scanner_input_text (GScanner *scanner,
1573 GTokenType g_scanner_get_next_token (GScanner *scanner);
1574 GTokenType g_scanner_peek_next_token (GScanner *scanner);
1575 GTokenType g_scanner_cur_token (GScanner *scanner);
1576 GValue g_scanner_cur_value (GScanner *scanner);
1577 guint g_scanner_cur_line (GScanner *scanner);
1578 guint g_scanner_cur_position (GScanner *scanner);
1579 gboolean g_scanner_eof (GScanner *scanner);
1580 guint g_scanner_set_scope (GScanner *scanner,
1582 void g_scanner_scope_add_symbol (GScanner *scanner,
1584 const gchar *symbol,
1586 void g_scanner_scope_remove_symbol (GScanner *scanner,
1588 const gchar *symbol);
1589 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
1591 const gchar *symbol);
1592 void g_scanner_scope_foreach_symbol (GScanner *scanner,
1595 gpointer func_data);
1596 gpointer g_scanner_lookup_symbol (GScanner *scanner,
1597 const gchar *symbol);
1598 void g_scanner_freeze_symbol_table (GScanner *scanner);
1599 void g_scanner_thaw_symbol_table (GScanner *scanner);
1600 void g_scanner_unexp_token (GScanner *scanner,
1601 GTokenType expected_token,
1602 const gchar *identifier_spec,
1603 const gchar *symbol_spec,
1604 const gchar *symbol_name,
1605 const gchar *message,
1607 void g_scanner_error (GScanner *scanner,
1608 const gchar *format,
1609 ...) G_GNUC_PRINTF (2,3);
1610 void g_scanner_warn (GScanner *scanner,
1611 const gchar *format,
1612 ...) G_GNUC_PRINTF (2,3);
1613 gint g_scanner_stat_mode (const gchar *filename);
1614 /* keep downward source compatibility */
1615 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
1616 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
1618 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
1619 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
1621 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
1622 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
1631 GCompletionFunc func;
1637 GCompletion* g_completion_new (GCompletionFunc func);
1638 void g_completion_add_items (GCompletion* cmp,
1640 void g_completion_remove_items (GCompletion* cmp,
1642 void g_completion_clear_items (GCompletion* cmp);
1643 GList* g_completion_complete (GCompletion* cmp,
1645 gchar** new_prefix);
1646 void g_completion_free (GCompletion* cmp);
1649 /* GRelation: Indexed Relations. Imagine a really simple table in a
1650 * database. Relations are not ordered. This data type is meant for
1651 * maintaining a N-way mapping.
1653 * g_relation_new() creates a relation with FIELDS fields
1655 * g_relation_destroy() frees all resources
1656 * g_tuples_destroy() frees the result of g_relation_select()
1658 * g_relation_index() indexes relation FIELD with the provided
1659 * equality and hash functions. this must be done before any
1660 * calls to insert are made.
1662 * g_relation_insert() inserts a new tuple. you are expected to
1663 * provide the right number of fields.
1665 * g_relation_delete() deletes all relations with KEY in FIELD
1666 * g_relation_select() returns ...
1667 * g_relation_count() counts ...
1670 GRelation* g_relation_new (gint fields);
1671 void g_relation_destroy (GRelation *relation);
1672 void g_relation_index (GRelation *relation,
1674 GHashFunc hash_func,
1675 GCompareFunc key_compare_func);
1676 void g_relation_insert (GRelation *relation,
1678 gint g_relation_delete (GRelation *relation,
1681 GTuples* g_relation_select (GRelation *relation,
1684 gint g_relation_count (GRelation *relation,
1687 gboolean g_relation_exists (GRelation *relation,
1689 void g_relation_print (GRelation *relation);
1691 void g_tuples_destroy (GTuples *tuples);
1692 gpointer g_tuples_index (GTuples *tuples,
1702 #endif /* __cplusplus */
1705 #endif /* __G_LIB_H__ */