PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/glib/gutils.h

https://github.com/prajnashi/glib
C Header | 430 lines | 396 code | 6 blank | 28 comment | 0 complexity | 856c07c9b26bf4147a23a83191a97857 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-3.0, BSD-3-Clause
  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. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #ifndef __G_UTILS_H__
  26. #define __G_UTILS_H__
  27. #include <glib/gtypes.h>
  28. #include <stdarg.h>
  29. G_BEGIN_DECLS
  30. #ifdef G_OS_WIN32
  31. /* On Win32, the canonical directory separator is the backslash, and
  32. * the search path separator is the semicolon. Note that also the
  33. * (forward) slash works as directory separator.
  34. */
  35. #define G_DIR_SEPARATOR '\\'
  36. #define G_DIR_SEPARATOR_S "\\"
  37. #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR || (c) == '/')
  38. #define G_SEARCHPATH_SEPARATOR ';'
  39. #define G_SEARCHPATH_SEPARATOR_S ";"
  40. #else /* !G_OS_WIN32 */
  41. /* Unix */
  42. #define G_DIR_SEPARATOR '/'
  43. #define G_DIR_SEPARATOR_S "/"
  44. #define G_IS_DIR_SEPARATOR(c) ((c) == G_DIR_SEPARATOR)
  45. #define G_SEARCHPATH_SEPARATOR ':'
  46. #define G_SEARCHPATH_SEPARATOR_S ":"
  47. #endif /* !G_OS_WIN32 */
  48. /* Define G_VA_COPY() to do the right thing for copying va_list variables.
  49. * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
  50. */
  51. #if !defined (G_VA_COPY)
  52. # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
  53. # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
  54. # elif defined (G_VA_COPY_AS_ARRAY)
  55. # define G_VA_COPY(ap1, ap2) g_memmove ((ap1), (ap2), sizeof (va_list))
  56. # else /* va_list is a pointer */
  57. # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
  58. # endif /* va_list is a pointer */
  59. #endif /* !G_VA_COPY */
  60. /* inlining hassle. for compilers that don't allow the `inline' keyword,
  61. * mostly because of strict ANSI C compliance or dumbness, we try to fall
  62. * back to either `__inline__' or `__inline'.
  63. * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be
  64. * actually *capable* to do function inlining, in which case inline
  65. * function bodies do make sense. we also define G_INLINE_FUNC to properly
  66. * export the function prototypes if no inlining can be performed.
  67. * inline function bodies have to be special cased with G_CAN_INLINE and a
  68. * .c file specific macro to allow one compiled instance with extern linkage
  69. * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
  70. */
  71. #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
  72. # undef inline
  73. # define inline __inline__
  74. #elif !defined (G_HAVE_INLINE)
  75. # undef inline
  76. # if defined (G_HAVE___INLINE__)
  77. # define inline __inline__
  78. # elif defined (G_HAVE___INLINE)
  79. # define inline __inline
  80. # else /* !inline && !__inline__ && !__inline */
  81. # define inline /* don't inline, then */
  82. # endif
  83. #endif
  84. #ifdef G_IMPLEMENT_INLINES
  85. # define G_INLINE_FUNC
  86. # undef G_CAN_INLINE
  87. #elif defined (__GNUC__)
  88. # define G_INLINE_FUNC extern inline
  89. #elif defined (G_CAN_INLINE)
  90. # define G_INLINE_FUNC static inline
  91. #else /* can't inline */
  92. # define G_INLINE_FUNC
  93. #endif /* !G_INLINE_FUNC */
  94. /* Retrive static string info
  95. */
  96. #ifdef G_OS_WIN32
  97. #define g_get_user_name g_get_user_name_utf8
  98. #define g_get_real_name g_get_real_name_utf8
  99. #define g_get_home_dir g_get_home_dir_utf8
  100. #define g_get_tmp_dir g_get_tmp_dir_utf8
  101. #endif
  102. G_CONST_RETURN gchar* g_get_user_name (void);
  103. G_CONST_RETURN gchar* g_get_real_name (void);
  104. G_CONST_RETURN gchar* g_get_home_dir (void);
  105. G_CONST_RETURN gchar* g_get_tmp_dir (void);
  106. G_CONST_RETURN gchar* g_get_host_name (void);
  107. gchar* g_get_prgname (void);
  108. void g_set_prgname (const gchar *prgname);
  109. G_CONST_RETURN gchar* g_get_application_name (void);
  110. void g_set_application_name (const gchar *application_name);
  111. G_CONST_RETURN gchar* g_get_user_data_dir (void);
  112. G_CONST_RETURN gchar* g_get_user_config_dir (void);
  113. G_CONST_RETURN gchar* g_get_user_cache_dir (void);
  114. G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_data_dirs (void);
  115. #ifdef G_OS_WIN32
  116. G_CONST_RETURN gchar* G_CONST_RETURN * g_win32_get_system_data_dirs_for_module (gconstpointer address);
  117. #endif
  118. #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
  119. static inline G_CONST_RETURN gchar * G_CONST_RETURN *
  120. g_win32_get_system_data_dirs (void)
  121. {
  122. return g_win32_get_system_data_dirs_for_module ((gconstpointer) &g_win32_get_system_data_dirs);
  123. }
  124. #define g_get_system_data_dirs g_win32_get_system_data_dirs
  125. #endif
  126. G_CONST_RETURN gchar* G_CONST_RETURN * g_get_system_config_dirs (void);
  127. G_CONST_RETURN gchar* G_CONST_RETURN * g_get_language_names (void);
  128. typedef struct _GDebugKey GDebugKey;
  129. struct _GDebugKey
  130. {
  131. gchar *key;
  132. guint value;
  133. };
  134. /* Miscellaneous utility functions
  135. */
  136. guint g_parse_debug_string (const gchar *string,
  137. const GDebugKey *keys,
  138. guint nkeys);
  139. gint g_snprintf (gchar *string,
  140. gulong n,
  141. gchar const *format,
  142. ...) G_GNUC_PRINTF (3, 4);
  143. gint g_vsnprintf (gchar *string,
  144. gulong n,
  145. gchar const *format,
  146. va_list args);
  147. /* Check if a file name is an absolute path */
  148. gboolean g_path_is_absolute (const gchar *file_name);
  149. /* In case of absolute paths, skip the root part */
  150. G_CONST_RETURN gchar* g_path_skip_root (const gchar *file_name);
  151. #ifndef G_DISABLE_DEPRECATED
  152. /* These two functions are deprecated and will be removed in the next
  153. * major release of GLib. Use g_path_get_dirname/g_path_get_basename
  154. * instead. Whatch out! The string returned by g_path_get_basename
  155. * must be g_freed, while the string returned by g_basename must not.*/
  156. G_CONST_RETURN gchar* g_basename (const gchar *file_name);
  157. #define g_dirname g_path_get_dirname
  158. #endif /* G_DISABLE_DEPRECATED */
  159. #ifdef G_OS_WIN32
  160. #define g_get_current_dir g_get_current_dir_utf8
  161. #endif
  162. /* The returned strings are newly allocated with g_malloc() */
  163. gchar* g_get_current_dir (void);
  164. gchar* g_path_get_basename (const gchar *file_name) G_GNUC_MALLOC;
  165. gchar* g_path_get_dirname (const gchar *file_name) G_GNUC_MALLOC;
  166. /* Set the pointer at the specified location to NULL */
  167. void g_nullify_pointer (gpointer *nullify_location);
  168. /* return the environment string for the variable. The returned memory
  169. * must not be freed. */
  170. #ifdef G_OS_WIN32
  171. #define g_getenv g_getenv_utf8
  172. #define g_setenv g_setenv_utf8
  173. #define g_unsetenv g_unsetenv_utf8
  174. #define g_find_program_in_path g_find_program_in_path_utf8
  175. #endif
  176. G_CONST_RETURN gchar* g_getenv (const gchar *variable);
  177. gboolean g_setenv (const gchar *variable,
  178. const gchar *value,
  179. gboolean overwrite);
  180. void g_unsetenv (const gchar *variable);
  181. gchar** g_listenv (void);
  182. /* private */
  183. const gchar* _g_getenv_nomalloc (const gchar *variable,
  184. gchar buffer[1024]);
  185. /* we try to provide a useful equivalent for ATEXIT if it is
  186. * not defined, but use is actually abandoned. people should
  187. * use g_atexit() instead.
  188. */
  189. typedef void (*GVoidFunc) (void);
  190. #ifndef ATEXIT
  191. # define ATEXIT(proc) g_ATEXIT(proc)
  192. #else
  193. # define G_NATIVE_ATEXIT
  194. #endif /* ATEXIT */
  195. /* we use a GLib function as a replacement for ATEXIT, so
  196. * the programmer is not required to check the return value
  197. * (if there is any in the implementation) and doesn't encounter
  198. * missing include files.
  199. */
  200. void g_atexit (GVoidFunc func);
  201. #ifdef G_OS_WIN32
  202. /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
  203. * atexit(), the function will be called when the GLib DLL is detached
  204. * from the program, which is not what the caller wants. The caller
  205. * wants the function to be called when it *itself* exits (or is
  206. * detached, in case the caller, too, is a DLL).
  207. */
  208. int atexit (void (*)(void));
  209. #define g_atexit(func) atexit(func)
  210. #endif
  211. /* Look for an executable in PATH, following execvp() rules */
  212. gchar* g_find_program_in_path (const gchar *program);
  213. /* Bit tests
  214. */
  215. G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask,
  216. gint nth_bit) G_GNUC_CONST;
  217. G_INLINE_FUNC gint g_bit_nth_msf (gulong mask,
  218. gint nth_bit) G_GNUC_CONST;
  219. G_INLINE_FUNC guint g_bit_storage (gulong number) G_GNUC_CONST;
  220. /* Trash Stacks
  221. * elements need to be >= sizeof (gpointer)
  222. */
  223. typedef struct _GTrashStack GTrashStack;
  224. struct _GTrashStack
  225. {
  226. GTrashStack *next;
  227. };
  228. G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p,
  229. gpointer data_p);
  230. G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p);
  231. G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p);
  232. G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p);
  233. /* inline function implementations
  234. */
  235. #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
  236. G_INLINE_FUNC gint
  237. g_bit_nth_lsf (gulong mask,
  238. gint nth_bit)
  239. {
  240. if (G_UNLIKELY (nth_bit < -1))
  241. nth_bit = -1;
  242. while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
  243. {
  244. nth_bit++;
  245. if (mask & (1UL << nth_bit))
  246. return nth_bit;
  247. }
  248. return -1;
  249. }
  250. G_INLINE_FUNC gint
  251. g_bit_nth_msf (gulong mask,
  252. gint nth_bit)
  253. {
  254. if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
  255. nth_bit = GLIB_SIZEOF_LONG * 8;
  256. while (nth_bit > 0)
  257. {
  258. nth_bit--;
  259. if (mask & (1UL << nth_bit))
  260. return nth_bit;
  261. }
  262. return -1;
  263. }
  264. G_INLINE_FUNC guint
  265. g_bit_storage (gulong number)
  266. {
  267. #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
  268. return G_LIKELY (number) ?
  269. ((GLIB_SIZEOF_LONG * 8 - 1) ^ __builtin_clzl(number)) + 1 : 1;
  270. #else
  271. register guint n_bits = 0;
  272. do
  273. {
  274. n_bits++;
  275. number >>= 1;
  276. }
  277. while (number);
  278. return n_bits;
  279. #endif
  280. }
  281. G_INLINE_FUNC void
  282. g_trash_stack_push (GTrashStack **stack_p,
  283. gpointer data_p)
  284. {
  285. GTrashStack *data = (GTrashStack *) data_p;
  286. data->next = *stack_p;
  287. *stack_p = data;
  288. }
  289. G_INLINE_FUNC gpointer
  290. g_trash_stack_pop (GTrashStack **stack_p)
  291. {
  292. GTrashStack *data;
  293. data = *stack_p;
  294. if (data)
  295. {
  296. *stack_p = data->next;
  297. /* NULLify private pointer here, most platforms store NULL as
  298. * subsequent 0 bytes
  299. */
  300. data->next = NULL;
  301. }
  302. return data;
  303. }
  304. G_INLINE_FUNC gpointer
  305. g_trash_stack_peek (GTrashStack **stack_p)
  306. {
  307. GTrashStack *data;
  308. data = *stack_p;
  309. return data;
  310. }
  311. G_INLINE_FUNC guint
  312. g_trash_stack_height (GTrashStack **stack_p)
  313. {
  314. GTrashStack *data;
  315. guint i = 0;
  316. for (data = *stack_p; data; data = data->next)
  317. i++;
  318. return i;
  319. }
  320. #endif /* G_CAN_INLINE || __G_UTILS_C__ */
  321. /* Glib version.
  322. * we prefix variable declarations so they can
  323. * properly get exported in windows dlls.
  324. */
  325. GLIB_VAR const guint glib_major_version;
  326. GLIB_VAR const guint glib_minor_version;
  327. GLIB_VAR const guint glib_micro_version;
  328. GLIB_VAR const guint glib_interface_age;
  329. GLIB_VAR const guint glib_binary_age;
  330. const gchar * glib_check_version (guint required_major,
  331. guint required_minor,
  332. guint required_micro);
  333. #define GLIB_CHECK_VERSION(major,minor,micro) \
  334. (GLIB_MAJOR_VERSION > (major) || \
  335. (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
  336. (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
  337. GLIB_MICRO_VERSION >= (micro)))
  338. G_END_DECLS
  339. /*
  340. * On Windows, this macro defines a DllMain function that stores the
  341. * actual DLL name that the code being compiled will be included in.
  342. * STATIC should be empty or 'static'. DLL_NAME is the name of the
  343. * (pointer to the) char array where the DLL name will be stored. If
  344. * this is used, you must also include <windows.h>. If you need a more complex
  345. * DLL entry point function, you cannot use this.
  346. *
  347. * On non-Windows platforms, expands to nothing.
  348. */
  349. #ifndef G_PLATFORM_WIN32
  350. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
  351. #else
  352. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
  353. static char *dll_name; \
  354. \
  355. BOOL WINAPI \
  356. DllMain (HINSTANCE hinstDLL, \
  357. DWORD fdwReason, \
  358. LPVOID lpvReserved) \
  359. { \
  360. wchar_t wcbfr[1000]; \
  361. char *tem; \
  362. switch (fdwReason) \
  363. { \
  364. case DLL_PROCESS_ATTACH: \
  365. GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
  366. tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
  367. dll_name = g_path_get_basename (tem); \
  368. g_free (tem); \
  369. break; \
  370. } \
  371. \
  372. return TRUE; \
  373. }
  374. #endif /* G_PLATFORM_WIN32 */
  375. #endif /* __G_UTILS_H__ */