cst_tokenstream.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 1999 */
  6. /* All Rights Reserved. */
  7. /* */
  8. /* Permission is hereby granted, free of charge, to use and distribute */
  9. /* this software and its documentation without restriction, including */
  10. /* without limitation the rights to use, copy, modify, merge, publish, */
  11. /* distribute, sublicense, and/or sell copies of this work, and to */
  12. /* permit persons to whom this work is furnished to do so, subject to */
  13. /* the following conditions: */
  14. /* 1. The code must retain the above copyright notice, this list of */
  15. /* conditions and the following disclaimer. */
  16. /* 2. Any modifications must be clearly marked as such. */
  17. /* 3. Original authors' names are not deleted. */
  18. /* 4. The authors' names are not used to endorse or promote products */
  19. /* derived from this software without specific prior written */
  20. /* permission. */
  21. /* */
  22. /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
  23. /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
  24. /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
  25. /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
  26. /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
  27. /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
  28. /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
  29. /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
  30. /* THIS SOFTWARE. */
  31. /* */
  32. /*************************************************************************/
  33. /* Author: Alan W Black (awb@cs.cmu.edu) */
  34. /* Date: July 1999 */
  35. /*************************************************************************/
  36. /* */
  37. /* Tokenizer for strings and files */
  38. /* */
  39. /*************************************************************************/
  40. #ifndef _CST_TOKENSTREAM_H__
  41. #define _CST_TOKENSTREAM_H__
  42. #include "cst_alloc.h"
  43. #include "cst_string.h"
  44. #include "cst_file.h"
  45. #include "cst_features.h"
  46. typedef struct cst_tokenstream_struct {
  47. cst_file fd;
  48. int file_pos;
  49. int line_number;
  50. int eof_flag;
  51. cst_string *string_buffer;
  52. int current_char;
  53. int token_pos;
  54. int ws_max;
  55. cst_string *whitespace;
  56. int prep_max;
  57. cst_string *prepunctuation;
  58. int token_max;
  59. cst_string *token;
  60. int postp_max;
  61. cst_string *postpunctuation;
  62. cst_features *tags; /* e.g xml tags */
  63. /* if set will find token boundaries at every utf8 character */
  64. int utf8_explode_mode;
  65. void *streamtype_data;
  66. /* Should only be set through set_charclasses as charclass table needs */
  67. /* to be updated when you reset these */
  68. const cst_string *p_whitespacesymbols;
  69. const cst_string *p_singlecharsymbols;
  70. const cst_string *p_prepunctuationsymbols;
  71. const cst_string *p_postpunctuationsymbols;
  72. cst_string charclass[256];
  73. /* To allow externally specified reading functions e.g. epub/xml */
  74. int (*open)(struct cst_tokenstream_struct *ts, const char *filename);
  75. void (*close)(struct cst_tokenstream_struct *ts);
  76. int (*eof)(struct cst_tokenstream_struct *ts);
  77. int (*seek)(struct cst_tokenstream_struct *ts, int pos);
  78. int (*tell)(struct cst_tokenstream_struct *ts);
  79. int (*size)(struct cst_tokenstream_struct *ts);
  80. int (*getc)(struct cst_tokenstream_struct *ts);
  81. } cst_tokenstream;
  82. #define TS_CHARCLASS_NONE 0
  83. #define TS_CHARCLASS_WHITESPACE 2
  84. #define TS_CHARCLASS_SINGLECHAR 4
  85. #define TS_CHARCLASS_PREPUNCT 8
  86. #define TS_CHARCLASS_POSTPUNCT 16
  87. #define TS_CHARCLASS_QUOTE 32
  88. #define ts_charclass(C,CLASS,TS) ((TS)->charclass[(unsigned char)C] & CLASS)
  89. extern const cst_string * const cst_ts_default_whitespacesymbols;
  90. extern const cst_string * const cst_ts_default_prepunctuationsymbols;
  91. extern const cst_string * const cst_ts_default_postpunctuationsymbols;
  92. extern const cst_string * const cst_ts_default_singlecharsymbols;
  93. /* Public functions for tokenstream manipulation */
  94. cst_tokenstream *ts_open(const char *filename,
  95. const cst_string *whitespacesymbols,
  96. const cst_string *singlecharsymbols,
  97. const cst_string *prepunctsymbols,
  98. const cst_string *postpunctsymbols);
  99. cst_tokenstream *ts_open_string(const cst_string *string,
  100. const cst_string *whitespacesymbols,
  101. const cst_string *singlecharsymbols,
  102. const cst_string *prepunctsymbols,
  103. const cst_string *postpunctsymbols);
  104. cst_tokenstream *ts_open_generic(const char *filename,
  105. const cst_string *whitespacesymbols,
  106. const cst_string *singlecharsymbols,
  107. const cst_string *prepunctsymbols,
  108. const cst_string *postpunctsymbols,
  109. void *streamtype_data,
  110. int (*open)(cst_tokenstream *ts,
  111. const char *filename),
  112. void (*close)(cst_tokenstream *ts),
  113. int (*eof)(cst_tokenstream *ts),
  114. int (*seek)(cst_tokenstream *ts, int pos),
  115. int (*tell)(cst_tokenstream *ts),
  116. int (*size)(cst_tokenstream *ts),
  117. int (*getc)(cst_tokenstream *ts));
  118. void ts_close(cst_tokenstream *ts);
  119. int ts_eof(cst_tokenstream *ts);
  120. const cst_string *ts_get(cst_tokenstream *ts);
  121. const cst_string *ts_get_quoted_token(cst_tokenstream *ts,
  122. char quote,
  123. char escape);
  124. /* Externally specified ts interfaces may need this */
  125. cst_string private_ts_getc(cst_tokenstream *ts);
  126. void set_charclasses(cst_tokenstream *ts,
  127. const cst_string *whitespace,
  128. const cst_string *singlecharsymbols,
  129. const cst_string *prepunctuation,
  130. const cst_string *postpunctuation);
  131. int ts_read(void *buff, int size, int num, cst_tokenstream *ts);
  132. int ts_set_stream_pos(cst_tokenstream *ts,int pos);
  133. int ts_get_stream_pos(cst_tokenstream *ts);
  134. int ts_get_stream_size(cst_tokenstream *ts);
  135. #endif