1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program 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.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
* @short_description: Common utility functions for PackageKit
26
* This file contains functions that may be useful.
35
#include <sys/types.h>
36
#include <sys/utsname.h>
41
#endif /* HAVE_UNISTD_H */
45
#include "egg-debug.h"
46
#include "egg-string.h"
50
* @text: The text the convert
51
* @value: The return numeric return value
53
* Converts a string into a signed integer value in a safe way.
55
* Return value: %TRUE if the string was converted correctly
58
egg_strtoint (const gchar *text, gint *value)
68
value_raw = g_ascii_strtoll (text, &endptr, 10);
75
if (value_raw > G_MAXINT || value_raw < G_MININT)
78
/* cast back down to value */
79
*value = (gint) value_raw;
85
* @text: The text the convert
86
* @value: The return numeric return value
88
* Converts a string into a unsigned integer value in a safe way.
90
* Return value: %TRUE if the string was converted correctly
93
egg_strtouint (const gchar *text, guint *value)
103
value_raw = g_ascii_strtoull (text, &endptr, 10);
110
if (value_raw > G_MAXINT)
113
/* cast back down to value */
114
*value = (guint) value_raw;
120
* @text: The text to check
122
* This function is a much safer way of doing "if (strlen (text) == 0))"
123
* as it does not rely on text being NULL terminated. It's also much
124
* quicker as it only checks the first byte rather than scanning the whole
125
* string just to verify it's not zero length.
127
* Return value: %TRUE if the string was converted correctly
130
egg_strzero (const gchar *text)
141
* @text: The text to check
142
* @len: The maximum length of the string
144
* This function is a much safer way of doing strlen as it checks for NULL and
145
* a stupidly long string.
147
* Return value: the length of the string, or len if the string is too long.
150
egg_strlen (const gchar *text, guint len)
155
if (text == NULL || text[0] == '\0')
158
/* only count up to len */
159
for (i=1; i<len; i++) {
168
* @id1: the first item of text to test
169
* @id2: the second item of text to test
171
* This function will check to see if the GStrv arrays are string equal
173
* Return value: %TRUE if the arrays are the same, or are both %NULL
176
egg_strvequal (gchar **id1, gchar **id2)
182
if (id1 == NULL && id2 == NULL)
185
if (id1 == NULL || id2 == NULL) {
186
egg_debug ("GStrv compare invalid '%p' and '%p'", id1, id2);
190
/* check different sizes */
191
length1 = g_strv_length (id1);
192
length2 = g_strv_length (id2);
193
if (length1 != length2)
196
/* text equal each one */
197
for (i=0; i<length1; i++) {
198
if (g_strcmp0 (id1[i], id2[i]) != 0)
207
* @text: The input text to make safe
208
* @find: What to search for
209
* @replace: What to replace with
211
* Replaces chars in the text with a replacement.
212
* The %find and %replace variables to not have to be of the same length
214
* Return value: the new string (copied)
217
egg_strreplace (const gchar *text, const gchar *find, const gchar *replace)
222
/* common case, not found */
223
if (strstr (text, find) == NULL) {
224
return g_strdup (text);
227
/* split apart and rejoin with new delimiter */
228
array = g_strsplit (text, find, 0);
229
retval = g_strjoinv (replace, array);
234
/***************************************************************************
235
*** MAKE CHECK TESTS ***
236
***************************************************************************/
238
#include "egg-test.h"
241
egg_string_test (EggTest *test)
252
if (!egg_test_start (test, "EggString"))
255
/************************************************************
256
**************** String array equal ******************
257
************************************************************/
258
egg_test_title (test, "egg_strvequal same argument");
259
id1 = g_strsplit ("the quick brown fox", " ", 0);
260
if (egg_strvequal (id1, id1))
261
egg_test_success (test, NULL);
263
egg_test_failed (test, "incorrect ret when both same");
266
/************************************************************/
267
egg_test_title (test, "egg_strvequal same");
268
id1 = g_strsplit ("the quick brown fox", " ", 0);
269
id2 = g_strsplit ("the quick brown fox", " ", 0);
270
if (egg_strvequal (id1, id2))
271
egg_test_success (test, NULL);
273
egg_test_failed (test, "incorrect ret when both same");
277
/************************************************************/
278
egg_test_title (test, "egg_strvequal different lengths");
279
id1 = g_strsplit ("the quick brown", " ", 0);
280
id2 = g_strsplit ("the quick brown fox", " ", 0);
281
if (!egg_strvequal (id1, id2))
282
egg_test_success (test, NULL);
284
egg_test_failed (test, "incorrect ret when both same");
288
/************************************************************/
289
egg_test_title (test, "egg_strvequal different");
290
id1 = g_strsplit ("the quick brown fox", " ", 0);
291
id2 = g_strsplit ("richard hughes maintainer dude", " ", 0);
292
if (!egg_strvequal (id1, id2))
293
egg_test_success (test, NULL);
295
egg_test_failed (test, "same when different");
299
/************************************************************
300
**************** Zero ******************
301
************************************************************/
303
egg_test_title (test, "test strzero (null)");
304
ret = egg_strzero (NULL);
306
egg_test_success (test, NULL);
308
egg_test_failed (test, "failed null");
310
/************************************************************/
311
egg_test_title (test, "test strzero (null first char)");
312
ret = egg_strzero ("");
314
egg_test_success (test, NULL);
316
egg_test_failed (test, "failed null");
318
/************************************************************/
319
egg_test_title (test, "test strzero (long string)");
320
ret = egg_strzero ("Richard");
322
egg_test_success (test, NULL);
324
egg_test_failed (test, "zero length word!");
326
/************************************************************/
327
egg_test_title (test, "id strcmp pass");
328
ret = (g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.1;i386;fedora") == 0);
329
egg_test_assert (test, ret);
331
/************************************************************/
332
egg_test_title (test, "id strcmp fail");
333
ret = (g_strcmp0 ("moo;0.0.1;i386;fedora", "moo;0.0.2;i386;fedora") == 0);
334
egg_test_assert (test, !ret);
336
/************************************************************
337
**************** strlen ******************
338
************************************************************/
339
egg_test_title (test, "strlen bigger");
340
length = egg_strlen ("123456789", 20);
342
egg_test_success (test, NULL);
344
egg_test_failed (test, "failed the strlen %i", length);
346
/************************************************************/
347
egg_test_title (test, "strlen smaller");
348
length = egg_strlen ("123456789", 5);
350
egg_test_success (test, NULL);
352
egg_test_failed (test, "failed the strlen %i", length);
354
/************************************************************/
355
egg_test_title (test, "strlen correct");
356
length = egg_strlen ("123456789", 9);
358
egg_test_success (test, NULL);
360
egg_test_failed (test, "failed the strlen %i", length);
362
/************************************************************
363
**************** Replace ******************
364
************************************************************/
365
egg_test_title (test, "replace start");
366
text_safe = egg_strreplace ("richard\nhughes", "r", "e");
367
if (g_strcmp0 (text_safe, "eichaed\nhughes") == 0)
368
egg_test_success (test, NULL);
370
egg_test_failed (test, "failed the replace '%s'", text_safe);
373
/************************************************************/
374
egg_test_title (test, "replace none");
375
text_safe = egg_strreplace ("richard\nhughes", "dave", "e");
376
if (g_strcmp0 (text_safe, "richard\nhughes") == 0)
377
egg_test_success (test, NULL);
379
egg_test_failed (test, "failed the replace '%s'", text_safe);
382
/************************************************************/
383
egg_test_title (test, "replace end");
384
text_safe = egg_strreplace ("richard\nhughes", "s", "e");
385
if (g_strcmp0 (text_safe, "richard\nhughee") == 0)
386
egg_test_success (test, NULL);
388
egg_test_failed (test, "failed the replace '%s'", text_safe);
391
/************************************************************/
392
egg_test_title (test, "replace unicode");
393
text_safe = egg_strreplace ("richard\n- hughes", "\n- ", "\n• ");
394
if (g_strcmp0 (text_safe, "richard\n• hughes") == 0)
395
egg_test_success (test, NULL);
397
egg_test_failed (test, "failed the replace '%s'", text_safe);
400
/************************************************************
401
************** Convert numbers ****************
402
************************************************************/
403
egg_test_title (test, "convert valid number");
404
ret = egg_strtoint ("234", &value);
405
if (ret && value == 234)
406
egg_test_success (test, NULL);
408
egg_test_failed (test, "value is %i", value);
410
/************************************************************/
411
egg_test_title (test, "convert negative valid number");
412
ret = egg_strtoint ("-234", &value);
413
if (ret && value == -234)
414
egg_test_success (test, NULL);
416
egg_test_failed (test, "value is %i", value);
418
/************************************************************/
419
egg_test_title (test, "don't convert invalid number");
420
ret = egg_strtoint ("dave", &value);
422
egg_test_success (test, NULL);
424
egg_test_failed (test, "value is %i", value);
426
/************************************************************/
427
egg_test_title (test, "convert NULL to a number");
428
ret = egg_strtouint (NULL, &uvalue);
430
egg_test_success (test, NULL);
432
egg_test_failed (test, "value is %i", uvalue);
434
/************************************************************/
435
egg_test_title (test, "convert valid uint number");
436
ret = egg_strtouint ("234", &uvalue);
437
if (ret && uvalue == 234)
438
egg_test_success (test, NULL);
440
egg_test_failed (test, "value is %i", uvalue);
442
/************************************************************/
443
egg_test_title (test, "convert invalid uint number");
444
ret = egg_strtouint ("-234", &uvalue);
446
egg_test_success (test, NULL);
448
egg_test_failed (test, "value is %i", uvalue);