-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcl_thwords.c
264 lines (244 loc) · 6.84 KB
/
tcl_thwords.c
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
/*
* tcl_thwords.c --
* Thai words related commands implementation.
*
* Copyright (c) 2001 Poonlap Veerathanabutr <[email protected]>
*
* $Id: tcl_thwords.c,v 1.2 2001-10-01 14:39:02 poonlap Exp $
*
*/
#include "thmod.h"
#include<tcl.h>
#include<thai/thailib.h>
#include<thai/thbrk.h>
/*
*---------------------------------------------------------------------
*
* tcl_thword --
* implementation of th_brk + add on.
*
* Results:
* Return list of breakpoints or
* Return the Unicode Obj, including token at breakpoint
*
* Side effects:
* none
*
* Algorithm:
* 1) Get UTF-8 and unicode strings
* 2) convert UTF-8 to tis
* 3) use th_brk to find breakpoints
* 4) create new unicode string and insert token at those breakpoints
* 5) return the result unicode string
*
*----------------------------------------------------------------------
*/
int
tcl_thword( ClientData client_data, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]) {
extern Tcl_Encoding tis620_enc;
int i, j, k, cn, bl, ts, ul, rn, *pos;
/*
i, j, k indexes
cn : break-point count
bl : byte size
ts : token size(number)
ul : unichar length
rn : result(unichar) length, including token
pos : pointer to int for store breakpoint
*/
Tcl_Obj *resultPtr;
thchar_t *ths;
char brkpoint[8];
Tcl_DString ds;
unsigned char *s; /* store UTF-8 */
Tcl_UniChar *token, *u, *r, *p;
Tcl_UniChar b[1];
b[0] = 0x003a; /* choose character : to be default token */
b[1] = 0x0000; /* ending with NULL */
resultPtr = Tcl_GetObjResult(interp);
/* arguments parsing, the part that I hate */
/* Please make it cleverer. */
if( objc >= 2 && objc <= 4){
s = Tcl_GetStringFromObj( objv[1], &bl);
ths = Tcl_UtfToExternalDString( tis620_enc, s, bl, &ds);
pos = (int *)Tcl_Alloc( Tcl_DStringLength(&ds) * sizeof(int));
cn = th_brk( ths, pos, Tcl_DStringLength(&ds));
if( objc == 2){
Tcl_ResetResult( interp);
for(i=0; i<cn; i++){
sprintf( brkpoint, "%d", pos[i]);
Tcl_AppendStringsToObj( resultPtr, brkpoint, " ", (char *)NULL);
}
} else if ( strcmp( Tcl_GetString(objv[2]), "-insert") == 0){
if( objc == 4 ){
token = Tcl_GetUnicode(objv[3]);
} else { /* objc == 3 */
token = b;
}
u = Tcl_GetUnicode( objv[1]);
ul = Tcl_UniCharLen(u);
ts = Tcl_UniCharLen(token);
rn = (ul+(cn*ts));
r = (Tcl_UniChar *)Tcl_Alloc( rn * sizeof(Tcl_UniChar));
/* Should I optimize the following code?, Any idea? */
p = r;
j = 0;
for(i=0; i<ul; i++){
if( i == pos[j]){
for(k=0; k<ts; k++){
*p++ = token[k];
}
if( j != cn -1 ){
j++;
}
}
*p++ = u[i];
}
Tcl_SetUnicodeObj( resultPtr, r, rn);
Tcl_Free((char *)r);
} else {
Tcl_AppendStringsToObj( resultPtr, "bad option \"",
Tcl_GetString(objv[2]),
"\": must be -insert",
(char *)NULL);
return TCL_ERROR;
}
Tcl_Free((char *)pos);
return TCL_OK;
} else {
Tcl_WrongNumArgs( interp, 1, objv,
"thai_string ?-insert? ?token?");
return TCL_ERROR;
}
}
/*
*---------------------------------------------------------------------
*
* tcl_thline --
* cut a long string into lines
* Do something like thword
*
* Results:
* Return list of breakpoints or
* Return the Unicode Obj, including token at breakpoint
*
* Side effects:
* none
*
* Algorithm:
* 1) Get UTF-8 and unicode strings
* 2) convert UTF-8 to tis
* 3) use th_brk to find breakpoints
* 4) create new unicode string and insert token at those breakpoints
* 5) return the result unicode string
*
*----------------------------------------------------------------------
*/
int
tcl_thline( ClientData client_data, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]) {
extern Tcl_Encoding tis620_enc;
int i, j, k, cn, bl, ts, ul, rn, *pos, lineLength, charCount, lineBreak_n, *lineBreak;
/*
i, j, k : indexes
cn : break-point count
bl : byte size(length)
ts : token size(number)
ul : unichar length
rn : result(unichar) length, including token
pos : pointer to int for store breakpoint
lineLength : number of character for one line
charCount : counting characters
lineBreak_n : number of breaked line
lineBreak : list of line break points
*/
Tcl_Obj *resultPtr;
thchar_t *ths;
char brkpoint[8];
Tcl_DString ds;
unsigned char *s; /* store UTF-8 */
Tcl_UniChar *token, *u, *r, *p;
Tcl_UniChar b[1];
b[0] = 0x000a; /* choose character \n to be default token */
b[1] = 0x0000; /* ending with NULL */
resultPtr = Tcl_GetObjResult(interp);
if( objc >= 3 && objc <= 5){
s = Tcl_GetStringFromObj( objv[1], &bl);
if( Tcl_GetIntFromObj( interp, objv[2], &lineLength) == TCL_OK){
if( lineLength <= 0 ){
Tcl_AppendStringsToObj( resultPtr,
Tcl_GetString(objv[2]),
": the value should be greater than 0",
(char *)NULL);
return TCL_ERROR;
}
} else {
return TCL_ERROR;
}
ths = Tcl_UtfToExternalDString( tis620_enc, s, bl, &ds);
pos = (int *)Tcl_Alloc( Tcl_DStringLength(&ds) * sizeof(int));
cn = th_brk( ths, pos, Tcl_DStringLength(&ds));
lineBreak = (int *)Tcl_Alloc( ((int)(Tcl_DStringLength(&ds)/(float)lineLength)) * sizeof(int));
charCount = lineLength;
j=0;
for( i=0; i<cn; i++){
if( pos[i] > charCount ){
charCount += lineLength;
lineBreak[j] = pos[i];
j++;
}
}
lineBreak_n = j;
Tcl_Free((char *)pos);
if( objc == 3){
for(i=0; i<lineBreak_n; i++){
sprintf( brkpoint, "%d", lineBreak[i]);
Tcl_AppendStringsToObj( resultPtr,
brkpoint,
" ",
(char *)NULL);
}
Tcl_Free((char *)lineBreak);
return TCL_OK;
} else if( strcmp( Tcl_GetString(objv[3]), "-insert") == 0){
if( objc == 5 ){
token = Tcl_GetUnicode( objv[4]);
} else {
token = b;
}
u = Tcl_GetUnicode( objv[1]);
ul = Tcl_UniCharLen(u);
ts = Tcl_UniCharLen(token);
rn = (ul+(lineBreak_n*ts));
r = (Tcl_UniChar *)Tcl_Alloc( rn * sizeof(Tcl_UniChar));
p = r;
j = 0;
for(i=0; i<ul; i++){
if( i == lineBreak[j]){
for(k=0; k<ts; k++){
*p++ = token[k];
}
if( j != lineBreak_n -1 ){
j++;
}
}
*p++ = u[i];
}
Tcl_SetUnicodeObj( resultPtr, r, rn);
Tcl_Free((char *)r);
} else {
Tcl_AppendStringsToObj( resultPtr, "bad option \"",
Tcl_GetString(objv[3]),
"\": must be -insert",
(char *)NULL);
return TCL_ERROR;
}
Tcl_Free((char *)lineBreak);
return TCL_OK;
} else {
Tcl_WrongNumArgs( interp, 1, objv,
"thai_string line_length ?-insert? ?token?");
return TCL_ERROR;
}
}