glib/glib.symbols glib/gutils.h Make also g_getenv(), g_setenv(),
[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 native Win32, directory separator is the backslash, and search path
38  * separator is the semicolon.
39  */
40 #define G_DIR_SEPARATOR '\\'
41 #define G_DIR_SEPARATOR_S "\\"
42 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
43 #define G_SEARCHPATH_SEPARATOR ';'
44 #define G_SEARCHPATH_SEPARATOR_S ";"
45
46 #else  /* !G_OS_WIN32 */
47
48 /* Unix */
49
50 #define G_DIR_SEPARATOR '/'
51 #define G_DIR_SEPARATOR_S "/"
52 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
53 #define G_SEARCHPATH_SEPARATOR ':'
54 #define G_SEARCHPATH_SEPARATOR_S ":"
55
56 #endif /* !G_OS_WIN32 */
57
58 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
59  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
60  */
61 #if !defined (G_VA_COPY)
62 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
63 #    define G_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
64 #  elif defined (G_VA_COPY_AS_ARRAY)
65 #    define G_VA_COPY(ap1, ap2)   g_memmove ((ap1), (ap2), sizeof (va_list))
66 #  else /* va_list is a pointer */
67 #    define G_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
68 #  endif /* va_list is a pointer */
69 #endif /* !G_VA_COPY */
70
71 /* inlining hassle. for compilers that don't allow the `inline' keyword,
72  * mostly because of strict ANSI C compliance or dumbness, we try to fall
73  * back to either `__inline__' or `__inline'.
74  * we define G_CAN_INLINE, if the compiler seems to be actually
75  * *capable* to do function inlining, in which case inline function bodys
76  * do make sense. we also define G_INLINE_FUNC to properly export the
77  * function prototypes if no inlining can be performed.
78  * inline function bodies have to be special cased with G_CAN_INLINE and a
79  * .c file specific macro to allow one compiled instance with extern linkage
80  * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
81  */
82 #ifdef G_IMPLEMENT_INLINES
83 #  define G_INLINE_FUNC extern
84 #  undef  G_CAN_INLINE
85 #endif
86 #ifndef G_INLINE_FUNC
87 #  define G_CAN_INLINE 1
88 #endif
89 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
90 #  undef inline
91 #  define inline __inline__
92 #elif !defined (G_HAVE_INLINE)
93 #  undef inline
94 #  if defined (G_HAVE___INLINE__)
95 #    define inline __inline__
96 #  elif defined (G_HAVE___INLINE)
97 #    define inline __inline
98 #  else /* !inline && !__inline__ && !__inline */
99 #    define inline  /* don't inline, then */
100 #    ifndef G_INLINE_FUNC
101 #      undef G_CAN_INLINE
102 #    endif
103 #  endif
104 #endif
105 #ifndef G_INLINE_FUNC
106 #  if defined (__GNUC__) && defined (__OPTIMIZE__)
107 #    define G_INLINE_FUNC extern inline
108 #  elif defined (G_CAN_INLINE) && !defined (__GNUC__)
109 #    define G_INLINE_FUNC static inline
110 #  else /* can't inline */
111 #    define G_INLINE_FUNC extern
112 #    undef G_CAN_INLINE
113 #  endif
114 #endif /* !G_INLINE_FUNC */
115
116 /* Retrive static string info
117  */
118 #ifdef G_OS_WIN32
119 #define g_get_user_name g_get_user_name_utf8
120 #define g_get_real_name g_get_real_name_utf8
121 #define g_get_home_dir g_get_home_dir_utf8
122 #define g_get_tmp_dir g_get_tmp_dir_utf8
123 #endif
124
125 G_CONST_RETURN gchar* g_get_user_name        (void);
126 G_CONST_RETURN gchar* g_get_real_name        (void);
127 G_CONST_RETURN gchar* g_get_home_dir         (void);
128 G_CONST_RETURN gchar* g_get_tmp_dir          (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 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
139
140 G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
141
142 typedef struct _GDebugKey       GDebugKey;
143 struct _GDebugKey
144 {
145   gchar *key;
146   guint  value;
147 };
148
149 /* Miscellaneous utility functions
150  */
151 guint                 g_parse_debug_string (const gchar     *string,
152                                             const GDebugKey *keys,
153                                             guint            nkeys);
154
155 gint                  g_snprintf           (gchar       *string,
156                                             gulong       n,
157                                             gchar const *format,
158                                             ...) G_GNUC_PRINTF (3, 4);
159 gint                  g_vsnprintf          (gchar       *string,
160                                             gulong       n,
161                                             gchar const *format,
162                                             va_list      args);
163
164 /* Check if a file name is an absolute path */
165 gboolean              g_path_is_absolute   (const gchar *file_name);
166
167 /* In case of absolute paths, skip the root part */
168 G_CONST_RETURN gchar* g_path_skip_root     (const gchar *file_name);
169
170 #ifndef G_DISABLE_DEPRECATED
171
172 /* These two functions are deprecated and will be removed in the next
173  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
174  * instead. Whatch out! The string returned by g_path_get_basename
175  * must be g_freed, while the string returned by g_basename must not.*/
176 G_CONST_RETURN gchar* g_basename           (const gchar *file_name);
177 #define g_dirname g_path_get_dirname
178
179 #endif /* G_DISABLE_DEPRECATED */
180
181 #ifdef G_OS_WIN32
182 #define g_get_current_dir g_get_current_dir_utf8
183 #endif
184
185 /* The returned strings are newly allocated with g_malloc() */
186 gchar*                g_get_current_dir    (void);
187 gchar*                g_path_get_basename  (const gchar *file_name) G_GNUC_MALLOC;
188 gchar*                g_path_get_dirname   (const gchar *file_name) G_GNUC_MALLOC;
189
190 /* Set the pointer at the specified location to NULL */
191 void                  g_nullify_pointer    (gpointer    *nullify_location);
192
193 /* return the environment string for the variable. The returned memory
194  * must not be freed. */
195 #ifdef G_OS_WIN32
196 #define g_getenv g_getenv_utf8
197 #define g_setenv g_setenv_utf8
198 #define g_unsetenv g_unsetenv_utf8
199 #define g_find_program_in_path g_find_program_in_path_utf8
200 #endif
201
202 G_CONST_RETURN gchar* g_getenv             (const gchar *variable);
203 gboolean              g_setenv             (const gchar *variable,
204                                             const gchar *value,
205                                             gboolean     overwrite);
206 void                  g_unsetenv           (const gchar *variable);
207
208
209 /* we try to provide a usefull equivalent for ATEXIT if it is
210  * not defined, but use is actually abandoned. people should
211  * use g_atexit() instead.
212  */
213 typedef void            (*GVoidFunc)            (void);
214 #ifndef ATEXIT
215 # define ATEXIT(proc)   g_ATEXIT(proc)
216 #else
217 # define G_NATIVE_ATEXIT
218 #endif /* ATEXIT */
219 /* we use a GLib function as a replacement for ATEXIT, so
220  * the programmer is not required to check the return value
221  * (if there is any in the implementation) and doesn't encounter
222  * missing include files.
223  */
224 void    g_atexit                (GVoidFunc    func);
225
226 /* Look for an executable in PATH, following execvp() rules */
227 gchar*  g_find_program_in_path  (const gchar *program);
228
229 /* Bit tests
230  */
231 G_INLINE_FUNC gint      g_bit_nth_lsf (gulong  mask,
232                                        gint    nth_bit);
233 G_INLINE_FUNC gint      g_bit_nth_msf (gulong  mask,
234                                        gint    nth_bit);
235 G_INLINE_FUNC guint     g_bit_storage (gulong  number);
236
237 /* Trash Stacks
238  * elements need to be >= sizeof (gpointer)
239  */
240 typedef struct _GTrashStack     GTrashStack;
241 struct _GTrashStack
242 {
243   GTrashStack *next;
244 };
245
246 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
247                                                  gpointer      data_p);
248 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
249 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
250 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
251
252 /* inline function implementations
253  */
254 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
255 G_INLINE_FUNC gint
256 g_bit_nth_lsf (gulong mask,
257                gint   nth_bit)
258 {
259   do
260     {
261       nth_bit++;
262       if (mask & (1UL << nth_bit))
263         return nth_bit;
264     }
265   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1));
266   return -1;
267 }
268 G_INLINE_FUNC gint
269 g_bit_nth_msf (gulong mask,
270                gint   nth_bit)
271 {
272   if (nth_bit < 0)
273     nth_bit = GLIB_SIZEOF_LONG * 8;
274   do
275     {
276       nth_bit--;
277       if (mask & (1UL << nth_bit))
278         return nth_bit;
279     }
280   while (nth_bit > 0);
281   return -1;
282 }
283 G_INLINE_FUNC guint
284 g_bit_storage (gulong number)
285 {
286   register guint n_bits = 0;
287   
288   do
289     {
290       n_bits++;
291       number >>= 1;
292     }
293   while (number);
294   return n_bits;
295 }
296 G_INLINE_FUNC void
297 g_trash_stack_push (GTrashStack **stack_p,
298                     gpointer      data_p)
299 {
300   GTrashStack *data = (GTrashStack *) data_p;
301
302   data->next = *stack_p;
303   *stack_p = data;
304 }
305 G_INLINE_FUNC gpointer
306 g_trash_stack_pop (GTrashStack **stack_p)
307 {
308   GTrashStack *data;
309
310   data = *stack_p;
311   if (data)
312     {
313       *stack_p = data->next;
314       /* NULLify private pointer here, most platforms store NULL as
315        * subsequent 0 bytes
316        */
317       data->next = NULL;
318     }
319
320   return data;
321 }
322 G_INLINE_FUNC gpointer
323 g_trash_stack_peek (GTrashStack **stack_p)
324 {
325   GTrashStack *data;
326
327   data = *stack_p;
328
329   return data;
330 }
331 G_INLINE_FUNC guint
332 g_trash_stack_height (GTrashStack **stack_p)
333 {
334   GTrashStack *data;
335   guint i = 0;
336
337   for (data = *stack_p; data; data = data->next)
338     i++;
339
340   return i;
341 }
342 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
343
344 /* Glib version.
345  * we prefix variable declarations so they can
346  * properly get exported in windows dlls.
347  */
348 GLIB_VAR const guint glib_major_version;
349 GLIB_VAR const guint glib_minor_version;
350 GLIB_VAR const guint glib_micro_version;
351 GLIB_VAR const guint glib_interface_age;
352 GLIB_VAR const guint glib_binary_age;
353
354 const gchar * glib_check_version (guint required_major,
355                                   guint required_minor,
356                                   guint required_micro);
357
358 #define GLIB_CHECK_VERSION(major,minor,micro)    \
359     (GLIB_MAJOR_VERSION > (major) || \
360      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
361      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
362       GLIB_MICRO_VERSION >= (micro)))
363
364 G_END_DECLS
365
366 /*
367  * On Windows, this macro defines a DllMain function that stores the
368  * actual DLL name that the code being compiled will be included in.
369  * STATIC should be empty or 'static'. DLL_NAME is the name of the
370  * (pointer to the) char array where the DLL name will be stored. If
371  * this is used, you must also include <windows.h>. If you need a more complex
372  * DLL entry point function, you cannot use this.
373  *
374  * On non-Windows platforms, expands to nothing.
375  */
376
377 #ifndef G_PLATFORM_WIN32
378 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
379 #else
380 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                    \
381 static char *dll_name;                                                     \
382                                                                            \
383 BOOL WINAPI                                                                \
384 DllMain (HINSTANCE hinstDLL,                                               \
385          DWORD     fdwReason,                                              \
386          LPVOID    lpvReserved)                                            \
387 {                                                                          \
388   char bfr[1000];                                                          \
389   switch (fdwReason)                                                       \
390     {                                                                      \
391     case DLL_PROCESS_ATTACH:                                               \
392       GetModuleFileName ((HMODULE) hinstDLL, bfr, sizeof (bfr));           \
393       dll_name = g_path_get_basename (bfr);                                \
394       break;                                                               \
395     }                                                                      \
396                                                                            \
397   return TRUE;                                                             \
398 }
399 #endif /* G_PLATFORM_WIN32 */
400
401 #endif /* __G_UTILS_H__ */