make it possible to disable single-file includes by defining
[platform/upstream/glib.git] / glib / gutils.h
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/.
25  */
26
27 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__G_LIB_H__) && !defined (GLIB_COMPILATION)
28 #error "Only <glib.h> can be included directly."
29 #endif
30
31 #ifndef __G_UTILS_H__
32 #define __G_UTILS_H__
33
34 #include <glib/gtypes.h>
35 #include <stdarg.h>
36
37 G_BEGIN_DECLS
38
39 #ifdef G_OS_WIN32
40
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.
44  */
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 ";"
50
51 #else  /* !G_OS_WIN32 */
52
53 /* Unix */
54
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 ":"
60
61 #endif /* !G_OS_WIN32 */
62
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.
65  */
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 */
75
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.
86  */
87 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
88 #  undef inline
89 #  define inline __inline__
90 #elif !defined (G_HAVE_INLINE)
91 #  undef 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 */
98 #  endif
99 #endif
100 #ifdef G_IMPLEMENT_INLINES
101 #  define G_INLINE_FUNC
102 #  undef  G_CAN_INLINE
103 #elif defined (__GNUC__) 
104 #  ifdef __GNUC_STDC_INLINE__
105 #   define G_INLINE_FUNC extern inline __attribute__ ((__gnu_inline__))
106 #  else
107 #   define G_INLINE_FUNC extern inline
108 #  endif
109 #elif defined (G_CAN_INLINE) 
110 #  define G_INLINE_FUNC static inline
111 #else /* can't inline */
112 #  define G_INLINE_FUNC
113 #endif /* !G_INLINE_FUNC */
114
115 /* Retrive static string info
116  */
117 #ifdef G_OS_WIN32
118 #define g_get_user_name g_get_user_name_utf8
119 #define g_get_real_name g_get_real_name_utf8
120 #define g_get_home_dir g_get_home_dir_utf8
121 #define g_get_tmp_dir g_get_tmp_dir_utf8
122 #endif
123
124 G_CONST_RETURN gchar* g_get_user_name        (void);
125 G_CONST_RETURN gchar* g_get_real_name        (void);
126 G_CONST_RETURN gchar* g_get_home_dir         (void);
127 G_CONST_RETURN gchar* g_get_tmp_dir          (void);
128 G_CONST_RETURN gchar* g_get_host_name        (void);
129 gchar*                g_get_prgname          (void);
130 void                  g_set_prgname          (const gchar *prgname);
131 G_CONST_RETURN gchar* g_get_application_name (void);
132 void                  g_set_application_name (const gchar *application_name);
133
134 G_CONST_RETURN gchar*    g_get_user_data_dir      (void);
135 G_CONST_RETURN gchar*    g_get_user_config_dir    (void);
136 G_CONST_RETURN gchar*    g_get_user_cache_dir     (void);
137 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs   (void);
138
139 #ifdef G_OS_WIN32
140 G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address);
141 #endif
142
143 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
144 static inline G_CONST_RETURN gchar * G_CONST_RETURN *
145 g_win32_get_system_data_dirs (void)
146 {
147   return g_win32_get_system_data_dirs_for_module ((gconstpointer) &g_win32_get_system_data_dirs);
148 }
149 #define g_get_system_data_dirs g_win32_get_system_data_dirs
150 #endif
151
152 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
153
154 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
155
156 /**
157  * GUserDirectory:
158  * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
159  * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
160  * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
161  * @G_USER_DIRECTORY_MUSIC: the user's Music directory
162  * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
163  * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
164  * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
165  * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
166  * @G_USER_N_DIRECTORIES: the number of enum values
167  *
168  * These are logical ids for special directories which are defined
169  * depending on the platform used. You should use g_get_user_special_dir()
170  * to retrieve the full path associated to the logical id.
171  *
172  * The #GUserDirectory enumeration can be extended at later date. Not
173  * every platform has a directory for every logical id in this
174  * enumeration.
175  *
176  * Since: 2.14
177  */
178 typedef enum {
179   G_USER_DIRECTORY_DESKTOP,
180   G_USER_DIRECTORY_DOCUMENTS,
181   G_USER_DIRECTORY_DOWNLOAD,
182   G_USER_DIRECTORY_MUSIC,
183   G_USER_DIRECTORY_PICTURES,
184   G_USER_DIRECTORY_PUBLIC_SHARE,
185   G_USER_DIRECTORY_TEMPLATES,
186   G_USER_DIRECTORY_VIDEOS,
187
188   G_USER_N_DIRECTORIES
189 } GUserDirectory;
190
191 G_CONST_RETURN gchar* g_get_user_special_dir (GUserDirectory directory);
192
193 typedef struct _GDebugKey       GDebugKey;
194 struct _GDebugKey
195 {
196   gchar *key;
197   guint  value;
198 };
199
200 /* Miscellaneous utility functions
201  */
202 guint                 g_parse_debug_string (const gchar     *string,
203                                             const GDebugKey *keys,
204                                             guint            nkeys);
205
206 gint                  g_snprintf           (gchar       *string,
207                                             gulong       n,
208                                             gchar const *format,
209                                             ...) G_GNUC_PRINTF (3, 4);
210 gint                  g_vsnprintf          (gchar       *string,
211                                             gulong       n,
212                                             gchar const *format,
213                                             va_list      args);
214
215 /* Check if a file name is an absolute path */
216 gboolean              g_path_is_absolute   (const gchar *file_name);
217
218 /* In case of absolute paths, skip the root part */
219 G_CONST_RETURN gchar* g_path_skip_root     (const gchar *file_name);
220
221 #ifndef G_DISABLE_DEPRECATED
222
223 /* These two functions are deprecated and will be removed in the next
224  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
225  * instead. Whatch out! The string returned by g_path_get_basename
226  * must be g_freed, while the string returned by g_basename must not.*/
227 G_CONST_RETURN gchar* g_basename           (const gchar *file_name);
228 #define g_dirname g_path_get_dirname
229
230 #endif /* G_DISABLE_DEPRECATED */
231
232 #ifdef G_OS_WIN32
233 #define g_get_current_dir g_get_current_dir_utf8
234 #endif
235
236 /* The returned strings are newly allocated with g_malloc() */
237 gchar*                g_get_current_dir    (void);
238 gchar*                g_path_get_basename  (const gchar *file_name) G_GNUC_MALLOC;
239 gchar*                g_path_get_dirname   (const gchar *file_name) G_GNUC_MALLOC;
240
241 /* Set the pointer at the specified location to NULL */
242 void                  g_nullify_pointer    (gpointer    *nullify_location);
243
244 /* return the environment string for the variable. The returned memory
245  * must not be freed. */
246 #ifdef G_OS_WIN32
247 #define g_getenv g_getenv_utf8
248 #define g_setenv g_setenv_utf8
249 #define g_unsetenv g_unsetenv_utf8
250 #define g_find_program_in_path g_find_program_in_path_utf8
251 #endif
252
253 G_CONST_RETURN gchar* g_getenv             (const gchar *variable);
254 gboolean              g_setenv             (const gchar *variable,
255                                             const gchar *value,
256                                             gboolean     overwrite);
257 void                  g_unsetenv           (const gchar *variable);
258 gchar**               g_listenv            (void);
259
260 /* private */
261 const gchar*         _g_getenv_nomalloc    (const gchar *variable,
262                                             gchar        buffer[1024]);
263
264 /* we try to provide a useful equivalent for ATEXIT if it is
265  * not defined, but use is actually abandoned. people should
266  * use g_atexit() instead.
267  */
268 typedef void            (*GVoidFunc)            (void);
269 #ifndef ATEXIT
270 # define ATEXIT(proc)   g_ATEXIT(proc)
271 #else
272 # define G_NATIVE_ATEXIT
273 #endif /* ATEXIT */
274 /* we use a GLib function as a replacement for ATEXIT, so
275  * the programmer is not required to check the return value
276  * (if there is any in the implementation) and doesn't encounter
277  * missing include files.
278  */
279 void    g_atexit                (GVoidFunc    func);
280
281 #ifdef G_OS_WIN32
282 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
283  * atexit(), the function will be called when the GLib DLL is detached
284  * from the program, which is not what the caller wants. The caller
285  * wants the function to be called when it *itself* exits (or is
286  * detached, in case the caller, too, is a DLL).
287  */
288 int atexit (void (*)(void));
289 #define g_atexit(func) atexit(func)
290 #endif
291
292 /* Look for an executable in PATH, following execvp() rules */
293 gchar*  g_find_program_in_path  (const gchar *program);
294
295 /* Bit tests
296  */
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;
302
303 /* Trash Stacks
304  * elements need to be >= sizeof (gpointer)
305  */
306 typedef struct _GTrashStack     GTrashStack;
307 struct _GTrashStack
308 {
309   GTrashStack *next;
310 };
311
312 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
313                                                  gpointer      data_p);
314 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
315 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
316 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
317
318 /* inline function implementations
319  */
320 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
321 G_INLINE_FUNC gint
322 g_bit_nth_lsf (gulong mask,
323                gint   nth_bit)
324 {
325   if (G_UNLIKELY (nth_bit < -1))
326     nth_bit = -1;
327   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
328     {
329       nth_bit++;
330       if (mask & (1UL << nth_bit))
331         return nth_bit;
332     }
333   return -1;
334 }
335 G_INLINE_FUNC gint
336 g_bit_nth_msf (gulong mask,
337                gint   nth_bit)
338 {
339   if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
340     nth_bit = GLIB_SIZEOF_LONG * 8;
341   while (nth_bit > 0)
342     {
343       nth_bit--;
344       if (mask & (1UL << nth_bit))
345         return nth_bit;
346     }
347   return -1;
348 }
349 G_INLINE_FUNC guint
350 g_bit_storage (gulong number)
351 {
352 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
353   return G_LIKELY (number) ?
354            ((GLIB_SIZEOF_LONG * 8 - 1) ^ __builtin_clzl(number)) + 1 : 1;
355 #else
356   register guint n_bits = 0;
357   
358   do
359     {
360       n_bits++;
361       number >>= 1;
362     }
363   while (number);
364   return n_bits;
365 #endif
366 }
367 G_INLINE_FUNC void
368 g_trash_stack_push (GTrashStack **stack_p,
369                     gpointer      data_p)
370 {
371   GTrashStack *data = (GTrashStack *) data_p;
372
373   data->next = *stack_p;
374   *stack_p = data;
375 }
376 G_INLINE_FUNC gpointer
377 g_trash_stack_pop (GTrashStack **stack_p)
378 {
379   GTrashStack *data;
380
381   data = *stack_p;
382   if (data)
383     {
384       *stack_p = data->next;
385       /* NULLify private pointer here, most platforms store NULL as
386        * subsequent 0 bytes
387        */
388       data->next = NULL;
389     }
390
391   return data;
392 }
393 G_INLINE_FUNC gpointer
394 g_trash_stack_peek (GTrashStack **stack_p)
395 {
396   GTrashStack *data;
397
398   data = *stack_p;
399
400   return data;
401 }
402 G_INLINE_FUNC guint
403 g_trash_stack_height (GTrashStack **stack_p)
404 {
405   GTrashStack *data;
406   guint i = 0;
407
408   for (data = *stack_p; data; data = data->next)
409     i++;
410
411   return i;
412 }
413 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
414
415 /* Glib version.
416  * we prefix variable declarations so they can
417  * properly get exported in windows dlls.
418  */
419 GLIB_VAR const guint glib_major_version;
420 GLIB_VAR const guint glib_minor_version;
421 GLIB_VAR const guint glib_micro_version;
422 GLIB_VAR const guint glib_interface_age;
423 GLIB_VAR const guint glib_binary_age;
424
425 const gchar * glib_check_version (guint required_major,
426                                   guint required_minor,
427                                   guint required_micro);
428
429 #define GLIB_CHECK_VERSION(major,minor,micro)    \
430     (GLIB_MAJOR_VERSION > (major) || \
431      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
432      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
433       GLIB_MICRO_VERSION >= (micro)))
434
435 G_END_DECLS
436
437 /*
438  * This macro will be deprecated in the future. This DllMain() is too
439  * complex. It is recommended to have a DLlMain() that just saves the
440  * handle to the DLL and then use that handle in normal code instead,
441  * for instance passing it to
442  * g_win32_get_package_installation_directory_of_module().
443  *
444  * On Windows, this macro defines a DllMain function that stores the
445  * actual DLL name that the code being compiled will be included in.
446  * STATIC should be empty or 'static'. DLL_NAME is the name of the
447  * (pointer to the) char array where the DLL name will be stored. If
448  * this is used, you must also include <windows.h>. If you need a more complex
449  * DLL entry point function, you cannot use this.
450  *
451  * On non-Windows platforms, expands to nothing.
452  */
453
454 #ifndef G_PLATFORM_WIN32
455 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
456 #else
457 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                 \
458 static char *dll_name;                                                  \
459                                                                         \
460 BOOL WINAPI                                                             \
461 DllMain (HINSTANCE hinstDLL,                                            \
462          DWORD     fdwReason,                                           \
463          LPVOID    lpvReserved)                                         \
464 {                                                                       \
465   wchar_t wcbfr[1000];                                                  \
466   char *tem;                                                            \
467   switch (fdwReason)                                                    \
468     {                                                                   \
469     case DLL_PROCESS_ATTACH:                                            \
470       GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
471       tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);              \
472       dll_name = g_path_get_basename (tem);                             \
473       g_free (tem);                                                     \
474       break;                                                            \
475     }                                                                   \
476                                                                         \
477   return TRUE;                                                          \
478 }
479 #endif /* G_PLATFORM_WIN32 */
480
481 #endif /* __G_UTILS_H__ */