Signal waiting threads, problem noticed by Christian Kellner.
[glib.git] / glib / gutils.h
blob5350b9c1d1571dbecbeb62426be9e8ec0f361924
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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #ifndef __G_UTILS_H__
28 #define __G_UTILS_H__
30 #include <glib/gtypes.h>
31 #include <stdarg.h>
33 G_BEGIN_DECLS
35 #ifdef G_OS_WIN32
37 /* On Win32, the canonical directory separator is the backslash, and
38 * the search path separator is the semicolon. Note that also the
39 * (forward) slash works as directory separator.
41 #define G_DIR_SEPARATOR '\\'
42 #define G_DIR_SEPARATOR_S "\\"
43 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
44 #define G_SEARCHPATH_SEPARATOR ';'
45 #define G_SEARCHPATH_SEPARATOR_S ";"
47 #else /* !G_OS_WIN32 */
49 /* Unix */
51 #define G_DIR_SEPARATOR '/'
52 #define G_DIR_SEPARATOR_S "/"
53 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
54 #define G_SEARCHPATH_SEPARATOR ':'
55 #define G_SEARCHPATH_SEPARATOR_S ":"
57 #endif /* !G_OS_WIN32 */
59 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
60 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
62 #if !defined (G_VA_COPY)
63 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
64 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
65 # elif defined (G_VA_COPY_AS_ARRAY)
66 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
67 # else /* va_list is a pointer */
68 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
69 # endif /* va_list is a pointer */
70 #endif /* !G_VA_COPY */
72 /* inlining hassle. for compilers that don't allow the `inline' keyword,
73 * mostly because of strict ANSI C compliance or dumbness, we try to fall
74 * back to either `__inline__' or `__inline'.
75 * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be
76 * actually *capable* to do function inlining, in which case inline
77 * function bodies do make sense. we also define G_INLINE_FUNC to properly
78 * export the function prototypes if no inlining can be performed.
79 * inline function bodies have to be special cased with G_CAN_INLINE and a
80 * .c file specific macro to allow one compiled instance with extern linkage
81 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
83 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
84 # undef inline
85 # define inline __inline__
86 #elif !defined (G_HAVE_INLINE)
87 # undef inline
88 # if defined (G_HAVE___INLINE__)
89 # define inline __inline__
90 # elif defined (G_HAVE___INLINE)
91 # define inline __inline
92 # else /* !inline && !__inline__ && !__inline */
93 # define inline /* don't inline, then */
94 # endif
95 #endif
96 #ifdef G_IMPLEMENT_INLINES
97 # define G_INLINE_FUNC
98 # undef G_CAN_INLINE
99 #elif defined (__GNUC__)
100 # define G_INLINE_FUNC extern inline
101 #elif defined (G_CAN_INLINE)
102 # define G_INLINE_FUNC static inline
103 #else /* can't inline */
104 # define G_INLINE_FUNC
105 #endif /* !G_INLINE_FUNC */
107 /* Retrive static string info
109 #ifdef G_OS_WIN32
110 #define g_get_user_name g_get_user_name_utf8
111 #define g_get_real_name g_get_real_name_utf8
112 #define g_get_home_dir g_get_home_dir_utf8
113 #define g_get_tmp_dir g_get_tmp_dir_utf8
114 #endif
116 G_CONST_RETURN gchar* g_get_user_name (void);
117 G_CONST_RETURN gchar* g_get_real_name (void);
118 G_CONST_RETURN gchar* g_get_home_dir (void);
119 G_CONST_RETURN gchar* g_get_tmp_dir (void);
120 G_CONST_RETURN gchar* g_get_host_name (void);
121 gchar* g_get_prgname (void);
122 void g_set_prgname (const gchar *prgname);
123 G_CONST_RETURN gchar* g_get_application_name (void);
124 void g_set_application_name (const gchar *application_name);
126 G_CONST_RETURN gchar* g_get_user_data_dir (void);
127 G_CONST_RETURN gchar* g_get_user_config_dir (void);
128 G_CONST_RETURN gchar* g_get_user_cache_dir (void);
129 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs (void);
131 #ifdef G_OS_WIN32
132 G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address);
133 #endif
135 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
136 static inline G_CONST_RETURN gchar * G_CONST_RETURN *
137 g_win32_get_system_data_dirs (void)
139 return g_win32_get_system_data_dirs_for_module ((gconstpointer) &g_win32_get_system_data_dirs);
141 #define g_get_system_data_dirs g_win32_get_system_data_dirs
142 #endif
144 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
146 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
148 typedef struct _GDebugKey GDebugKey;
149 struct _GDebugKey
151 gchar *key;
152 guint value;
155 /* Miscellaneous utility functions
157 guint g_parse_debug_string (const gchar *string,
158 const GDebugKey *keys,
159 guint nkeys);
161 gint g_snprintf (gchar *string,
162 gulong n,
163 gchar const *format,
164 ...) G_GNUC_PRINTF (3, 4);
165 gint g_vsnprintf (gchar *string,
166 gulong n,
167 gchar const *format,
168 va_list args);
170 /* Check if a file name is an absolute path */
171 gboolean g_path_is_absolute (const gchar *file_name);
173 /* In case of absolute paths, skip the root part */
174 G_CONST_RETURN gchar* g_path_skip_root (const gchar *file_name);
176 #ifndef G_DISABLE_DEPRECATED
178 /* These two functions are deprecated and will be removed in the next
179 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
180 * instead. Whatch out! The string returned by g_path_get_basename
181 * must be g_freed, while the string returned by g_basename must not.*/
182 G_CONST_RETURN gchar* g_basename (const gchar *file_name);
183 #define g_dirname g_path_get_dirname
185 #endif /* G_DISABLE_DEPRECATED */
187 #ifdef G_OS_WIN32
188 #define g_get_current_dir g_get_current_dir_utf8
189 #endif
191 /* The returned strings are newly allocated with g_malloc() */
192 gchar* g_get_current_dir (void);
193 gchar* g_path_get_basename (const gchar *file_name) G_GNUC_MALLOC;
194 gchar* g_path_get_dirname (const gchar *file_name) G_GNUC_MALLOC;
196 /* Set the pointer at the specified location to NULL */
197 void g_nullify_pointer (gpointer *nullify_location);
199 /* return the environment string for the variable. The returned memory
200 * must not be freed. */
201 #ifdef G_OS_WIN32
202 #define g_getenv g_getenv_utf8
203 #define g_setenv g_setenv_utf8
204 #define g_unsetenv g_unsetenv_utf8
205 #define g_find_program_in_path g_find_program_in_path_utf8
206 #endif
208 G_CONST_RETURN gchar* g_getenv (const gchar *variable);
209 gboolean g_setenv (const gchar *variable,
210 const gchar *value,
211 gboolean overwrite);
212 void g_unsetenv (const gchar *variable);
213 gchar** g_listenv (void);
215 /* we try to provide a usefull equivalent for ATEXIT if it is
216 * not defined, but use is actually abandoned. people should
217 * use g_atexit() instead.
219 typedef void (*GVoidFunc) (void);
220 #ifndef ATEXIT
221 # define ATEXIT(proc) g_ATEXIT(proc)
222 #else
223 # define G_NATIVE_ATEXIT
224 #endif /* ATEXIT */
225 /* we use a GLib function as a replacement for ATEXIT, so
226 * the programmer is not required to check the return value
227 * (if there is any in the implementation) and doesn't encounter
228 * missing include files.
230 void g_atexit (GVoidFunc func);
232 #ifdef G_OS_WIN32
233 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
234 * atexit(), the function will be called when the GLib DLL is detached
235 * from the program, which is not what the caller wants. The caller
236 * wants the function to be called when it *itself* exits (or is
237 * detached, in case the caller, too, is a DLL).
239 int atexit (void (*)(void));
240 #define g_atexit(func) atexit(func)
241 #endif
243 /* Look for an executable in PATH, following execvp() rules */
244 gchar* g_find_program_in_path (const gchar *program);
246 /* Bit tests
248 G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask,
249 gint nth_bit);
250 G_INLINE_FUNC gint g_bit_nth_msf (gulong mask,
251 gint nth_bit);
252 G_INLINE_FUNC guint g_bit_storage (gulong number);
254 /* Trash Stacks
255 * elements need to be >= sizeof (gpointer)
257 typedef struct _GTrashStack GTrashStack;
258 struct _GTrashStack
260 GTrashStack *next;
263 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
264 gpointer data_p);
265 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
266 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
267 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
269 /* inline function implementations
271 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
272 G_INLINE_FUNC gint
273 g_bit_nth_lsf (gulong mask,
274 gint nth_bit)
278 nth_bit++;
279 if (mask & (1UL << nth_bit))
280 return nth_bit;
282 while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1));
283 return -1;
285 G_INLINE_FUNC gint
286 g_bit_nth_msf (gulong mask,
287 gint nth_bit)
289 if (nth_bit < 0)
290 nth_bit = GLIB_SIZEOF_LONG * 8;
293 nth_bit--;
294 if (mask & (1UL << nth_bit))
295 return nth_bit;
297 while (nth_bit > 0);
298 return -1;
300 G_INLINE_FUNC guint
301 g_bit_storage (gulong number)
303 register guint n_bits = 0;
307 n_bits++;
308 number >>= 1;
310 while (number);
311 return n_bits;
313 G_INLINE_FUNC void
314 g_trash_stack_push (GTrashStack **stack_p,
315 gpointer data_p)
317 GTrashStack *data = (GTrashStack *) data_p;
319 data->next = *stack_p;
320 *stack_p = data;
322 G_INLINE_FUNC gpointer
323 g_trash_stack_pop (GTrashStack **stack_p)
325 GTrashStack *data;
327 data = *stack_p;
328 if (data)
330 *stack_p = data->next;
331 /* NULLify private pointer here, most platforms store NULL as
332 * subsequent 0 bytes
334 data->next = NULL;
337 return data;
339 G_INLINE_FUNC gpointer
340 g_trash_stack_peek (GTrashStack **stack_p)
342 GTrashStack *data;
344 data = *stack_p;
346 return data;
348 G_INLINE_FUNC guint
349 g_trash_stack_height (GTrashStack **stack_p)
351 GTrashStack *data;
352 guint i = 0;
354 for (data = *stack_p; data; data = data->next)
355 i++;
357 return i;
359 #endif /* G_CAN_INLINE || __G_UTILS_C__ */
361 /* Glib version.
362 * we prefix variable declarations so they can
363 * properly get exported in windows dlls.
365 GLIB_VAR const guint glib_major_version;
366 GLIB_VAR const guint glib_minor_version;
367 GLIB_VAR const guint glib_micro_version;
368 GLIB_VAR const guint glib_interface_age;
369 GLIB_VAR const guint glib_binary_age;
371 const gchar * glib_check_version (guint required_major,
372 guint required_minor,
373 guint required_micro);
375 #define GLIB_CHECK_VERSION(major,minor,micro) \
376 (GLIB_MAJOR_VERSION > (major) || \
377 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
378 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
379 GLIB_MICRO_VERSION >= (micro)))
381 G_END_DECLS
384 * On Windows, this macro defines a DllMain function that stores the
385 * actual DLL name that the code being compiled will be included in.
386 * STATIC should be empty or 'static'. DLL_NAME is the name of the
387 * (pointer to the) char array where the DLL name will be stored. If
388 * this is used, you must also include <windows.h>. If you need a more complex
389 * DLL entry point function, you cannot use this.
391 * On non-Windows platforms, expands to nothing.
394 #ifndef G_PLATFORM_WIN32
395 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
396 #else
397 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
398 static char *dll_name; \
400 BOOL WINAPI \
401 DllMain (HINSTANCE hinstDLL, \
402 DWORD fdwReason, \
403 LPVOID lpvReserved) \
405 wchar_t wcbfr[1000]; \
406 char cpbfr[1000]; \
407 char *tem; \
408 switch (fdwReason) \
410 case DLL_PROCESS_ATTACH: \
411 if (GetVersion () < 0x80000000) \
413 GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
414 tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
415 dll_name = g_path_get_basename (tem); \
416 g_free (tem); \
418 else \
420 GetModuleFileNameA ((HMODULE) hinstDLL, cpbfr, G_N_ELEMENTS (cpbfr)); \
421 tem = g_locale_to_utf8 (cpbfr, -1, NULL, NULL, NULL); \
422 dll_name = g_path_get_basename (tem); \
423 g_free (tem); \
425 break; \
428 return TRUE; \
430 #endif /* G_PLATFORM_WIN32 */
432 #endif /* __G_UTILS_H__ */