77c76e29d73dda9a5718991601af0c623b3573ce
1 /**
2 * Copyright (C) ARM Limited 2010-2012. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
9 #include <string.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include "XMLReader.h"
13 extern void handleException();
15 XMLReader::XMLReader(const char* xmlstring) {
16 mPtr = (char*)xmlstring;
17 mNoMore = false;
18 mFirstTime = true;
19 }
21 XMLReader::~XMLReader() {
22 }
24 char* XMLReader::nextTag() {
25 static char tag[128]; // arbitrarily set max tag size to 127 characters + nul
27 // Check if past the end of the root tag
28 if (mNoMore) return NULL;
30 // Find start character
31 mPtr = strchr(mPtr, '<');
33 if (mPtr == NULL) return mPtr;
35 // Skip tag if it begins with <?
36 if (mPtr[1] == '?') {
37 mPtr++;
38 return nextTag();
39 }
41 // Find end of tag name
42 mPtr++;
43 char* end = strchr(mPtr, ' ');
44 if (end == NULL)
45 end = strchr(mPtr, '>');
46 if (end == NULL)
47 return 0;
49 // Check if tag has no attributes
50 char* tagend = strchr(mPtr, '>');
51 if (tagend < end) end = tagend;
53 // Check the tag name length
54 unsigned int length = (int)end - (int)mPtr;
55 if (length > sizeof(tag) - 1) {
56 // tag name too long, skip it
57 return nextTag();
58 }
60 // Return the tag name
61 strncpy(tag, mPtr, length);
62 tag[length] = 0;
64 // Mark the root tag
65 if (mFirstTime) {
66 mEndXML[0] = '/';
67 strcpy(&mEndXML[1], tag);
68 mFirstTime = false;
69 } else if (strcmp(tag, mEndXML) == 0) {
70 // End of root tag found
71 mNoMore = true;
72 }
74 return tag;
75 }
77 void XMLReader::getAttribute(const char* name, char* value, int maxSize, const char* defValue) {
78 char searchString[128];
80 // Set up default
81 strncpy(value, defValue, maxSize - 1);
82 value[maxSize - 1] = 0;
84 // Determine search string by ending the name with ="
85 if (strlen(name) > sizeof(searchString) - 3) return;
86 strcpy(searchString, name);
87 strcat(searchString, "=\"");
89 // Find the beginning of the attribute's search string
90 char* begin = strstr(mPtr, searchString);
91 if (begin == NULL) return;
93 // Find the beginning of the attribute's value
94 begin += strlen(searchString);
96 // Find the end of the current tag to make sure the attribute exists within the tag
97 char* endtag = strchr(mPtr, '>');
98 if (endtag < begin) return;
100 // Find the end of the attribute's value
101 char* end = strchr(begin, '"');
102 if (end == NULL) return;
104 // Determine length
105 int length = (int)end - (int)begin;
106 if (length > maxSize - 1) return;
108 strncpy(value, begin, length);
109 value[length] = 0;
110 }
112 int XMLReader::getAttributeAsInteger(const char* name, int defValue) {
113 char value[32];
114 getAttribute(name, value, sizeof(value), "");
115 if (value[0] == 0) return defValue;
116 if (value[0] == '0' && value[1] == 'x') {
117 return (int) strtoul(&value[2], (char**)NULL, 16);
118 }
119 return strtol(value, NULL, 10);
120 }
122 bool XMLReader::getAttributeAsBoolean(const char* name, bool defValue) {
123 char value[32];
124 getAttribute(name, value, sizeof(value), "");
125 if (value[0] == 0) return defValue;
127 // Convert to lowercase
128 int i = 0;
129 while (value[i]) {
130 value[i] = tolower(value[i]);
131 i++;
132 }
134 if (strcmp(value, "true") == 0 || strcmp(value, "yes") == 0 || strcmp(value, "1") == 0 || strcmp(value, "on") == 0) return true;
135 else if (strcmp(value, "false") == 0 || strcmp(value, "no") == 0 || strcmp(value, "0") == 0 || strcmp(value, "off") == 0) return false;
136 else return defValue;
137 }
139 int XMLReader::getAttributeLength(const char* name) {
140 char searchString[128]; // arbitrarily large amount
142 // Determine search string by ending the name with ="
143 if (strlen(name) > sizeof(searchString) - 3) return 0;
144 strcpy(searchString, name);
145 strcat(searchString, "=\"");
147 // Find the beginning of the attribute's search string
148 char* begin = strstr(mPtr, searchString);
149 if (begin == NULL) return 0;
151 // Find the beginning of the attribute's value
152 begin += strlen(searchString);
154 // Find the end of the current tag to make sure the attribute exists within the tag
155 char* endtag = strchr(mPtr, '>');
156 if (endtag < begin) return 0;
158 // Find the end of the attribute's value
159 char* end = strchr(begin, '"');
160 if (end == NULL) return 0;
162 // Determine length
163 return (int)end - (int)begin;
164 }