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 #include <glibconfig.h>
29 /* glib provides definitions for the extrema of many
30 * of the standard types. These are:
47 #define G_MINFLOAT FLT_MIN
48 #define G_MAXFLOAT FLT_MAX
49 #define G_MINDOUBLE DBL_MIN
50 #define G_MAXDOUBLE DBL_MAX
56 #define G_MINFLOAT MINFLOAT
57 #define G_MAXFLOAT MAXFLOAT
58 #define G_MINDOUBLE MINDOUBLE
59 #define G_MAXDOUBLE MAXDOUBLE
61 #endif /* HAVE_VALUES_H */
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
79 #endif /* HAVE_FLOAT_H */
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
88 #endif /* HAVE_VALUES_H */
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.
97 #define NULL ((void*) 0)
105 #define TRUE (!FALSE)
109 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
112 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
115 #define ABS(a) (((a) < 0) ? -(a) : (a))
118 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
121 /* Provide simple enum value macro wrappers that ease automated enum value
122 * stringification code.
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 */
137 /* Provide simple macro statement wrappers (adapted from Perl):
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 ...
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)'.
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 )
151 # if (defined (sun) || defined (__sun__))
152 # define G_STMT_START if (1)
153 # define G_STMT_END else (void)0
155 # define G_STMT_START do
156 # define G_STMT_END while (0)
161 /* Provide macros to feature the GCC function attribute.
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
180 #endif /* !__GNUC__ */
182 /* Hacker macro to place breakpoints for x86 machines.
183 * Actual use is strongly deprecated of course ;)
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__ */
191 /* Wrap the __PRETTY_FUNCTION__ and __FUNCTION__ variables with macros,
192 * so we can refer to them as strings unconditionally.
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__ */
205 # define ATEXIT(proc) (atexit (proc))
206 # elif defined (HAVE_ON_EXIT)
207 # define ATEXIT(proc) (on_exit ((void (*)(int, void *))(proc), NULL))
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).
219 #define g_new(type,count) ALLOC(type,count)
220 #define g_new0(type,count) CALLOC(type,count)
222 #else /* __DMALLOC_H__ */
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__ */
230 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
231 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
233 sizeof (type) * (pre_alloc), \
236 #define g_chunk_new(type, chunk) ( \
237 (type *) g_mem_chunk_alloc (chunk) \
239 #define g_chunk_new0(type, chunk) ( \
240 (type *) memset (g_mem_chunk_alloc (chunk), 0, sizeof (type)) \
242 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
243 g_mem_chunk_free ((mem_chunk), (mem)); \
246 #define g_string(x) #x
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.
255 #ifdef G_DISABLE_ASSERT
257 #define g_assert(expr)
258 #define g_assert_not_reached()
260 #else /* !G_DISABLE_ASSERT */
264 #define g_assert(expr) G_STMT_START{\
266 g_error ("file %s: line %d (%s): \"%s\"", \
269 __PRETTY_FUNCTION__, \
272 #define g_assert_not_reached() G_STMT_START{ \
273 g_error ("file %s: line %d (%s): \"should not be reached\"", \
276 __PRETTY_FUNCTION__); }G_STMT_END
278 #else /* !__GNUC__ */
280 #define g_assert(expr) G_STMT_START{\
282 g_error ("file %s: line %d: \"%s\"", \
287 #define g_assert_not_reached() G_STMT_START{ \
288 g_error ("file %s: line %d: \"should not be reached\"", \
290 __LINE__); }G_STMT_END
292 #endif /* __GNUC__ */
294 #endif /* G_DISABLE_ASSERT */
296 #ifdef G_DISABLE_CHECKS
298 #define g_return_if_fail(expr)
299 #define g_return_val_if_fail(expr,val)
301 #else /* !G_DISABLE_CHECKS */
305 #define g_return_if_fail(expr) G_STMT_START{ \
308 g_warning ("file %s: line %d (%s): assertion \"%s\" failed.", \
311 __PRETTY_FUNCTION__, \
316 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
319 g_warning ("file %s: line %d (%s): assertion \"%s\" failed.", \
322 __PRETTY_FUNCTION__, \
327 #else /* !__GNUC__ */
329 #define g_return_if_fail(expr) G_STMT_START{ \
332 g_warning ("file %s: line %d: assertion. \"%s\" failed", \
339 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
342 g_warning ("file %s: line %d: assertion \"%s\" failed.", \
349 #endif /* !__GNUC__ */
351 #endif /* G_DISABLE_CHECKS */
357 #endif /* __cplusplus */
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.
367 typedef short gshort;
370 typedef gint gboolean;
372 typedef unsigned char guchar;
373 typedef unsigned short gushort;
374 typedef unsigned long gulong;
375 typedef unsigned int guint;
377 typedef float gfloat;
378 typedef double gdouble;
380 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
381 * Since gldouble isn't used anywhere, just disable it for now */
384 #ifdef HAVE_LONG_DOUBLE
385 typedef long double gldouble;
386 #else /* HAVE_LONG_DOUBLE */
387 typedef double gldouble;
388 #endif /* HAVE_LONG_DOUBLE */
391 typedef void* gpointer;
392 typedef const void *gconstpointer;
394 #if (SIZEOF_CHAR == 1)
395 typedef signed char gint8;
396 typedef unsigned char guint8;
397 #endif /* SIZEOF_CHAR */
400 #if (SIZEOF_SHORT == 2)
401 typedef signed short gint16;
402 typedef unsigned short guint16;
403 #endif /* SIZEOF_SHORT */
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 */
414 #if (SIZEOF_LONG == 8)
415 #define HAVE_GINT64 1
416 typedef signed long gint64;
417 typedef unsigned long guint64;
418 #elif (SIZEOF_LONG_LONG == 8)
419 #define HAVE_GINT64 1
420 typedef signed long long gint64;
421 typedef unsigned long long guint64;
428 /* Define macros for storing integers inside pointers */
430 #if (SIZEOF_INT == SIZEOF_VOID_P)
432 #define GPOINTER_TO_INT(p) ((gint)(p))
433 #define GPOINTER_TO_UINT(p) ((guint)(p))
435 #define GINT_TO_POINTER(i) ((gpointer)(i))
436 #define GUINT_TO_POINTER(u) ((gpointer)(u))
438 #elif (SIZEOF_LONG == SIZEOF_VOID_P)
440 #define GPOINTER_TO_INT(p) ((gint)(glong)(p))
441 #define GPOINTER_TO_UINT(p) ((guint)(gulong)(p))
443 #define GINT_TO_POINTER(i) ((gpointer)(glong)(i))
444 #define GUINT_TO_POINTER(u) ((gpointer)(gulong)(u))
447 /* This should never happen */
450 typedef gint32 gssize;
451 typedef guint32 gsize;
452 typedef guint32 GQuark;
455 typedef gint32 GTime;
458 typedef struct _GList GList;
459 typedef struct _GSList GSList;
460 typedef struct _GHashTable GHashTable;
461 typedef struct _GCache GCache;
462 typedef struct _GTree GTree;
463 typedef struct _GTimer GTimer;
464 typedef struct _GMemChunk GMemChunk;
465 typedef struct _GListAllocator GListAllocator;
466 typedef struct _GStringChunk GStringChunk;
467 typedef struct _GString GString;
468 typedef struct _GArray GArray;
469 typedef struct _GPtrArray GPtrArray;
470 typedef struct _GByteArray GByteArray;
471 typedef struct _GDebugKey GDebugKey;
472 typedef struct _GScannerConfig GScannerConfig;
473 typedef struct _GScanner GScanner;
474 typedef union _GValue GValue;
475 typedef struct _GCompletion GCompletion;
476 typedef struct _GRelation GRelation;
477 typedef struct _GTuples GTuples;
480 typedef void (*GFunc) (gpointer data,
482 typedef void (*GHFunc) (gpointer key,
485 typedef gpointer (*GCacheNewFunc) (gpointer key);
486 typedef gpointer (*GCacheDupFunc) (gpointer value);
487 typedef void (*GCacheDestroyFunc) (gpointer value);
488 typedef gint (*GTraverseFunc) (gpointer key,
491 typedef gint (*GSearchFunc) (gpointer key,
493 typedef void (*GErrorFunc) (gchar *str);
494 typedef void (*GWarningFunc) (gchar *str);
495 typedef void (*GPrintFunc) (gchar *str);
496 typedef void (*GScannerMsgFunc) (GScanner *scanner,
499 typedef void (*GDestroyNotify) (gpointer data);
501 typedef guint (*GHashFunc) (gconstpointer key);
502 typedef gint (*GCompareFunc) (gconstpointer a,
504 typedef gchar* (*GCompletionFunc) (gpointer);
554 struct _GCache { gint dummy; };
555 struct _GTree { gint dummy; };
556 struct _GTimer { gint dummy; };
557 struct _GMemChunk { gint dummy; };
558 struct _GListAllocator { gint dummy; };
559 struct _GStringChunk { gint dummy; };
568 /* Doubly linked lists
570 GList* g_list_alloc (void);
571 void g_list_free (GList *list);
572 void g_list_free_1 (GList *list);
573 GList* g_list_append (GList *list,
575 GList* g_list_prepend (GList *list,
577 GList* g_list_insert (GList *list,
580 GList* g_list_insert_sorted (GList *list,
583 GList* g_list_concat (GList *list1,
585 GList* g_list_remove (GList *list,
587 GList* g_list_remove_link (GList *list,
589 GList* g_list_reverse (GList *list);
590 GList* g_list_nth (GList *list,
592 GList* g_list_find (GList *list,
594 GList* g_list_find_custom (GList *list,
597 gint g_list_position (GList *list,
599 gint g_list_index (GList *list,
601 GList* g_list_last (GList *list);
602 GList* g_list_first (GList *list);
603 guint g_list_length (GList *list);
604 void g_list_foreach (GList *list,
607 gpointer g_list_nth_data (GList *list,
610 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
611 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
613 /* Singly linked lists
615 GSList* g_slist_alloc (void);
616 void g_slist_free (GSList *list);
617 void g_slist_free_1 (GSList *list);
618 GSList* g_slist_append (GSList *list,
620 GSList* g_slist_prepend (GSList *list,
622 GSList* g_slist_insert (GSList *list,
625 GSList* g_slist_insert_sorted (GSList *list,
628 GSList* g_slist_concat (GSList *list1,
630 GSList* g_slist_remove (GSList *list,
632 GSList* g_slist_remove_link (GSList *list,
634 GSList* g_slist_reverse (GSList *list);
635 GSList* g_slist_nth (GSList *list,
637 GSList* g_slist_find (GSList *list,
639 GSList* g_slist_find_custom (GSList *list,
642 gint g_slist_position (GSList *list,
644 gint g_slist_index (GSList *list,
646 GSList* g_slist_last (GSList *list);
647 guint g_slist_length (GSList *list);
648 void g_slist_foreach (GSList *list,
651 gpointer g_slist_nth_data (GSList *list,
654 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
658 GListAllocator* g_list_allocator_new (void);
659 void g_list_allocator_free (GListAllocator* allocator);
660 GListAllocator* g_slist_set_allocator (GListAllocator* allocator);
661 GListAllocator* g_list_set_allocator (GListAllocator* allocator);
666 GHashTable* g_hash_table_new (GHashFunc hash_func,
667 GCompareFunc key_compare_func);
668 void g_hash_table_destroy (GHashTable *hash_table);
669 void g_hash_table_insert (GHashTable *hash_table,
672 void g_hash_table_remove (GHashTable *hash_table,
674 gpointer g_hash_table_lookup (GHashTable *hash_table,
676 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
677 gconstpointer lookup_key,
680 void g_hash_table_freeze (GHashTable *hash_table);
681 void g_hash_table_thaw (GHashTable *hash_table);
682 void g_hash_table_foreach (GHashTable *hash_table,
685 gint g_hash_table_size (GHashTable *hash_table);
690 GCache* g_cache_new (GCacheNewFunc value_new_func,
691 GCacheDestroyFunc value_destroy_func,
692 GCacheDupFunc key_dup_func,
693 GCacheDestroyFunc key_destroy_func,
694 GHashFunc hash_key_func,
695 GHashFunc hash_value_func,
696 GCompareFunc key_compare_func);
697 void g_cache_destroy (GCache *cache);
698 gpointer g_cache_insert (GCache *cache,
700 void g_cache_remove (GCache *cache,
702 void g_cache_key_foreach (GCache *cache,
705 void g_cache_value_foreach (GCache *cache,
712 GTree* g_tree_new (GCompareFunc key_compare_func);
713 void g_tree_destroy (GTree *tree);
714 void g_tree_insert (GTree *tree,
717 void g_tree_remove (GTree *tree,
719 gpointer g_tree_lookup (GTree *tree,
721 void g_tree_traverse (GTree *tree,
722 GTraverseFunc traverse_func,
723 GTraverseType traverse_type,
725 gpointer g_tree_search (GTree *tree,
726 GSearchFunc search_func,
728 gint g_tree_height (GTree *tree);
729 gint g_tree_nnodes (GTree *tree);
737 #define g_malloc(size) (gpointer) MALLOC(size)
738 #define g_malloc0(size) (gpointer) CALLOC(char,size)
739 #define g_realloc(mem,size) (gpointer) REALLOC(mem,char,size)
740 #define g_free(mem) FREE(mem)
742 #else /* USE_DMALLOC */
744 gpointer g_malloc (gulong size);
745 gpointer g_malloc0 (gulong size);
746 gpointer g_realloc (gpointer mem,
748 void g_free (gpointer mem);
750 #endif /* USE_DMALLOC */
752 void g_mem_profile (void);
753 void g_mem_check (gpointer mem);
756 /* "g_mem_chunk_new" creates a new memory chunk.
757 * Memory chunks are used to allocate pieces of memory which are
758 * always the same size. Lists are a good example of such a data type.
759 * The memory chunk allocates and frees blocks of memory as needed.
760 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
761 * allocated in a mem chunk. ("g_free" will most likely cause a seg
762 * fault...somewhere).
764 * Oh yeah, GMemChunk is an opaque data type. (You don't really
765 * want to know what's going on inside do you?)
768 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
769 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
770 * atom. (They are also useful for lists which use MemChunk to allocate
771 * memory but are also part of the MemChunk implementation).
772 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
775 #define G_ALLOC_ONLY 1
776 #define G_ALLOC_AND_FREE 2
778 GMemChunk* g_mem_chunk_new (gchar *name,
782 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
783 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
784 void g_mem_chunk_free (GMemChunk *mem_chunk,
786 void g_mem_chunk_clean (GMemChunk *mem_chunk);
787 void g_mem_chunk_reset (GMemChunk *mem_chunk);
788 void g_mem_chunk_print (GMemChunk *mem_chunk);
789 void g_mem_chunk_info (void);
791 /* Ah yes...we have a "g_blow_chunks" function.
792 * "g_blow_chunks" simply compresses all the chunks. This operation
793 * consists of freeing every memory area that should be freed (but
794 * which we haven't gotten around to doing yet). And, no,
795 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
796 * much better name than "g_mem_chunk_clean_all" or something
799 void g_blow_chunks (void);
804 GTimer* g_timer_new (void);
805 void g_timer_destroy (GTimer *timer);
806 void g_timer_start (GTimer *timer);
807 void g_timer_stop (GTimer *timer);
808 void g_timer_reset (GTimer *timer);
809 gdouble g_timer_elapsed (GTimer *timer,
810 gulong *microseconds);
815 void g_error (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
816 void g_warning (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
817 void g_message (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
818 void g_print (const gchar *format, ...) G_GNUC_PRINTF (1, 2);
821 /* String utility functions
823 #define G_STR_DELIMITERS "_-|> <."
824 void g_strdelimit (gchar *string,
825 const gchar *delimiters,
826 gchar new_delimiter);
827 gchar* g_strdup (const gchar *str);
828 gchar* g_strconcat (const gchar *string1,
829 ...); /* NULL terminated */
830 gdouble g_strtod (const gchar *nptr,
832 gchar* g_strerror (gint errnum);
833 gchar* g_strsignal (gint signum);
834 gint g_strcasecmp (const gchar *s1,
836 void g_strdown (gchar *string);
837 void g_strup (gchar *string);
838 void g_strreverse (gchar *string);
841 /* Retrive static info
843 gchar* g_get_user_name (void);
844 gchar* g_get_real_name (void);
845 gchar* g_get_home_dir (void);
846 gchar* g_get_tmp_dir (void);
847 gchar* g_get_prgname (void);
848 void g_set_prgname (const gchar *prgname);
851 /* Miscellaneous utility functions
853 guint g_parse_debug_string (const gchar *string,
856 gint g_snprintf (gchar *string,
859 ...) G_GNUC_PRINTF (3, 4);
860 gchar* g_basename (const gchar *file_name);
861 gchar* g_dirname (const gchar *file_name);
863 gchar* g_getcwd (void);
867 /* We make the assumption that if memmove isn't available, then
868 * bcopy will do the job. This isn't safe everywhere. (bcopy can't
869 * necessarily handle overlapping copies) */
871 #define g_memmove memmove
873 #define g_memmove(a,b,c) bcopy(b,a,c)
878 GErrorFunc g_set_error_handler (GErrorFunc func);
879 GWarningFunc g_set_warning_handler (GWarningFunc func);
880 GPrintFunc g_set_message_handler (GPrintFunc func);
881 GPrintFunc g_set_print_handler (GPrintFunc func);
883 void g_debug (const gchar *progname);
884 void g_attach_process (const gchar *progname,
886 void g_stack_trace (const gchar *progname,
893 GStringChunk* g_string_chunk_new (gint size);
894 void g_string_chunk_free (GStringChunk *chunk);
895 gchar* g_string_chunk_insert (GStringChunk *chunk,
896 const gchar *string);
897 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
898 const gchar *string);
902 GString* g_string_new (const gchar *init);
903 GString* g_string_sized_new (guint dfl_size);
904 void g_string_free (GString *string,
906 GString* g_string_assign (GString *lval,
908 GString* g_string_truncate (GString *string,
910 GString* g_string_append (GString *string,
912 GString* g_string_append_c (GString *string,
914 GString* g_string_prepend (GString *string,
916 GString* g_string_prepend_c (GString *string,
918 GString* g_string_insert (GString *string,
921 GString* g_string_insert_c (GString *string,
924 GString* g_string_erase (GString *string,
927 GString* g_string_down (GString *string);
928 GString* g_string_up (GString *string);
929 void g_string_sprintf (GString *string,
931 ...) G_GNUC_PRINTF (2, 3);
932 void g_string_sprintfa (GString *string,
934 ...) G_GNUC_PRINTF (2, 3);
938 #define g_array_length(array,type) \
939 (((array)->len)/sizeof(type))
940 #define g_array_append_val(array,type,val) \
941 g_rarray_append (array, (gpointer) &val, sizeof (type))
942 #define g_array_append_vals(array,type,vals,nvals) \
943 g_rarray_append (array, (gpointer) vals, sizeof (type) * nvals)
944 #define g_array_prepend_val(array,type,val) \
945 g_rarray_prepend (array, (gpointer) &val, sizeof (type))
946 #define g_array_prepend_vals(array,type,vals,nvals) \
947 g_rarray_prepend (array, (gpointer) vals, sizeof (type) * nvals)
948 #define g_array_truncate(array,type,length) \
949 g_rarray_truncate (array, length, sizeof (type))
950 #define g_array_index(array,type,index) \
951 ((type*) array->data)[index]
953 GArray* g_array_new (gint zero_terminated);
954 void g_array_free (GArray *array,
956 GArray* g_rarray_append (GArray *array,
959 GArray* g_rarray_prepend (GArray *array,
962 GArray* g_rarray_truncate (GArray *array,
966 /* Resizable pointer array. This interface is much less complicated
967 * than the above. Add appends appends a pointer. Remove fills any
968 * cleared spot and shortens the array.
971 #define g_ptr_array_index(array,index) (array->pdata)[index]
973 GPtrArray* g_ptr_array_new (void);
974 void g_ptr_array_free (GPtrArray *array,
976 void g_ptr_array_set_size (GPtrArray *array,
978 void g_ptr_array_remove_index (GPtrArray *array,
980 gboolean g_ptr_array_remove (GPtrArray *array,
982 void g_ptr_array_add (GPtrArray *array,
985 /* Byte arrays, an array of guint8. Implemented as a GArray,
989 GByteArray* g_byte_array_new (void);
990 void g_byte_array_free (GByteArray *array,
993 GByteArray* g_byte_array_append (GByteArray *array,
997 GByteArray* g_byte_array_prepend (GByteArray *array,
1001 GByteArray* g_byte_array_truncate (GByteArray *array,
1007 gint g_str_equal (gconstpointer v,
1009 guint g_str_hash (gconstpointer v);
1011 gint g_int_equal (gconstpointer v,
1013 guint g_int_hash (gconstpointer v);
1015 /* This "hash" function will just return the key's adress as an
1016 * unsigned integer. Useful for hashing on plain adresses or
1017 * simple integer values.
1019 guint g_direct_hash (gconstpointer v);
1020 gint g_direct_equal (gconstpointer v,
1024 /* Quarks (string<->id association)
1026 GQuark g_quark_try_string (const gchar *string);
1027 GQuark g_quark_from_static_string (const gchar *string);
1028 GQuark g_quark_from_string (const gchar *string);
1029 gchar* g_quark_to_string (GQuark quark);
1031 /* Location Associated Data
1033 void g_dataset_destroy (gconstpointer dataset_location);
1034 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1036 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1039 GDestroyNotify destroy_func);
1040 void g_dataset_id_set_destroy (gconstpointer dataset_location,
1042 GDestroyNotify destroy_func);
1044 #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
1045 #define g_dataset_id_remove_data(l,k) G_STMT_START{g_dataset_id_set_data((l),(k),NULL);}G_STMT_END
1046 #define g_dataset_get_data(l,k) (g_dataset_id_get_data((l),g_quark_try_string(k)))
1047 #define g_dataset_set_data_full(l,k,d,f) G_STMT_START{g_dataset_id_set_data_full((l),g_quark_from_string(k),(d),(f));}G_STMT_END
1048 #define g_dataset_set_destroy(l,k,f) G_STMT_START{g_dataset_id_set_destroy((l),g_quark_from_string(k),(f));}G_STMT_END
1049 #define g_dataset_set_data(l,k,d) G_STMT_START{g_dataset_set_data_full((l),(k),(d),NULL);}G_STMT_END
1050 #define g_dataset_remove_data(l,k) G_STMT_START{g_dataset_set_data((l),(k),NULL);}G_STMT_END
1053 /* GScanner: Flexible lexical scanner for general purpose.
1056 /* Character sets */
1057 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1058 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1059 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1060 "\307\310\311\312\313\314\315\316\317\320"\
1061 "\321\322\323\324\325\326"\
1062 "\330\331\332\333\334\335\336"
1063 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1064 "\347\350\351\352\353\354\355\356\357\360"\
1065 "\361\362\363\364\365\366"\
1066 "\370\371\372\373\374\375\376\377"
1073 G_ERR_UNEXP_EOF_IN_STRING,
1074 G_ERR_UNEXP_EOF_IN_COMMENT,
1075 G_ERR_NON_DIGIT_IN_CONST,
1078 G_ERR_FLOAT_MALFORMED
1086 G_TOKEN_LEFT_PAREN = '(',
1087 G_TOKEN_RIGHT_PAREN = ')',
1088 G_TOKEN_LEFT_CURLY = '{',
1089 G_TOKEN_RIGHT_CURLY = '}',
1090 G_TOKEN_LEFT_BRACE = '[',
1091 G_TOKEN_RIGHT_BRACE = ']',
1092 G_TOKEN_EQUAL_SIGN = '=',
1093 G_TOKEN_COMMA = ',',
1109 G_TOKEN_IDENTIFIER_NULL,
1111 G_TOKEN_COMMENT_SINGLE,
1112 G_TOKEN_COMMENT_MULTI,
1119 gchar *v_identifier;
1131 struct _GScannerConfig
1135 gchar *cset_skip_characters; /* default: " \t\n" */
1136 gchar *cset_identifier_first;
1137 gchar *cset_identifier_nth;
1138 gchar *cpair_comment_single; /* default: "#\n" */
1140 /* Should symbol lookup work case sensitive?
1142 guint case_sensitive : 1;
1144 /* Boolean values to be adjusted "on the fly"
1145 * to configure scanning behaviour.
1147 guint skip_comment_multi : 1; /* C like comment */
1148 guint skip_comment_single : 1; /* single line comment */
1149 guint scan_comment_multi : 1; /* scan multi line comments? */
1150 guint scan_identifier : 1;
1151 guint scan_identifier_1char : 1;
1152 guint scan_identifier_NULL : 1;
1153 guint scan_symbols : 1;
1154 guint scan_binary : 1;
1155 guint scan_octal : 1;
1156 guint scan_float : 1;
1157 guint scan_hex : 1; /* `0x0ff0' */
1158 guint scan_hex_dollar : 1; /* `$0ff0' */
1159 guint scan_string_sq : 1; /* string: 'anything' */
1160 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
1161 guint numbers_2_int : 1; /* bin, octal, hex => int */
1162 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
1163 guint identifier_2_string : 1;
1164 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
1165 guint symbol_2_token : 1;
1166 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
1173 guint max_parse_errors;
1175 /* g_scanner_error() increments this field */
1178 /* name of input stream, featured by the default message handler */
1179 const gchar *input_name;
1181 /* data pointer for derived structures */
1182 gpointer derived_data;
1184 /* link into the scanner configuration */
1185 GScannerConfig *config;
1187 /* fields filled in after g_scanner_get_next_token() */
1193 /* fields filled in after g_scanner_peek_next_token() */
1194 GTokenType next_token;
1197 guint next_position;
1199 /* to be considered private */
1200 GHashTable *symbol_table;
1207 /* handler function for _warn and _error */
1208 GScannerMsgFunc msg_handler;
1211 GScanner* g_scanner_new (GScannerConfig *config_templ);
1212 void g_scanner_destroy (GScanner *scanner);
1213 void g_scanner_input_file (GScanner *scanner,
1215 void g_scanner_input_text (GScanner *scanner,
1218 GTokenType g_scanner_get_next_token (GScanner *scanner);
1219 GTokenType g_scanner_peek_next_token (GScanner *scanner);
1220 GTokenType g_scanner_cur_token (GScanner *scanner);
1221 GValue g_scanner_cur_value (GScanner *scanner);
1222 guint g_scanner_cur_line (GScanner *scanner);
1223 guint g_scanner_cur_position (GScanner *scanner);
1224 gboolean g_scanner_eof (GScanner *scanner);
1225 guint g_scanner_set_scope (GScanner *scanner,
1227 void g_scanner_scope_add_symbol (GScanner *scanner,
1229 const gchar *symbol,
1231 void g_scanner_scope_remove_symbol (GScanner *scanner,
1233 const gchar *symbol);
1234 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
1236 const gchar *symbol);
1237 void g_scanner_scope_foreach_symbol (GScanner *scanner,
1240 gpointer func_data);
1241 gpointer g_scanner_lookup_symbol (GScanner *scanner,
1242 const gchar *symbol);
1243 void g_scanner_freeze_symbol_table (GScanner *scanner);
1244 void g_scanner_thaw_symbol_table (GScanner *scanner);
1245 void g_scanner_unexp_token (GScanner *scanner,
1246 GTokenType expected_token,
1247 const gchar *identifier_spec,
1248 const gchar *symbol_spec,
1249 const gchar *symbol_name,
1250 const gchar *message,
1252 void g_scanner_error (GScanner *scanner,
1253 const gchar *format,
1254 ...) G_GNUC_PRINTF (2,3);
1255 void g_scanner_warn (GScanner *scanner,
1256 const gchar *format,
1257 ...) G_GNUC_PRINTF (2,3);
1258 gint g_scanner_stat_mode (const gchar *filename);
1259 /* keep downward source compatibility */
1260 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
1261 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
1263 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
1264 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
1266 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
1267 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
1277 GCompletionFunc func;
1283 GCompletion* g_completion_new (GCompletionFunc func);
1284 void g_completion_add_items (GCompletion* cmp,
1286 void g_completion_remove_items (GCompletion* cmp,
1288 void g_completion_clear_items (GCompletion* cmp);
1289 GList* g_completion_complete (GCompletion* cmp,
1291 gchar** new_prefix);
1292 void g_completion_free (GCompletion* cmp);
1295 /* GRelation: Indexed Relations. Imagine a really simple table in a
1296 * database. Relations are not ordered. This data type is meant for
1297 * maintaining a N-way mapping.
1299 * g_relation_new() creates a relation with FIELDS fields
1301 * g_relation_destroy() frees all resources
1302 * g_tuples_destroy() frees the result of g_relation_select()
1304 * g_relation_index() indexes relation FIELD with the provided
1305 * equality and hash functions. this must be done before any
1306 * calls to insert are made.
1308 * g_relation_insert() inserts a new tuple. you are expected to
1309 * provide the right number of fields.
1311 * g_relation_delete() deletes all relations with KEY in FIELD
1312 * g_relation_select() returns ...
1313 * g_relation_count() counts ...
1316 GRelation* g_relation_new (gint fields);
1317 void g_relation_destroy (GRelation *relation);
1318 void g_relation_index (GRelation *relation,
1320 GHashFunc hash_func,
1321 GCompareFunc key_compare_func);
1322 void g_relation_insert (GRelation *relation,
1324 gint g_relation_delete (GRelation *relation,
1327 GTuples* g_relation_select (GRelation *relation,
1330 gint g_relation_count (GRelation *relation,
1333 gboolean g_relation_exists (GRelation *relation,
1335 void g_relation_print (GRelation *relation);
1337 void g_tuples_destroy (GTuples *tuples);
1338 gpointer g_tuples_index (GTuples *tuples,
1345 extern const guint glib_major_version;
1346 extern const guint glib_minor_version;
1347 extern const guint glib_micro_version;
1351 #endif /* __cplusplus */
1354 #endif /* __G_LIB_H__ */