mceditor: refactoring.
[midnight-commander.git] / lib / glibcompat.c
blob0d58e4934b1e3aa1e4048bad7fe065a4e2ff043c
1 /*
2 GLIB - Library of useful routines for C programming
4 Copyright (C) 2009-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009, 2013.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file glibcompat.c
27 * \brief Source: compatibility with older versions of glib
29 * Following code was copied from glib to GNU Midnight Commander to
30 * provide compatibility with older versions of glib.
33 #include <config.h>
34 #include <string.h>
36 #include "global.h"
37 #include "glibcompat.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
50 /*** public functions ****************************************************************************/
51 /* --------------------------------------------------------------------------------------------- */
53 #if ! GLIB_CHECK_VERSION (2, 63, 3)
54 /**
55 * g_clear_slist: (skip)
56 * @slist_ptr: (not nullable): a #GSList return location
57 * @destroy: (nullable): the function to pass to g_slist_free_full() or NULL to not free elements
59 * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy.
61 * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing.
63 * Since: 2.64
65 void
66 g_clear_slist (GSList ** slist_ptr, GDestroyNotify destroy)
68 GSList *slist;
70 slist = *slist_ptr;
72 if (slist != NULL)
74 *slist_ptr = NULL;
76 if (destroy != NULL)
77 g_slist_free_full (slist, destroy);
78 else
79 g_slist_free (slist);
83 /* --------------------------------------------------------------------------------------------- */
85 /**
86 * g_clear_list:
87 * @list_ptr: (not nullable): a #GList return location
88 * @destroy: (nullable): the function to pass to g_list_free_full() or NULL to not free elements
90 * Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy.
92 * @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing.
94 * Since: 2.64
96 void
97 g_clear_list (GList ** list_ptr, GDestroyNotify destroy)
99 GList *list;
101 list = *list_ptr;
103 if (list != NULL)
105 *list_ptr = NULL;
107 if (destroy != NULL)
108 g_list_free_full (list, destroy);
109 else
110 g_list_free (list);
114 #endif /* ! GLIB_CHECK_VERSION (2, 63, 3) */
116 /* --------------------------------------------------------------------------------------------- */
118 #if ! GLIB_CHECK_VERSION (2, 60, 0)
120 * g_queue_clear_full:
121 * @queue: a pointer to a #GQueue
122 * @free_func: (nullable): the function to be called to free memory allocated
124 * Convenience method, which frees all the memory used by a #GQueue,
125 * and calls the provided @free_func on each item in the #GQueue.
127 * Since: 2.60
129 void
130 g_queue_clear_full (GQueue * queue, GDestroyNotify free_func)
132 g_return_if_fail (queue != NULL);
134 if (free_func != NULL)
135 g_queue_foreach (queue, (GFunc) free_func, NULL);
137 g_queue_clear (queue);
139 #endif /* ! GLIB_CHECK_VERSION (2, 60, 0) */
141 /* --------------------------------------------------------------------------------------------- */
143 #if ! GLIB_CHECK_VERSION (2, 77, 0)
145 * g_string_new_take:
146 * @init: (nullable): initial text used as the string.
147 * Ownership of the string is transferred to the #GString.
148 * Passing NULL creates an empty string.
150 * Creates a new #GString, initialized with the given string.
152 * After this call, @init belongs to the #GString and may no longer be
153 * modified by the caller. The memory of @data has to be dynamically
154 * allocated and will eventually be freed with g_free().
156 * Returns: the new #GString
158 GString *
159 g_string_new_take (char *init)
161 GString *string;
163 if (init == NULL)
164 return g_string_new (NULL);
166 string = g_slice_new (GString);
168 string->str = init;
169 string->len = strlen (string->str);
170 string->allocated_len = string->len + 1;
172 return string;
174 #endif /* ! GLIB_CHECK_VERSION (2, 77, 0) */
176 /* --------------------------------------------------------------------------------------------- */
179 * mc_g_string_copy:
180 * @dest: (not nullable): the destination #GString. Its current contents are destroyed
181 * @src: (not nullable): the source #GString
182 * @return: @dest
184 * Copies the bytes from a #GString into a #GString, destroying any previous contents.
185 * It is rather like the standard strcpy() function, except that you do not have to worry about
186 * having enough space to copy the string.
188 * There is no such API in GLib2.
190 GString *
191 mc_g_string_copy (GString * dest, const GString * src)
193 g_return_val_if_fail (src != NULL, NULL);
194 g_return_val_if_fail (dest != NULL, NULL);
196 g_string_set_size (dest, 0);
197 g_string_append_len (dest, src->str, src->len);
199 return dest;
202 /* --------------------------------------------------------------------------------------------- */
205 * mc_g_string_dup:
206 * @s: (nullable): the source #GString
207 * @return: @copy of @s
209 * Copies the bytes from one #GString to another.
211 * There is no such API in GLib2.
213 GString *
214 mc_g_string_dup (const GString * s)
216 GString *ret = NULL;
218 if (s != NULL)
219 ret = g_string_new_len (s->str, s->len);
221 return ret;
224 /* --------------------------------------------------------------------------------------------- */