1 | /*␊ |
2 | * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * ␊ |
5 | * Portions Copyright (c) 2003 Apple Computer, Inc. All Rights␊ |
6 | * Reserved. ␊ |
7 | * The contents of this file constitute Original Code as defined in and␊ |
8 | * are subject to the Apple Public Source License Version 2.0 (the␊ |
9 | * "License"). You may not use this file except in compliance with the␊ |
10 | * License. Please obtain a copy of the License at␊ |
11 | * http://www.apple.com/publicsource and read it before using this file.␊ |
12 | * ␊ |
13 | * This Original Code and all software distributed under the License are␊ |
14 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
17 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
18 | * License for the specific language governing rights and limitations␊ |
19 | * under the License.␊ |
20 | * ␊ |
21 | */␊ |
22 | ␊ |
23 | #include "bootstruct.h"␊ |
24 | #include "libsaio.h"␊ |
25 | #include "sl.h"␊ |
26 | #include "xml.h"␊ |
27 | ␊ |
28 | string_ref *ref_strings = NULL;␊ |
29 | ␊ |
30 | /// TODO: remove below␊ |
31 | static char* buffer_start = NULL;␊ |
32 | // TODO: redo the next two functions␊ |
33 | void SaveRefString(char* string, int id)␊ |
34 | {␊ |
35 | ␉//printf("Adding Ref String %d (%s)\n", id, string);␊ |
36 | ␉string_ref* tmp = ref_strings;␊ |
37 | ␉while(tmp)␊ |
38 | ␉{␊ |
39 | ␉␉if(tmp->id == id)␊ |
40 | ␉␉{␊ |
41 | ␉␉␉tmp->string = malloc(strlen(string)+1);␊ |
42 | ␉␉␉sprintf(tmp->string, "%s", string);␊ |
43 | ␉␉␉return;␊ |
44 | ␉␉}␊ |
45 | ␉␉tmp = tmp->next;␊ |
46 | ␉}␊ |
47 | ␉␊ |
48 | ␉string_ref* new_ref = malloc(sizeof(string_ref));␊ |
49 | ␉new_ref->string = malloc(strlen(string)+1);␊ |
50 | ␉sprintf(new_ref->string, "%s", string);␊ |
51 | ␉new_ref->id = id;␊ |
52 | ␉new_ref->next = ref_strings;␊ |
53 | ␉ref_strings = new_ref;␊ |
54 | }␊ |
55 | ␊ |
56 | char* GetRefString(int id)␊ |
57 | {␊ |
58 | ␉string_ref* tmp = ref_strings;␊ |
59 | ␉while(tmp)␊ |
60 | ␉{␊ |
61 | ␉␉if(tmp->id == id) return tmp->string;␊ |
62 | ␉␉tmp = tmp->next;␊ |
63 | ␉}␊ |
64 | ␉//verbose("Unable to locate Ref String %d\n", id);␊ |
65 | ␉return "";␊ |
66 | }␊ |
67 | ␊ |
68 | struct Module␊ |
69 | {␊ |
70 | ␉struct Module *nextModule;␊ |
71 | ␉long␉␉willLoad;␊ |
72 | ␉TagPtr␉␉dict;␊ |
73 | ␉char␉␉* plistAddr;␊ |
74 | ␉long␉␉plistLength;␊ |
75 | ␉char␉␉* driverPath;␊ |
76 | };␊ |
77 | typedef struct Module Module, *ModulePtr;␊ |
78 | ␊ |
79 | struct DriverInfo␊ |
80 | {␊ |
81 | ␉char␉* plistAddr;␊ |
82 | ␉long␉plistLength;␊ |
83 | ␉void␉* moduleAddr;␊ |
84 | ␉long␉moduleLength;␊ |
85 | };␊ |
86 | typedef struct DriverInfo DriverInfo, *DriverInfoPtr;␊ |
87 | ␊ |
88 | #define kDriverPackageSignature1 'MKXT'␊ |
89 | #define kDriverPackageSignature2 'MOSX'␊ |
90 | ␊ |
91 | struct DriversPackage␊ |
92 | {␊ |
93 | ␉unsigned long signature1;␊ |
94 | ␉unsigned long signature2;␊ |
95 | ␉unsigned long length;␊ |
96 | ␉unsigned long adler32;␊ |
97 | ␉unsigned long version;␊ |
98 | ␉unsigned long numDrivers;␊ |
99 | ␉unsigned long reserved1;␊ |
100 | ␉unsigned long reserved2;␊ |
101 | };␊ |
102 | typedef struct DriversPackage DriversPackage;␊ |
103 | ␊ |
104 | enum␊ |
105 | {␊ |
106 | ␉kCFBundleType2,␊ |
107 | ␉kCFBundleType3␊ |
108 | };␊ |
109 | ␊ |
110 | ␊ |
111 | #define DOFREE 1␊ |
112 | ␊ |
113 | static long ParseTagList(char *buffer, TagPtr *tag, long type, long empty);␊ |
114 | static long ParseTagKey(char *buffer, TagPtr *tag);␊ |
115 | static long ParseTagString(char *buffer, TagPtr *tag);␊ |
116 | static long ParseTagInteger(char *buffer, TagPtr *tag);␊ |
117 | static long ParseTagData(char *buffer, TagPtr *tag);␊ |
118 | static long ParseTagDate(char *buffer, TagPtr *tag);␊ |
119 | static long GetNextTag(char *buffer, char **tag, long *start);␊ |
120 | static long FixDataMatchingTag(char *buffer, char *tag);␊ |
121 | static TagPtr NewTag(void);␊ |
122 | static char *NewSymbol(char *string);␊ |
123 | #if DOFREE␊ |
124 | static void FreeSymbol(char *string);␊ |
125 | #endif␊ |
126 | ␊ |
127 | ␊ |
128 | //==========================================================================␊ |
129 | // XMLGetProperty␊ |
130 | ␊ |
131 | TagPtr XMLGetProperty(TagPtr dict, const char * key)␊ |
132 | {␊ |
133 | ␉if (dict->type != kTagTypeDict)␊ |
134 | ␉{␊ |
135 | ␉␉return 0;␊ |
136 | ␉}␊ |
137 | ␊ |
138 | ␉TagPtr tag = 0;␊ |
139 | ␉TagPtr tagList = dict->tag;␊ |
140 | ␊ |
141 | ␉while (tagList)␊ |
142 | ␉{␊ |
143 | ␉␉tag = tagList;␊ |
144 | ␉␉tagList = tag->tagNext;␊ |
145 | ␊ |
146 | ␉␉if ((tag->type != kTagTypeKey) || (tag->string == 0))␊ |
147 | ␉␉{␊ |
148 | ␉␉␉continue;␊ |
149 | ␉␉}␊ |
150 | ␊ |
151 | ␉␉if (!strcmp(tag->string, key))␊ |
152 | ␉␉{␊ |
153 | ␉␉␉return tag->tag;␊ |
154 | ␉␉}␊ |
155 | ␉}␊ |
156 | ␊ |
157 | ␉return 0;␊ |
158 | }␊ |
159 | ␊ |
160 | //==========================================================================␊ |
161 | // XMLGetProperty␊ |
162 | ␊ |
163 | TagPtr␊ |
164 | XMLGetKey( TagPtr dict, int id )␊ |
165 | {␊ |
166 | TagPtr tagList, tag;␊ |
167 | ␊ |
168 | if (dict->type != kTagTypeDict) return 0;␊ |
169 | ␊ |
170 | tag = 0;␊ |
171 | int element = 0;␊ |
172 | tagList = dict->tag;␊ |
173 | while (tagList && element != id)␊ |
174 | {␊ |
175 | tag = tagList;␊ |
176 | tagList = tag->tagNext;␊ |
177 | ␊ |
178 | if ((tag->type != kTagTypeKey) || (tag->string == 0)) continue;␊ |
179 | element++;␊ |
180 | if(id == element) return tag;␊ |
181 | }␊ |
182 | return 0;␊ |
183 | }␊ |
184 | ␊ |
185 | TagPtr XMLGetValueForKey(TagPtr key)␊ |
186 | {␊ |
187 | if (!key ||␊ |
188 | key->type != kTagTypeKey) return 0;␊ |
189 | ␊ |
190 | return key->tag;␊ |
191 | }␊ |
192 | ␊ |
193 | ␊ |
194 | // XMLGetTag(int index)␊ |
195 | ␊ |
196 | // XMLTagCount( TagPtr dict )␊ |
197 | int XMLTagCount( TagPtr dict )␊ |
198 | {␊ |
199 | ␉int count = 0;␊ |
200 | ␉TagPtr tagList, tag;␊ |
201 | ␊ |
202 | if (dict->type != kTagTypeDict && dict->type != kTagTypeArray) return 0;␊ |
203 | ␉tag = 0;␊ |
204 | tagList = dict->tag;␊ |
205 | while (tagList)␊ |
206 | {␊ |
207 | ␉␉tag = tagList;␊ |
208 | tagList = tag->tagNext;␊ |
209 | ␉␉␊ |
210 | ␉␉if (((tag->type != kTagTypeKey) && ((tag->string == 0) || (tag->string[0] == 0)))␊ |
211 | ␉␉␉&& (dict->type != kTagTypeArray)␉// If we are an array, any element is valid␊ |
212 | ␉␉␉) continue;␊ |
213 | ␉␉␊ |
214 | ␉␉//if(tag->type == kTagTypeKey) printf("Located key %s\n", tag->string);␊ |
215 | ␊ |
216 | ␉␉count++;␊ |
217 | }␊ |
218 | ␉␊ |
219 | ␉return count;␊ |
220 | }␊ |
221 | ␊ |
222 | TagPtr XMLGetElement( TagPtr dict, int id )␊ |
223 | {␊ |
224 | ␉if(dict->type != kTagTypeArray) return 0;␊ |
225 | ␉␊ |
226 | ␉int element = 0;␊ |
227 | ␉TagPtr tmp = dict->tag;␊ |
228 | ␊ |
229 | ␉while(element < id)␊ |
230 | ␉{␊ |
231 | ␉␉element++;␊ |
232 | ␉␉tmp = tmp->tagNext;␊ |
233 | ␉}␊ |
234 | ␉␊ |
235 | ␉return tmp;␊ |
236 | }␊ |
237 | /* Function for basic XML character entities parsing */␊ |
238 | ␊ |
239 | char*␊ |
240 | XMLDecode(const char* src)␊ |
241 | {␊ |
242 | typedef const struct XMLEntity {␊ |
243 | const char* name;␊ |
244 | size_t nameLen;␊ |
245 | char value;␊ |
246 | } XMLEntity;␊ |
247 | ␊ |
248 | /* This is ugly, but better than specifying the lengths by hand */␊ |
249 | #define _e(str,c) {str,sizeof(str)-1,c}␊ |
250 | const XMLEntity ents[] = {␊ |
251 | _e("quot;",'"'), _e("apos;",'\''),␊ |
252 | _e("lt;", '<'), _e("gt;", '>'),␊ |
253 | _e("amp;", '&')␊ |
254 | };␊ |
255 | ␊ |
256 | size_t len;␊ |
257 | const char *s;␊ |
258 | char *out, *o;␊ |
259 | ␊ |
260 | if ( !src || !(len = strlen(src)) || !(out = malloc(len+1)) )␊ |
261 | return 0;␊ |
262 | ␊ |
263 | o = out;␊ |
264 | s = src;␊ |
265 | while (s <= src+len) /* Make sure the terminator is also copied */␊ |
266 | {␊ |
267 | if ( *s == '&' )␊ |
268 | {␊ |
269 | bool entFound = false;␊ |
270 | int i;␊ |
271 | ␊ |
272 | s++;␊ |
273 | for ( i = 0; i < sizeof(ents); i++)␊ |
274 | {␊ |
275 | if ( strncmp(s, ents[i].name, ents[i].nameLen) == 0 )␊ |
276 | {␊ |
277 | entFound = true;␊ |
278 | break;␊ |
279 | }␊ |
280 | }␊ |
281 | if ( entFound )␊ |
282 | {␊ |
283 | *o++ = ents[i].value;␊ |
284 | s += ents[i].nameLen;␊ |
285 | continue;␊ |
286 | }␊ |
287 | }␊ |
288 | ␊ |
289 | *o++ = *s++;␊ |
290 | }␊ |
291 | ␊ |
292 | return out;␊ |
293 | } ␊ |
294 | ␊ |
295 | //#if UNUSED␊ |
296 | //==========================================================================␊ |
297 | // XMLParseFile␊ |
298 | // Expects to see one dictionary in the XML file, the final pos will be returned␊ |
299 | // If the pos is not equal to the strlen, then there are multiple dicts␊ |
300 | // Puts the first dictionary it finds in the␊ |
301 | // tag pointer and returns the end of the dic, or returns -1 if not found.␊ |
302 | //␊ |
303 | long␊ |
304 | XMLParseFile( char * buffer, TagPtr * dict )␊ |
305 | {␊ |
306 | long length, pos;␊ |
307 | TagPtr tag;␊ |
308 | pos = 0;␊ |
309 | ␉char *configBuffer;␊ |
310 | ␉␊ |
311 | int strlength = strlen(buffer);␊ |
312 | configBuffer = malloc(strlength+1);␊ |
313 | bcopy(buffer, configBuffer, strlength);␊ |
314 | configBuffer[strlength] = 0;␊ |
315 | ␊ |
316 | ␉buffer_start = configBuffer;␊ |
317 | ␊ |
318 | while (1)␊ |
319 | {␊ |
320 | length = XMLParseNextTag(configBuffer + pos, &tag);␊ |
321 | if (length == -1) break;␊ |
322 | ␊ |
323 | pos += length;␊ |
324 | ␊ |
325 | if (tag == 0) continue;␊ |
326 | if (tag->type == kTagTypeDict) break;␊ |
327 | ␊ |
328 | XMLFreeTag(tag);␊ |
329 | }␊ |
330 | ␉free(configBuffer);␊ |
331 | ␉if (length < 0) {␊ |
332 | return -1;␊ |
333 | }␊ |
334 | *dict = tag;␊ |
335 | return pos;␊ |
336 | }␊ |
337 | //#endif /* UNUSED */␊ |
338 | ␊ |
339 | //==========================================================================␊ |
340 | // ParseNextTag␊ |
341 | // TODO: cleanup␊ |
342 | long␊ |
343 | XMLParseNextTag( char * buffer, TagPtr * tag )␊ |
344 | {␊ |
345 | ␉long length, pos;␊ |
346 | ␉char * tagName;␊ |
347 | ␉␊ |
348 | ␉length = GetNextTag(buffer, &tagName, 0);␊ |
349 | ␉if (length == -1) return -1;␊ |
350 | ␉␊ |
351 | ␉pos = length;␊ |
352 | ␉if (!strncmp(tagName, kXMLTagPList, 6))␊ |
353 | ␉{␊ |
354 | ␉␉length = 0;␊ |
355 | ␉}␊ |
356 | ␉/***** dict ****/␊ |
357 | ␉else if (!strcmp(tagName, kXMLTagDict))␊ |
358 | ␉{␊ |
359 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeDict, 0);␊ |
360 | ␉}␊ |
361 | ␉else if (!strncmp(tagName, kXMLTagDict, strlen(kXMLTagDict)) && tagName[strlen(tagName)-1] == '/')␊ |
362 | ␉{␊ |
363 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeDict, 1);␊ |
364 | ␉}␊ |
365 | ␉else if (!strncmp(tagName, kXMLTagDict " ", strlen(kXMLTagDict " ")))␊ |
366 | ␉{␊ |
367 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeDict, 0);␊ |
368 | ␉}␊ |
369 | ␉/***** key ****/␊ |
370 | ␉else if (!strcmp(tagName, kXMLTagKey))␊ |
371 | ␉{␊ |
372 | ␉␉length = ParseTagKey(buffer + pos, tag);␊ |
373 | ␉}␊ |
374 | ␉␊ |
375 | ␉/***** string ****/␊ |
376 | ␉else if (!strcmp(tagName, kXMLTagString))␊ |
377 | ␉{␊ |
378 | ␉␉length = ParseTagString(buffer + pos, tag);␊ |
379 | ␉}␊ |
380 | ␉else if (!strncmp(tagName, kXMLTagString " ", strlen(kXMLTagString " ")))␊ |
381 | ␉{␊ |
382 | ␉␉// TODO: save tag if if found␊ |
383 | ␉␉if(!strncmp(tagName + strlen(kXMLTagString " "), kXMLStringID, strlen(kXMLStringID)))␊ |
384 | ␉␉{␊ |
385 | ␉␉␉// ID=␊ |
386 | ␉␉␉int id = 0;␊ |
387 | ␉␉␉int cnt = strlen(kXMLTagString " " kXMLStringID "\"") + 1;␊ |
388 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
389 | ␉␉␉tagName[cnt] = 0;␊ |
390 | ␉␉␉char* val = tagName + strlen(kXMLTagString " " kXMLStringID "\"");␊ |
391 | ␉␉␉while(*val)␊ |
392 | ␉␉␉{␊ |
393 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
394 | ␉␉␉␉{␊ |
395 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
396 | ␉␉␉␉}␊ |
397 | ␉␉␉␉else␊ |
398 | ␉␉␉␉{␊ |
399 | ␉␉␉␉␉printf("ParseStringID error (0x%x)\n", *val);␊ |
400 | ␉␉␉␉␉getchar();␊ |
401 | ␉␉␉␉␉return -1;␊ |
402 | ␉␉␉␉}␊ |
403 | ␉␉␉}␊ |
404 | ␉␉␉length = ParseTagString(buffer + pos, tag);␊ |
405 | ␉␉␉␊ |
406 | ␉␉␉SaveRefString(buffer + pos, id);␊ |
407 | ␉␉}␊ |
408 | ␉␉else if(!strncmp(tagName + strlen(kXMLTagString " "), kXMLStringIDRef, strlen(kXMLStringIDRef)))␊ |
409 | ␉␉{␊ |
410 | ␉␉␉// IDREF=␊ |
411 | ␉␉␉int id = 0;␊ |
412 | ␉␉␉int cnt = strlen(kXMLTagString " " kXMLStringIDRef "\"") + 1;␊ |
413 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
414 | ␉␉␉tagName[cnt] = 0;␊ |
415 | ␉␉␉char* val = tagName + strlen(kXMLTagString " " kXMLStringIDRef "\"");␊ |
416 | ␉␉␉while(*val)␊ |
417 | ␉␉␉{␊ |
418 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
419 | ␉␉␉␉{␊ |
420 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
421 | ␉␉␉␉}␊ |
422 | ␉␉␉␉else␊ |
423 | ␉␉␉␉{␊ |
424 | ␉␉␉␉␉printf("ParseStringIDREF error (0x%x)\n", *val);␊ |
425 | ␉␉␉␉␉getchar();␊ |
426 | ␉␉␉␉␉return -1;␊ |
427 | ␉␉␉␉}␊ |
428 | ␉␉␉}␊ |
429 | ␉␉␉char* str = GetRefString(id);␊ |
430 | ␊ |
431 | ␉␉␉TagPtr tmpTag = NewTag();␊ |
432 | ␉␉␉tmpTag->type = kTagTypeString;␊ |
433 | ␉␉␉tmpTag->string = str;␊ |
434 | ␉␉␉tmpTag->tag = 0;␊ |
435 | ␉␉␉tmpTag->tagNext = 0;␊ |
436 | ␉␉␉tmpTag->offset = buffer_start ? buffer - buffer_start + pos : 0;␊ |
437 | ␉␉␉*tag = tmpTag;␊ |
438 | ␉␉␉␊ |
439 | ␉␉␉length = 0;␊ |
440 | ␉␉␉//printf("Located IDREF, id = %d, string = %s\n", id, str);␊ |
441 | ␉␉}␊ |
442 | ␉}␊ |
443 | ␉␊ |
444 | ␉/***** integer ****/␊ |
445 | ␉else if (!strcmp(tagName, kXMLTagInteger))␊ |
446 | ␉{␊ |
447 | ␉␉length = ParseTagInteger(buffer + pos, tag);␊ |
448 | ␉}␊ |
449 | ␉else if (!strncmp(tagName, kXMLTagInteger " ", strlen(kXMLTagInteger " ")))␊ |
450 | ␉{␊ |
451 | ␉␉if(!strncmp(tagName + strlen(kXMLTagInteger " "), kXMLStringID, strlen(kXMLStringID)))␊ |
452 | ␉␉{␊ |
453 | ␉␉␉// ID=␊ |
454 | ␉␉␉int id = 0;␊ |
455 | ␉␉␉int cnt = strlen(kXMLTagInteger " " kXMLStringID "\"") + 1;␊ |
456 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
457 | ␉␉␉tagName[cnt] = 0;␊ |
458 | ␉␉␉char* val = tagName + strlen(kXMLTagInteger " " kXMLStringID "\"");␊ |
459 | ␉␉␉while(*val)␊ |
460 | ␉␉␉{␊ |
461 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
462 | ␉␉␉␉{␊ |
463 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
464 | ␉␉␉␉}␊ |
465 | ␉␉␉␉else␊ |
466 | ␉␉␉␉{␊ |
467 | ␉␉␉␉␉printf("ParseIntegerID error (0x%x)\n", *val);␊ |
468 | ␉␉␉␉␉getchar();␊ |
469 | ␉␉␉␉␉return -1;␊ |
470 | ␉␉␉␉}␊ |
471 | ␉␉␉}␊ |
472 | ␉␉␉length = ParseTagInteger(buffer + pos, tag);␊ |
473 | ␉␉␉␊ |
474 | ␉␉␉SaveRefString((*tag)->string, id);␊ |
475 | ␉␉}␊ |
476 | ␉␉else if(!strncmp(tagName + strlen(kXMLTagInteger " "), kXMLStringIDRef, strlen(kXMLStringIDRef)))␊ |
477 | ␉␉{␊ |
478 | ␉␉␉// IDREF=␊ |
479 | ␉␉␉int id = 0;␊ |
480 | ␉␉␉int cnt = strlen(kXMLTagInteger " " kXMLStringIDRef "\"") + 1;␊ |
481 | ␉␉␉while ((tagName[cnt] != '\0') && (tagName[cnt] != '"')) cnt++;␊ |
482 | ␉␉␉tagName[cnt] = 0;␊ |
483 | ␉␉␉char* val = tagName + strlen(kXMLTagInteger " " kXMLStringIDRef "\"");␊ |
484 | ␉␉␉while(*val)␊ |
485 | ␉␉␉{␊ |
486 | ␉␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
487 | ␉␉␉␉{␊ |
488 | ␉␉␉␉␉id = (id * 10) + (*val++ - '0');␊ |
489 | ␉␉␉␉}␊ |
490 | ␉␉␉␉else␊ |
491 | ␉␉␉␉{␊ |
492 | ␉␉␉␉␉printf("ParseStringIDREF error (0x%x)\n", *val);␊ |
493 | ␉␉␉␉␉getchar();␊ |
494 | ␉␉␉␉␉return -1;␊ |
495 | ␉␉␉␉}␊ |
496 | ␉␉␉}␊ |
497 | ␉␉␉int integer = (int)GetRefString(id);␊ |
498 | ␉␉␉␊ |
499 | ␉␉␉TagPtr tmpTag = NewTag();␊ |
500 | ␉␉␉tmpTag->type = kTagTypeInteger;␊ |
501 | ␉␉␉tmpTag->string = (char*) integer;␊ |
502 | ␉␉␉tmpTag->tag = 0;␊ |
503 | ␉␉␉tmpTag->tagNext = 0;␊ |
504 | ␉␉␉tmpTag->offset = buffer_start ? buffer - buffer_start + pos : 0;␊ |
505 | ␉␉␉␊ |
506 | ␉␉␉*tag = tmpTag;␊ |
507 | ␉␉␉␊ |
508 | ␉␉␉length = 0;␊ |
509 | ␉␉␉//printf("Located IDREF, id = %d, string = %s\n", id, str);␊ |
510 | ␉␉}␊ |
511 | ␉␉else␊ |
512 | ␉␉{␊ |
513 | ␉␉␉length = ParseTagInteger(buffer + pos, tag);␊ |
514 | ␉␉}␊ |
515 | ␉}␊ |
516 | ␉␊ |
517 | ␉/***** data ****/␊ |
518 | ␉else if (!strcmp(tagName, kXMLTagData))␊ |
519 | ␉{␊ |
520 | ␉␉length = ParseTagData(buffer + pos, tag);␊ |
521 | ␉}␊ |
522 | ␉else if (!strncmp(tagName, kXMLTagData " ", strlen(kXMLTagData " ")))␊ |
523 | ␉{␊ |
524 | ␉␉length = ParseTagData(buffer + pos, tag);␊ |
525 | ␉}␊ |
526 | ␉else if (!strcmp(tagName, kXMLTagDate))␊ |
527 | ␉{␊ |
528 | ␉␉length = ParseTagDate(buffer + pos, tag);␊ |
529 | ␉}␊ |
530 | ␉␊ |
531 | ␉/***** date ****/␊ |
532 | ␉else if (!strncmp(tagName, kXMLTagDate " ", strlen(kXMLTagDate " ")))␊ |
533 | ␉{␊ |
534 | ␉␉length = ParseTagDate(buffer + pos, tag);␊ |
535 | ␉}␊ |
536 | ␉␊ |
537 | ␉/***** false ****/␊ |
538 | ␉else if (!strcmp(tagName, kXMLTagFalse))␊ |
539 | ␉{␊ |
540 | ␉␉length = ParseTagBoolean(buffer + pos, tag, kTagTypeFalse);␊ |
541 | ␉}␊ |
542 | ␉/***** true ****/␊ |
543 | ␉else if (!strcmp(tagName, kXMLTagTrue))␊ |
544 | ␉{␊ |
545 | ␉␉length = ParseTagBoolean(buffer + pos, tag, kTagTypeTrue);␊ |
546 | ␉}␊ |
547 | ␉␊ |
548 | ␉/***** array ****/␊ |
549 | ␉else if (!strcmp(tagName, kXMLTagArray))␊ |
550 | ␉{␊ |
551 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeArray, 0);␊ |
552 | ␉}␊ |
553 | ␉else if (!strncmp(tagName, kXMLTagArray " ", strlen(kXMLTagArray " ")))␊ |
554 | ␉{␊ |
555 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeArray, 0);␊ |
556 | ␉}␊ |
557 | ␉else if (!strcmp(tagName, kXMLTagArray "/"))␊ |
558 | ␉{␊ |
559 | ␉␉length = ParseTagList(buffer + pos, tag, kTagTypeArray, 1);␊ |
560 | ␉}␊ |
561 | ␉␊ |
562 | ␉/***** unknown ****/␊ |
563 | ␉else␊ |
564 | ␉{␊ |
565 | ␉␉*tag = 0;␊ |
566 | ␉␉length = 0;␊ |
567 | ␉}␊ |
568 | ␉␊ |
569 | ␉if (length == -1) return -1;␊ |
570 | ␉␊ |
571 | ␉return pos + length;␊ |
572 | }␊ |
573 | ␊ |
574 | //==========================================================================␊ |
575 | // ParseTagList␊ |
576 | ␊ |
577 | static long␊ |
578 | ParseTagList( char * buffer, TagPtr * tag, long type, long empty )␊ |
579 | {␊ |
580 | ␉long length, pos;␊ |
581 | ␉TagPtr tagList, tmpTag;␊ |
582 | ␊ |
583 | tagList = 0;␊ |
584 | pos = 0;␊ |
585 | ␊ |
586 | if (!empty)␊ |
587 | {␊ |
588 | while (1)␊ |
589 | {␊ |
590 | length = XMLParseNextTag(buffer + pos, &tmpTag);␊ |
591 | if (length == -1) break;␊ |
592 | ␊ |
593 | pos += length;␊ |
594 | ␊ |
595 | if (tmpTag == 0) break;␊ |
596 | tmpTag->tagNext = tagList;␊ |
597 | tagList = tmpTag;␊ |
598 | }␊ |
599 | ␊ |
600 | if (length == -1)␊ |
601 | {␊ |
602 | XMLFreeTag(tagList);␊ |
603 | return -1;␊ |
604 | }␊ |
605 | }␊ |
606 | ␊ |
607 | tmpTag = NewTag();␊ |
608 | if (tmpTag == 0)␊ |
609 | {␊ |
610 | XMLFreeTag(tagList);␊ |
611 | return -1;␊ |
612 | }␊ |
613 | ␊ |
614 | tmpTag->type = type;␊ |
615 | tmpTag->string = 0;␊ |
616 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start : 0;␊ |
617 | tmpTag->tag = tagList;␊ |
618 | tmpTag->tagNext = 0;␊ |
619 | ␊ |
620 | *tag = tmpTag;␊ |
621 | ␊ |
622 | return pos;␊ |
623 | }␊ |
624 | ␊ |
625 | //==========================================================================␊ |
626 | // ParseTagKey␊ |
627 | ␊ |
628 | static long␊ |
629 | ParseTagKey( char * buffer, TagPtr * tag )␊ |
630 | {␊ |
631 | long length, length2;␊ |
632 | char *string;␊ |
633 | TagPtr tmpTag, subTag;␊ |
634 | ␊ |
635 | length = FixDataMatchingTag(buffer, kXMLTagKey);␊ |
636 | if (length == -1) return -1;␊ |
637 | ␊ |
638 | length2 = XMLParseNextTag(buffer + length, &subTag);␊ |
639 | if (length2 == -1) return -1;␊ |
640 | ␊ |
641 | tmpTag = NewTag();␊ |
642 | if (tmpTag == 0)␊ |
643 | {␊ |
644 | XMLFreeTag(subTag);␊ |
645 | return -1;␊ |
646 | }␊ |
647 | ␊ |
648 | string = NewSymbol(buffer);␊ |
649 | if (string == 0)␊ |
650 | {␊ |
651 | XMLFreeTag(subTag);␊ |
652 | XMLFreeTag(tmpTag);␊ |
653 | return -1;␊ |
654 | }␊ |
655 | ␊ |
656 | tmpTag->type = kTagTypeKey;␊ |
657 | tmpTag->string = string;␊ |
658 | tmpTag->tag = subTag;␊ |
659 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
660 | tmpTag->tagNext = 0;␊ |
661 | ␊ |
662 | *tag = tmpTag;␊ |
663 | ␊ |
664 | return length + length2;␊ |
665 | }␊ |
666 | ␊ |
667 | //==========================================================================␊ |
668 | // ParseTagString␊ |
669 | ␊ |
670 | static long␊ |
671 | ParseTagString( char * buffer, TagPtr * tag )␊ |
672 | {␊ |
673 | long length;␊ |
674 | char * string;␊ |
675 | ␊ |
676 | length = FixDataMatchingTag(buffer, kXMLTagString);␊ |
677 | if (length == -1) return -1;␊ |
678 | ␊ |
679 | ␉TagPtr tmpTag = NewTag();␊ |
680 | if (tmpTag == 0) return -1;␊ |
681 | ␊ |
682 | string = NewSymbol(buffer);␊ |
683 | if (string == 0)␊ |
684 | {␊ |
685 | XMLFreeTag(tmpTag);␊ |
686 | return -1;␊ |
687 | }␊ |
688 | ␊ |
689 | tmpTag->type = kTagTypeString;␊ |
690 | tmpTag->string = string;␊ |
691 | tmpTag->tag = 0;␊ |
692 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
693 | tmpTag->tagNext = 0;␊ |
694 | ␊ |
695 | *tag = tmpTag;␊ |
696 | return length;␊ |
697 | }␊ |
698 | ␊ |
699 | //==========================================================================␊ |
700 | // ParseTagInteger␊ |
701 | ␊ |
702 | static long␊ |
703 | ParseTagInteger( char * buffer, TagPtr * tag )␊ |
704 | {␊ |
705 | long length, integer;␊ |
706 | ␉bool negative = false;␊ |
707 | TagPtr tmpTag;␊ |
708 | ␉char* val = buffer;␊ |
709 | int size;␊ |
710 | ␉␊ |
711 | ␉if(buffer[0] == '<')␊ |
712 | ␉{␊ |
713 | ␉␉printf("Warning integer is non existant\n");␊ |
714 | ␉␉getchar();␊ |
715 | ␉␉tmpTag = NewTag();␊ |
716 | ␉␉tmpTag->type = kTagTypeInteger;␊ |
717 | ␉␉tmpTag->string = 0;␊ |
718 | ␉␉tmpTag->tag = 0;␊ |
719 | ␉␉tmpTag->offset = 0;␊ |
720 | ␉␉tmpTag->tagNext = 0;␊ |
721 | ␉␉␊ |
722 | ␉␉*tag = tmpTag;␊ |
723 | ␉␉␊ |
724 | ␉␉return 0;␊ |
725 | ␉}␊ |
726 | ␉␊ |
727 | ␉size = length = FixDataMatchingTag(buffer, kXMLTagInteger);␊ |
728 | ␉if (length == -1) return -1;␊ |
729 | ␊ |
730 | ␉tmpTag = NewTag();␊ |
731 | ␉if (tmpTag == 0) return -1;␊ |
732 | ␊ |
733 | ␉integer = 0;␊ |
734 | ␊ |
735 | ␉if(size > 1 && (val[1] == 'x' || val[1] == 'X'))␉// Hex value␊ |
736 | ␉{␊ |
737 | ␉␉val += 2;␊ |
738 | ␉␉while(*val)␊ |
739 | ␉␉{␊ |
740 | ␉␉␉if ((*val >= '0' && *val <= '9'))␉// 0 - 9␊ |
741 | ␉␉␉{␊ |
742 | ␉␉␉␉integer = (integer * 16) + (*val++ - '0');␊ |
743 | ␉␉␉}␊ |
744 | ␉␉␉else if ((*val >= 'a' && *val <= 'f'))␉// a - f␊ |
745 | ␉␉␉{␊ |
746 | ␉␉␉␉integer = (integer * 16) + (*val++ - 'a' + 10);␊ |
747 | ␉␉␉}␊ |
748 | ␉␉␉else if ((*val >= 'A' && *val <= 'F'))␉// A - F␊ |
749 | ␉␉␉{␊ |
750 | ␉␉␉␉integer = (integer * 16) + (*val++ - 'a' + 10);␊ |
751 | ␉␉␉}␊ |
752 | ␉␉␉else␊ |
753 | ␉␉␉{␊ |
754 | ␉␉␉␉printf("ParseTagInteger hex error (0x%x) in buffer %s\n", *val, buffer);␊ |
755 | ␉␉␉␉getchar();␊ |
756 | ␉␉␉␉return -1;␊ |
757 | ␉␉␉}␊ |
758 | ␉␉}␊ |
759 | ␉}␊ |
760 | ␉else if ( size )␉// Decimal value␊ |
761 | ␉{␊ |
762 | ␉␉if (*val == '-')␊ |
763 | ␉␉{␊ |
764 | ␉␉␉negative = true;␊ |
765 | ␉␉␉val++;␊ |
766 | ␉␉␉size--;␊ |
767 | ␉␉}␊ |
768 | ␉␉␊ |
769 | ␉␉for (integer = 0; size > 0; size--)␊ |
770 | ␉␉{␊ |
771 | ␉␉␉if(*val) // UGLY HACK, fix me.␊ |
772 | ␉␉␉{␊ |
773 | ␉␉␉␉if (*val < '0' || *val > '9')␊ |
774 | ␉␉␉␉{␊ |
775 | ␉␉␉␉␉printf("ParseTagInteger decimal error (0x%x) in buffer %s\n", *val, buffer);␊ |
776 | ␉␉␉␉␉getchar();␊ |
777 | ␉␉␉␉␉return -1;␊ |
778 | ␉␉␉␉}␊ |
779 | ␉␉␉␉␊ |
780 | ␉␉␉␉integer = (integer * 10) + (*val++ - '0');␊ |
781 | ␉␉␉}␊ |
782 | ␉␉}␊ |
783 | ␉␉␊ |
784 | ␉␉if (negative)␊ |
785 | ␉␉␉integer = -integer;␊ |
786 | ␉}␊ |
787 | ␉␉␊ |
788 | tmpTag->type = kTagTypeInteger;␊ |
789 | ␉tmpTag->string = (char *)integer;␊ |
790 | ␉tmpTag->tag = 0;␊ |
791 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
792 | tmpTag->tagNext = 0;␊ |
793 | ␊ |
794 | *tag = tmpTag;␊ |
795 | ␊ |
796 | return length;␊ |
797 | }␊ |
798 | ␊ |
799 | //==========================================================================␊ |
800 | // ParseTagData␊ |
801 | ␊ |
802 | static long␊ |
803 | ParseTagData( char * buffer, TagPtr * tag )␊ |
804 | {␊ |
805 | long length;␊ |
806 | TagPtr tmpTag;␊ |
807 | ␊ |
808 | length = FixDataMatchingTag(buffer, kXMLTagData);␊ |
809 | if (length == -1) return -1;␊ |
810 | ␊ |
811 | tmpTag = NewTag();␊ |
812 | if (tmpTag == 0) return -1;␊ |
813 | ␊ |
814 | ␉//printf("ParseTagData unimplimented\n");␊ |
815 | ␉//printf("Data: %s\n", buffer);␊ |
816 | ␉//␉getchar();␊ |
817 | ␉␊ |
818 | ␉// TODO: base64 decode␊ |
819 | ␉␊ |
820 | ␉char* string = NewSymbol(buffer);␊ |
821 | tmpTag->type = kTagTypeData;␊ |
822 | tmpTag->string = string;␊ |
823 | tmpTag->tag = 0;␊ |
824 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
825 | tmpTag->tagNext = 0;␊ |
826 | ␊ |
827 | *tag = tmpTag;␊ |
828 | ␊ |
829 | return length;␊ |
830 | }␊ |
831 | ␊ |
832 | //==========================================================================␊ |
833 | // ParseTagDate␊ |
834 | ␊ |
835 | static long␊ |
836 | ParseTagDate( char * buffer, TagPtr * tag )␊ |
837 | {␊ |
838 | long length;␊ |
839 | TagPtr tmpTag;␊ |
840 | ␊ |
841 | length = FixDataMatchingTag(buffer, kXMLTagDate);␊ |
842 | if (length == -1) return -1;␊ |
843 | ␊ |
844 | tmpTag = NewTag();␊ |
845 | if (tmpTag == 0) return -1;␊ |
846 | ␊ |
847 | ␉printf("ParseTagDate unimplimented\n");␊ |
848 | ␉getchar();␊ |
849 | ␉␊ |
850 | tmpTag->type = kTagTypeDate;␊ |
851 | tmpTag->string = 0;␊ |
852 | tmpTag->tag = 0;␊ |
853 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
854 | tmpTag->tagNext = 0;␊ |
855 | ␊ |
856 | *tag = tmpTag;␊ |
857 | ␊ |
858 | return length;␊ |
859 | }␊ |
860 | ␊ |
861 | //==========================================================================␊ |
862 | // ParseTagBoolean␊ |
863 | ␊ |
864 | long␊ |
865 | ParseTagBoolean( char * buffer, TagPtr * tag, long type )␊ |
866 | {␊ |
867 | TagPtr tmpTag;␊ |
868 | ␊ |
869 | tmpTag = NewTag();␊ |
870 | if (tmpTag == 0) return -1;␊ |
871 | ␊ |
872 | tmpTag->type = type;␊ |
873 | tmpTag->string = 0;␊ |
874 | tmpTag->tag = 0;␊ |
875 | ␉tmpTag->offset = buffer_start ? buffer - buffer_start: 0;␊ |
876 | tmpTag->tagNext = 0;␊ |
877 | ␊ |
878 | *tag = tmpTag;␊ |
879 | ␊ |
880 | return 0;␊ |
881 | }␊ |
882 | ␊ |
883 | //==========================================================================␊ |
884 | // GetNextTag␊ |
885 | ␊ |
886 | static long␊ |
887 | GetNextTag( char * buffer, char ** tag, long * start )␊ |
888 | {␊ |
889 | long cnt, cnt2;␊ |
890 | ␊ |
891 | if (tag == 0) return -1;␊ |
892 | ␊ |
893 | // Find the start of the tag.␊ |
894 | cnt = 0;␊ |
895 | while ((buffer[cnt] != '\0') && (buffer[cnt] != '<')) cnt++;␊ |
896 | if (buffer[cnt] == '\0') return -1;␊ |
897 | ␊ |
898 | // Find the end of the tag.␊ |
899 | cnt2 = cnt + 1;␊ |
900 | while ((buffer[cnt2] != '\0') && (buffer[cnt2] != '>')) cnt2++;␊ |
901 | if (buffer[cnt2] == '\0') return -1;␊ |
902 | ␊ |
903 | // Fix the tag data.␊ |
904 | *tag = buffer + cnt + 1;␊ |
905 | buffer[cnt2] = '\0';␊ |
906 | if (start) *start = cnt;␊ |
907 | ␊ |
908 | return cnt2 + 1;␊ |
909 | }␊ |
910 | ␊ |
911 | //==========================================================================␊ |
912 | // FixDataMatchingTag␊ |
913 | // Modifies 'buffer' to add a '\0' at the end of the tag matching 'tag'.␊ |
914 | // Returns the length of the data found, counting the end tag,␊ |
915 | // or -1 if the end tag was not found.␊ |
916 | ␊ |
917 | static long␊ |
918 | FixDataMatchingTag( char * buffer, char * tag )␊ |
919 | {␊ |
920 | long length, start, stop;␊ |
921 | char * endTag;␊ |
922 | ␊ |
923 | start = 0;␊ |
924 | while (1)␊ |
925 | {␊ |
926 | length = GetNextTag(buffer + start, &endTag, &stop);␊ |
927 | if (length == -1) return -1;␊ |
928 | ␊ |
929 | if ((*endTag == '/') && !strcmp(endTag + 1, tag)) break;␊ |
930 | start += length;␊ |
931 | }␊ |
932 | ␊ |
933 | buffer[start + stop] = '\0';␊ |
934 | ␊ |
935 | return start + length;␊ |
936 | }␊ |
937 | ␊ |
938 | //==========================================================================␊ |
939 | // NewTag␊ |
940 | ␊ |
941 | #define kTagsPerBlock (0x1000)␊ |
942 | ␊ |
943 | static TagPtr gTagsFree;␊ |
944 | ␊ |
945 | static TagPtr␊ |
946 | NewTag( void )␊ |
947 | {␊ |
948 | ␉long cnt;␊ |
949 | ␉TagPtr tag;␊ |
950 | ␊ |
951 | if (gTagsFree == 0)␊ |
952 | {␊ |
953 | tag = (TagPtr)malloc(kTagsPerBlock * sizeof(Tag));␊ |
954 | if (tag == 0) return 0;␊ |
955 | ␊ |
956 | // Initalize the new tags.␊ |
957 | for (cnt = 0; cnt < kTagsPerBlock; cnt++)␊ |
958 | {␊ |
959 | tag[cnt].type = kTagTypeNone;␊ |
960 | tag[cnt].string = 0;␊ |
961 | tag[cnt].tag = 0;␊ |
962 | tag[cnt].tagNext = tag + cnt + 1;␊ |
963 | }␊ |
964 | tag[kTagsPerBlock - 1].tagNext = 0;␊ |
965 | ␊ |
966 | gTagsFree = tag;␊ |
967 | }␊ |
968 | ␊ |
969 | tag = gTagsFree;␊ |
970 | gTagsFree = tag->tagNext;␊ |
971 | ␊ |
972 | return tag;␊ |
973 | }␊ |
974 | ␊ |
975 | //==========================================================================␊ |
976 | // XMLFreeTag␊ |
977 | ␊ |
978 | void␊ |
979 | XMLFreeTag( TagPtr tag )␊ |
980 | {␊ |
981 | #if DOFREE␊ |
982 | if (tag == 0) return;␊ |
983 | ␊ |
984 | if (tag->string) FreeSymbol(tag->string);␊ |
985 | ␊ |
986 | XMLFreeTag(tag->tag);␊ |
987 | XMLFreeTag(tag->tagNext);␊ |
988 | ␊ |
989 | // Clear and free the tag.␊ |
990 | tag->type = kTagTypeNone;␊ |
991 | tag->string = 0;␊ |
992 | tag->tag = 0;␊ |
993 | ␉tag->offset = 0;␊ |
994 | tag->tagNext = gTagsFree;␊ |
995 | gTagsFree = tag;␊ |
996 | #else␊ |
997 | return;␊ |
998 | #endif␊ |
999 | }␊ |
1000 | ␊ |
1001 | //==========================================================================␊ |
1002 | // Symbol object.␊ |
1003 | ␊ |
1004 | struct Symbol␊ |
1005 | {␊ |
1006 | long refCount;␊ |
1007 | struct Symbol *next;␊ |
1008 | char string[];␊ |
1009 | };␊ |
1010 | typedef struct Symbol Symbol, *SymbolPtr;␊ |
1011 | ␊ |
1012 | static SymbolPtr FindSymbol(char * string, SymbolPtr * prevSymbol);␊ |
1013 | ␊ |
1014 | static SymbolPtr gSymbolsHead;␊ |
1015 | ␊ |
1016 | //==========================================================================␊ |
1017 | // NewSymbol␊ |
1018 | ␊ |
1019 | static char *␊ |
1020 | NewSymbol( char * string )␊ |
1021 | {␊ |
1022 | static SymbolPtr lastGuy = 0;␊ |
1023 | ␉SymbolPtr symbol;␊ |
1024 | ␊ |
1025 | // Look for string in the list of symbols.␊ |
1026 | symbol = FindSymbol(string, 0);␊ |
1027 | ␊ |
1028 | // Add the new symbol.␊ |
1029 | if (symbol == 0)␊ |
1030 | {␊ |
1031 | symbol = (SymbolPtr)malloc(sizeof(Symbol) + 1 + strlen(string));␊ |
1032 | if (symbol == 0) //return 0;␊ |
1033 | stop("NULL symbol!");␊ |
1034 | ␊ |
1035 | // Set the symbol's data.␊ |
1036 | symbol->refCount = 0;␊ |
1037 | strcpy(symbol->string, string);␊ |
1038 | ␊ |
1039 | // Add the symbol to the list.␊ |
1040 | symbol->next = gSymbolsHead;␊ |
1041 | gSymbolsHead = symbol;␊ |
1042 | }␊ |
1043 | ␊ |
1044 | // Update the refCount and return the string.␊ |
1045 | symbol->refCount++;␊ |
1046 | ␊ |
1047 | if (lastGuy && lastGuy->next != 0) stop("last guy not last!");␊ |
1048 | return symbol->string;␊ |
1049 | }␊ |
1050 | ␊ |
1051 | //==========================================================================␊ |
1052 | // FreeSymbol␊ |
1053 | ␊ |
1054 | #if DOFREE␊ |
1055 | static void␊ |
1056 | FreeSymbol( char * string )␊ |
1057 | {␊ |
1058 | SymbolPtr symbol, prev;␊ |
1059 | ␉prev = 0;␊ |
1060 | ␊ |
1061 | // Look for string in the list of symbols.␊ |
1062 | symbol = FindSymbol(string, &prev);␊ |
1063 | if (symbol == 0) return;␊ |
1064 | ␊ |
1065 | // Update the refCount.␊ |
1066 | symbol->refCount--;␊ |
1067 | ␊ |
1068 | if (symbol->refCount != 0) return;␊ |
1069 | ␊ |
1070 | // Remove the symbol from the list.␊ |
1071 | if (prev != 0) prev->next = symbol->next;␊ |
1072 | else gSymbolsHead = symbol->next;␊ |
1073 | ␊ |
1074 | // Free the symbol's memory.␊ |
1075 | free(symbol);␊ |
1076 | }␊ |
1077 | #endif␊ |
1078 | ␊ |
1079 | //==========================================================================␊ |
1080 | // FindSymbol␊ |
1081 | ␊ |
1082 | static SymbolPtr␊ |
1083 | FindSymbol( char * string, SymbolPtr * prevSymbol )␊ |
1084 | {␊ |
1085 | SymbolPtr symbol, prev;␊ |
1086 | ␊ |
1087 | symbol = gSymbolsHead;␊ |
1088 | prev = 0;␊ |
1089 | ␊ |
1090 | while (symbol != 0) {␊ |
1091 | if (!strcmp(symbol->string, string)) break;␊ |
1092 | ␊ |
1093 | prev = symbol;␊ |
1094 | symbol = symbol->next;␊ |
1095 | }␊ |
1096 | ␊ |
1097 | if ((symbol != 0) && (prevSymbol != 0)) *prevSymbol = prev;␊ |
1098 | ␊ |
1099 | return symbol;␊ |
1100 | }␊ |
1101 | ␊ |
1102 | bool XMLIsType(TagPtr dict, enum xmltype type)␊ |
1103 | {␊ |
1104 | ␉if(!dict) return (type == kTagTypeNone);␊ |
1105 | ␉return (dict->type == type);␊ |
1106 | }␊ |
1107 | ␊ |
1108 | /*** Cast functions ***/␊ |
1109 | bool XMLIsArray(TagPtr entry)␊ |
1110 | {␊ |
1111 | return entry && (entry->type == kTagTypeArray);␊ |
1112 | }␊ |
1113 | ␊ |
1114 | TagPtr XMLCastArray(TagPtr dict)␊ |
1115 | {␊ |
1116 | ␉if(!dict) return NULL;␊ |
1117 | ␉if(dict->type == kTagTypeArray) return dict;␊ |
1118 | ␉else return NULL;␊ |
1119 | }␊ |
1120 | ␊ |
1121 | bool XMLIsDict(TagPtr entry)␊ |
1122 | {␊ |
1123 | return entry && (entry->type == kTagTypeDict);␊ |
1124 | }␊ |
1125 | ␊ |
1126 | ␊ |
1127 | TagPtr XMLCastDict(TagPtr dict)␊ |
1128 | {␊ |
1129 | ␉if(!dict) return NULL;␊ |
1130 | ␉if(dict->type == kTagTypeDict) return dict;␊ |
1131 | ␉else return NULL;␊ |
1132 | }␊ |
1133 | ␊ |
1134 | bool XMLIsString(TagPtr entry)␊ |
1135 | {␊ |
1136 | return entry && ␊ |
1137 | ((entry->type == kTagTypeString) ||␊ |
1138 | (entry->type == kTagTypeKey));␊ |
1139 | }␊ |
1140 | ␊ |
1141 | char* XMLCastString(TagPtr dict)␊ |
1142 | {␊ |
1143 | ␉if(!dict) return NULL;␊ |
1144 | ␊ |
1145 | ␉if((dict->type == kTagTypeString) ||␊ |
1146 | ␉ (dict->type == kTagTypeKey)) return dict->string;␊ |
1147 | ␉␊ |
1148 | ␉return NULL;␊ |
1149 | }␊ |
1150 | ␊ |
1151 | long XMLCastStringOffset(TagPtr dict)␊ |
1152 | {␊ |
1153 | ␉if(dict &&␊ |
1154 | ␉ ((dict->type == kTagTypeString) ||␊ |
1155 | ␉ (dict->type == kTagTypeKey)))␊ |
1156 | ␉{␊ |
1157 | ␉␉return dict->offset;␊ |
1158 | ␉}␊ |
1159 | ␉else ␊ |
1160 | ␉{␊ |
1161 | ␉␉return -1;␊ |
1162 | ␉}␊ |
1163 | }␊ |
1164 | ␊ |
1165 | bool XMLIsBoolean(TagPtr entry)␊ |
1166 | {␊ |
1167 | return entry && ␊ |
1168 | ((entry->type == kTagTypeTrue) ||␊ |
1169 | (entry->type == kTagTypeFalse));␊ |
1170 | }␊ |
1171 | ␊ |
1172 | bool XMLCastBoolean(TagPtr dict)␊ |
1173 | {␊ |
1174 | ␉if(!dict) return false;␊ |
1175 | ␉if(dict->type == kTagTypeTrue) return true;␊ |
1176 | ␉return false;␊ |
1177 | }␊ |
1178 | ␊ |
1179 | bool XMLIsInteger(TagPtr entry)␊ |
1180 | {␊ |
1181 | return entry && (entry->type == kTagTypeInteger);␊ |
1182 | }␊ |
1183 | ␊ |
1184 | int XMLCastInteger(TagPtr dict)␊ |
1185 | {␊ |
1186 | ␉if(!dict)␊ |
1187 | ␉{␊ |
1188 | ␉␉//printf("XMLCastInteger: null dict\n");␊ |
1189 | ␉␉return 0;␊ |
1190 | ␉}␊ |
1191 | ␉if(dict->type == kTagTypeInteger) return (int)(dict->string);␊ |
1192 | ␉return 0;␊ |
1193 | }␊ |
1194 | ␊ |
1195 | bool XMLAddTagToDictionary(TagPtr dict, char* key, TagPtr value)␊ |
1196 | {␊ |
1197 | if (!dict || dict->type != kTagTypeDict) return false;␊ |
1198 | ␊ |
1199 | TagPtr tmpTag;␊ |
1200 | char* string;␊ |
1201 | ␊ |
1202 | tmpTag = NewTag();␊ |
1203 | if (tmpTag == 0)␊ |
1204 | {␊ |
1205 | return false;␊ |
1206 | }␊ |
1207 | ␊ |
1208 | string = NewSymbol(key);␊ |
1209 | if (string == 0)␊ |
1210 | {␊ |
1211 | XMLFreeTag(tmpTag);␊ |
1212 | return false;␊ |
1213 | }␊ |
1214 | ␊ |
1215 | tmpTag->type = kTagTypeKey;␊ |
1216 | tmpTag->string = string;␊ |
1217 | tmpTag->tag = value;␊ |
1218 | ␉tmpTag->offset = 0;␊ |
1219 | tmpTag->tagNext = 0;␊ |
1220 | ␊ |
1221 | TagPtr tagList = dict->tag;␊ |
1222 | if(!tagList)␊ |
1223 | {␊ |
1224 | // First tag␊ |
1225 | dict->tag = tmpTag;␊ |
1226 | return true;␊ |
1227 | }␊ |
1228 | while(tagList && tagList->tagNext) tagList = tagList->tagNext;␊ |
1229 | if(tagList)␊ |
1230 | {␊ |
1231 | tagList->tagNext = tmpTag;␊ |
1232 | return true;␊ |
1233 | }␊ |
1234 | return false;␊ |
1235 | }␊ |
1236 | |