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 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 */
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 /* need this utility macro, but it's not always present in system headers
73 * copy it from linux features.h for those who need it
76 #if defined __GNUC__ && defined __GNUC_MINOR__
77 # define __GNUC_PREREQ(maj, min) \
78 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
80 # define __GNUC_PREREQ(maj, min) 0
84 /* inlining hassle. for compilers that don't allow the `inline' keyword,
85 * mostly because of strict ANSI C compliance or dumbness, we try to fall
86 * back to either `__inline__' or `__inline'.
87 * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be
88 * actually *capable* to do function inlining, in which case inline
89 * function bodies do make sense. we also define G_INLINE_FUNC to properly
90 * export the function prototypes if no inlining can be performed.
91 * inline function bodies have to be special cased with G_CAN_INLINE and a
92 * .c file specific macro to allow one compiled instance with extern linkage
93 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
95 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
97 # define inline __inline__
98 #elif !defined (G_HAVE_INLINE)
100 # if defined (G_HAVE___INLINE__)
101 # define inline __inline__
102 # elif defined (G_HAVE___INLINE)
103 # define inline __inline
104 # else /* !inline && !__inline__ && !__inline */
105 # define inline /* don't inline, then */
108 #ifdef G_IMPLEMENT_INLINES
109 # define G_INLINE_FUNC
111 #elif defined (__GNUC__)
112 # if __GNUC_PREREQ (4,2) && defined (__STDC_VERSION__) \
113 && __STDC_VERSION__ >= 199901L
114 # define G_INLINE_FUNC extern __inline __attribute__ ((__gnu_inline__))
116 # define G_INLINE_FUNC extern __inline
118 #elif defined (G_CAN_INLINE)
119 # define G_INLINE_FUNC static inline
120 #else /* can't inline */
121 # define G_INLINE_FUNC
122 #endif /* !G_INLINE_FUNC */
124 /* Retrive static string info
127 #define g_get_user_name g_get_user_name_utf8
128 #define g_get_real_name g_get_real_name_utf8
129 #define g_get_home_dir g_get_home_dir_utf8
130 #define g_get_tmp_dir g_get_tmp_dir_utf8
133 G_CONST_RETURN gchar* g_get_user_name (void);
134 G_CONST_RETURN gchar* g_get_real_name (void);
135 G_CONST_RETURN gchar* g_get_home_dir (void);
136 G_CONST_RETURN gchar* g_get_tmp_dir (void);
137 G_CONST_RETURN gchar* g_get_host_name (void);
138 gchar* g_get_prgname (void);
139 void g_set_prgname (const gchar *prgname);
140 G_CONST_RETURN gchar* g_get_application_name (void);
141 void g_set_application_name (const gchar *application_name);
143 G_CONST_RETURN gchar* g_get_user_data_dir (void);
144 G_CONST_RETURN gchar* g_get_user_config_dir (void);
145 G_CONST_RETURN gchar* g_get_user_cache_dir (void);
146 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs (void);
149 G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address);
152 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
153 static inline G_CONST_RETURN gchar * G_CONST_RETURN *
154 g_win32_get_system_data_dirs (void)
156 return g_win32_get_system_data_dirs_for_module ((gconstpointer) &g_win32_get_system_data_dirs);
158 #define g_get_system_data_dirs g_win32_get_system_data_dirs
161 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
163 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
167 * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
168 * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
169 * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
170 * @G_USER_DIRECTORY_MUSIC: the user's Music directory
171 * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
172 * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
173 * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
174 * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
175 * @G_USER_N_DIRECTORIES: the number of enum values
177 * These are logical ids for special directories which are defined
178 * depending on the platform used. You should use g_get_user_special_dir()
179 * to retrieve the full path associated to the logical id.
181 * The #GUserDirectory enumeration can be extended at later date. Not
182 * every platform has a directory for every logical id in this
188 G_USER_DIRECTORY_DESKTOP,
189 G_USER_DIRECTORY_DOCUMENTS,
190 G_USER_DIRECTORY_DOWNLOAD,
191 G_USER_DIRECTORY_MUSIC,
192 G_USER_DIRECTORY_PICTURES,
193 G_USER_DIRECTORY_PUBLIC_SHARE,
194 G_USER_DIRECTORY_TEMPLATES,
195 G_USER_DIRECTORY_VIDEOS,
200 G_CONST_RETURN gchar* g_get_user_special_dir (GUserDirectory directory);
202 typedef struct _GDebugKey GDebugKey;
209 /* Miscellaneous utility functions
211 guint g_parse_debug_string (const gchar *string,
212 const GDebugKey *keys,
215 gint g_snprintf (gchar *string,
218 ...) G_GNUC_PRINTF (3, 4);
219 gint g_vsnprintf (gchar *string,
224 /* Check if a file name is an absolute path */
225 gboolean g_path_is_absolute (const gchar *file_name);
227 /* In case of absolute paths, skip the root part */
228 G_CONST_RETURN gchar* g_path_skip_root (const gchar *file_name);
230 #ifndef G_DISABLE_DEPRECATED
232 /* These two functions are deprecated and will be removed in the next
233 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
234 * instead. Whatch out! The string returned by g_path_get_basename
235 * must be g_freed, while the string returned by g_basename must not.*/
236 G_CONST_RETURN gchar* g_basename (const gchar *file_name);
237 #define g_dirname g_path_get_dirname
239 #endif /* G_DISABLE_DEPRECATED */
242 #define g_get_current_dir g_get_current_dir_utf8
245 /* The returned strings are newly allocated with g_malloc() */
246 gchar* g_get_current_dir (void);
247 gchar* g_path_get_basename (const gchar *file_name) G_GNUC_MALLOC;
248 gchar* g_path_get_dirname (const gchar *file_name) G_GNUC_MALLOC;
250 /* Set the pointer at the specified location to NULL */
251 void g_nullify_pointer (gpointer *nullify_location);
253 /* return the environment string for the variable. The returned memory
254 * must not be freed. */
256 #define g_getenv g_getenv_utf8
257 #define g_setenv g_setenv_utf8
258 #define g_unsetenv g_unsetenv_utf8
259 #define g_find_program_in_path g_find_program_in_path_utf8
262 G_CONST_RETURN gchar* g_getenv (const gchar *variable);
263 gboolean g_setenv (const gchar *variable,
266 void g_unsetenv (const gchar *variable);
267 gchar** g_listenv (void);
270 const gchar* _g_getenv_nomalloc (const gchar *variable,
273 /* we try to provide a useful equivalent for ATEXIT if it is
274 * not defined, but use is actually abandoned. people should
275 * use g_atexit() instead.
277 typedef void (*GVoidFunc) (void);
279 # define ATEXIT(proc) g_ATEXIT(proc)
281 # define G_NATIVE_ATEXIT
283 /* we use a GLib function as a replacement for ATEXIT, so
284 * the programmer is not required to check the return value
285 * (if there is any in the implementation) and doesn't encounter
286 * missing include files.
288 void g_atexit (GVoidFunc func);
291 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
292 * atexit(), the function will be called when the GLib DLL is detached
293 * from the program, which is not what the caller wants. The caller
294 * wants the function to be called when it *itself* exits (or is
295 * detached, in case the caller, too, is a DLL).
297 int atexit (void (*)(void));
298 #define g_atexit(func) atexit(func)
301 /* Look for an executable in PATH, following execvp() rules */
302 gchar* g_find_program_in_path (const gchar *program);
306 G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask,
307 gint nth_bit) G_GNUC_CONST;
308 G_INLINE_FUNC gint g_bit_nth_msf (gulong mask,
309 gint nth_bit) G_GNUC_CONST;
310 G_INLINE_FUNC guint g_bit_storage (gulong number) G_GNUC_CONST;
313 * elements need to be >= sizeof (gpointer)
315 typedef struct _GTrashStack GTrashStack;
321 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
323 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
324 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
325 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
327 /* inline function implementations
329 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
331 g_bit_nth_lsf (gulong mask,
334 if (G_UNLIKELY (nth_bit < -1))
336 while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
339 if (mask & (1UL << nth_bit))
345 g_bit_nth_msf (gulong mask,
348 if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
349 nth_bit = GLIB_SIZEOF_LONG * 8;
353 if (mask & (1UL << nth_bit))
359 g_bit_storage (gulong number)
361 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
362 return G_LIKELY (number) ?
363 ((GLIB_SIZEOF_LONG * 8 - 1) ^ __builtin_clzl(number)) + 1 : 1;
365 register guint n_bits = 0;
377 g_trash_stack_push (GTrashStack **stack_p,
380 GTrashStack *data = (GTrashStack *) data_p;
382 data->next = *stack_p;
385 G_INLINE_FUNC gpointer
386 g_trash_stack_pop (GTrashStack **stack_p)
393 *stack_p = data->next;
394 /* NULLify private pointer here, most platforms store NULL as
402 G_INLINE_FUNC gpointer
403 g_trash_stack_peek (GTrashStack **stack_p)
412 g_trash_stack_height (GTrashStack **stack_p)
417 for (data = *stack_p; data; data = data->next)
422 #endif /* G_CAN_INLINE || __G_UTILS_C__ */
425 * we prefix variable declarations so they can
426 * properly get exported in windows dlls.
428 GLIB_VAR const guint glib_major_version;
429 GLIB_VAR const guint glib_minor_version;
430 GLIB_VAR const guint glib_micro_version;
431 GLIB_VAR const guint glib_interface_age;
432 GLIB_VAR const guint glib_binary_age;
434 const gchar * glib_check_version (guint required_major,
435 guint required_minor,
436 guint required_micro);
438 #define GLIB_CHECK_VERSION(major,minor,micro) \
439 (GLIB_MAJOR_VERSION > (major) || \
440 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
441 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
442 GLIB_MICRO_VERSION >= (micro)))
447 * On Windows, this macro defines a DllMain function that stores the
448 * actual DLL name that the code being compiled will be included in.
449 * STATIC should be empty or 'static'. DLL_NAME is the name of the
450 * (pointer to the) char array where the DLL name will be stored. If
451 * this is used, you must also include <windows.h>. If you need a more complex
452 * DLL entry point function, you cannot use this.
454 * On non-Windows platforms, expands to nothing.
457 #ifndef G_PLATFORM_WIN32
458 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
460 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
461 static char *dll_name; \
464 DllMain (HINSTANCE hinstDLL, \
466 LPVOID lpvReserved) \
468 wchar_t wcbfr[1000]; \
472 case DLL_PROCESS_ATTACH: \
473 GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
474 tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
475 dll_name = g_path_get_basename (tem); \
482 #endif /* G_PLATFORM_WIN32 */
484 #endif /* __G_UTILS_H__ */