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/.
30 #include <glib/gtypes.h>
37 /* On native Win32, directory separator is the backslash, and search path
38 * separator is the semicolon.
40 #define G_DIR_SEPARATOR '\\'
41 #define G_DIR_SEPARATOR_S "\\"
42 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
43 #define G_SEARCHPATH_SEPARATOR ';'
44 #define G_SEARCHPATH_SEPARATOR_S ";"
46 #else /* !G_OS_WIN32 */
50 #define G_DIR_SEPARATOR '/'
51 #define G_DIR_SEPARATOR_S "/"
52 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
53 #define G_SEARCHPATH_SEPARATOR ':'
54 #define G_SEARCHPATH_SEPARATOR_S ":"
56 #endif /* !G_OS_WIN32 */
58 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
59 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
61 #if !defined (G_VA_COPY)
62 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
63 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
64 # elif defined (G_VA_COPY_AS_ARRAY)
65 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
66 # else /* va_list is a pointer */
67 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
68 # endif /* va_list is a pointer */
69 #endif /* !G_VA_COPY */
71 /* inlining hassle. for compilers that don't allow the `inline' keyword,
72 * mostly because of strict ANSI C compliance or dumbness, we try to fall
73 * back to either `__inline__' or `__inline'.
74 * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be
75 * actually *capable* to do function inlining, in which case inline
76 * function bodies do make sense. we also define G_INLINE_FUNC to properly
77 * export the function prototypes if no inlining can be performed.
78 * inline function bodies have to be special cased with G_CAN_INLINE and a
79 * .c file specific macro to allow one compiled instance with extern linkage
80 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
82 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
84 # define inline __inline__
85 #elif !defined (G_HAVE_INLINE)
87 # if defined (G_HAVE___INLINE__)
88 # define inline __inline__
89 # elif defined (G_HAVE___INLINE)
90 # define inline __inline
91 # else /* !inline && !__inline__ && !__inline */
92 # define inline /* don't inline, then */
95 #ifdef G_IMPLEMENT_INLINES
96 # define G_INLINE_FUNC
98 #elif defined (__GNUC__)
99 # define G_INLINE_FUNC extern inline
100 #elif defined (G_CAN_INLINE)
101 # define G_INLINE_FUNC static inline
102 #else /* can't inline */
103 # define G_INLINE_FUNC
104 #endif /* !G_INLINE_FUNC */
106 /* Retrive static string info
109 #define g_get_user_name g_get_user_name_utf8
110 #define g_get_real_name g_get_real_name_utf8
111 #define g_get_home_dir g_get_home_dir_utf8
112 #define g_get_tmp_dir g_get_tmp_dir_utf8
115 G_CONST_RETURN gchar* g_get_user_name (void);
116 G_CONST_RETURN gchar* g_get_real_name (void);
117 G_CONST_RETURN gchar* g_get_home_dir (void);
118 G_CONST_RETURN gchar* g_get_tmp_dir (void);
119 gchar* g_get_prgname (void);
120 void g_set_prgname (const gchar *prgname);
121 G_CONST_RETURN gchar* g_get_application_name (void);
122 void g_set_application_name (const gchar *application_name);
124 G_CONST_RETURN gchar* g_get_user_data_dir (void);
125 G_CONST_RETURN gchar* g_get_user_config_dir (void);
126 G_CONST_RETURN gchar* g_get_user_cache_dir (void);
127 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs (void);
130 G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address);
131 static G_CONST_RETURN gchar * G_CONST_RETURN *
132 g_win32_get_system_data_dirs (void)
134 return g_win32_get_system_data_dirs_for_module (&g_win32_get_system_data_dirs);
136 #define g_get_system_data_dirs g_win32_get_system_data_dirs
139 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
141 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
143 typedef struct _GDebugKey GDebugKey;
150 /* Miscellaneous utility functions
152 guint g_parse_debug_string (const gchar *string,
153 const GDebugKey *keys,
156 gint g_snprintf (gchar *string,
159 ...) G_GNUC_PRINTF (3, 4);
160 gint g_vsnprintf (gchar *string,
165 /* Check if a file name is an absolute path */
166 gboolean g_path_is_absolute (const gchar *file_name);
168 /* In case of absolute paths, skip the root part */
169 G_CONST_RETURN gchar* g_path_skip_root (const gchar *file_name);
171 #ifndef G_DISABLE_DEPRECATED
173 /* These two functions are deprecated and will be removed in the next
174 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
175 * instead. Whatch out! The string returned by g_path_get_basename
176 * must be g_freed, while the string returned by g_basename must not.*/
177 G_CONST_RETURN gchar* g_basename (const gchar *file_name);
178 #define g_dirname g_path_get_dirname
180 #endif /* G_DISABLE_DEPRECATED */
183 #define g_get_current_dir g_get_current_dir_utf8
186 /* The returned strings are newly allocated with g_malloc() */
187 gchar* g_get_current_dir (void);
188 gchar* g_path_get_basename (const gchar *file_name) G_GNUC_MALLOC;
189 gchar* g_path_get_dirname (const gchar *file_name) G_GNUC_MALLOC;
191 /* Set the pointer at the specified location to NULL */
192 void g_nullify_pointer (gpointer *nullify_location);
194 /* return the environment string for the variable. The returned memory
195 * must not be freed. */
197 #define g_getenv g_getenv_utf8
198 #define g_setenv g_setenv_utf8
199 #define g_unsetenv g_unsetenv_utf8
200 #define g_find_program_in_path g_find_program_in_path_utf8
203 G_CONST_RETURN gchar* g_getenv (const gchar *variable);
204 gboolean g_setenv (const gchar *variable,
207 void g_unsetenv (const gchar *variable);
208 gchar** g_listenv (void);
210 /* we try to provide a usefull equivalent for ATEXIT if it is
211 * not defined, but use is actually abandoned. people should
212 * use g_atexit() instead.
214 typedef void (*GVoidFunc) (void);
216 # define ATEXIT(proc) g_ATEXIT(proc)
218 # define G_NATIVE_ATEXIT
220 /* we use a GLib function as a replacement for ATEXIT, so
221 * the programmer is not required to check the return value
222 * (if there is any in the implementation) and doesn't encounter
223 * missing include files.
225 void g_atexit (GVoidFunc func);
227 /* Look for an executable in PATH, following execvp() rules */
228 gchar* g_find_program_in_path (const gchar *program);
232 G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask,
234 G_INLINE_FUNC gint g_bit_nth_msf (gulong mask,
236 G_INLINE_FUNC guint g_bit_storage (gulong number);
239 * elements need to be >= sizeof (gpointer)
241 typedef struct _GTrashStack GTrashStack;
247 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
249 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
250 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
251 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
253 /* inline function implementations
255 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
257 g_bit_nth_lsf (gulong mask,
263 if (mask & (1UL << nth_bit))
266 while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1));
270 g_bit_nth_msf (gulong mask,
274 nth_bit = GLIB_SIZEOF_LONG * 8;
278 if (mask & (1UL << nth_bit))
285 g_bit_storage (gulong number)
287 register guint n_bits = 0;
298 g_trash_stack_push (GTrashStack **stack_p,
301 GTrashStack *data = (GTrashStack *) data_p;
303 data->next = *stack_p;
306 G_INLINE_FUNC gpointer
307 g_trash_stack_pop (GTrashStack **stack_p)
314 *stack_p = data->next;
315 /* NULLify private pointer here, most platforms store NULL as
323 G_INLINE_FUNC gpointer
324 g_trash_stack_peek (GTrashStack **stack_p)
333 g_trash_stack_height (GTrashStack **stack_p)
338 for (data = *stack_p; data; data = data->next)
343 #endif /* G_CAN_INLINE || __G_UTILS_C__ */
346 * we prefix variable declarations so they can
347 * properly get exported in windows dlls.
349 GLIB_VAR const guint glib_major_version;
350 GLIB_VAR const guint glib_minor_version;
351 GLIB_VAR const guint glib_micro_version;
352 GLIB_VAR const guint glib_interface_age;
353 GLIB_VAR const guint glib_binary_age;
355 const gchar * glib_check_version (guint required_major,
356 guint required_minor,
357 guint required_micro);
359 #define GLIB_CHECK_VERSION(major,minor,micro) \
360 (GLIB_MAJOR_VERSION > (major) || \
361 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
362 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
363 GLIB_MICRO_VERSION >= (micro)))
368 * On Windows, this macro defines a DllMain function that stores the
369 * actual DLL name that the code being compiled will be included in.
370 * STATIC should be empty or 'static'. DLL_NAME is the name of the
371 * (pointer to the) char array where the DLL name will be stored. If
372 * this is used, you must also include <windows.h>. If you need a more complex
373 * DLL entry point function, you cannot use this.
375 * On non-Windows platforms, expands to nothing.
378 #ifndef G_PLATFORM_WIN32
379 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
381 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
382 static char *dll_name; \
385 DllMain (HINSTANCE hinstDLL, \
387 LPVOID lpvReserved) \
389 wchar_t wcbfr[1000]; \
394 case DLL_PROCESS_ATTACH: \
395 if (GetVersion () < 0x80000000) \
397 GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
398 tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
399 dll_name = g_path_get_basename (tem); \
404 GetModuleFileNameA ((HMODULE) hinstDLL, cpbfr, G_N_ELEMENTS (cpbfr)); \
405 tem = g_locale_to_utf8 (cpbfr, -1, NULL, NULL, NULL); \
406 dll_name = g_path_get_basename (tem); \
414 #endif /* G_PLATFORM_WIN32 */
416 #endif /* __G_UTILS_H__ */