gutils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.1 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_UTILS_H__
  24. #define __G_UTILS_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <glib/gtypes.h>
  29. #include <stdarg.h>
  30. G_BEGIN_DECLS
  31. /* Define G_VA_COPY() to do the right thing for copying va_list variables.
  32. * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
  33. */
  34. #if !defined (G_VA_COPY)
  35. # if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
  36. # define G_VA_COPY(ap1, ap2) (*(ap1) = *(ap2))
  37. # elif defined (G_VA_COPY_AS_ARRAY)
  38. # define G_VA_COPY(ap1, ap2) memmove ((ap1), (ap2), sizeof (va_list))
  39. # else /* va_list is a pointer */
  40. # define G_VA_COPY(ap1, ap2) ((ap1) = (ap2))
  41. # endif /* va_list is a pointer */
  42. #endif /* !G_VA_COPY */
  43. GLIB_AVAILABLE_IN_ALL
  44. const gchar * g_get_user_name (void);
  45. GLIB_AVAILABLE_IN_ALL
  46. const gchar * g_get_real_name (void);
  47. GLIB_AVAILABLE_IN_ALL
  48. const gchar * g_get_home_dir (void);
  49. GLIB_AVAILABLE_IN_ALL
  50. const gchar * g_get_tmp_dir (void);
  51. GLIB_AVAILABLE_IN_ALL
  52. const gchar * g_get_host_name (void);
  53. GLIB_AVAILABLE_IN_ALL
  54. const gchar * g_get_prgname (void);
  55. GLIB_AVAILABLE_IN_ALL
  56. void g_set_prgname (const gchar *prgname);
  57. GLIB_AVAILABLE_IN_ALL
  58. const gchar * g_get_application_name (void);
  59. GLIB_AVAILABLE_IN_ALL
  60. void g_set_application_name (const gchar *application_name);
  61. GLIB_AVAILABLE_IN_ALL
  62. void g_reload_user_special_dirs_cache (void);
  63. GLIB_AVAILABLE_IN_ALL
  64. const gchar * g_get_user_data_dir (void);
  65. GLIB_AVAILABLE_IN_ALL
  66. const gchar * g_get_user_config_dir (void);
  67. GLIB_AVAILABLE_IN_ALL
  68. const gchar * g_get_user_cache_dir (void);
  69. GLIB_AVAILABLE_IN_ALL
  70. const gchar * const * g_get_system_data_dirs (void);
  71. #ifdef G_OS_WIN32
  72. /* This function is not part of the public GLib API */
  73. GLIB_AVAILABLE_IN_ALL
  74. const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
  75. #endif
  76. #if defined (G_OS_WIN32) && defined (G_CAN_INLINE)
  77. /* This function is not part of the public GLib API either. Just call
  78. * g_get_system_data_dirs() in your code, never mind that that is
  79. * actually a macro and you will in fact call this inline function.
  80. */
  81. static inline const gchar * const *
  82. _g_win32_get_system_data_dirs (void)
  83. {
  84. return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
  85. }
  86. #define g_get_system_data_dirs _g_win32_get_system_data_dirs
  87. #endif
  88. GLIB_AVAILABLE_IN_ALL
  89. const gchar * const * g_get_system_config_dirs (void);
  90. GLIB_AVAILABLE_IN_ALL
  91. const gchar * g_get_user_runtime_dir (void);
  92. /**
  93. * GUserDirectory:
  94. * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
  95. * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
  96. * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
  97. * @G_USER_DIRECTORY_MUSIC: the user's Music directory
  98. * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
  99. * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
  100. * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
  101. * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
  102. * @G_USER_N_DIRECTORIES: the number of enum values
  103. *
  104. * These are logical ids for special directories which are defined
  105. * depending on the platform used. You should use g_get_user_special_dir()
  106. * to retrieve the full path associated to the logical id.
  107. *
  108. * The #GUserDirectory enumeration can be extended at later date. Not
  109. * every platform has a directory for every logical id in this
  110. * enumeration.
  111. *
  112. * Since: 2.14
  113. */
  114. typedef enum {
  115. G_USER_DIRECTORY_DESKTOP,
  116. G_USER_DIRECTORY_DOCUMENTS,
  117. G_USER_DIRECTORY_DOWNLOAD,
  118. G_USER_DIRECTORY_MUSIC,
  119. G_USER_DIRECTORY_PICTURES,
  120. G_USER_DIRECTORY_PUBLIC_SHARE,
  121. G_USER_DIRECTORY_TEMPLATES,
  122. G_USER_DIRECTORY_VIDEOS,
  123. G_USER_N_DIRECTORIES
  124. } GUserDirectory;
  125. GLIB_AVAILABLE_IN_ALL
  126. const gchar * g_get_user_special_dir (GUserDirectory directory);
  127. /**
  128. * GDebugKey:
  129. * @key: the string
  130. * @value: the flag
  131. *
  132. * Associates a string with a bit flag.
  133. * Used in g_parse_debug_string().
  134. */
  135. typedef struct _GDebugKey GDebugKey;
  136. struct _GDebugKey
  137. {
  138. const gchar *key;
  139. guint value;
  140. };
  141. /* Miscellaneous utility functions
  142. */
  143. GLIB_AVAILABLE_IN_ALL
  144. guint g_parse_debug_string (const gchar *string,
  145. const GDebugKey *keys,
  146. guint nkeys);
  147. GLIB_AVAILABLE_IN_ALL
  148. gint g_snprintf (gchar *string,
  149. gulong n,
  150. gchar const *format,
  151. ...) G_GNUC_PRINTF (3, 4);
  152. GLIB_AVAILABLE_IN_ALL
  153. gint g_vsnprintf (gchar *string,
  154. gulong n,
  155. gchar const *format,
  156. va_list args)
  157. G_GNUC_PRINTF(3, 0);
  158. GLIB_AVAILABLE_IN_ALL
  159. void g_nullify_pointer (gpointer *nullify_location);
  160. typedef enum
  161. {
  162. G_FORMAT_SIZE_DEFAULT = 0,
  163. G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
  164. G_FORMAT_SIZE_IEC_UNITS = 1 << 1,
  165. G_FORMAT_SIZE_BITS = 1 << 2
  166. } GFormatSizeFlags;
  167. GLIB_AVAILABLE_IN_2_30
  168. gchar *g_format_size_full (guint64 size,
  169. GFormatSizeFlags flags);
  170. GLIB_AVAILABLE_IN_2_30
  171. gchar *g_format_size (guint64 size);
  172. GLIB_DEPRECATED_IN_2_30_FOR(g_format_size)
  173. gchar *g_format_size_for_display (goffset size);
  174. #define g_ATEXIT(proc) (atexit (proc)) GLIB_DEPRECATED_MACRO_IN_2_32
  175. #define g_memmove(dest,src,len) \
  176. G_STMT_START { memmove ((dest), (src), (len)); } G_STMT_END GLIB_DEPRECATED_MACRO_IN_2_40_FOR(memmove)
  177. /**
  178. * GVoidFunc:
  179. *
  180. * Declares a type of function which takes no arguments
  181. * and has no return value. It is used to specify the type
  182. * function passed to g_atexit().
  183. */
  184. typedef void (*GVoidFunc) (void) GLIB_DEPRECATED_TYPE_IN_2_32;
  185. #define ATEXIT(proc) g_ATEXIT(proc) GLIB_DEPRECATED_MACRO_IN_2_32
  186. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  187. GLIB_DEPRECATED
  188. void g_atexit (GVoidFunc func);
  189. G_GNUC_END_IGNORE_DEPRECATIONS
  190. #ifdef G_OS_WIN32
  191. /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
  192. * atexit(), the function will be called when the GLib DLL is detached
  193. * from the program, which is not what the caller wants. The caller
  194. * wants the function to be called when it *itself* exits (or is
  195. * detached, in case the caller, too, is a DLL).
  196. */
  197. #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
  198. int atexit (void (*)(void));
  199. #endif
  200. #define g_atexit(func) atexit(func) GLIB_DEPRECATED_MACRO_IN_2_32
  201. #endif
  202. /* Look for an executable in PATH, following execvp() rules */
  203. GLIB_AVAILABLE_IN_ALL
  204. gchar* g_find_program_in_path (const gchar *program);
  205. /* Bit tests
  206. *
  207. * These are defined in a convoluted way because we want the compiler to
  208. * be able to inline the code for performance reasons, but for
  209. * historical reasons, we must continue to provide non-inline versions
  210. * on our ABI.
  211. *
  212. * We define these as functions in gutils.c which are just implemented
  213. * as calls to the _impl() versions in order to preserve the ABI.
  214. */
  215. #define g_bit_nth_lsf(mask, nth_bit) g_bit_nth_lsf_impl(mask, nth_bit)
  216. #define g_bit_nth_msf(mask, nth_bit) g_bit_nth_msf_impl(mask, nth_bit)
  217. #define g_bit_storage(number) g_bit_storage_impl(number)
  218. GLIB_AVAILABLE_IN_ALL
  219. gint (g_bit_nth_lsf) (gulong mask,
  220. gint nth_bit);
  221. GLIB_AVAILABLE_IN_ALL
  222. gint (g_bit_nth_msf) (gulong mask,
  223. gint nth_bit);
  224. GLIB_AVAILABLE_IN_ALL
  225. guint (g_bit_storage) (gulong number);
  226. static inline gint
  227. g_bit_nth_lsf_impl (gulong mask,
  228. gint nth_bit)
  229. {
  230. if (G_UNLIKELY (nth_bit < -1))
  231. nth_bit = -1;
  232. while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
  233. {
  234. nth_bit++;
  235. if (mask & (1UL << nth_bit))
  236. return nth_bit;
  237. }
  238. return -1;
  239. }
  240. static inline gint
  241. g_bit_nth_msf_impl (gulong mask,
  242. gint nth_bit)
  243. {
  244. if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
  245. nth_bit = GLIB_SIZEOF_LONG * 8;
  246. while (nth_bit > 0)
  247. {
  248. nth_bit--;
  249. if (mask & (1UL << nth_bit))
  250. return nth_bit;
  251. }
  252. return -1;
  253. }
  254. static inline guint
  255. g_bit_storage_impl (gulong number)
  256. {
  257. #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
  258. return G_LIKELY (number) ?
  259. ((GLIB_SIZEOF_LONG * 8U - 1) ^ (guint) __builtin_clzl(number)) + 1 : 1;
  260. #else
  261. guint n_bits = 0;
  262. do
  263. {
  264. n_bits++;
  265. number >>= 1;
  266. }
  267. while (number);
  268. return n_bits;
  269. #endif
  270. }
  271. /* Crashes the program. */
  272. #if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_50
  273. #ifndef G_OS_WIN32
  274. # include <stdlib.h>
  275. # define g_abort() abort ()
  276. #else
  277. GLIB_AVAILABLE_IN_2_50
  278. void g_abort (void) G_GNUC_NORETURN G_ANALYZER_NORETURN;
  279. #endif
  280. #endif
  281. /*
  282. * This macro is deprecated. This DllMain() is too complex. It is
  283. * recommended to write an explicit minimal DLlMain() that just saves
  284. * the handle to the DLL and then use that handle instead, for
  285. * instance passing it to
  286. * g_win32_get_package_installation_directory_of_module().
  287. *
  288. * On Windows, this macro defines a DllMain function that stores the
  289. * actual DLL name that the code being compiled will be included in.
  290. * STATIC should be empty or 'static'. DLL_NAME is the name of the
  291. * (pointer to the) char array where the DLL name will be stored. If
  292. * this is used, you must also include <windows.h>. If you need a more complex
  293. * DLL entry point function, you cannot use this.
  294. *
  295. * On non-Windows platforms, expands to nothing.
  296. */
  297. #ifndef G_PLATFORM_WIN32
  298. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) GLIB_DEPRECATED_MACRO_IN_2_26
  299. #else
  300. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
  301. static char *dll_name; \
  302. \
  303. BOOL WINAPI \
  304. DllMain (HINSTANCE hinstDLL, \
  305. DWORD fdwReason, \
  306. LPVOID lpvReserved) \
  307. { \
  308. wchar_t wcbfr[1000]; \
  309. char *tem; \
  310. switch (fdwReason) \
  311. { \
  312. case DLL_PROCESS_ATTACH: \
  313. GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
  314. tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
  315. dll_name = g_path_get_basename (tem); \
  316. g_free (tem); \
  317. break; \
  318. } \
  319. \
  320. return TRUE; \
  321. } GLIB_DEPRECATED_MACRO_IN_2_26
  322. #endif /* G_PLATFORM_WIN32 */
  323. G_END_DECLS
  324. #endif /* __G_UTILS_H__ */