glibcompat.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. GLIB - Library of useful routines for C programming
  3. Copyright (C) 2009-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009, 2013.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file glibcompat.c
  20. * \brief Source: compatibility with older versions of glib
  21. *
  22. * Following code was copied from glib to GNU Midnight Commander to
  23. * provide compatibility with older versions of glib.
  24. */
  25. #include <config.h>
  26. #include <string.h>
  27. #include "global.h"
  28. #include "glibcompat.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. /*** file scope functions ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. /* --------------------------------------------------------------------------------------------- */
  37. /*** public functions ****************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. #if ! GLIB_CHECK_VERSION (2, 54, 0)
  40. /**
  41. * g_ptr_array_find_with_equal_func: (skip)
  42. * @haystack: pointer array to be searched
  43. * @needle: pointer to look for
  44. * @equal_func: (nullable): the function to call for each element, which should
  45. * return %TRUE when the desired element is found; or %NULL to use pointer
  46. * equality
  47. * @index_: (optional) (out): return location for the index of
  48. * the element, if found
  49. *
  50. * Checks whether @needle exists in @haystack, using the given @equal_func.
  51. * If the element is found, %TRUE is returned and the element^A^A^As index is
  52. * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
  53. * is undefined. If @needle exists multiple times in @haystack, the index of
  54. * the first instance is returned.
  55. *
  56. * @equal_func is called with the element from the array as its first parameter,
  57. * and @needle as its second parameter. If @equal_func is %NULL, pointer
  58. * equality is used.
  59. *
  60. * Returns: %TRUE if @needle is one of the elements of @haystack
  61. * Since: 2.54
  62. */
  63. gboolean
  64. g_ptr_array_find_with_equal_func (GPtrArray *haystack, gconstpointer needle, GEqualFunc equal_func,
  65. guint *index_)
  66. {
  67. guint i;
  68. g_return_val_if_fail (haystack != NULL, FALSE);
  69. if (equal_func == NULL)
  70. equal_func = g_direct_equal;
  71. for (i = 0; i < haystack->len; i++)
  72. if (equal_func (g_ptr_array_index (haystack, i), needle))
  73. {
  74. if (index_ != NULL)
  75. *index_ = i;
  76. return TRUE;
  77. }
  78. return FALSE;
  79. }
  80. #endif /* ! GLIB_CHECK_VERSION (2, 54, 0) */
  81. /* --------------------------------------------------------------------------------------------- */
  82. #if ! GLIB_CHECK_VERSION (2, 63, 3)
  83. /**
  84. * g_clear_slist: (skip)
  85. * @slist_ptr: (not nullable): a #GSList return location
  86. * @destroy: (nullable): the function to pass to g_slist_free_full() or NULL to not free elements
  87. *
  88. * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy.
  89. *
  90. * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing.
  91. *
  92. * Since: 2.64
  93. */
  94. void
  95. g_clear_slist (GSList **slist_ptr, GDestroyNotify destroy)
  96. {
  97. GSList *slist;
  98. slist = *slist_ptr;
  99. if (slist != NULL)
  100. {
  101. *slist_ptr = NULL;
  102. if (destroy != NULL)
  103. g_slist_free_full (slist, destroy);
  104. else
  105. g_slist_free (slist);
  106. }
  107. }
  108. /* --------------------------------------------------------------------------------------------- */
  109. /**
  110. * g_clear_list:
  111. * @list_ptr: (not nullable): a #GList return location
  112. * @destroy: (nullable): the function to pass to g_list_free_full() or NULL to not free elements
  113. *
  114. * Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy.
  115. *
  116. * @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing.
  117. *
  118. * Since: 2.64
  119. */
  120. void
  121. g_clear_list (GList **list_ptr, GDestroyNotify destroy)
  122. {
  123. GList *list;
  124. list = *list_ptr;
  125. if (list != NULL)
  126. {
  127. *list_ptr = NULL;
  128. if (destroy != NULL)
  129. g_list_free_full (list, destroy);
  130. else
  131. g_list_free (list);
  132. }
  133. }
  134. #endif /* ! GLIB_CHECK_VERSION (2, 63, 3) */
  135. /* --------------------------------------------------------------------------------------------- */
  136. #if ! GLIB_CHECK_VERSION (2, 60, 0)
  137. /**
  138. * g_queue_clear_full:
  139. * @queue: a pointer to a #GQueue
  140. * @free_func: (nullable): the function to be called to free memory allocated
  141. *
  142. * Convenience method, which frees all the memory used by a #GQueue,
  143. * and calls the provided @free_func on each item in the #GQueue.
  144. *
  145. * Since: 2.60
  146. */
  147. void
  148. g_queue_clear_full (GQueue *queue, GDestroyNotify free_func)
  149. {
  150. g_return_if_fail (queue != NULL);
  151. if (free_func != NULL)
  152. g_queue_foreach (queue, (GFunc) free_func, NULL);
  153. g_queue_clear (queue);
  154. }
  155. #endif /* ! GLIB_CHECK_VERSION (2, 60, 0) */
  156. /* --------------------------------------------------------------------------------------------- */
  157. #if ! GLIB_CHECK_VERSION (2, 77, 0)
  158. /**
  159. * g_string_new_take:
  160. * @init: (nullable): initial text used as the string.
  161. * Ownership of the string is transferred to the #GString.
  162. * Passing NULL creates an empty string.
  163. *
  164. * Creates a new #GString, initialized with the given string.
  165. *
  166. * After this call, @init belongs to the #GString and may no longer be
  167. * modified by the caller. The memory of @data has to be dynamically
  168. * allocated and will eventually be freed with g_free().
  169. *
  170. * Returns: the new #GString
  171. */
  172. GString *
  173. g_string_new_take (char *init)
  174. {
  175. GString *string;
  176. if (init == NULL)
  177. return g_string_new (NULL);
  178. string = g_slice_new (GString);
  179. string->str = init;
  180. string->len = strlen (string->str);
  181. string->allocated_len = string->len + 1;
  182. return string;
  183. }
  184. #endif /* ! GLIB_CHECK_VERSION (2, 77, 0) */
  185. /* --------------------------------------------------------------------------------------------- */
  186. /**
  187. * mc_g_string_copy:
  188. * @dest: (not nullable): the destination #GString. Its current contents are destroyed
  189. * @src: (not nullable): the source #GString
  190. * @return: @dest
  191. *
  192. * Copies the bytes from a #GString into a #GString, destroying any previous contents.
  193. * It is rather like the standard strcpy() function, except that you do not have to worry about
  194. * having enough space to copy the string.
  195. *
  196. * There is no such API in GLib2.
  197. */
  198. GString *
  199. mc_g_string_copy (GString *dest, const GString *src)
  200. {
  201. g_return_val_if_fail (src != NULL, NULL);
  202. g_return_val_if_fail (dest != NULL, NULL);
  203. g_string_set_size (dest, 0);
  204. g_string_append_len (dest, src->str, src->len);
  205. return dest;
  206. }
  207. /* --------------------------------------------------------------------------------------------- */
  208. /**
  209. * mc_g_string_dup:
  210. * @s: (nullable): the source #GString
  211. * @return: @copy of @s
  212. *
  213. * Copies the bytes from one #GString to another.
  214. *
  215. * There is no such API in GLib2.
  216. */
  217. GString *
  218. mc_g_string_dup (const GString *s)
  219. {
  220. GString *ret = NULL;
  221. if (s != NULL)
  222. ret = g_string_new_len (s->str, s->len);
  223. return ret;
  224. }
  225. /* --------------------------------------------------------------------------------------------- */
  226. /**
  227. * mc_g_string_append_c_len:
  228. * @s: (not nullable): the destination #GString.
  229. * @c: the byte to append onto the end of @s
  230. * @len: the number of bytes @c to append onto the end of @s
  231. * @return: @s
  232. *
  233. * Adds @len bytes @c onto the end of @s.
  234. *
  235. * There is no such API in GLib2.
  236. */
  237. GString *
  238. mc_g_string_append_c_len (GString *s, gchar c, guint len)
  239. {
  240. g_return_val_if_fail (s != NULL, NULL);
  241. if (len != 0)
  242. {
  243. guint s_len = s->len;
  244. g_string_set_size (s, s->len + len);
  245. memset (s->str + s_len, (unsigned char) c, len);
  246. }
  247. return s;
  248. }
  249. /* --------------------------------------------------------------------------------------------- */