2 * LiveML - LiveML is screen(graphic) controling library using XML.
5 * Copyright (C) 2010 Nothan
6 * http://github.com/nothan/liveml/
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * private@nothan.xrea.jp
27 * http://tsuioku-denrai.xrea.jp/
30 #ifndef __XMLPARSER_H__
31 #define __XMLPARSER_H__
40 CLOSE_TAG = 0xFFFFFFFF,
44 typedef int action_type;
58 class ParameterOption {
59 friend class Parameter;
60 friend class XMLParser;
69 vector<ReplaceValue> _replaces;
74 void set(size_t index, param_type type, const char *name = NULL)
76 if (name != NULL) _name = name;
80 void addReplaceValue(const char* value, const char *to)
82 ReplaceValue rep = {value, to};
83 _replaces.push_back(rep);
85 const char* getReplaceValue(const char* value)
87 for (int i = 0; i < _replaces.size(); i++)
89 if (_replaces[i].value.find(value)) return _replaces[i].to.c_str();
101 for (size_t i = 0; i < values.size(); i++) free(values[i]);
103 void unset(size_t index)
105 if (values[index] == NULL) return;
107 values[index] = NULL;
109 bool has(size_t index)
111 if (values.size() < index + 1) return false;
112 return values[index] != NULL;
114 const void* get(size_t index)
116 if (values.size() < index + 1) return NULL;
117 return values[index];
119 Tag* getTag(size_t index)
121 return (Tag*)get(index);
123 float getFloat(size_t index)
125 return fixed_float_to_float(calcu_decode((const char*)get(index), NULL));
127 int getInteger(size_t index)
129 return fixed_float_to_int(calcu_decode((const char*)get(index), NULL));
131 const char* getString(size_t index)
133 return (const char*)values[index];
135 void set(size_t index, void *data)
137 if (values.size() < index + 1) values.resize(index + 1);
139 values[index] = data;
141 void setInteger(size_t index, int value)
143 int *buf = (int*)malloc(sizeof(int));
146 void setCalcu(size_t index, const char *str)
148 set(index, calcu_encode((char*)str, NULL));
150 void setString(size_t index, const char *str)
152 size_t size = strlen(str);
153 void *buf = malloc(size + 1);
154 strcpy((char*)buf, (char*)str);
157 void setByParameterOption(ParameterOption &po, void *value)
162 value = (void*)po.getReplaceValue((char*)value);
163 setString(po._index, (char*)value);
166 value = (void*)po.getReplaceValue((char*)value);
167 setCalcu(po._index, (char*)value);
170 case PARAM_CLOSE_TAG:
171 case PARAM_PARENT_TAG:
172 set(po._index, value);
176 bool cmpHash(size_t index, const char*buf)
178 for (int i = 0; i < 4; i++)
180 if (((int*)values[index])[i] != ((int*)buf)[i]) return false;
195 friend class XMLParser;
197 bool (*_func)(Parameter&, XMLParser&);
198 vector<ParameterOption> _params;
200 RegisterTag(void) { _func = NULL; }
201 void setName(string name) { _name = name; }
202 ParameterOption* addParameter(void) { _params.resize(_params.size() + 1); return &_params.back(); }
203 void setFunction(bool (*f)(Parameter&, XMLParser&)) { _func = f; }
208 vector<RegisterTag> _registerTags;
216 RegisterTag* addRegisterTag(const char *name);
217 size_t countRegisteredTags(void);
218 tag_type getTagType(const char *name);
219 bool loadText(const char *text);
220 bool loadFile(const char *file);
221 virtual bool parse(TiXmlElement *root);
223 Tag* addTag(tag_type);
224 void setCurrentTag(Tag *t) { _currentTag = t; }
225 Tag* getCurrentTag(void) { return _currentTag; }
226 void setParentTag(Tag *t) { _parentTag = t; }
227 Tag* getParentTag(void) { return _parentTag; }
230 #endif // __XMLPARSER_H__