gutils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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, 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 functions 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) && !defined (__cplusplus)
  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. } GFormatSizeFlags;
  166. GLIB_AVAILABLE_IN_2_30
  167. gchar *g_format_size_full (guint64 size,
  168. GFormatSizeFlags flags);
  169. GLIB_AVAILABLE_IN_2_30
  170. gchar *g_format_size (guint64 size);
  171. GLIB_DEPRECATED_FOR(g_format_size)
  172. gchar *g_format_size_for_display (goffset size);
  173. #ifndef G_DISABLE_DEPRECATED
  174. /**
  175. * GVoidFunc:
  176. *
  177. * Declares a type of function which takes no arguments
  178. * and has no return value. It is used to specify the type
  179. * function passed to g_atexit().
  180. */
  181. typedef void (*GVoidFunc) (void);
  182. #define ATEXIT(proc) g_ATEXIT(proc)
  183. GLIB_DEPRECATED
  184. void g_atexit (GVoidFunc func);
  185. #ifdef G_OS_WIN32
  186. /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
  187. * atexit(), the function will be called when the GLib DLL is detached
  188. * from the program, which is not what the caller wants. The caller
  189. * wants the function to be called when it *itself* exits (or is
  190. * detached, in case the caller, too, is a DLL).
  191. */
  192. #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
  193. int atexit (void (*)(void));
  194. #endif
  195. #define g_atexit(func) atexit(func)
  196. #endif
  197. #endif
  198. /* Look for an executable in PATH, following execvp() rules */
  199. GLIB_AVAILABLE_IN_ALL
  200. gchar* g_find_program_in_path (const gchar *program);
  201. /* Bit tests
  202. *
  203. * These are defined in a convoluted way because we want the compiler to
  204. * be able to inline the code for performance reasons, but for
  205. * historical reasons, we must continue to provide non-inline versions
  206. * on our ABI.
  207. *
  208. * We define these as functions in gutils.c which are just implemented
  209. * as calls to the _impl() versions in order to preserve the ABI.
  210. */
  211. #define g_bit_nth_lsf(mask, nth_bit) g_bit_nth_lsf_impl(mask, nth_bit)
  212. #define g_bit_nth_msf(mask, nth_bit) g_bit_nth_msf_impl(mask, nth_bit)
  213. #define g_bit_storage(number) g_bit_storage_impl(number)
  214. GLIB_AVAILABLE_IN_ALL
  215. gint (g_bit_nth_lsf) (gulong mask,
  216. gint nth_bit);
  217. GLIB_AVAILABLE_IN_ALL
  218. gint (g_bit_nth_msf) (gulong mask,
  219. gint nth_bit);
  220. GLIB_AVAILABLE_IN_ALL
  221. guint (g_bit_storage) (gulong number);
  222. static inline gint
  223. g_bit_nth_lsf_impl (gulong mask,
  224. gint nth_bit)
  225. {
  226. if (G_UNLIKELY (nth_bit < -1))
  227. nth_bit = -1;
  228. while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
  229. {
  230. nth_bit++;
  231. if (mask & (1UL << nth_bit))
  232. return nth_bit;
  233. }
  234. return -1;
  235. }
  236. static inline gint
  237. g_bit_nth_msf_impl (gulong mask,
  238. gint nth_bit)
  239. {
  240. if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
  241. nth_bit = GLIB_SIZEOF_LONG * 8;
  242. while (nth_bit > 0)
  243. {
  244. nth_bit--;
  245. if (mask & (1UL << nth_bit))
  246. return nth_bit;
  247. }
  248. return -1;
  249. }
  250. static inline guint
  251. g_bit_storage_impl (gulong number)
  252. {
  253. #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
  254. return G_LIKELY (number) ?
  255. ((GLIB_SIZEOF_LONG * 8U - 1) ^ (guint) __builtin_clzl(number)) + 1 : 1;
  256. #else
  257. guint n_bits = 0;
  258. do
  259. {
  260. n_bits++;
  261. number >>= 1;
  262. }
  263. while (number);
  264. return n_bits;
  265. #endif
  266. }
  267. #ifndef G_DISABLE_DEPRECATED
  268. /*
  269. * This macro is deprecated. This DllMain() is too complex. It is
  270. * recommended to write an explicit minimal DLlMain() that just saves
  271. * the handle to the DLL and then use that handle instead, for
  272. * instance passing it to
  273. * g_win32_get_package_installation_directory_of_module().
  274. *
  275. * On Windows, this macro defines a DllMain function that stores the
  276. * actual DLL name that the code being compiled will be included in.
  277. * STATIC should be empty or 'static'. DLL_NAME is the name of the
  278. * (pointer to the) char array where the DLL name will be stored. If
  279. * this is used, you must also include <windows.h>. If you need a more complex
  280. * DLL entry point function, you cannot use this.
  281. *
  282. * On non-Windows platforms, expands to nothing.
  283. */
  284. #ifndef G_PLATFORM_WIN32
  285. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
  286. #else
  287. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
  288. static char *dll_name; \
  289. \
  290. BOOL WINAPI \
  291. DllMain (HINSTANCE hinstDLL, \
  292. DWORD fdwReason, \
  293. LPVOID lpvReserved) \
  294. { \
  295. wchar_t wcbfr[1000]; \
  296. char *tem; \
  297. switch (fdwReason) \
  298. { \
  299. case DLL_PROCESS_ATTACH: \
  300. GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
  301. tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
  302. dll_name = g_path_get_basename (tem); \
  303. g_free (tem); \
  304. break; \
  305. } \
  306. \
  307. return TRUE; \
  308. }
  309. #endif /* !G_DISABLE_DEPRECATED */
  310. #endif /* G_PLATFORM_WIN32 */
  311. G_END_DECLS
  312. #endif /* __G_UTILS_H__ */