b3657011d4880c2ce9557f95ff45633388e70626
[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 #if !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 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
40  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
41  */
42 #if !defined (G_VA_COPY)
43 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
44 #    define G_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
45 #  elif defined (G_VA_COPY_AS_ARRAY)
46 #    define G_VA_COPY(ap1, ap2)   g_memmove ((ap1), (ap2), sizeof (va_list))
47 #  else /* va_list is a pointer */
48 #    define G_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
49 #  endif /* va_list is a pointer */
50 #endif /* !G_VA_COPY */
51
52 /* inlining hassle. for compilers that don't allow the `inline' keyword,
53  * mostly because of strict ANSI C compliance or dumbness, we try to fall
54  * back to either `__inline__' or `__inline'.
55  * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be 
56  * actually *capable* to do function inlining, in which case inline 
57  * function bodies do make sense. we also define G_INLINE_FUNC to properly 
58  * export the function prototypes if no inlining can be performed.
59  * inline function bodies have to be special cased with G_CAN_INLINE and a
60  * .c file specific macro to allow one compiled instance with extern linkage
61  * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
62  */
63 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
64 #  undef inline
65 #  define inline __inline__
66 #elif !defined (G_HAVE_INLINE)
67 #  undef inline
68 #  if defined (G_HAVE___INLINE__)
69 #    define inline __inline__
70 #  elif defined (G_HAVE___INLINE)
71 #    define inline __inline
72 #  else /* !inline && !__inline__ && !__inline */
73 #    define inline  /* don't inline, then */
74 #  endif
75 #endif
76 #ifdef G_IMPLEMENT_INLINES
77 #  define G_INLINE_FUNC
78 #  undef  G_CAN_INLINE
79 #elif defined (__GNUC__) 
80 #  define G_INLINE_FUNC static __inline __attribute__ ((unused))
81 #elif defined (G_CAN_INLINE) 
82 #  define G_INLINE_FUNC static inline
83 #else /* can't inline */
84 #  define G_INLINE_FUNC
85 #endif /* !G_INLINE_FUNC */
86
87 #ifndef __GTK_DOC_IGNORE__
88 #ifdef G_OS_WIN32
89 #define g_get_user_name g_get_user_name_utf8
90 #define g_get_real_name g_get_real_name_utf8
91 #define g_get_home_dir g_get_home_dir_utf8
92 #define g_get_tmp_dir g_get_tmp_dir_utf8
93 #endif
94 #endif
95
96 const gchar *         g_get_user_name        (void);
97 const gchar *         g_get_real_name        (void);
98 const gchar *         g_get_home_dir         (void);
99 const gchar *         g_get_tmp_dir          (void);
100 const gchar *         g_get_host_name        (void);
101 gchar *               g_get_prgname          (void);
102 void                  g_set_prgname          (const gchar *prgname);
103 const gchar *         g_get_application_name (void);
104 void                  g_set_application_name (const gchar *application_name);
105
106 void      g_reload_user_special_dirs_cache     (void);
107 const gchar *         g_get_user_data_dir      (void);
108 const gchar *         g_get_user_config_dir    (void);
109 const gchar *         g_get_user_cache_dir     (void);
110 const gchar * const * g_get_system_data_dirs   (void);
111
112 #ifdef G_OS_WIN32
113 /* This functions is not part of the public GLib API */
114 const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
115 #endif
116
117 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
118 /* This function is not part of the public GLib API either. Just call
119  * g_get_system_data_dirs() in your code, never mind that that is
120  * actually a macro and you will in fact call this inline function.
121  */
122 static inline const gchar * const *
123 _g_win32_get_system_data_dirs (void)
124 {
125   return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
126 }
127 #define g_get_system_data_dirs _g_win32_get_system_data_dirs
128 #endif
129
130 const gchar * const * g_get_system_config_dirs (void);
131
132 const gchar * g_get_user_runtime_dir (void);
133
134 /**
135  * GUserDirectory:
136  * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
137  * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
138  * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
139  * @G_USER_DIRECTORY_MUSIC: the user's Music directory
140  * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
141  * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
142  * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
143  * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
144  * @G_USER_N_DIRECTORIES: the number of enum values
145  *
146  * These are logical ids for special directories which are defined
147  * depending on the platform used. You should use g_get_user_special_dir()
148  * to retrieve the full path associated to the logical id.
149  *
150  * The #GUserDirectory enumeration can be extended at later date. Not
151  * every platform has a directory for every logical id in this
152  * enumeration.
153  *
154  * Since: 2.14
155  */
156 typedef enum {
157   G_USER_DIRECTORY_DESKTOP,
158   G_USER_DIRECTORY_DOCUMENTS,
159   G_USER_DIRECTORY_DOWNLOAD,
160   G_USER_DIRECTORY_MUSIC,
161   G_USER_DIRECTORY_PICTURES,
162   G_USER_DIRECTORY_PUBLIC_SHARE,
163   G_USER_DIRECTORY_TEMPLATES,
164   G_USER_DIRECTORY_VIDEOS,
165
166   G_USER_N_DIRECTORIES
167 } GUserDirectory;
168
169 const gchar * g_get_user_special_dir (GUserDirectory directory);
170
171 /**
172  * GDebugKey:
173  * @key: the string
174  * @value: the flag
175  *
176  * Associates a string with a bit flag.
177  * Used in g_parse_debug_string().
178  */
179 typedef struct _GDebugKey GDebugKey;
180 struct _GDebugKey
181 {
182   const gchar *key;
183   guint        value;
184 };
185
186 /* Miscellaneous utility functions
187  */
188 guint                 g_parse_debug_string (const gchar     *string,
189                                             const GDebugKey *keys,
190                                             guint            nkeys);
191
192 gint                  g_snprintf           (gchar       *string,
193                                             gulong       n,
194                                             gchar const *format,
195                                             ...) G_GNUC_PRINTF (3, 4);
196 gint                  g_vsnprintf          (gchar       *string,
197                                             gulong       n,
198                                             gchar const *format,
199                                             va_list      args);
200
201 void                  g_nullify_pointer    (gpointer    *nullify_location);
202
203 typedef enum
204 {
205   G_FORMAT_SIZE_DEFAULT     = 0,
206   G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
207   G_FORMAT_SIZE_IEC_UNITS   = 1 << 1
208 } GFormatSizeFlags;
209
210 gchar *g_format_size_full   (guint64          size,
211                              GFormatSizeFlags flags);
212 gchar *g_format_size        (guint64          size);
213
214 #ifndef G_DISABLE_DEPRECATED
215 GLIB_DEPRECATED_FOR(g_format_size)
216 gchar *g_format_size_for_display (goffset size);
217
218 /**
219  * GVoidFunc:
220  *
221  * Declares a type of function which takes no arguments
222  * and has no return value. It is used to specify the type
223  * function passed to g_atexit().
224  */
225 typedef void (*GVoidFunc) (void);
226 #ifndef ATEXIT
227 # define ATEXIT(proc) g_ATEXIT(proc)
228 #else
229 # define G_NATIVE_ATEXIT
230 #endif /* ATEXIT */
231 /* we use a GLib function as a replacement for ATEXIT, so
232  * the programmer is not required to check the return value
233  * (if there is any in the implementation) and doesn't encounter
234  * missing include files.
235  */
236 GLIB_DEPRECATED
237 void    g_atexit                (GVoidFunc    func);
238
239 #ifdef G_OS_WIN32
240 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
241  * atexit(), the function will be called when the GLib DLL is detached
242  * from the program, which is not what the caller wants. The caller
243  * wants the function to be called when it *itself* exits (or is
244  * detached, in case the caller, too, is a DLL).
245  */
246 #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
247 int atexit (void (*)(void));
248 #endif
249 #define g_atexit(func) atexit(func)
250 #endif
251
252 #endif  /* G_DISABLE_DEPRECATED */
253
254 #ifndef __GTK_DOC_IGNORE__
255 #ifdef G_OS_WIN32
256 #define g_find_program_in_path g_find_program_in_path_utf8
257 #endif
258 #endif
259
260 /* Look for an executable in PATH, following execvp() rules */
261 gchar*  g_find_program_in_path  (const gchar *program);
262
263 /* Bit tests
264  */
265 G_INLINE_FUNC gint      g_bit_nth_lsf (gulong  mask,
266                                        gint    nth_bit) G_GNUC_CONST;
267 G_INLINE_FUNC gint      g_bit_nth_msf (gulong  mask,
268                                        gint    nth_bit) G_GNUC_CONST;
269 G_INLINE_FUNC guint     g_bit_storage (gulong  number) G_GNUC_CONST;
270
271 /* inline function implementations
272  */
273 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
274 G_INLINE_FUNC gint
275 g_bit_nth_lsf (gulong mask,
276                gint   nth_bit)
277 {
278   if (G_UNLIKELY (nth_bit < -1))
279     nth_bit = -1;
280   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
281     {
282       nth_bit++;
283       if (mask & (1UL << nth_bit))
284         return nth_bit;
285     }
286   return -1;
287 }
288 G_INLINE_FUNC gint
289 g_bit_nth_msf (gulong mask,
290                gint   nth_bit)
291 {
292   if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
293     nth_bit = GLIB_SIZEOF_LONG * 8;
294   while (nth_bit > 0)
295     {
296       nth_bit--;
297       if (mask & (1UL << nth_bit))
298         return nth_bit;
299     }
300   return -1;
301 }
302 G_INLINE_FUNC guint
303 g_bit_storage (gulong number)
304 {
305 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
306   return G_LIKELY (number) ?
307            ((GLIB_SIZEOF_LONG * 8U - 1) ^ __builtin_clzl(number)) + 1 : 1;
308 #else
309   register guint n_bits = 0;
310   
311   do
312     {
313       n_bits++;
314       number >>= 1;
315     }
316   while (number);
317   return n_bits;
318 #endif
319 }
320 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
321
322 G_END_DECLS
323
324 #ifndef G_DISABLE_DEPRECATED
325
326 /*
327  * This macro is deprecated. This DllMain() is too complex. It is
328  * recommended to write an explicit minimal DLlMain() that just saves
329  * the handle to the DLL and then use that handle instead, for
330  * instance passing it to
331  * g_win32_get_package_installation_directory_of_module().
332  *
333  * On Windows, this macro defines a DllMain function that stores the
334  * actual DLL name that the code being compiled will be included in.
335  * STATIC should be empty or 'static'. DLL_NAME is the name of the
336  * (pointer to the) char array where the DLL name will be stored. If
337  * this is used, you must also include <windows.h>. If you need a more complex
338  * DLL entry point function, you cannot use this.
339  *
340  * On non-Windows platforms, expands to nothing.
341  */
342
343 #ifndef G_PLATFORM_WIN32
344 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
345 #else
346 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                 \
347 static char *dll_name;                                                  \
348                                                                         \
349 BOOL WINAPI                                                             \
350 DllMain (HINSTANCE hinstDLL,                                            \
351          DWORD     fdwReason,                                           \
352          LPVOID    lpvReserved)                                         \
353 {                                                                       \
354   wchar_t wcbfr[1000];                                                  \
355   char *tem;                                                            \
356   switch (fdwReason)                                                    \
357     {                                                                   \
358     case DLL_PROCESS_ATTACH:                                            \
359       GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
360       tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);              \
361       dll_name = g_path_get_basename (tem);                             \
362       g_free (tem);                                                     \
363       break;                                                            \
364     }                                                                   \
365                                                                         \
366   return TRUE;                                                          \
367 }
368
369 #endif  /* !G_DISABLE_DEPRECATED */
370
371 #endif /* G_PLATFORM_WIN32 */
372
373 #endif /* __G_UTILS_H__ */