gutils.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /* inlining hassle. for compilers that don't allow the 'inline' keyword,
  44. * mostly because of strict ANSI C compliance or dumbness, we try to fall
  45. * back to either '__inline__' or '__inline'.
  46. * G_CAN_INLINE is defined in glibconfig.h if the compiler seems to be
  47. * actually *capable* to do function inlining, in which case inline
  48. * function bodies do make sense. we also define G_INLINE_FUNC to properly
  49. * export the function prototypes if no inlining can be performed.
  50. * inline function bodies have to be special cased with G_CAN_INLINE and a
  51. * .c file specific macro to allow one compiled instance with extern linkage
  52. * of the functions by defining G_IMPLEMENT_INLINES and the .c file macro.
  53. */
  54. #if defined (G_HAVE_INLINE) && defined (__GNUC__) && defined (__STRICT_ANSI__)
  55. # undef inline
  56. # define inline __inline__
  57. #elif !defined (G_HAVE_INLINE)
  58. # undef inline
  59. # if defined (G_HAVE___INLINE__)
  60. # define inline __inline__
  61. # elif defined (G_HAVE___INLINE)
  62. # define inline __inline
  63. # else /* !inline && !__inline__ && !__inline */
  64. # define inline /* don't inline, then */
  65. # endif
  66. #endif
  67. #ifdef G_IMPLEMENT_INLINES
  68. # define G_INLINE_FUNC _GLIB_EXTERN
  69. # undef G_CAN_INLINE
  70. #elif defined (__GNUC__)
  71. # define G_INLINE_FUNC static __inline __attribute__ ((unused))
  72. #elif defined (G_CAN_INLINE)
  73. # define G_INLINE_FUNC static inline
  74. #else /* can't inline */
  75. # define G_INLINE_FUNC _GLIB_EXTERN
  76. #endif /* !G_INLINE_FUNC */
  77. GLIB_AVAILABLE_IN_ALL
  78. const gchar * g_get_user_name (void);
  79. GLIB_AVAILABLE_IN_ALL
  80. const gchar * g_get_real_name (void);
  81. GLIB_AVAILABLE_IN_ALL
  82. const gchar * g_get_home_dir (void);
  83. GLIB_AVAILABLE_IN_ALL
  84. const gchar * g_get_tmp_dir (void);
  85. GLIB_AVAILABLE_IN_ALL
  86. const gchar * g_get_host_name (void);
  87. GLIB_AVAILABLE_IN_ALL
  88. const gchar * g_get_prgname (void);
  89. GLIB_AVAILABLE_IN_ALL
  90. void g_set_prgname (const gchar *prgname);
  91. GLIB_AVAILABLE_IN_ALL
  92. const gchar * g_get_application_name (void);
  93. GLIB_AVAILABLE_IN_ALL
  94. void g_set_application_name (const gchar *application_name);
  95. GLIB_AVAILABLE_IN_ALL
  96. void g_reload_user_special_dirs_cache (void);
  97. GLIB_AVAILABLE_IN_ALL
  98. const gchar * g_get_user_data_dir (void);
  99. GLIB_AVAILABLE_IN_ALL
  100. const gchar * g_get_user_config_dir (void);
  101. GLIB_AVAILABLE_IN_ALL
  102. const gchar * g_get_user_cache_dir (void);
  103. GLIB_AVAILABLE_IN_ALL
  104. const gchar * const * g_get_system_data_dirs (void);
  105. #ifdef G_OS_WIN32
  106. /* This functions is not part of the public GLib API */
  107. GLIB_AVAILABLE_IN_ALL
  108. const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
  109. #endif
  110. #if defined (G_OS_WIN32) && defined (G_CAN_INLINE) && !defined (__cplusplus)
  111. /* This function is not part of the public GLib API either. Just call
  112. * g_get_system_data_dirs() in your code, never mind that that is
  113. * actually a macro and you will in fact call this inline function.
  114. */
  115. static inline const gchar * const *
  116. _g_win32_get_system_data_dirs (void)
  117. {
  118. return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
  119. }
  120. #define g_get_system_data_dirs _g_win32_get_system_data_dirs
  121. #endif
  122. GLIB_AVAILABLE_IN_ALL
  123. const gchar * const * g_get_system_config_dirs (void);
  124. GLIB_AVAILABLE_IN_ALL
  125. const gchar * g_get_user_runtime_dir (void);
  126. /**
  127. * GUserDirectory:
  128. * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
  129. * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
  130. * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
  131. * @G_USER_DIRECTORY_MUSIC: the user's Music directory
  132. * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
  133. * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
  134. * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
  135. * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
  136. * @G_USER_N_DIRECTORIES: the number of enum values
  137. *
  138. * These are logical ids for special directories which are defined
  139. * depending on the platform used. You should use g_get_user_special_dir()
  140. * to retrieve the full path associated to the logical id.
  141. *
  142. * The #GUserDirectory enumeration can be extended at later date. Not
  143. * every platform has a directory for every logical id in this
  144. * enumeration.
  145. *
  146. * Since: 2.14
  147. */
  148. typedef enum {
  149. G_USER_DIRECTORY_DESKTOP,
  150. G_USER_DIRECTORY_DOCUMENTS,
  151. G_USER_DIRECTORY_DOWNLOAD,
  152. G_USER_DIRECTORY_MUSIC,
  153. G_USER_DIRECTORY_PICTURES,
  154. G_USER_DIRECTORY_PUBLIC_SHARE,
  155. G_USER_DIRECTORY_TEMPLATES,
  156. G_USER_DIRECTORY_VIDEOS,
  157. G_USER_N_DIRECTORIES
  158. } GUserDirectory;
  159. GLIB_AVAILABLE_IN_ALL
  160. const gchar * g_get_user_special_dir (GUserDirectory directory);
  161. /**
  162. * GDebugKey:
  163. * @key: the string
  164. * @value: the flag
  165. *
  166. * Associates a string with a bit flag.
  167. * Used in g_parse_debug_string().
  168. */
  169. typedef struct _GDebugKey GDebugKey;
  170. struct _GDebugKey
  171. {
  172. const gchar *key;
  173. guint value;
  174. };
  175. /* Miscellaneous utility functions
  176. */
  177. GLIB_AVAILABLE_IN_ALL
  178. guint g_parse_debug_string (const gchar *string,
  179. const GDebugKey *keys,
  180. guint nkeys);
  181. GLIB_AVAILABLE_IN_ALL
  182. gint g_snprintf (gchar *string,
  183. gulong n,
  184. gchar const *format,
  185. ...) G_GNUC_PRINTF (3, 4);
  186. GLIB_AVAILABLE_IN_ALL
  187. gint g_vsnprintf (gchar *string,
  188. gulong n,
  189. gchar const *format,
  190. va_list args)
  191. G_GNUC_PRINTF(3, 0);
  192. GLIB_AVAILABLE_IN_ALL
  193. void g_nullify_pointer (gpointer *nullify_location);
  194. typedef enum
  195. {
  196. G_FORMAT_SIZE_DEFAULT = 0,
  197. G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
  198. G_FORMAT_SIZE_IEC_UNITS = 1 << 1
  199. } GFormatSizeFlags;
  200. GLIB_AVAILABLE_IN_2_30
  201. gchar *g_format_size_full (guint64 size,
  202. GFormatSizeFlags flags);
  203. GLIB_AVAILABLE_IN_2_30
  204. gchar *g_format_size (guint64 size);
  205. GLIB_DEPRECATED_FOR(g_format_size)
  206. gchar *g_format_size_for_display (goffset size);
  207. #ifndef G_DISABLE_DEPRECATED
  208. /**
  209. * GVoidFunc:
  210. *
  211. * Declares a type of function which takes no arguments
  212. * and has no return value. It is used to specify the type
  213. * function passed to g_atexit().
  214. */
  215. typedef void (*GVoidFunc) (void);
  216. #define ATEXIT(proc) g_ATEXIT(proc)
  217. GLIB_DEPRECATED
  218. void g_atexit (GVoidFunc func);
  219. #ifdef G_OS_WIN32
  220. /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
  221. * atexit(), the function will be called when the GLib DLL is detached
  222. * from the program, which is not what the caller wants. The caller
  223. * wants the function to be called when it *itself* exits (or is
  224. * detached, in case the caller, too, is a DLL).
  225. */
  226. #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
  227. int atexit (void (*)(void));
  228. #endif
  229. #define g_atexit(func) atexit(func)
  230. #endif
  231. #endif /* G_DISABLE_DEPRECATED */
  232. /* Look for an executable in PATH, following execvp() rules */
  233. GLIB_AVAILABLE_IN_ALL
  234. gchar* g_find_program_in_path (const gchar *program);
  235. /* Bit tests
  236. */
  237. G_INLINE_FUNC gint g_bit_nth_lsf (gulong mask,
  238. gint nth_bit) G_GNUC_CONST;
  239. G_INLINE_FUNC gint g_bit_nth_msf (gulong mask,
  240. gint nth_bit) G_GNUC_CONST;
  241. G_INLINE_FUNC guint g_bit_storage (gulong number) G_GNUC_CONST;
  242. /* inline function implementations
  243. */
  244. #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__)
  245. G_INLINE_FUNC gint
  246. g_bit_nth_lsf (gulong mask,
  247. gint nth_bit)
  248. {
  249. if (G_UNLIKELY (nth_bit < -1))
  250. nth_bit = -1;
  251. while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
  252. {
  253. nth_bit++;
  254. if (mask & (1UL << nth_bit))
  255. return nth_bit;
  256. }
  257. return -1;
  258. }
  259. G_INLINE_FUNC gint
  260. g_bit_nth_msf (gulong mask,
  261. gint nth_bit)
  262. {
  263. if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
  264. nth_bit = GLIB_SIZEOF_LONG * 8;
  265. while (nth_bit > 0)
  266. {
  267. nth_bit--;
  268. if (mask & (1UL << nth_bit))
  269. return nth_bit;
  270. }
  271. return -1;
  272. }
  273. G_INLINE_FUNC guint
  274. g_bit_storage (gulong number)
  275. {
  276. #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
  277. return G_LIKELY (number) ?
  278. ((GLIB_SIZEOF_LONG * 8U - 1) ^ (guint) __builtin_clzl(number)) + 1 : 1;
  279. #else
  280. register guint n_bits = 0;
  281. do
  282. {
  283. n_bits++;
  284. number >>= 1;
  285. }
  286. while (number);
  287. return n_bits;
  288. #endif
  289. }
  290. #endif /* G_CAN_INLINE || __G_UTILS_C__ */
  291. #ifndef G_DISABLE_DEPRECATED
  292. /*
  293. * This macro is deprecated. This DllMain() is too complex. It is
  294. * recommended to write an explicit minimal DLlMain() that just saves
  295. * the handle to the DLL and then use that handle instead, for
  296. * instance passing it to
  297. * g_win32_get_package_installation_directory_of_module().
  298. *
  299. * On Windows, this macro defines a DllMain function that stores the
  300. * actual DLL name that the code being compiled will be included in.
  301. * STATIC should be empty or 'static'. DLL_NAME is the name of the
  302. * (pointer to the) char array where the DLL name will be stored. If
  303. * this is used, you must also include <windows.h>. If you need a more complex
  304. * DLL entry point function, you cannot use this.
  305. *
  306. * On non-Windows platforms, expands to nothing.
  307. */
  308. #ifndef G_PLATFORM_WIN32
  309. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name)
  310. #else
  311. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
  312. static char *dll_name; \
  313. \
  314. BOOL WINAPI \
  315. DllMain (HINSTANCE hinstDLL, \
  316. DWORD fdwReason, \
  317. LPVOID lpvReserved) \
  318. { \
  319. wchar_t wcbfr[1000]; \
  320. char *tem; \
  321. switch (fdwReason) \
  322. { \
  323. case DLL_PROCESS_ATTACH: \
  324. GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
  325. tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
  326. dll_name = g_path_get_basename (tem); \
  327. g_free (tem); \
  328. break; \
  329. } \
  330. \
  331. return TRUE; \
  332. }
  333. #endif /* !G_DISABLE_DEPRECATED */
  334. #endif /* G_PLATFORM_WIN32 */
  335. G_END_DECLS
  336. #endif /* __G_UTILS_H__ */