• R/O
  • SSH
  • HTTPS

bchan: 提交


Commit MetaInfo

修訂571 (tree)
時間2013-11-16 03:26:36
作者ornse01

Log Message

add handling tad segments array structure.

Change Summary

差異

--- bchanf/trunk/src/tad/tadfragment.h (nonexistent)
+++ bchanf/trunk/src/tad/tadfragment.h (revision 571)
@@ -0,0 +1,87 @@
1+/*
2+ * tadfragment.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: tadfragment */
29+/* Detail name: */
30+
31+#include <basic.h>
32+#include <btron/dp.h>
33+#include <tad.h>
34+
35+#include <coll/bytearray.h>
36+#include <coll/wordarray.h>
37+
38+#ifndef __TADFRAGMENT_H__
39+#define __TADFRAGMENT_H__
40+
41+/* Functionality name: tadfragment */
42+/* Detail name: */
43+struct tadfragment_t_ {
44+ bytearray_t rawdata;
45+ wordarray_t pos;
46+};
47+typedef struct tadfragment_t_ tadfragment_t;
48+
49+/* Functionality name: tadfragment */
50+/* Detail name: cursor */
51+struct tadfragment_cursor_t_ {
52+ tadfragment_t *target;
53+ wordarray_cursor_t base;
54+};
55+typedef struct tadfragment_cursor_t_ tadfragment_cursor_t;
56+
57+/* Functionality name: tadfragment */
58+/* Detail name: cursor */
59+/* Data structure identifier: segment */
60+struct tadfragment_cursor_segment_ {
61+ enum TADFRAGMENT_CURSOR_SEGMENTTYPE {
62+ TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE,
63+ TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR,
64+ TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE,
65+ } type;
66+ UB *p;
67+ W len;
68+};
69+typedef struct tadfragment_cursor_segment_ tadfragment_cursor_segment;
70+
71+IMPORT W tadfragment_initialize(tadfragment_t *fragment);
72+IMPORT VOID tadfragment_finalize(tadfragment_t *fragment);
73+IMPORT UB* tadfragment_getbuffer(tadfragment_t *fragment);
74+IMPORT W tadfragment_getsegmentlength(tadfragment_t *fragment);
75+IMPORT W tadfragment_getbufferlength(tadfragment_t *fragment);
76+IMPORT W tadfragment_pushback(tadfragment_t *fragment, UB *data, W len);
77+IMPORT W tadfragment_popback(tadfragment_t *fragment);
78+
79+IMPORT VOID tadfragment_cursor_initialize(tadfragment_cursor_t *cursor, tadfragment_t *fragment);
80+IMPORT VOID tadfragment_cursor_finalize(tadfragment_cursor_t *cursor);
81+IMPORT W tadfragment_cursor_move(tadfragment_cursor_t *cursor, W diff);
82+IMPORT W tadfragment_cursor_erase(tadfragment_cursor_t *cursor, W len);
83+IMPORT W tadfragment_cursor_insert(tadfragment_cursor_t *cursor, UB *data, W len);
84+IMPORT Bool tadfragment_cursor_isend(tadfragment_cursor_t *cursor);
85+IMPORT W tadfragment_cursor_getdata(tadfragment_cursor_t *cursor, tadfragment_cursor_segment *p);
86+
87+#endif
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
--- bchanf/trunk/src/tad/test_tadfragment.c (nonexistent)
+++ bchanf/trunk/src/tad/test_tadfragment.c (revision 571)
@@ -0,0 +1,917 @@
1+/*
2+ * test_taditerator.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 "tadfragment.h"
30+
31+#include <basic.h>
32+#include <bstdio.h>
33+#include <bstdlib.h>
34+#include <tcode.h>
35+
36+#include <unittest_driver.h>
37+
38+typedef struct {
39+ TC *input;
40+ W input_len;
41+ W segment_num;
42+} test_tadfragment_pushback_t;
43+
44+LOCAL UNITTEST_RESULT test_tadfragment_pushback_common(test_tadfragment_pushback_t *testdata)
45+{
46+ tadfragment_t fragment;
47+ W len, err;
48+ UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
49+
50+ err = tadfragment_initialize(&fragment);
51+ if (err < 0) {
52+ return UNITTEST_RESULT_FAIL;
53+ }
54+
55+ err = tadfragment_pushback(&fragment, (UB*)testdata->input, testdata->input_len);
56+ if (err < 0) {
57+ printf("tadfragment_pushback error\n");
58+ result = UNITTEST_RESULT_FAIL;
59+ }
60+
61+ len = tadfragment_getsegmentlength(&fragment);
62+ if (len != testdata->segment_num) {
63+ printf("tadfragment_getsegmentlength fail: expected = %d, result = %d\n", testdata->segment_num, len);
64+ result = UNITTEST_RESULT_FAIL;
65+ }
66+
67+ len = tadfragment_getbufferlength(&fragment);
68+ if (len != testdata->input_len) {
69+ printf("tadfragment_getbufferlength fail: expected = %d, result = %d\n", testdata->input_len, len);
70+ result = UNITTEST_RESULT_FAIL;
71+ }
72+
73+ tadfragment_finalize(&fragment);
74+
75+ return result;
76+}
77+
78+LOCAL UNITTEST_RESULT test_tadfragment_pushback_1()
79+{
80+ TC tad[] = {TK_A, TK_B, TK_C};
81+ W tad_len = sizeof(tad);
82+ W seg_len = 3;
83+ test_tadfragment_pushback_t testdata = {
84+ tad, tad_len, seg_len
85+ };
86+ return test_tadfragment_pushback_common(&testdata);
87+}
88+
89+LOCAL UNITTEST_RESULT test_tadfragment_pushback_2()
90+{
91+ TC tad[] = {TK_A, TK_B, TK_C, TK_D};
92+ W tad_len = sizeof(tad);
93+ W seg_len = 4;
94+ test_tadfragment_pushback_t testdata = {
95+ tad, tad_len, seg_len
96+ };
97+ return test_tadfragment_pushback_common(&testdata);
98+}
99+
100+LOCAL UNITTEST_RESULT test_tadfragment_pushback_3()
101+{
102+ TC tad[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
103+ W tad_len = sizeof(tad);
104+ W seg_len = 3;
105+ test_tadfragment_pushback_t testdata = {
106+ tad, tad_len, seg_len
107+ };
108+ return test_tadfragment_pushback_common(&testdata);
109+}
110+
111+LOCAL UNITTEST_RESULT test_tadfragment_pushback_4()
112+{
113+ TC tad[] = {TK_A, TK_B, 0xFE21};
114+ W tad_len = sizeof(tad);
115+ W seg_len = 3;
116+ test_tadfragment_pushback_t testdata = {
117+ tad, tad_len, seg_len
118+ };
119+ return test_tadfragment_pushback_common(&testdata);
120+}
121+
122+LOCAL UNITTEST_RESULT test_tadfragment_pushback_5()
123+{
124+ TC tad[] = {TK_A, TK_B, 0xFE21, TK_D};
125+ W tad_len = sizeof(tad);
126+ W seg_len = 4;
127+ test_tadfragment_pushback_t testdata = {
128+ tad, tad_len, seg_len
129+ };
130+ return test_tadfragment_pushback_common(&testdata);
131+}
132+
133+LOCAL UNITTEST_RESULT test_tadfragment_pushback_6()
134+{
135+ TC tad[] = {TK_A, TK_B, 0xFEFE, 0xFE21};
136+ W tad_len = sizeof(tad);
137+ W seg_len = 3;
138+ test_tadfragment_pushback_t testdata = {
139+ tad, tad_len, seg_len
140+ };
141+ return test_tadfragment_pushback_common(&testdata);
142+}
143+
144+LOCAL UNITTEST_RESULT test_tadfragment_pushback_7()
145+{
146+ TC tad[] = {TK_A, TK_B, 0xFEFE, 0xFE21, TK_C};
147+ W tad_len = sizeof(tad);
148+ W seg_len = 4;
149+ test_tadfragment_pushback_t testdata = {
150+ tad, tad_len, seg_len
151+ };
152+ return test_tadfragment_pushback_common(&testdata);
153+}
154+
155+typedef struct {
156+ TC *input;
157+ W input_len;
158+ W segment_num;
159+ W expected_len;
160+} test_tadfragment_popback_t;
161+
162+LOCAL UNITTEST_RESULT test_tadfragment_popback_common(test_tadfragment_popback_t *testdata)
163+{
164+ tadfragment_t fragment;
165+ W len, err;
166+ UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
167+
168+ err = tadfragment_initialize(&fragment);
169+ if (err < 0) {
170+ return UNITTEST_RESULT_FAIL;
171+ }
172+
173+ err = tadfragment_pushback(&fragment, (UB*)testdata->input, testdata->input_len);
174+ if (err < 0) {
175+ printf("tadfragment_pushback error\n");
176+ result = UNITTEST_RESULT_FAIL;
177+ }
178+ err = tadfragment_popback(&fragment);
179+ if (err < 0) {
180+ printf("tadfragment_popback error\n");
181+ result = UNITTEST_RESULT_FAIL;
182+ }
183+
184+ len = tadfragment_getsegmentlength(&fragment);
185+ if (len != testdata->segment_num) {
186+ printf("tadfragment_getsegmentlength fail: expected = %d, result = %d\n", testdata->segment_num, len);
187+ result = UNITTEST_RESULT_FAIL;
188+ }
189+
190+ len = tadfragment_getbufferlength(&fragment);
191+ if (len != testdata->expected_len) {
192+ printf("tadfragment_getbufferlength fail: expected = %d, result = %d\n", testdata->expected_len, len);
193+ result = UNITTEST_RESULT_FAIL;
194+ }
195+
196+ tadfragment_finalize(&fragment);
197+
198+ return result;
199+}
200+
201+LOCAL UNITTEST_RESULT test_tadfragment_popback_1()
202+{
203+ TC tad[] = {TK_A, TK_B, TK_C};
204+ W tad_len = sizeof(tad);
205+ W expected_len = tad_len - sizeof(TC);
206+ W seg_len = 2;
207+ test_tadfragment_popback_t testdata = {
208+ tad, tad_len, seg_len, expected_len
209+ };
210+ return test_tadfragment_popback_common(&testdata);
211+}
212+
213+LOCAL UNITTEST_RESULT test_tadfragment_popback_2()
214+{
215+ TC tad[] = {TK_A, TK_B, TK_C, TK_D};
216+ W tad_len = sizeof(tad);
217+ W expected_len = tad_len - sizeof(TC);
218+ W seg_len = 3;
219+ test_tadfragment_popback_t testdata = {
220+ tad, tad_len, seg_len, expected_len
221+ };
222+ return test_tadfragment_popback_common(&testdata);
223+}
224+
225+LOCAL UNITTEST_RESULT test_tadfragment_popback_3()
226+{
227+ TC tad[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
228+ W tad_len = sizeof(tad);
229+ W expected_len = tad_len - sizeof(TC);
230+ W seg_len = 2;
231+ test_tadfragment_popback_t testdata = {
232+ tad, tad_len, seg_len, expected_len
233+ };
234+ return test_tadfragment_popback_common(&testdata);
235+}
236+
237+LOCAL UNITTEST_RESULT test_tadfragment_popback_4()
238+{
239+ TC tad[] = {TK_A, TK_B, 0xFF80, 0x0002, 0x0000};
240+ W tad_len = sizeof(tad);
241+ W expected_len = tad_len - sizeof(TC)*3;
242+ W seg_len = 2;
243+ test_tadfragment_popback_t testdata = {
244+ tad, tad_len, seg_len, expected_len
245+ };
246+ return test_tadfragment_popback_common(&testdata);
247+}
248+
249+LOCAL UNITTEST_RESULT test_tadfragment_popback_5()
250+{
251+ TC tad[] = {TK_A, TK_B, 0xFE21};
252+ W tad_len = sizeof(tad);
253+ W expected_len = tad_len - sizeof(TC);
254+ W seg_len = 2;
255+ test_tadfragment_popback_t testdata = {
256+ tad, tad_len, seg_len, expected_len
257+ };
258+ return test_tadfragment_popback_common(&testdata);
259+}
260+
261+LOCAL UNITTEST_RESULT test_tadfragment_popback_6()
262+{
263+ TC tad[] = {TK_A, TK_B, 0xFE21, TK_D};
264+ W tad_len = sizeof(tad);
265+ W expected_len = tad_len - sizeof(TC);
266+ W seg_len = 3;
267+ test_tadfragment_popback_t testdata = {
268+ tad, tad_len, seg_len, expected_len
269+ };
270+ return test_tadfragment_popback_common(&testdata);
271+}
272+
273+LOCAL UNITTEST_RESULT test_tadfragment_popback_7()
274+{
275+ TC tad[] = {TK_A, TK_B, 0xFEFE, 0xFE21};
276+ W tad_len = sizeof(tad);
277+ W expected_len = tad_len - sizeof(TC)*2;
278+ W seg_len = 2;
279+ test_tadfragment_popback_t testdata = {
280+ tad, tad_len, seg_len, expected_len
281+ };
282+ return test_tadfragment_popback_common(&testdata);
283+}
284+
285+LOCAL UNITTEST_RESULT test_tadfragment_popback_8()
286+{
287+ TC tad[] = {TK_A, TK_B, 0xFEFE, 0xFE21, TK_C};
288+ W tad_len = sizeof(tad);
289+ W expected_len = tad_len - sizeof(TC);
290+ W seg_len = 3;
291+ test_tadfragment_popback_t testdata = {
292+ tad, tad_len, seg_len, expected_len
293+ };
294+ return test_tadfragment_popback_common(&testdata);
295+}
296+
297+typedef struct {
298+ TC *original;
299+ W original_len;
300+ TC *insert;
301+ W insert_len;
302+ W insert_pos;
303+ TC *expected;
304+ W expected_len;
305+ W expected_segment_num;
306+} test_tadfragment_cursor_insert_t;
307+
308+LOCAL UNITTEST_RESULT test_tadfragment_cursor_insert_common(test_tadfragment_cursor_insert_t *testdata)
309+{
310+ tadfragment_t fragment;
311+ tadfragment_cursor_t cursor;
312+ W len, err;
313+ UB *buf;
314+ UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
315+
316+ err = tadfragment_initialize(&fragment);
317+ if (err < 0) {
318+ return UNITTEST_RESULT_FAIL;
319+ }
320+
321+ err = tadfragment_pushback(&fragment, (UB*)testdata->original, testdata->original_len);
322+ if (err < 0) {
323+ printf("tadfragment_pushback error\n");
324+ result = UNITTEST_RESULT_FAIL;
325+ }
326+
327+ tadfragment_cursor_initialize(&cursor, &fragment);
328+
329+ err = tadfragment_cursor_move(&cursor, testdata->insert_pos);
330+ if (err < 0) {
331+ printf("tadfragment_cursor_move error\n");
332+ result = UNITTEST_RESULT_FAIL;
333+ }
334+ err = tadfragment_cursor_insert(&cursor, (UB*)testdata->insert, testdata->insert_len);
335+ if (err < 0) {
336+ printf("tadfragment_cursor_insert error\n");
337+ result = UNITTEST_RESULT_FAIL;
338+ }
339+
340+ tadfragment_cursor_finalize(&cursor);
341+
342+ len = tadfragment_getsegmentlength(&fragment);
343+ if (len != testdata->expected_segment_num) {
344+ printf("tadfragment_getsegmentlength fail: expected = %d, result = %d\n", testdata->expected_segment_num, len);
345+ result = UNITTEST_RESULT_FAIL;
346+ }
347+
348+ len = tadfragment_getbufferlength(&fragment);
349+ if (len != testdata->expected_len) {
350+ printf("tadfragment_getbufferlength fail: expected = %d, result = %d\n", testdata->expected_len, len);
351+ result = UNITTEST_RESULT_FAIL;
352+ }
353+
354+ buf = tadfragment_getbuffer(&fragment);
355+ if (memcmp(buf, testdata->expected, testdata->expected_len) != 0) {
356+ printf("tadfragment_getbuffer fail\n");
357+ {
358+ W i;
359+ for (i = 0; i < testdata->expected_len; i++) {
360+ printf("%02x, %02x\n", buf[i], ((UB*)testdata->expected)[i]);
361+ }
362+ }
363+ result = UNITTEST_RESULT_FAIL;
364+ }
365+
366+ tadfragment_finalize(&fragment);
367+
368+ return result;
369+}
370+
371+LOCAL UNITTEST_RESULT test_tadfragment_cursor_insert_1()
372+{
373+ TC original[] = {TK_A, TK_B, TK_C};
374+ W original_len = sizeof(original);
375+ TC insert[] = {TK_D, TK_E};
376+ W insert_len = sizeof(insert);
377+ W insert_pos = 0;
378+ TC expected[] = {TK_D, TK_E, TK_A, TK_B, TK_C};
379+ W expected_len = sizeof(expected);
380+ W expected_segment_num = 5;
381+ test_tadfragment_cursor_insert_t testdata = {
382+ original, original_len,
383+ insert, insert_len, insert_pos,
384+ expected, expected_len, expected_segment_num
385+ };
386+ return test_tadfragment_cursor_insert_common(&testdata);
387+}
388+
389+LOCAL UNITTEST_RESULT test_tadfragment_cursor_insert_2()
390+{
391+ TC original[] = {TK_A, TK_B, TK_C};
392+ W original_len = sizeof(original);
393+ TC insert[] = {TK_D, TK_E};
394+ W insert_len = sizeof(insert);
395+ W insert_pos = 1;
396+ TC expected[] = {TK_A, TK_D, TK_E, TK_B, TK_C};
397+ W expected_len = sizeof(expected);
398+ W expected_segment_num = 5;
399+ test_tadfragment_cursor_insert_t testdata = {
400+ original, original_len,
401+ insert, insert_len, insert_pos,
402+ expected, expected_len, expected_segment_num
403+ };
404+ return test_tadfragment_cursor_insert_common(&testdata);
405+}
406+
407+LOCAL UNITTEST_RESULT test_tadfragment_cursor_insert_3()
408+{
409+ TC original[] = {TK_A, TK_B, TK_C};
410+ W original_len = sizeof(original);
411+ TC insert[] = {TK_D, TK_E};
412+ W insert_len = sizeof(insert);
413+ W insert_pos = 2;
414+ TC expected[] = {TK_A, TK_B, TK_D, TK_E, TK_C};
415+ W expected_len = sizeof(expected);
416+ W expected_segment_num = 5;
417+ test_tadfragment_cursor_insert_t testdata = {
418+ original, original_len,
419+ insert, insert_len, insert_pos,
420+ expected, expected_len, expected_segment_num
421+ };
422+ return test_tadfragment_cursor_insert_common(&testdata);
423+}
424+
425+LOCAL UNITTEST_RESULT test_tadfragment_cursor_insert_4()
426+{
427+ TC original[] = {TK_A, TK_B, TK_C};
428+ W original_len = sizeof(original);
429+ TC insert[] = {TK_D, TK_E};
430+ W insert_len = sizeof(insert);
431+ W insert_pos = 3;
432+ TC expected[] = {TK_A, TK_B, TK_C, TK_D, TK_E};
433+ W expected_len = sizeof(expected);
434+ W expected_segment_num = 5;
435+ test_tadfragment_cursor_insert_t testdata = {
436+ original, original_len,
437+ insert, insert_len, insert_pos,
438+ expected, expected_len, expected_segment_num
439+ };
440+ return test_tadfragment_cursor_insert_common(&testdata);
441+}
442+
443+LOCAL Bool test_tadfragment_common_verify_segment(tadfragment_cursor_segment *segment, tadfragment_cursor_segment *expected)
444+{
445+ Bool result = True;
446+
447+ if (segment->type != expected->type) {
448+ printf("segment type fail: expected = %d, result = %d\n", expected->type, segment->type);
449+ result = False;
450+ }
451+ if (segment->len != expected->len) {
452+ printf("segment length fail: expected = %d, result = %d\n", expected->len, segment->len);
453+ result = False;
454+ }
455+ if (memcmp(segment->p, expected->p, segment->len) != 0) {
456+ printf("segment data fail\n");
457+ result = False;
458+ }
459+
460+ return result;
461+}
462+
463+LOCAL Bool test_tadfragment_common_verify_segments(tadfragment_cursor_t *cursor, tadfragment_cursor_segment *expected, W expected_len)
464+{
465+ tadfragment_cursor_segment segment;
466+ W i, err;
467+ Bool result = True, verify;
468+
469+ for (i = 0;; i++) {
470+ err = tadfragment_cursor_getdata(cursor, &segment);
471+ if (i < expected_len) {
472+ if (err < 0) {
473+ printf("tadfragment_pushback error\n");
474+ result = False;
475+ }
476+
477+ verify = test_tadfragment_common_verify_segment(&segment, expected + i);
478+ if (verify == False) {
479+ printf("segment %d is not expected\n", i);
480+ result = False;
481+ }
482+ }
483+
484+ err = tadfragment_cursor_move(cursor, 1);
485+ if (err < 0) {
486+ break;
487+ }
488+ }
489+ if (i != expected_len) {
490+ printf("tadfragment_cursor_move time fail: expected = %d, result = %d\n", expected_len, i);
491+ result = False;
492+ }
493+
494+ return result;
495+}
496+
497+typedef struct {
498+ TC *input;
499+ W input_len;
500+ tadfragment_cursor_segment *expected;
501+ W expected_len;
502+} test_tadfragment_cursor_getdata_t;
503+
504+LOCAL UNITTEST_RESULT test_tadfragment_cursor_getdata_common(test_tadfragment_cursor_getdata_t *testdata)
505+{
506+ tadfragment_t fragment;
507+ tadfragment_cursor_t cursor;
508+ W err;
509+ Bool verify;
510+ UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
511+
512+ err = tadfragment_initialize(&fragment);
513+ if (err < 0) {
514+ return UNITTEST_RESULT_FAIL;
515+ }
516+
517+ err = tadfragment_pushback(&fragment, (UB*)testdata->input, testdata->input_len);
518+ if (err < 0) {
519+ printf("tadfragment_pushback error\n");
520+ result = UNITTEST_RESULT_FAIL;
521+ }
522+
523+ tadfragment_cursor_initialize(&cursor, &fragment);
524+
525+ verify = test_tadfragment_common_verify_segments(&cursor, testdata->expected, testdata->expected_len);
526+ if (verify == False) {
527+ result = UNITTEST_RESULT_FAIL;
528+ }
529+
530+ tadfragment_cursor_finalize(&cursor);
531+
532+ tadfragment_finalize(&fragment);
533+
534+ return result;
535+}
536+
537+LOCAL UNITTEST_RESULT test_tadfragment_cursor_getdata_1()
538+{
539+ TC input[] = {TK_A, TK_B, TK_C};
540+ W input_len = sizeof(input);
541+ tadfragment_cursor_segment expected[] = {
542+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
543+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_B}, 2},
544+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_C}, 2},
545+ };
546+ W expected_len = 3;
547+ test_tadfragment_cursor_getdata_t testdata = {
548+ input, input_len,
549+ expected, expected_len
550+ };
551+ return test_tadfragment_cursor_getdata_common(&testdata);
552+}
553+
554+LOCAL UNITTEST_RESULT test_tadfragment_cursor_getdata_2()
555+{
556+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
557+ W input_len = sizeof(input);
558+ tadfragment_cursor_segment expected[] = {
559+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
560+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB*)(TC[]){0xFF80, 0x0002, 0x0000}, 6},
561+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_C}, 2},
562+ };
563+ W expected_len = 3;
564+ test_tadfragment_cursor_getdata_t testdata = {
565+ input, input_len,
566+ expected, expected_len
567+ };
568+ return test_tadfragment_cursor_getdata_common(&testdata);
569+}
570+
571+LOCAL UNITTEST_RESULT test_tadfragment_cursor_getdata_3()
572+{
573+ TC input[] = {TK_A, TK_B, 0xFE21};
574+ W input_len = sizeof(input);
575+ tadfragment_cursor_segment expected[] = {
576+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
577+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_B}, 2},
578+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB*)(TC[]){0xFE21}, 2},
579+ };
580+ W expected_len = 3;
581+ test_tadfragment_cursor_getdata_t testdata = {
582+ input, input_len,
583+ expected, expected_len
584+ };
585+ return test_tadfragment_cursor_getdata_common(&testdata);
586+}
587+
588+LOCAL UNITTEST_RESULT test_tadfragment_cursor_getdata_4()
589+{
590+ TC input[] = {TK_A, 0xFEFE, 0xFE21, TK_C};
591+ W input_len = sizeof(input);
592+ tadfragment_cursor_segment expected[] = {
593+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
594+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE, (UB*)(TC[]){0xFEFE, 0xFE21}, 4},
595+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_C}, 2},
596+ };
597+ W expected_len = 3;
598+ test_tadfragment_cursor_getdata_t testdata = {
599+ input, input_len,
600+ expected, expected_len
601+ };
602+ return test_tadfragment_cursor_getdata_common(&testdata);
603+}
604+
605+typedef struct {
606+ TC *input;
607+ W input_len;
608+ W erase_pos;
609+ W erase_len;
610+ Bool expected_pos_after_erase_is_end;
611+ tadfragment_cursor_segment *expected_pos_after_erase;
612+ tadfragment_cursor_segment *expected;
613+ W expected_len;
614+} test_tadfragment_cursor_erase_t;
615+
616+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_common(test_tadfragment_cursor_erase_t *testdata)
617+{
618+ tadfragment_t fragment;
619+ tadfragment_cursor_t cursor;
620+ tadfragment_cursor_segment segment;
621+ W err;
622+ Bool verify;
623+ UNITTEST_RESULT result = UNITTEST_RESULT_PASS;
624+
625+ err = tadfragment_initialize(&fragment);
626+ if (err < 0) {
627+ return UNITTEST_RESULT_FAIL;
628+ }
629+
630+ err = tadfragment_pushback(&fragment, (UB*)testdata->input, testdata->input_len);
631+ if (err < 0) {
632+ printf("tadfragment_pushback error\n");
633+ result = UNITTEST_RESULT_FAIL;
634+ }
635+
636+ tadfragment_cursor_initialize(&cursor, &fragment);
637+ tadfragment_cursor_move(&cursor, testdata->erase_pos);
638+ err = tadfragment_cursor_erase(&cursor, testdata->erase_len);
639+ if (err < 0) {
640+ printf("tadfragment_erase error\n");
641+ result = UNITTEST_RESULT_FAIL;
642+ }
643+ if (testdata->expected_pos_after_erase_is_end != False) {
644+ if (tadfragment_cursor_isend(&cursor) == False) {
645+ printf("tadfragment_cursor is not end error\n");
646+ result = UNITTEST_RESULT_FAIL;
647+ }
648+ } else {
649+ err = tadfragment_cursor_getdata(&cursor, &segment);
650+ if (err < 0) {
651+ printf("tadfragment_cursor_getdata error\n");
652+ result = UNITTEST_RESULT_FAIL;
653+ }
654+ verify = test_tadfragment_common_verify_segment(&segment, testdata->expected_pos_after_erase);
655+ if (verify == False) {
656+ printf("tadfragment_cursor_erase position error\n");
657+ result = UNITTEST_RESULT_FAIL;
658+ }
659+ }
660+ tadfragment_cursor_finalize(&cursor);
661+
662+
663+ tadfragment_cursor_initialize(&cursor, &fragment);
664+ if (test_tadfragment_common_verify_segments(&cursor, testdata->expected, testdata->expected_len) == False) {
665+ result = UNITTEST_RESULT_FAIL;
666+ }
667+ tadfragment_cursor_finalize(&cursor);
668+
669+ tadfragment_finalize(&fragment);
670+
671+ return result;
672+}
673+
674+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_1()
675+{
676+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
677+ W input_len = sizeof(input);
678+ Bool expected_pos_after_erase_is_end = False;
679+ tadfragment_cursor_segment expected_pos_after_erase = {
680+ TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE,
681+ (UB*)(TC[]){0xFF80, 0x0002, 0x0000},
682+ 6
683+ };
684+ W erase_pos = 0;
685+ W erase_len = 1;
686+ tadfragment_cursor_segment expected[] = {
687+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB*)(TC[]){0xFF80, 0x0002, 0x0000}, 6},
688+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_C}, 2},
689+ };
690+ W expected_len = 2;
691+ test_tadfragment_cursor_erase_t testdata = {
692+ input, input_len,
693+ erase_pos, erase_len,
694+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
695+ expected, expected_len
696+ };
697+ return test_tadfragment_cursor_erase_common(&testdata);
698+}
699+
700+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_2()
701+{
702+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
703+ W input_len = sizeof(input);
704+ W erase_pos = 0;
705+ W erase_len = 2;
706+ Bool expected_pos_after_erase_is_end = False;
707+ tadfragment_cursor_segment expected_pos_after_erase = {
708+ TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR,
709+ (UB*)(TC[]){TK_C},
710+ 2
711+ };
712+ tadfragment_cursor_segment expected[] = {
713+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_C}, 2},
714+ };
715+ W expected_len = 1;
716+ test_tadfragment_cursor_erase_t testdata = {
717+ input, input_len,
718+ erase_pos, erase_len,
719+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
720+ expected, expected_len
721+ };
722+ return test_tadfragment_cursor_erase_common(&testdata);
723+}
724+
725+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_3()
726+{
727+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
728+ W input_len = sizeof(input);
729+ W erase_pos = 0;
730+ W erase_len = 3;
731+ Bool expected_pos_after_erase_is_end = True;
732+ tadfragment_cursor_segment expected_pos_after_erase = {
733+ };
734+ tadfragment_cursor_segment expected[] = {
735+ };
736+ W expected_len = 0;
737+ test_tadfragment_cursor_erase_t testdata = {
738+ input, input_len,
739+ erase_pos, erase_len,
740+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
741+ expected, expected_len
742+ };
743+ return test_tadfragment_cursor_erase_common(&testdata);
744+}
745+
746+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_4()
747+{
748+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
749+ W input_len = sizeof(input);
750+ W erase_pos = 0;
751+ W erase_len = 4;
752+ Bool expected_pos_after_erase_is_end = True;
753+ tadfragment_cursor_segment expected_pos_after_erase = {
754+ };
755+ tadfragment_cursor_segment expected[] = {
756+ };
757+ W expected_len = 0;
758+ test_tadfragment_cursor_erase_t testdata = {
759+ input, input_len,
760+ erase_pos, erase_len,
761+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
762+ expected, expected_len
763+ };
764+ return test_tadfragment_cursor_erase_common(&testdata);
765+}
766+
767+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_5()
768+{
769+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
770+ W input_len = sizeof(input);
771+ W erase_pos = 1;
772+ W erase_len = 1;
773+ Bool expected_pos_after_erase_is_end = False;
774+ tadfragment_cursor_segment expected_pos_after_erase = {
775+ TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR,
776+ (UB*)(TC[]){TK_C},
777+ 2
778+ };
779+ tadfragment_cursor_segment expected[] = {
780+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
781+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_C}, 2},
782+ };
783+ W expected_len = 2;
784+ test_tadfragment_cursor_erase_t testdata = {
785+ input, input_len,
786+ erase_pos, erase_len,
787+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
788+ expected, expected_len
789+ };
790+ return test_tadfragment_cursor_erase_common(&testdata);
791+}
792+
793+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_6()
794+{
795+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
796+ W input_len = sizeof(input);
797+ W erase_pos = 1;
798+ W erase_len = 2;
799+ Bool expected_pos_after_erase_is_end = True;
800+ tadfragment_cursor_segment expected_pos_after_erase = {
801+ };
802+ tadfragment_cursor_segment expected[] = {
803+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
804+ };
805+ W expected_len = 1;
806+ test_tadfragment_cursor_erase_t testdata = {
807+ input, input_len,
808+ erase_pos, erase_len,
809+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
810+ expected, expected_len
811+ };
812+ return test_tadfragment_cursor_erase_common(&testdata);
813+}
814+
815+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_7()
816+{
817+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
818+ W input_len = sizeof(input);
819+ W erase_pos = 1;
820+ W erase_len = 3;
821+ Bool expected_pos_after_erase_is_end = True;
822+ tadfragment_cursor_segment expected_pos_after_erase = {
823+ };
824+ tadfragment_cursor_segment expected[] = {
825+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
826+ };
827+ W expected_len = 1;
828+ test_tadfragment_cursor_erase_t testdata = {
829+ input, input_len,
830+ erase_pos, erase_len,
831+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
832+ expected, expected_len
833+ };
834+ return test_tadfragment_cursor_erase_common(&testdata);
835+}
836+
837+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_8()
838+{
839+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
840+ W input_len = sizeof(input);
841+ W erase_pos = 2;
842+ W erase_len = 1;
843+ Bool expected_pos_after_erase_is_end = True;
844+ tadfragment_cursor_segment expected_pos_after_erase = {
845+ };
846+ tadfragment_cursor_segment expected[] = {
847+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
848+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB*)(TC[]){0xFF80, 0x0002, 0x0000}, 6},
849+ };
850+ W expected_len = 2;
851+ test_tadfragment_cursor_erase_t testdata = {
852+ input, input_len,
853+ erase_pos, erase_len,
854+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
855+ expected, expected_len
856+ };
857+ return test_tadfragment_cursor_erase_common(&testdata);
858+}
859+
860+LOCAL UNITTEST_RESULT test_tadfragment_cursor_erase_9()
861+{
862+ TC input[] = {TK_A, 0xFF80, 0x0002, 0x0000, TK_C};
863+ W input_len = sizeof(input);
864+ W erase_pos = 2;
865+ W erase_len = 2;
866+ Bool expected_pos_after_erase_is_end = True;
867+ tadfragment_cursor_segment expected_pos_after_erase = {
868+ };
869+ tadfragment_cursor_segment expected[] = {
870+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR, (UB*)(TC[]){TK_A}, 2},
871+ {TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE, (UB*)(TC[]){0xFF80, 0x0002, 0x0000}, 6},
872+ };
873+ W expected_len = 2;
874+ test_tadfragment_cursor_erase_t testdata = {
875+ input, input_len,
876+ erase_pos, erase_len,
877+ expected_pos_after_erase_is_end, &expected_pos_after_erase,
878+ expected, expected_len
879+ };
880+ return test_tadfragment_cursor_erase_common(&testdata);
881+}
882+
883+EXPORT VOID test_tadfragment_main(unittest_driver_t *driver)
884+{
885+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_pushback_1);
886+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_pushback_2);
887+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_pushback_3);
888+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_pushback_4);
889+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_pushback_5);
890+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_pushback_6);
891+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_pushback_7);
892+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_1);
893+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_2);
894+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_3);
895+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_4);
896+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_5);
897+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_6);
898+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_7);
899+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_popback_8);
900+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_insert_1);
901+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_insert_2);
902+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_insert_3);
903+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_insert_4);
904+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_getdata_1);
905+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_getdata_2);
906+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_getdata_3);
907+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_getdata_4);
908+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_1);
909+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_2);
910+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_3);
911+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_4);
912+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_5);
913+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_6);
914+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_7);
915+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_8);
916+ UNITTEST_DRIVER_REGIST(driver, test_tadfragment_cursor_erase_9);
917+}
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
--- bchanf/trunk/src/tad/tadfragment.c (nonexistent)
+++ bchanf/trunk/src/tad/tadfragment.c (revision 571)
@@ -0,0 +1,501 @@
1+/*
2+ * tadfragment.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 "tadfragment.h"
28+
29+#include <basic.h>
30+#include <bstdio.h>
31+
32+#include <coll/bytearray.h>
33+#include <coll/wordarray.h>
34+#include "tadlexer_le.h"
35+
36+#ifdef BCHAN_CONFIG_DEBUG
37+# define DP(arg) printf arg
38+# define DP_ER(msg, err) printf("%s (%d/%x)\n", msg, err>>16, err)
39+#else
40+# define DP(arg) /**/
41+# define DP_ER(msg, err) /**/
42+#endif
43+
44+#if 0
45+# define DP_STATE(state) printf("%s\n", state)
46+#else
47+# define DP_STATE(state) /**/
48+#endif
49+
50+struct tadfragment_pos_iterator_t_ {
51+ enum {
52+ TADFRAGMENT_POS_ITERATOR_STATE_BEFORE_SEGSTART,
53+ TADFRAGMENT_POS_ITERATOR_STATE_READING_TLANG,
54+ TADFRAGMENT_POS_ITERATOR_STATE_READING,
55+ TADFRAGMENT_POS_ITERATOR_STATE_COMPLETED,
56+ TADFRAGMENT_POS_ITERATOR_STATE_ERROR,
57+ } state;
58+ UB *data;
59+ W len;
60+ W pos;
61+ tadlexer_le_t lexer;
62+ struct {
63+ tadlexer_le_result *result;
64+ W len;
65+ W pos;
66+ } lexer_result_handling;
67+};
68+typedef struct tadfragment_pos_iterator_t_ tadfragment_pos_iterator_t;
69+
70+struct tadfragment_pos_iterator_result_ {
71+ enum {
72+ TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_SEGMENT,
73+ TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_ERROR,
74+ } type;
75+ W index;
76+};
77+typedef struct tadfragment_pos_iterator_result_ tadfragment_pos_iterator_result;
78+
79+LOCAL W tadfragment_pos_iterator_input(tadfragment_pos_iterator_t *iterator, tadlexer_le_result *result, Bool *segstart)
80+{
81+ Bool fixed;
82+ W err = 0;
83+
84+ *segstart = False;
85+
86+ switch (iterator->state) {
87+ case TADFRAGMENT_POS_ITERATOR_STATE_BEFORE_SEGSTART:
88+ DP_STATE("BEFORE_SEGSTART");
89+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_READING;
90+ *segstart = True;
91+ break;
92+ case TADFRAGMENT_POS_ITERATOR_STATE_READING_TLANG:
93+ DP_STATE("READING_TLANG");
94+ if (result->type == TADLEXER_LE_RESULTTYPE_SEGMENT_END) {
95+ fixed = tadlexer_le_result_is_fixedsegment(result);
96+ if (fixed == False) {
97+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_ERROR;
98+ err = -1; /* TODO */
99+ break;
100+ }
101+ if (result->val.ch == 0xFEFE) {
102+ break;
103+ }
104+ if ((result->val.ch & 0xFF00) != 0xFE00) {
105+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_ERROR;
106+ err = -1; /* TODO */
107+ break;
108+ }
109+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_BEFORE_SEGSTART;
110+ }
111+ break;
112+ case TADFRAGMENT_POS_ITERATOR_STATE_READING:
113+ DP_STATE("READING");
114+ if (result->type != TADLEXER_LE_RESULTTYPE_SEGMENT_END) {
115+ break;
116+ }
117+ fixed = tadlexer_le_result_is_fixedsegment(result);
118+ if ((fixed != False)&&(result->val.ch == 0xFEFE)) {
119+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_READING_TLANG;
120+ break;
121+ }
122+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_BEFORE_SEGSTART;
123+ break;
124+ case TADFRAGMENT_POS_ITERATOR_STATE_COMPLETED:
125+ DP_STATE("COMPLETED");
126+ break;
127+ case TADFRAGMENT_POS_ITERATOR_STATE_ERROR:
128+ DP_STATE("ERROR");
129+ err = -1; /* TODO */
130+ break;
131+ }
132+
133+ return err;
134+}
135+
136+LOCAL Bool tadfragment_pos_iterator_next(tadfragment_pos_iterator_t *iterator, tadfragment_pos_iterator_result *result)
137+{
138+ UB ch;
139+ W pos, len, err;
140+ Bool segstart;
141+ tadlexer_le_result *lexer_result;
142+
143+ for (; iterator->lexer_result_handling.pos < iterator->lexer_result_handling.len; ) {
144+ pos = iterator->lexer_result_handling.pos++;
145+ lexer_result = iterator->lexer_result_handling.result + pos;
146+ err = tadfragment_pos_iterator_input(iterator, lexer_result, &segstart);
147+ if (err < 0) {
148+ result->type = TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_ERROR;
149+ return False;
150+ }
151+ if (segstart != False) {
152+ result->type = TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_SEGMENT;
153+ result->index = iterator->pos - 1;
154+ return True;
155+ }
156+ }
157+
158+ for (; iterator->pos < iterator->len;) {
159+ ch = iterator->data[iterator->pos++];
160+ tadlexer_le_inputbyte(&iterator->lexer, ch, &lexer_result, &len);
161+ iterator->lexer_result_handling.result = lexer_result;
162+ iterator->lexer_result_handling.len = len;
163+ iterator->lexer_result_handling.pos = 0;
164+
165+ for (; iterator->lexer_result_handling.pos < iterator->lexer_result_handling.len; ) {
166+ pos = iterator->lexer_result_handling.pos++;
167+ lexer_result = iterator->lexer_result_handling.result + pos;
168+ err = tadfragment_pos_iterator_input(iterator, lexer_result, &segstart);
169+ if (err < 0) {
170+ result->type = TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_ERROR;
171+ return False;
172+ }
173+ if (segstart != False) {
174+ result->type = TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_SEGMENT;
175+ result->index = iterator->pos - 1;
176+ return True;
177+ }
178+ }
179+ }
180+
181+ if (iterator->state == TADFRAGMENT_POS_ITERATOR_STATE_BEFORE_SEGSTART) {
182+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_COMPLETED;
183+ result->type = TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_SEGMENT;
184+ result->index = iterator->len;
185+ } else {
186+ result->type = TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_ERROR;
187+ }
188+
189+ return False;
190+}
191+
192+LOCAL VOID tadfragment_pos_iterator_initialize(tadfragment_pos_iterator_t *iterator, UB *data, W len)
193+{
194+ tadlexer_le_initialize(&iterator->lexer);
195+ iterator->state = TADFRAGMENT_POS_ITERATOR_STATE_BEFORE_SEGSTART;
196+ iterator->data = data;
197+ iterator->len = len;
198+ iterator->pos = 0;
199+ iterator->lexer_result_handling.result = NULL;
200+ iterator->lexer_result_handling.len = 0;
201+ iterator->lexer_result_handling.pos = 0;
202+}
203+
204+LOCAL VOID tadfragment_pos_iterator_finalize(tadfragment_pos_iterator_t *iterator)
205+{
206+ tadlexer_le_finalize(&iterator->lexer);
207+}
208+
209+LOCAL Bool tadfragment_verifydata(UB *data, W len)
210+{
211+ tadfragment_pos_iterator_t iterator;
212+ tadfragment_pos_iterator_result result;
213+ Bool cont, ret = True;
214+
215+ tadfragment_pos_iterator_initialize(&iterator, data, len);
216+ for (;;) {
217+ cont = tadfragment_pos_iterator_next(&iterator, &result);
218+ if (cont == False) {
219+ break;
220+ }
221+ if (result.type == TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_SEGMENT) {
222+ } else {
223+ ret = False;
224+ break;
225+ }
226+ }
227+ tadfragment_pos_iterator_finalize(&iterator);
228+
229+ return True;
230+}
231+
232+EXPORT UB* tadfragment_getbuffer(tadfragment_t *fragment)
233+{
234+ return bytearray_getbuffer(&fragment->rawdata);
235+}
236+
237+EXPORT W tadfragment_getsegmentlength(tadfragment_t *fragment)
238+{
239+ return wordarray_getlength(&fragment->pos);
240+}
241+
242+EXPORT W tadfragment_getbufferlength(tadfragment_t *fragment)
243+{
244+ return bytearray_getlength(&fragment->rawdata);
245+}
246+
247+EXPORT W tadfragment_pushback(tadfragment_t *fragment, UB *data, W len)
248+{
249+ tadfragment_cursor_t cursor;
250+ W num, err;
251+
252+ tadfragment_cursor_initialize(&cursor, fragment);
253+
254+ num = tadfragment_getsegmentlength(fragment);
255+ tadfragment_cursor_move(&cursor, num);
256+
257+ err = tadfragment_cursor_insert(&cursor, data, len);
258+
259+ tadfragment_cursor_finalize(&cursor);
260+
261+ return err;
262+}
263+
264+EXPORT W tadfragment_popback(tadfragment_t *fragment)
265+{
266+ W len, val, i, size;
267+
268+ len = wordarray_getlength(&fragment->pos);
269+ if (len == 0) {
270+ return -1; /* TODO */
271+ }
272+
273+ wordarray_getat(&fragment->pos, len-1, &val);
274+ size = bytearray_getlength(&fragment->rawdata);
275+
276+ for (i = 0; i < size - val; i++) {
277+ bytearray_popback(&fragment->rawdata);
278+ }
279+ wordarray_popback(&fragment->pos);
280+
281+ return 0;
282+}
283+
284+EXPORT W tadfragment_initialize(tadfragment_t *fragment)
285+{
286+ W err;
287+
288+ err = bytearray_initialize(&fragment->rawdata);
289+ if (err < 0) {
290+ return err;
291+ }
292+ err = wordarray_initialize(&fragment->pos);
293+ if (err < 0) {
294+ bytearray_finalize(&fragment->rawdata);
295+ return err;
296+ }
297+
298+ return 0;
299+}
300+
301+EXPORT VOID tadfragment_finalize(tadfragment_t *fragment)
302+{
303+ wordarray_finalize(&fragment->pos);
304+ bytearray_finalize(&fragment->rawdata);
305+}
306+
307+EXPORT W tadfragment_cursor_move(tadfragment_cursor_t *cursor, W diff)
308+{
309+ return wordarray_cursor_move(&cursor->base, diff);
310+}
311+
312+EXPORT W tadfragment_cursor_erase(tadfragment_cursor_t *cursor, W len)
313+{
314+ bytearray_cursor_t bytecursor;
315+ Bool isend;
316+ W start_pos, end_pos, diff, val, count, err;
317+
318+ if (len < 0) {
319+ return -1; /* TODO */
320+ }
321+
322+ isend = tadfragment_cursor_isend(cursor);
323+ if (isend != False) {
324+ return -1; /* TODO */
325+ }
326+
327+ wordarray_cursor_getW(&cursor->base, &start_pos);
328+ err = tadfragment_cursor_move(cursor, len);
329+ if (err < 0) {
330+ diff = bytearray_getlength(&cursor->target->rawdata) - start_pos;
331+ } else {
332+ wordarray_cursor_getW(&cursor->base, &end_pos);
333+ tadfragment_cursor_move(cursor, -len);
334+
335+ diff = end_pos - start_pos;
336+ }
337+
338+ wordarray_cursor_erase(&cursor->base, len);
339+
340+ bytearray_cursor_initialize(&bytecursor, &cursor->target->rawdata);
341+ bytearray_cursor_move(&bytecursor, start_pos);
342+ bytearray_cursor_erase(&bytecursor, diff);
343+ bytearray_cursor_finalize(&bytecursor);
344+
345+ count = 0;
346+ for (;;) {
347+ err = wordarray_cursor_getW(&cursor->base, &val);
348+ if (err < 0) {
349+ /* TODO: error or end position */
350+ break;
351+ }
352+ wordarray_cursor_setW(&cursor->base, val - diff);
353+ wordarray_cursor_move(&cursor->base, 1);
354+ count++;
355+ }
356+ wordarray_cursor_move(&cursor->base, -count);
357+
358+ return 0;
359+}
360+
361+LOCAL W tadfragment_cursor_getnextpos(tadfragment_cursor_t *cursor, W *pos)
362+{
363+ W err;
364+ Bool isend;
365+
366+ err = tadfragment_cursor_move(cursor, 1);
367+ if (err < 0) {
368+ return err;
369+ }
370+ isend = tadfragment_cursor_isend(cursor);
371+ if (isend != False) {
372+ *pos = bytearray_getlength(&cursor->target->rawdata);
373+ tadfragment_cursor_move(cursor, -1);
374+ return 0;
375+ }
376+
377+ err = wordarray_cursor_getW(&cursor->base, pos);
378+ tadfragment_cursor_move(cursor, -1);
379+
380+ return err;
381+}
382+
383+EXPORT W tadfragment_cursor_insert(tadfragment_cursor_t *cursor, UB *data, W len)
384+{
385+ bytearray_cursor_t bytecursor;
386+ tadfragment_pos_iterator_t iterator;
387+ tadfragment_pos_iterator_result result;
388+ Bool ok, cont, isend;
389+ W offset, val, err, err2;
390+
391+ if (len < 0) {
392+ return -1; /* TODO */
393+ }
394+ if (len == 0) {
395+ return 0;
396+ }
397+
398+ ok = tadfragment_verifydata(data, len);
399+ if (ok == False) {
400+ return -1; /* TODO */
401+ }
402+
403+ isend = tadfragment_cursor_isend(cursor);
404+ if (isend == False) {
405+ err = wordarray_cursor_getW(&cursor->base, &offset);
406+ if (err < 0) {
407+ printf("BBB\n");
408+ return err;
409+ }
410+ } else {
411+ offset = bytearray_getlength(&cursor->target->rawdata);
412+ }
413+
414+ bytearray_cursor_initialize(&bytecursor, &cursor->target->rawdata);
415+ bytearray_cursor_move(&bytecursor, offset);
416+ err = bytearray_cursor_insert(&bytecursor, data, len);
417+ bytearray_cursor_finalize(&bytecursor);
418+ if (err < 0) {
419+ return err;
420+ }
421+
422+ tadfragment_pos_iterator_initialize(&iterator, data, len);
423+ for (;;) {
424+ cont = tadfragment_pos_iterator_next(&iterator, &result);
425+ if (cont == False) {
426+ break;
427+ }
428+ if (result.type == TADFRAGMENT_POS_ITERATOR_RESULT_TYPE_SEGMENT) {
429+ val = result.index + offset;
430+ err = wordarray_cursor_insert(&cursor->base, &val, 1);
431+ if (err < 0) {
432+ break;
433+ }
434+ wordarray_cursor_move(&cursor->base, 1);
435+ }
436+ }
437+ tadfragment_pos_iterator_finalize(&iterator);
438+
439+ for (;;) {
440+ err2 = wordarray_cursor_getW(&cursor->base, &val);
441+ if (err2 < 0) {
442+ /* TODO: error or end position */
443+ break;
444+ }
445+ wordarray_cursor_setW(&cursor->base, val + len);
446+ wordarray_cursor_move(&cursor->base, 1);
447+ }
448+
449+ return err;
450+}
451+
452+EXPORT Bool tadfragment_cursor_isend(tadfragment_cursor_t *cursor)
453+{
454+ return wordarray_cursor_isend(&cursor->base);
455+}
456+
457+EXPORT W tadfragment_cursor_getdata(tadfragment_cursor_t *cursor, tadfragment_cursor_segment *p)
458+{
459+ Bool end;
460+ W offset, offset_next, err;
461+ UH ch;
462+
463+ end = tadfragment_cursor_isend(cursor);
464+ if (end != False) {
465+ return -1; // TODO
466+ }
467+
468+ err = wordarray_cursor_getW(&cursor->base, &offset);
469+ if (err < 0) {
470+ return err;
471+ }
472+ err = tadfragment_cursor_getnextpos(cursor, &offset_next);
473+ if (err < 0) {
474+ return err;
475+ }
476+
477+ p->p = bytearray_getbuffer(&cursor->target->rawdata) + offset;
478+ p->len = offset_next - offset;
479+
480+ ch = *(UH*)p->p;
481+ if ((ch & 0xFF80) == 0xFF80) {
482+ p->type = TADFRAGMENT_CURSOR_SEGMENTTYPE_VARIABLE;
483+ } else if ((ch & 0xFE00) == 0xFE00) {
484+ p->type = TADFRAGMENT_CURSOR_SEGMENTTYPE_LANGCODE;
485+ } else {
486+ p->type = TADFRAGMENT_CURSOR_SEGMENTTYPE_CHAR;
487+ }
488+
489+ return 0;
490+}
491+
492+EXPORT VOID tadfragment_cursor_initialize(tadfragment_cursor_t *cursor, tadfragment_t *fragment)
493+{
494+ cursor->target = fragment;
495+ wordarray_cursor_initialize(&cursor->base, &fragment->pos);
496+}
497+
498+EXPORT VOID tadfragment_cursor_finalize(tadfragment_cursor_t *cursor)
499+{
500+ wordarray_cursor_finalize(&cursor->base);
501+}
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Show on old repository browser