source: trunk/third/glib2/glib/gutils.h @ 18159

Revision 18159, 10.7 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18158, which included commits to RCS files with non-trunk default branches.
Line 
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#include <glib/gtypes.h>
31#include <stdarg.h>
32
33G_BEGIN_DECLS
34
35#ifdef G_OS_WIN32
36
37/* On native Win32, directory separator is the backslash, and search path
38 * separator is the semicolon.
39 */
40#define G_DIR_SEPARATOR '\\'
41#define G_DIR_SEPARATOR_S "\\"
42#define G_SEARCHPATH_SEPARATOR ';'
43#define G_SEARCHPATH_SEPARATOR_S ";"
44
45#else  /* !G_OS_WIN32 */
46
47/* Unix */
48
49#define G_DIR_SEPARATOR '/'
50#define G_DIR_SEPARATOR_S "/"
51#define G_SEARCHPATH_SEPARATOR ':'
52#define G_SEARCHPATH_SEPARATOR_S ":"
53
54#endif /* !G_OS_WIN32 */
55
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.
58 */
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 */
68
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.
79 */
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 */
113
114/* Retrive static string info
115 */
116G_CONST_RETURN gchar* g_get_user_name        (void);
117G_CONST_RETURN gchar* g_get_real_name        (void);
118G_CONST_RETURN gchar* g_get_home_dir         (void);
119G_CONST_RETURN gchar* g_get_tmp_dir          (void);
120gchar*                g_get_prgname          (void);
121void                  g_set_prgname          (const gchar *prgname);
122G_CONST_RETURN gchar* g_get_application_name (void);
123void                  g_set_application_name (const gchar *application_name);
124
125
126typedef struct _GDebugKey       GDebugKey;
127struct _GDebugKey
128{
129  gchar *key;
130  guint  value;
131};
132
133/* Miscellaneous utility functions
134 */
135guint                 g_parse_debug_string (const gchar     *string,
136                                            const GDebugKey *keys,
137                                            guint            nkeys);
138
139gint                  g_snprintf           (gchar       *string,
140                                            gulong       n,
141                                            gchar const *format,
142                                            ...) G_GNUC_PRINTF (3, 4);
143gint                  g_vsnprintf          (gchar       *string,
144                                            gulong       n,
145                                            gchar const *format,
146                                            va_list      args);
147
148/* Check if a file name is an absolute path */
149gboolean              g_path_is_absolute   (const gchar *file_name);
150
151/* In case of absolute paths, skip the root part */
152G_CONST_RETURN gchar* g_path_skip_root     (const gchar *file_name);
153
154#ifndef G_DISABLE_DEPRECATED
155
156/* These two functions are deprecated and will be removed in the next
157 * major release of GLib. Use g_path_get_dirname/g_path_get_basename
158 * instead. Whatch out! The string returned by g_path_get_basename
159 * must be g_freed, while the string returned by g_basename must not.*/
160G_CONST_RETURN gchar* g_basename           (const gchar *file_name);
161#define g_dirname g_path_get_dirname
162
163#endif /* G_DISABLE_DEPRECATED */
164
165/* The returned strings are newly allocated with g_malloc() */
166gchar*                g_get_current_dir    (void);
167gchar*                g_path_get_basename  (const gchar *file_name);
168gchar*                g_path_get_dirname   (const gchar *file_name);
169
170
171/* Set the pointer at the specified location to NULL */
172void                  g_nullify_pointer    (gpointer    *nullify_location);
173
174/* Get the codeset for the current locale */
175/* gchar * g_get_codeset    (void); */
176
177/* return the environment string for the variable. The returned memory
178 * must not be freed. */
179G_CONST_RETURN gchar* g_getenv             (const gchar *variable);
180
181
182/* we try to provide a usefull equivalent for ATEXIT if it is
183 * not defined, but use is actually abandoned. people should
184 * use g_atexit() instead.
185 */
186typedef void            (*GVoidFunc)            (void);
187#ifndef ATEXIT
188# define ATEXIT(proc)   g_ATEXIT(proc)
189#else
190# define G_NATIVE_ATEXIT
191#endif /* ATEXIT */
192/* we use a GLib function as a replacement for ATEXIT, so
193 * the programmer is not required to check the return value
194 * (if there is any in the implementation) and doesn't encounter
195 * missing include files.
196 */
197void    g_atexit                (GVoidFunc    func);
198
199/* Look for an executable in PATH, following execvp() rules */
200gchar*  g_find_program_in_path  (const gchar *program);
201
202/* Bit tests
203 */
204G_INLINE_FUNC gint      g_bit_nth_lsf (gulong  mask,
205                                       gint    nth_bit);
206G_INLINE_FUNC gint      g_bit_nth_msf (gulong  mask,
207                                       gint    nth_bit);
208G_INLINE_FUNC guint     g_bit_storage (gulong  number);
209
210/* Trash Stacks
211 * elements need to be >= sizeof (gpointer)
212 */
213typedef struct _GTrashStack     GTrashStack;
214struct _GTrashStack
215{
216  GTrashStack *next;
217};
218
219G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
220                                                 gpointer      data_p);
221G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
222G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
223G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
224
225/* inline function implementations
226 */
227#if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
228G_INLINE_FUNC gint
229g_bit_nth_lsf (gulong mask,
230               gint   nth_bit)
231{
232  do
233    {
234      nth_bit++;
235      if (mask & (1 << (gulong) nth_bit))
236        return nth_bit;
237    }
238  while (nth_bit < 31);
239  return -1;
240}
241G_INLINE_FUNC gint
242g_bit_nth_msf (gulong mask,
243               gint   nth_bit)
244{
245  if (nth_bit < 0)
246    nth_bit = GLIB_SIZEOF_LONG * 8;
247  do
248    {
249      nth_bit--;
250      if (mask & (1 << (gulong) nth_bit))
251        return nth_bit;
252    }
253  while (nth_bit > 0);
254  return -1;
255}
256G_INLINE_FUNC guint
257g_bit_storage (gulong number)
258{
259  register guint n_bits = 0;
260 
261  do
262    {
263      n_bits++;
264      number >>= 1;
265    }
266  while (number);
267  return n_bits;
268}
269G_INLINE_FUNC void
270g_trash_stack_push (GTrashStack **stack_p,
271                    gpointer      data_p)
272{
273  GTrashStack *data = (GTrashStack *) data_p;
274
275  data->next = *stack_p;
276  *stack_p = data;
277}
278G_INLINE_FUNC gpointer
279g_trash_stack_pop (GTrashStack **stack_p)
280{
281  GTrashStack *data;
282
283  data = *stack_p;
284  if (data)
285    {
286      *stack_p = data->next;
287      /* NULLify private pointer here, most platforms store NULL as
288       * subsequent 0 bytes
289       */
290      data->next = NULL;
291    }
292
293  return data;
294}
295G_INLINE_FUNC gpointer
296g_trash_stack_peek (GTrashStack **stack_p)
297{
298  GTrashStack *data;
299
300  data = *stack_p;
301
302  return data;
303}
304G_INLINE_FUNC guint
305g_trash_stack_height (GTrashStack **stack_p)
306{
307  GTrashStack *data;
308  guint i = 0;
309
310  for (data = *stack_p; data; data = data->next)
311    i++;
312
313  return i;
314}
315#endif  /* G_CAN_INLINE || __G_UTILS_C__ */
316
317/* Glib version.
318 * we prefix variable declarations so they can
319 * properly get exported in windows dlls.
320 */
321GLIB_VAR const guint glib_major_version;
322GLIB_VAR const guint glib_minor_version;
323GLIB_VAR const guint glib_micro_version;
324GLIB_VAR const guint glib_interface_age;
325GLIB_VAR const guint glib_binary_age;
326
327#define GLIB_CHECK_VERSION(major,minor,micro)    \
328    (GLIB_MAJOR_VERSION > (major) || \
329     (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
330     (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
331      GLIB_MICRO_VERSION >= (micro)))
332
333G_END_DECLS
334
335/*
336 * On Windows, this macro defines a DllMain function that stores the
337 * actual DLL name that the code being compiled will be included in.
338 * STATIC should be empty or 'static'. DLL_NAME is the name of the
339 * (pointer to the) char array where the DLL name will be stored. If
340 * this is used, you must also include <windows.h>. If you need a more complex
341 * DLL entry point function, you cannot use this.
342 *
343 * On non-Windows platforms, expands to nothing.
344 */
345
346#ifndef G_PLATFORM_WIN32
347# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
348#else
349# define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                    \
350static char *dll_name;                                                     \
351                                                                           \
352BOOL WINAPI                                                                \
353DllMain (HINSTANCE hinstDLL,                                               \
354         DWORD     fdwReason,                                              \
355         LPVOID    lpvReserved)                                            \
356{                                                                          \
357  char bfr[1000];                                                          \
358  switch (fdwReason)                                                       \
359    {                                                                      \
360    case DLL_PROCESS_ATTACH:                                               \
361      GetModuleFileName ((HMODULE) hinstDLL, bfr, sizeof (bfr));           \
362      dll_name = g_path_get_basename (bfr);                                \
363      break;                                                               \
364    }                                                                      \
365                                                                           \
366  return TRUE;                                                             \
367}
368#endif /* G_PLATFORM_WIN32 */
369
370#endif /* __G_UTILS_H__ */
Note: See TracBrowser for help on using the repository browser.