You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

491 line
16 KiB

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