tracker-0.16.2/src/libtracker-common/tracker-type-utils.c

No issues found

  1 /*
  2  * Copyright (C) 2006, Jamie McCracken <jamiemcc@gnome.org>
  3  * Copyright (C) 2008, Nokia <ivan.frade@nokia.com>
  4  *
  5  * This library is free software; you can redistribute it and/or
  6  * modify it under the terms of the GNU Lesser General Public
  7  * License as published by the Free Software Foundation; either
  8  * version 2.1 of the License, or (at your option) any later version.
  9  *
 10  * This library is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  * Lesser General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU Lesser General Public
 16  * License along with this library; if not, write to the
 17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 18  * Boston, MA  02110-1301, USA.
 19  */
 20 
 21 #include "config.h"
 22 
 23 #define _XOPEN_SOURCE
 24 #define _XOPEN_SOURCE_EXTENDED 1	/* strcasecmp is XPG4v2 */
 25 #include <time.h>
 26 
 27 #include <strings.h>
 28 #include <string.h>
 29 #include <stdlib.h>
 30 
 31 #include <glib.h>
 32 
 33 #include "tracker-log.h"
 34 #include "tracker-utils.h"
 35 #include "tracker-type-utils.h"
 36 
 37 gchar *
 38 tracker_glong_to_string (glong i)
 39 {
 40 	return g_strdup_printf ("%ld", i);
 41 }
 42 
 43 gchar *
 44 tracker_gint_to_string (gint i)
 45 {
 46 	return g_strdup_printf ("%d", i);
 47 }
 48 
 49 gchar *
 50 tracker_guint_to_string (guint i)
 51 {
 52 	return g_strdup_printf ("%u", i);
 53 }
 54 
 55 gchar *
 56 tracker_gint32_to_string (gint32 i)
 57 {
 58 	return g_strdup_printf ("%" G_GINT32_FORMAT, i);
 59 }
 60 
 61 gchar *
 62 tracker_guint32_to_string (guint32 i)
 63 {
 64 	return g_strdup_printf ("%" G_GUINT32_FORMAT, i);
 65 }
 66 
 67 gboolean
 68 tracker_string_to_uint (const gchar *s,
 69                         guint       *value)
 70 {
 71 	unsigned long int  n;
 72 	gchar             *end;
 73 
 74 	g_return_val_if_fail (s != NULL, FALSE);
 75 	g_return_val_if_fail (value != NULL, FALSE);
 76 
 77 	n = (guint) strtoul (s, &end, 10);
 78 
 79 	if (end == s) {
 80 		*value = 0;
 81 		return FALSE;
 82 	}
 83 
 84 	if (n > G_MAXUINT) {
 85 		*value = 0;
 86 		return FALSE;
 87 
 88 	} else {
 89 		*value = (guint) n;
 90 		return TRUE;
 91 	}
 92 }
 93 
 94 gint
 95 tracker_string_in_string_list (const gchar  *str,
 96                                gchar       **strv)
 97 {
 98 	gchar **p;
 99 	gint    i;
100 
101 	g_return_val_if_fail (str != NULL, -1);
102 
103 	if (!strv) {
104 		return -1;
105 	}
106 
107 	for (p = strv, i = 0; *p; p++, i++) {
108 		if (strcasecmp (*p, str) == 0) {
109 			return i;
110 		}
111 	}
112 
113 	return -1;
114 }
115 
116 gboolean
117 tracker_string_in_gslist (const gchar *str,
118                           GSList      *list)
119 {
120 	GSList *l;
121 
122 	g_return_val_if_fail (str != NULL, FALSE);
123 
124 	for (l = list; l; l = l->next) {
125 		if (g_strcmp0 (l->data, str) == 0) {
126 			return TRUE;
127 		}
128 	}
129 
130 	return FALSE;
131 }
132 
133 GSList *
134 tracker_string_list_to_gslist (gchar **strv,
135                                gsize   size)
136 {
137 	GSList *list;
138 	gsize   i;
139 	gsize   size_used;
140 
141 	g_return_val_if_fail (strv != NULL, NULL);
142 
143 	if (size < 1) {
144 		size_used = g_strv_length (strv);
145 	} else {
146 		size_used = size;
147 	}
148 
149 	list = NULL;
150 
151 	for (i = 0; i < size_used; i++) {
152 		if (strv[i]) {
153 			list = g_slist_prepend (list, g_strdup (strv[i]));
154 		} else {
155 			break;
156 		}
157 	}
158 
159 	return g_slist_reverse (list);
160 }
161 
162 gchar *
163 tracker_string_list_to_string (gchar **strv,
164                                gsize   size,
165                                gchar   sep)
166 {
167 	GString *string;
168 	gsize    i;
169 	gsize    size_used;
170 
171 	g_return_val_if_fail (strv != NULL, NULL);
172 
173 	if (size < 1) {
174 		size_used = g_strv_length (strv);
175 	} else {
176 		size_used = size;
177 	}
178 
179 	string = g_string_new ("");
180 
181 	for (i = 0; i < size_used; i++) {
182 		if (strv[i]) {
183 			if (i > 0) {
184 				g_string_append_c (string, sep);
185 			}
186 
187 			string = g_string_append (string, strv[i]);
188 		} else {
189 			break;
190 		}
191 	}
192 
193 	return g_string_free (string, FALSE);
194 }
195 
196 gchar **
197 tracker_string_to_string_list (const gchar *str)
198 {
199 	gchar **result;
200 
201 	result = g_new0 (gchar *, 2);
202 
203 	result [0] = g_strdup (str);
204 	result [1] = NULL;
205 
206 	return result;
207 }
208 
209 gchar **
210 tracker_gslist_to_string_list (GSList *list)
211 {
212 	GSList  *l;
213 	gchar  **strv;
214 	gint     i;
215 
216 	strv = g_new0 (gchar*, g_slist_length (list) + 1);
217 
218 	for (l = list, i = 0; l; l = l->next) {
219 		if (!l->data) {
220 			continue;
221 		}
222 
223 		strv[i++] = g_strdup (l->data);
224 	}
225 
226 	strv[i] = NULL;
227 
228 	return strv;
229 }
230 
231 gboolean
232 tracker_gslist_with_string_data_equal (GSList *list1,
233                                        GSList *list2)
234 {
235         GSList *sl;
236 
237         if (list1 == list2) {
238                 return TRUE;
239         }
240 
241         if (g_slist_length (list1) != g_slist_length (list2)) {
242                 return FALSE;
243         }
244 
245         /* NOTE: This is probably not the most efficient way to do
246          * this, but we don't want to order the list first since that
247          * would involve creating new memory. This would make sense
248          * for large list operations I think. We don't expect to be
249          * doing much if any of that.
250          */
251         for (sl = list1; sl; sl = sl->next) {
252                 const gchar *str;
253 
254                 str = sl->data;
255 
256                 /* If we are not still in the list, remove the dir */
257                 if (!tracker_string_in_gslist (str, list2)) {
258                         return FALSE;
259                 }
260         }
261 
262         for (sl = list2; sl; sl = sl->next) {
263                 const gchar *str;
264 
265                 str = sl->data;
266 
267                 /* If we are now in the list, add the dir */
268                 if (!tracker_string_in_gslist (str, list1)) {
269                         return FALSE;
270                 }
271         }
272 
273         return TRUE;
274 }
275 
276 GSList *
277 tracker_gslist_copy_with_string_data (GSList *list)
278 {
279 	GSList *l;
280 	GSList *new_list;
281 
282 	if (!list) {
283 		return NULL;
284 	}
285 
286 	new_list = NULL;
287 
288 	for (l = list; l; l = l->next) {
289 		new_list = g_slist_prepend (new_list, g_strdup (l->data));
290 	}
291 
292 	new_list = g_slist_reverse (new_list);
293 
294 	return new_list;
295 }
296 
297 GList *
298 tracker_glist_copy_with_string_data (GList *list)
299 {
300 	GList *l;
301 	GList *new_list;
302 
303 	if (!list) {
304 		return NULL;
305 	}
306 
307 	new_list = NULL;
308 
309 	for (l = list; l; l = l->next) {
310 		new_list = g_list_prepend (new_list, g_strdup (l->data));
311 	}
312 
313 	new_list = g_list_reverse (new_list);
314 
315 	return new_list;
316 }
317 
318 gchar *
319 tracker_string_boolean_to_string_gint (const gchar *value)
320 {
321 	g_return_val_if_fail (value != NULL, NULL);
322 
323 	if (g_ascii_strcasecmp (value, "true") == 0) {
324 		return g_strdup ("1");
325 	} else if (g_ascii_strcasecmp (value, "false") == 0) {
326 		return g_strdup ("0");
327 	} else {
328 		return g_strdup (value);
329 	}
330 }