annotate lwlib/lw_string.c @ 326:2eb058346cad

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