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