implement TC language code short format.
@@ -0,0 +1,389 @@ | ||
1 | +/* | |
2 | + * test_tadlangcode.c | |
3 | + * | |
4 | + * Copyright (c) 2013 project bchan | |
5 | + * | |
6 | + * This software is provided 'as-is', without any express or implied | |
7 | + * warranty. In no event will the authors be held liable for any damages | |
8 | + * arising from the use of this software. | |
9 | + * | |
10 | + * Permission is granted to anyone to use this software for any purpose, | |
11 | + * including commercial applications, and to alter it and redistribute it | |
12 | + * freely, subject to the following restrictions: | |
13 | + * | |
14 | + * 1. The origin of this software must not be misrepresented; you must not | |
15 | + * claim that you wrote the original software. If you use this software | |
16 | + * in a product, an acknowledgment in the product documentation would be | |
17 | + * appreciated but is not required. | |
18 | + * | |
19 | + * 2. Altered source versions must be plainly marked as such, and must not be | |
20 | + * misrepresented as being the original software. | |
21 | + * | |
22 | + * 3. This notice may not be removed or altered from any source | |
23 | + * distribution. | |
24 | + * | |
25 | + */ | |
26 | + | |
27 | +#include "test_tad.h" | |
28 | + | |
29 | +#include "tadlangcode.h" | |
30 | + | |
31 | +#include <basic.h> | |
32 | +#include <bstdio.h> | |
33 | +#include <bstdlib.h> | |
34 | +#include <bstring.h> | |
35 | +#include <tstring.h> | |
36 | + | |
37 | +#include <unittest_driver.h> | |
38 | + | |
39 | +LOCAL UNITTEST_RESULT test_TCtotadlangcode_common(TC *testdata, W testdata_len, tadlangcode *expected) | |
40 | +{ | |
41 | + W err; | |
42 | + Bool cmp; | |
43 | + tadlangcode result; | |
44 | + | |
45 | + err = TCtotadlangcode(testdata, testdata_len, &result); | |
46 | + if (err < 0) { | |
47 | + return UNITTEST_RESULT_FAIL; | |
48 | + } | |
49 | + cmp = tadlangcodecmp(&result, expected); | |
50 | + if (cmp == False) { | |
51 | + return UNITTEST_RESULT_FAIL; | |
52 | + } | |
53 | + return UNITTEST_RESULT_PASS; | |
54 | +} | |
55 | + | |
56 | +LOCAL UNITTEST_RESULT test_TCtotadlangcode_1() | |
57 | +{ | |
58 | + TC testdata[] = {0xFE21}; | |
59 | + W testdata_len = 1; | |
60 | + tadlangcode expected = {0, 0x21}; | |
61 | + return test_TCtotadlangcode_common(testdata, testdata_len, &expected); | |
62 | +} | |
63 | + | |
64 | +LOCAL UNITTEST_RESULT test_TCtotadlangcode_2() | |
65 | +{ | |
66 | + TC testdata[] = {0xFE22}; | |
67 | + W testdata_len = 1; | |
68 | + tadlangcode expected = {0, 0x22}; | |
69 | + return test_TCtotadlangcode_common(testdata, testdata_len, &expected); | |
70 | +} | |
71 | + | |
72 | +LOCAL UNITTEST_RESULT test_TCtotadlangcode_3() | |
73 | +{ | |
74 | + TC testdata[] = {0xFEFE, 0xFE21}; | |
75 | + W testdata_len = 2; | |
76 | + tadlangcode expected = {2, 0x21}; | |
77 | + return test_TCtotadlangcode_common(testdata, testdata_len, &expected); | |
78 | +} | |
79 | + | |
80 | +LOCAL UNITTEST_RESULT test_TCtotadlangcode_4() | |
81 | +{ | |
82 | + TC testdata[] = {0xFEFE, 0x0021}; | |
83 | + W testdata_len = 2; | |
84 | + tadlangcode expected = {1, 0x21}; | |
85 | + return test_TCtotadlangcode_common(testdata, testdata_len, &expected); | |
86 | +} | |
87 | + | |
88 | +LOCAL UNITTEST_RESULT test_TCtotadlangcode_5() | |
89 | +{ | |
90 | + TC testdata[] = {0xFEFE, 0xFEFE, 0xFE21}; | |
91 | + W testdata_len = 3; | |
92 | + tadlangcode expected = {4, 0x21}; | |
93 | + return test_TCtotadlangcode_common(testdata, testdata_len, &expected); | |
94 | +} | |
95 | + | |
96 | +LOCAL UNITTEST_RESULT test_TCtotadlangcode_6() | |
97 | +{ | |
98 | + TC testdata[] = {0xFEFE, 0xFEFE, 0x0021}; | |
99 | + W testdata_len = 3; | |
100 | + tadlangcode expected = {3, 0x21}; | |
101 | + return test_TCtotadlangcode_common(testdata, testdata_len, &expected); | |
102 | +} | |
103 | + | |
104 | +LOCAL UNITTEST_RESULT test_tadlangcodetoTC_common(tadlangcode *testdata, TC *expected, W expected_len) | |
105 | +{ | |
106 | + W len; | |
107 | + TC *str; | |
108 | + | |
109 | + len = tadlangcodetoTC(testdata, NULL, 1); | |
110 | + if (len != expected_len) { | |
111 | + printf("tadlangcodetoTC length check 1 error\n"); | |
112 | + return UNITTEST_RESULT_FAIL; | |
113 | + } | |
114 | + | |
115 | + str = malloc(sizeof(TC)*len); | |
116 | + if (str == NULL) { | |
117 | + return UNITTEST_RESULT_FAIL; | |
118 | + } | |
119 | + | |
120 | + len = tadlangcodetoTC(testdata, str, -1); | |
121 | + if (len != expected_len) { | |
122 | + free(str); | |
123 | + printf("tadlangcodetoTC length check 2 error\n"); | |
124 | + return UNITTEST_RESULT_FAIL; | |
125 | + } | |
126 | + | |
127 | + len = tadlangcodetoTC(testdata, str, len); | |
128 | + if (len != expected_len) { | |
129 | + free(str); | |
130 | + printf("tadlangcodetoTC converted length error\n"); | |
131 | + return UNITTEST_RESULT_FAIL; | |
132 | + } | |
133 | + if (tc_strncmp(str, expected, len) != 0) { | |
134 | + free(str); | |
135 | + printf("tadlangcodetoTC convarted string error\n"); | |
136 | + return UNITTEST_RESULT_FAIL; | |
137 | + } | |
138 | + | |
139 | + free(str); | |
140 | + | |
141 | + return UNITTEST_RESULT_PASS; | |
142 | +} | |
143 | + | |
144 | +LOCAL UNITTEST_RESULT test_tadlangcodetoTC_1() | |
145 | +{ | |
146 | + tadlangcode testdata = {0, 0x21}; | |
147 | + TC expected[] = {0xFE21}; | |
148 | + W expected_len = 1; | |
149 | + return test_tadlangcodetoTC_common(&testdata, expected, expected_len); | |
150 | +} | |
151 | + | |
152 | +LOCAL UNITTEST_RESULT test_tadlangcodetoTC_2() | |
153 | +{ | |
154 | + tadlangcode testdata = {0, 0x22}; | |
155 | + TC expected[] = {0xFE22}; | |
156 | + W expected_len = 1; | |
157 | + return test_tadlangcodetoTC_common(&testdata, expected, expected_len); | |
158 | +} | |
159 | + | |
160 | +LOCAL UNITTEST_RESULT test_tadlangcodetoTC_3() | |
161 | +{ | |
162 | + tadlangcode testdata = {2, 0x21}; | |
163 | + TC expected[] = {0xFEFE, 0xFE21}; | |
164 | + W expected_len = 2; | |
165 | + return test_tadlangcodetoTC_common(&testdata, expected, expected_len); | |
166 | +} | |
167 | + | |
168 | +LOCAL UNITTEST_RESULT test_tadlangcodetoTC_4() | |
169 | +{ | |
170 | + tadlangcode testdata = {1, 0x21}; | |
171 | + TC expected[] = {0xFEFE, 0x0021}; | |
172 | + W expected_len = 2; | |
173 | + return test_tadlangcodetoTC_common(&testdata, expected, expected_len); | |
174 | +} | |
175 | + | |
176 | +LOCAL UNITTEST_RESULT test_tadlangcodetoTC_5() | |
177 | +{ | |
178 | + tadlangcode testdata = {4, 0x21}; | |
179 | + TC expected[] = {0xFEFE, 0xFEFE, 0xFE21}; | |
180 | + W expected_len = 3; | |
181 | + return test_tadlangcodetoTC_common(&testdata, expected, expected_len); | |
182 | +} | |
183 | + | |
184 | +LOCAL UNITTEST_RESULT test_tadlangcodetoTC_6() | |
185 | +{ | |
186 | + tadlangcode testdata = {3, 0x21}; | |
187 | + TC expected[] = {0xFEFE, 0xFEFE, 0x0021}; | |
188 | + W expected_len = 3; | |
189 | + return test_tadlangcodetoTC_common(&testdata, expected, expected_len); | |
190 | +} | |
191 | + | |
192 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_common(tadlangcode *testdata1, TC *testdata2, W testdata2_len, Bool expected) | |
193 | +{ | |
194 | + Bool ret; | |
195 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
196 | + | |
197 | + ret = tadlangcodecmpTC(testdata2, testdata2_len, testdata1); | |
198 | + if (ret == False) { | |
199 | + if (expected != False) { | |
200 | + result = UNITTEST_RESULT_FAIL; | |
201 | + } | |
202 | + } else { | |
203 | + if (expected == False) { | |
204 | + result = UNITTEST_RESULT_FAIL; | |
205 | + } | |
206 | + } | |
207 | + | |
208 | + return result; | |
209 | +} | |
210 | + | |
211 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_1() | |
212 | +{ | |
213 | + tadlangcode testdata1 = {0, 0x21}; | |
214 | + TC testdata2[] = {0xFE21}; | |
215 | + W testdata2_len = 1; | |
216 | + Bool expected = True; | |
217 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
218 | +} | |
219 | + | |
220 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_2() | |
221 | +{ | |
222 | + tadlangcode testdata1 = {0, 0x22}; | |
223 | + TC testdata2[] = {0xFE22}; | |
224 | + W testdata2_len = 1; | |
225 | + Bool expected = True; | |
226 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
227 | +} | |
228 | + | |
229 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_3() | |
230 | +{ | |
231 | + tadlangcode testdata1 = {2, 0x21}; | |
232 | + TC testdata2[] = {0xFEFE, 0xFE21}; | |
233 | + W testdata2_len = 2; | |
234 | + Bool expected = True; | |
235 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
236 | +} | |
237 | + | |
238 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_4() | |
239 | +{ | |
240 | + tadlangcode testdata1 = {1, 0x21}; | |
241 | + TC testdata2[] = {0xFEFE, 0x0021}; | |
242 | + W testdata2_len = 2; | |
243 | + Bool expected = True; | |
244 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
245 | +} | |
246 | + | |
247 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_5() | |
248 | +{ | |
249 | + tadlangcode testdata1 = {4, 0x21}; | |
250 | + TC testdata2[] = {0xFEFE, 0xFEFE, 0xFE21}; | |
251 | + W testdata2_len = 3; | |
252 | + Bool expected = True; | |
253 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
254 | +} | |
255 | + | |
256 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_6() | |
257 | +{ | |
258 | + tadlangcode testdata1 = {3, 0x21}; | |
259 | + TC testdata2[] = {0xFEFE, 0xFEFE, 0x0021}; | |
260 | + W testdata2_len = 3; | |
261 | + Bool expected = True; | |
262 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
263 | +} | |
264 | + | |
265 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_7() | |
266 | +{ | |
267 | + tadlangcode testdata1 = {0, 0x21}; | |
268 | + TC testdata2[] = {0xFE22}; | |
269 | + W testdata2_len = 1; | |
270 | + Bool expected = False; | |
271 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
272 | +} | |
273 | + | |
274 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_8() | |
275 | +{ | |
276 | + tadlangcode testdata1 = {2, 0x21}; | |
277 | + TC testdata2[] = {0xFEFE, 0xFE22}; | |
278 | + W testdata2_len = 2; | |
279 | + Bool expected = False; | |
280 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
281 | +} | |
282 | + | |
283 | +LOCAL UNITTEST_RESULT test_tadlangcodecmpTC_9() | |
284 | +{ | |
285 | + tadlangcode testdata1 = {4, 0x21}; | |
286 | + TC testdata2[] = {0xFEFE, 0xFEFE, 0xFE22}; | |
287 | + W testdata2_len = 3; | |
288 | + Bool expected = False; | |
289 | + return test_tadlangcodecmpTC_common(&testdata1, testdata2, testdata2_len, expected); | |
290 | +} | |
291 | + | |
292 | +LOCAL UNITTEST_RESULT test_tadlangcodecmp_common(tadlangcode *testdata1, tadlangcode *testdata2, Bool expected) | |
293 | +{ | |
294 | + Bool ret; | |
295 | + UNITTEST_RESULT result = UNITTEST_RESULT_PASS; | |
296 | + | |
297 | + ret = tadlangcodecmp(testdata1, testdata2); | |
298 | + if (ret == False) { | |
299 | + if (expected != False) { | |
300 | + result = UNITTEST_RESULT_FAIL; | |
301 | + } | |
302 | + } else { | |
303 | + if (expected == False) { | |
304 | + result = UNITTEST_RESULT_FAIL; | |
305 | + } | |
306 | + } | |
307 | + | |
308 | + return result; | |
309 | +} | |
310 | + | |
311 | +LOCAL UNITTEST_RESULT test_tadlangcodecmp_1() | |
312 | +{ | |
313 | + tadlangcode testdata1 = {0, 0x21}; | |
314 | + tadlangcode testdata2 = {0, 0x21}; | |
315 | + Bool expected = True; | |
316 | + return test_tadlangcodecmp_common(&testdata1, &testdata2, expected); | |
317 | +} | |
318 | + | |
319 | + | |
320 | +LOCAL UNITTEST_RESULT test_tadlangcodecmp_2() | |
321 | +{ | |
322 | + tadlangcode testdata1 = {0, 0x21}; | |
323 | + tadlangcode testdata2 = {0, 0x22}; | |
324 | + Bool expected = False; | |
325 | + return test_tadlangcodecmp_common(&testdata1, &testdata2, expected); | |
326 | +} | |
327 | + | |
328 | +LOCAL UNITTEST_RESULT test_tadlangcodecmp_3() | |
329 | +{ | |
330 | + tadlangcode testdata1 = {0, 0x21}; | |
331 | + tadlangcode testdata2 = {1, 0x21}; | |
332 | + Bool expected = False; | |
333 | + return test_tadlangcodecmp_common(&testdata1, &testdata2, expected); | |
334 | +} | |
335 | + | |
336 | +LOCAL UNITTEST_RESULT test_tadlangcodecmp_4() | |
337 | +{ | |
338 | + tadlangcode testdata1 = {1, 0x21}; | |
339 | + tadlangcode testdata2 = {1, 0x21}; | |
340 | + Bool expected = True; | |
341 | + return test_tadlangcodecmp_common(&testdata1, &testdata2, expected); | |
342 | +} | |
343 | + | |
344 | +LOCAL UNITTEST_RESULT test_tadlangcodecmp_5() | |
345 | +{ | |
346 | + tadlangcode testdata1 = {1, 0x21}; | |
347 | + tadlangcode testdata2 = {1, 0x22}; | |
348 | + Bool expected = False; | |
349 | + return test_tadlangcodecmp_common(&testdata1, &testdata2, expected); | |
350 | +} | |
351 | + | |
352 | +LOCAL UNITTEST_RESULT test_tadlangcodecmp_6() | |
353 | +{ | |
354 | + tadlangcode testdata1 = {1, 0x21}; | |
355 | + tadlangcode testdata2 = {2, 0x21}; | |
356 | + Bool expected = False; | |
357 | + return test_tadlangcodecmp_common(&testdata1, &testdata2, expected); | |
358 | +} | |
359 | + | |
360 | +EXPORT VOID test_tadlangcode_main(unittest_driver_t *driver) | |
361 | +{ | |
362 | + UNITTEST_DRIVER_REGIST(driver, test_TCtotadlangcode_1); | |
363 | + UNITTEST_DRIVER_REGIST(driver, test_TCtotadlangcode_2); | |
364 | + UNITTEST_DRIVER_REGIST(driver, test_TCtotadlangcode_3); | |
365 | + UNITTEST_DRIVER_REGIST(driver, test_TCtotadlangcode_4); | |
366 | + UNITTEST_DRIVER_REGIST(driver, test_TCtotadlangcode_5); | |
367 | + UNITTEST_DRIVER_REGIST(driver, test_TCtotadlangcode_6); | |
368 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodetoTC_1); | |
369 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodetoTC_2); | |
370 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodetoTC_3); | |
371 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodetoTC_4); | |
372 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodetoTC_5); | |
373 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodetoTC_6); | |
374 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_1); | |
375 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_2); | |
376 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_3); | |
377 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_4); | |
378 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_5); | |
379 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_6); | |
380 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_7); | |
381 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_8); | |
382 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmpTC_9); | |
383 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmp_1); | |
384 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmp_2); | |
385 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmp_3); | |
386 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmp_4); | |
387 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmp_5); | |
388 | + UNITTEST_DRIVER_REGIST(driver, test_tadlangcodecmp_6); | |
389 | +} |
@@ -0,0 +1,161 @@ | ||
1 | +/* | |
2 | + * tadlangcode.c | |
3 | + * | |
4 | + * Copyright (c) 2013 project bchan | |
5 | + * | |
6 | + * This software is provided 'as-is', without any express or implied | |
7 | + * warranty. In no event will the authors be held liable for any damages | |
8 | + * arising from the use of this software. | |
9 | + * | |
10 | + * Permission is granted to anyone to use this software for any purpose, | |
11 | + * including commercial applications, and to alter it and redistribute it | |
12 | + * freely, subject to the following restrictions: | |
13 | + * | |
14 | + * 1. The origin of this software must not be misrepresented; you must not | |
15 | + * claim that you wrote the original software. If you use this software | |
16 | + * in a product, an acknowledgment in the product documentation would be | |
17 | + * appreciated but is not required. | |
18 | + * | |
19 | + * 2. Altered source versions must be plainly marked as such, and must not be | |
20 | + * misrepresented as being the original software. | |
21 | + * | |
22 | + * 3. This notice may not be removed or altered from any source | |
23 | + * distribution. | |
24 | + * | |
25 | + */ | |
26 | + | |
27 | +#include "tadlangcode.h" | |
28 | + | |
29 | +#include <bstdio.h> | |
30 | + | |
31 | +#ifdef BCHAN_CONFIG_DEBUG | |
32 | +# define DP(arg) printf arg | |
33 | +# define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err) | |
34 | +#else | |
35 | +# define DP(arg) /**/ | |
36 | +# define DP_ER(msg, err) /**/ | |
37 | +#endif | |
38 | + | |
39 | +EXPORT TADLANGCODE_PARSER_RESULT tadlangcode_parser_inputchar(tadlangcode_parser_t *parser, TC ch) | |
40 | +{ | |
41 | + switch (parser->state) { | |
42 | + case TADLANGCODE_PARSER_STATE_START: | |
43 | + if ((ch & 0xFF00) != 0xFE00) { | |
44 | + return TADLANGCODE_PARSER_RESULT_INVALID; | |
45 | + } | |
46 | + if (ch == 0xFEFE) { | |
47 | + parser->state = TADLANGCODE_PARSER_STATE_LANGCODE; | |
48 | + parser->iter = 1; | |
49 | + return TADLANGCODE_PARSER_RESULT_CONTINUE; | |
50 | + } | |
51 | + parser->state = TADLANGCODE_PARSER_STATE_DETERMINED; | |
52 | + parser->iter = 0; | |
53 | + parser->code = ch & 0xFF; | |
54 | + return TADLANGCODE_PARSER_RESULT_DETERMINED; | |
55 | + case TADLANGCODE_PARSER_STATE_LANGCODE: | |
56 | + if (ch == 0xFEFE) { | |
57 | + parser->iter += 2; | |
58 | + return TADLANGCODE_PARSER_RESULT_CONTINUE; | |
59 | + } | |
60 | + if ((ch & 0xFF00) == 0x0000) { | |
61 | + parser->state = TADLANGCODE_PARSER_STATE_DETERMINED; | |
62 | + parser->code = ch & 0xFF; | |
63 | + return TADLANGCODE_PARSER_RESULT_DETERMINED; | |
64 | + } | |
65 | + if ((ch & 0xFF00) == 0xFE00) { | |
66 | + parser->state = TADLANGCODE_PARSER_STATE_DETERMINED; | |
67 | + parser->iter += 1; | |
68 | + parser->code = ch & 0xFF; | |
69 | + return TADLANGCODE_PARSER_RESULT_DETERMINED; | |
70 | + } | |
71 | + return TADLANGCODE_PARSER_RESULT_INVALID; | |
72 | + case TADLANGCODE_PARSER_STATE_DETERMINED: | |
73 | + return TADLANGCODE_PARSER_RESULT_INVALID; | |
74 | + } | |
75 | + return TADLANGCODE_PARSER_RESULT_INVALID; | |
76 | +} | |
77 | + | |
78 | +EXPORT W tadlangcode_parser_getlangcode(tadlangcode_parser_t *parser, tadlangcode *langcode) | |
79 | +{ | |
80 | + if (parser->state != TADLANGCODE_PARSER_STATE_DETERMINED) { | |
81 | + return -1; /* TODO */ | |
82 | + } | |
83 | + langcode->iter = parser->iter; | |
84 | + langcode->code = parser->code; | |
85 | + return 0; | |
86 | +} | |
87 | + | |
88 | +EXPORT VOID tadlangcode_parser_initialize(tadlangcode_parser_t *parser) | |
89 | +{ | |
90 | + parser->state = TADLANGCODE_PARSER_STATE_START; | |
91 | + parser->iter = 0; | |
92 | + parser->code = 0; | |
93 | +} | |
94 | + | |
95 | +EXPORT VOID tadlangcode_parser_finalize(tadlangcode_parser_t *parser) | |
96 | +{ | |
97 | +} | |
98 | + | |
99 | +EXPORT W tadlangcodetoTC(tadlangcode *src, TC *dest, W dest_len) | |
100 | +{ | |
101 | + W i, req_len; | |
102 | + | |
103 | + req_len = 2 + (src->iter+1)/2*2; | |
104 | + | |
105 | + if (dest == NULL || dest_len < 0) { | |
106 | + return req_len / sizeof(TC); | |
107 | + } | |
108 | + | |
109 | + for (i = 0; i < (src->iter + 1) / 2; i++) { | |
110 | + dest[i] = 0xFEFE; | |
111 | + } | |
112 | + if (src->iter % 2 == 1) { | |
113 | + dest[i] = src->code; | |
114 | + } else { | |
115 | + dest[i] = 0xFE00 | src->code; | |
116 | + } | |
117 | + | |
118 | + return req_len / sizeof(TC); | |
119 | +} | |
120 | + | |
121 | +EXPORT W TCtotadlangcode(TC *src, W src_len, tadlangcode *dest) | |
122 | +{ | |
123 | + tadlangcode_parser_t parser; | |
124 | + W i, err; | |
125 | + TADLANGCODE_PARSER_RESULT result; | |
126 | + | |
127 | + tadlangcode_parser_initialize(&parser); | |
128 | + | |
129 | + for (i = 0; i < src_len; i++) { | |
130 | + result = tadlangcode_parser_inputchar(&parser, src[i]); | |
131 | + if (result == TADLANGCODE_PARSER_RESULT_DETERMINED) { | |
132 | + break; | |
133 | + } | |
134 | + } | |
135 | + | |
136 | + err = tadlangcode_parser_getlangcode(&parser, dest); | |
137 | + | |
138 | + tadlangcode_parser_finalize(&parser); | |
139 | + | |
140 | + return err; | |
141 | +} | |
142 | + | |
143 | +EXPORT Bool tadlangcodecmpTC(TC *str, W len, tadlangcode *langcode) | |
144 | +{ | |
145 | + W err; | |
146 | + tadlangcode lang; | |
147 | + | |
148 | + err = TCtotadlangcode(str, len, &lang); | |
149 | + if (err < 0) { | |
150 | + return False; | |
151 | + } | |
152 | + return tadlangcodecmp(&lang, langcode); | |
153 | +} | |
154 | + | |
155 | +EXPORT Bool tadlangcodecmp(tadlangcode *langcode1, tadlangcode *langcode2) | |
156 | +{ | |
157 | + if ((langcode1->iter == langcode2->iter)&&(langcode1->code == langcode2->code)) { | |
158 | + return True; | |
159 | + } | |
160 | + return False; | |
161 | +} |
@@ -0,0 +1,75 @@ | ||
1 | +/* | |
2 | + * tadlangcode.h | |
3 | + * | |
4 | + * Copyright (c) 2013 project bchan | |
5 | + * | |
6 | + * This software is provided 'as-is', without any express or implied | |
7 | + * warranty. In no event will the authors be held liable for any damages | |
8 | + * arising from the use of this software. | |
9 | + * | |
10 | + * Permission is granted to anyone to use this software for any purpose, | |
11 | + * including commercial applications, and to alter it and redistribute it | |
12 | + * freely, subject to the following restrictions: | |
13 | + * | |
14 | + * 1. The origin of this software must not be misrepresented; you must not | |
15 | + * claim that you wrote the original software. If you use this software | |
16 | + * in a product, an acknowledgment in the product documentation would be | |
17 | + * appreciated but is not required. | |
18 | + * | |
19 | + * 2. Altered source versions must be plainly marked as such, and must not be | |
20 | + * misrepresented as being the original software. | |
21 | + * | |
22 | + * 3. This notice may not be removed or altered from any source | |
23 | + * distribution. | |
24 | + * | |
25 | + */ | |
26 | + | |
27 | +/* Vendor name: */ | |
28 | +/* Functionality name: tadlangcode */ | |
29 | +/* Detail name: */ | |
30 | + | |
31 | +#include <basic.h> | |
32 | + | |
33 | +#ifndef __TADLANGCODE_H__ | |
34 | +#define __TADLANGCODE_H__ | |
35 | + | |
36 | +struct tadlangcode_ { | |
37 | + UW iter; | |
38 | + UB code; | |
39 | +}; | |
40 | +typedef struct tadlangcode_ tadlangcode; | |
41 | + | |
42 | +IMPORT W tadlangcodetoTC(tadlangcode *src, TC *dest, W dest_len); | |
43 | +IMPORT W TCtotadlangcode(TC *src, W src_len, tadlangcode *dest); | |
44 | +IMPORT Bool tadlangcodecmpTC(TC *str, W len, tadlangcode *langcode); | |
45 | +IMPORT Bool tadlangcodecmp(tadlangcode *langcode1, tadlangcode *langcode2); | |
46 | + | |
47 | +/* Functionality name: tadlangcode */ | |
48 | +/* Detail name: parser */ | |
49 | +struct tadlangcode_parser_t_ { | |
50 | + enum { | |
51 | + TADLANGCODE_PARSER_STATE_START, | |
52 | + TADLANGCODE_PARSER_STATE_LANGCODE, | |
53 | + TADLANGCODE_PARSER_STATE_DETERMINED, | |
54 | + } state; | |
55 | + UW iter; | |
56 | + UB code; | |
57 | +}; | |
58 | +typedef struct tadlangcode_parser_t_ tadlangcode_parser_t; | |
59 | + | |
60 | +/* Functionality name: tadlangcode */ | |
61 | +/* Detail name: parser */ | |
62 | +/* Data structure identifier: result */ | |
63 | +enum TADLANGCODE_PARSER_RESULT_ { | |
64 | + TADLANGCODE_PARSER_RESULT_CONTINUE, | |
65 | + TADLANGCODE_PARSER_RESULT_INVALID, | |
66 | + TADLANGCODE_PARSER_RESULT_DETERMINED, | |
67 | +}; | |
68 | +typedef enum TADLANGCODE_PARSER_RESULT_ TADLANGCODE_PARSER_RESULT; | |
69 | + | |
70 | +IMPORT VOID tadlangcode_parser_initialize(tadlangcode_parser_t *parser); | |
71 | +IMPORT VOID tadlangcode_parser_finalize(tadlangcode_parser_t *parser); | |
72 | +IMPORT TADLANGCODE_PARSER_RESULT tadlangcode_parser_inputchar(tadlangcode_parser_t *parser, TC ch); | |
73 | +IMPORT W tadlangcode_parser_getlangcode(tadlangcode_parser_t *parser, tadlangcode *langcode); | |
74 | + | |
75 | +#endif |