~ubuntu-branches/ubuntu/wily/mate-power-manager/wily

« back to all changes in this revision

Viewing changes to src/egg-string.c

  • Committer: Package Import Robot
  • Author(s): Mike Gabriel, Martin Wimpress, Mike Gabriel
  • Date: 2015-09-02 05:08:49 UTC
  • mfrom: (7.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20150902050849-4krrri9he6z6tiby
Tags: 1.10.2-1
[ Martin Wimpress ]
* New upstream release.
* debian/mate-power-manager-common.install:
  + Add usr/share/{help,man}.
* debian/mate-power-manager.install:
  + Remove usr/share/man.
* debian/rules:
  + Correct typo.
  + Add --enable-deprecated and --disable-strict build options.
  + Remove dfsg suffix.
* debian/watch:
  + Remove dfsg suffix.
* debian/copyright:
  + Add copyright attribution for help authors and translators.
  + Remove patches/gpm-manager.patch, no longer upstream.
* debian/patches:
  + Drop 2001_omit-gfdl-licensed-help-files.patch, the mate-power-manager
    help is DFSG compliant.

[ Mike Gabriel ]
* Upload to unstable.
  - This new upstream release fixes keyboard backlight adjustment for
    laptops with non-standard levels. (This hopefully closes: #775500,
    please reopen, if not).
* lintian:
  + Move overrides in mate-power-manager.lintian-overrides over to
    bin:package mate-power-manager-common (man page have been moved
    over into the latter).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com>
4
 
 *
5
 
 * Licensed under the GNU General Public License Version 2
6
 
 *
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.
11
 
 *
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.
16
 
 *
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.
20
 
 */
21
 
 
22
 
/**
23
 
 * SECTION:pk-common
24
 
 * @short_description: Common utility functions for PackageKit
25
 
 *
26
 
 * This file contains functions that may be useful.
27
 
 */
28
 
 
29
 
#include "config.h"
30
 
 
31
 
#include <stdlib.h>
32
 
#include <stdio.h>
33
 
 
34
 
#include <string.h>
35
 
#include <sys/types.h>
36
 
#include <sys/utsname.h>
37
 
#include <sys/stat.h>
38
 
 
39
 
#ifdef HAVE_UNISTD_H
40
 
#include <unistd.h>
41
 
#endif /* HAVE_UNISTD_H */
42
 
 
43
 
#include <glib.h>
44
 
 
45
 
#include "egg-debug.h"
46
 
#include "egg-string.h"
47
 
 
48
 
/**
49
 
 * egg_strtoint:
50
 
 * @text: The text the convert
51
 
 * @value: The return numeric return value
52
 
 *
53
 
 * Converts a string into a signed integer value in a safe way.
54
 
 *
55
 
 * Return value: %TRUE if the string was converted correctly
56
 
 **/
57
 
gboolean
58
 
egg_strtoint (const gchar *text, gint *value)
59
 
{
60
 
        gchar *endptr = NULL;
61
 
        gint64 value_raw;
62
 
 
63
 
        /* invalid */
64
 
        if (text == NULL)
65
 
                return FALSE;
66
 
 
67
 
        /* parse */
68
 
        value_raw = g_ascii_strtoll (text, &endptr, 10);
69
 
 
70
 
        /* parsing error */
71
 
        if (endptr == text)
72
 
                return FALSE;
73
 
 
74
 
        /* out of range */
75
 
        if (value_raw > G_MAXINT || value_raw < G_MININT)
76
 
                return FALSE;
77
 
 
78
 
        /* cast back down to value */
79
 
        *value = (gint) value_raw;
80
 
        return TRUE;
81
 
}
82
 
 
83
 
/**
84
 
 * egg_strtouint:
85
 
 * @text: The text the convert
86
 
 * @value: The return numeric return value
87
 
 *
88
 
 * Converts a string into a unsigned integer value in a safe way.
89
 
 *
90
 
 * Return value: %TRUE if the string was converted correctly
91
 
 **/
92
 
gboolean
93
 
egg_strtouint (const gchar *text, guint *value)
94
 
{
95
 
        gchar *endptr = NULL;
96
 
        guint64 value_raw;
97
 
 
98
 
        /* invalid */
99
 
        if (text == NULL)
100
 
                return FALSE;
101
 
 
102
 
        /* parse */
103
 
        value_raw = g_ascii_strtoull (text, &endptr, 10);
104
 
 
105
 
        /* parsing error */
106
 
        if (endptr == text)
107
 
                return FALSE;
108
 
 
109
 
        /* out of range */
110
 
        if (value_raw > G_MAXINT)
111
 
                return FALSE;
112
 
 
113
 
        /* cast back down to value */
114
 
        *value = (guint) value_raw;
115
 
        return TRUE;
116
 
}
117
 
 
118
 
/**
119
 
 * egg_strzero:
120
 
 * @text: The text to check
121
 
 *
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.
126
 
 *
127
 
 * Return value: %TRUE if the string was converted correctly
128
 
 **/
129
 
gboolean
130
 
egg_strzero (const gchar *text)
131
 
{
132
 
        if (text == NULL)
133
 
                return TRUE;
134
 
        if (text[0] == '\0')
135
 
                return TRUE;
136
 
        return FALSE;
137
 
}
138
 
 
139
 
/**
140
 
 * egg_strlen:
141
 
 * @text: The text to check
142
 
 * @len: The maximum length of the string
143
 
 *
144
 
 * This function is a much safer way of doing strlen as it checks for NULL and
145
 
 * a stupidly long string.
146
 
 *
147
 
 * Return value: the length of the string, or len if the string is too long.
148
 
 **/
149
 
guint
150
 
egg_strlen (const gchar *text, guint len)
151
 
{
152
 
        guint i;
153
 
 
154
 
        /* common case */
155
 
        if (text == NULL || text[0] == '\0')
156
 
                return 0;
157
 
 
158
 
        /* only count up to len */
159
 
        for (i=1; i<len; i++) {
160
 
                if (text[i] == '\0')
161
 
                        break;
162
 
        }
163
 
        return i;
164
 
}
165
 
 
166
 
/**
167
 
 * egg_strvequal:
168
 
 * @id1: the first item of text to test
169
 
 * @id2: the second item of text to test
170
 
 *
171
 
 * This function will check to see if the GStrv arrays are string equal
172
 
 *
173
 
 * Return value: %TRUE if the arrays are the same, or are both %NULL
174
 
 **/
175
 
gboolean
176
 
egg_strvequal (gchar **id1, gchar **id2)
177
 
{
178
 
        guint i;
179
 
        guint length1;
180
 
        guint length2;
181
 
 
182
 
        if (id1 == NULL && id2 == NULL)
183
 
                return TRUE;
184
 
 
185
 
        if (id1 == NULL || id2 == NULL) {
186
 
                egg_debug ("GStrv compare invalid '%p' and '%p'", id1, id2);
187
 
                return FALSE;
188
 
        }
189
 
 
190
 
        /* check different sizes */
191
 
        length1 = g_strv_length (id1);
192
 
        length2 = g_strv_length (id2);
193
 
        if (length1 != length2)
194
 
                return FALSE;
195
 
 
196
 
        /* text equal each one */
197
 
        for (i=0; i<length1; i++) {
198
 
                if (g_strcmp0 (id1[i], id2[i]) != 0)
199
 
                        return FALSE;
200
 
        }
201
 
 
202
 
        return TRUE;
203
 
}
204
 
 
205
 
/**
206
 
 * egg_strreplace:
207
 
 * @text: The input text to make safe
208
 
 * @find: What to search for
209
 
 * @replace: What to replace with
210
 
 *
211
 
 * Replaces chars in the text with a replacement.
212
 
 * The %find and %replace variables to not have to be of the same length
213
 
 *
214
 
 * Return value: the new string (copied)
215
 
 **/
216
 
gchar *
217
 
egg_strreplace (const gchar *text, const gchar *find, const gchar *replace)
218
 
{
219
 
        gchar **array;
220
 
        gchar *retval;
221
 
 
222
 
        /* common case, not found */
223
 
        if (strstr (text, find) == NULL) {
224
 
                return g_strdup (text);
225
 
        }
226
 
 
227
 
        /* split apart and rejoin with new delimiter */
228
 
        array = g_strsplit (text, find, 0);
229
 
        retval = g_strjoinv (replace, array);
230
 
        g_strfreev (array);
231
 
        return retval;
232
 
}
233
 
 
234
 
/***************************************************************************
235
 
 ***                          MAKE CHECK TESTS                           ***
236
 
 ***************************************************************************/
237
 
#ifdef EGG_TEST
238
 
#include "egg-test.h"
239
 
 
240
 
void
241
 
egg_string_test (EggTest *test)
242
 
{
243
 
        gboolean ret;
244
 
        gchar *text_safe;
245
 
        const gchar *temp;
246
 
        guint length;
247
 
        gint value;
248
 
        guint uvalue;
249
 
        gchar **id1;
250
 
        gchar **id2;
251
 
 
252
 
        if (!egg_test_start (test, "EggString"))
253
 
                return;
254
 
 
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);
262
 
        else
263
 
                egg_test_failed (test, "incorrect ret when both same");
264
 
        g_strfreev (id1);
265
 
 
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);
272
 
        else
273
 
                egg_test_failed (test, "incorrect ret when both same");
274
 
        g_strfreev (id1);
275
 
        g_strfreev (id2);
276
 
 
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);
283
 
        else
284
 
                egg_test_failed (test, "incorrect ret when both same");
285
 
        g_strfreev (id1);
286
 
        g_strfreev (id2);
287
 
 
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);
294
 
        else
295
 
                egg_test_failed (test, "same when different");
296
 
        g_strfreev (id1);
297
 
        g_strfreev (id2);
298
 
 
299
 
        /************************************************************
300
 
         ****************          Zero            ******************
301
 
         ************************************************************/
302
 
        temp = NULL;
303
 
        egg_test_title (test, "test strzero (null)");
304
 
        ret = egg_strzero (NULL);
305
 
        if (ret)
306
 
                egg_test_success (test, NULL);
307
 
        else
308
 
                egg_test_failed (test, "failed null");
309
 
 
310
 
        /************************************************************/
311
 
        egg_test_title (test, "test strzero (null first char)");
312
 
        ret = egg_strzero ("");
313
 
        if (ret)
314
 
                egg_test_success (test, NULL);
315
 
        else
316
 
                egg_test_failed (test, "failed null");
317
 
 
318
 
        /************************************************************/
319
 
        egg_test_title (test, "test strzero (long string)");
320
 
        ret = egg_strzero ("Richard");
321
 
        if (!ret)
322
 
                egg_test_success (test, NULL);
323
 
        else
324
 
                egg_test_failed (test, "zero length word!");
325
 
 
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);
330
 
 
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);
335
 
 
336
 
        /************************************************************
337
 
         ****************          strlen          ******************
338
 
         ************************************************************/
339
 
        egg_test_title (test, "strlen bigger");
340
 
        length = egg_strlen ("123456789", 20);
341
 
        if (length == 9)
342
 
                egg_test_success (test, NULL);
343
 
        else
344
 
                egg_test_failed (test, "failed the strlen %i", length);
345
 
 
346
 
        /************************************************************/
347
 
        egg_test_title (test, "strlen smaller");
348
 
        length = egg_strlen ("123456789", 5);
349
 
        if (length == 5)
350
 
                egg_test_success (test, NULL);
351
 
        else
352
 
                egg_test_failed (test, "failed the strlen %i", length);
353
 
 
354
 
        /************************************************************/
355
 
        egg_test_title (test, "strlen correct");
356
 
        length = egg_strlen ("123456789", 9);
357
 
        if (length == 9)
358
 
                egg_test_success (test, NULL);
359
 
        else
360
 
                egg_test_failed (test, "failed the strlen %i", length);
361
 
 
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);
369
 
        else
370
 
                egg_test_failed (test, "failed the replace '%s'", text_safe);
371
 
        g_free (text_safe);
372
 
 
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);
378
 
        else
379
 
                egg_test_failed (test, "failed the replace '%s'", text_safe);
380
 
        g_free (text_safe);
381
 
 
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);
387
 
        else
388
 
                egg_test_failed (test, "failed the replace '%s'", text_safe);
389
 
        g_free (text_safe);
390
 
 
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);
396
 
        else
397
 
                egg_test_failed (test, "failed the replace '%s'", text_safe);
398
 
        g_free (text_safe);
399
 
 
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);
407
 
        else
408
 
                egg_test_failed (test, "value is %i", value);
409
 
 
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);
415
 
        else
416
 
                egg_test_failed (test, "value is %i", value);
417
 
 
418
 
        /************************************************************/
419
 
        egg_test_title (test, "don't convert invalid number");
420
 
        ret = egg_strtoint ("dave", &value);
421
 
        if (!ret)
422
 
                egg_test_success (test, NULL);
423
 
        else
424
 
                egg_test_failed (test, "value is %i", value);
425
 
 
426
 
        /************************************************************/
427
 
        egg_test_title (test, "convert NULL to a number");
428
 
        ret = egg_strtouint (NULL, &uvalue);
429
 
        if (!ret)
430
 
                egg_test_success (test, NULL);
431
 
        else
432
 
                egg_test_failed (test, "value is %i", uvalue);
433
 
 
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);
439
 
        else
440
 
                egg_test_failed (test, "value is %i", uvalue);
441
 
 
442
 
        /************************************************************/
443
 
        egg_test_title (test, "convert invalid uint number");
444
 
        ret = egg_strtouint ("-234", &uvalue);
445
 
        if (ret == FALSE)
446
 
                egg_test_success (test, NULL);
447
 
        else
448
 
                egg_test_failed (test, "value is %i", uvalue);
449
 
 
450
 
        egg_test_end (test);
451
 
}
452
 
#endif
453