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