Maintain the struct order when initializing
[glib.git] / glib / gutils.h
blob530a86d8b7d2bfdd7914056425e26b8779ad2179
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #ifndef __G_UTILS_H__
26 #define __G_UTILS_H__
28 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
29 #error "Only <glib.h> can be included directly."
30 #endif
32 #include <glib/gtypes.h>
33 #include <stdarg.h>
35 G_BEGIN_DECLS
37 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
38 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
40 #if !defined (G_VA_COPY)
41 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
42 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
43 # elif defined (G_VA_COPY_AS_ARRAY)
44 # define G_VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list))
45 # else /* va_list is a pointer */
46 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
47 # endif /* va_list is a pointer */
48 #endif /* !G_VA_COPY */
50 /* inlining hassle. for compilers that don't allow the 'inline' keyword,
51 * mostly because of strict ANSI C compliance or dumbness, we try to fall
52 * back to either '__inline__' or '__inline'.
53 * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be
54 * actually *capable* to do function inlining, in which case inline
55 * function bodies do make sense. we also define G_INLINE_FUNC to properly
56 * export the function prototypes if no inlining can be performed.
57 * inline function bodies have to be special cased with G_CAN_INLINE and a
58 * .c file specific macro to allow one compiled instance with extern linkage
59 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
61 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
62 # undef inline
63 # define inline __inline__
64 #elif !defined (G_HAVE_INLINE)
65 # undef inline
66 # if defined (G_HAVE___INLINE__)
67 # define inline __inline__
68 # elif defined (G_HAVE___INLINE)
69 # define inline __inline
70 # else /* !inline && !__inline__ && !__inline */
71 # define inline /* don't inline, then */
72 # endif
73 #endif
74 #ifdef G_IMPLEMENT_INLINES
75 # define G_INLINE_FUNC _GLIB_EXTERN
76 # undef G_CAN_INLINE
77 #elif defined (__GNUC__)
78 # define G_INLINE_FUNC static __inline __attribute__ ((unused))
79 #elif defined (G_CAN_INLINE)
80 # define G_INLINE_FUNC static inline
81 #else /* can't inline */
82 # define G_INLINE_FUNC _GLIB_EXTERN
83 #endif /* !G_INLINE_FUNC */
85 GLIB_AVAILABLE_IN_ALL
86 const gchar * g_get_user_name (void);
87 GLIB_AVAILABLE_IN_ALL
88 const gchar * g_get_real_name (void);
89 GLIB_AVAILABLE_IN_ALL
90 const gchar * g_get_home_dir (void);
91 GLIB_AVAILABLE_IN_ALL
92 const gchar * g_get_tmp_dir (void);
93 GLIB_AVAILABLE_IN_ALL
94 const gchar * g_get_host_name (void);
95 GLIB_AVAILABLE_IN_ALL
96 const gchar * g_get_prgname (void);
97 GLIB_AVAILABLE_IN_ALL
98 void g_set_prgname (const gchar *prgname);
99 GLIB_AVAILABLE_IN_ALL
100 const gchar * g_get_application_name (void);
101 GLIB_AVAILABLE_IN_ALL
102 void g_set_application_name (const gchar *application_name);
104 GLIB_AVAILABLE_IN_ALL
105 void g_reload_user_special_dirs_cache (void);
106 GLIB_AVAILABLE_IN_ALL
107 const gchar * g_get_user_data_dir (void);
108 GLIB_AVAILABLE_IN_ALL
109 const gchar * g_get_user_config_dir (void);
110 GLIB_AVAILABLE_IN_ALL
111 const gchar * g_get_user_cache_dir (void);
112 GLIB_AVAILABLE_IN_ALL
113 const gchar * const * g_get_system_data_dirs (void);
115 #ifdef G_OS_WIN32
116 /* This functions is not part of the public GLib API */
117 GLIB_AVAILABLE_IN_ALL
118 const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
119 #endif
121 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
122 /* This function is not part of the public GLib API either. Just call
123 * g_get_system_data_dirs() in your code, never mind that that is
124 * actually a macro and you will in fact call this inline function.
126 static inline const gchar * const *
127 _g_win32_get_system_data_dirs (void)
129 return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
131 #define g_get_system_data_dirs _g_win32_get_system_data_dirs
132 #endif
134 GLIB_AVAILABLE_IN_ALL
135 const gchar * const * g_get_system_config_dirs (void);
137 GLIB_AVAILABLE_IN_ALL
138 const gchar * g_get_user_runtime_dir (void);
141 * GUserDirectory:
142 * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
143 * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
144 * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
145 * @G_USER_DIRECTORY_MUSIC: the user's Music directory
146 * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
147 * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
148 * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
149 * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
150 * @G_USER_N_DIRECTORIES: the number of enum values
152 * These are logical ids for special directories which are defined
153 * depending on the platform used. You should use g_get_user_special_dir()
154 * to retrieve the full path associated to the logical id.
156 * The #GUserDirectory enumeration can be extended at later date. Not
157 * every platform has a directory for every logical id in this
158 * enumeration.
160 * Since: 2.14
162 typedef enum {
163 G_USER_DIRECTORY_DESKTOP,
164 G_USER_DIRECTORY_DOCUMENTS,
165 G_USER_DIRECTORY_DOWNLOAD,
166 G_USER_DIRECTORY_MUSIC,
167 G_USER_DIRECTORY_PICTURES,
168 G_USER_DIRECTORY_PUBLIC_SHARE,
169 G_USER_DIRECTORY_TEMPLATES,
170 G_USER_DIRECTORY_VIDEOS,
172 G_USER_N_DIRECTORIES
173 } GUserDirectory;
175 GLIB_AVAILABLE_IN_ALL
176 const gchar * g_get_user_special_dir (GUserDirectory directory);
179 * GDebugKey:
180 * @key: the string
181 * @value: the flag
183 * Associates a string with a bit flag.
184 * Used in g_parse_debug_string().
186 typedef struct _GDebugKey GDebugKey;
187 struct _GDebugKey
189 const gchar *key;
190 guint value;
193 /* Miscellaneous utility functions
195 GLIB_AVAILABLE_IN_ALL
196 guint g_parse_debug_string (const gchar *string,
197 const GDebugKey *keys,
198 guint nkeys);
200 GLIB_AVAILABLE_IN_ALL
201 gint g_snprintf (gchar *string,
202 gulong n,
203 gchar const *format,
204 ...) G_GNUC_PRINTF (3, 4);
205 GLIB_AVAILABLE_IN_ALL
206 gint g_vsnprintf (gchar *string,
207 gulong n,
208 gchar const *format,
209 va_list args)
210 G_GNUC_PRINTF(3, 0);
212 GLIB_AVAILABLE_IN_ALL
213 void g_nullify_pointer (gpointer *nullify_location);
215 typedef enum
217 G_FORMAT_SIZE_DEFAULT = 0,
218 G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
219 G_FORMAT_SIZE_IEC_UNITS = 1 << 1
220 } GFormatSizeFlags;
222 GLIB_AVAILABLE_IN_2_30
223 gchar *g_format_size_full (guint64 size,
224 GFormatSizeFlags flags);
225 GLIB_AVAILABLE_IN_2_30
226 gchar *g_format_size (guint64 size);
228 GLIB_DEPRECATED_FOR(g_format_size)
229 gchar *g_format_size_for_display (goffset size);
231 #ifndef G_DISABLE_DEPRECATED
233 * GVoidFunc:
235 * Declares a type of function which takes no arguments
236 * and has no return value. It is used to specify the type
237 * function passed to g_atexit().
239 typedef void (*GVoidFunc) (void);
240 #define ATEXIT(proc) g_ATEXIT(proc)
241 GLIB_DEPRECATED
242 void g_atexit (GVoidFunc func);
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).
251 #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
252 int atexit (void (*)(void));
253 #endif
254 #define g_atexit(func) atexit(func)
255 #endif
257 #endif
260 /* Look for an executable in PATH, following execvp() rules */
261 GLIB_AVAILABLE_IN_ALL
262 gchar* g_find_program_in_path (const gchar *program);
264 /* Bit tests
266 G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask,
267 gint nth_bit) G_GNUC_CONST;
268 G_INLINE_FUNC gint g_bit_nth_msf (gulong mask,
269 gint nth_bit) G_GNUC_CONST;
270 G_INLINE_FUNC guint g_bit_storage (gulong number) G_GNUC_CONST;
272 /* inline function implementations
274 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
275 G_INLINE_FUNC gint
276 g_bit_nth_lsf (gulong mask,
277 gint nth_bit)
279 if (G_UNLIKELY (nth_bit < -1))
280 nth_bit = -1;
281 while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
283 nth_bit++;
284 if (mask & (1UL << nth_bit))
285 return nth_bit;
287 return -1;
289 G_INLINE_FUNC gint
290 g_bit_nth_msf (gulong mask,
291 gint nth_bit)
293 if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
294 nth_bit = GLIB_SIZEOF_LONG * 8;
295 while (nth_bit > 0)
297 nth_bit--;
298 if (mask & (1UL << nth_bit))
299 return nth_bit;
301 return -1;
303 G_INLINE_FUNC guint
304 g_bit_storage (gulong number)
306 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
307 return G_LIKELY (number) ?
308 ((GLIB_SIZEOF_LONG * 8U - 1) ^ (guint) __builtin_clzl(number)) + 1 : 1;
309 #else
310 guint n_bits = 0;
314 n_bits++;
315 number >>= 1;
317 while (number);
318 return n_bits;
319 #endif
321 #endif /* G_CAN_INLINE || __G_UTILS_C__ */
323 #ifndef G_DISABLE_DEPRECATED
326 * This macro is deprecated. This DllMain() is too complex. It is
327 * recommended to write an explicit minimal DLlMain() that just saves
328 * the handle to the DLL and then use that handle instead, for
329 * instance passing it to
330 * g_win32_get_package_installation_directory_of_module().
332 * On Windows, this macro defines a DllMain function that stores the
333 * actual DLL name that the code being compiled will be included in.
334 * STATIC should be empty or 'static'. DLL_NAME is the name of the
335 * (pointer to the) char array where the DLL name will be stored. If
336 * this is used, you must also include <windows.h>. If you need a more complex
337 * DLL entry point function, you cannot use this.
339 * On non-Windows platforms, expands to nothing.
342 #ifndef G_PLATFORM_WIN32
343 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
344 #else
345 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
346 static char *dll_name; \
348 BOOL WINAPI \
349 DllMain (HINSTANCE hinstDLL, \
350 DWORD fdwReason, \
351 LPVOID lpvReserved) \
353 wchar_t wcbfr[1000]; \
354 char *tem; \
355 switch (fdwReason) \
357 case DLL_PROCESS_ATTACH: \
358 GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
359 tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
360 dll_name = g_path_get_basename (tem); \
361 g_free (tem); \
362 break; \
365 return TRUE; \
368 #endif /* !G_DISABLE_DEPRECATED */
370 #endif /* G_PLATFORM_WIN32 */
372 G_END_DECLS
374 #endif /* __G_UTILS_H__ */