gutils.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  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. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  28. #error "Only <glib.h> can be included directly."
  29. #endif
  30. #include <glib/gtypes.h>
  31. #include <stdarg.h>
  32. G_BEGIN_DECLS
  33. GLIB_AVAILABLE_IN_ALL
  34. const gchar * g_get_user_name (void);
  35. GLIB_AVAILABLE_IN_ALL
  36. const gchar * g_get_real_name (void);
  37. GLIB_AVAILABLE_IN_ALL
  38. const gchar * g_get_home_dir (void);
  39. GLIB_AVAILABLE_IN_ALL
  40. const gchar * g_get_tmp_dir (void);
  41. GLIB_AVAILABLE_IN_ALL
  42. const gchar * g_get_host_name (void);
  43. GLIB_AVAILABLE_IN_ALL
  44. const gchar * g_get_prgname (void);
  45. GLIB_AVAILABLE_IN_ALL
  46. void g_set_prgname (const gchar *prgname);
  47. GLIB_AVAILABLE_IN_ALL
  48. const gchar * g_get_application_name (void);
  49. GLIB_AVAILABLE_IN_ALL
  50. void g_set_application_name (const gchar *application_name);
  51. GLIB_AVAILABLE_IN_2_64
  52. gchar * g_get_os_info (const gchar *key_name);
  53. /**
  54. * G_OS_INFO_KEY_NAME:
  55. *
  56. * A key to get the name of the operating system excluding version information suitable for presentation to the user, e.g. "YoYoOS"
  57. *
  58. * Since: 2.64
  59. */
  60. #define G_OS_INFO_KEY_NAME \
  61. GLIB_AVAILABLE_MACRO_IN_2_64 \
  62. "NAME"
  63. /**
  64. * G_OS_INFO_KEY_PRETTY_NAME:
  65. *
  66. * A key to get the name of the operating system in a format suitable for presentation to the user, e.g. "YoYoOS Foo"
  67. *
  68. * Since: 2.64
  69. */
  70. #define G_OS_INFO_KEY_PRETTY_NAME \
  71. GLIB_AVAILABLE_MACRO_IN_2_64 \
  72. "PRETTY_NAME"
  73. /**
  74. * G_OS_INFO_KEY_VERSION:
  75. *
  76. * A key to get the operating system version suitable for presentation to the user, e.g. "42 (Foo)"
  77. *
  78. * Since: 2.64
  79. */
  80. #define G_OS_INFO_KEY_VERSION \
  81. GLIB_AVAILABLE_MACRO_IN_2_64 \
  82. "VERSION"
  83. /**
  84. * G_OS_INFO_KEY_VERSION_CODENAME:
  85. *
  86. * A key to get a codename identifying the operating system release suitable for processing by scripts or usage in generated filenames, e.g. "foo"
  87. *
  88. * Since: 2.64
  89. */
  90. #define G_OS_INFO_KEY_VERSION_CODENAME \
  91. GLIB_AVAILABLE_MACRO_IN_2_64 \
  92. "VERSION_CODENAME"
  93. /**
  94. * G_OS_INFO_KEY_VERSION_ID:
  95. *
  96. * A key to get the version of the operating system suitable for processing by scripts or usage in generated filenames, e.g. "42"
  97. *
  98. * Since: 2.64
  99. */
  100. #define G_OS_INFO_KEY_VERSION_ID \
  101. GLIB_AVAILABLE_MACRO_IN_2_64 \
  102. "VERSION_ID"
  103. /**
  104. * G_OS_INFO_KEY_ID:
  105. *
  106. * A key to get an ID identifying the operating system suitable for processing by scripts or usage in generated filenames, e.g. "yoyoos"
  107. *
  108. * Since: 2.64
  109. */
  110. #define G_OS_INFO_KEY_ID \
  111. GLIB_AVAILABLE_MACRO_IN_2_64 \
  112. "ID"
  113. /**
  114. * G_OS_INFO_KEY_HOME_URL:
  115. *
  116. * A key to get the homepage for the operating system, e.g. "https://www.yoyo-os.com/"
  117. *
  118. * Since: 2.64
  119. */
  120. #define G_OS_INFO_KEY_HOME_URL \
  121. GLIB_AVAILABLE_MACRO_IN_2_64 \
  122. "HOME_URL"
  123. /**
  124. * G_OS_INFO_KEY_DOCUMENTATION_URL:
  125. *
  126. * A key to get the documentation page for the operating system, e.g. "https://docs.yoyo-os.com/"
  127. *
  128. * Since: 2.64
  129. */
  130. #define G_OS_INFO_KEY_DOCUMENTATION_URL \
  131. GLIB_AVAILABLE_MACRO_IN_2_64 \
  132. "DOCUMENTATION_URL"
  133. /**
  134. * G_OS_INFO_KEY_SUPPORT_URL:
  135. *
  136. * A key to get the support page for the operating system, e.g. "https://support.yoyo-os.com/"
  137. *
  138. * Since: 2.64
  139. */
  140. #define G_OS_INFO_KEY_SUPPORT_URL \
  141. GLIB_AVAILABLE_MACRO_IN_2_64 \
  142. "SUPPORT_URL"
  143. /**
  144. * G_OS_INFO_KEY_BUG_REPORT_URL:
  145. *
  146. * A key to get the bug reporting page for the operating system, e.g. "https://bugs.yoyo-os.com/"
  147. *
  148. * Since: 2.64
  149. */
  150. #define G_OS_INFO_KEY_BUG_REPORT_URL \
  151. GLIB_AVAILABLE_MACRO_IN_2_64 \
  152. "BUG_REPORT_URL"
  153. /**
  154. * G_OS_INFO_KEY_PRIVACY_POLICY_URL:
  155. *
  156. * A key to get the privacy policy for the operating system, e.g. "https://privacy.yoyo-os.com/"
  157. *
  158. * Since: 2.64
  159. */
  160. #define G_OS_INFO_KEY_PRIVACY_POLICY_URL \
  161. GLIB_AVAILABLE_MACRO_IN_2_64 \
  162. "PRIVACY_POLICY_URL"
  163. GLIB_AVAILABLE_IN_ALL
  164. void g_reload_user_special_dirs_cache (void);
  165. GLIB_AVAILABLE_IN_ALL
  166. const gchar * g_get_user_data_dir (void);
  167. GLIB_AVAILABLE_IN_ALL
  168. const gchar * g_get_user_config_dir (void);
  169. GLIB_AVAILABLE_IN_ALL
  170. const gchar * g_get_user_cache_dir (void);
  171. GLIB_AVAILABLE_IN_2_72
  172. const gchar * g_get_user_state_dir (void);
  173. GLIB_AVAILABLE_IN_ALL
  174. const gchar * const * g_get_system_data_dirs (void);
  175. #ifdef G_OS_WIN32
  176. /* This function is not part of the public GLib API */
  177. GLIB_AVAILABLE_IN_ALL
  178. const gchar * const * g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void));
  179. #endif
  180. #if defined (G_OS_WIN32) && defined (G_CAN_INLINE)
  181. /* This function is not part of the public GLib API either. Just call
  182. * g_get_system_data_dirs() in your code, never mind that that is
  183. * actually a macro and you will in fact call this inline function.
  184. */
  185. static inline const gchar * const *
  186. _g_win32_get_system_data_dirs (void)
  187. {
  188. return g_win32_get_system_data_dirs_for_module ((void (*)(void)) &_g_win32_get_system_data_dirs);
  189. }
  190. #define g_get_system_data_dirs _g_win32_get_system_data_dirs
  191. #endif
  192. GLIB_AVAILABLE_IN_ALL
  193. const gchar * const * g_get_system_config_dirs (void);
  194. GLIB_AVAILABLE_IN_ALL
  195. const gchar * g_get_user_runtime_dir (void);
  196. /**
  197. * GUserDirectory:
  198. * @G_USER_DIRECTORY_DESKTOP: the user's Desktop directory
  199. * @G_USER_DIRECTORY_DOCUMENTS: the user's Documents directory
  200. * @G_USER_DIRECTORY_DOWNLOAD: the user's Downloads directory
  201. * @G_USER_DIRECTORY_MUSIC: the user's Music directory
  202. * @G_USER_DIRECTORY_PICTURES: the user's Pictures directory
  203. * @G_USER_DIRECTORY_PUBLIC_SHARE: the user's shared directory
  204. * @G_USER_DIRECTORY_TEMPLATES: the user's Templates directory
  205. * @G_USER_DIRECTORY_VIDEOS: the user's Movies directory
  206. * @G_USER_N_DIRECTORIES: the number of enum values
  207. *
  208. * These are logical ids for special directories which are defined
  209. * depending on the platform used. You should use g_get_user_special_dir()
  210. * to retrieve the full path associated to the logical id.
  211. *
  212. * The #GUserDirectory enumeration can be extended at later date. Not
  213. * every platform has a directory for every logical id in this
  214. * enumeration.
  215. *
  216. * Since: 2.14
  217. */
  218. typedef enum {
  219. G_USER_DIRECTORY_DESKTOP,
  220. G_USER_DIRECTORY_DOCUMENTS,
  221. G_USER_DIRECTORY_DOWNLOAD,
  222. G_USER_DIRECTORY_MUSIC,
  223. G_USER_DIRECTORY_PICTURES,
  224. G_USER_DIRECTORY_PUBLIC_SHARE,
  225. G_USER_DIRECTORY_TEMPLATES,
  226. G_USER_DIRECTORY_VIDEOS,
  227. G_USER_N_DIRECTORIES
  228. } GUserDirectory;
  229. GLIB_AVAILABLE_IN_ALL
  230. const gchar * g_get_user_special_dir (GUserDirectory directory);
  231. /**
  232. * GDebugKey:
  233. * @key: the string
  234. * @value: the flag
  235. *
  236. * Associates a string with a bit flag.
  237. * Used in g_parse_debug_string().
  238. */
  239. typedef struct _GDebugKey GDebugKey;
  240. struct _GDebugKey
  241. {
  242. const gchar *key;
  243. guint value;
  244. };
  245. /* Miscellaneous utility functions
  246. */
  247. GLIB_AVAILABLE_IN_ALL
  248. guint g_parse_debug_string (const gchar *string,
  249. const GDebugKey *keys,
  250. guint nkeys);
  251. GLIB_AVAILABLE_IN_ALL
  252. gint g_snprintf (gchar *string,
  253. gulong n,
  254. gchar const *format,
  255. ...) G_GNUC_PRINTF (3, 4);
  256. GLIB_AVAILABLE_IN_ALL
  257. gint g_vsnprintf (gchar *string,
  258. gulong n,
  259. gchar const *format,
  260. va_list args)
  261. G_GNUC_PRINTF(3, 0);
  262. GLIB_AVAILABLE_IN_ALL
  263. void g_nullify_pointer (gpointer *nullify_location);
  264. typedef enum
  265. {
  266. G_FORMAT_SIZE_DEFAULT = 0,
  267. G_FORMAT_SIZE_LONG_FORMAT = 1 << 0,
  268. G_FORMAT_SIZE_IEC_UNITS = 1 << 1,
  269. G_FORMAT_SIZE_BITS = 1 << 2,
  270. G_FORMAT_SIZE_ONLY_VALUE GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 1 << 3,
  271. G_FORMAT_SIZE_ONLY_UNIT GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 1 << 4
  272. } GFormatSizeFlags;
  273. GLIB_AVAILABLE_IN_2_30
  274. gchar *g_format_size_full (guint64 size,
  275. GFormatSizeFlags flags);
  276. GLIB_AVAILABLE_IN_2_30
  277. gchar *g_format_size (guint64 size);
  278. GLIB_DEPRECATED_IN_2_30_FOR(g_format_size)
  279. gchar *g_format_size_for_display (goffset size);
  280. #define g_ATEXIT(proc) (atexit (proc)) GLIB_DEPRECATED_MACRO_IN_2_32
  281. #define g_memmove(dest,src,len) \
  282. G_STMT_START { memmove ((dest), (src), (len)); } G_STMT_END GLIB_DEPRECATED_MACRO_IN_2_40_FOR(memmove)
  283. /**
  284. * GVoidFunc:
  285. *
  286. * Declares a type of function which takes no arguments
  287. * and has no return value. It is used to specify the type
  288. * function passed to g_atexit().
  289. */
  290. typedef void (*GVoidFunc) (void) GLIB_DEPRECATED_TYPE_IN_2_32;
  291. #define ATEXIT(proc) g_ATEXIT(proc) GLIB_DEPRECATED_MACRO_IN_2_32
  292. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  293. GLIB_DEPRECATED
  294. void g_atexit (GVoidFunc func);
  295. G_GNUC_END_IGNORE_DEPRECATIONS
  296. #ifdef G_OS_WIN32
  297. /* It's a bad idea to wrap atexit() on Windows. If the GLib DLL calls
  298. * atexit(), the function will be called when the GLib DLL is detached
  299. * from the program, which is not what the caller wants. The caller
  300. * wants the function to be called when it *itself* exits (or is
  301. * detached, in case the caller, too, is a DLL).
  302. */
  303. #if (defined(__MINGW_H) && !defined(_STDLIB_H_)) || (defined(_MSC_VER) && !defined(_INC_STDLIB))
  304. int atexit (void (*)(void));
  305. #endif
  306. #define g_atexit(func) atexit(func) GLIB_DEPRECATED_MACRO_IN_2_32
  307. #endif
  308. /* Look for an executable in PATH, following execvp() rules */
  309. GLIB_AVAILABLE_IN_ALL
  310. gchar* g_find_program_in_path (const gchar *program);
  311. /* Bit tests
  312. *
  313. * These are defined in a convoluted way because we want the compiler to
  314. * be able to inline the code for performance reasons, but for
  315. * historical reasons, we must continue to provide non-inline versions
  316. * on our ABI.
  317. *
  318. * We define these as functions in gutils.c which are just implemented
  319. * as calls to the _impl() versions in order to preserve the ABI.
  320. */
  321. #define g_bit_nth_lsf(mask, nth_bit) g_bit_nth_lsf_impl(mask, nth_bit)
  322. #define g_bit_nth_msf(mask, nth_bit) g_bit_nth_msf_impl(mask, nth_bit)
  323. #define g_bit_storage(number) g_bit_storage_impl(number)
  324. GLIB_AVAILABLE_IN_ALL
  325. gint (g_bit_nth_lsf) (gulong mask,
  326. gint nth_bit);
  327. GLIB_AVAILABLE_IN_ALL
  328. gint (g_bit_nth_msf) (gulong mask,
  329. gint nth_bit);
  330. GLIB_AVAILABLE_IN_ALL
  331. guint (g_bit_storage) (gulong number);
  332. static inline gint
  333. g_bit_nth_lsf_impl (gulong mask,
  334. gint nth_bit)
  335. {
  336. if (G_UNLIKELY (nth_bit < -1))
  337. nth_bit = -1;
  338. while (nth_bit < ((GLIB_SIZEOF_LONG * 8) - 1))
  339. {
  340. nth_bit++;
  341. if (mask & (1UL << nth_bit))
  342. return nth_bit;
  343. }
  344. return -1;
  345. }
  346. static inline gint
  347. g_bit_nth_msf_impl (gulong mask,
  348. gint nth_bit)
  349. {
  350. if (nth_bit < 0 || G_UNLIKELY (nth_bit > GLIB_SIZEOF_LONG * 8))
  351. nth_bit = GLIB_SIZEOF_LONG * 8;
  352. while (nth_bit > 0)
  353. {
  354. nth_bit--;
  355. if (mask & (1UL << nth_bit))
  356. return nth_bit;
  357. }
  358. return -1;
  359. }
  360. static inline guint
  361. g_bit_storage_impl (gulong number)
  362. {
  363. #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
  364. return G_LIKELY (number) ?
  365. ((GLIB_SIZEOF_LONG * 8U - 1) ^ (guint) __builtin_clzl(number)) + 1 : 1;
  366. #else
  367. guint n_bits = 0;
  368. do
  369. {
  370. n_bits++;
  371. number >>= 1;
  372. }
  373. while (number);
  374. return n_bits;
  375. #endif
  376. }
  377. /* Crashes the program. */
  378. #if GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_50
  379. #ifndef G_OS_WIN32
  380. # include <stdlib.h>
  381. # define g_abort() abort ()
  382. #else
  383. G_NORETURN GLIB_AVAILABLE_IN_2_50 void g_abort (void) G_ANALYZER_NORETURN;
  384. #endif
  385. #endif
  386. /*
  387. * This macro is deprecated. This DllMain() is too complex. It is
  388. * recommended to write an explicit minimal DLlMain() that just saves
  389. * the handle to the DLL and then use that handle instead, for
  390. * instance passing it to
  391. * g_win32_get_package_installation_directory_of_module().
  392. *
  393. * On Windows, this macro defines a DllMain function that stores the
  394. * actual DLL name that the code being compiled will be included in.
  395. * STATIC should be empty or 'static'. DLL_NAME is the name of the
  396. * (pointer to the) char array where the DLL name will be stored. If
  397. * this is used, you must also include <windows.h>. If you need a more complex
  398. * DLL entry point function, you cannot use this.
  399. *
  400. * On non-Windows platforms, expands to nothing.
  401. */
  402. #ifndef G_PLATFORM_WIN32
  403. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) GLIB_DEPRECATED_MACRO_IN_2_26
  404. #else
  405. # define G_WIN32_DLLMAIN_FOR_DLL_NAME(static, dll_name) \
  406. static char *dll_name; \
  407. \
  408. BOOL WINAPI \
  409. DllMain (HINSTANCE hinstDLL, \
  410. DWORD fdwReason, \
  411. LPVOID lpvReserved) \
  412. { \
  413. wchar_t wcbfr[1000]; \
  414. char *tem; \
  415. switch (fdwReason) \
  416. { \
  417. case DLL_PROCESS_ATTACH: \
  418. GetModuleFileNameW ((HMODULE) hinstDLL, wcbfr, G_N_ELEMENTS (wcbfr)); \
  419. tem = g_utf16_to_utf8 (wcbfr, -1, NULL, NULL, NULL); \
  420. dll_name = g_path_get_basename (tem); \
  421. g_free (tem); \
  422. break; \
  423. } \
  424. \
  425. return TRUE; \
  426. } GLIB_DEPRECATED_MACRO_IN_2_26
  427. #endif /* G_PLATFORM_WIN32 */
  428. G_END_DECLS
  429. #endif /* __G_UTILS_H__ */