4bc852827910f948fc9692f7236780a732fcfa88
[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 #  define G_INLINE_FUNC extern inline
101 #elif defined (G_CAN_INLINE) 
102 #  define G_INLINE_FUNC static inline
103 #else /* can't inline */
104 #  define G_INLINE_FUNC
105 #endif /* !G_INLINE_FUNC */
106
107 /* Retrive static string info
108  */
109 #ifdef G_OS_WIN32
110 #define g_get_user_name g_get_user_name_utf8
111 #define g_get_real_name g_get_real_name_utf8
112 #define g_get_home_dir g_get_home_dir_utf8
113 #define g_get_tmp_dir g_get_tmp_dir_utf8
114 #endif
115
116 G_CONST_RETURN gchar* g_get_user_name        (void);
117 G_CONST_RETURN gchar* g_get_real_name        (void);
118 G_CONST_RETURN gchar* g_get_home_dir         (void);
119 G_CONST_RETURN gchar* g_get_tmp_dir          (void);
120 gchar*                g_get_prgname          (void);
121 void                  g_set_prgname          (const gchar *prgname);
122 G_CONST_RETURN gchar* g_get_application_name (void);
123 void                  g_set_application_name (const gchar *application_name);
124
125 G_CONST_RETURN gchar*    g_get_user_data_dir      (void);
126 G_CONST_RETURN gchar*    g_get_user_config_dir    (void);
127 G_CONST_RETURN gchar*    g_get_user_cache_dir     (void);
128 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs   (void);
129
130 #ifdef G_OS_WIN32
131 G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address);
132 static G_CONST_RETURN gchar * G_CONST_RETURN *
133 g_win32_get_system_data_dirs (void)
134 {
135   return g_win32_get_system_data_dirs_for_module ((gconstpointer) &g_win32_get_system_data_dirs);
136 }
137 #define g_get_system_data_dirs g_win32_get_system_data_dirs
138 #endif
139
140 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
141
142 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
143
144 typedef struct _GDebugKey       GDebugKey;
145 struct _GDebugKey
146 {
147   gchar *key;
148   guint  value;
149 };
150
151 /* Miscellaneous utility functions
152  */
153 guint                 g_parse_debug_string (const gchar     *string,
154                                             const GDebugKey *keys,
155                                             guint            nkeys);
156
157 gint                  g_snprintf           (gchar       *string,
158                                             gulong       n,
159                                             gchar const *format,
160                                             ...) G_GNUC_PRINTF (3, 4);
161 gint                  g_vsnprintf          (gchar       *string,
162                                             gulong       n,
163                                             gchar const *format,
164                                             va_list      args);
165
166 /* Check if a file name is an absolute path */
167 gboolean              g_path_is_absolute   (const gchar *file_name);
168
169 /* In case of absolute paths, skip the root part */
170 G_CONST_RETURN gchar* g_path_skip_root     (const gchar *file_name);
171
172 #ifndef G_DISABLE_DEPRECATED
173
174 /* These two functions are deprecated and will be removed in the next
175  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
176  * instead. Whatch out! The string returned by g_path_get_basename
177  * must be g_freed, while the string returned by g_basename must not.*/
178 G_CONST_RETURN gchar* g_basename           (const gchar *file_name);
179 #define g_dirname g_path_get_dirname
180
181 #endif /* G_DISABLE_DEPRECATED */
182
183 #ifdef G_OS_WIN32
184 #define g_get_current_dir g_get_current_dir_utf8
185 #endif
186
187 /* The returned strings are newly allocated with g_malloc() */
188 gchar*                g_get_current_dir    (void);
189 gchar*                g_path_get_basename  (const gchar *file_name) G_GNUC_MALLOC;
190 gchar*                g_path_get_dirname   (const gchar *file_name) G_GNUC_MALLOC;
191
192 /* Set the pointer at the specified location to NULL */
193 void                  g_nullify_pointer    (gpointer    *nullify_location);
194
195 /* return the environment string for the variable. The returned memory
196  * must not be freed. */
197 #ifdef G_OS_WIN32
198 #define g_getenv g_getenv_utf8
199 #define g_setenv g_setenv_utf8
200 #define g_unsetenv g_unsetenv_utf8
201 #define g_find_program_in_path g_find_program_in_path_utf8
202 #endif
203
204 G_CONST_RETURN gchar* g_getenv             (const gchar *variable);
205 gboolean              g_setenv             (const gchar *variable,
206                                             const gchar *value,
207                                             gboolean     overwrite);
208 void                  g_unsetenv           (const gchar *variable);
209 gchar**               g_listenv            (void);
210
211 /* we try to provide a usefull equivalent for ATEXIT if it is
212  * not defined, but use is actually abandoned. people should
213  * use g_atexit() instead.
214  */
215 typedef void            (*GVoidFunc)            (void);
216 #ifndef ATEXIT
217 # define ATEXIT(proc)   g_ATEXIT(proc)
218 #else
219 # define G_NATIVE_ATEXIT
220 #endif /* ATEXIT */
221 /* we use a GLib function as a replacement for ATEXIT, so
222  * the programmer is not required to check the return value
223  * (if there is any in the implementation) and doesn't encounter
224  * missing include files.
225  */
226 void    g_atexit                (GVoidFunc    func);
227
228 /* Look for an executable in PATH, following execvp() rules */
229 gchar*  g_find_program_in_path  (const gchar *program);
230
231 /* Bit tests
232  */
233 G_INLINE_FUNC gint      g_bit_nth_lsf (gulong  mask,
234                                        gint    nth_bit);
235 G_INLINE_FUNC gint      g_bit_nth_msf (gulong  mask,
236                                        gint    nth_bit);
237 G_INLINE_FUNC guint     g_bit_storage (gulong  number);
238
239 /* Trash Stacks
240  * elements need to be >= sizeof (gpointer)
241  */
242 typedef struct _GTrashStack     GTrashStack;
243 struct _GTrashStack
244 {
245   GTrashStack *next;
246 };
247
248 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
249                                                  gpointer      data_p);
250 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
251 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
252 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
253
254 /* inline function implementations
255  */
256 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
257 G_INLINE_FUNC gint
258 g_bit_nth_lsf (gulong mask,
259                gint   nth_bit)
260 {
261   do
262     {
263       nth_bit++;
264       if (mask & (1UL << nth_bit))
265         return nth_bit;
266     }
267   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1));
268   return -1;
269 }
270 G_INLINE_FUNC gint
271 g_bit_nth_msf (gulong mask,
272                gint   nth_bit)
273 {
274   if (nth_bit < 0)
275     nth_bit = GLIB_SIZEOF_LONG * 8;
276   do
277     {
278       nth_bit--;
279       if (mask & (1UL << nth_bit))
280         return nth_bit;
281     }
282   while (nth_bit > 0);
283   return -1;
284 }
285 G_INLINE_FUNC guint
286 g_bit_storage (gulong number)
287 {
288   register guint n_bits = 0;
289   
290   do
291     {
292       n_bits++;
293       number >>= 1;
294     }
295   while (number);
296   return n_bits;
297 }
298 G_INLINE_FUNC void
299 g_trash_stack_push (GTrashStack **stack_p,
300                     gpointer      data_p)
301 {
302   GTrashStack *data = (GTrashStack *) data_p;
303
304   data->next = *stack_p;
305   *stack_p = data;
306 }
307 G_INLINE_FUNC gpointer
308 g_trash_stack_pop (GTrashStack **stack_p)
309 {
310   GTrashStack *data;
311
312   data = *stack_p;
313   if (data)
314     {
315       *stack_p = data->next;
316       /* NULLify private pointer here, most platforms store NULL as
317        * subsequent 0 bytes
318        */
319       data->next = NULL;
320     }
321
322   return data;
323 }
324 G_INLINE_FUNC gpointer
325 g_trash_stack_peek (GTrashStack **stack_p)
326 {
327   GTrashStack *data;
328
329   data = *stack_p;
330
331   return data;
332 }
333 G_INLINE_FUNC guint
334 g_trash_stack_height (GTrashStack **stack_p)
335 {
336   GTrashStack *data;
337   guint i = 0;
338
339   for (data = *stack_p; data; data = data->next)
340     i++;
341
342   return i;
343 }
344 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
345
346 /* Glib version.
347  * we prefix variable declarations so they can
348  * properly get exported in windows dlls.
349  */
350 GLIB_VAR const guint glib_major_version;
351 GLIB_VAR const guint glib_minor_version;
352 GLIB_VAR const guint glib_micro_version;
353 GLIB_VAR const guint glib_interface_age;
354 GLIB_VAR const guint glib_binary_age;
355
356 const gchar * glib_check_version (guint required_major,
357                                   guint required_minor,
358                                   guint required_micro);
359
360 #define GLIB_CHECK_VERSION(major,minor,micro)    \
361     (GLIB_MAJOR_VERSION > (major) || \
362      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
363      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
364       GLIB_MICRO_VERSION >= (micro)))
365
366 G_END_DECLS
367
368 /*
369  * On Windows, this macro defines a DllMain function that stores the
370  * actual DLL name that the code being compiled will be included in.
371  * STATIC should be empty or 'static'. DLL_NAME is the name of the
372  * (pointer to the) char array where the DLL name will be stored. If
373  * this is used, you must also include <windows.h>. If you need a more complex
374  * DLL entry point function, you cannot use this.
375  *
376  * On non-Windows platforms, expands to nothing.
377  */
378
379 #ifndef G_PLATFORM_WIN32
380 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
381 #else
382 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                 \
383 static char *dll_name;                                                  \
384                                                                         \
385 BOOL WINAPI                                                             \
386 DllMain (HINSTANCE hinstDLL,                                            \
387          DWORD     fdwReason,                                           \
388          LPVOID    lpvReserved)                                         \
389 {                                                                       \
390   wchar_t wcbfr[1000];                                                  \
391   char cpbfr[1000];                                                     \
392   char *tem;                                                            \
393   switch (fdwReason)                                                    \
394     {                                                                   \
395     case DLL_PROCESS_ATTACH:                                            \
396       if (GetVersion () < 0x80000000)                                   \
397         {                                                               \
398           GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
399           tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);          \
400           dll_name = g_path_get_basename (tem);                         \
401           g_free (tem);                                                 \
402         }                                                               \
403       else                                                              \
404         {                                                               \
405           GetModuleFileNameA ((HMODULE) hinstDLL, cpbfr, G_N_ELEMENTS (cpbfr)); \
406           tem = g_locale_to_utf8 (cpbfr, -1, NULL, NULL, NULL);         \
407           dll_name = g_path_get_basename (tem);                         \
408           g_free (tem);                                                 \
409         }                                                               \
410       break;                                                            \
411     }                                                                   \
412                                                                         \
413   return TRUE;                                                          \
414 }
415 #endif /* G_PLATFORM_WIN32 */
416
417 #endif /* __G_UTILS_H__ */