Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / string.c
Commit [+]AuthorDateLineData
9a89e6cc kojima2000-07-15 22:08:25 +00001
af287fb8 dan2001-11-17 04:26:08 +00002#include "wconfig.h"
3
9a89e6cc kojima2000-07-15 22:08:25 +00004#include <string.h>
5#include <stdlib.h>
6#include <ctype.h>
7
8#include "WUtil.h"
9
9a89e6cc kojima2000-07-15 22:08:25 +000010#define PRC_ALPHA 0
11#define PRC_BLANK 1
12#define PRC_ESCAPE 2
13#define PRC_DQUOTE 3
14#define PRC_EOS 4
15#define PRC_SQUOTE 5
16
17typedef struct {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020018 short nstate;
19 short output;
9a89e6cc kojima2000-07-15 22:08:25 +000020} DFA;
21
9a89e6cc kojima2000-07-15 22:08:25 +000022static DFA mtable[9][6] = {
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020023 {{3, 1}, {0, 0}, {4, 0}, {1, 0}, {8, 0}, {6, 0}},
24 {{1, 1}, {1, 1}, {2, 0}, {3, 0}, {5, 0}, {1, 1}},
25 {{1, 1}, {1, 1}, {1, 1}, {1, 1}, {5, 0}, {1, 1}},
26 {{3, 1}, {5, 0}, {4, 0}, {1, 0}, {5, 0}, {6, 0}},
27 {{3, 1}, {3, 1}, {3, 1}, {3, 1}, {5, 0}, {3, 1}},
28 {{-1, -1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, /* final state */
29 {{6, 1}, {6, 1}, {7, 0}, {6, 1}, {5, 0}, {3, 0}},
30 {{6, 1}, {6, 1}, {6, 1}, {6, 1}, {5, 0}, {6, 1}},
31 {{-1, -1}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}}, /* final state */
9a89e6cc kojima2000-07-15 22:08:25 +000032};
33
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020034char *wtokennext(char *word, char **next)
9a89e6cc kojima2000-07-15 22:08:25 +000035{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020036 char *ptr;
37 char *ret, *t;
38 int state, ctype;
39
40 t = ret = wmalloc(strlen(word) + 1);
41 ptr = word;
42
43 state = 0;
44 *t = 0;
45 while (1) {
46 if (*ptr == 0)
47 ctype = PRC_EOS;
48 else if (*ptr == '\\')
49 ctype = PRC_ESCAPE;
50 else if (*ptr == '"')
51 ctype = PRC_DQUOTE;
52 else if (*ptr == '\'')
53 ctype = PRC_SQUOTE;
54 else if (*ptr == ' ' || *ptr == '\t')
55 ctype = PRC_BLANK;
56 else
57 ctype = PRC_ALPHA;
58
59 if (mtable[state][ctype].output) {
60 *t = *ptr;
61 t++;
62 *t = 0;
63 }
64 state = mtable[state][ctype].nstate;
65 ptr++;
66 if (mtable[state][0].output < 0) {
67 break;
68 }
69 }
70
71 if (*ret == 0)
72 t = NULL;
73 else
74 t = wstrdup(ret);
75
76 wfree(ret);
77
78 if (ctype == PRC_EOS)
79 *next = NULL;
80 else
81 *next = ptr;
82
83 return t;
9a89e6cc kojima2000-07-15 22:08:25 +000084}
85
9a89e6cc kojima2000-07-15 22:08:25 +000086/* separate a string in tokens, taking " and ' into account */
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020087void wtokensplit(char *command, char ***argv, int *argc)
9a89e6cc kojima2000-07-15 22:08:25 +000088{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020089 char *token, *line;
90 int count;
91
92 count = 0;
93 line = command;
94 do {
95 token = wtokennext(line, &line);
96 if (token) {
97 if (count == 0)
98 *argv = wmalloc(sizeof(char **));
99 else
100 *argv = wrealloc(*argv, (count + 1) * sizeof(char **));
101 (*argv)[count++] = token;
102 }
103 } while (token != NULL && line != NULL);
104
105 *argc = count;
9a89e6cc kojima2000-07-15 22:08:25 +0000106}
107
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200108char *wtokenjoin(char **list, int count)
9a89e6cc kojima2000-07-15 22:08:25 +0000109{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200110 int i, j;
111 char *flat_string, *wspace;
112
113 j = 0;
114 for (i = 0; i < count; i++) {
115 if (list[i] != NULL && list[i][0] != 0) {
116 j += strlen(list[i]);
117 if (strpbrk(list[i], " \t"))
118 j += 2;
119 }
120 }
121
122 flat_string = wmalloc(j + count + 1);
123
124 *flat_string = 0;
125 for (i = 0; i < count; i++) {
126 if (list[i] != NULL && list[i][0] != 0) {
127 if (i > 0)
128 strcat(flat_string, " ");
129 wspace = strpbrk(list[i], " \t");
130 if (wspace)
131 strcat(flat_string, "\"");
132 strcat(flat_string, list[i]);
133 if (wspace)
134 strcat(flat_string, "\"");
135 }
136 }
137
138 return flat_string;
9a89e6cc kojima2000-07-15 22:08:25 +0000139}
140
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200141void wtokenfree(char **tokens, int count)
9a89e6cc kojima2000-07-15 22:08:25 +0000142{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200143 while (--count)
144 wfree(tokens[count]);
145 wfree(tokens);
9a89e6cc kojima2000-07-15 22:08:25 +0000146}
147
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200148char *wtrimspace(char *s)
9a89e6cc kojima2000-07-15 22:08:25 +0000149{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200150 char *t;
151 char *c;
99ce8b2d dan2000-10-25 22:41:03 +0000152
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200153 while (isspace(*s) && *s)
154 s++;
155 t = s + strlen(s) - 1;
156 while (t > s && isspace(*t))
157 t--;
99ce8b2d dan2000-10-25 22:41:03 +0000158
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200159 c = wmalloc(t - s + 2);
160 memcpy(c, s, t - s + 1);
161 c[t - s + 1] = 0;
99ce8b2d dan2000-10-25 22:41:03 +0000162
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200163 return c;
9a89e6cc kojima2000-07-15 22:08:25 +0000164}
165
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200166char *wstrdup(char *str)
3ab16b3e dan2001-05-27 15:58:28 +0000167{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200168 assert(str != NULL);
3ab16b3e dan2001-05-27 15:58:28 +0000169
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200170 return strcpy(wmalloc(strlen(str) + 1), str);
3ab16b3e dan2001-05-27 15:58:28 +0000171}
172
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200173char *wstrndup(char *str, size_t len)
ca43aba0 dan2002-12-02 00:01:05 +0000174{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200175 char *copy;
ca43aba0 dan2002-12-02 00:01:05 +0000176
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200177 assert(str != NULL);
ca43aba0 dan2002-12-02 00:01:05 +0000178
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200179 len = WMIN(len, strlen(str));
180 copy = strncpy(wmalloc(len + 1), str, len);
181 copy[len] = 0;
ca43aba0 dan2002-12-02 00:01:05 +0000182
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200183 return copy;
ca43aba0 dan2002-12-02 00:01:05 +0000184}
185
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200186char *wstrconcat(char *str1, char *str2)
3ab16b3e dan2001-05-27 15:58:28 +0000187{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200188 char *str;
3ab16b3e dan2001-05-27 15:58:28 +0000189
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200190 if (!str1)
191 return wstrdup(str2);
192 else if (!str2)
193 return wstrdup(str1);
3ab16b3e dan2001-05-27 15:58:28 +0000194
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200195 str = wmalloc(strlen(str1) + strlen(str2) + 1);
196 strcpy(str, str1);
197 strcat(str, str2);
3ab16b3e dan2001-05-27 15:58:28 +0000198
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200199 return str;
3ab16b3e dan2001-05-27 15:58:28 +0000200}
201
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200202char *wstrappend(char *dst, char *src)
3ab16b3e dan2001-05-27 15:58:28 +0000203{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200204 if (!dst)
205 return wstrdup(src);
206 else if (!src || *src == 0)
207 return dst;
3ab16b3e dan2001-05-27 15:58:28 +0000208
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200209 dst = wrealloc(dst, strlen(dst) + strlen(src) + 1);
210 strcat(dst, src);
3ab16b3e dan2001-05-27 15:58:28 +0000211
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200212 return dst;
3ab16b3e dan2001-05-27 15:58:28 +0000213}
214
af287fb8 dan2001-11-17 04:26:08 +0000215#ifndef HAVE_STRCASECMP
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200216int strcasecmp(const char *s1, const char *s2)
af287fb8 dan2001-11-17 04:26:08 +0000217{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200218 while (*s1 && *s2 && (tolower(*s1) == tolower(*s2))) {
219 s1++;
220 s2++;
221 }
af287fb8 dan2001-11-17 04:26:08 +0000222
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200223 return (tolower(*s1) - tolower(*s2));
af287fb8 dan2001-11-17 04:26:08 +0000224}
225#endif