| 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 |
#include "cst_tokenstream.h" |
| 86 |
|
| 87 |
const cst_string * const cst_ts_default_whitespacesymbols = " \t\n\r"; |
| 88 |
const cst_string * const cst_ts_default_singlecharsymbols = "(){}[]"; |
| 89 |
const cst_string * const cst_ts_default_prepunctuationsymbols = "\"'`({["; |
| 90 |
const cst_string * const cst_ts_default_postpunctuationsymbols = "\"'`.,:;!?(){}[]"; |
| 91 |
|
| 92 |
#define TS_BUFFER_SIZE 256 |
| 93 |
#define TS_EOF -1 |
| 94 |
|
| 95 |
static cst_string ts_getc(cst_tokenstream *ts); |
| 96 |
|
| 97 |
static void set_charclass_table(cst_tokenstream *ts) |
| 98 |
{ |
| 99 |
int i; |
| 100 |
memset(ts->charclass,0,256); |
| 101 |
|
| 102 |
for (i=0; ts->p_whitespacesymbols[i]; i++) |
| 103 |
ts->charclass[(unsigned char)ts->p_whitespacesymbols[i]] |= TS_CHARCLASS_WHITESPACE; |
| 104 |
for (i=0; ts->p_singlecharsymbols[i]; i++) |
| 105 |
ts->charclass[(unsigned char)ts->p_singlecharsymbols[i]] |= TS_CHARCLASS_SINGLECHAR; |
| 106 |
for (i=0; ts->p_prepunctuationsymbols[i]; i++) |
| 107 |
ts->charclass[(unsigned char)ts->p_prepunctuationsymbols[i]] |= TS_CHARCLASS_PREPUNCT; |
| 108 |
for (i=0; ts->p_postpunctuationsymbols[i]; i++) |
| 109 |
ts->charclass[(unsigned char)ts->p_postpunctuationsymbols[i]]|=TS_CHARCLASS_POSTPUNCT; |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
void set_charclasses(cst_tokenstream *ts, |
| 114 |
const cst_string *whitespace, |
| 115 |
const cst_string *singlecharsymbols, |
| 116 |
const cst_string *prepunctuation, |
| 117 |
const cst_string *postpunctuation) |
| 118 |
{ |
| 119 |
ts->p_whitespacesymbols = |
| 120 |
(whitespace ? whitespace : cst_ts_default_whitespacesymbols); |
| 121 |
ts->p_singlecharsymbols = |
| 122 |
(singlecharsymbols ? singlecharsymbols : cst_ts_default_singlecharsymbols); |
| 123 |
ts->p_prepunctuationsymbols = |
| 124 |
(prepunctuation ? prepunctuation : cst_ts_default_prepunctuationsymbols); |
| 125 |
ts->p_postpunctuationsymbols = |
| 126 |
(postpunctuation ? postpunctuation : cst_ts_default_postpunctuationsymbols); |
| 127 |
|
| 128 |
set_charclass_table(ts); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
static void extend_buffer(cst_string **buffer,int *buffer_max) |
| 133 |
{ |
| 134 |
int new_max; |
| 135 |
cst_string *new_buffer; |
| 136 |
|
| 137 |
new_max = (*buffer_max)+(*buffer_max)/5; |
| 138 |
new_buffer = cst_alloc(cst_string,new_max); |
| 139 |
memmove(new_buffer,*buffer,*buffer_max); |
| 140 |
cst_free(*buffer); |
| 141 |
*buffer = new_buffer; |
| 142 |
*buffer_max = new_max; |
| 143 |
} |
| 144 |
|
| 145 |
static cst_tokenstream *new_tokenstream(const cst_string *whitespace, |
| 146 |
const cst_string *singlechars, |
| 147 |
const cst_string *prepunct, |
| 148 |
const cst_string *postpunct) |
| 149 |
{ |
| 150 |
cst_tokenstream *ts = cst_alloc(cst_tokenstream,1); |
| 151 |
ts->fd = NULL; |
| 152 |
ts->file_pos = 0; |
| 153 |
ts->line_number = 0; |
| 154 |
ts->string_buffer = NULL; |
| 155 |
ts->token_pos = 0; |
| 156 |
ts->whitespace = cst_alloc(cst_string,TS_BUFFER_SIZE); |
| 157 |
ts->ws_max = TS_BUFFER_SIZE; |
| 158 |
if (prepunct && prepunct[0]) |
| 159 |
{ |
| 160 |
ts->prepunctuation = cst_alloc(cst_string,TS_BUFFER_SIZE); |
| 161 |
ts->prep_max = TS_BUFFER_SIZE; |
| 162 |
} |
| 163 |
ts->token = cst_alloc(cst_string,TS_BUFFER_SIZE); |
| 164 |
ts->token_max = TS_BUFFER_SIZE; |
| 165 |
if (postpunct && postpunct[0]) |
| 166 |
{ |
| 167 |
ts->postpunctuation = cst_alloc(cst_string,TS_BUFFER_SIZE); |
| 168 |
ts->postp_max = TS_BUFFER_SIZE; |
| 169 |
} |
| 170 |
|
| 171 |
set_charclasses(ts,whitespace,singlechars,prepunct,postpunct); |
| 172 |
ts->current_char = 0; |
| 173 |
|
| 174 |
return ts; |
| 175 |
} |
| 176 |
|
| 177 |
void delete_tokenstream(cst_tokenstream *ts) |
| 178 |
{ |
| 179 |
cst_free(ts->whitespace); |
| 180 |
cst_free(ts->token); |
| 181 |
if (ts->prepunctuation) cst_free(ts->prepunctuation); |
| 182 |
if (ts->postpunctuation) cst_free(ts->postpunctuation); |
| 183 |
cst_free(ts); |
| 184 |
} |
| 185 |
|
| 186 |
#ifndef FLITE_PLUS_HTS_ENGINE |
| 187 |
cst_tokenstream *ts_open(const char *filename, |
| 188 |
const cst_string *whitespace, |
| 189 |
const cst_string *singlechars, |
| 190 |
const cst_string *prepunct, |
| 191 |
const cst_string *postpunct) |
| 192 |
{ |
| 193 |
cst_tokenstream *ts = new_tokenstream(whitespace, |
| 194 |
singlechars, |
| 195 |
prepunct, |
| 196 |
postpunct); |
| 197 |
|
| 198 |
#ifndef UNDER_CE |
| 199 |
if (cst_streq("-",filename)) |
| 200 |
ts->fd = stdin; |
| 201 |
else |
| 202 |
#endif |
| 203 |
ts->fd = cst_fopen(filename,CST_OPEN_READ|CST_OPEN_BINARY); |
| 204 |
ts_getc(ts); |
| 205 |
|
| 206 |
if (ts->fd == NULL) |
| 207 |
{ |
| 208 |
delete_tokenstream(ts); |
| 209 |
return NULL; |
| 210 |
} |
| 211 |
else |
| 212 |
return ts; |
| 213 |
} |
| 214 |
#endif |
| 215 |
|
| 216 |
cst_tokenstream *ts_open_string(const cst_string *string, |
| 217 |
const cst_string *whitespace, |
| 218 |
const cst_string *singlechars, |
| 219 |
const cst_string *prepunct, |
| 220 |
const cst_string *postpunct) |
| 221 |
{ |
| 222 |
cst_tokenstream *ts = new_tokenstream(whitespace, |
| 223 |
singlechars, |
| 224 |
prepunct, |
| 225 |
postpunct); |
| 226 |
|
| 227 |
ts->string_buffer = cst_strdup(string); |
| 228 |
ts_getc(ts); |
| 229 |
|
| 230 |
return ts; |
| 231 |
} |
| 232 |
|
| 233 |
void ts_close(cst_tokenstream *ts) |
| 234 |
{ |
| 235 |
if (ts->fd != NULL) |
| 236 |
{ |
| 237 |
#ifndef FLITE_PLUS_HTS_ENGINE |
| 238 |
#ifndef UNDER_CE |
| 239 |
if (ts->fd != stdin) |
| 240 |
#endif |
| 241 |
cst_fclose(ts->fd); |
| 242 |
#endif |
| 243 |
ts->fd = NULL; |
| 244 |
} |
| 245 |
if (ts->string_buffer != NULL) |
| 246 |
{ |
| 247 |
cst_free(ts->string_buffer); |
| 248 |
ts->string_buffer = NULL; |
| 249 |
} |
| 250 |
delete_tokenstream(ts); |
| 251 |
} |
| 252 |
|
| 253 |
static void get_token_sub_part(cst_tokenstream *ts, |
| 254 |
int charclass, |
| 255 |
cst_string **buffer, |
| 256 |
int *buffer_max) |
| 257 |
{ |
| 258 |
int p; |
| 259 |
|
| 260 |
for (p=0; ((ts->current_char != TS_EOF) && |
| 261 |
(ts_charclass(ts->current_char,charclass,ts)) && |
| 262 |
(!ts_charclass(ts->current_char, |
| 263 |
TS_CHARCLASS_SINGLECHAR,ts))); p++) |
| 264 |
{ |
| 265 |
if (p >= *buffer_max) extend_buffer(buffer,buffer_max); |
| 266 |
(*buffer)[p] = ts->current_char; |
| 267 |
ts_getc(ts); |
| 268 |
} |
| 269 |
(*buffer)[p] = '\0'; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
static void get_token_sub_part_2(cst_tokenstream *ts, |
| 274 |
int endclass1, |
| 275 |
cst_string **buffer, |
| 276 |
int *buffer_max) |
| 277 |
{ |
| 278 |
int p; |
| 279 |
|
| 280 |
for (p=0; ((ts->current_char != TS_EOF) && |
| 281 |
(!ts_charclass(ts->current_char,endclass1,ts)) && |
| 282 |
(!ts_charclass(ts->current_char, |
| 283 |
TS_CHARCLASS_SINGLECHAR,ts))); |
| 284 |
p++) |
| 285 |
{ |
| 286 |
if (p >= *buffer_max) extend_buffer(buffer,buffer_max); |
| 287 |
(*buffer)[p] = ts->current_char; |
| 288 |
ts_getc(ts); |
| 289 |
} |
| 290 |
(*buffer)[p] = '\0'; |
| 291 |
} |
| 292 |
|
| 293 |
static void get_token_postpunctuation(cst_tokenstream *ts) |
| 294 |
{ |
| 295 |
int p,t; |
| 296 |
|
| 297 |
t = cst_strlen(ts->token); |
| 298 |
for (p=t; |
| 299 |
(p > 0) && |
| 300 |
((ts->token[p] == '\0') || |
| 301 |
(ts_charclass(ts->token[p],TS_CHARCLASS_POSTPUNCT,ts))); |
| 302 |
p--); |
| 303 |
|
| 304 |
if (t != p) |
| 305 |
{ |
| 306 |
if (t-p >= ts->postp_max) |
| 307 |
extend_buffer(&ts->postpunctuation,&ts->postp_max); |
| 308 |
|
| 309 |
memmove(ts->postpunctuation,&ts->token[p+1],(t-p)); |
| 310 |
|
| 311 |
ts->token[p+1] = '\0'; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
int ts_eof(cst_tokenstream *ts) |
| 316 |
{ |
| 317 |
if (ts->current_char == TS_EOF) |
| 318 |
return TRUE; |
| 319 |
else |
| 320 |
return FALSE; |
| 321 |
} |
| 322 |
|
| 323 |
int ts_set_stream_pos(cst_tokenstream *ts, int pos) |
| 324 |
{ |
| 325 |
|
| 326 |
int new_pos, l; |
| 327 |
|
| 328 |
if (ts->fd) |
| 329 |
#ifdef FLITE_PLUS_HTS_ENGINE |
| 330 |
new_pos = pos < 0 ? 0 : pos; |
| 331 |
#else |
| 332 |
new_pos = (int)cst_fseek(ts->fd, (long)pos, CST_SEEK_ABSOLUTE); |
| 333 |
#endif |
| 334 |
else if (ts->string_buffer) |
| 335 |
{ |
| 336 |
l = cst_strlen(ts->string_buffer); |
| 337 |
if (pos > l) |
| 338 |
new_pos = l; |
| 339 |
else if (pos < 0) |
| 340 |
new_pos = 0; |
| 341 |
else |
| 342 |
new_pos = pos; |
| 343 |
} |
| 344 |
else |
| 345 |
new_pos = pos; |
| 346 |
|
| 347 |
ts->file_pos = new_pos; |
| 348 |
ts->current_char = ' '; |
| 349 |
|
| 350 |
return ts->file_pos; |
| 351 |
} |
| 352 |
|
| 353 |
int ts_get_stream_pos(cst_tokenstream *ts) |
| 354 |
{ |
| 355 |
return ts->file_pos; |
| 356 |
} |
| 357 |
|
| 358 |
static cst_string ts_getc(cst_tokenstream *ts) |
| 359 |
{ |
| 360 |
if (ts->fd) |
| 361 |
{ |
| 362 |
#ifndef FLITE_PLUS_HTS_ENGINE |
| 363 |
ts->current_char = cst_fgetc(ts->fd); |
| 364 |
#endif |
| 365 |
} |
| 366 |
else if (ts->string_buffer) |
| 367 |
{ |
| 368 |
if (ts->string_buffer[ts->file_pos] == '\0') |
| 369 |
ts->current_char = TS_EOF; |
| 370 |
else |
| 371 |
ts->current_char = ts->string_buffer[ts->file_pos]; |
| 372 |
} |
| 373 |
|
| 374 |
if (ts->current_char != TS_EOF) |
| 375 |
ts->file_pos++; |
| 376 |
if (ts->current_char == '\n') |
| 377 |
ts->line_number++; |
| 378 |
return ts->current_char; |
| 379 |
} |
| 380 |
|
| 381 |
const cst_string *ts_get_quoted_token(cst_tokenstream *ts, |
| 382 |
char quote, |
| 383 |
char escape) |
| 384 |
{ |
| 385 |
|
| 386 |
|
| 387 |
int p; |
| 388 |
|
| 389 |
|
| 390 |
ts->charclass[(unsigned int)quote] |= TS_CHARCLASS_QUOTE; |
| 391 |
ts->charclass[(unsigned int)escape] |= TS_CHARCLASS_QUOTE; |
| 392 |
|
| 393 |
|
| 394 |
get_token_sub_part(ts,TS_CHARCLASS_WHITESPACE, |
| 395 |
&ts->whitespace, |
| 396 |
&ts->ws_max); |
| 397 |
ts->token_pos = ts->file_pos - 1; |
| 398 |
|
| 399 |
if (ts->current_char == quote) |
| 400 |
{ |
| 401 |
ts_getc(ts); |
| 402 |
|
| 403 |
for (p=0; ((ts->current_char != TS_EOF) && |
| 404 |
(ts->current_char != quote)); |
| 405 |
p++) |
| 406 |
{ |
| 407 |
if (p >= ts->token_max) |
| 408 |
extend_buffer(&ts->token,&ts->token_max); |
| 409 |
ts->token[p] = ts->current_char; |
| 410 |
ts_getc(ts); |
| 411 |
if (ts->current_char == escape) |
| 412 |
{ |
| 413 |
ts_get(ts); |
| 414 |
if (p >= ts->token_max) |
| 415 |
extend_buffer(&ts->token,&ts->token_max); |
| 416 |
ts->token[p] = ts->current_char; |
| 417 |
ts_get(ts); |
| 418 |
} |
| 419 |
} |
| 420 |
ts->token[p] = '\0'; |
| 421 |
ts_getc(ts); |
| 422 |
} |
| 423 |
else |
| 424 |
{ |
| 425 |
|
| 426 |
get_token_sub_part(ts,TS_CHARCLASS_PREPUNCT, |
| 427 |
&ts->prepunctuation, |
| 428 |
&ts->prep_max); |
| 429 |
|
| 430 |
if (!ts_charclass(ts->current_char,TS_CHARCLASS_SINGLECHAR,ts)) |
| 431 |
{ |
| 432 |
if (2 >= ts->token_max) extend_buffer(&ts->token,&ts->token_max); |
| 433 |
ts->token[0] = ts->current_char; |
| 434 |
ts->token[1] = '\0'; |
| 435 |
ts_getc(ts); |
| 436 |
} |
| 437 |
else |
| 438 |
get_token_sub_part_2(ts, |
| 439 |
TS_CHARCLASS_WHITESPACE, |
| 440 |
&ts->token, |
| 441 |
&ts->token_max); |
| 442 |
|
| 443 |
|
| 444 |
get_token_postpunctuation(ts); |
| 445 |
} |
| 446 |
|
| 447 |
return ts->token; |
| 448 |
} |
| 449 |
|
| 450 |
const cst_string *ts_get(cst_tokenstream *ts) |
| 451 |
{ |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
get_token_sub_part(ts, |
| 456 |
TS_CHARCLASS_WHITESPACE, |
| 457 |
&ts->whitespace, |
| 458 |
&ts->ws_max); |
| 459 |
|
| 460 |
|
| 461 |
ts->token_pos = ts->file_pos - 1; |
| 462 |
|
| 463 |
|
| 464 |
if (ts->current_char != TS_EOF && |
| 465 |
ts_charclass(ts->current_char,TS_CHARCLASS_PREPUNCT,ts)) |
| 466 |
get_token_sub_part(ts, |
| 467 |
TS_CHARCLASS_PREPUNCT, |
| 468 |
&ts->prepunctuation, |
| 469 |
&ts->prep_max); |
| 470 |
else if (ts->prepunctuation) |
| 471 |
ts->prepunctuation[0] = '\0'; |
| 472 |
|
| 473 |
if (ts->current_char != TS_EOF && |
| 474 |
ts_charclass(ts->current_char,TS_CHARCLASS_SINGLECHAR,ts)) |
| 475 |
{ |
| 476 |
if (2 >= ts->token_max) extend_buffer(&ts->token,&ts->token_max); |
| 477 |
ts->token[0] = ts->current_char; |
| 478 |
ts->token[1] = '\0'; |
| 479 |
ts_getc(ts); |
| 480 |
} |
| 481 |
else |
| 482 |
get_token_sub_part_2(ts, |
| 483 |
TS_CHARCLASS_WHITESPACE, |
| 484 |
&ts->token, |
| 485 |
&ts->token_max); |
| 486 |
|
| 487 |
|
| 488 |
if (ts->p_postpunctuationsymbols[0]) |
| 489 |
get_token_postpunctuation(ts); |
| 490 |
|
| 491 |
return ts->token; |
| 492 |
} |
| 493 |
|
| 494 |
int ts_read(void *buff, int size, int num, cst_tokenstream *ts) |
| 495 |
{ |
| 496 |
|
| 497 |
|
| 498 |
int i,j,p; |
| 499 |
cst_string *cbuff; |
| 500 |
|
| 501 |
cbuff = (cst_string *)buff; |
| 502 |
|
| 503 |
for (p=i=0; i < num; i++) |
| 504 |
for (j=0; j < size; j++,p++) |
| 505 |
cbuff[p] = ts_getc(ts); |
| 506 |
|
| 507 |
return i; |
| 508 |
} |