| 1 |
/* |
| 2 |
* glib-compat.c |
| 3 |
* Functions copied from glib 2.10 |
| 4 |
* |
| 5 |
* Copyright 2005 David Schleef <[email protected]> |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Library General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library 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 GNU |
| 15 |
* Library General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Library General Public |
| 18 |
* License along with this library; if not, write to the |
| 19 |
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 20 |
* Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef __GLIB_COMPAT_PRIVATE_H__ |
| 24 |
#define __GLIB_COMPAT_PRIVATE_H__ |
| 25 |
|
| 26 |
#include <glib.h> |
| 27 |
|
| 28 |
G_BEGIN_DECLS |
| 29 |
|
| 30 |
/* copies */ |
| 31 |
|
| 32 |
#if !GLIB_CHECK_VERSION(2,68,0) |
| 33 |
#ifndef g_string_replace |
| 34 |
#define g_string_replace gst_g_string_replace |
| 35 |
#endif |
| 36 |
|
| 37 |
static inline guint |
| 38 |
gst_g_string_replace (GString *string, |
| 39 |
const gchar *find, |
| 40 |
const gchar *replace, |
| 41 |
guint limit) |
| 42 |
{ |
| 43 |
gsize f_len, r_len, pos; |
| 44 |
gchar *cur, *next; |
| 45 |
guint n = 0; |
| 46 |
|
| 47 |
g_return_val_if_fail (string != NULL, 0); |
| 48 |
g_return_val_if_fail (find != NULL, 0); |
| 49 |
g_return_val_if_fail (replace != NULL, 0); |
| 50 |
|
| 51 |
f_len = strlen (find); |
| 52 |
r_len = strlen (replace); |
| 53 |
cur = string->str; |
| 54 |
|
| 55 |
while ((next = strstr (cur, find)) != NULL) |
| 56 |
{ |
| 57 |
pos = next - string->str; |
| 58 |
g_string_erase (string, pos, f_len); |
| 59 |
g_string_insert (string, pos, replace); |
| 60 |
cur = string->str + pos + r_len; |
| 61 |
n++; |
| 62 |
/* Only match the empty string once at any given position, to |
| 63 |
* avoid infinite loops */ |
| 64 |
if (f_len == 0) |
| 65 |
{ |
| 66 |
if (cur[0] == '\0') |
| 67 |
break; |
| 68 |
else |
| 69 |
cur++; |
| 70 |
} |
| 71 |
if (n == limit) |
| 72 |
break; |
| 73 |
} |
| 74 |
|
| 75 |
return n; |
| 76 |
} |
| 77 |
#endif /* GLIB_CHECK_VERSION */ |
| 78 |
|
| 79 |
/* adaptations */ |
| 80 |
|
| 81 |
#if !GLIB_CHECK_VERSION(2, 81, 1) |
| 82 |
#define g_sort_array(a,n,s,f,udata) gst_g_sort_array(a,n,s,f,udata) |
| 83 |
|
| 84 |
// Don't need to maintain ABI compat here (n_elements), since we never pass |
| 85 |
// the function as pointer but always call it directly ourselves. |
| 86 |
static inline void |
| 87 |
gst_g_sort_array (const void *array, |
| 88 |
gssize n_elements, |
| 89 |
size_t element_size, |
| 90 |
GCompareDataFunc compare_func, |
| 91 |
void *user_data) |
| 92 |
{ |
| 93 |
if (n_elements >= 0 && n_elements <= G_MAXINT) { |
| 94 |
g_qsort_with_data (array, n_elements, element_size, compare_func, user_data); |
| 95 |
} else { |
| 96 |
g_abort (); |
| 97 |
} |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
G_END_DECLS |
| 102 |
|
| 103 |
#endif |