1 gutils.h |
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public |
|
16 * License along with this library; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 /* |
|
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
23 * file for a list of people on the GLib Team. See the ChangeLog |
|
24 * files for a list of changes. These files are distributed with |
|
25 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
26 */ |
|
27 |
|
28 #ifndef __G_UTILS_H__ |
|
29 #define __G_UTILS_H__ |
|
30 |
|
31 #include <_ansi.h> |
|
32 #include <glib/gtypes.h> |
|
33 #include <stdarg.h> |
|
34 |
|
35 G_BEGIN_DECLS |
|
36 |
|
37 #if defined(G_OS_WIN32) || defined(__SYMBIAN32__) |
|
38 |
|
39 /* On Win32, the canonical directory separator is the backslash, and |
|
40 * the search path separator is the semicolon. Note that also the |
|
41 * (forward) slash works as directory separator. |
|
42 */ |
|
43 #define G_DIR_SEPARATOR '\\' |
|
44 #define G_DIR_SEPARATOR_S "\\" |
|
45 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/') |
|
46 #define G_SEARCHPATH_SEPARATOR ';' |
|
47 #define G_SEARCHPATH_SEPARATOR_S ";" |
|
48 |
|
49 #else /* !G_OS_WIN32 */ |
|
50 |
|
51 /* Unix */ |
|
52 |
|
53 #define G_DIR_SEPARATOR '/' |
|
54 #define G_DIR_SEPARATOR_S "/" |
|
55 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR) |
|
56 #define G_SEARCHPATH_SEPARATOR ':' |
|
57 #define G_SEARCHPATH_SEPARATOR_S ":" |
|
58 |
|
59 #endif /* !G_OS_WIN32 */ |
|
60 |
|
61 /* Define G_VA_COPY() to do the right thing for copying va_list variables. |
|
62 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy. |
|
63 */ |
|
64 #if !defined (G_VA_COPY) |
|
65 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32)) |
|
66 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2)) |
|
67 # elif defined (G_VA_COPY_AS_ARRAY) |
|
68 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list)) |
|
69 # else /* va_list is a pointer */ |
|
70 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2)) |
|
71 # endif /* va_list is a pointer */ |
|
72 #endif /* !G_VA_COPY */ |
|
73 |
|
74 /* inlining hassle. for compilers that don't allow the `inline' keyword, |
|
75 * mostly because of strict ANSI C compliance or dumbness, we try to fall |
|
76 * back to either `__inline__' or `__inline'. |
|
77 * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be |
|
78 * actually *capable* to do function inlining, in which case inline |
|
79 * function bodies do make sense. we also define G_INLINE_FUNC to properly |
|
80 * export the function prototypes if no inlining can be performed. |
|
81 * inline function bodies have to be special cased with G_CAN_INLINE and a |
|
82 * .c file specific macro to allow one compiled instance with extern linkage |
|
83 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro. |
|
84 */ |
|
85 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__) |
|
86 # undef inline |
|
87 # define inline __inline__ |
|
88 #elif !defined (G_HAVE_INLINE) |
|
89 # undef inline |
|
90 # if defined (G_HAVE___INLINE__) |
|
91 # define inline __inline__ |
|
92 # elif defined (G_HAVE___INLINE) |
|
93 # define inline __inline |
|
94 # else /* !inline && !__inline__ && !__inline */ |
|
95 # define inline /* don't inline, then */ |
|
96 # endif |
|
97 #endif |
|
98 #ifdef G_IMPLEMENT_INLINES |
|
99 # define G_INLINE_FUNC |
|
100 # undef G_CAN_INLINE |
|
101 #elif defined (__GNUC__) |
|
102 # ifdef __SYMBIAN32__ |
|
103 # define G_INLINE_FUNC extern __inline |
|
104 # else |
|
105 # define G_INLINE_FUNC extern inline |
|
106 # endif |
|
107 #elif defined (G_CAN_INLINE) |
|
108 # ifdef __SYMBIAN32__ |
|
109 # define G_INLINE_FUNC static __inline |
|
110 # else |
|
111 # define G_INLINE_FUNC static inline |
|
112 # endif |
|
113 #else /* can't inline */ |
|
114 # define G_INLINE_FUNC |
|
115 #endif /* !G_INLINE_FUNC */ |
|
116 |
|
117 /* Retrive static string info |
|
118 */ |
|
119 #ifdef G_OS_WIN32 |
|
120 #define g_get_user_name g_get_user_name_utf8 |
|
121 #define g_get_real_name g_get_real_name_utf8 |
|
122 #define g_get_home_dir g_get_home_dir_utf8 |
|
123 #define g_get_tmp_dir g_get_tmp_dir_utf8 |
|
124 #endif |
|
125 |
|
126 IMPORT_C G_CONST_RETURN gchar* g_get_user_name (void); |
|
127 IMPORT_C G_CONST_RETURN gchar* g_get_real_name (void); |
|
128 IMPORT_C G_CONST_RETURN gchar* g_get_home_dir (void); |
|
129 IMPORT_C G_CONST_RETURN gchar* g_get_tmp_dir (void); |
|
130 IMPORT_C G_CONST_RETURN gchar* g_get_host_name (void); |
|
131 IMPORT_C gchar* g_get_prgname (void); |
|
132 IMPORT_C void g_set_prgname (const gchar *prgname); |
|
133 IMPORT_C G_CONST_RETURN gchar* g_get_application_name (void); |
|
134 IMPORT_C void g_set_application_name (const gchar *application_name); |
|
135 |
|
136 IMPORT_C G_CONST_RETURN gchar* g_get_user_data_dir (void); |
|
137 IMPORT_C G_CONST_RETURN gchar* g_get_user_config_dir (void); |
|
138 IMPORT_C G_CONST_RETURN gchar* g_get_user_cache_dir (void); |
|
139 IMPORT_C G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs (void); |
|
140 |
|
141 #ifdef G_OS_WIN32 |
|
142 G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address); |
|
143 #endif |
|
144 |
|
145 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus) |
|
146 static inline G_CONST_RETURN gchar * G_CONST_RETURN * |
|
147 g_win32_get_system_data_dirs (void) |
|
148 { |
|
149 return g_win32_get_system_data_dirs_for_module ((gconstpointer) &g_win32_get_system_data_dirs); |
|
150 } |
|
151 #define g_get_system_data_dirs g_win32_get_system_data_dirs |
|
152 #endif |
|
153 |
|
154 IMPORT_C G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void); |
|
155 |
|
156 IMPORT_C G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void); |
|
157 |
|
158 typedef struct _GDebugKey GDebugKey; |
|
159 struct _GDebugKey |
|
160 { |
|
161 gchar *key; |
|
162 guint value; |
|
163 }; |
|
164 |
|
165 /* Miscellaneous utility functions |
|
166 */ |
|
167 IMPORT_C guint g_parse_debug_string (const gchar *string, |
|
168 const GDebugKey *keys, |
|
169 guint nkeys); |
|
170 |
|
171 IMPORT_C gint g_snprintf (gchar *string, |
|
172 gulong n, |
|
173 gchar const *format, |
|
174 ...) G_GNUC_PRINTF (3, 4); |
|
175 IMPORT_C gint g_vsnprintf (gchar *string, |
|
176 gulong n, |
|
177 gchar const *format, |
|
178 va_list args); |
|
179 |
|
180 /* Check if a file name is an absolute path */ |
|
181 IMPORT_C gboolean g_path_is_absolute (const gchar *file_name); |
|
182 |
|
183 /* In case of absolute paths, skip the root part */ |
|
184 IMPORT_C G_CONST_RETURN gchar* g_path_skip_root (const gchar *file_name); |
|
185 |
|
186 #ifndef G_DISABLE_DEPRECATED |
|
187 |
|
188 /* These two functions are deprecated and will be removed in the next |
|
189 * major release of GLib. Use g_path_get_dirname/g_path_get_basename |
|
190 * instead. Whatch out! The string returned by g_path_get_basename |
|
191 * must be g_freed, while the string returned by g_basename must not.*/ |
|
192 IMPORT_C G_CONST_RETURN gchar* g_basename (const gchar *file_name); |
|
193 #define g_dirname g_path_get_dirname |
|
194 |
|
195 #endif /* G_DISABLE_DEPRECATED */ |
|
196 |
|
197 #ifdef G_OS_WIN32 |
|
198 #define g_get_current_dir g_get_current_dir_utf8 |
|
199 #endif |
|
200 |
|
201 /* The returned strings are newly allocated with g_malloc() */ |
|
202 IMPORT_C gchar* g_get_current_dir (void); |
|
203 IMPORT_C gchar* g_path_get_basename (const gchar *file_name) G_GNUC_MALLOC; |
|
204 IMPORT_C gchar* g_path_get_dirname (const gchar *file_name) G_GNUC_MALLOC; |
|
205 |
|
206 /* Set the pointer at the specified location to NULL */ |
|
207 IMPORT_C void g_nullify_pointer (gpointer *nullify_location); |
|
208 |
|
209 /* return the environment string for the variable. The returned memory |
|
210 * must not be freed. */ |
|
211 #ifdef G_OS_WIN32 |
|
212 #define g_getenv g_getenv_utf8 |
|
213 #define g_setenv g_setenv_utf8 |
|
214 #define g_unsetenv g_unsetenv_utf8 |
|
215 #define g_find_program_in_path g_find_program_in_path_utf8 |
|
216 #endif |
|
217 |
|
218 IMPORT_C G_CONST_RETURN gchar* g_getenv (const gchar *variable); |
|
219 IMPORT_C gboolean g_setenv (const gchar *variable, |
|
220 const gchar *value, |
|
221 gboolean overwrite); |
|
222 IMPORT_C void g_unsetenv (const gchar *variable); |
|
223 IMPORT_C gchar** g_listenv (void); |
|
224 const gchar* _g_getenv_nomalloc (const gchar *variable, |
|
225 gchar buffer[1024]); |
|
226 |
|
227 /* we try to provide a usefull equivalent for ATEXIT if it is |
|
228 * not defined, but use is actually abandoned. people should |
|
229 * use g_atexit() instead. |
|
230 */ |
|
231 typedef void (*GVoidFunc) (void); |
|
232 #ifndef ATEXIT |
|
233 # define ATEXIT(proc) g_ATEXIT(proc) |
|
234 #else |
|
235 # define G_NATIVE_ATEXIT |
|
236 #endif /* ATEXIT */ |
|
237 /* we use a GLib function as a replacement for ATEXIT, so |
|
238 * the programmer is not required to check the return value |
|
239 * (if there is any in the implementation) and doesn't encounter |
|
240 * missing include files. |
|
241 */ |
|
242 IMPORT_C void g_atexit (GVoidFunc func); |
|
243 |
|
244 #ifdef G_OS_WIN32 |
|
245 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls |
|
246 * atexit(), the function will be called when the GLib DLL is detached |
|
247 * from the program, which is not what the caller wants. The caller |
|
248 * wants the function to be called when it *itself* exits (or is |
|
249 * detached, in case the caller, too, is a DLL). |
|
250 */ |
|
251 int atexit (void (*)(void)); |
|
252 #define g_atexit(func) atexit(func) |
|
253 #endif |
|
254 |
|
255 /* Look for an executable in PATH, following execvp() rules */ |
|
256 IMPORT_C gchar* g_find_program_in_path (const gchar *program); |
|
257 |
|
258 /* Bit tests |
|
259 */ |
|
260 G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask, |
|
261 gint nth_bit); |
|
262 G_INLINE_FUNC gint g_bit_nth_msf (gulong mask, |
|
263 gint nth_bit); |
|
264 G_INLINE_FUNC guint g_bit_storage (gulong number); |
|
265 |
|
266 /* Trash Stacks |
|
267 * elements need to be >= sizeof (gpointer) |
|
268 */ |
|
269 typedef struct _GTrashStack GTrashStack; |
|
270 struct _GTrashStack |
|
271 { |
|
272 GTrashStack *next; |
|
273 }; |
|
274 |
|
275 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p, |
|
276 gpointer data_p); |
|
277 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p); |
|
278 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p); |
|
279 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p); |
|
280 |
|
281 /* inline function implementations |
|
282 */ |
|
283 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__) |
|
284 G_INLINE_FUNC gint |
|
285 g_bit_nth_lsf (gulong mask, |
|
286 gint nth_bit) |
|
287 { |
|
288 do |
|
289 { |
|
290 nth_bit++; |
|
291 if (mask & (1UL << nth_bit)) |
|
292 return nth_bit; |
|
293 } |
|
294 while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1)); |
|
295 return -1; |
|
296 } |
|
297 G_INLINE_FUNC gint |
|
298 g_bit_nth_msf (gulong mask, |
|
299 gint nth_bit) |
|
300 { |
|
301 if (nth_bit < 0) |
|
302 nth_bit = GLIB_SIZEOF_LONG * 8; |
|
303 do |
|
304 { |
|
305 nth_bit--; |
|
306 if (mask & (1UL << nth_bit)) |
|
307 return nth_bit; |
|
308 } |
|
309 while (nth_bit > 0); |
|
310 return -1; |
|
311 } |
|
312 G_INLINE_FUNC guint |
|
313 g_bit_storage (gulong number) |
|
314 { |
|
315 register guint n_bits = 0; |
|
316 |
|
317 do |
|
318 { |
|
319 n_bits++; |
|
320 number >>= 1; |
|
321 } |
|
322 while (number); |
|
323 return n_bits; |
|
324 } |
|
325 G_INLINE_FUNC void |
|
326 g_trash_stack_push (GTrashStack **stack_p, |
|
327 gpointer data_p) |
|
328 { |
|
329 GTrashStack *data = (GTrashStack *) data_p; |
|
330 |
|
331 data->next = *stack_p; |
|
332 *stack_p = data; |
|
333 } |
|
334 G_INLINE_FUNC gpointer |
|
335 g_trash_stack_pop (GTrashStack **stack_p) |
|
336 { |
|
337 GTrashStack *data; |
|
338 |
|
339 data = *stack_p; |
|
340 if (data) |
|
341 { |
|
342 *stack_p = data->next; |
|
343 /* NULLify private pointer here, most platforms store NULL as |
|
344 * subsequent 0 bytes |
|
345 */ |
|
346 data->next = NULL; |
|
347 } |
|
348 |
|
349 return data; |
|
350 } |
|
351 G_INLINE_FUNC gpointer |
|
352 g_trash_stack_peek (GTrashStack **stack_p) |
|
353 { |
|
354 GTrashStack *data; |
|
355 |
|
356 data = *stack_p; |
|
357 |
|
358 return data; |
|
359 } |
|
360 G_INLINE_FUNC guint |
|
361 g_trash_stack_height (GTrashStack **stack_p) |
|
362 { |
|
363 GTrashStack *data; |
|
364 guint i = 0; |
|
365 |
|
366 for (data = *stack_p; data; data = data->next) |
|
367 i++; |
|
368 |
|
369 return i; |
|
370 } |
|
371 #endif /* G_CAN_INLINE || __G_UTILS_C__ */ |
|
372 |
|
373 /* Glib version. |
|
374 * we prefix variable declarations so they can |
|
375 * properly get exported in windows dlls. |
|
376 */ |
|
377 GLIB_VAR const guint glib_major_version; |
|
378 GLIB_VAR const guint glib_minor_version; |
|
379 GLIB_VAR const guint glib_micro_version; |
|
380 GLIB_VAR const guint glib_interface_age; |
|
381 GLIB_VAR const guint glib_binary_age; |
|
382 |
|
383 #ifdef __SYMBIAN32__ |
|
384 IMPORT_C const guint *_glib_major_version(); |
|
385 IMPORT_C const guint *_glib_minor_version(); |
|
386 IMPORT_C const guint *_glib_micro_version(); |
|
387 IMPORT_C const guint *_glib_interface_age(); |
|
388 IMPORT_C const guint *_glib_binary_age(); |
|
389 #endif /* __SYMBIAN32__ */ |
|
390 |
|
391 IMPORT_C const gchar * glib_check_version (guint required_major, |
|
392 guint required_minor, |
|
393 guint required_micro); |
|
394 |
|
395 #define GLIB_CHECK_VERSION(major,minor,micro) \ |
|
396 (GLIB_MAJOR_VERSION > (major) || \ |
|
397 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \ |
|
398 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \ |
|
399 GLIB_MICRO_VERSION >= (micro))) |
|
400 |
|
401 G_END_DECLS |
|
402 |
|
403 /* |
|
404 * On Windows, this macro defines a DllMain function that stores the |
|
405 * actual DLL name that the code being compiled will be included in. |
|
406 * STATIC should be empty or 'static'. DLL_NAME is the name of the |
|
407 * (pointer to the) char array where the DLL name will be stored. If |
|
408 * this is used, you must also include <windows.h>. If you need a more complex |
|
409 * DLL entry point function, you cannot use this. |
|
410 * |
|
411 * On non-Windows platforms, expands to nothing. |
|
412 */ |
|
413 |
|
414 #ifndef G_PLATFORM_WIN32 |
|
415 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) |
|
416 #else |
|
417 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \ |
|
418 static char *dll_name; \ |
|
419 \ |
|
420 BOOL WINAPI \ |
|
421 DllMain (HINSTANCE hinstDLL, \ |
|
422 DWORD fdwReason, \ |
|
423 LPVOID lpvReserved) \ |
|
424 { \ |
|
425 wchar_t wcbfr[1000]; \ |
|
426 char cpbfr[1000]; \ |
|
427 char *tem; \ |
|
428 switch (fdwReason) \ |
|
429 { \ |
|
430 case DLL_PROCESS_ATTACH: \ |
|
431 if (GetVersion () < 0x80000000) \ |
|
432 { \ |
|
433 GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \ |
|
434 tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \ |
|
435 dll_name = g_path_get_basename (tem); \ |
|
436 g_free (tem); \ |
|
437 } \ |
|
438 else \ |
|
439 { \ |
|
440 GetModuleFileNameA ((HMODULE) hinstDLL, cpbfr, G_N_ELEMENTS (cpbfr)); \ |
|
441 tem = g_locale_to_utf8 (cpbfr, -1, NULL, NULL, NULL); \ |
|
442 dll_name = g_path_get_basename (tem); \ |
|
443 g_free (tem); \ |
|
444 } \ |
|
445 break; \ |
|
446 } \ |
|
447 \ |
|
448 return TRUE; \ |
|
449 } |
|
450 #endif /* G_PLATFORM_WIN32 */ |
|
451 |
|
452 #endif /* __G_UTILS_H__ */ |