int main (int argc, char* argv[]) { thchar_t str[MAXLINELENGTH]; thchar_t out[MAXLINELENGTH*6+1]; int pos[MAXLINELENGTH]; int outputLength; int numCut, i; int interactive = 0; if (argc >= 2) { if (0 == strcmp (argv[1], "-i")) interactive = 1; } if (interactive) { while (!feof (stdin)) { printf ("Please enter thai words/sentences: "); fgets ((char *)str, MAXLINELENGTH-1, stdin); if (!feof (stdin)) { numCut = th_brk (str, pos, MAXLINELENGTH); printf ("Total %d cut points.", numCut); if (numCut > 0) { printf ("Cut points list: %d", pos[0]); for (i = 1; i < numCut; i++) { printf(", %d", pos[i]); } } printf("\n"); outputLength = th_brk_line (str, out, sizeof out, "<WBR>"); printf ("Output string length is %d\n", outputLength-1); /* the penultimate is \n */ printf ("Output string is %s", out); printf("***********************************************************************\n"); } } } else { strcpy ((char *)str, "ÊÇÑÊ´Õ¤ÃѺ ¹Õèà»ç¹¡Ò÷´ÊͺµÑÇàͧ"); printf ("Testing with string: %s\n", str); numCut = th_brk (str, pos, MAXLINELENGTH); printf ("Total %d cut points.", numCut); if (numCut != 6) { printf("Error! should be 6.. test th_brk() failed...\n"); exit (-1); } printf("Cut points list: %d", pos[0]); for (i = 1; i < numCut; i++) { printf(", %d", pos[i]); } printf("\n"); outputLength = th_brk_line (str, out, sizeof out, "<WBR>"); printf ("Output string is %s\n", out); printf ("Output string length is %d\n", outputLength); if (outputLength != 62) { printf ("Error! should be 62.. test th_brk_line() failed...\n"); exit (-1); } printf ("*** End of thbrk self test ******\n"); } return 0; }
static VALUE f_th_brk_line(int argc,VALUE *argv,VALUE obj) { char *cut_code; char *out; size_t out_len; int n; VALUE ret; /* FIXME: Is there any better method to handle this. */ if(argc < 1) { rb_raise(rb_eStandardError,"%s","Invalid argument."); } if(argc < 2) { cut_code="|"; } else { cut_code=RSTRING_PTR(argv[1]); } out_len = RSTRING_LEN(argv[0]) * 2 + 1; out = ALLOC_N(char, out_len); n = th_brk_line((thchar_t *)RSTRING_PTR(argv[0]), (thchar_t *)out, out_len, cut_code); out[n] = '\0'; ret = rb_str_new2(out); free(out); return ret; }
int main (int argc, char* argv[]) { thchar_t str[MAXLINELENGTH]; thwchar_t ustr[MAXLINELENGTH], uout[MAXLINELENGTH], unicodeCutCode[6]; thchar_t out1[MAXLINELENGTH*2+1]; thchar_t out2[MAXLINELENGTH]; int pos[MAXLINELENGTH]; int outputLength, unicodeCutCodeLength; int numCut, i; strcpy ((char *)str, "ÊÇÑÊ´Õ¤ÃѺ ¡Í.ÃÁ¹. ¹Õèà»ç¹¡Ò÷´ÊͺµÑÇàͧ"); printf ("Testing with input string: %s\n", str); printf ("Converting to Unicode...\n"); th_tis2uni_line (str, ustr, MAXLINELENGTH); printf ("Calling th_wbrk()...\n"); numCut = th_wbrk (ustr, pos, MAXLINELENGTH); printf ("Total %d cut points.", numCut); if (numCut > 0) { printf ("Cut points list: %d", pos[0]); for (i = 1; i < numCut; i++) { printf (", %d", pos[i]); } } printf ("\n"); if (numCut != 7) { printf ("Error! Should have 7 cut points.\n Test th_wbrk() failed...\n"); exit (-1); } unicodeCutCodeLength = th_tis2uni_line ((const thchar_t *) "<WBR>", (thwchar_t*) unicodeCutCode, 6); printf ("Calling th_wbrk_line() ....\n"); outputLength = th_wbrk_line (ustr, (thwchar_t*) uout, MAXLINELENGTH, unicodeCutCode); printf ("Return value from th_wbrk_line is %d\n", outputLength); printf ("Output string length is %d\n", wcslen(uout)); if (outputLength != 75) { printf ("Error! Output string length != 75. " "Test th_wbrk_line() failed...\n"); exit (-1); } printf ("Compare with result from th_brk_line()..\n"); th_brk_line (str, out1, MAXLINELENGTH*2+1, "<WBR>"); th_uni2tis_line(uout, out2, MAXLINELENGTH); if (strcmp ((const char *)out1, (const char *)out2) == 0) { printf ("Correct! .. test th_wbrk_line() passed...\n"); } else { printf ("Error! Comparison of results from th_brk_line() " "and th_wbrk_line() failed.\n"); printf ("th_brk_line :\"%s\"\n", out1); printf ("th_wbrk_line:\"%s\"\n", out2); printf ("Test th_wbrk_line() failed...\n"); exit (-1); } return 0; }