New lua versions
[ryzomcore.git] / studio / src / plugins / gui_editor / expression_loader.cpp
blob057c6c774d5e5b9a1a86d67cdfce3df94f6e201a
1 // Ryzom Core Studio - GUI Editor Plugin
2 //
3 // Copyright (C) 2010-2014 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU Affero General Public License as
7 // published by the Free Software Foundation, either version 3 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Affero General Public License for more details.
15 // You should have received a copy of the GNU Affero General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "expression_loader.h"
19 #include "expression_info.h"
21 #include <QFile>
22 #include <QXmlStreamReader>
24 class ExpressionLoaderPvt
26 public:
28 bool parseName()
30 QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
31 if( reader.hasError() )
32 return false;
34 m_info->name = text;
36 return true;
39 bool parseValue()
41 QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
42 if( reader.hasError() )
43 return false;
45 if( text.toLower() == "true" )
46 m_info->value = true;
47 else
48 m_info->value = false;
50 return true;
53 bool parseCategory()
55 QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
56 if( reader.hasError() )
57 return false;
59 m_info->category = text;
61 return true;
64 bool parseVariable()
66 QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
67 if( reader.hasError() )
68 return false;
70 if( text.toLower() == "true" )
71 m_info->variable = true;
72 else
73 m_info->variable = false;
75 return true;
78 bool parseSlot()
80 QString text = reader.readElementText( QXmlStreamReader::ErrorOnUnexpectedElement );
81 if( reader.hasError() )
82 return false;
84 m_info->slotNames.push_back( text );
86 return true;
89 bool parseSlots()
91 bool error = false;
93 while( !reader.atEnd() )
95 reader.readNext();
97 if( reader.isStartElement() )
99 QString name = reader.name().toString();
100 if( name == "slot" )
101 error = !parseSlot();
103 else
104 if( reader.isEndElement() )
106 if( reader.name() == "slots" )
107 break;
111 if( reader.atEnd() )
112 return false;
114 return true;
117 bool load( QFile *f )
119 reader.clear();
120 reader.setDevice( f );
122 bool error = false;
124 // start of document
125 reader.readNext();
126 if( reader.atEnd() )
127 return false;
129 // root node
130 reader.readNext();
131 if( reader.atEnd() )
132 return false;
134 if( reader.isStartElement() )
136 // Not an expression file?
137 if( reader.name() != "expression" )
138 return false;
141 while( !reader.atEnd() )
143 reader.readNext();
145 if( reader.isStartElement() )
147 QString name = reader.name().toString();
149 if( name == "name" )
150 error = !parseName();
151 else
152 if( name == "value" )
153 error = !parseValue();
154 else
155 if( name == "category" )
156 error = !parseCategory();
157 else
158 if( name == "variable" )
159 error = !parseVariable();
160 else
161 if( name == "slots" )
162 error = !parseSlots();
165 if( error )
166 break;
169 if( error || reader.hasError() )
171 return false;
174 return true;
177 void setInfo( ExpressionInfo *info ){ m_info = info; }
179 private:
180 QXmlStreamReader reader;
181 ExpressionInfo *m_info;
184 ExpressionLoader::ExpressionLoader()
186 m_pvt = new ExpressionLoaderPvt;
189 ExpressionLoader::~ExpressionLoader()
191 delete m_pvt;
192 m_pvt = NULL;
195 ExpressionInfo* ExpressionLoader::load( const QString &filename )
197 ExpressionInfo *info = NULL;
198 bool ok = false;
200 QFile f( filename );
201 if( !f.open( QIODevice::ReadOnly | QIODevice::Text ) )
202 return NULL;
204 info = new ExpressionInfo();
205 m_pvt->setInfo( info );
206 ok = m_pvt->load( &f );
208 f.close();
210 if( !ok )
212 delete info;
213 info = NULL;
216 return info;