checkin of last changes for 2.9, mostly docs
[TestXSLT.git] / ragel_xmldeclscanner_out.c
1 /* Automatically generated by Ragel from "ragel_xmldeclscanner.c".
2  *
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.
7  */
8
9 # 1 "ragel_xmldeclscanner.c"
10
11 /*
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.
15  * 
16  * See the Ragel online documentation for additional
17  * information about the format of this file.
18  *
19  * Written by Marc Liyanage <http://www.entropy.ch>
20  *
21  */
22  
23  
24 #include <CoreFoundation/CoreFoundation.h>
25 #include "ragel_xmldeclscanner.h"
26
27
28 # 29 "ragel_xmldeclscanner_out.c"
29 /* Forward dec state for the transition structure. */
30 struct XMLDeclScannerStateStruct;
31
32 /* Only non-static data: current state, acceptance indicator. */
33 struct XMLDeclScannerStruct
34 {
35         int *curState;
36         int accept;
37 # 20 "ragel_xmldeclscanner.c"
38
39                 char *mark_encodingstart;
40                 char *mark_encodingend;
41         
42 # 43 "ragel_xmldeclscanner_out.c"
43 };
44 typedef struct XMLDeclScannerStruct XMLDeclScanner;
45
46 /* Init the fsm. */
47 void XMLDeclScannerInit( XMLDeclScanner *fsm );
48
49 /* Execute some chunk of data. */
50 void XMLDeclScannerExecute( XMLDeclScanner *fsm, char *data, int dlen );
51
52 /* Indicate to the fsm tha there is no more data. */
53 void XMLDeclScannerFinish( XMLDeclScanner *fsm );
54
55 /* Did the machine accept? */
56 int XMLDeclScannerAccept( XMLDeclScanner *fsm );
57
58 # 25 "ragel_xmldeclscanner.c"
59
60
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
67
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
74
75 /* The array of functions. */
76 static int XMLDeclScanner_f[] = {
77         1, 0, 1, 1
78 };
79
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
115 };
116
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, 
144         77, 78, 79, 79, 0
145 };
146
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, 
192         0
193 };
194
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, 
217         0
218 };
219
220 /* The start state. */
221 static int *XMLDeclScanner_start = s+15;
222
223 /* Init the fsm to a runnable state. */
224 void XMLDeclScannerInit( XMLDeclScanner *fsm )
225 {
226         fsm->curState = XMLDeclScanner_start;
227         fsm->accept = 0;
228 # 29 "ragel_xmldeclscanner.c"
229
230                 fsm->mark_encodingstart = fsm->mark_encodingend = NULL;
231          }
232 # 233 "ragel_xmldeclscanner_out.c"
233 }
234
235 /* Did the fsm accept? */
236 int XMLDeclScannerAccept( XMLDeclScanner *fsm )
237 {
238         return fsm->accept;
239 }
240
241 /* Binary search an array of keys looking for a key. */
242 static char *XMLDeclScannerBSearch( char c, char *keys, int len )
243 {
244         char *lower = keys;
245         char *mid;
246         char *upper = keys + len - 1;
247         while (1) {
248                 if ( upper < lower )
249                         return 0;
250
251                 /* Find the midpoint. */
252                 mid = lower + ((upper-lower) >> 1);
253
254                 if ( c < *mid )
255                         upper = mid - 1;
256                 else if ( c > *mid )
257                         lower = mid + 1;
258                 else
259                         return mid;
260         }
261 }
262
263 /* Binary search an array of keys looking for a key. */
264 static char *XMLDeclScannerRangeBSearch( char c, char *keys, int len )
265 {
266         char *lower = keys;
267         char *mid;
268         char *upper = keys + len - 2;
269         while (1) {
270                 if ( upper < lower )
271                         return 0;
272
273                 /* Find the midpoint. Be sure to settle on the
274                  * lower end of a range. */
275                 mid = lower + (((upper-lower) >> 1) & ~1);
276
277                 if ( c < mid[0] )
278                         upper = mid - 2;
279                 else if ( c > mid[1] )
280                         lower = mid + 2;
281                 else {
282                         /* The key was found in the range mid, return it. */
283                         return mid;
284                 }
285         }
286 }
287
288 /* Execute the fsm on some chunk of data. */
289 void XMLDeclScannerExecute( XMLDeclScanner *fsm, char *data, int dlen )
290 {
291         char *p = data;
292         int len = dlen;
293         int *cs = fsm->curState;
294         int specs, **trans, *funcs, nfuncs;
295         char *keys;
296         unsigned char *inds;
297
298         if ( data == 0 )
299                 goto finishInput;
300
301 again:
302         if ( cs == 0 || len == 0 )
303                 goto out;
304
305         /* Get required data. */
306         specs = *cs++;
307         keys = k + *cs++;
308         inds = i + *cs++;
309
310         /* Try flat index. */
311         if ( specs & SPEC_ANY_FLAT ) {
312                 int indsLen = *cs++;
313                 keys += 2;
314                 inds += indsLen;
315         }
316
317         /* Try binary search single. */
318         if ( specs & SPEC_ANY_SINGLE ) {
319                 /* Try to find the key. */
320                 int indsLen = *cs++;
321                 char *match = XMLDeclScannerBSearch( *p, keys, indsLen );
322
323                 if ( match != 0 ) {
324                         trans = t + (inds[match - keys]<<1);
325                         goto match;
326                 }
327
328                 /* Advance over the keys and indicies. */
329                 keys += indsLen;
330                 inds += indsLen;
331         }
332
333         /* Try binary search range. */
334         if ( specs & SPEC_ANY_RANGE ) {
335                 /* Try to find the key. */
336                 int indsLen = *cs++;
337                 char *match = XMLDeclScannerRangeBSearch( *p, keys, (indsLen<<1) );
338
339                 if ( match != 0 ) {
340                         trans = t + (inds[(match - keys)>>1]<<1);
341                         goto match;
342                 }
343
344                 /* Advance over the keys and indicies. */
345                 keys += (indsLen<<1);
346                 inds += indsLen;
347         }
348
349         /* Try the default transition. */
350         if ( specs & SPEC_ANY_DEF ) {
351                 trans = t + ((*inds)<<1);
352                 goto match;
353         }
354
355         /* No match. */
356         cs = 0;
357         goto out;
358
359 match:
360         /* Move to the new state. */
361         cs = *trans++;
362
363         /* Check for functions. */
364         if ( (funcs=*trans) == 0 )
365                 goto noFuncs;
366
367 execFuncs:
368         nfuncs = *funcs++;
369         while ( nfuncs-- > 0 ) {
370                 switch ( *funcs++ ) {
371         case 0:
372 # 34 "ragel_xmldeclscanner.c"
373         {
374                 DEBUG && fprintf(stderr, "encodingstart: %d %c\n", p - data, *p);
375                 fsm->mark_encodingstart = p;
376         } break;
377         case 1:
378 # 39 "ragel_xmldeclscanner.c"
379         {
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);
383         } break;
384 # 385 "ragel_xmldeclscanner_out.c"
385                 }
386         }
387
388 noFuncs:
389         p++, len--;
390         goto again;
391
392 finishInput:
393         if ( cs != 0 && *cs & SPEC_IS_FINAL ) {
394                 /* The machine accepts. */
395                 fsm->accept = 1;
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);
400                         len = 1;
401                         goto execFuncs;
402                 }
403         }
404         else {
405                 /* If we are not in a final state then this
406                  * is an error. Move to the error state. */
407                 fsm->curState = 0;
408         }
409
410 out:
411         fsm->curState = cs;
412 }
413
414 /* Indicate to the fsm that the input is done. Does cleanup tasks. */
415 void XMLDeclScannerFinish( XMLDeclScanner *fsm )
416 {
417         XMLDeclScannerExecute( fsm, 0, 0 );
418 }
419
420 #undef f
421 #undef s
422 #undef k
423 #undef i
424 #undef t
425 #undef SPEC_ANY_FLAT
426 #undef SPEC_ANY_SINGLE
427 #undef SPEC_ANY_RANGE
428 #undef SPEC_ANY_DEF
429 #undef SPEC_IS_FINAL
430 #undef SPEC_OUT_FUNC
431
432 # 66 "ragel_xmldeclscanner.c"
433
434
435
436
437
438 unsigned int getEncodingFromXmlDecl(char *data, int len) {
439
440         int encodinglength, result;
441         CFStringRef encodingString;
442         
443         XMLDeclScanner scanner, *machine = &scanner;
444
445         XMLDeclScannerInit(machine);
446         XMLDeclScannerExecute(machine, data, len > 512 ? 512 : len);
447         XMLDeclScannerFinish(machine);
448
449         if (!XMLDeclScannerAccept(machine)) {
450                 return 0;
451         }
452
453         encodinglength = machine->mark_encodingend - machine->mark_encodingstart;
454
455
456         encodingString = CFStringCreateWithBytes (
457                 NULL,
458                 machine->mark_encodingstart,
459                 (CFIndex)encodinglength,
460                 kCFStringEncodingASCII,
461                 0
462         );
463
464         result = CFStringConvertIANACharSetNameToEncoding(encodingString);
465         CFRelease(encodingString);
466
467         if (result == kCFStringEncodingInvalidId)
468                 return 0;
469                 
470         result = CFStringConvertEncodingToNSStringEncoding(result);
471         
472         if (result == kCFStringEncodingInvalidId)
473                 return 0;
474
475         return result;
476         
477 }
478
479
480
481 /*      No longer used, now using CoreFoundation 
482
483         struct encodingPair {
484                 int encoding;
485                 char *matchstring;
486         };
487
488         struct encodingPair encodingPairs[] = {
489                 {NSUTF8StringEncoding, "UTF-8"},
490                 {NSISOLatin1StringEncoding, "ISO-8859-1"},
491                 {0, NULL},
492         };
493
494
495
496         for (i = 0; encodingPairs[i].matchstring; i++) {
497
498                 testlength = strlen(encodingPairs[i].matchstring);
499                 if (!strncasecmp(machine->mark_encodingstart, encodingPairs[i].matchstring, encodinglength < testlength ? encodinglength : testlength)) {
500                         return encodingPairs[i].encoding;
501                 }
502                 
503         }
504
505         return 0;
506
507 */
508