326
|
1 /*
|
|
2 lw_string.c
|
|
3
|
|
4 Copyright © 2010 William Astle
|
|
5
|
|
6 This file is part of LWTOOLS.
|
|
7
|
|
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
|
|
9 terms of the GNU General Public License as published by the Free Software
|
|
10 Foundation, either version 3 of the License, or (at your option) any later
|
|
11 version.
|
|
12
|
|
13 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
16 more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License along with
|
|
19 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 #include <config.h>
|
|
23
|
|
24 #include <string.h>
|
|
25 #include <stdlib.h>
|
|
26
|
|
27 #define ___lw_string_c_seen___
|
|
28 #include "lw_alloc.h"
|
|
29 #include "lw_string.h"
|
|
30
|
|
31 char *lw_strdup(const char *s)
|
|
32 {
|
|
33 char *r;
|
|
34
|
|
35 if (!s)
|
|
36 s = "(null)";
|
|
37
|
|
38 r = lw_alloc(strlen(s) + 1);
|
|
39 strcpy(r, s);
|
|
40 return r;
|
|
41 }
|
|
42
|
|
43 char *lw_token(const char *s, int sep, const char **ap)
|
|
44 {
|
|
45 const char *p;
|
|
46 char *r;
|
|
47
|
|
48 if (!s)
|
|
49 return NULL;
|
|
50
|
|
51 p = strchr(s, sep);
|
|
52 if (!p)
|
|
53 {
|
|
54 if (ap)
|
|
55 *ap = NULL;
|
|
56 return lw_strdup(s);
|
|
57 }
|
|
58
|
|
59 r = lw_alloc(p - s + 1);
|
|
60 strncpy(r, (char *)s, p - s);
|
|
61 r[p - s] = '\0';
|
|
62
|
|
63 if (ap)
|
|
64 {
|
|
65 while (*p && *p == sep)
|
|
66 p++;
|
|
67 *ap = p;
|
|
68 }
|
|
69 return r;
|
|
70 }
|