Readded GFreeFunc, g_node_insert_after and g_find_program_in_path resp.,
[glib.git] / gutils.h
blobc4dcb1b8acad6591a32c3bcb1fec368a33d85f7b
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
27 #ifndef __G_UTILS_H__
28 #define __G_UTILS_H__
30 #include <gtypes.h>
31 #include <stdarg.h>
33 G_BEGIN_DECLS
35 #ifdef G_OS_WIN32
37 /* On native Win32, directory separator is the backslash, and search path
38 * separator is the semicolon.
40 #define G_DIR_SEPARATOR '\\'
41 #define G_DIR_SEPARATOR_S "\\"
42 #define G_SEARCHPATH_SEPARATOR ';'
43 #define G_SEARCHPATH_SEPARATOR_S ";"
45 #else /* !G_OS_WIN32 */
47 /* Unix */
49 #define G_DIR_SEPARATOR '/'
50 #define G_DIR_SEPARATOR_S "/"
51 #define G_SEARCHPATH_SEPARATOR ':'
52 #define G_SEARCHPATH_SEPARATOR_S ":"
54 #endif /* !G_OS_WIN32 */
56 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
57 * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
59 #if !defined (G_VA_COPY)
60 # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
61 # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
62 # elif defined (G_VA_COPY_AS_ARRAY)
63 # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
64 # else /* va_list is a pointer */
65 # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
66 # endif /* va_list is a pointer */
67 #endif /* !G_VA_COPY */
69 /* inlining hassle. for compilers that don't allow the `inline' keyword,
70 * mostly because of strict ANSI C compliance or dumbness, we try to fall
71 * back to either `__inline__' or `__inline'.
72 * we define G_CAN_INLINE, if the compiler seems to be actually
73 * *capable* to do function inlining, in which case inline function bodys
74 * do make sense. we also define G_INLINE_FUNC to properly export the
75 * function prototypes if no inlining can be performed.
76 * inline function bodies have to be special cased with G_CAN_INLINE and a
77 * .c file specific macro to allow one compiled instance with extern linkage
78 * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
80 #ifdef G_IMPLEMENT_INLINES
81 # define G_INLINE_FUNC extern
82 # undef G_CAN_INLINE
83 #endif
84 #ifndef G_INLINE_FUNC
85 # define G_CAN_INLINE 1
86 #endif
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 # ifndef G_INLINE_FUNC
99 # undef G_CAN_INLINE
100 # endif
101 # endif
102 #endif
103 #ifndef G_INLINE_FUNC
104 # if defined (__GNUC__) && (__OPTIMIZE__)
105 # define G_INLINE_FUNC extern inline
106 # elif defined (G_CAN_INLINE) && !defined (__GNUC__)
107 # define G_INLINE_FUNC static inline
108 # else /* can't inline */
109 # define G_INLINE_FUNC extern
110 # undef G_CAN_INLINE
111 # endif
112 #endif /* !G_INLINE_FUNC */
114 /* Retrive static string info
116 gchar* g_get_user_name (void);
117 gchar* g_get_real_name (void);
118 gchar* g_get_home_dir (void);
119 gchar* g_get_tmp_dir (void);
120 gchar* g_get_prgname (void);
121 void g_set_prgname (const gchar *prgname);
124 typedef struct _GDebugKey GDebugKey;
125 struct _GDebugKey
127 gchar *key;
128 guint value;
131 /* Miscellaneous utility functions
133 guint g_parse_debug_string (const gchar *string,
134 GDebugKey *keys,
135 guint nkeys);
136 gint g_snprintf (gchar *string,
137 gulong n,
138 gchar const *format,
139 ...) G_GNUC_PRINTF (3, 4);
140 gint g_vsnprintf (gchar *string,
141 gulong n,
142 gchar const *format,
143 va_list args);
144 /* Check if a file name is an absolute path */
145 gboolean g_path_is_absolute (const gchar *file_name);
146 /* In case of absolute paths, skip the root part */
147 gchar* g_path_skip_root (gchar *file_name);
149 /* These two functions are deprecated and will be removed in the next
150 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
151 * instead. Whatch out! The string returned by g_path_get_basename
152 * must be g_freed, while the string returned by g_basename must not.*/
153 gchar* g_basename (const gchar *file_name);
154 gchar* g_dirname (const gchar *file_name);
156 /* The returned strings are newly allocated with g_malloc() */
157 gchar* g_get_current_dir (void);
158 gchar* g_path_get_basename (const gchar *file_name);
159 gchar* g_path_get_dirname (const gchar *file_name);
161 /* Get the codeset for the current locale */
162 /* gchar * g_get_codeset (void); */
164 /* return the environment string for the variable. The returned memory
165 * must not be freed. */
166 gchar* g_getenv (const gchar *variable);
169 /* we try to provide a usefull equivalent for ATEXIT if it is
170 * not defined, but use is actually abandoned. people should
171 * use g_atexit() instead.
173 typedef void (*GVoidFunc) (void);
174 #ifndef ATEXIT
175 # define ATEXIT(proc) g_ATEXIT(proc)
176 #else
177 # define G_NATIVE_ATEXIT
178 #endif /* ATEXIT */
179 /* we use a GLib function as a replacement for ATEXIT, so
180 * the programmer is not required to check the return value
181 * (if there is any in the implementation) and doesn't encounter
182 * missing include files.
184 void g_atexit (GVoidFunc func);
186 /* Look for an executable in PATH, following execvp() rules */
187 gchar* g_find_program_in_path (const gchar *program);
189 /* Bit tests
191 G_INLINE_FUNC gint g_bit_nth_lsf (guint32 mask,
192 gint nth_bit);
193 G_INLINE_FUNC gint g_bit_nth_msf (guint32 mask,
194 gint nth_bit);
195 G_INLINE_FUNC guint g_bit_storage (guint number);
197 /* Trash Stacks
198 * elements need to be >= sizeof (gpointer)
200 typedef struct _GTrashStack GTrashStack;
201 struct _GTrashStack
203 GTrashStack *next;
206 G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
207 gpointer data_p);
208 G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
209 G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
210 G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
212 /* inline function implementations
214 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
215 G_INLINE_FUNC gint
216 g_bit_nth_lsf (guint32 mask,
217 gint nth_bit)
221 nth_bit++;
222 if (mask & (1 << (guint) nth_bit))
223 return nth_bit;
225 while (nth_bit < 32);
226 return -1;
228 G_INLINE_FUNC gint
229 g_bit_nth_msf (guint32 mask,
230 gint nth_bit)
232 if (nth_bit < 0)
233 nth_bit = 32;
236 nth_bit--;
237 if (mask & (1 << (guint) nth_bit))
238 return nth_bit;
240 while (nth_bit > 0);
241 return -1;
243 G_INLINE_FUNC guint
244 g_bit_storage (guint number)
246 register guint n_bits = 0;
250 n_bits++;
251 number >>= 1;
253 while (number);
254 return n_bits;
256 G_INLINE_FUNC void
257 g_trash_stack_push (GTrashStack **stack_p,
258 gpointer data_p)
260 GTrashStack *data = (GTrashStack *) data_p;
262 data->next = *stack_p;
263 *stack_p = data;
265 G_INLINE_FUNC gpointer
266 g_trash_stack_pop (GTrashStack **stack_p)
268 GTrashStack *data;
270 data = *stack_p;
271 if (data)
273 *stack_p = data->next;
274 /* NULLify private pointer here, most platforms store NULL as
275 * subsequent 0 bytes
277 data->next = NULL;
280 return data;
282 G_INLINE_FUNC gpointer
283 g_trash_stack_peek (GTrashStack **stack_p)
285 GTrashStack *data;
287 data = *stack_p;
289 return data;
291 G_INLINE_FUNC guint
292 g_trash_stack_height (GTrashStack **stack_p)
294 GTrashStack *data;
295 guint i = 0;
297 for (data = *stack_p; data; data = data->next)
298 i++;
300 return i;
302 #endif /* G_CAN_INLINE || __G_UTILS_C__ */
304 /* Glib version.
305 * we prefix variable declarations so they can
306 * properly get exported in windows dlls.
308 #ifdef G_OS_WIN32
309 # ifdef GLIB_COMPILATION
310 # define GLIB_VAR __declspec(dllexport)
311 # else /* !GLIB_COMPILATION */
312 # define GLIB_VAR extern __declspec(dllimport)
313 # endif /* !GLIB_COMPILATION */
314 #else /* !G_OS_WIN32 */
315 # define GLIB_VAR extern
316 #endif /* !G_OS_WIN32 */
318 GLIB_VAR const guint glib_major_version;
319 GLIB_VAR const guint glib_minor_version;
320 GLIB_VAR const guint glib_micro_version;
321 GLIB_VAR const guint glib_interface_age;
322 GLIB_VAR const guint glib_binary_age;
324 #define GLIB_CHECK_VERSION(major,minor,micro) \
325 (GLIB_MAJOR_VERSION > (major) || \
326 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
327 (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
328 GLIB_MICRO_VERSION >= (micro)))
330 G_END_DECLS
332 #endif /* __G_UTILS_H__ */