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 glibconfig.h provides definitions for
23 * the extrema of many of the standard types. These are:
25 * G_MINSHORT, G_MAXSHORT
27 * G_MINLONG, G_MAXLONG
28 * G_MINFLOAT, G_MAXFLOAT
29 * G_MINDOUBLE, G_MAXDOUBLE
31 * It also provides the following typedefs:
38 * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
41 * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
42 * This is useful to pass an integer instead of a pointer to a callback.
44 * GINT_TO_POINTER(i), GUINT_TO_POINTER(i)
45 * GPOINTER_TO_INT(p), GPOINTER_TO_UINT(p)
47 * Finally, it provide the following wrappers to STDC functions:
50 * To register hooks which are executed on exit().
51 * Usually a wrapper for STDC atexit.
53 * void *g_memmove(void *dest, const void *src, guint count);
54 * A wrapper for STDC memmove, or an implementation, if memmove doesn't
55 * exist. The prototype looks like the above, give or take a const,
58 #include <glibconfig.h>
60 /* include varargs functions for assertment macros
64 /* optionally feature DMALLOC memory allocation debugger
73 /* On native Win32, directory separator is the backslash, and search path
74 * separator is the semicolon.
76 #define G_DIR_SEPARATOR '\\'
77 #define G_DIR_SEPARATOR_S "\\"
78 #define G_SEARCHPATH_SEPARATOR ';'
79 #define G_SEARCHPATH_SEPARATOR_S ";"
81 #else /* !NATIVE_WIN32 */
85 #define G_DIR_SEPARATOR '/'
86 #define G_DIR_SEPARATOR_S "/"
87 #define G_SEARCHPATH_SEPARATOR ':'
88 #define G_SEARCHPATH_SEPARATOR_S ":"
90 #endif /* !NATIVE_WIN32 */
93 /* Make MSVC more pedantic, this is a recommended pragma list
94 * from _Win32_Programming_ by Rector and Newcomer.
96 #pragma warning(error:4002)
97 #pragma warning(error:4003)
98 #pragma warning(1:4010)
99 #pragma warning(error:4013)
100 #pragma warning(1:4016)
101 #pragma warning(error:4020)
102 #pragma warning(error:4021)
103 #pragma warning(error:4027)
104 #pragma warning(error:4029)
105 #pragma warning(error:4033)
106 #pragma warning(error:4035)
107 #pragma warning(error:4045)
108 #pragma warning(error:4047)
109 #pragma warning(error:4049)
110 #pragma warning(error:4053)
111 #pragma warning(error:4071)
112 #pragma warning(disable:4101)
113 #pragma warning(error:4150)
115 #pragma warning(disable:4244) /* No possible loss of data warnings, please */
116 #endif /* _MSC_VER */
121 #endif /* __cplusplus */
124 /* Provide definitions for some commonly used macros.
125 * Some of them are only provided if they haven't already
126 * been defined. It is assumed that if they are already
127 * defined then the current definition is correct.
130 #define NULL ((void*) 0)
138 #define TRUE (!FALSE)
142 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
145 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
148 #define ABS(a) (((a) < 0) ? -(a) : (a))
151 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
154 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
155 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
157 #if !defined (G_VA_COPY)
158 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
159 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
160 # elif defined (G_VA_COPY_AS_ARRAY)
161 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
162 # else /* va_list is a pointer */
163 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
164 # endif /* va_list is a pointer */
165 #endif /* !G_VA_COPY */
168 /* Provide convenience macros for handling structure
169 * fields through their offsets.
171 #define G_STRUCT_OFFSET(struct_type, member) \
172 ((gulong) ((gchar*) &((struct_type*) 0)->member))
173 #define G_STRUCT_MEMBER_P(struct_p, struct_offset) \
174 ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
175 #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset) \
176 (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
179 /* inlining hassle. for compilers that don't allow the `inline' keyword,
180 * mostly because of strict ANSI C compliance or dumbness, we try to fall
181 * back to either `__inline__' or `__inline'.
182 * we define G_CAN_INLINE, if the compiler seems to be actually
183 * *capable* to do function inlining, in which case inline function bodys
184 * do make sense. we also define G_INLINE_FUNC to properly export the
185 * function prototypes if no inlinig can be performed.
186 * we special case most of the stuff, so inline functions can have a normal
187 * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
189 #ifndef G_INLINE_FUNC
190 # define G_CAN_INLINE 1
193 # if defined (__GNUC__) && defined (__STRICT_ANSI__)
195 # define inline __inline__
197 #else /* !G_HAVE_INLINE */
199 # if defined (G_HAVE___INLINE__)
200 # define inline __inline__
201 # else /* !inline && !__inline__ */
202 # if defined (G_HAVE___INLINE)
203 # define inline __inline
204 # else /* !inline && !__inline__ && !__inline */
205 # define inline /* don't inline, then */
206 # ifndef G_INLINE_FUNC
212 #ifndef G_INLINE_FUNC
215 # define G_INLINE_FUNC extern inline
218 # define G_INLINE_FUNC extern
220 # else /* !__GNUC__ */
222 # define G_INLINE_FUNC static inline
224 # define G_INLINE_FUNC extern
226 # endif /* !__GNUC__ */
227 #endif /* !G_INLINE_FUNC */
230 /* Provide simple macro statement wrappers (adapted from Perl):
231 * G_STMT_START { statements; } G_STMT_END;
232 * can be used as a single statement, as in
233 * if (x) G_STMT_START { ... } G_STMT_END; else ...
235 * For gcc we will wrap the statements within `({' and `})' braces.
236 * For SunOS they will be wrapped within `if (1)' and `else (void) 0',
237 * and otherwise within `do' and `while (0)'.
239 #if !(defined (G_STMT_START) && defined (G_STMT_END))
240 # if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
241 # define G_STMT_START (void)(
242 # define G_STMT_END )
244 # if (defined (sun) || defined (__sun__))
245 # define G_STMT_START if (1)
246 # define G_STMT_END else (void)0
248 # define G_STMT_START do
249 # define G_STMT_END while (0)
255 /* Provide macros to feature the GCC function attribute.
257 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
258 #define G_GNUC_PRINTF( format_idx, arg_idx ) \
259 __attribute__((format (printf, format_idx, arg_idx)))
260 #define G_GNUC_SCANF( format_idx, arg_idx ) \
261 __attribute__((format (scanf, format_idx, arg_idx)))
262 #define G_GNUC_FORMAT( arg_idx ) \
263 __attribute__((format_arg (arg_idx)))
264 #define G_GNUC_NORETURN \
265 __attribute__((noreturn))
266 #define G_GNUC_CONST \
267 __attribute__((const))
268 #define G_GNUC_UNUSED \
269 __attribute__((unused))
270 #else /* !__GNUC__ */
271 #define G_GNUC_PRINTF( format_idx, arg_idx )
272 #define G_GNUC_SCANF( format_idx, arg_idx )
273 #define G_GNUC_FORMAT( arg_idx )
274 #define G_GNUC_NORETURN
276 #define G_GNUC_UNUSED
277 #endif /* !__GNUC__ */
280 /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
281 * macros, so we can refer to them as strings unconditionally.
284 #define G_GNUC_FUNCTION (__FUNCTION__)
285 #define G_GNUC_PRETTY_FUNCTION (__PRETTY_FUNCTION__)
286 #else /* !__GNUC__ */
287 #define G_GNUC_FUNCTION ("")
288 #define G_GNUC_PRETTY_FUNCTION ("")
289 #endif /* !__GNUC__ */
291 /* we try to provide a usefull equivalent for ATEXIT if it is
292 * not defined, but use is actually abandoned. people should
293 * use g_atexit() instead.
296 # define ATEXIT(proc) g_ATEXIT(proc)
298 # define G_NATIVE_ATEXIT
301 /* Hacker macro to place breakpoints for x86 machines.
302 * Actual use is strongly deprecated of course ;)
304 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
305 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
306 #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
307 #define G_BREAKPOINT() G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
308 #else /* !__i386__ && !__alpha__ */
309 #define G_BREAKPOINT()
310 #endif /* __i386__ */
313 /* Provide macros for easily allocating memory. The macros
314 * will cast the allocated memory to the specified type
315 * in order to avoid compiler warnings. (Makes the code neater).
319 # define g_new(type, count) (ALLOC (type, count))
320 # define g_new0(type, count) (CALLOC (type, count))
321 # define g_renew(type, mem, count) (REALLOC (mem, type, count))
322 #else /* __DMALLOC_H__ */
323 # define g_new(type, count) \
324 ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
325 # define g_new0(type, count) \
326 ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
327 # define g_renew(type, mem, count) \
328 ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
329 #endif /* __DMALLOC_H__ */
331 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
332 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
334 sizeof (type) * (pre_alloc), \
337 #define g_chunk_new(type, chunk) ( \
338 (type *) g_mem_chunk_alloc (chunk) \
340 #define g_chunk_new0(type, chunk) ( \
341 (type *) g_mem_chunk_alloc0 (chunk) \
343 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
344 g_mem_chunk_free ((mem_chunk), (mem)); \
348 #define g_string(x) #x
351 /* Provide macros for error handling. The "assert" macros will
352 * exit on failure. The "return" macros will exit the current
353 * function. Two different definitions are given for the macros
354 * if G_DISABLE_ASSERT is not defined, in order to support gcc's
355 * __PRETTY_FUNCTION__ capability.
358 #ifdef G_DISABLE_ASSERT
360 #define g_assert(expr)
361 #define g_assert_not_reached()
363 #else /* !G_DISABLE_ASSERT */
367 #define g_assert(expr) G_STMT_START{ \
369 g_log (G_LOG_DOMAIN, \
371 "file %s: line %d (%s): assertion failed: (%s)", \
374 __PRETTY_FUNCTION__, \
377 #define g_assert_not_reached() G_STMT_START{ \
378 g_log (G_LOG_DOMAIN, \
380 "file %s: line %d (%s): should not be reached", \
383 __PRETTY_FUNCTION__); }G_STMT_END
385 #else /* !__GNUC__ */
387 #define g_assert(expr) G_STMT_START{ \
389 g_log (G_LOG_DOMAIN, \
391 "file %s: line %d: assertion failed: (%s)", \
396 #define g_assert_not_reached() G_STMT_START{ \
397 g_log (G_LOG_DOMAIN, \
399 "file %s: line %d: should not be reached", \
401 __LINE__); }G_STMT_END
403 #endif /* __GNUC__ */
405 #endif /* !G_DISABLE_ASSERT */
408 #ifdef G_DISABLE_CHECKS
410 #define g_return_if_fail(expr)
411 #define g_return_val_if_fail(expr,val)
413 #else /* !G_DISABLE_CHECKS */
417 #define g_return_if_fail(expr) G_STMT_START{ \
420 g_log (G_LOG_DOMAIN, \
421 G_LOG_LEVEL_CRITICAL, \
422 "file %s: line %d (%s): assertion `%s' failed.", \
425 __PRETTY_FUNCTION__, \
430 #define g_return_val_if_fail(expr,val) G_STMT_START{ \
433 g_log (G_LOG_DOMAIN, \
434 G_LOG_LEVEL_CRITICAL, \
435 "file %s: line %d (%s): assertion `%s' failed.", \
438 __PRETTY_FUNCTION__, \
443 #else /* !__GNUC__ */
445 #define g_return_if_fail(expr) G_STMT_START{ \
448 g_log (G_LOG_DOMAIN, \
449 G_LOG_LEVEL_CRITICAL, \
450 "file %s: line %d: assertion `%s' failed.", \
457 #define g_return_val_if_fail(expr, val) G_STMT_START{ \
460 g_log (G_LOG_DOMAIN, \
461 G_LOG_LEVEL_CRITICAL, \
462 "file %s: line %d: assertion `%s' failed.", \
469 #endif /* !__GNUC__ */
471 #endif /* !G_DISABLE_CHECKS */
474 /* Provide type definitions for commonly used types.
475 * These are useful because a "gint8" can be adjusted
476 * to be 1 byte (8 bits) on all platforms. Similarly and
477 * more importantly, "gint32" can be adjusted to be
478 * 4 bytes (32 bits) on all platforms.
482 typedef short gshort;
485 typedef gint gboolean;
487 typedef unsigned char guchar;
488 typedef unsigned short gushort;
489 typedef unsigned long gulong;
490 typedef unsigned int guint;
492 typedef float gfloat;
493 typedef double gdouble;
495 /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
496 * Since gldouble isn't used anywhere, just disable it for now */
499 #ifdef HAVE_LONG_DOUBLE
500 typedef long double gldouble;
501 #else /* HAVE_LONG_DOUBLE */
502 typedef double gldouble;
503 #endif /* HAVE_LONG_DOUBLE */
506 typedef void* gpointer;
507 typedef const void *gconstpointer;
510 typedef gint32 gssize;
511 typedef guint32 gsize;
512 typedef guint32 GQuark;
513 typedef gint32 GTime;
516 /* Portable endian checks and conversions
519 #define G_LITTLE_ENDIAN 1234
520 #define G_BIG_ENDIAN 4321
521 #define G_PDP_ENDIAN 3412 /* unused, need specific PDP check */
523 /* Basic bit swapping functions
525 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((guint16) ( \
526 (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
527 (((guint16) (val) & (guint16) 0xff00U) >> 8)))
528 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((guint32) ( \
529 (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
530 (((guint32) (val) & (guint32) 0x0000ff00U) << 8) | \
531 (((guint32) (val) & (guint32) 0x00ff0000U) >> 8) | \
532 (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
534 /* Intel specific stuff for speed
536 #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
538 # define GUINT16_SWAP_LE_BE_X86(val) \
540 ({ register guint16 __v; \
541 if (__builtin_constant_p (val)) \
542 __v = GUINT16_SWAP_LE_BE_CONSTANT (val); \
544 __asm__ __const__ ("rorw $8, %w0" \
546 : "0" ((guint16) (val))); \
549 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
551 # if !defined(__i486__) && !defined(__i586__) \
552 && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
553 # define GUINT32_SWAP_LE_BE_X86(val) \
555 ({ register guint32 __v; \
556 if (__builtin_constant_p (val)) \
557 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
559 __asm__ __const__ ("rorw $8, %w0\n\t" \
563 : "0" ((guint32) (val))); \
566 # else /* 486 and higher has bswap */
567 # define GUINT32_SWAP_LE_BE_X86(val) \
569 ({ register guint32 __v; \
570 if (__builtin_constant_p (val)) \
571 __v = GUINT32_SWAP_LE_BE_CONSTANT (val); \
573 __asm__ __const__ ("bswap %0" \
575 : "0" ((guint32) (val))); \
577 # endif /* processor specific 32-bit stuff */
579 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
581 #else /* !__i386__ */
582 # define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
583 # define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
584 #endif /* __i386__ */
587 # define GUINT64_SWAP_LE_BE_CONSTANT(val) ((guint64) ( \
588 (((guint64) (val) & \
589 (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) | \
590 (((guint64) (val) & \
591 (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) | \
592 (((guint64) (val) & \
593 (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
594 (((guint64) (val) & \
595 (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) << 8) | \
596 (((guint64) (val) & \
597 (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
598 (((guint64) (val) & \
599 (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
600 (((guint64) (val) & \
601 (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
602 (((guint64) (val) & \
603 (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
605 # if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
606 # define GUINT64_SWAP_LE_BE_X86(val) \
608 ({ union { guint64 __ll; \
609 guint32 __l[2]; } __r; \
610 if (__builtin_constant_p (val)) \
611 __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val); \
614 union { guint64 __ll; \
615 guint32 __l[2]; } __w; \
616 __w.__ll = ((guint64) val); \
617 __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]); \
618 __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]); \
622 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
624 # else /* !__i386__ */
625 # define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
629 #define GUINT16_SWAP_LE_PDP(val) ((guint16) (val))
630 #define GUINT16_SWAP_BE_PDP(val) (GUINT16_SWAP_LE_BE (val))
631 #define GUINT32_SWAP_LE_PDP(val) ((guint32) ( \
632 (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
633 (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
634 #define GUINT32_SWAP_BE_PDP(val) ((guint32) ( \
635 (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
636 (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
638 /* The TO_?E stuff is defined in glibconfig.h. The transformation is symmetric,
639 so the FROM just maps to the TO.
641 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
642 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
643 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
644 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
645 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
646 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
647 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
648 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
651 #define GINT64_FROM_LE(val) (GINT32_TO_LE (val))
652 #define GUINT64_FROM_LE(val) (GUINT32_TO_LE (val))
653 #define GINT64_FROM_BE(val) (GINT32_TO_BE (val))
654 #define GUINT64_FROM_BE(val) (GUINT32_TO_BE (val))
657 #define GLONG_FROM_LE(val) (GLONG_TO_LE (val))
658 #define GULONG_FROM_LE(val) (GULONG_TO_LE (val))
659 #define GLONG_FROM_BE(val) (GLONG_TO_BE (val))
660 #define GULONG_FROM_BE(val) (GULONG_TO_BE (val))
662 #define GINT_FROM_LE(val) (GINT_TO_LE (val))
663 #define GUINT_FROM_LE(val) (GUINT_TO_LE (val))
664 #define GINT_FROM_BE(val) (GINT_TO_BE (val))
665 #define GUINT_FROM_BE(val) (GUINT_TO_BE (val))
667 /* Portable versions of host-network order stuff
669 #define g_ntohl(val) (GUINT32_FROM_BE (val))
670 #define g_ntohs(val) (GUINT16_FROM_BE (val))
671 #define g_htonl(val) (GUINT32_TO_BE (val))
672 #define g_htons(val) (GUINT16_TO_BE (val))
676 * we prefix variable declarations so they can
677 * properly get exported in windows dlls.
680 # ifdef GLIB_COMPILATION
681 # define GUTILS_C_VAR __declspec(dllexport)
682 # else /* !GLIB_COMPILATION */
683 # define GUTILS_C_VAR __declspec(dllimport)
684 # endif /* !GLIB_COMPILATION */
685 #else /* !NATIVE_WIN32 */
686 # define GUTILS_C_VAR extern
687 #endif /* !NATIVE_WIN32 */
689 GUTILS_C_VAR const guint glib_major_version;
690 GUTILS_C_VAR const guint glib_minor_version;
691 GUTILS_C_VAR const guint glib_micro_version;
692 GUTILS_C_VAR const guint glib_interface_age;
693 GUTILS_C_VAR const guint glib_binary_age;
695 /* Forward declarations of glib types.
698 typedef struct _GArray GArray;
699 typedef struct _GByteArray GByteArray;
700 typedef struct _GCache GCache;
701 typedef struct _GCompletion GCompletion;
702 typedef struct _GData GData;
703 typedef struct _GDebugKey GDebugKey;
704 typedef struct _GHashTable GHashTable;
705 typedef struct _GHook GHook;
706 typedef struct _GHookList GHookList;
707 typedef struct _GList GList;
708 typedef struct _GListAllocator GListAllocator;
709 typedef struct _GMemChunk GMemChunk;
710 typedef struct _GNode GNode;
711 typedef struct _GPtrArray GPtrArray;
712 typedef struct _GRelation GRelation;
713 typedef struct _GScanner GScanner;
714 typedef struct _GScannerConfig GScannerConfig;
715 typedef struct _GSList GSList;
716 typedef struct _GString GString;
717 typedef struct _GStringChunk GStringChunk;
718 typedef struct _GTimer GTimer;
719 typedef struct _GTree GTree;
720 typedef struct _GTuples GTuples;
721 typedef union _GTokenValue GTokenValue;
722 typedef struct _GIOChannel GIOChannel;
727 G_TRAVERSE_LEAFS = 1 << 0,
728 G_TRAVERSE_NON_LEAFS = 1 << 1,
729 G_TRAVERSE_ALL = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
730 G_TRAVERSE_MASK = 0x03
741 /* Log level shift offset for user defined
742 * log levels (0-7 are used by GLib).
744 #define G_LOG_LEVEL_USER_SHIFT (8)
746 /* Glib log levels and flags.
751 G_LOG_FLAG_RECURSION = 1 << 0,
752 G_LOG_FLAG_FATAL = 1 << 1,
754 /* GLib log levels */
755 G_LOG_LEVEL_ERROR = 1 << 2, /* always fatal */
756 G_LOG_LEVEL_CRITICAL = 1 << 3,
757 G_LOG_LEVEL_WARNING = 1 << 4,
758 G_LOG_LEVEL_MESSAGE = 1 << 5,
759 G_LOG_LEVEL_INFO = 1 << 6,
760 G_LOG_LEVEL_DEBUG = 1 << 7,
762 G_LOG_LEVEL_MASK = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
765 /* GLib log levels that are considered fatal by default */
766 #define G_LOG_FATAL_MASK (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
769 typedef gpointer (*GCacheNewFunc) (gpointer key);
770 typedef gpointer (*GCacheDupFunc) (gpointer value);
771 typedef void (*GCacheDestroyFunc) (gpointer value);
772 typedef gint (*GCompareFunc) (gconstpointer a,
774 typedef gchar* (*GCompletionFunc) (gpointer);
775 typedef void (*GDestroyNotify) (gpointer data);
776 typedef void (*GDataForeachFunc) (GQuark key_id,
779 typedef void (*GFunc) (gpointer data,
781 typedef guint (*GHashFunc) (gconstpointer key);
782 typedef void (*GHFunc) (gpointer key,
785 typedef gboolean (*GHRFunc) (gpointer key,
788 typedef gint (*GHookCompareFunc) (GHook *new_hook,
790 typedef gboolean (*GHookFindFunc) (GHook *hook,
792 typedef void (*GHookMarshaller) (GHook *hook,
794 typedef void (*GHookFunc) (gpointer data);
795 typedef gboolean (*GHookCheckFunc) (gpointer data);
796 typedef void (*GHookFreeFunc) (GHookList *hook_list,
798 typedef void (*GLogFunc) (const gchar *log_domain,
799 GLogLevelFlags log_level,
800 const gchar *message,
802 typedef gboolean (*GNodeTraverseFunc) (GNode *node,
804 typedef void (*GNodeForeachFunc) (GNode *node,
806 typedef gint (*GSearchFunc) (gpointer key,
808 typedef void (*GScannerMsgFunc) (GScanner *scanner,
811 typedef gint (*GTraverseFunc) (gpointer key,
814 typedef void (*GVoidFunc) (void);
866 /* Doubly linked lists
868 GList* g_list_alloc (void);
869 void g_list_free (GList *list);
870 void g_list_free_1 (GList *list);
871 GList* g_list_append (GList *list,
873 GList* g_list_prepend (GList *list,
875 GList* g_list_insert (GList *list,
878 GList* g_list_insert_sorted (GList *list,
881 GList* g_list_concat (GList *list1,
883 GList* g_list_remove (GList *list,
885 GList* g_list_remove_link (GList *list,
887 GList* g_list_reverse (GList *list);
888 GList* g_list_nth (GList *list,
890 GList* g_list_find (GList *list,
892 GList* g_list_find_custom (GList *list,
895 gint g_list_position (GList *list,
897 gint g_list_index (GList *list,
899 GList* g_list_last (GList *list);
900 GList* g_list_first (GList *list);
901 guint g_list_length (GList *list);
902 void g_list_foreach (GList *list,
905 gpointer g_list_nth_data (GList *list,
907 #define g_list_previous(list) ((list) ? (((GList *)(list))->prev) : NULL)
908 #define g_list_next(list) ((list) ? (((GList *)(list))->next) : NULL)
911 /* Singly linked lists
913 GSList* g_slist_alloc (void);
914 void g_slist_free (GSList *list);
915 void g_slist_free_1 (GSList *list);
916 GSList* g_slist_append (GSList *list,
918 GSList* g_slist_prepend (GSList *list,
920 GSList* g_slist_insert (GSList *list,
923 GSList* g_slist_insert_sorted (GSList *list,
926 GSList* g_slist_concat (GSList *list1,
928 GSList* g_slist_remove (GSList *list,
930 GSList* g_slist_remove_link (GSList *list,
932 GSList* g_slist_reverse (GSList *list);
933 GSList* g_slist_nth (GSList *list,
935 GSList* g_slist_find (GSList *list,
937 GSList* g_slist_find_custom (GSList *list,
940 gint g_slist_position (GSList *list,
942 gint g_slist_index (GSList *list,
944 GSList* g_slist_last (GSList *list);
945 guint g_slist_length (GSList *list);
946 void g_slist_foreach (GSList *list,
949 gpointer g_slist_nth_data (GSList *list,
951 #define g_slist_next(slist) ((slist) ? (((GSList *)(slist))->next) : NULL)
956 GListAllocator* g_list_allocator_new (void);
957 void g_list_allocator_free (GListAllocator* allocator);
958 GListAllocator* g_slist_set_allocator (GListAllocator* allocator);
959 GListAllocator* g_list_set_allocator (GListAllocator* allocator);
964 GHashTable* g_hash_table_new (GHashFunc hash_func,
965 GCompareFunc key_compare_func);
966 void g_hash_table_destroy (GHashTable *hash_table);
967 void g_hash_table_insert (GHashTable *hash_table,
970 void g_hash_table_remove (GHashTable *hash_table,
972 gpointer g_hash_table_lookup (GHashTable *hash_table,
974 gboolean g_hash_table_lookup_extended(GHashTable *hash_table,
975 gconstpointer lookup_key,
978 void g_hash_table_freeze (GHashTable *hash_table);
979 void g_hash_table_thaw (GHashTable *hash_table);
980 void g_hash_table_foreach (GHashTable *hash_table,
983 gint g_hash_table_foreach_remove (GHashTable *hash_table,
986 gint g_hash_table_size (GHashTable *hash_table);
991 GCache* g_cache_new (GCacheNewFunc value_new_func,
992 GCacheDestroyFunc value_destroy_func,
993 GCacheDupFunc key_dup_func,
994 GCacheDestroyFunc key_destroy_func,
995 GHashFunc hash_key_func,
996 GHashFunc hash_value_func,
997 GCompareFunc key_compare_func);
998 void g_cache_destroy (GCache *cache);
999 gpointer g_cache_insert (GCache *cache,
1001 void g_cache_remove (GCache *cache,
1003 void g_cache_key_foreach (GCache *cache,
1005 gpointer user_data);
1006 void g_cache_value_foreach (GCache *cache,
1008 gpointer user_data);
1011 /* Balanced binary trees
1013 GTree* g_tree_new (GCompareFunc key_compare_func);
1014 void g_tree_destroy (GTree *tree);
1015 void g_tree_insert (GTree *tree,
1018 void g_tree_remove (GTree *tree,
1020 gpointer g_tree_lookup (GTree *tree,
1022 void g_tree_traverse (GTree *tree,
1023 GTraverseFunc traverse_func,
1024 GTraverseType traverse_type,
1026 gpointer g_tree_search (GTree *tree,
1027 GSearchFunc search_func,
1029 gint g_tree_height (GTree *tree);
1030 gint g_tree_nnodes (GTree *tree);
1034 /* N-way tree implementation
1045 #define G_NODE_IS_ROOT(node) (((GNode*) (node))->parent == NULL && \
1046 ((GNode*) (node))->prev == NULL && \
1047 ((GNode*) (node))->next == NULL)
1048 #define G_NODE_IS_LEAF(node) (((GNode*) (node))->children == NULL)
1050 GNode* g_node_new (gpointer data);
1051 void g_node_destroy (GNode *root);
1052 void g_node_unlink (GNode *node);
1053 GNode* g_node_insert (GNode *parent,
1056 GNode* g_node_insert_before (GNode *parent,
1059 GNode* g_node_prepend (GNode *parent,
1061 guint g_node_n_nodes (GNode *root,
1062 GTraverseFlags flags);
1063 GNode* g_node_get_root (GNode *node);
1064 gboolean g_node_is_ancestor (GNode *node,
1066 guint g_node_depth (GNode *node);
1067 GNode* g_node_find (GNode *root,
1068 GTraverseType order,
1069 GTraverseFlags flags,
1072 /* convenience macros */
1073 #define g_node_append(parent, node) \
1074 g_node_insert_before ((parent), NULL, (node))
1075 #define g_node_insert_data(parent, position, data) \
1076 g_node_insert ((parent), (position), g_node_new (data))
1077 #define g_node_insert_data_before(parent, sibling, data) \
1078 g_node_insert_before ((parent), (sibling), g_node_new (data))
1079 #define g_node_prepend_data(parent, data) \
1080 g_node_prepend ((parent), g_node_new (data))
1081 #define g_node_append_data(parent, data) \
1082 g_node_insert_before ((parent), NULL, g_node_new (data))
1084 /* traversal function, assumes that `node' is root
1085 * (only traverses `node' and its subtree).
1086 * this function is just a high level interface to
1087 * low level traversal functions, optimized for speed.
1089 void g_node_traverse (GNode *root,
1090 GTraverseType order,
1091 GTraverseFlags flags,
1093 GNodeTraverseFunc func,
1096 /* return the maximum tree height starting with `node', this is an expensive
1097 * operation, since we need to visit all nodes. this could be shortened by
1098 * adding `guint height' to struct _GNode, but then again, this is not very
1099 * often needed, and would make g_node_insert() more time consuming.
1101 guint g_node_max_height (GNode *root);
1103 void g_node_children_foreach (GNode *node,
1104 GTraverseFlags flags,
1105 GNodeForeachFunc func,
1107 void g_node_reverse_children (GNode *node);
1108 guint g_node_n_children (GNode *node);
1109 GNode* g_node_nth_child (GNode *node,
1111 GNode* g_node_last_child (GNode *node);
1112 GNode* g_node_find_child (GNode *node,
1113 GTraverseFlags flags,
1115 gint g_node_child_position (GNode *node,
1117 gint g_node_child_index (GNode *node,
1120 GNode* g_node_first_sibling (GNode *node);
1121 GNode* g_node_last_sibling (GNode *node);
1123 #define g_node_prev_sibling(node) ((node) ? \
1124 ((GNode*) (node))->prev : NULL)
1125 #define g_node_next_sibling(node) ((node) ? \
1126 ((GNode*) (node))->next : NULL)
1127 #define g_node_first_child(node) ((node) ? \
1128 ((GNode*) (node))->children : NULL)
1131 /* Callback maintenance functions
1133 #define G_HOOK_FLAG_USER_SHIFT (4)
1136 G_HOOK_FLAG_ACTIVE = 1 << 0,
1137 G_HOOK_FLAG_IN_CALL = 1 << 1,
1138 G_HOOK_FLAG_MASK = 0x0f
1147 GMemChunk *hook_memchunk;
1148 GHookFreeFunc hook_free; /* virtual function */
1160 GDestroyNotify destroy;
1163 #define G_HOOK_ACTIVE(hook) ((((GHook*) hook)->flags & \
1164 G_HOOK_FLAG_ACTIVE) != 0)
1165 #define G_HOOK_IN_CALL(hook) ((((GHook*) hook)->flags & \
1166 G_HOOK_FLAG_IN_CALL) != 0)
1167 #define G_HOOK_IS_VALID(hook) (((GHook*) hook)->hook_id != 0 && \
1168 G_HOOK_ACTIVE (hook))
1169 #define G_HOOK_IS_UNLINKED(hook) (((GHook*) hook)->next == NULL && \
1170 ((GHook*) hook)->prev == NULL && \
1171 ((GHook*) hook)->hook_id == 0 && \
1172 ((GHook*) hook)->ref_count == 0)
1174 void g_hook_list_init (GHookList *hook_list,
1176 void g_hook_list_clear (GHookList *hook_list);
1177 GHook* g_hook_alloc (GHookList *hook_list);
1178 void g_hook_free (GHookList *hook_list,
1180 void g_hook_ref (GHookList *hook_list,
1182 void g_hook_unref (GHookList *hook_list,
1184 gboolean g_hook_destroy (GHookList *hook_list,
1186 void g_hook_destroy_link (GHookList *hook_list,
1188 void g_hook_prepend (GHookList *hook_list,
1190 void g_hook_insert_before (GHookList *hook_list,
1193 void g_hook_insert_sorted (GHookList *hook_list,
1195 GHookCompareFunc func);
1196 GHook* g_hook_get (GHookList *hook_list,
1198 GHook* g_hook_find (GHookList *hook_list,
1199 gboolean need_valids,
1202 GHook* g_hook_find_data (GHookList *hook_list,
1203 gboolean need_valids,
1205 GHook* g_hook_find_func (GHookList *hook_list,
1206 gboolean need_valids,
1208 GHook* g_hook_find_func_data (GHookList *hook_list,
1209 gboolean need_valids,
1212 GHook* g_hook_first_valid (GHookList *hook_list,
1213 gboolean may_be_in_call);
1214 GHook* g_hook_next_valid (GHook *hook,
1215 gboolean may_be_in_call);
1217 /* GHookCompareFunc implementation to insert hooks sorted by their id */
1218 gint g_hook_compare_ids (GHook *new_hook,
1221 /* convenience macros */
1222 #define g_hook_append( hook_list, hook ) \
1223 g_hook_insert_before ((hook_list), NULL, (hook))
1225 /* invoke all valid hooks with the (*GHookFunc) signature.
1227 void g_hook_list_invoke (GHookList *hook_list,
1228 gboolean may_recurse);
1229 /* invoke all valid hooks with the (*GHookCheckFunc) signature,
1230 * and destroy the hook if FALSE is returned.
1232 void g_hook_list_invoke_check (GHookList *hook_list,
1233 gboolean may_recurse);
1234 /* invoke a marshaller on all valid hooks.
1236 void g_hook_list_marshal (GHookList *hook_list,
1237 gboolean may_recurse,
1238 GHookMarshaller marshaller,
1242 /* Fatal error handlers.
1243 * g_on_error_query() will prompt the user to either
1244 * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
1245 * g_on_error_stack_trace() invokes gdb, which attaches to the current
1246 * process and shows a stack trace.
1247 * These function may cause different actions on non-unix platforms.
1248 * The prg_name arg is required by gdb to find the executable, if it is
1249 * passed as NULL, g_on_error_query() will try g_get_prgname().
1251 void g_on_error_query (const gchar *prg_name);
1252 void g_on_error_stack_trace (const gchar *prg_name);
1255 /* Logging mechanism
1257 extern const gchar *g_log_domain_glib;
1258 guint g_log_set_handler (const gchar *log_domain,
1259 GLogLevelFlags log_levels,
1261 gpointer user_data);
1262 void g_log_remove_handler (const gchar *log_domain,
1264 void g_log_default_handler (const gchar *log_domain,
1265 GLogLevelFlags log_level,
1266 const gchar *message,
1267 gpointer unused_data);
1268 void g_log (const gchar *log_domain,
1269 GLogLevelFlags log_level,
1270 const gchar *format,
1271 ...) G_GNUC_PRINTF (3, 4);
1272 void g_logv (const gchar *log_domain,
1273 GLogLevelFlags log_level,
1274 const gchar *format,
1276 GLogLevelFlags g_log_set_fatal_mask (const gchar *log_domain,
1277 GLogLevelFlags fatal_mask);
1278 GLogLevelFlags g_log_set_always_fatal (GLogLevelFlags fatal_mask);
1279 #ifndef G_LOG_DOMAIN
1280 #define G_LOG_DOMAIN (NULL)
1281 #endif /* G_LOG_DOMAIN */
1283 #define g_error(format, args...) g_log (G_LOG_DOMAIN, \
1284 G_LOG_LEVEL_ERROR, \
1286 #define g_message(format, args...) g_log (G_LOG_DOMAIN, \
1287 G_LOG_LEVEL_MESSAGE, \
1289 #define g_warning(format, args...) g_log (G_LOG_DOMAIN, \
1290 G_LOG_LEVEL_WARNING, \
1292 #else /* !__GNUC__ */
1294 g_error (const gchar *format,
1298 va_start (args, format);
1299 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
1303 g_message (const gchar *format,
1307 va_start (args, format);
1308 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
1312 g_warning (const gchar *format,
1316 va_start (args, format);
1317 g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
1320 #endif /* !__GNUC__ */
1322 typedef void (*GPrintFunc) (const gchar *string);
1323 void g_print (const gchar *format,
1324 ...) G_GNUC_PRINTF (1, 2);
1325 GPrintFunc g_set_print_handler (GPrintFunc func);
1326 void g_printerr (const gchar *format,
1327 ...) G_GNUC_PRINTF (1, 2);
1328 GPrintFunc g_set_printerr_handler (GPrintFunc func);
1330 /* deprecated compatibility functions, use g_log_set_handler() instead */
1331 typedef void (*GErrorFunc) (const gchar *str);
1332 typedef void (*GWarningFunc) (const gchar *str);
1333 GErrorFunc g_set_error_handler (GErrorFunc func);
1334 GWarningFunc g_set_warning_handler (GWarningFunc func);
1335 GPrintFunc g_set_message_handler (GPrintFunc func);
1338 /* Memory allocation and debugging
1342 #define g_malloc(size) ((gpointer) MALLOC (size))
1343 #define g_malloc0(size) ((gpointer) CALLOC (char, size))
1344 #define g_realloc(mem,size) ((gpointer) REALLOC (mem, char, size))
1345 #define g_free(mem) FREE (mem)
1347 #else /* !USE_DMALLOC */
1349 gpointer g_malloc (gulong size);
1350 gpointer g_malloc0 (gulong size);
1351 gpointer g_realloc (gpointer mem,
1353 void g_free (gpointer mem);
1355 #endif /* !USE_DMALLOC */
1357 void g_mem_profile (void);
1358 void g_mem_check (gpointer mem);
1361 /* "g_mem_chunk_new" creates a new memory chunk.
1362 * Memory chunks are used to allocate pieces of memory which are
1363 * always the same size. Lists are a good example of such a data type.
1364 * The memory chunk allocates and frees blocks of memory as needed.
1365 * Just be sure to call "g_mem_chunk_free" and not "g_free" on data
1366 * allocated in a mem chunk. ("g_free" will most likely cause a seg
1367 * fault...somewhere).
1369 * Oh yeah, GMemChunk is an opaque data type. (You don't really
1370 * want to know what's going on inside do you?)
1373 /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
1374 * is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
1375 * atom. (They are also useful for lists which use MemChunk to allocate
1376 * memory but are also part of the MemChunk implementation).
1377 * ALLOC_AND_FREE MemChunk's can allocate and free memory.
1380 #define G_ALLOC_ONLY 1
1381 #define G_ALLOC_AND_FREE 2
1383 GMemChunk* g_mem_chunk_new (gchar *name,
1387 void g_mem_chunk_destroy (GMemChunk *mem_chunk);
1388 gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
1389 gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
1390 void g_mem_chunk_free (GMemChunk *mem_chunk,
1392 void g_mem_chunk_clean (GMemChunk *mem_chunk);
1393 void g_mem_chunk_reset (GMemChunk *mem_chunk);
1394 void g_mem_chunk_print (GMemChunk *mem_chunk);
1395 void g_mem_chunk_info (void);
1397 /* Ah yes...we have a "g_blow_chunks" function.
1398 * "g_blow_chunks" simply compresses all the chunks. This operation
1399 * consists of freeing every memory area that should be freed (but
1400 * which we haven't gotten around to doing yet). And, no,
1401 * "g_blow_chunks" doesn't follow the naming scheme, but it is a
1402 * much better name than "g_mem_chunk_clean_all" or something
1405 void g_blow_chunks (void);
1410 GTimer* g_timer_new (void);
1411 void g_timer_destroy (GTimer *timer);
1412 void g_timer_start (GTimer *timer);
1413 void g_timer_stop (GTimer *timer);
1414 void g_timer_reset (GTimer *timer);
1415 gdouble g_timer_elapsed (GTimer *timer,
1416 gulong *microseconds);
1419 /* String utility functions that modify a string argument or
1420 * return a constant string that must not be freed.
1422 #define G_STR_DELIMITERS "_-|> <."
1423 gchar* g_strdelimit (gchar *string,
1424 const gchar *delimiters,
1425 gchar new_delimiter);
1426 gdouble g_strtod (const gchar *nptr,
1428 gchar* g_strerror (gint errnum);
1429 gchar* g_strsignal (gint signum);
1430 gint g_strcasecmp (const gchar *s1,
1432 gint g_strncasecmp (const gchar *s1,
1435 void g_strdown (gchar *string);
1436 void g_strup (gchar *string);
1437 void g_strreverse (gchar *string);
1438 /* removes leading spaces */
1439 gchar* g_strchug (gchar *string);
1440 /* removes trailing spaces */
1441 gchar* g_strchomp (gchar *string);
1442 /* removes leading & trailing spaces */
1443 #define g_strstrip( string ) g_strchomp (g_strchug (string))
1445 /* String utility functions that return a newly allocated string which
1446 * ought to be freed from the caller at some point.
1448 gchar* g_strdup (const gchar *str);
1449 gchar* g_strdup_printf (const gchar *format,
1450 ...) G_GNUC_PRINTF (1, 2);
1451 gchar* g_strdup_vprintf (const gchar *format,
1453 gchar* g_strndup (const gchar *str,
1455 gchar* g_strnfill (guint length,
1457 gchar* g_strconcat (const gchar *string1,
1458 ...); /* NULL terminated */
1459 gchar* g_strjoin (const gchar *separator,
1460 ...); /* NULL terminated */
1461 gchar* g_strescape (gchar *string);
1462 gpointer g_memdup (gconstpointer mem,
1465 /* NULL terminated string arrays.
1466 * g_strsplit() splits up string into max_tokens tokens at delim and
1467 * returns a newly allocated string array.
1468 * g_strjoinv() concatenates all of str_array's strings, sliding in an
1469 * optional separator, the returned string is newly allocated.
1470 * g_strfreev() frees the array itself and all of its strings.
1472 gchar** g_strsplit (const gchar *string,
1473 const gchar *delimiter,
1475 gchar* g_strjoinv (const gchar *separator,
1477 void g_strfreev (gchar **str_array);
1481 /* calculate a string size, guarranteed to fit format + args.
1483 guint g_printf_string_upper_bound (const gchar* format,
1487 /* Retrive static string info
1489 gchar* g_get_user_name (void);
1490 gchar* g_get_real_name (void);
1491 gchar* g_get_home_dir (void);
1492 gchar* g_get_tmp_dir (void);
1493 gchar* g_get_prgname (void);
1494 void g_set_prgname (const gchar *prgname);
1497 /* Miscellaneous utility functions
1499 guint g_parse_debug_string (const gchar *string,
1502 gint g_snprintf (gchar *string,
1504 gchar const *format,
1505 ...) G_GNUC_PRINTF (3, 4);
1506 gint g_vsnprintf (gchar *string,
1508 gchar const *format,
1510 gchar* g_basename (const gchar *file_name);
1511 /* Check if a file name is an absolute path */
1512 gboolean g_path_is_absolute (const gchar *file_name);
1513 /* In case of absolute paths, skip the root part */
1514 gchar* g_path_skip_root (gchar *file_name);
1516 /* strings are newly allocated with g_malloc() */
1517 gchar* g_dirname (const gchar *file_name);
1518 gchar* g_get_current_dir (void);
1519 gchar* g_getenv (const gchar *variable);
1522 /* we use a GLib function as a replacement for ATEXIT, so
1523 * the programmer is not required to check the return value
1524 * (if there is any in the implementation) and doesn't encounter
1525 * missing include files.
1527 void g_atexit (GVoidFunc func);
1532 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
1536 g_bit_nth_lsf (guint32 mask,
1542 if (mask & (1 << (guint) nth_bit))
1545 while (nth_bit < 32);
1548 #endif /* G_CAN_INLINE */
1550 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
1554 g_bit_nth_msf (guint32 mask,
1562 if (mask & (1 << (guint) nth_bit))
1565 while (nth_bit > 0);
1568 #endif /* G_CAN_INLINE */
1570 G_INLINE_FUNC guint g_bit_storage (guint number);
1573 g_bit_storage (guint number)
1575 register guint n_bits = 0;
1585 #endif /* G_CAN_INLINE */
1589 GStringChunk* g_string_chunk_new (gint size);
1590 void g_string_chunk_free (GStringChunk *chunk);
1591 gchar* g_string_chunk_insert (GStringChunk *chunk,
1592 const gchar *string);
1593 gchar* g_string_chunk_insert_const (GStringChunk *chunk,
1594 const gchar *string);
1599 GString* g_string_new (const gchar *init);
1600 GString* g_string_sized_new (guint dfl_size);
1601 void g_string_free (GString *string,
1603 GString* g_string_assign (GString *lval,
1605 GString* g_string_truncate (GString *string,
1607 GString* g_string_append (GString *string,
1609 GString* g_string_append_c (GString *string,
1611 GString* g_string_prepend (GString *string,
1613 GString* g_string_prepend_c (GString *string,
1615 GString* g_string_insert (GString *string,
1618 GString* g_string_insert_c (GString *string,
1621 GString* g_string_erase (GString *string,
1624 GString* g_string_down (GString *string);
1625 GString* g_string_up (GString *string);
1626 void g_string_sprintf (GString *string,
1627 const gchar *format,
1628 ...) G_GNUC_PRINTF (2, 3);
1629 void g_string_sprintfa (GString *string,
1630 const gchar *format,
1631 ...) G_GNUC_PRINTF (2, 3);
1634 /* Resizable arrays, remove fills any cleared spot and shortens the
1635 * array, while preserving the order. remove_fast will distort the
1636 * order by moving the last element to the position of the removed
1639 #define g_array_append_val(a,v) g_array_append_vals(a,&v,1)
1640 #define g_array_prepend_val(a,v) g_array_prepend_vals(a,&v,1)
1641 #define g_array_index(a,t,i) (((t*)a->data)[i])
1643 GArray* g_array_new (gboolean zero_terminated,
1645 guint element_size);
1646 void g_array_free (GArray *array,
1647 gboolean free_segment);
1648 GArray* g_array_append_vals (GArray *array,
1651 GArray* g_array_prepend_vals (GArray *array,
1654 GArray* g_array_set_size (GArray *array,
1656 GArray* g_array_remove_index (GArray *array,
1658 GArray* g_array_remove_index_fast (GArray *array,
1661 /* Resizable pointer array. This interface is much less complicated
1662 * than the above. Add appends appends a pointer. Remove fills any
1663 * cleared spot and shortens the array. remove_fast will again distort
1666 #define g_ptr_array_index(array,index) (array->pdata)[index]
1667 GPtrArray* g_ptr_array_new (void);
1668 void g_ptr_array_free (GPtrArray *array,
1670 void g_ptr_array_set_size (GPtrArray *array,
1672 gpointer g_ptr_array_remove_index (GPtrArray *array,
1674 gpointer g_ptr_array_remove_index_fast (GPtrArray *array,
1676 gboolean g_ptr_array_remove (GPtrArray *array,
1678 gboolean g_ptr_array_remove_fast (GPtrArray *array,
1680 void g_ptr_array_add (GPtrArray *array,
1683 /* Byte arrays, an array of guint8. Implemented as a GArray,
1687 GByteArray* g_byte_array_new (void);
1688 void g_byte_array_free (GByteArray *array,
1689 gboolean free_segment);
1690 GByteArray* g_byte_array_append (GByteArray *array,
1693 GByteArray* g_byte_array_prepend (GByteArray *array,
1696 GByteArray* g_byte_array_set_size (GByteArray *array,
1698 GByteArray* g_byte_array_remove_index (GByteArray *array,
1700 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1706 gint g_str_equal (gconstpointer v,
1708 guint g_str_hash (gconstpointer v);
1710 gint g_int_equal (gconstpointer v,
1712 guint g_int_hash (gconstpointer v);
1714 /* This "hash" function will just return the key's adress as an
1715 * unsigned integer. Useful for hashing on plain adresses or
1716 * simple integer values.
1718 guint g_direct_hash (gconstpointer v);
1719 gint g_direct_equal (gconstpointer v,
1723 /* Quarks (string<->id association)
1725 GQuark g_quark_try_string (const gchar *string);
1726 GQuark g_quark_from_static_string (const gchar *string);
1727 GQuark g_quark_from_string (const gchar *string);
1728 gchar* g_quark_to_string (GQuark quark);
1733 void g_datalist_init (GData **datalist);
1734 void g_datalist_clear (GData **datalist);
1735 gpointer g_datalist_id_get_data (GData **datalist,
1737 void g_datalist_id_set_data_full (GData **datalist,
1740 GDestroyNotify destroy_func);
1741 void g_datalist_id_remove_no_notify (GData **datalist,
1743 void g_datalist_foreach (GData **datalist,
1744 GDataForeachFunc func,
1745 gpointer user_data);
1746 #define g_datalist_id_set_data(dl, q, d) \
1747 g_datalist_id_set_data_full ((dl), (q), (d), NULL)
1748 #define g_datalist_id_remove_data(dl, q) \
1749 g_datalist_id_set_data ((dl), (q), NULL)
1750 #define g_datalist_get_data(dl, k) \
1751 (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
1752 #define g_datalist_set_data_full(dl, k, d, f) \
1753 g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
1754 #define g_datalist_remove_no_notify(dl, k) \
1755 g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
1756 #define g_datalist_set_data(dl, k, d) \
1757 g_datalist_set_data_full ((dl), (k), (d), NULL)
1758 #define g_datalist_remove_data(dl, k) \
1759 g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
1762 /* Location Associated Keyed Data
1764 void g_dataset_destroy (gconstpointer dataset_location);
1765 gpointer g_dataset_id_get_data (gconstpointer dataset_location,
1767 void g_dataset_id_set_data_full (gconstpointer dataset_location,
1770 GDestroyNotify destroy_func);
1771 void g_dataset_id_remove_no_notify (gconstpointer dataset_location,
1773 void g_dataset_foreach (gconstpointer dataset_location,
1774 GDataForeachFunc func,
1775 gpointer user_data);
1776 #define g_dataset_id_set_data(l, k, d) \
1777 g_dataset_id_set_data_full ((l), (k), (d), NULL)
1778 #define g_dataset_id_remove_data(l, k) \
1779 g_dataset_id_set_data ((l), (k), NULL)
1780 #define g_dataset_get_data(l, k) \
1781 (g_dataset_id_get_data ((l), g_quark_try_string (k)))
1782 #define g_dataset_set_data_full(l, k, d, f) \
1783 g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
1784 #define g_dataset_remove_no_notify(l, k) \
1785 g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
1786 #define g_dataset_set_data(l, k, d) \
1787 g_dataset_set_data_full ((l), (k), (d), NULL)
1788 #define g_dataset_remove_data(l, k) \
1789 g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
1792 /* GScanner: Flexible lexical scanner for general purpose.
1795 /* Character sets */
1796 #define G_CSET_A_2_Z "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1797 #define G_CSET_a_2_z "abcdefghijklmnopqrstuvwxyz"
1798 #define G_CSET_LATINC "\300\301\302\303\304\305\306"\
1799 "\307\310\311\312\313\314\315\316\317\320"\
1800 "\321\322\323\324\325\326"\
1801 "\330\331\332\333\334\335\336"
1802 #define G_CSET_LATINS "\337\340\341\342\343\344\345\346"\
1803 "\347\350\351\352\353\354\355\356\357\360"\
1804 "\361\362\363\364\365\366"\
1805 "\370\371\372\373\374\375\376\377"
1812 G_ERR_UNEXP_EOF_IN_STRING,
1813 G_ERR_UNEXP_EOF_IN_COMMENT,
1814 G_ERR_NON_DIGIT_IN_CONST,
1817 G_ERR_FLOAT_MALFORMED
1825 G_TOKEN_LEFT_PAREN = '(',
1826 G_TOKEN_RIGHT_PAREN = ')',
1827 G_TOKEN_LEFT_CURLY = '{',
1828 G_TOKEN_RIGHT_CURLY = '}',
1829 G_TOKEN_LEFT_BRACE = '[',
1830 G_TOKEN_RIGHT_BRACE = ']',
1831 G_TOKEN_EQUAL_SIGN = '=',
1832 G_TOKEN_COMMA = ',',
1848 G_TOKEN_IDENTIFIER_NULL,
1850 G_TOKEN_COMMENT_SINGLE,
1851 G_TOKEN_COMMENT_MULTI,
1858 gchar *v_identifier;
1870 struct _GScannerConfig
1874 gchar *cset_skip_characters; /* default: " \t\n" */
1875 gchar *cset_identifier_first;
1876 gchar *cset_identifier_nth;
1877 gchar *cpair_comment_single; /* default: "#\n" */
1879 /* Should symbol lookup work case sensitive?
1881 guint case_sensitive : 1;
1883 /* Boolean values to be adjusted "on the fly"
1884 * to configure scanning behaviour.
1886 guint skip_comment_multi : 1; /* C like comment */
1887 guint skip_comment_single : 1; /* single line comment */
1888 guint scan_comment_multi : 1; /* scan multi line comments? */
1889 guint scan_identifier : 1;
1890 guint scan_identifier_1char : 1;
1891 guint scan_identifier_NULL : 1;
1892 guint scan_symbols : 1;
1893 guint scan_binary : 1;
1894 guint scan_octal : 1;
1895 guint scan_float : 1;
1896 guint scan_hex : 1; /* `0x0ff0' */
1897 guint scan_hex_dollar : 1; /* `$0ff0' */
1898 guint scan_string_sq : 1; /* string: 'anything' */
1899 guint scan_string_dq : 1; /* string: "\\-escapes!\n" */
1900 guint numbers_2_int : 1; /* bin, octal, hex => int */
1901 guint int_2_float : 1; /* int => G_TOKEN_FLOAT? */
1902 guint identifier_2_string : 1;
1903 guint char_2_token : 1; /* return G_TOKEN_CHAR? */
1904 guint symbol_2_token : 1;
1905 guint scope_0_fallback : 1; /* try scope 0 on lookups? */
1912 guint max_parse_errors;
1914 /* g_scanner_error() increments this field */
1917 /* name of input stream, featured by the default message handler */
1918 const gchar *input_name;
1920 /* data pointer for derived structures */
1921 gpointer derived_data;
1923 /* link into the scanner configuration */
1924 GScannerConfig *config;
1926 /* fields filled in after g_scanner_get_next_token() */
1932 /* fields filled in after g_scanner_peek_next_token() */
1933 GTokenType next_token;
1934 GTokenValue next_value;
1936 guint next_position;
1938 /* to be considered private */
1939 GHashTable *symbol_table;
1942 const gchar *text_end;
1946 /* handler function for _warn and _error */
1947 GScannerMsgFunc msg_handler;
1950 GScanner* g_scanner_new (GScannerConfig *config_templ);
1951 void g_scanner_destroy (GScanner *scanner);
1952 void g_scanner_input_file (GScanner *scanner,
1954 void g_scanner_sync_file_offset (GScanner *scanner);
1955 void g_scanner_input_text (GScanner *scanner,
1958 GTokenType g_scanner_get_next_token (GScanner *scanner);
1959 GTokenType g_scanner_peek_next_token (GScanner *scanner);
1960 GTokenType g_scanner_cur_token (GScanner *scanner);
1961 GTokenValue g_scanner_cur_value (GScanner *scanner);
1962 guint g_scanner_cur_line (GScanner *scanner);
1963 guint g_scanner_cur_position (GScanner *scanner);
1964 gboolean g_scanner_eof (GScanner *scanner);
1965 guint g_scanner_set_scope (GScanner *scanner,
1967 void g_scanner_scope_add_symbol (GScanner *scanner,
1969 const gchar *symbol,
1971 void g_scanner_scope_remove_symbol (GScanner *scanner,
1973 const gchar *symbol);
1974 gpointer g_scanner_scope_lookup_symbol (GScanner *scanner,
1976 const gchar *symbol);
1977 void g_scanner_scope_foreach_symbol (GScanner *scanner,
1980 gpointer func_data);
1981 gpointer g_scanner_lookup_symbol (GScanner *scanner,
1982 const gchar *symbol);
1983 void g_scanner_freeze_symbol_table (GScanner *scanner);
1984 void g_scanner_thaw_symbol_table (GScanner *scanner);
1985 void g_scanner_unexp_token (GScanner *scanner,
1986 GTokenType expected_token,
1987 const gchar *identifier_spec,
1988 const gchar *symbol_spec,
1989 const gchar *symbol_name,
1990 const gchar *message,
1992 void g_scanner_error (GScanner *scanner,
1993 const gchar *format,
1994 ...) G_GNUC_PRINTF (2,3);
1995 void g_scanner_warn (GScanner *scanner,
1996 const gchar *format,
1997 ...) G_GNUC_PRINTF (2,3);
1998 gint g_scanner_stat_mode (const gchar *filename);
1999 /* keep downward source compatibility */
2000 #define g_scanner_add_symbol( scanner, symbol, value ) G_STMT_START { \
2001 g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
2003 #define g_scanner_remove_symbol( scanner, symbol ) G_STMT_START { \
2004 g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
2006 #define g_scanner_foreach_symbol( scanner, func, data ) G_STMT_START { \
2007 g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
2016 GCompletionFunc func;
2022 GCompletion* g_completion_new (GCompletionFunc func);
2023 void g_completion_add_items (GCompletion* cmp,
2025 void g_completion_remove_items (GCompletion* cmp,
2027 void g_completion_clear_items (GCompletion* cmp);
2028 GList* g_completion_complete (GCompletion* cmp,
2030 gchar** new_prefix);
2031 void g_completion_free (GCompletion* cmp);
2034 /* GRelation: Indexed Relations. Imagine a really simple table in a
2035 * database. Relations are not ordered. This data type is meant for
2036 * maintaining a N-way mapping.
2038 * g_relation_new() creates a relation with FIELDS fields
2040 * g_relation_destroy() frees all resources
2041 * g_tuples_destroy() frees the result of g_relation_select()
2043 * g_relation_index() indexes relation FIELD with the provided
2044 * equality and hash functions. this must be done before any
2045 * calls to insert are made.
2047 * g_relation_insert() inserts a new tuple. you are expected to
2048 * provide the right number of fields.
2050 * g_relation_delete() deletes all relations with KEY in FIELD
2051 * g_relation_select() returns ...
2052 * g_relation_count() counts ...
2055 GRelation* g_relation_new (gint fields);
2056 void g_relation_destroy (GRelation *relation);
2057 void g_relation_index (GRelation *relation,
2059 GHashFunc hash_func,
2060 GCompareFunc key_compare_func);
2061 void g_relation_insert (GRelation *relation,
2063 gint g_relation_delete (GRelation *relation,
2066 GTuples* g_relation_select (GRelation *relation,
2069 gint g_relation_count (GRelation *relation,
2072 gboolean g_relation_exists (GRelation *relation,
2074 void g_relation_print (GRelation *relation);
2076 void g_tuples_destroy (GTuples *tuples);
2077 gpointer g_tuples_index (GTuples *tuples,
2085 /* This function returns prime numbers spaced by approximately 1.5-2.0
2086 * and is for use in resizing data structures which prefer
2087 * prime-valued sizes. The closest spaced prime function returns the
2088 * next largest prime, or the highest it knows about which is about
2091 guint g_spaced_primes_closest (guint num);
2095 * These are used for plug-in communication in the GIMP, for instance.
2096 * On Unix, it's simply an encapsulated file descriptor (a pipe).
2097 * On Windows, it's a handle to an anonymouos pipe, *and* (in the case
2098 * of the writable end) a thread id to post a message to when you have written
2103 gint fd; /* file handle (pseudo such in Win32) */
2105 guint peer; /* thread to post message to */
2106 guint peer_fd; /* read handle (in the other process) */
2107 guint offset; /* counter of accumulated bytes, to
2108 * be included in the message posted
2109 * so we keep in sync.
2111 guint peer_offset; /* in input channels where the writer's
2112 * offset is, so we don't try to read too much
2117 GIOChannel *g_iochannel_new (gint fd);
2118 void g_iochannel_free (GIOChannel *channel);
2119 void g_iochannel_close_and_free (GIOChannel *channel);
2120 void g_iochannel_wakeup_peer (GIOChannel *channel);
2121 #ifndef NATIVE_WIN32
2122 # define g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
2126 /* Windows emulation stubs for common unix functions
2130 #define MAXPATHLEN 1024
2135 /* These POSIXish functions are available in the Microsoft C library
2136 * prefixed with underscore (which of course technically speaking is
2137 * the Right Thing, as they are non-ANSI. Not that being non-ANSI
2138 * prevents Microsoft from practically requiring you to include
2139 * <windows.h> every now and then...).
2141 * You still need to include the appropriate headers to get the
2142 * prototypes, <io.h> or <direct.h>.
2144 * For some functions, we provide emulators in glib, which are prefixed
2147 #define getcwd _getcwd
2148 #define getpid _getpid
2149 #define access _access
2152 #define write _write
2153 #define lseek _lseek
2154 #define close _close
2155 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
2156 #define popen _popen
2157 #define pclose _pclose
2158 #define fdopen _fdopen
2159 #define ftruncate(fd, size) gwin_ftruncate (fd, size)
2160 #define opendir gwin_opendir
2161 #define readdir gwin_readdir
2162 #define rewinddir gwin_rewinddir
2163 #define closedir gwin_closedir
2165 #define NAME_MAX 255
2171 gboolean just_opened;
2172 guint find_file_handle;
2173 gpointer find_file_data;
2175 typedef struct DIR DIR;
2178 gchar d_name[NAME_MAX + 1];
2181 /* emulation functions */
2182 extern int gwin_ftruncate (gint f,
2184 DIR* gwin_opendir (const gchar *dirname);
2185 struct dirent* gwin_readdir (DIR *dir);
2186 void gwin_rewinddir (DIR *dir);
2187 gint gwin_closedir (DIR *dir);
2189 #endif /* _MSC_VER */
2191 #endif /* NATIVE_WIN32 */
2196 #endif /* __cplusplus */
2199 #endif /* __G_LIB_H__ */