1 /* Automatically generated by Ragel from "ragel_xmldeclscanner.c".
3 * Parts of this file are copied from Ragel source covered by the GNU
4 * GPL. As a special exception, you may use the parts of this file copied
5 * from Ragel source without restriction. The remainder is derived from
6 * "ragel_xmldeclscanner.c" and inherits the copyright status of that file.
9 # 1 "ragel_xmldeclscanner.c"
12 * This is an input file for the "Ragel" finite state machine compiler utility.
13 * It produces output code which implements a tag scanner for XML data.
14 * It is used for intelligent xml tag completion in an XML editor interface.
16 * See the Ragel online documentation for additional
17 * information about the format of this file.
19 * Written by Marc Liyanage <http://www.entropy.ch>
24 #include <CoreFoundation/CoreFoundation.h>
25 #include "ragel_xmldeclscanner.h"
28 # 29 "ragel_xmldeclscanner_out.c"
29 /* Forward dec state for the transition structure. */
30 struct XMLDeclScannerStateStruct;
32 /* Only non-static data: current state, acceptance indicator. */
33 struct XMLDeclScannerStruct
37 # 20 "ragel_xmldeclscanner.c"
39 char *mark_encodingstart;
40 char *mark_encodingend;
42 # 43 "ragel_xmldeclscanner_out.c"
44 typedef struct XMLDeclScannerStruct XMLDeclScanner;
47 void XMLDeclScannerInit( XMLDeclScanner *fsm );
49 /* Execute some chunk of data. */
50 void XMLDeclScannerExecute( XMLDeclScanner *fsm, char *data, int dlen );
52 /* Indicate to the fsm tha there is no more data. */
53 void XMLDeclScannerFinish( XMLDeclScanner *fsm );
55 /* Did the machine accept? */
56 int XMLDeclScannerAccept( XMLDeclScanner *fsm );
58 # 25 "ragel_xmldeclscanner.c"
61 # 62 "ragel_xmldeclscanner_out.c"
62 #define f XMLDeclScanner_f
63 #define s XMLDeclScanner_s
64 #define k XMLDeclScanner_k
65 #define i XMLDeclScanner_i
66 #define t XMLDeclScanner_t
68 #define SPEC_ANY_FLAT 0x01
69 #define SPEC_ANY_SINGLE 0x02
70 #define SPEC_ANY_RANGE 0x04
71 #define SPEC_ANY_DEF 0x08
72 #define SPEC_IS_FINAL 0x10
73 #define SPEC_OUT_FUNC 0x20
75 /* The array of functions. */
76 static int XMLDeclScanner_f[] = {
80 /* The array of keys of transitions. */
81 static char XMLDeclScanner_k[] = {
82 -69, -65, 60, -17, 60, 63, 120, 109,
83 108, 32, 9, 13, 118, 101, 114, 115,
84 105, 111, 110, 61, 34, 39, 45, 46,
85 58, 95, 48, 57, 65, 90, 97, 122,
86 39, 45, 46, 58, 95, 48, 57, 65,
87 90, 97, 122, 39, 45, 46, 58, 95,
88 48, 57, 65, 90, 97, 122, 39, 45,
89 46, 58, 95, 48, 57, 65, 90, 97,
90 122, 39, 45, 46, 58, 95, 48, 57,
91 65, 90, 97, 122, 32, 9, 13, 45,
92 46, 58, 95, 48, 57, 65, 90, 97,
93 122, 34, 45, 46, 58, 95, 48, 57,
94 65, 90, 97, 122, 34, 45, 46, 58,
95 95, 48, 57, 65, 90, 97, 122, 34,
96 45, 46, 58, 95, 48, 57, 65, 90,
97 97, 122, 34, 45, 46, 58, 95, 48,
98 57, 65, 90, 97, 122, 32, 9, 13,
99 101, 110, 99, 111, 100, 105, 110, 103,
100 61, 34, 39, 65, 90, 97, 122, 34,
101 45, 46, 95, 48, 57, 65, 90, 97,
102 122, 34, 45, 46, 95, 48, 57, 65,
103 90, 97, 122, 34, 45, 46, 95, 48,
104 57, 65, 90, 97, 122, 32, 63, 9,
105 13, 65, 90, 97, 122, 39, 45, 46,
106 95, 48, 57, 65, 90, 97, 122, 39,
107 45, 46, 95, 48, 57, 65, 90, 97,
108 122, 39, 45, 46, 95, 48, 57, 65,
109 90, 97, 122, 32, 63, 9, 13, 116,
110 97, 110, 100, 97, 108, 111, 110, 101,
111 61, 34, 39, 110, 121, 101, 115, 39,
112 111, 39, 32, 63, 9, 13, 110, 121,
113 101, 115, 34, 111, 34, 32, 63, 9,
114 13, 63, 63, 115, 62, 0
117 /* The array of indicies into the transition array. */
118 static unsigned char XMLDeclScanner_i[] = {
119 1, 2, 3, 4, 3, 5, 6, 7,
120 8, 9, 9, 10, 11, 12, 13, 14,
121 15, 16, 17, 18, 19, 20, 21, 21,
122 21, 21, 21, 21, 22, 23, 24, 24,
123 24, 24, 24, 24, 22, 23, 24, 24,
124 24, 24, 24, 24, 22, 23, 24, 24,
125 24, 24, 24, 24, 22, 23, 24, 24,
126 24, 24, 24, 24, 25, 25, 26, 27,
127 27, 27, 27, 27, 27, 28, 29, 30,
128 30, 30, 30, 30, 30, 28, 29, 30,
129 30, 30, 30, 30, 30, 28, 29, 30,
130 30, 30, 30, 30, 30, 28, 29, 30,
131 30, 30, 30, 30, 30, 25, 25, 31,
132 32, 33, 34, 35, 36, 37, 38, 39,
133 40, 41, 42, 42, 43, 44, 45, 45,
134 45, 45, 45, 43, 44, 45, 45, 45,
135 45, 45, 43, 44, 45, 45, 45, 45,
136 45, 46, 47, 46, 48, 48, 49, 50,
137 51, 51, 51, 51, 51, 49, 50, 51,
138 51, 51, 51, 51, 49, 50, 51, 51,
139 51, 51, 51, 46, 47, 46, 52, 53,
140 54, 55, 56, 57, 58, 59, 60, 61,
141 62, 63, 64, 65, 66, 67, 68, 69,
142 68, 70, 47, 70, 71, 72, 73, 74,
143 75, 76, 75, 70, 47, 70, 47, 47,
147 /* The aray of states. */
148 static int XMLDeclScanner_s[] = {
149 768, 0, 0, 1026, 0, 0, 1, 1026,
150 1, 1, 1, 1026, 2, 2, 1, 1026,
151 3, 3, 2, 1026, 5, 5, 1, 1026,
152 6, 6, 1, 1026, 7, 7, 1, 1026,
153 8, 8, 1, 1286, 9, 9, 1, 1,
154 1026, 12, 11, 1, 1026, 13, 12, 1,
155 1026, 14, 13, 1, 1026, 15, 14, 1,
156 1026, 16, 15, 1, 1026, 17, 16, 1,
157 1026, 18, 17, 1, 1026, 19, 18, 1,
158 1026, 20, 19, 2, 1286, 22, 21, 4,
159 3, 1286, 32, 28, 5, 3, 1286, 43,
160 36, 5, 3, 1286, 54, 44, 5, 3,
161 1286, 65, 52, 5, 3, 1286, 76, 60,
162 1, 1, 1286, 79, 62, 4, 3, 1286,
163 89, 69, 5, 3, 1286, 100, 77, 5,
164 3, 1286, 111, 85, 5, 3, 1286, 122,
165 93, 5, 3, 1286, 133, 101, 1, 1,
166 1026, 136, 103, 1, 1026, 137, 104, 1,
167 1026, 138, 105, 1, 1026, 139, 106, 1,
168 1026, 140, 107, 1, 1026, 141, 108, 1,
169 1026, 142, 109, 1, 1026, 143, 110, 1,
170 1026, 144, 111, 1, 1026, 145, 112, 2,
171 1028, 147, 114, 2, 1286, 151, 116, 4,
172 3, 1286, 161, 123, 4, 3, 1286, 171,
173 130, 4, 3, 1286, 181, 137, 2, 1,
174 1028, 185, 140, 2, 1286, 189, 142, 4,
175 3, 1286, 199, 149, 4, 3, 1286, 209,
176 156, 4, 3, 1286, 219, 163, 2, 1,
177 1026, 223, 166, 1, 1026, 224, 167, 1,
178 1026, 225, 168, 1, 1026, 226, 169, 1,
179 1026, 227, 170, 1, 1026, 228, 171, 1,
180 1026, 229, 172, 1, 1026, 230, 173, 1,
181 1026, 231, 174, 1, 1026, 232, 175, 1,
182 1026, 233, 176, 2, 1026, 235, 178, 2,
183 1026, 237, 180, 1, 1026, 238, 181, 1,
184 1026, 239, 182, 1, 1026, 240, 183, 1,
185 1026, 241, 184, 1, 1286, 242, 185, 2,
186 1, 1026, 246, 188, 2, 1026, 248, 190,
187 1, 1026, 249, 191, 1, 1026, 250, 192,
188 1, 1026, 251, 193, 1, 1026, 252, 194,
189 1, 1286, 253, 195, 2, 1, 1026, 257,
190 198, 1, 1026, 258, 199, 2, 1026, 260,
191 201, 1, 792, 261, 202, 792, 261, 203,
195 /* The array of transitions. */
196 static int *XMLDeclScanner_t[] = {
197 0, 0, s+7, 0, s+11, 0, s+19, 0,
198 s+3, 0, s+23, 0, s+27, 0, s+31, 0,
199 s+35, 0, s+40, 0, s+44, 0, s+48, 0,
200 s+52, 0, s+56, 0, s+60, 0, s+64, 0,
201 s+68, 0, s+72, 0, s+106, 0, s+76, 0,
202 s+86, 0, s+81, 0, s+101, 0, s+96, 0,
203 s+91, 0, s+136, 0, s+116, 0, s+111, 0,
204 s+131, 0, s+126, 0, s+121, 0, s+140, 0,
205 s+144, 0, s+148, 0, s+152, 0, s+156, 0,
206 s+160, 0, s+164, 0, s+168, 0, s+172, 0,
207 s+176, 0, s+200, 0, s+180, f+0, s+195, f+2,
208 s+190, 0, s+185, 0, s+330, 0, s+334, 0,
209 s+204, f+0, s+219, f+2, s+214, 0, s+209, 0,
210 s+228, 0, s+232, 0, s+236, 0, s+240, 0,
211 s+244, 0, s+248, 0, s+252, 0, s+256, 0,
212 s+260, 0, s+264, 0, s+297, 0, s+268, 0,
213 s+284, 0, s+272, 0, s+276, 0, s+280, 0,
214 s+292, 0, s+288, 0, s+326, 0, s+313, 0,
215 s+301, 0, s+305, 0, s+309, 0, s+321, 0,
216 s+317, 0, s+224, 0, s+338, 0, s+341, 0,
220 /* The start state. */
221 static int *XMLDeclScanner_start = s+15;
223 /* Init the fsm to a runnable state. */
224 void XMLDeclScannerInit( XMLDeclScanner *fsm )
226 fsm->curState = XMLDeclScanner_start;
228 # 29 "ragel_xmldeclscanner.c"
230 fsm->mark_encodingstart = fsm->mark_encodingend = NULL;
232 # 233 "ragel_xmldeclscanner_out.c"
235 /* Did the fsm accept? */
236 int XMLDeclScannerAccept( XMLDeclScanner *fsm )
241 /* Binary search an array of keys looking for a key. */
242 static char *XMLDeclScannerBSearch( char c, char *keys, int len )
246 char *upper = keys + len - 1;
251 /* Find the midpoint. */
252 mid = lower + ((upper-lower) >> 1);
263 /* Binary search an array of keys looking for a key. */
264 static char *XMLDeclScannerRangeBSearch( char c, char *keys, int len )
268 char *upper = keys + len - 2;
273 /* Find the midpoint. Be sure to settle on the
274 * lower end of a range. */
275 mid = lower + (((upper-lower) >> 1) & ~1);
279 else if ( c > mid[1] )
282 /* The key was found in the range mid, return it. */
288 /* Execute the fsm on some chunk of data. */
289 void XMLDeclScannerExecute( XMLDeclScanner *fsm, char *data, int dlen )
293 int *cs = fsm->curState;
294 int specs, **trans, *funcs, nfuncs;
302 if ( cs == 0 || len == 0 )
305 /* Get required data. */
310 /* Try flat index. */
311 if ( specs & SPEC_ANY_FLAT ) {
317 /* Try binary search single. */
318 if ( specs & SPEC_ANY_SINGLE ) {
319 /* Try to find the key. */
321 char *match = XMLDeclScannerBSearch( *p, keys, indsLen );
324 trans = t + (inds[match - keys]<<1);
328 /* Advance over the keys and indicies. */
333 /* Try binary search range. */
334 if ( specs & SPEC_ANY_RANGE ) {
335 /* Try to find the key. */
337 char *match = XMLDeclScannerRangeBSearch( *p, keys, (indsLen<<1) );
340 trans = t + (inds[(match - keys)>>1]<<1);
344 /* Advance over the keys and indicies. */
345 keys += (indsLen<<1);
349 /* Try the default transition. */
350 if ( specs & SPEC_ANY_DEF ) {
351 trans = t + ((*inds)<<1);
360 /* Move to the new state. */
363 /* Check for functions. */
364 if ( (funcs=*trans) == 0 )
369 while ( nfuncs-- > 0 ) {
370 switch ( *funcs++ ) {
372 # 34 "ragel_xmldeclscanner.c"
374 DEBUG && fprintf(stderr, "encodingstart: %d %c\n", p - data, *p);
375 fsm->mark_encodingstart = p;
378 # 39 "ragel_xmldeclscanner.c"
380 fsm->mark_encodingend = p;
381 DEBUG && fprintf(stderr, "encodingend: %d %c\n", p - data, *p);
382 DEBUG && fprintf(stderr, "length: %d\n", fsm->mark_encodingend - fsm->mark_encodingstart);
384 # 385 "ragel_xmldeclscanner_out.c"
393 if ( cs != 0 && *cs & SPEC_IS_FINAL ) {
394 /* The machine accepts. */
396 /* If finishing in a final state then execute the
397 * out functions for it. (if any). */
398 if ( *cs & SPEC_OUT_FUNC ) {
399 funcs = f+*(cs + (*cs>>8)-1);
405 /* If we are not in a final state then this
406 * is an error. Move to the error state. */
414 /* Indicate to the fsm that the input is done. Does cleanup tasks. */
415 void XMLDeclScannerFinish( XMLDeclScanner *fsm )
417 XMLDeclScannerExecute( fsm, 0, 0 );
426 #undef SPEC_ANY_SINGLE
427 #undef SPEC_ANY_RANGE
432 # 66 "ragel_xmldeclscanner.c"
438 unsigned int getEncodingFromXmlDecl(char *data, int len) {
440 int encodinglength, result;
441 CFStringRef encodingString;
443 XMLDeclScanner scanner, *machine = &scanner;
445 XMLDeclScannerInit(machine);
446 XMLDeclScannerExecute(machine, data, len > 512 ? 512 : len);
447 XMLDeclScannerFinish(machine);
449 if (!XMLDeclScannerAccept(machine)) {
453 encodinglength = machine->mark_encodingend - machine->mark_encodingstart;
456 encodingString = CFStringCreateWithBytes (
458 machine->mark_encodingstart,
459 (CFIndex)encodinglength,
460 kCFStringEncodingASCII,
464 result = CFStringConvertIANACharSetNameToEncoding(encodingString);
465 CFRelease(encodingString);
467 if (result == kCFStringEncodingInvalidId)
470 result = CFStringConvertEncodingToNSStringEncoding(result);
472 if (result == kCFStringEncodingInvalidId)
481 /* No longer used, now using CoreFoundation
483 struct encodingPair {
488 struct encodingPair encodingPairs[] = {
489 {NSUTF8StringEncoding, "UTF-8"},
490 {NSISOLatin1StringEncoding, "ISO-8859-1"},
496 for (i = 0; encodingPairs[i].matchstring; i++) {
498 testlength = strlen(encodingPairs[i].matchstring);
499 if (!strncasecmp(machine->mark_encodingstart, encodingPairs[i].matchstring, encodinglength < testlength ? encodinglength : testlength)) {
500 return encodingPairs[i].encoding;