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