1 /* Test driver for thbrk
2 */
3
4 #define MAXLINELENGTH 1000
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <thai/thbrk.h>
10
11 /* run with "-i" argument to get the interactive version
12 otherwise it will run the self test and exit */
13
main(int argc,char * argv[])14 int main (int argc, char* argv[])
15 {
16 thchar_t str[MAXLINELENGTH];
17 thchar_t out[MAXLINELENGTH*6+1];
18 int pos[MAXLINELENGTH];
19 int outputLength;
20 int numCut, i;
21 int interactive = 0;
22 ThBrk *brk;
23
24 if (argc >= 2) {
25 if (0 == strcmp (argv[1], "-i"))
26 interactive = 1;
27 }
28
29 brk = th_brk_new (NULL);
30 if (!brk) {
31 printf ("Unable to create word breaker!\n");
32 exit (-1);
33 }
34
35 if (interactive) {
36 while (!feof (stdin)) {
37 printf ("Please enter thai words/sentences: ");
38 if (!fgets ((char *)str, MAXLINELENGTH-1, stdin)) {
39 numCut = th_brk_find_breaks (brk, str, pos, MAXLINELENGTH);
40 printf ("Total %d cut points.", numCut);
41 if (numCut > 0) {
42 printf ("Cut points list: %d", pos[0]);
43 for (i = 1; i < numCut; i++) {
44 printf(", %d", pos[i]);
45 }
46 }
47 printf("\n");
48 outputLength = th_brk_insert_breaks (brk, str, out, sizeof out,
49 "<WBR>");
50 printf ("Output string length is %d\n", outputLength-1); /* the penultimate is \n */
51 printf ("Output string is %s", out);
52 printf("***********************************************************************\n");
53 }
54 }
55 } else {
56 strcpy ((char *)str, "���ʴդ�Ѻ ��.���. ����繡�÷��ͺ����ͧ");
57 printf ("Testing with string: %s\n", str);
58 numCut = th_brk_find_breaks (brk, str, pos, MAXLINELENGTH);
59 printf ("Total %d cut points.", numCut);
60 if (numCut != 7) {
61 printf("Error! should be 7.. test th_brk_find_breaks() failed...\n");
62 exit (-1);
63 }
64
65 printf("Cut points list: %d", pos[0]);
66 for (i = 1; i < numCut; i++) {
67 printf(", %d", pos[i]);
68 }
69 printf("\n");
70 outputLength = th_brk_insert_breaks (brk, str, out, sizeof out, "<WBR>");
71 printf ("Output string is %s\n", out);
72 printf ("Output string length is %d\n", outputLength);
73 if (outputLength != 75) {
74 printf ("Error! should be 75.. test th_brk_insert_breaks() failed...\n");
75 exit (-1);
76 }
77 printf ("*** End of thbrk self test ******\n");
78 }
79
80 th_brk_delete (brk);
81
82 return 0;
83 }
84