glibcompat.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. GLIB - Library of useful routines for C programming
  3. Copyright (C) 2009-2019
  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, 28, 0)
  38. /**
  39. * g_slist_free_full:
  40. * @list: a pointer to a #GSList
  41. * @free_func: the function to be called to free each element's data
  42. *
  43. * Convenience method, which frees all the memory used by a #GSList, and
  44. * calls the specified destroy function on every element's data.
  45. *
  46. * Since: 2.28
  47. **/
  48. void
  49. g_slist_free_full (GSList * list, GDestroyNotify free_func)
  50. {
  51. g_slist_foreach (list, (GFunc) free_func, NULL);
  52. g_slist_free (list);
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. /**
  56. * g_list_free_full:
  57. * @list: a pointer to a #GList
  58. * @free_func: the function to be called to free each element's data
  59. *
  60. * Convenience method, which frees all the memory used by a #GList, and
  61. * calls the specified destroy function on every element's data.
  62. *
  63. * Since: 2.28
  64. */
  65. void
  66. g_list_free_full (GList * list, GDestroyNotify free_func)
  67. {
  68. g_list_foreach (list, (GFunc) free_func, NULL);
  69. g_list_free (list);
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. #endif /* ! GLIB_CHECK_VERSION (2, 28, 0) */
  73. #if ! GLIB_CHECK_VERSION (2, 64, 0)
  74. /**
  75. * g_clear_slist: (skip)
  76. * @slist_ptr: (not nullable): a #GSList return location
  77. * @destroy: (nullable): the function to pass to g_slist_free_full() or NULL to not free elements
  78. *
  79. * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy.
  80. *
  81. * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing.
  82. *
  83. * Since: 2.64
  84. */
  85. void
  86. g_clear_slist (GSList ** slist_ptr, GDestroyNotify destroy)
  87. {
  88. GSList *slist;
  89. slist = *slist_ptr;
  90. if (slist != NULL)
  91. {
  92. *slist_ptr = NULL;
  93. if (destroy != NULL)
  94. g_slist_free_full (slist, destroy);
  95. else
  96. g_slist_free (slist);
  97. }
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. /**
  101. * g_clear_list:
  102. * @list_ptr: (not nullable): a #GList return location
  103. * @destroy: (nullable): the function to pass to g_list_free_full() or NULL to not free elements
  104. *
  105. * Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy.
  106. *
  107. * @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing.
  108. *
  109. * Since: 2.64
  110. */
  111. void
  112. g_clear_list (GList ** list_ptr, GDestroyNotify destroy)
  113. {
  114. GList *list;
  115. list = *list_ptr;
  116. if (list != NULL)
  117. {
  118. *list_ptr = NULL;
  119. if (destroy != NULL)
  120. g_list_free_full (list, destroy);
  121. else
  122. g_list_free (list);
  123. }
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. #endif /* ! GLIB_CHECK_VERSION (2, 64, 0) */
  127. #if ! GLIB_CHECK_VERSION (2, 32, 0)
  128. /**
  129. * g_queue_free_full:
  130. * @queue: a pointer to a #GQueue
  131. * @free_func: the function to be called to free each element's data
  132. *
  133. * Convenience method, which frees all the memory used by a #GQueue,
  134. * and calls the specified destroy function on every element's data.
  135. *
  136. * Since: 2.32
  137. */
  138. void
  139. g_queue_free_full (GQueue * queue, GDestroyNotify free_func)
  140. {
  141. g_queue_foreach (queue, (GFunc) free_func, NULL);
  142. g_queue_free (queue);
  143. }
  144. #endif /* ! GLIB_CHECK_VERSION (2, 32, 0) */
  145. /* --------------------------------------------------------------------------------------------- */
  146. #if ! GLIB_CHECK_VERSION (2, 60, 0)
  147. /**
  148. * g_queue_clear_full:
  149. * @queue: a pointer to a #GQueue
  150. * @free_func: (nullable): the function to be called to free memory allocated
  151. *
  152. * Convenience method, which frees all the memory used by a #GQueue,
  153. * and calls the provided @free_func on each item in the #GQueue.
  154. *
  155. * Since: 2.60
  156. */
  157. void
  158. g_queue_clear_full (GQueue * queue, GDestroyNotify free_func)
  159. {
  160. g_return_if_fail (queue != NULL);
  161. if (free_func != NULL)
  162. g_queue_foreach (queue, (GFunc) free_func, NULL);
  163. g_queue_clear (queue);
  164. }
  165. #endif /* ! GLIB_CHECK_VERSION (2, 60, 0) */
  166. /* --------------------------------------------------------------------------------------------- */