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 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
34 #include <glib/gtypes.h>
41 /* On Win32, the canonical directory separator is the backslash, and
42 * the search path separator is the semicolon. Note that also the
43 * (forward) slash works as directory separator.
45 #define G_DIR_SEPARATOR '\\'
46 #define G_DIR_SEPARATOR_S "\\"
47 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
48 #define G_SEARCHPATH_SEPARATOR ';'
49 #define G_SEARCHPATH_SEPARATOR_S ";"
51 #else /* !G_OS_WIN32 */
55 #define G_DIR_SEPARATOR '/'
56 #define G_DIR_SEPARATOR_S "/"
57 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
58 #define G_SEARCHPATH_SEPARATOR ':'
59 #define G_SEARCHPATH_SEPARATOR_S ":"
61 #endif /* !G_OS_WIN32 */
63 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
64 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
66 #if !defined (G_VA_COPY)
67 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
68 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
69 # elif defined (G_VA_COPY_AS_ARRAY)
70 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
71 # else /* va_list is a pointer */
72 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
73 # endif /* va_list is a pointer */
74 #endif /* !G_VA_COPY */
76 /* inlining hassle. for compilers that don't allow the `inline' keyword,
77 * mostly because of strict ANSI C compliance or dumbness, we try to fall
78 * back to either `__inline__' or `__inline'.
79 * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be
80 * actually *capable* to do function inlining, in which case inline
81 * function bodies do make sense. we also define G_INLINE_FUNC to properly
82 * export the function prototypes if no inlining can be performed.
83 * inline function bodies have to be special cased with G_CAN_INLINE and a
84 * .c file specific macro to allow one compiled instance with extern linkage
85 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
87 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
89 # define inline __inline__
90 #elif !defined (G_HAVE_INLINE)
92 # if defined (G_HAVE___INLINE__)
93 # define inline __inline__
94 # elif defined (G_HAVE___INLINE)
95 # define inline __inline
96 # else /* !inline && !__inline__ && !__inline */
97 # define inline /* don't inline, then */
100 #ifdef G_IMPLEMENT_INLINES
101 # define G_INLINE_FUNC
103 #elif defined (__GNUC__)
104 # define G_INLINE_FUNC static __inline __attribute__ ((unused))
105 #elif defined (G_CAN_INLINE)
106 # define G_INLINE_FUNC static inline
107 #else /* can't inline */
108 # define G_INLINE_FUNC
109 #endif /* !G_INLINE_FUNC */
111 #ifndef __GTK_DOC_IGNORE__
113 #define g_get_user_name g_get_user_name_utf8
114 #define g_get_real_name g_get_real_name_utf8
115 #define g_get_home_dir g_get_home_dir_utf8
116 #define g_get_tmp_dir g_get_tmp_dir_utf8
120 const gchar * g_get_user_name (void);
121 const gchar * g_get_real_name (void);
122 const gchar * g_get_home_dir (void);
123 const gchar * g_get_tmp_dir (void);
124 const gchar * g_get_host_name (void);
125 gchar * g_get_prgname (void);
126 void g_set_prgname (const gchar *prgname);
127 const gchar * g_get_application_name (void);
128 void g_set_application_name (const gchar *application_name);
130 void g_reload_user_special_dirs_cache (void);
131 const gchar * g_get_user_data_dir (void);
132 const gchar * g_get_user_config_dir (void);
133 const gchar * g_get_user_cache_dir (void);
134 const gchar * const * g_get_system_data_dirs (void);
137 /* This functions is not part of the public GLib API */
138 const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
141 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
142 /* This function is not part of the public GLib API either. Just call
143 * g_get_system_data_dirs() in your code, never mind that that is
144 * actually a macro and you will in fact call this inline function.
146 static inline const gchar * const *
147 _g_win32_get_system_data_dirs (void)
149 return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
151 #define g_get_system_data_dirs _g_win32_get_system_data_dirs
154 const gchar * const * g_get_system_config_dirs (void);
156 const gchar * g_get_user_runtime_dir (void);
160 * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
161 * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
162 * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
163 * @G_USER_DIRECTORY_MUSIC: the user's Music directory
164 * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
165 * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
166 * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
167 * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
168 * @G_USER_N_DIRECTORIES: the number of enum values
170 * These are logical ids for special directories which are defined
171 * depending on the platform used. You should use g_get_user_special_dir()
172 * to retrieve the full path associated to the logical id.
174 * The #GUserDirectory enumeration can be extended at later date. Not
175 * every platform has a directory for every logical id in this
181 G_USER_DIRECTORY_DESKTOP,
182 G_USER_DIRECTORY_DOCUMENTS,
183 G_USER_DIRECTORY_DOWNLOAD,
184 G_USER_DIRECTORY_MUSIC,
185 G_USER_DIRECTORY_PICTURES,
186 G_USER_DIRECTORY_PUBLIC_SHARE,
187 G_USER_DIRECTORY_TEMPLATES,
188 G_USER_DIRECTORY_VIDEOS,
193 const gchar * g_get_user_special_dir (GUserDirectory directory);
200 * Associates a string with a bit flag.
201 * Used in g_parse_debug_string().
203 typedef struct _GDebugKey GDebugKey;
210 /* Miscellaneous utility functions
212 guint g_parse_debug_string (const gchar *string,
213 const GDebugKey *keys,
216 gint g_snprintf (gchar *string,
219 ...) G_GNUC_PRINTF (3, 4);
220 gint g_vsnprintf (gchar *string,
225 /* Check if a file name is an absolute path */
226 gboolean g_path_is_absolute (const gchar *file_name);
228 /* In case of absolute paths, skip the root part */
229 const gchar * g_path_skip_root (const gchar *file_name);
231 #ifndef G_DISABLE_DEPRECATED
233 GLIB_DEPRECATED_FOR(g_path_get_basename)
234 const gchar * g_basename (const gchar *file_name);
235 #define g_dirname g_path_get_dirname
237 #endif /* G_DISABLE_DEPRECATED */
239 #ifndef __GTK_DOC_IGNORE__
241 #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);
256 * Declares a type of function which takes no arguments
257 * and has no return value. It is used to specify the type
258 * function passed to g_atexit().
260 typedef void (*GVoidFunc) (void);
262 # define ATEXIT(proc) g_ATEXIT(proc)
264 # define G_NATIVE_ATEXIT
266 /* we use a GLib function as a replacement for ATEXIT, so
267 * the programmer is not required to check the return value
268 * (if there is any in the implementation) and doesn't encounter
269 * missing include files.
271 void g_atexit (GVoidFunc func);
274 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
275 * atexit(), the function will be called when the GLib DLL is detached
276 * from the program, which is not what the caller wants. The caller
277 * wants the function to be called when it *itself* exits (or is
278 * detached, in case the caller, too, is a DLL).
280 #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
281 int atexit (void (*)(void));
283 #define g_atexit(func) atexit(func)
286 #ifndef __GTK_DOC_IGNORE__
288 #define g_find_program_in_path g_find_program_in_path_utf8
292 /* Look for an executable in PATH, following execvp() rules */
293 gchar* g_find_program_in_path (const gchar *program);
297 G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask,
298 gint nth_bit) G_GNUC_CONST;
299 G_INLINE_FUNC gint g_bit_nth_msf (gulong mask,
300 gint nth_bit) G_GNUC_CONST;
301 G_INLINE_FUNC guint g_bit_storage (gulong number) G_GNUC_CONST;
303 /* inline function implementations
305 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
307 g_bit_nth_lsf (gulong mask,
310 if (G_UNLIKELY (nth_bit < -1))
312 while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
315 if (mask & (1UL << nth_bit))
321 g_bit_nth_msf (gulong mask,
324 if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
325 nth_bit = GLIB_SIZEOF_LONG * 8;
329 if (mask & (1UL << nth_bit))
335 g_bit_storage (gulong number)
337 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
338 return G_LIKELY (number) ?
339 ((GLIB_SIZEOF_LONG * 8U - 1) ^ __builtin_clzl(number)) + 1 : 1;
341 register guint n_bits = 0;
352 #endif /* G_CAN_INLINE || __G_UTILS_C__ */
356 #ifndef G_DISABLE_DEPRECATED
359 * This macro is deprecated. This DllMain() is too complex. It is
360 * recommended to write an explicit minimal DLlMain() that just saves
361 * the handle to the DLL and then use that handle instead, for
362 * instance passing it to
363 * g_win32_get_package_installation_directory_of_module().
365 * On Windows, this macro defines a DllMain function that stores the
366 * actual DLL name that the code being compiled will be included in.
367 * STATIC should be empty or 'static'. DLL_NAME is the name of the
368 * (pointer to the) char array where the DLL name will be stored. If
369 * this is used, you must also include <windows.h>. If you need a more complex
370 * DLL entry point function, you cannot use this.
372 * On non-Windows platforms, expands to nothing.
375 #ifndef G_PLATFORM_WIN32
376 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
378 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
379 static char *dll_name; \
382 DllMain (HINSTANCE hinstDLL, \
384 LPVOID lpvReserved) \
386 wchar_t wcbfr[1000]; \
390 case DLL_PROCESS_ATTACH: \
391 GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
392 tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
393 dll_name = g_path_get_basename (tem); \
401 #endif /* !G_DISABLE_DEPRECATED */
403 #endif /* G_PLATFORM_WIN32 */
405 #endif /* __G_UTILS_H__ */