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  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  * Boston, MA 02111-1307, USA.
00019  */
00020 
00021 /*
00022  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
00023  * file for a list of people on the GLib Team.  See the ChangeLog
00024  * files for a list of changes.  These files are distributed with
00025  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
00026  */
00027 
00028 #ifndef __G_UTILS_H__
00029 #define __G_UTILS_H__
00030 
00031 #include <_ansi.h>
00032 #include <glib/gtypes.h>
00033 #include <stdarg.h>
00034 
00035 G_BEGIN_DECLS
00036 
00037 #if defined(G_OS_WIN32) || defined(SYMBIAN)
00038 
00039 /* On Win32, the canonical directory separator is the backslash, and
00040  * the search path separator is the semicolon. Note that also the
00041  * (forward) slash works as directory separator.
00042  */
00043 #define G_DIR_SEPARATOR '\\'
00044 #define G_DIR_SEPARATOR_S "\\"
00045 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
00046 #define G_SEARCHPATH_SEPARATOR ';'
00047 #define G_SEARCHPATH_SEPARATOR_S ";"
00048 
00049 #else  /* !G_OS_WIN32 */
00050 
00051 /* Unix */
00052 
00053 #define G_DIR_SEPARATOR '/'
00054 #define G_DIR_SEPARATOR_S "/"
00055 #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
00056 #define G_SEARCHPATH_SEPARATOR ':'
00057 #define G_SEARCHPATH_SEPARATOR_S ":"
00058 
00059 #endif /* !G_OS_WIN32 */
00060 
00061 /* Define G_VA_COPY() to do the right thing for copying va_list variables.
00062  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
00063  */
00064 #if !defined (G_VA_COPY)
00065 #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
00066 #    define G_VA_COPY(ap1, ap2)   (*(ap1) = *(ap2))
00067 #  elif defined (G_VA_COPY_AS_ARRAY)
00068 #    define G_VA_COPY(ap1, ap2)   g_memmove ((ap1), (ap2), sizeof (va_list))
00069 #  else /* va_list is a pointer */
00070 #    define G_VA_COPY(ap1, ap2)   ((ap1) = (ap2))
00071 #  endif /* va_list is a pointer */
00072 #endif /* !G_VA_COPY */
00073 
00074 /* inlining hassle. for compilers that don't allow the `inline' keyword,
00075  * mostly because of strict ANSI C compliance or dumbness, we try to fall
00076  * back to either `__inline__' or `__inline'.
00077  * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be 
00078  * actually *capable* to do function inlining, in which case inline 
00079  * function bodies do make sense. we also define G_INLINE_FUNC to properly 
00080  * export the function prototypes if no inlining can be performed.
00081  * inline function bodies have to be special cased with G_CAN_INLINE and a
00082  * .c file specific macro to allow one compiled instance with extern linkage
00083  * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
00084  */
00085 #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
00086 #  undef inline
00087 #  define inline __inline__
00088 #elif !defined (G_HAVE_INLINE)
00089 #  undef inline
00090 #  if defined (G_HAVE___INLINE__)
00091 #    define inline __inline__
00092 #  elif defined (G_HAVE___INLINE)
00093 #    define inline __inline
00094 #  else /* !inline && !__inline__ && !__inline */
00095 #    define inline  /* don't inline, then */
00096 #  endif
00097 #endif
00098 #ifdef G_IMPLEMENT_INLINES
00099 #  define G_INLINE_FUNC
00100 #  undef  G_CAN_INLINE
00101 #elif defined (__GNUC__) 
00102 #       ifdef SYMBIAN
00103 #               define G_INLINE_FUNC extern __inline
00104 #       else
00105 #               define G_INLINE_FUNC extern inline
00106 #       endif
00107 #elif defined (G_CAN_INLINE) 
00108 #       ifdef SYMBIAN
00109 #               define G_INLINE_FUNC static __inline
00110 #       else
00111 #               define G_INLINE_FUNC static inline
00112 #       endif
00113 #else /* can't inline */
00114 #  define G_INLINE_FUNC
00115 #endif /* !G_INLINE_FUNC */
00116 
00117 /* Retrive static string info
00118  */
00119 #ifdef G_OS_WIN32
00120 #define g_get_user_name g_get_user_name_utf8
00121 #define g_get_real_name g_get_real_name_utf8
00122 #define g_get_home_dir g_get_home_dir_utf8
00123 #define g_get_tmp_dir g_get_tmp_dir_utf8
00124 #endif
00125 
00126 IMPORT_C G_CONST_RETURN gchar* g_get_user_name        (void);
00127 IMPORT_C G_CONST_RETURN gchar* g_get_real_name        (void);
00128 IMPORT_C G_CONST_RETURN gchar* g_get_home_dir         (void);
00129 IMPORT_C G_CONST_RETURN gchar* g_get_tmp_dir          (void);
00130 IMPORT_C G_CONST_RETURN gchar* g_get_host_name       (void);
00131 IMPORT_C gchar*                g_get_prgname          (void);
00132 IMPORT_C void                  g_set_prgname          (const gchar *prgname);
00133 IMPORT_C G_CONST_RETURN gchar* g_get_application_name (void);
00134 IMPORT_C void                  g_set_application_name (const gchar *application_name);
00135 
00136 IMPORT_C G_CONST_RETURN gchar*    g_get_user_data_dir      (void);
00137 IMPORT_C G_CONST_RETURN gchar*    g_get_user_config_dir    (void);
00138 IMPORT_C G_CONST_RETURN gchar*    g_get_user_cache_dir     (void);
00139 IMPORT_C G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs   (void);
00140 
00141 #ifdef G_OS_WIN32
00142 G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address);
00143 #endif
00144 
00145 #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
00146 static inline G_CONST_RETURN gchar * G_CONST_RETURN *
00147 g_win32_get_system_data_dirs (void)
00148 {
00149   return g_win32_get_system_data_dirs_for_module ((gconstpointer) &g_win32_get_system_data_dirs);
00150 }
00151 #define g_get_system_data_dirs g_win32_get_system_data_dirs
00152 #endif
00153 
00154 IMPORT_C G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
00155 
00156 IMPORT_C G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
00157 
00158 typedef struct _GDebugKey       GDebugKey;
00159 struct _GDebugKey
00160 {
00161   gchar *key;
00162   guint  value;
00163 };
00164 
00165 /* Miscellaneous utility functions
00166  */
00167 IMPORT_C guint                 g_parse_debug_string (const gchar     *string,
00168                                             const GDebugKey *keys,
00169                                             guint            nkeys);
00170 
00171 IMPORT_C gint                  g_snprintf           (gchar       *string,
00172                                             gulong       n,
00173                                             gchar const *format,
00174                                             ...) G_GNUC_PRINTF (3, 4);
00175 IMPORT_C gint                  g_vsnprintf          (gchar       *string,
00176                                             gulong       n,
00177                                             gchar const *format,
00178                                             va_list      args);
00179 
00180 /* Check if a file name is an absolute path */
00181 IMPORT_C gboolean              g_path_is_absolute   (const gchar *file_name);
00182 
00183 /* In case of absolute paths, skip the root part */
00184 IMPORT_C G_CONST_RETURN gchar* g_path_skip_root     (const gchar *file_name);
00185 
00186 #ifndef G_DISABLE_DEPRECATED
00187 
00188 /* These two functions are deprecated and will be removed in the next
00189  * major release of GLib. Use g_path_get_dirname/g_path_get_basename
00190  * instead. Whatch out! The string returned by g_path_get_basename
00191  * must be g_freed, while the string returned by g_basename must not.*/
00192 IMPORT_C G_CONST_RETURN gchar* g_basename           (const gchar *file_name);
00193 #define g_dirname g_path_get_dirname
00194 
00195 #endif /* G_DISABLE_DEPRECATED */
00196 
00197 #ifdef G_OS_WIN32
00198 #define g_get_current_dir g_get_current_dir_utf8
00199 #endif
00200 
00201 /* The returned strings are newly allocated with g_malloc() */
00202 IMPORT_C gchar*                g_get_current_dir    (void);
00203 IMPORT_C gchar*                g_path_get_basename  (const gchar *file_name) G_GNUC_MALLOC;
00204 IMPORT_C gchar*                g_path_get_dirname   (const gchar *file_name) G_GNUC_MALLOC;
00205 
00206 /* Set the pointer at the specified location to NULL */
00207 IMPORT_C void                  g_nullify_pointer    (gpointer    *nullify_location);
00208 
00209 /* return the environment string for the variable. The returned memory
00210  * must not be freed. */
00211 #ifdef G_OS_WIN32
00212 #define g_getenv g_getenv_utf8
00213 #define g_setenv g_setenv_utf8
00214 #define g_unsetenv g_unsetenv_utf8
00215 #define g_find_program_in_path g_find_program_in_path_utf8
00216 #endif
00217 
00218 IMPORT_C G_CONST_RETURN gchar* g_getenv             (const gchar *variable);
00219 IMPORT_C gboolean              g_setenv             (const gchar *variable,
00220                                             const gchar *value,
00221                                             gboolean     overwrite);
00222 IMPORT_C void                  g_unsetenv           (const gchar *variable);
00223 IMPORT_C gchar**               g_listenv            (void);
00224 const gchar*         _g_getenv_nomalloc    (const gchar *variable,
00225                                             gchar        buffer[1024]);
00226 
00227 /* we try to provide a usefull equivalent for ATEXIT if it is
00228  * not defined, but use is actually abandoned. people should
00229  * use g_atexit() instead.
00230  */
00231 typedef void            (*GVoidFunc)            (void);
00232 #ifndef ATEXIT
00233 # define ATEXIT(proc)   g_ATEXIT(proc)
00234 #else
00235 # define G_NATIVE_ATEXIT
00236 #endif /* ATEXIT */
00237 /* we use a GLib function as a replacement for ATEXIT, so
00238  * the programmer is not required to check the return value
00239  * (if there is any in the implementation) and doesn't encounter
00240  * missing include files.
00241  */
00242 IMPORT_C void   g_atexit                (GVoidFunc    func);
00243 
00244 #ifdef G_OS_WIN32
00245 /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
00246  * atexit(), the function will be called when the GLib DLL is detached
00247  * from the program, which is not what the caller wants. The caller
00248  * wants the function to be called when it *itself* exits (or is
00249  * detached, in case the caller, too, is a DLL).
00250  */
00251 int atexit (void (*)(void));
00252 #define g_atexit(func) atexit(func)
00253 #endif
00254 
00255 /* Look for an executable in PATH, following execvp() rules */
00256 IMPORT_C gchar*  g_find_program_in_path  (const gchar *program);
00257 
00258 /* Bit tests
00259  */
00260 G_INLINE_FUNC gint      g_bit_nth_lsf (gulong  mask,
00261                                        gint    nth_bit);
00262 G_INLINE_FUNC gint      g_bit_nth_msf (gulong  mask,
00263                                        gint    nth_bit);
00264 G_INLINE_FUNC guint     g_bit_storage (gulong  number);
00265 
00266 /* Trash Stacks
00267  * elements need to be >= sizeof (gpointer)
00268  */
00269 typedef struct _GTrashStack     GTrashStack;
00270 struct _GTrashStack
00271 {
00272   GTrashStack *next;
00273 };
00274 
00275 G_INLINE_FUNC void      g_trash_stack_push      (GTrashStack **stack_p,
00276                                                  gpointer      data_p);
00277 G_INLINE_FUNC gpointer  g_trash_stack_pop       (GTrashStack **stack_p);
00278 G_INLINE_FUNC gpointer  g_trash_stack_peek      (GTrashStack **stack_p);
00279 G_INLINE_FUNC guint     g_trash_stack_height    (GTrashStack **stack_p);
00280 
00281 /* inline function implementations
00282  */
00283 #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
00284 G_INLINE_FUNC gint
00285 g_bit_nth_lsf (gulong mask,
00286                gint   nth_bit)
00287 {
00288   do
00289     {
00290       nth_bit++;
00291       if (mask & (1UL << nth_bit))
00292         return nth_bit;
00293     }
00294   while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1));
00295   return -1;
00296 }
00297 G_INLINE_FUNC gint
00298 g_bit_nth_msf (gulong mask,
00299                gint   nth_bit)
00300 {
00301   if (nth_bit < 0)
00302     nth_bit = GLIB_SIZEOF_LONG * 8;
00303   do
00304     {
00305       nth_bit--;
00306       if (mask & (1UL << nth_bit))
00307         return nth_bit;
00308     }
00309   while (nth_bit > 0);
00310   return -1;
00311 }
00312 G_INLINE_FUNC guint
00313 g_bit_storage (gulong number)
00314 {
00315   register guint n_bits = 0;
00316   
00317   do
00318     {
00319       n_bits++;
00320       number >>= 1;
00321     }
00322   while (number);
00323   return n_bits;
00324 }
00325 G_INLINE_FUNC void
00326 g_trash_stack_push (GTrashStack **stack_p,
00327                     gpointer      data_p)
00328 {
00329   GTrashStack *data = (GTrashStack *) data_p;
00330 
00331   data->next = *stack_p;
00332   *stack_p = data;
00333 }
00334 G_INLINE_FUNC gpointer
00335 g_trash_stack_pop (GTrashStack **stack_p)
00336 {
00337   GTrashStack *data;
00338 
00339   data = *stack_p;
00340   if (data)
00341     {
00342       *stack_p = data->next;
00343       /* NULLify private pointer here, most platforms store NULL as
00344        * subsequent 0 bytes
00345        */
00346       data->next = NULL;
00347     }
00348 
00349   return data;
00350 }
00351 G_INLINE_FUNC gpointer
00352 g_trash_stack_peek (GTrashStack **stack_p)
00353 {
00354   GTrashStack *data;
00355 
00356   data = *stack_p;
00357 
00358   return data;
00359 }
00360 G_INLINE_FUNC guint
00361 g_trash_stack_height (GTrashStack **stack_p)
00362 {
00363   GTrashStack *data;
00364   guint i = 0;
00365 
00366   for (data = *stack_p; data; data = data->next)
00367     i++;
00368 
00369   return i;
00370 }
00371 #endif  /* G_CAN_INLINE || __G_UTILS_C__ */
00372 
00373 /* Glib version.
00374  * we prefix variable declarations so they can
00375  * properly get exported in windows dlls.
00376  */
00377 GLIB_VAR const guint glib_major_version;
00378 GLIB_VAR const guint glib_minor_version;
00379 GLIB_VAR const guint glib_micro_version;
00380 GLIB_VAR const guint glib_interface_age;
00381 GLIB_VAR const guint glib_binary_age;
00382 
00383 #ifdef SYMBIAN
00384 IMPORT_C const  guint *_glib_major_version();
00385 IMPORT_C const  guint *_glib_minor_version();
00386 IMPORT_C const  guint *_glib_micro_version();
00387 IMPORT_C const  guint *_glib_interface_age();
00388 IMPORT_C const  guint *_glib_binary_age();
00389 #endif /* SYMBIAN */
00390 
00391 IMPORT_C const gchar * glib_check_version (guint required_major,
00392                                   guint required_minor,
00393                                   guint required_micro);
00394 
00395 #define GLIB_CHECK_VERSION(major,minor,micro)    \
00396     (GLIB_MAJOR_VERSION > (major) || \
00397      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
00398      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
00399       GLIB_MICRO_VERSION >= (micro)))
00400 
00401 G_END_DECLS
00402 
00403 /*
00404  * On Windows, this macro defines a DllMain function that stores the
00405  * actual DLL name that the code being compiled will be included in.
00406  * STATIC should be empty or 'static'. DLL_NAME is the name of the
00407  * (pointer to the) char array where the DLL name will be stored. If
00408  * this is used, you must also include <windows.h>. If you need a more complex
00409  * DLL entry point function, you cannot use this.
00410  *
00411  * On non-Windows platforms, expands to nothing.
00412  */
00413 
00414 #ifndef G_PLATFORM_WIN32
00415 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
00416 #else
00417 # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)                 \
00418 static char *dll_name;                                                  \
00419                                                                         \
00420 BOOL WINAPI                                                             \
00421 DllMain (HINSTANCE hinstDLL,                                            \
00422          DWORD     fdwReason,                                           \
00423          LPVOID    lpvReserved)                                         \
00424 {                                                                       \
00425   wchar_t wcbfr[1000];                                                  \
00426   char cpbfr[1000];                                                     \
00427   char *tem;                                                            \
00428   switch (fdwReason)                                                    \
00429     {                                                                   \
00430     case DLL_PROCESS_ATTACH:                                            \
00431       if (GetVersion () < 0x80000000)                                   \
00432         {                                                               \
00433           GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
00434           tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL);          \
00435           dll_name = g_path_get_basename (tem);                         \
00436           g_free (tem);                                                 \
00437         }                                                               \
00438       else                                                              \
00439         {                                                               \
00440           GetModuleFileNameA ((HMODULE) hinstDLL, cpbfr, G_N_ELEMENTS (cpbfr)); \
00441           tem = g_locale_to_utf8 (cpbfr, -1, NULL, NULL, NULL);         \
00442           dll_name = g_path_get_basename (tem);                         \
00443           g_free (tem);                                                 \
00444         }                                                               \
00445       break;                                                            \
00446     }                                                                   \
00447                                                                         \
00448   return TRUE;                                                          \
00449 }
00450 #endif /* G_PLATFORM_WIN32 */
00451 
00452 #endif /* __G_UTILS_H__ */

Copyright © Nokia Corporation 2001-2008
Back to top