]>
Commit | Line | Data |
---|---|---|
374ca955 A |
1 | /* |
2 | ******************************************************************************* | |
3 | * | |
4 | * Copyright (C) 2003, International Business Machines | |
5 | * Corporation and others. All Rights Reserved. | |
6 | * | |
7 | ******************************************************************************* | |
8 | * file name: ucm.h | |
9 | * encoding: US-ASCII | |
10 | * tab size: 8 (not used) | |
11 | * indentation:4 | |
12 | * | |
13 | * created on: 2003jun20 | |
14 | * created by: Markus W. Scherer | |
15 | * | |
16 | * Definitions for the .ucm file parser and handler module ucm.c. | |
17 | */ | |
18 | ||
19 | #ifndef __UCM_H__ | |
20 | #define __UCM_H__ | |
21 | ||
22 | #include "unicode/utypes.h" | |
23 | #include "ucnvmbcs.h" | |
24 | #include "ucnv_ext.h" | |
25 | #include "filestrm.h" | |
26 | #include <stdio.h> | |
27 | ||
28 | U_CDECL_BEGIN | |
29 | ||
30 | /* | |
31 | * Per-mapping data structure | |
32 | * | |
33 | * u if uLen==1: Unicode code point | |
34 | * else index to uLen code points | |
35 | * b if bLen<=4: up to 4 bytes | |
36 | * else index to bLen bytes | |
37 | * uLen number of code points | |
38 | * bLen number of words containing left-justified bytes | |
39 | * bIsMultipleChars indicates that the bytes contain more than one sequence | |
40 | * according to the state table | |
41 | * f flag for roundtrip (0), fallback (1), sub mapping (2), reverse fallback (3) | |
42 | * same values as in the source file after | | |
43 | */ | |
44 | typedef struct UCMapping { | |
45 | UChar32 u; | |
46 | union { | |
47 | uint32_t index; | |
48 | uint8_t bytes[4]; | |
49 | } b; | |
50 | int8_t uLen, bLen, f, moveFlag; | |
51 | } UCMapping; | |
52 | ||
53 | enum { | |
54 | UCM_FLAGS_INITIAL, /* no mappings parsed yet */ | |
55 | UCM_FLAGS_EXPLICIT, /* .ucm file has mappings with | fallback indicators */ | |
56 | UCM_FLAGS_IMPLICIT, /* .ucm file has mappings without | fallback indicators, later wins */ | |
57 | UCM_FLAGS_MIXED /* both implicit and explicit */ | |
58 | }; | |
59 | ||
60 | typedef struct UCMTable { | |
61 | UCMapping *mappings; | |
62 | int32_t mappingsCapacity, mappingsLength; | |
63 | ||
64 | UChar32 *codePoints; | |
65 | int32_t codePointsCapacity, codePointsLength; | |
66 | ||
67 | uint8_t *bytes; | |
68 | int32_t bytesCapacity, bytesLength; | |
69 | ||
70 | /* index map for mapping by bytes first */ | |
71 | int32_t *reverseMap; | |
72 | ||
73 | uint8_t unicodeMask; | |
74 | int8_t flagsType; /* UCM_FLAGS_INITIAL etc. */ | |
75 | UBool isSorted; | |
76 | } UCMTable; | |
77 | ||
78 | enum { | |
79 | MBCS_STATE_FLAG_DIRECT=1, | |
80 | MBCS_STATE_FLAG_SURROGATES, | |
81 | ||
82 | MBCS_STATE_FLAG_READY=16 | |
83 | }; | |
84 | ||
85 | typedef struct UCMStates { | |
86 | int32_t stateTable[MBCS_MAX_STATE_COUNT][256]; | |
87 | uint32_t stateFlags[MBCS_MAX_STATE_COUNT], | |
88 | stateOffsetSum[MBCS_MAX_STATE_COUNT]; | |
89 | ||
90 | int32_t countStates, minCharLength, maxCharLength, countToUCodeUnits; | |
91 | int8_t conversionType, outputType; | |
92 | } UCMStates; | |
93 | ||
94 | typedef struct UCMFile { | |
95 | UCMTable *base, *ext; | |
96 | UCMStates states; | |
97 | ||
98 | char baseName[UCNV_MAX_CONVERTER_NAME_LENGTH]; | |
99 | } UCMFile; | |
100 | ||
101 | /* simple accesses ---------------------------------------------------------- */ | |
102 | ||
103 | #define UCM_GET_CODE_POINTS(t, m) \ | |
104 | (((m)->uLen==1) ? &(m)->u : (t)->codePoints+(m)->u) | |
105 | ||
106 | #define UCM_GET_BYTES(t, m) \ | |
107 | (((m)->bLen<=4) ? (m)->b.bytes : (t)->bytes+(m)->b.index) | |
108 | ||
109 | /* APIs --------------------------------------------------------------------- */ | |
110 | ||
111 | U_CAPI UCMFile * U_EXPORT2 | |
112 | ucm_open(void); | |
113 | ||
114 | U_CAPI void U_EXPORT2 | |
115 | ucm_close(UCMFile *ucm); | |
116 | ||
117 | U_CAPI UBool U_EXPORT2 | |
118 | ucm_parseHeaderLine(UCMFile *ucm, | |
119 | char *line, char **pKey, char **pValue); | |
120 | ||
121 | /* @return -1 illegal bytes 0 suitable for base table 1 needs to go into extension table */ | |
122 | U_CAPI int32_t U_EXPORT2 | |
123 | ucm_mappingType(UCMStates *baseStates, | |
124 | UCMapping *m, | |
125 | UChar32 codePoints[UCNV_EXT_MAX_UCHARS], | |
126 | uint8_t bytes[UCNV_EXT_MAX_BYTES]); | |
127 | ||
128 | /* add a mapping to the base or extension table as appropriate */ | |
129 | U_CAPI UBool U_EXPORT2 | |
130 | ucm_addMappingAuto(UCMFile *ucm, UBool forBase, UCMStates *baseStates, | |
131 | UCMapping *m, | |
132 | UChar32 codePoints[UCNV_EXT_MAX_UCHARS], | |
133 | uint8_t bytes[UCNV_EXT_MAX_BYTES]); | |
134 | ||
135 | U_CAPI UBool U_EXPORT2 | |
136 | ucm_addMappingFromLine(UCMFile *ucm, const char *line, UBool forBase, UCMStates *baseStates); | |
137 | ||
138 | ||
139 | U_CAPI UCMTable * U_EXPORT2 | |
140 | ucm_openTable(void); | |
141 | ||
142 | U_CAPI void U_EXPORT2 | |
143 | ucm_closeTable(UCMTable *table); | |
144 | ||
145 | U_CAPI void U_EXPORT2 | |
146 | ucm_resetTable(UCMTable *table); | |
147 | ||
148 | U_CAPI void U_EXPORT2 | |
149 | ucm_sortTable(UCMTable *t); | |
150 | ||
151 | /** | |
152 | * Read a table from a .ucm file, from after the CHARMAP line to | |
153 | * including the END CHARMAP line. | |
154 | */ | |
155 | U_CAPI void U_EXPORT2 | |
156 | ucm_readTable(UCMFile *ucm, FileStream* convFile, | |
157 | UBool forBase, UCMStates *baseStates, | |
158 | UErrorCode *pErrorCode); | |
159 | ||
160 | /** | |
161 | * Check the validity of mappings against a base table's states; | |
162 | * necessary for extension-only tables that were read before their base tables. | |
163 | */ | |
164 | U_CAPI UBool U_EXPORT2 | |
165 | ucm_checkValidity(UCMTable *ext, UCMStates *baseStates); | |
166 | ||
167 | /** | |
168 | * Check a base table against an extension table. | |
169 | * Set the moveTarget!=NULL if it is possible to move mappings from the base. | |
170 | * This is the case where base and extension tables are parsed from a single file | |
171 | * (moveTarget==ext) | |
172 | * or when delta file mappings are subtracted from a base table. | |
173 | * | |
174 | * When a base table cannot be modified because a delta file is parsed in makeconv, | |
175 | * then set moveTarget=NULL. | |
176 | * | |
177 | * if(intersectBase) then mappings that exist in the base table but not in | |
178 | * the extension table are moved to moveTarget instead of showing an error. | |
179 | * | |
180 | * Special mode: | |
181 | * If intersectBase==2 for a DBCS extension table, then SBCS mappings are | |
182 | * not moved out of the base unless their Unicode input requires it. | |
183 | * This helps ucmkbase generate base tables for DBCS-only extension .cnv files. | |
184 | * | |
185 | * For both tables in the same file, the extension table is automatically | |
186 | * built. | |
187 | * For separate files, the extension file can use a complete mapping table, | |
188 | * so that common mappings need not be stripped out manually. | |
189 | * | |
190 | * | |
191 | * Sort both tables, and then for each mapping direction: | |
192 | * | |
193 | * If intersectBase is TRUE and the base table contains a mapping | |
194 | * that does not exist in the extension table, then this mapping is moved | |
195 | * to moveTarget. | |
196 | * | |
197 | * - otherwise - | |
198 | * | |
199 | * If the base table contains a mapping for which the input sequence is | |
200 | * the same as the extension input, then | |
201 | * - if the output is the same: remove the extension mapping | |
202 | * - else: error | |
203 | * | |
204 | * If the base table contains a mapping for which the input sequence is | |
205 | * a prefix of the extension input, then | |
206 | * - if moveTarget!=NULL: move the base mapping to the moveTarget table | |
207 | * - else: error | |
208 | * | |
209 | * @return FALSE in case of an irreparable error | |
210 | */ | |
211 | U_CAPI UBool U_EXPORT2 | |
212 | ucm_checkBaseExt(UCMStates *baseStates, UCMTable *base, UCMTable *ext, | |
213 | UCMTable *moveTarget, UBool intersectBase); | |
214 | ||
215 | U_CAPI void U_EXPORT2 | |
216 | ucm_printTable(UCMTable *table, FILE *f, UBool byUnicode); | |
217 | ||
218 | U_CAPI void U_EXPORT2 | |
219 | ucm_printMapping(UCMTable *table, UCMapping *m, FILE *f); | |
220 | ||
221 | ||
222 | U_CAPI void U_EXPORT2 | |
223 | ucm_addState(UCMStates *states, const char *s); | |
224 | ||
225 | U_CAPI void U_EXPORT2 | |
226 | ucm_processStates(UCMStates *states); | |
227 | ||
228 | U_CAPI int32_t U_EXPORT2 | |
229 | ucm_countChars(UCMStates *states, | |
230 | const uint8_t *bytes, int32_t length); | |
231 | ||
232 | ||
233 | U_CAPI int8_t U_EXPORT2 | |
234 | ucm_parseBytes(uint8_t bytes[UCNV_EXT_MAX_BYTES], const char *line, const char **ps); | |
235 | ||
236 | U_CAPI UBool U_EXPORT2 | |
237 | ucm_parseMappingLine(UCMapping *m, | |
238 | UChar32 codePoints[UCNV_EXT_MAX_UCHARS], | |
239 | uint8_t bytes[UCNV_EXT_MAX_BYTES], | |
240 | const char *line); | |
241 | ||
242 | U_CAPI void U_EXPORT2 | |
243 | ucm_addMapping(UCMTable *table, | |
244 | UCMapping *m, | |
245 | UChar32 codePoints[UCNV_EXT_MAX_UCHARS], | |
246 | uint8_t bytes[UCNV_EXT_MAX_BYTES]); | |
247 | ||
248 | /* very makeconv-specific functions ----------------------------------------- */ | |
249 | ||
250 | /* finalize and optimize states after the toUnicode mappings are processed */ | |
251 | U_CAPI void U_EXPORT2 | |
252 | ucm_optimizeStates(UCMStates *states, | |
253 | uint16_t **pUnicodeCodeUnits, | |
254 | _MBCSToUFallback *toUFallbacks, int32_t countToUFallbacks, | |
255 | UBool verbose); | |
256 | ||
257 | /* moved here because it is used inside ucmstate.c */ | |
258 | U_CAPI int32_t U_EXPORT2 | |
259 | ucm_findFallback(_MBCSToUFallback *toUFallbacks, int32_t countToUFallbacks, | |
260 | uint32_t offset); | |
261 | ||
262 | /* very rptp2ucm-specific functions ----------------------------------------- */ | |
263 | ||
264 | /* | |
265 | * Input: Separate tables with mappings from/to Unicode, | |
266 | * subchar and subchar1 (0 if none). | |
267 | * All mappings must have flag 0. | |
268 | * | |
269 | * Output: fromUTable will contain the union of mappings with the correct | |
270 | * precision flags, and be sorted. | |
271 | */ | |
272 | U_CAPI void U_EXPORT2 | |
273 | ucm_mergeTables(UCMTable *fromUTable, UCMTable *toUTable, | |
274 | const uint8_t *subchar, int32_t subcharLength, | |
275 | uint8_t subchar1); | |
276 | ||
277 | U_CAPI UBool U_EXPORT2 | |
278 | ucm_separateMappings(UCMFile *ucm, UBool isSISO); | |
279 | ||
280 | U_CDECL_END | |
281 | ||
282 | #endif |