OSDN Git Service

first commit
[liveml/LiveML.git] / src / xmlparser.h
1 /**
2  * LiveML - LiveML is screen(graphic) controling library using XML.
3  *
4  * LGPL License
5  * Copyright (C) 2010 Nothan
6  * http://github.com/nothan/liveml/
7  * All rights reserved.
8  *
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.
13  *
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.
18  *
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
22  *
23  * Nothan
24  * private@nothan.xrea.jp
25  *
26  * Tsuioku Denrai
27  * http://tsuioku-denrai.xrea.jp/
28  */
29
30 #ifndef __XMLPARSER_H__
31 #define __XMLPARSER_H__
32
33 #include <tinyxml.h>
34 #include <vector>
35 #include "list.hpp"
36 #include "calcu.h"
37 using namespace std;
38
39 enum {
40   CLOSE_TAG = 0xFFFFFFFF,
41 };
42
43 typedef int tag_type;
44 typedef int action_type;
45
46 enum param_type {
47   PARAM_STRING,
48   PARAM_NUMERIC,
49   PARAM_TAG,
50   PARAM_CLOSE_TAG,
51   PARAM_PARENT_TAG
52 };
53
54 class Parameter;
55 class XMLParser;
56 struct Tag;
57
58 class ParameterOption {
59   friend class Parameter;
60   friend class XMLParser;
61
62   struct ReplaceValue {
63     string value;
64     string to;
65   };
66   size_t _index;
67   string _name;
68   param_type _type;
69   vector<ReplaceValue> _replaces;
70 public:
71   ~ParameterOption()
72   {
73   }
74   void set(size_t index, param_type type, const char *name = NULL)
75   {
76     if (name != NULL) _name = name;
77     _index = index;
78     _type = type;
79   }
80   void addReplaceValue(const char* value, const char *to)
81   {
82     ReplaceValue rep = {value, to};
83     _replaces.push_back(rep);
84   }
85   const char* getReplaceValue(const char* value)
86   {
87     for (int i = 0; i < _replaces.size(); i++)
88     {
89       if (_replaces[i].value.find(value)) return _replaces[i].to.c_str();
90     }
91     return value;
92   }
93 };
94
95 class Parameter
96 {
97   vector<void*> values;
98 public:
99   ~Parameter()
100   {
101     for (size_t i = 0; i < values.size(); i++) free(values[i]);
102   }
103   void unset(size_t index)
104   {
105     if (values[index] == NULL) return;
106     free(values[index]);
107     values[index] = NULL;
108   }
109   bool has(size_t index)
110   {
111     if (values.size() < index + 1) return false;
112     return values[index] != NULL;
113   }
114   const void* get(size_t index)
115   {
116     if (values.size() < index + 1) return NULL;
117     return values[index];
118   }
119   Tag* getTag(size_t index)
120   {
121     return (Tag*)get(index);
122   }
123   float getFloat(size_t index)
124   {
125     return fixed_float_to_float(calcu_decode((const char*)get(index), NULL));
126   }
127   int getInteger(size_t index)
128   {
129     return fixed_float_to_int(calcu_decode((const char*)get(index), NULL));
130   }
131   const char* getString(size_t index)
132   {
133     return (const char*)values[index];
134   }
135   void set(size_t index, void *data)
136   {
137     if (values.size() < index + 1) values.resize(index + 1);
138     else unset(index);
139     values[index] = data;
140   }
141   void setInteger(size_t index, int value)
142   {
143     int *buf = (int*)malloc(sizeof(int));
144     set(index, buf);
145   }
146   void setCalcu(size_t index, const char *str)
147   {
148     set(index, calcu_encode((char*)str, NULL));
149   }
150   void setString(size_t index, const char *str)
151   {
152     size_t size = strlen(str);
153     void *buf = malloc(size + 1);
154     strcpy((char*)buf, (char*)str);
155     set(index, buf);
156   }
157   void setByParameterOption(ParameterOption &po, void *value)
158   {
159     switch (po._type)
160     {
161     case PARAM_STRING:
162       value = (void*)po.getReplaceValue((char*)value);
163       setString(po._index, (char*)value);
164       break;
165     case PARAM_NUMERIC:
166       value = (void*)po.getReplaceValue((char*)value);
167       setCalcu(po._index, (char*)value);
168       break;
169     case PARAM_TAG:
170     case PARAM_CLOSE_TAG:
171     case PARAM_PARENT_TAG:
172       set(po._index, value);
173       break;
174     }
175   }
176   bool cmpHash(size_t index, const char*buf)
177   {
178     for (int i = 0; i < 4; i++)
179     {
180       if (((int*)values[index])[i] != ((int*)buf)[i]) return false;
181     }
182
183     return true;
184   }
185 };
186
187 struct Tag
188 {
189   tag_type type;
190   Parameter param;
191 };
192
193 class RegisterTag
194 {
195   friend class XMLParser;
196   string _name;
197   bool (*_func)(Parameter&, XMLParser&);
198   vector<ParameterOption> _params;
199 public:
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; }
204 };
205
206 class XMLParser
207 {
208   vector<RegisterTag> _registerTags;
209   Tag* _currentTag;
210   Tag* _parentTag;
211
212 public:
213   list<Tag> tagList;
214
215   XMLParser(void);
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);
222
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; }
228 };
229
230 #endif // __XMLPARSER_H__