gutils.h

Go to the documentation of this file.
00001 /* GLIB - Library of useful routines for C programming
00002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 /*
00021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00022  * file for a list of people on the GLib Team.  See the ChangeLog
00023  * files for a list of changes.  These files are distributed with
00024  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
00025  */
00026 
00027 #ifndef __G_UTILS_H__
00028 #define __G_UTILS_H__
00029 
00030 #include <g_types.h>
00031 #include <stdarg.h>
00032 
00033 G_BEGIN_DECLS
00034 
00035 #ifdef G_OS_WIN32
00036 
00037 /* On native Win32, directory separator is the backslash, and search path
00038  * separator is the semicolon.
00039  */
00040 #define G_DIR_SEPARATOR '\\'
00041 #define G_DIR_SEPARATOR_S "\\"
00042 #define G_SEARCHPATH_SEPARATOR ';'
00043 #define G_SEARCHPATH_SEPARATOR_S ";"
00044 
00045 #else  /* !G_OS_WIN32 */
00046 
00047 /* Unix */
00048 
00049 #define G_DIR_SEPARATOR '/'
00050 #define G_DIR_SEPARATOR_S "/"
00051 #define G_SEARCHPATH_SEPARATOR ':'
00052 #define G_SEARCHPATH_SEPARATOR_S ":"
00053 
00054 #endif /* !G_OS_WIN32 */
00055 
00056 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
00057  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
00058  */
00059 #if !defined (G_VA_COPY)
00060 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00061 #    define G_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00062 #  elif defined (G_VA_COPY_AS_ARRAY)
00063 #    define G_VA_COPY(ap1, ap2)   g_memmove ((ap1), (ap2), sizeof (va_list))
00064 #  else /* va_list is a pointer */
00065 #    define G_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00066 #  endif /* va_list is a pointer */
00067 #endif /* !G_VA_COPY */
00068 
00069 /* inlining hassle. for compilers that don't allow the `inline' keyword,
00070  * mostly because of strict ANSI C compliance or dumbness, we try to fall
00071  * back to either `__inline__' or `__inline'.
00072  * we define G_CAN_INLINE, if the compiler seems to be actually
00073  * *capable* to do function inlining, in which case inline function bodys
00074  * do make sense. we also define G_INLINE_FUNC to properly export the
00075  * function prototypes if no inlining can be performed.
00076  * inline function bodies have to be special cased with G_CAN_INLINE and a
00077  * .c file specific macro to allow one compiled instance with extern linkage
00078  * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
00079  */
00080 #ifdef G_IMPLEMENT_INLINES
00081 #  define G_INLINE_FUNC extern
00082 #  undef  G_CAN_INLINE
00083 #endif
00084 #ifndef G_INLINE_FUNC
00085 #  define G_CAN_INLINE 1
00086 #endif
00087 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
00088 #  undef inline
00089 #  define inline __inline__
00090 #elif !defined (G_HAVE_INLINE)
00091 #  undef inline
00092 #  if defined (G_HAVE___INLINE__)
00093 #    define inline __inline__
00094 #  elif defined (G_HAVE___INLINE)
00095 #    define inline __inline
00096 #  else /* !inline && !__inline__ && !__inline */
00097 #    define inline  /* don't inline, then */
00098 #    ifndef G_INLINE_FUNC
00099 #      undef G_CAN_INLINE
00100 #    endif
00101 #  endif
00102 #endif
00103 #ifndef G_INLINE_FUNC
00104 #  if defined (__GNUC__) && (__OPTIMIZE__)
00105 #    define G_INLINE_FUNC extern inline
00106 #  elif defined (G_CAN_INLINE) && !defined (__GNUC__)
00107 #    define G_INLINE_FUNC static inline
00108 #  else /* can't inline */
00109 #    define G_INLINE_FUNC extern
00110 #    undef G_CAN_INLINE
00111 #  endif
00112 #endif /* !G_INLINE_FUNC */
00113 
00114 /* Retrive static string info
00115  */
00116 gchar*  g_get_user_name         (void);
00117 gchar*  g_get_real_name         (void);
00118 gchar*  g_get_home_dir          (void);
00119 gchar*  g_get_tmp_dir           (void);
00120 gchar*  g_get_prgname           (void);
00121 void    g_set_prgname           (const gchar *prgname);
00122 
00123 
00124 typedef struct _GDebugKey       GDebugKey;
00125 struct _GDebugKey
00126 {
00127   gchar *key;
00128   guint  value;
00129 };
00130 
00131 /* Miscellaneous utility functions
00132  */
00133 guint   g_parse_debug_string    (const gchar *string,
00134                                  GDebugKey   *keys,
00135                                  guint        nkeys);
00136 gint    g_snprintf              (gchar       *string,
00137                                  gulong       n,
00138                                  gchar const *format,
00139                                  ...) G_GNUC_PRINTF (3, 4);
00140 gint    g_vsnprintf             (gchar       *string,
00141                                  gulong       n,
00142                                  gchar const *format,
00143                                  va_list      args);
00144 /* Check if a file name is an absolute path */
00145 gboolean g_path_is_absolute     (const gchar *file_name);
00146 /* In case of absolute paths, skip the root part */
00147 gchar*  g_path_skip_root        (gchar       *file_name);
00148 
00149 /* These two functions are deprecated and will be removed in the next
00150  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
00151  * instead. Whatch out! The string returned by g_path_get_basename
00152  * must be g_freed, while the string returned by g_basename must not.*/
00153 gchar*  g_basename              (const gchar *file_name);
00154 gchar*  g_dirname               (const gchar *file_name);
00155 
00156 /* The returned strings are newly allocated with g_malloc() */
00157 gchar*  g_get_current_dir       (void);
00158 gchar*  g_path_get_basename     (const gchar *file_name);
00159 gchar*  g_path_get_dirname      (const gchar *file_name);
00160 
00161 /* Get the codeset for the current locale */
00162 /* gchar * g_get_codeset    (void); */
00163 
00164 /* return the environment string for the variable. The returned memory
00165  * must not be freed. */
00166 gchar*  g_getenv                (const gchar *variable);
00167 
00168 
00169 /* we try to provide a usefull equivalent for ATEXIT if it is
00170  * not defined, but use is actually abandoned. people should
00171  * use g_atexit() instead.
00172  */
00173 typedef void            (*GVoidFunc)            (void);
00174 #ifndef ATEXIT
00175 # define ATEXIT(proc)   g_ATEXIT(proc)
00176 #else
00177 # define G_NATIVE_ATEXIT
00178 #endif /* ATEXIT */
00179 /* we use a GLib function as a replacement for ATEXIT, so
00180  * the programmer is not required to check the return value
00181  * (if there is any in the implementation) and doesn't encounter
00182  * missing include files.
00183  */
00184 void    g_atexit                (GVoidFunc    func);
00185 
00186 /* Look for an executable in PATH, following execvp() rules */
00187 gchar*  g_find_program_in_path  (const gchar *program);
00188 
00189 /* Bit tests
00190  */
00191 G_INLINE_FUNC gint      g_bit_nth_lsf (guint32 mask,
00192                                        gint    nth_bit);
00193 G_INLINE_FUNC gint      g_bit_nth_msf (guint32 mask,
00194                                        gint    nth_bit);
00195 G_INLINE_FUNC guint     g_bit_storage (guint number);
00196 
00197 /* Trash Stacks
00198  * elements need to be >= sizeof (gpointer)
00199  */
00200 typedef struct _GTrashStack     GTrashStack;
00201 struct _GTrashStack
00202 {
00203   GTrashStack *next;
00204 };
00205 
00206 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
00207                                                  gpointer      data_p);
00208 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
00209 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
00210 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
00211 
00212 /* inline function implementations
00213  */
00214 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
00215 G_INLINE_FUNC gint
00216 g_bit_nth_lsf (guint32 mask,
00217                gint    nth_bit)
00218 {
00219   do
00220     {
00221       nth_bit++;
00222       if (mask & (1 << (guint) nth_bit))
00223         return nth_bit;
00224     }
00225   while (nth_bit < 32);
00226   return -1;
00227 }
00228 G_INLINE_FUNC gint
00229 g_bit_nth_msf (guint32 mask,
00230                gint    nth_bit)
00231 {
00232   if (nth_bit < 0)
00233     nth_bit = 32;
00234   do
00235     {
00236       nth_bit--;
00237       if (mask & (1 << (guint) nth_bit))
00238         return nth_bit;
00239     }
00240   while (nth_bit > 0);
00241   return -1;
00242 }
00243 G_INLINE_FUNC guint
00244 g_bit_storage (guint number)
00245 {
00246   register guint n_bits = 0;
00247   
00248   do
00249     {
00250       n_bits++;
00251       number >>= 1;
00252     }
00253   while (number);
00254   return n_bits;
00255 }
00256 G_INLINE_FUNC void
00257 g_trash_stack_push (GTrashStack **stack_p,
00258                     gpointer      data_p)
00259 {
00260   GTrashStack *data = (GTrashStack *) data_p;
00261 
00262   data->next = *stack_p;
00263   *stack_p = data;
00264 }
00265 G_INLINE_FUNC gpointer
00266 g_trash_stack_pop (GTrashStack **stack_p)
00267 {
00268   GTrashStack *data;
00269 
00270   data = *stack_p;
00271   if (data)
00272     {
00273       *stack_p = data->next;
00274       /* NULLify private pointer here, most platforms store NULL as
00275        * subsequent 0 bytes
00276        */
00277       data->next = NULL;
00278     }
00279 
00280   return data;
00281 }
00282 G_INLINE_FUNC gpointer
00283 g_trash_stack_peek (GTrashStack **stack_p)
00284 {
00285   GTrashStack *data;
00286 
00287   data = *stack_p;
00288 
00289   return data;
00290 }
00291 G_INLINE_FUNC guint
00292 g_trash_stack_height (GTrashStack **stack_p)
00293 {
00294   GTrashStack *data;
00295   guint i = 0;
00296 
00297   for (data = *stack_p; data; data = data->next)
00298     i++;
00299 
00300   return i;
00301 }
00302 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
00303 
00304 /* Glib version.
00305  * we prefix variable declarations so they can
00306  * properly get exported in windows dlls.
00307  */
00308 #ifdef G_OS_WIN32
00309 #  ifdef GLIB_COMPILATION
00310 #    define GLIB_VAR __declspec(dllexport)
00311 #  else /* !GLIB_COMPILATION */
00312 #    define GLIB_VAR extern __declspec(dllimport)
00313 #  endif /* !GLIB_COMPILATION */
00314 #else /* !G_OS_WIN32 */
00315 #  define GLIB_VAR extern
00316 #endif /* !G_OS_WIN32 */
00317 
00318 GLIB_VAR const guint glib_major_version;
00319 GLIB_VAR const guint glib_minor_version;
00320 GLIB_VAR const guint glib_micro_version;
00321 GLIB_VAR const guint glib_interface_age;
00322 GLIB_VAR const guint glib_binary_age;
00323 
00324 #define GLIB_CHECK_VERSION(major,minor,micro)    \
00325     (GLIB_MAJOR_VERSION > (major) || \
00326      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
00327      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
00328       GLIB_MICRO_VERSION >= (micro)))
00329 
00330 G_END_DECLS
00331 
00332 #endif /* __G_UTILS_H__ */
00333 
00334 
00335 

Generated on Tue Jul 5 14:14:43 2011 for ROOT_528-00b_version by  doxygen 1.5.1