aboutsummaryrefslogtreecommitdiff
path: root/lib/tests/utf8.c
blob: ce6c040ca27b3f2d5764cd32b40a63f72f97bfe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* This file is part of GNU Dico
   Copyright (C) 2012, 2016-2018 Sergey Poznyakoff

   GNU Dico is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Dico is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dico.h>

typedef int (*opfn) (int argc, char **argv);

struct optab {
    char *name;
    opfn fun;
};

static unsigned *
strtowc(const char *str)
{
    unsigned *buf;
    if (utf8_mbstr_to_wc(str, &buf, NULL)) {
	dico_log(L_ERR, errno, "cannot convert \"%s\"", str);
	abort();
    }
    return buf;
}


static int
help(int argc, char **argv)
{
    printf("No more help, sorry");
    return 0;
}

static int
op_strlen(int argc, char **argv)
{
    char *opname = *argv++;
    argc--;
    if (argc != 1) {
	dico_log(L_ERR, 0, "%s requires one argument", opname);
	return 1;
    }
    printf("%lu\n", (unsigned long) utf8_strlen(argv[0]));
    return 0;
}

static int
op_strcasecmp(int argc, char **argv)
{
    char *opname = *argv++;
    argc--;
    if (argc != 2) {
	dico_log(L_ERR, 0, "%s requires two arguments", opname);
	return 1;
    }
    printf("%d\n", utf8_strcasecmp(argv[0], argv[1]));
    return 0;
}

static int
op_strncasecmp(int argc, char **argv)
{
    char *opname = *argv++;
    argc--;
    if (argc != 3) {
	dico_log(L_ERR, 0, "%s requires three arguments", opname);
	return 1;
    }
    printf("%d\n", utf8_strncasecmp(argv[0], argv[1], atoi(argv[2])));
    return 0;
}

static int
op_wc_strcmp(int argc, char **argv)
{
    unsigned *wa, *wb;
    char *opname = *argv++;
    argc--;
    if (argc != 2) {
	dico_log(L_ERR, 0, "%s requires two arguments", opname);
	return 1;
    }

    wa = strtowc(argv[0]);
    wb = strtowc(argv[1]);

    printf("%d\n", utf8_wc_strcmp(wa, wb));
    return 0;
}

static int
op_wc_strncmp(int argc, char **argv)
{
    unsigned *wa, *wb;
    char *opname = *argv++;
    argc--;
    if (argc != 3) {
	dico_log(L_ERR, 0, "%s requires three arguments", opname);
	return 1;
    }

    wa = strtowc(argv[0]);
    wb = strtowc(argv[1]);

    printf("%d\n", utf8_wc_strncmp(wa, wb, atoi(argv[2])));
    return 0;
}

static int
op_wc_strcasecmp(int argc, char **argv)
{
    unsigned *wa, *wb;
    char *opname = *argv++;
    argc--;
    if (argc != 2) {
	dico_log(L_ERR, 0, "%s requires two arguments", opname);
	return 1;
    }

    wa = strtowc(argv[0]);
    wb = strtowc(argv[1]);

    printf("%d\n", utf8_wc_strcasecmp(wa, wb));
    return 0;
}

static int
op_wc_strncasecmp(int argc, char **argv)
{
    unsigned *wa, *wb;
    char *opname = *argv++;
    argc--;
    if (argc != 3) {
	dico_log(L_ERR, 0, "%s requires three arguments", opname);
	return 1;
    }

    wa = strtowc(argv[0]);
    wb = strtowc(argv[1]);

    printf("%d\n", utf8_wc_strncasecmp(wa, wb, atoi(argv[2])));
    return 0;
}

static int
op_toupper(int argc, char **argv)
{
    char *opname = *argv++;
    argc--;
    if (argc != 1) {
	dico_log(L_ERR, 0, "%s requires one arguments", opname);
	return 1;
    }
    if (utf8_toupper(argv[0]))
	abort();
    printf("%s\n", argv[0]);
    return 0;
}

static int
op_tolower(int argc, char **argv)
{
    char *opname = *argv++;
    argc--;
    if (argc != 1) {
	dico_log(L_ERR, 0, "%s requires one argument", opname);
	return 1;
    }
    if (utf8_tolower(argv[0]))
	abort();
    printf("%s\n", argv[0]);
    return 0;
}

static int
op_wc_strchr(int argc, char **argv)
{
    unsigned *wa, *wb;
    const unsigned *p;
    char *opname = *argv++;
    argc--;
    if (argc != 2) {
	dico_log(L_ERR, 0, "%s requires two arguments", opname);
	return 1;
    }

    wa = strtowc(argv[0]);
    wb = strtowc(argv[1]);
    p = utf8_wc_strchr(wa, wb[0]);
    if (!p)
	return 2;
    printf("%td\n", p - wa);
    return 0;
}

static int
op_wc_strchr_ci(int argc, char **argv)
{
    unsigned *wa, *wb;
    const unsigned *p;
    char *opname = *argv++;
    argc--;
    if (argc != 2) {
	dico_log(L_ERR, 0, "%s requires two arguments", opname);
	return 1;
    }

    wa = strtowc(argv[0]);
    wb = strtowc(argv[1]);
    p = utf8_wc_strchr_ci(wa, wb[0]);
    if (!p)
	return 2;
    printf("%td\n", p - wa);
    return 0;
}

static int
op_wc_strstr(int argc, char **argv)
{
    unsigned *wa, *wb;
    const unsigned *p;
    char *opname = *argv++;
    argc--;
    if (argc != 2) {
	dico_log(L_ERR, 0, "%s requires two arguments", opname);
	return 1;
    }

    wa = strtowc(argv[0]);
    wb = strtowc(argv[1]);
    p = utf8_wc_strstr(wa, wb);
    if (!p) {
	if (errno) {
	    dico_log(L_ERR, errno, "can't match");
	    return 3;
	}
	return 2;
    }
    printf("%td\n", p - wa);
    return 0;
}

struct optab optab[] = {
    { "help", help },
    { "strlen", op_strlen },
    { "strcasecmp", op_strcasecmp },
    { "strncasecmp", op_strncasecmp },
    { "wc_strcmp", op_wc_strcmp },
    { "wc_strncmp", op_wc_strncmp },
    { "wc_strcasecmp", op_wc_strcasecmp },
    { "wc_strncasecmp", op_wc_strncasecmp },
    { "toupper", op_toupper },
    { "tolower", op_tolower },
    { "wc_strchr", op_wc_strchr },
    { "wc_strchr_ci", op_wc_strchr_ci },
    { "wc_strstr", op_wc_strstr },
    { NULL }
};

void
usage(FILE *fp)
{
    fprintf(fp, "usage: %s [op] [args]\n", dico_program_name);
}

int
main(int argc, char **argv)
{
    struct optab *op;
    
    dico_set_program_name(argv[0]);
    argc--;
    argv++;
    if (!argc) {
	usage(stderr);
	return 1;
    }
    for (op = optab; op->name; op++)
	if (strcmp(*argv, op->name) == 0)
	    return op->fun(argc, argv);
    dico_log(L_ERR, 0, "unknown operation");
    return 1;
}

Return to:

Send suggestions and report system problems to the System administrator.