~profzoom/ubuntu/quantal/wmaker/bug-1079925

« back to all changes in this revision

Viewing changes to WINGs/string.c

  • Committer: Bazaar Package Importer
  • Author(s): Marcelo E. Magallon
  • Date: 2004-11-10 14:05:30 UTC
  • Revision ID: james.westby@ubuntu.com-20041110140530-qpd66b5lm38x7apk
Tags: upstream-0.91.0
ImportĀ upstreamĀ versionĀ 0.91.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "wconfig.h"
 
3
 
 
4
#include <string.h>
 
5
#include <stdlib.h>
 
6
#include <ctype.h>
 
7
 
 
8
#include "WUtil.h"
 
9
 
 
10
 
 
11
 
 
12
#define PRC_ALPHA       0
 
13
#define PRC_BLANK       1
 
14
#define PRC_ESCAPE      2
 
15
#define PRC_DQUOTE      3
 
16
#define PRC_EOS         4
 
17
#define PRC_SQUOTE      5
 
18
 
 
19
typedef struct {
 
20
    short nstate;
 
21
    short output;
 
22
} DFA;
 
23
 
 
24
 
 
25
static DFA mtable[9][6] = {
 
26
    {{3,1},{0,0},{4,0},{1,0},{8,0},{6,0}},
 
27
    {{1,1},{1,1},{2,0},{3,0},{5,0},{1,1}},
 
28
    {{1,1},{1,1},{1,1},{1,1},{5,0},{1,1}},
 
29
    {{3,1},{5,0},{4,0},{1,0},{5,0},{6,0}},
 
30
    {{3,1},{3,1},{3,1},{3,1},{5,0},{3,1}},
 
31
    {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
 
32
    {{6,1},{6,1},{7,0},{6,1},{5,0},{3,0}},
 
33
    {{6,1},{6,1},{6,1},{6,1},{5,0},{6,1}},
 
34
    {{-1,-1},{0,0},{0,0},{0,0},{0,0},{0,0}}, /* final state */
 
35
};
 
36
 
 
37
char*
 
38
wtokennext(char *word, char **next)
 
39
{
 
40
    char *ptr;
 
41
    char *ret, *t;
 
42
    int state, ctype;
 
43
 
 
44
    t = ret = wmalloc(strlen(word)+1);
 
45
    ptr = word;
 
46
 
 
47
    state = 0;
 
48
    *t = 0;
 
49
    while (1) {
 
50
        if (*ptr==0)
 
51
            ctype = PRC_EOS;
 
52
        else if (*ptr=='\\')
 
53
            ctype = PRC_ESCAPE;
 
54
        else if (*ptr=='"')
 
55
            ctype = PRC_DQUOTE;
 
56
        else if (*ptr=='\'')
 
57
            ctype = PRC_SQUOTE;
 
58
        else if (*ptr==' ' || *ptr=='\t')
 
59
            ctype = PRC_BLANK;
 
60
        else
 
61
            ctype = PRC_ALPHA;
 
62
 
 
63
        if (mtable[state][ctype].output) {
 
64
            *t = *ptr; t++;
 
65
            *t = 0;
 
66
        }
 
67
        state = mtable[state][ctype].nstate;
 
68
        ptr++;
 
69
        if (mtable[state][0].output<0) {
 
70
            break;
 
71
        }
 
72
    }
 
73
 
 
74
    if (*ret==0)
 
75
        t = NULL;
 
76
    else
 
77
        t = wstrdup(ret);
 
78
 
 
79
    wfree(ret);
 
80
 
 
81
    if (ctype==PRC_EOS)
 
82
        *next = NULL;
 
83
    else
 
84
        *next = ptr;
 
85
 
 
86
    return t;
 
87
}
 
88
 
 
89
 
 
90
/* separate a string in tokens, taking " and ' into account */
 
91
void
 
92
wtokensplit(char *command, char ***argv, int *argc)
 
93
{
 
94
    char *token, *line;
 
95
    int count;
 
96
 
 
97
    count = 0;
 
98
    line = command;
 
99
    do {
 
100
        token = wtokennext(line, &line);
 
101
        if (token) {
 
102
            if (count == 0)
 
103
                *argv = wmalloc(sizeof(char**));
 
104
            else
 
105
                *argv = wrealloc(*argv, (count+1)*sizeof(char**));
 
106
            (*argv)[count++] = token;
 
107
        }
 
108
    } while (token!=NULL && line!=NULL);
 
109
 
 
110
    *argc = count;
 
111
}
 
112
 
 
113
 
 
114
 
 
115
 
 
116
 
 
117
char*
 
118
wtokenjoin(char **list, int count)
 
119
{
 
120
    int i, j;
 
121
    char *flat_string, *wspace;
 
122
 
 
123
    j = 0;
 
124
    for (i=0; i<count; i++) {
 
125
        if (list[i]!=NULL && list[i][0]!=0) {
 
126
            j += strlen(list[i]);
 
127
            if (strpbrk(list[i], " \t"))
 
128
                j += 2;
 
129
        }
 
130
    }
 
131
 
 
132
    flat_string = wmalloc(j+count+1);
 
133
 
 
134
    *flat_string = 0;
 
135
    for (i=0; i<count; i++) {
 
136
        if (list[i]!=NULL && list[i][0]!=0) {
 
137
            if (i>0)
 
138
                strcat(flat_string, " ");
 
139
            wspace = strpbrk(list[i], " \t");
 
140
            if (wspace)
 
141
                strcat(flat_string, "\"");
 
142
            strcat(flat_string, list[i]);
 
143
            if (wspace)
 
144
                strcat(flat_string, "\"");
 
145
        }
 
146
    }
 
147
 
 
148
    return flat_string;
 
149
}
 
150
 
 
151
 
 
152
 
 
153
void
 
154
wtokenfree(char **tokens, int count)
 
155
{
 
156
    while (--count) wfree(tokens[count]);
 
157
    wfree(tokens);
 
158
}
 
159
 
 
160
 
 
161
 
 
162
char*
 
163
wtrimspace(char *s)
 
164
{
 
165
    char *t;
 
166
    char *c;
 
167
 
 
168
    while (isspace(*s) && *s) s++;
 
169
    t = s+strlen(s)-1;
 
170
    while (t > s && isspace(*t)) t--;
 
171
 
 
172
    c = wmalloc(t-s+2);
 
173
    memcpy(c, s, t-s+1);
 
174
    c[t-s+1] = 0;
 
175
 
 
176
    return c;
 
177
}
 
178
 
 
179
 
 
180
char*
 
181
wstrdup(char *str)
 
182
{
 
183
    assert(str!=NULL);
 
184
 
 
185
    return strcpy(wmalloc(strlen(str)+1), str);
 
186
}
 
187
 
 
188
 
 
189
char*
 
190
wstrndup(char *str, size_t len)
 
191
{
 
192
    char *copy;
 
193
 
 
194
    assert(str!=NULL);
 
195
 
 
196
    len = WMIN(len, strlen(str));
 
197
    copy = strncpy(wmalloc(len+1), str, len);
 
198
    copy[len] = 0;
 
199
 
 
200
    return copy;
 
201
}
 
202
 
 
203
 
 
204
char*
 
205
wstrconcat(char *str1, char *str2)
 
206
{
 
207
    char *str;
 
208
 
 
209
    if (!str1)
 
210
        return wstrdup(str2);
 
211
    else if (!str2)
 
212
        return wstrdup(str1);
 
213
 
 
214
    str = wmalloc(strlen(str1)+strlen(str2)+1);
 
215
    strcpy(str, str1);
 
216
    strcat(str, str2);
 
217
 
 
218
    return str;
 
219
}
 
220
 
 
221
 
 
222
char*
 
223
wstrappend(char *dst, char *src)
 
224
{
 
225
    if (!dst)
 
226
        return wstrdup(src);
 
227
    else if (!src || *src==0)
 
228
        return dst;
 
229
 
 
230
    dst = wrealloc(dst, strlen(dst)+strlen(src)+1);
 
231
    strcat(dst, src);
 
232
 
 
233
    return dst;
 
234
}
 
235
 
 
236
 
 
237
#ifndef HAVE_STRCASECMP
 
238
int
 
239
strcasecmp(const char *s1, const char *s2)
 
240
{
 
241
    while (*s1 && *s2 && (tolower(*s1)==tolower(*s2))) {
 
242
        s1++;
 
243
        s2++;
 
244
    }
 
245
 
 
246
    return (tolower(*s1) - tolower(*s2));
 
247
}
 
248
#endif
 
249
 
 
250