1 /*
2 GLIB - Library of useful routines for C programming
3
4 Copyright (C) 2009-2026
5 Free Software Foundation, Inc.
6
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009, 2013.
9
10 This file is part of the Midnight Commander.
11
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.
16
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.
21
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26 /** \file glibcompat.c
27 * \brief Source: compatibility with older versions of glib
28 *
29 * Following code was copied from glib to GNU Midnight Commander to
30 * provide compatibility with older versions of glib.
31 */
32
33 #include <config.h>
34 #include <string.h>
35
36 #include "global.h"
37 #include "glibcompat.h"
38
39 /*** global variables ****************************************************************************/
40
41 /*** file scope macro definitions ****************************************************************/
42
43 /*** file scope type declarations ****************************************************************/
44
45 /*** file scope variables ************************************************************************/
46
47 /* --------------------------------------------------------------------------------------------- */
48 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
50
51 #if !GLIB_CHECK_VERSION(2, 76, 0)
52 /* Call the GCompareFunc pointed to by @user_data with the elements pointed to by @a and @b. */
53 static gint
54 ptr_array_sort_values_compare (gconstpointer a, gconstpointer b, gpointer user_data)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
55 {
56 const GCompareFunc func = *(const GCompareFunc *) user_data;
57
58 return func (*(const gconstpointer *) a, *(const gconstpointer *) b);
59 }
60 #endif
61
62 /* --------------------------------------------------------------------------------------------- */
63
64 /* --------------------------------------------------------------------------------------------- */
65 /*** public functions ****************************************************************************/
66 /* --------------------------------------------------------------------------------------------- */
67
68 #if !GLIB_CHECK_VERSION(2, 54, 0)
69 /**
70 * g_ptr_array_find_with_equal_func: (skip)
71 * @haystack: pointer array to be searched
72 * @needle: pointer to look for
73 * @equal_func: (nullable): the function to call for each element, which should
74 * return %TRUE when the desired element is found; or %NULL to use pointer
75 * equality
76 * @index_: (optional) (out): return location for the index of
77 * the element, if found
78 *
79 * Checks whether @needle exists in @haystack, using the given @equal_func.
80 * If the element is found, %TRUE is returned and the element^A^A^As index is
81 * returned in @index_ (if non-%NULL). Otherwise, %FALSE is returned and @index_
82 * is undefined. If @needle exists multiple times in @haystack, the index of
83 * the first instance is returned.
84 *
85 * @equal_func is called with the element from the array as its first parameter,
86 * and @needle as its second parameter. If @equal_func is %NULL, pointer
87 * equality is used.
88 *
89 * Returns: %TRUE if @needle is one of the elements of @haystack
90 * Since: 2.54
91 */
92 gboolean
93 g_ptr_array_find_with_equal_func (GPtrArray *haystack, gconstpointer needle, GEqualFunc equal_func,
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
94 guint *index_)
95 {
96 guint i;
97
98 g_return_val_if_fail (haystack != NULL, FALSE);
99
100 if (equal_func == NULL)
101 equal_func = g_direct_equal;
102
103 for (i = 0; i < haystack->len; i++)
104 if (equal_func (g_ptr_array_index (haystack, i), needle))
105 {
106 if (index_ != NULL)
107 *index_ = i;
108 return TRUE;
109 }
110
111 return FALSE;
112 }
113 #endif
114
115 /* --------------------------------------------------------------------------------------------- */
116
117 #if !GLIB_CHECK_VERSION(2, 63, 3)
118 /**
119 * g_clear_slist: (skip)
120 * @slist_ptr: (not nullable): a #GSList return location
121 * @destroy: (nullable): the function to pass to g_slist_free_full() or NULL to not free elements
122 *
123 * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy.
124 *
125 * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing.
126 *
127 * Since: 2.64
128 */
129 void
130 g_clear_slist (GSList **slist_ptr, GDestroyNotify destroy)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
131 {
132 GSList *slist;
133
134 slist = *slist_ptr;
135
136 if (slist != NULL)
137 {
138 *slist_ptr = NULL;
139
140 if (destroy != NULL)
141 g_slist_free_full (slist, destroy);
142 else
143 g_slist_free (slist);
144 }
145 }
146
147 /* --------------------------------------------------------------------------------------------- */
148
149 /**
150 * g_clear_list:
151 * @list_ptr: (not nullable): a #GList return location
152 * @destroy: (nullable): the function to pass to g_list_free_full() or NULL to not free elements
153 *
154 * Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy.
155 *
156 * @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing.
157 *
158 * Since: 2.64
159 */
160 void
161 g_clear_list (GList **list_ptr, GDestroyNotify destroy)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
162 {
163 GList *list;
164
165 list = *list_ptr;
166
167 if (list != NULL)
168 {
169 *list_ptr = NULL;
170
171 if (destroy != NULL)
172 g_list_free_full (list, destroy);
173 else
174 g_list_free (list);
175 }
176 }
177
178 #endif
179
180 /* --------------------------------------------------------------------------------------------- */
181
182 #if !GLIB_CHECK_VERSION(2, 60, 0)
183 /**
184 * g_queue_clear_full:
185 * @queue: a pointer to a #GQueue
186 * @free_func: (nullable): the function to be called to free memory allocated
187 *
188 * Convenience method, which frees all the memory used by a #GQueue,
189 * and calls the provided @free_func on each item in the #GQueue.
190 *
191 * Since: 2.60
192 */
193 void
194 g_queue_clear_full (GQueue *queue, GDestroyNotify free_func)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
195 {
196 g_return_if_fail (queue != NULL);
197
198 if (free_func != NULL)
199 g_queue_foreach (queue, (GFunc) free_func, NULL);
200
201 g_queue_clear (queue);
202 }
203 #endif
204
205 /* --------------------------------------------------------------------------------------------- */
206
207 #if !GLIB_CHECK_VERSION(2, 76, 0)
208 /**
209 * g_ptr_array_sort_values:
210 * @array: a #GPtrArray
211 * @func: comparison function
212 *
213 * Sorts the array like g_ptr_array_sort(), but @func is called with the elements of the array,
214 * not with pointers to them, so e.g. strcmp() can be used to sort an array of strings.
215 *
216 * Since: 2.76
217 */
218 void
219 g_ptr_array_sort_values (GPtrArray *array, GCompareFunc func)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
220 {
221 g_ptr_array_sort_with_data (array, ptr_array_sort_values_compare, &func);
222 }
223 #endif
224
225 /* --------------------------------------------------------------------------------------------- */
226
227 #if !GLIB_CHECK_VERSION(2, 77, 0)
228 /**
229 * g_string_new_take:
230 * @init: (nullable): initial text used as the string.
231 * Ownership of the string is transferred to the #GString.
232 * Passing NULL creates an empty string.
233 *
234 * Creates a new #GString, initialized with the given string.
235 *
236 * After this call, @init belongs to the #GString and may no longer be
237 * modified by the caller. The memory of @data has to be dynamically
238 * allocated and will eventually be freed with g_free().
239 *
240 * Returns: the new #GString
241 */
242 GString *
243 g_string_new_take (char *init)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
244 {
245 GString *string;
246
247 if (init == NULL)
248 return g_string_new (NULL);
249
250 string = g_slice_new (GString);
251
252 string->str = init;
253 string->len = strlen (string->str);
254 string->allocated_len = string->len + 1;
255
256 return string;
257 }
258 #endif
259
260 /* --------------------------------------------------------------------------------------------- */
261
262 /**
263 * mc_g_string_copy:
264 * @dest: (not nullable): the destination #GString. Its current contents are destroyed
265 * @src: (not nullable): the source #GString
266 * @return: @dest
267 *
268 * Copies the bytes from a #GString into a #GString, destroying any previous contents.
269 * It is rather like the standard strcpy() function, except that you do not have to worry about
270 * having enough space to copy the string.
271 *
272 * There is no such API in GLib2.
273 */
274 GString *
275 mc_g_string_copy (GString *dest, const GString *src)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
276 {
277 g_return_val_if_fail (src != NULL, NULL);
278 g_return_val_if_fail (dest != NULL, NULL);
279
280 g_string_set_size (dest, 0);
281 g_string_append_len (dest, src->str, src->len);
282
283 return dest;
284 }
285
286 /* --------------------------------------------------------------------------------------------- */
287
288 /**
289 * mc_g_string_dup:
290 * @s: (nullable): the source #GString
291 * @return: @copy of @s
292 *
293 * Copies the bytes from one #GString to another.
294 *
295 * There is no such API in GLib2.
296 */
297 GString *
298 mc_g_string_dup (const GString *s)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
299 {
300 GString *ret = NULL;
301
302 if (s != NULL)
303 ret = g_string_new_len (s->str, s->len);
304
305 return ret;
306 }
307
308 /* --------------------------------------------------------------------------------------------- */
309
310 /**
311 * mc_g_string_append_c_len:
312 * @s: (not nullable): the destination #GString.
313 * @c: the byte to append onto the end of @s
314 * @len: the number of bytes @c to append onto the end of @s
315 * @return: @s
316 *
317 * Adds @len bytes @c onto the end of @s.
318 *
319 * There is no such API in GLib2.
320 */
321 GString *
322 mc_g_string_append_c_len (GString *s, gchar c, guint len)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
323 {
324 g_return_val_if_fail (s != NULL, NULL);
325
326 if (len != 0)
327 {
328 guint s_len = s->len;
329
330 g_string_set_size (s, s->len + len);
331 memset (s->str + s_len, (unsigned char) c, len);
332 }
333
334 return s;
335 }
336
337 /* --------------------------------------------------------------------------------------------- */
338
339 /**
340 * mc_g_string_concat:
341 * @dst: (not nullable): the destination #GString.
342 * @src: (not nullable): the source #GString.
343 * @return: @dst
344 *
345 * Adds @src to the end of @dst.
346 *
347 * There is no such API in GLib2.
348 */
349 GString *
350 mc_g_string_concat (GString *dest, const GString *src)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
351 {
352 g_return_val_if_fail (dest != NULL, NULL);
353 g_return_val_if_fail (src != NULL, NULL);
354
355 g_string_append_len (dest, src->str, src->len);
356
357 return dest;
358 }
359
360 /* --------------------------------------------------------------------------------------------- */