| 1 | /* $Header: d:/cvsroot/tads/tads3/TCSRC.H,v 1.3 1999/07/11 00:46:55 MJRoberts Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 5 | * |
| 6 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 7 | * on using and copying this software. |
| 8 | */ |
| 9 | /* |
| 10 | Name |
| 11 | tcsrc.h - TADS3 compiler source file reader |
| 12 | Function |
| 13 | Provides I/O on source files, translating character sets between |
| 14 | the file's character set and the internal UTF8 representation. |
| 15 | |
| 16 | A source file's character set is determined as follows: |
| 17 | |
| 18 | - If the first two bytes of the file are 0xFF, 0xFE, the file is taken |
| 19 | as Unicode UCS-2 encoding, 16 bits per character, with low-order bytes |
| 20 | stored first in each byte pair making up a character. (The special |
| 21 | Unicode marker character is 0xFEFF, and the character 0xFFFE is |
| 22 | specifically illegal. So, if we see FF FE in that order, we know that |
| 23 | it must be taken to mean 0xFEFF, hence high byte second, because the |
| 24 | other possibility - high byte first - would yield an invalid character.) |
| 25 | |
| 26 | - If the first two bytes of the file are 0xFE, 0xFF, the file is taken |
| 27 | as Unicode UCS-2 encoding, 16 bits per character, with high-order bytes |
| 28 | stored first. |
| 29 | |
| 30 | - If the first nine bytes of the contain the ASCII string "#charset" |
| 31 | followed by an ASCII 0x20 byte, we obtain the name of the character |
| 32 | set stored in the file by skipping past any following 0x20 or 0x09 |
| 33 | bytes. The next character must be a double quote (0x22) character. |
| 34 | We will scan characters up to the next double quote (0x22) character, |
| 35 | which must occur within 256 bytes or before any CR or LF (0x0D, 0x0A) |
| 36 | characters. The characters between the quotes will be taken as the |
| 37 | character set name. |
| 38 | |
| 39 | - If none of the above information is found at the beginning of the |
| 40 | file, we take the file to be in the current global default character |
| 41 | set. |
| 42 | Notes |
| 43 | |
| 44 | Modified |
| 45 | 04/12/99 MJRoberts - Creation |
| 46 | */ |
| 47 | |
| 48 | #ifndef TCSRC_H |
| 49 | #define TCSRC_H |
| 50 | |
| 51 | #include <stdlib.h> |
| 52 | |
| 53 | |
| 54 | /* ------------------------------------------------------------------------ */ |
| 55 | /* |
| 56 | * Generic source object |
| 57 | */ |
| 58 | class CTcSrcObject |
| 59 | { |
| 60 | public: |
| 61 | CTcSrcObject() { } |
| 62 | virtual ~CTcSrcObject() { } |
| 63 | |
| 64 | /* |
| 65 | * Read the next line. Fills in the buffer with a null-terminated |
| 66 | * string, ending in a newline if the line fits in the buffer, or |
| 67 | * ending without a newline if not. Returns zero at end of file. |
| 68 | */ |
| 69 | virtual size_t read_line(char *buf, size_t buflen) = 0; |
| 70 | |
| 71 | /* have we reached the end of the file? */ |
| 72 | virtual int at_eof() const = 0; |
| 73 | }; |
| 74 | |
| 75 | /* ------------------------------------------------------------------------ */ |
| 76 | /* |
| 77 | * Source File base class |
| 78 | */ |
| 79 | class CTcSrcFile: public CTcSrcObject |
| 80 | { |
| 81 | public: |
| 82 | /* |
| 83 | * Create the source file reader. We take ownership of the mapper |
| 84 | * object, so we'll delete it when we're deleted. |
| 85 | */ |
| 86 | CTcSrcFile(osfildef *fp, class CCharmapToUni *mapper); |
| 87 | |
| 88 | /* delete */ |
| 89 | virtual ~CTcSrcFile(); |
| 90 | |
| 91 | /* |
| 92 | * Read a line of text from the file. On success, returns the |
| 93 | * length of the line read, including the null terminator character |
| 94 | * and any newline character. At end of file or other error, |
| 95 | * returns zero. Because the result is always null-terminated, a |
| 96 | * return value of zero will never occur except on error. |
| 97 | * |
| 98 | * The result will be a UTF-8 string, and will always be |
| 99 | * null-terminated. If the source line fits into the buffer, a |
| 100 | * newline character ('\n') will be the last character of the |
| 101 | * string. If the line is too long for the buffer, the result will |
| 102 | * end with something other than a newline character, and the next |
| 103 | * read_line() call will retrieve the remainder of the line (or as |
| 104 | * much of the remainder as will fit into the buffer for that call). |
| 105 | * The one exception is that, if the file does not end in a newline, |
| 106 | * the last line will be returned without a newline; this condition |
| 107 | * can be distinguished by the non-zero return value of the |
| 108 | * subsequent call to read_line(). |
| 109 | * |
| 110 | * This routine translates from the source character set to UTF-8, |
| 111 | * and automatically translates newline conventions. We handle CR, |
| 112 | * LF, CR-LF, or LF-CR newlines (CR is ASCII 13, LF is ASCII 10). |
| 113 | */ |
| 114 | size_t read_line(char *buf, size_t bufl); |
| 115 | |
| 116 | /* determine if we've reached end of file */ |
| 117 | int at_eof() const { return at_eof_; } |
| 118 | |
| 119 | /* |
| 120 | * Open a source file. We'll scan the beginning of the file to |
| 121 | * determine what type of source file reader to use, then create an |
| 122 | * appropriate source file reader subclass to read the file. We |
| 123 | * expect the filename to be limited to ASCII characters. |
| 124 | * |
| 125 | * If we can't identify the character set that the file uses, we'll |
| 126 | * use the given default character set. If no default character set |
| 127 | * is given, we'll create a plain ASCII reader. |
| 128 | * |
| 129 | * If we encounter a #charset directive, and we can't load the |
| 130 | * desired character set map, we'll set *charset_error to true; |
| 131 | * otherwise, we'll set *charset_error to false. Note that |
| 132 | * *charset_error will be set to false if there's simply no #charset |
| 133 | * directive. |
| 134 | * |
| 135 | * If we fail to open the default character set, we'll return null |
| 136 | * and set *default_charset_error to true. |
| 137 | */ |
| 138 | static CTcSrcFile *open_source(const char *filename, |
| 139 | class CResLoader *res_loader, |
| 140 | const char *default_charset, |
| 141 | int *charset_error, |
| 142 | int *default_charset_error); |
| 143 | |
| 144 | /* |
| 145 | * Open a plain ASCII source file or a Unicode file. This doesn't look |
| 146 | * for a #charset marker, but it does check for Unicode byte-order |
| 147 | * markers. If we find a Unicode byte-order marker, we'll read the |
| 148 | * file using the suitable Unicode mapper; otherwise we'll read it |
| 149 | * using a plain ASCII mapper. |
| 150 | */ |
| 151 | static CTcSrcFile *open_plain(const char *filename); |
| 152 | |
| 153 | /* |
| 154 | * Open a plain ASCII source file. |
| 155 | */ |
| 156 | static CTcSrcFile *open_ascii(const char *filename); |
| 157 | |
| 158 | protected: |
| 159 | /* |
| 160 | * match the leading substring of a unicode utf-16 string to a given |
| 161 | * ascii string |
| 162 | */ |
| 163 | static int ucs_str_starts_with(const char *ustr, size_t ulen, |
| 164 | const char *astr, |
| 165 | int big_endian, int case_fold) |
| 166 | { |
| 167 | /* compare each character of the unicode string to the ascii string */ |
| 168 | for ( ; ulen >= 2 && *astr != '\0' ; ustr += 2, ulen -= 2, ++astr) |
| 169 | { |
| 170 | /* if the characters don't match, we don't have a match */ |
| 171 | if (!ucs_char_eq(ustr, *astr, big_endian, case_fold)) |
| 172 | return FALSE; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * if we reached the end of the ASCII string, we have a match; |
| 177 | * otherwise, we ran out of the Unicode string before we ran out of |
| 178 | * the ASCII string, so we don't have a match |
| 179 | */ |
| 180 | return (*astr == 0); |
| 181 | } |
| 182 | |
| 183 | /* does a Unicode character match an ASCII character? */ |
| 184 | static int ucs_char_eq(const char *ustr, char ac, int big_endian, |
| 185 | int case_fold) |
| 186 | { |
| 187 | uchar lo, hi; |
| 188 | uint uc; |
| 189 | |
| 190 | /* get this unicode character, translating its endianness */ |
| 191 | if (big_endian) |
| 192 | hi = (uchar)*ustr, lo = (uchar)*(ustr + 1); |
| 193 | else |
| 194 | lo = (uchar)*ustr, hi = (uchar)*(ustr + 1); |
| 195 | uc = (hi << 8) + lo; |
| 196 | |
| 197 | /* if it's outside of ASCII range, we obviously can't match */ |
| 198 | if (uc > 127) |
| 199 | return FALSE; |
| 200 | |
| 201 | /* if we're folding case, convert both to lower case */ |
| 202 | if (case_fold) |
| 203 | ac = (char)tolower(ac), uc = tolower((char)uc); |
| 204 | |
| 205 | /* compare the characters */ |
| 206 | return (ac == (char)uc); |
| 207 | } |
| 208 | |
| 209 | /* end-of-file flag */ |
| 210 | unsigned int at_eof_ : 1; |
| 211 | |
| 212 | /* my source file */ |
| 213 | class CVmDataSource *fp_; |
| 214 | |
| 215 | /* read buffer */ |
| 216 | char buf_[1024]; |
| 217 | |
| 218 | /* amount of data in the buffer */ |
| 219 | size_t rem_; |
| 220 | |
| 221 | /* current position in buffer */ |
| 222 | char *p_; |
| 223 | |
| 224 | /* my character mapper */ |
| 225 | class CCharmapToUni *mapper_; |
| 226 | }; |
| 227 | |
| 228 | /* ------------------------------------------------------------------------ */ |
| 229 | /* |
| 230 | * Memory buffer-based source reader |
| 231 | */ |
| 232 | class CTcSrcMemory: public CTcSrcObject |
| 233 | { |
| 234 | public: |
| 235 | CTcSrcMemory(const char *buf, size_t len, class CCharmapToUni *mapper); |
| 236 | ~CTcSrcMemory(); |
| 237 | |
| 238 | /* read the next line */ |
| 239 | size_t read_line(char *buf, size_t bufl); |
| 240 | |
| 241 | /* determine if we've reached end of file */ |
| 242 | int at_eof() const { return (*buf_ == '\0'); } |
| 243 | |
| 244 | private: |
| 245 | /* allocated buffer */ |
| 246 | char *buf_alo_; |
| 247 | |
| 248 | /* current buffer pointer */ |
| 249 | const char *buf_; |
| 250 | }; |
| 251 | |
| 252 | #endif /* TCSRC_H */ |
| 253 |