annotate lwlib/lw_string.c @ 372:90de73ba0cac

Created a useful debug framework and adjusted lw_expr_print() to return a "static" dynamic string
author lost@starbug
date Thu, 22 Apr 2010 18:19:06 -0600
parents c15cca3ae6a2
children
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
329
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
43 char *lw_strndup(const char *s, int len)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
44 {
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
45 char *r;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
46 int sl;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
47
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
48 sl = strlen(s);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
49 if (sl > len)
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
50 sl = len;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
51
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
52 r = lw_alloc(sl + 1);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
53 memmove(r, s, sl);
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
54 r[sl] = '\0';
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
55 return r;
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
56 }
c15cca3ae6a2 Created first pass of input layer
lost
parents: 326
diff changeset
57
326
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
58 char *lw_token(const char *s, int sep, const char **ap)
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
59 {
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
60 const char *p;
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
61 char *r;
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 (!s)
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
64 return NULL;
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
65
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
66 p = strchr(s, sep);
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
67 if (!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 if (ap)
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
70 *ap = NULL;
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
71 return lw_strdup(s);
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
72 }
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
73
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
74 r = lw_alloc(p - s + 1);
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
75 strncpy(r, (char *)s, p - s);
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
76 r[p - s] = '\0';
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
77
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
78 if (ap)
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
79 {
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
80 while (*p && *p == sep)
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
81 p++;
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
82 *ap = p;
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
83 }
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
84 return r;
2eb058346cad Added string module and expanded stringlist module
lost
parents:
diff changeset
85 }