glibcompat.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. GLIB - Library of useful routines for C programming
  3. Copyright (C) 2009-2021
  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. /*** file scope functions ************************************************************************/
  34. /* --------------------------------------------------------------------------------------------- */
  35. /*** public functions ****************************************************************************/
  36. /* --------------------------------------------------------------------------------------------- */
  37. #if ! GLIB_CHECK_VERSION (2, 63, 3)
  38. /**
  39. * g_clear_slist: (skip)
  40. * @slist_ptr: (not nullable): a #GSList return location
  41. * @destroy: (nullable): the function to pass to g_slist_free_full() or NULL to not free elements
  42. *
  43. * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy.
  44. *
  45. * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing.
  46. *
  47. * Since: 2.64
  48. */
  49. void
  50. g_clear_slist (GSList ** slist_ptr, GDestroyNotify destroy)
  51. {
  52. GSList *slist;
  53. slist = *slist_ptr;
  54. if (slist != NULL)
  55. {
  56. *slist_ptr = NULL;
  57. if (destroy != NULL)
  58. g_slist_free_full (slist, destroy);
  59. else
  60. g_slist_free (slist);
  61. }
  62. }
  63. /* --------------------------------------------------------------------------------------------- */
  64. /**
  65. * g_clear_list:
  66. * @list_ptr: (not nullable): a #GList return location
  67. * @destroy: (nullable): the function to pass to g_list_free_full() or NULL to not free elements
  68. *
  69. * Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy.
  70. *
  71. * @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing.
  72. *
  73. * Since: 2.64
  74. */
  75. void
  76. g_clear_list (GList ** list_ptr, GDestroyNotify destroy)
  77. {
  78. GList *list;
  79. list = *list_ptr;
  80. if (list != NULL)
  81. {
  82. *list_ptr = NULL;
  83. if (destroy != NULL)
  84. g_list_free_full (list, destroy);
  85. else
  86. g_list_free (list);
  87. }
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. #endif /* ! GLIB_CHECK_VERSION (2, 63, 3) */
  91. #if ! GLIB_CHECK_VERSION (2, 32, 0)
  92. /**
  93. * g_queue_free_full:
  94. * @queue: a pointer to a #GQueue
  95. * @free_func: the function to be called to free each element's data
  96. *
  97. * Convenience method, which frees all the memory used by a #GQueue,
  98. * and calls the specified destroy function on every element's data.
  99. *
  100. * Since: 2.32
  101. */
  102. void
  103. g_queue_free_full (GQueue * queue, GDestroyNotify free_func)
  104. {
  105. g_queue_foreach (queue, (GFunc) free_func, NULL);
  106. g_queue_free (queue);
  107. }
  108. #endif /* ! GLIB_CHECK_VERSION (2, 32, 0) */
  109. /* --------------------------------------------------------------------------------------------- */
  110. #if ! GLIB_CHECK_VERSION (2, 60, 0)
  111. /**
  112. * g_queue_clear_full:
  113. * @queue: a pointer to a #GQueue
  114. * @free_func: (nullable): the function to be called to free memory allocated
  115. *
  116. * Convenience method, which frees all the memory used by a #GQueue,
  117. * and calls the provided @free_func on each item in the #GQueue.
  118. *
  119. * Since: 2.60
  120. */
  121. void
  122. g_queue_clear_full (GQueue * queue, GDestroyNotify free_func)
  123. {
  124. g_return_if_fail (queue != NULL);
  125. if (free_func != NULL)
  126. g_queue_foreach (queue, (GFunc) free_func, NULL);
  127. g_queue_clear (queue);
  128. }
  129. #endif /* ! GLIB_CHECK_VERSION (2, 60, 0) */
  130. /* --------------------------------------------------------------------------------------------- */
  131. /**
  132. * mc_g_string_copy:
  133. * @dest: (not nullable): the destination #GString. Its current contents are destroyed
  134. * @src: (not nullable): the source #GString
  135. * @return: @dest
  136. *
  137. * Copies the bytes from a #GString into a #GString, destroying any previous contents.
  138. * It is rather like the standard strcpy() function, except that you do not have to worry about
  139. * having enough space to copy the string.
  140. *
  141. * There is no such API in GLib2.
  142. */
  143. GString *
  144. mc_g_string_copy (GString * dest, const GString * src)
  145. {
  146. g_return_val_if_fail (src != NULL, NULL);
  147. g_return_val_if_fail (dest != NULL, NULL);
  148. g_string_set_size (dest, 0);
  149. g_string_append_len (dest, src->str, src->len);
  150. return dest;
  151. }
  152. /* --------------------------------------------------------------------------------------------- */
  153. /**
  154. * mc_g_string_dup:
  155. * @s: (nullable): the source #GString
  156. * @return: @copy of @s
  157. *
  158. * Copies the bytes from one #GString to another.
  159. *
  160. * There is no such API in GLib2.
  161. */
  162. GString *
  163. mc_g_string_dup (const GString * s)
  164. {
  165. GString *ret = NULL;
  166. if (s != NULL)
  167. ret = g_string_new_len (s->str, s->len);
  168. return ret;
  169. }
  170. /* --------------------------------------------------------------------------------------------- */