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