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__
39 #define DCODE(code) code
44 #define PARAMFUNC(name) void* name(const char* value, XMLParser &parser)
45 #define LML_TAGFUNC(name) XML_TAGRTN name(Parameter ¶m, XMLParser &parser)
55 typedef size_t tag_type;
56 typedef size_t action_type;
57 typedef size_t param_type;
63 class ParameterOption {
72 vector<ReplaceValue> _replaces;
79 void set(size_t index, param_type type, const char *name = NULL)
81 if (name != NULL) _name = name;
96 param_type getType(void)
101 void addReplaceValue(const char* value, const char *to)
103 ReplaceValue rep = {value, to};
104 _replaces.push_back(rep);
106 const char* getReplaceValue(const char* value)
108 for (size_t i = 0; i < _replaces.size(); i++)
110 if (_replaces[i].value == value) return _replaces[i].to.c_str();
118 vector<void*> values;
122 for (size_t i = 0; i < values.size(); i++) free(values[i]);
125 void unset(size_t index)
127 if (values[index] == NULL) return;
129 values[index] = NULL;
132 bool has(size_t index)
134 if (values.size() < index + 1) return false;
135 return values[index] != NULL;
138 const void* get(size_t index)
140 if (values.size() < index + 1) return NULL;
141 return values[index];
144 void set(size_t index, void *data)
146 if (values.size() < index + 1) values.resize(index + 1);
148 values[index] = data;
151 Tag* getTag(size_t index)
153 return (Tag*)get(index);
156 const char* getString(size_t index)
158 return (const char*)values[index];
161 size_t getENum(size_t index)
163 return *(size_t*)values[index];
166 bool cmpHash(size_t index, const char*buf)
168 for (size_t i = 0; i < 4; i++)
170 if (((int*)values[index])[i] != ((int*)buf)[i]) return false;
182 void setName(string name)
198 static const size_t NOTFOUND = 0xFFFFFFFF;
200 virtual ~NameContainer()
204 T* operator[](size_t id)
209 T* operator[](const char* name)
211 return get(getId(name));
216 _list.resize(_list.size() + 1);
224 if (id >= _list.size()) return NULL;
229 virtual size_t getId(const char *name)
231 for (tag_type i = 0; i < _list.size(); i++)
233 if (_list[i].getName() == name) return i;
239 bool has(const char *name)
241 return getId(name) != NOTFOUND;
246 return id != NOTFOUND;
254 void resize(size_t size)
261 class UNameContainer : public NameContainer<T>
264 T* add(const char *name)
266 size_t id = NameContainer<T>::getId(name);
268 if (id != NameContainer<T>::NOTFOUND)
270 return NameContainer<T>::get(id);
272 T &i = *NameContainer<T>::add();
279 class TagType : public UNameItem
282 XML_TAGRTN (*_func)(Parameter&, XMLParser&);
283 vector<ParameterOption> _params;
295 ParameterOption* addParameter(void)
297 _params.resize(_params.size() + 1);
298 return &_params.back();
300 ParameterOption* getParameter(size_t id)
302 if (id >= _params.size()) return NULL;
306 size_t countParameter(void)
308 return _params.size();
311 void setFunction(XML_TAGRTN (*f)(Parameter&, XMLParser&))
315 bool hasFunction(void)
317 return _func != NULL;
319 XML_TAGRTN (*getFunction(void))(Parameter&, XMLParser&)
325 class TagTypeContainer : public UNameContainer<TagType>
330 TagType &i = *NameContainer<TagType>::add();
331 i.id = _list.size() - 1;
336 TagType* add(const char *name, const char *parent = NULL)
338 TagType &i = *UNameContainer<TagType>::add(name);
339 i.id = _list.size() - 1;
354 void* (*func)(const char*, XMLParser&);
357 class XMLParser : public TagTypeContainer
360 TagTypeContainer _tagTypeContainer;
361 vector<TagType> _registerTags;
362 vector<ParameterType> _parameterTypes;
366 static PARAMFUNC(paramString);
367 static PARAMFUNC(paramTag);
368 static PARAMFUNC(paramCloseTag);
369 static PARAMFUNC(paramParentTag);
372 const param_type PARAM_STRING;
373 const param_type PARAM_TAG;
374 const param_type PARAM_CLOSE_TAG;
375 const param_type PARAM_PARENT_TAG;
378 list<Tag*> tagLevelList;
381 param_type registerParameterType(void*(*func)(const char*, XMLParser&));
383 virtual bool loadText(const char *text) = 0;
384 virtual bool loadFile(const char *file) = 0;
386 TagType* addTagType(const char *name, const char *parent = NULL)
388 return _tagTypeContainer.add(name, parent);
390 TagType* getTagType(const char *name) { return _tagTypeContainer[name]; }
391 tag_type getTagTypeId(const char *name) { return _tagTypeContainer.getId(name); }
392 size_t countTagType(void) { return _tagTypeContainer.size(); };
393 bool checkTagLevel(TagType*);
395 Tag* addTag(TagType*);
396 void setCurrentTag(Tag *t) { _currentTag = t; }
397 Tag* getCurrentTag(void) { return _currentTag; }
398 void setParentTag(Tag *t) { _parentTag = t; }
399 Tag* getParentTag(void) { return _parentTag; }
402 #endif // __XMLPARSER_H__