]> git.stg.codes - stg.git/blob - projects/convertor/settings.cpp
Добавление исходников
[stg.git] / projects / convertor / settings.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26 $Revision: 1.6 $
27 $Date: 2009/06/22 16:26:54 $
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <string>
35
36 using namespace std;
37
38 #include "settings.h"
39 #include "common.h"
40
41 //-----------------------------------------------------------------------------
42 SETTINGS::SETTINGS(const char * cf)
43 {
44 confFile = string(cf);
45 }
46 //-----------------------------------------------------------------------------
47 SETTINGS::~SETTINGS()
48 {
49
50 }
51 //-----------------------------------------------------------------------------
52 /*
53 int SETTINGS::ParseYesNo(const string & value, bool * val)
54 {
55 if (0 == strcasecmp(value.c_str(), "yes"))
56     {
57     *val = true;
58     return 0;
59     }
60 if (0 == strcasecmp(value.c_str(), "no"))
61     {
62     *val = false;
63     return 0;
64     }
65
66 strError = "Incorrect value \'" + value + "\'.";
67 return -1;
68 }
69 //-----------------------------------------------------------------------------
70 int SETTINGS::ParseInt(const string & value, int * val)
71 {
72 char *res;
73 *val = strtol(value.c_str(), &res, 10);
74 if (*res != 0)
75     {
76     strError = "Cannot convert \'" + value + "\' to integer.";
77     return -1;
78     }
79 return 0;
80 }
81 //-----------------------------------------------------------------------------
82 int SETTINGS::ParseIntInRange(const string & value, int min, int max, int * val)
83 {
84 if (ParseInt(value, val) != 0)
85     return -1;
86
87 if (*val < min || *val > max)
88     {
89     strError = "Value \'" + value + "\' out of range.";
90     return -1;
91     }
92
93 return 0;
94 }
95 */
96 //-----------------------------------------------------------------------------
97 int SETTINGS::ParseModuleSettings(const DOTCONFDocumentNode * node, vector<PARAM_VALUE> * params)
98 {
99 /*if (!node)
100     return 0;*/
101 const DOTCONFDocumentNode * childNode;
102 PARAM_VALUE pv;
103 const char * value;
104
105 pv.param = node->getName();
106
107 if (node->getValue(1))
108     {
109     strError = "Unexpected value \'" + string(node->getValue(1)) + "\'.";
110     return -1;
111     }
112
113 value = node->getValue(0);
114
115 if (!value)
116     {
117     strError = "Module name expected.";
118     return -1;
119     }
120
121 childNode = node->getChildNode();
122 while (childNode)
123     {
124     pv.param = childNode->getName();
125     int i = 0;
126     while ((value = childNode->getValue(i)) != NULL)
127         {
128         //printfd(__FILE__, "--> param=\'%s\' value=\'%s\'\n", childNode->getName(), value);
129         pv.value.push_back(value);
130         i++;
131         }
132     params->push_back(pv);
133     pv.value.clear();
134     childNode = childNode->getNextNode();
135     }
136
137 /*for (unsigned i = 0; i < params->size(); i++)
138     {
139     printfd(__FILE__, "param \'%s\'\n", (*params)[i].param.c_str());
140     for (unsigned j = 0; j < (*params)[i].value.size(); j++)
141         {
142         printfd(__FILE__, "value \'%s\'\n", (*params)[i].value[j].c_str());
143         }
144     }*/
145
146 return 0;
147 }
148 //-----------------------------------------------------------------------------
149 string SETTINGS::GetStrError() const
150 {
151 return strError;
152 }
153 //-----------------------------------------------------------------------------
154 int SETTINGS::ReadSettings()
155 {
156 const char * requiredOptions[] = {
157     "ModulesPath",
158     "SourceStoreModule",
159     "DestStoreModule",
160     NULL
161     };
162 int sourceStoreModulesCount = 0;
163 int destStoreModulesCount = 0;
164
165 DOTCONFDocument conf(DOTCONFDocument::CASEINSENSITIVE);
166 conf.setRequiredOptionNames(requiredOptions);
167
168 //printfd(__FILE__, "Conffile: %s\n", confFile.c_str());
169
170 if(conf.setContent(confFile.c_str()) != 0)
171     {
172     strError = "Cannot read file " + confFile + ".";
173     return -1;
174     }
175
176 const DOTCONFDocumentNode * node = conf.getFirstNode();
177
178 while (node)
179     {
180     if (strcasecmp(node->getName(), "ModulesPath") == 0)
181         {
182         modulesPath = node->getValue(0);
183         //printfd(__FILE__, "ModulesPath: %s\n", logFile.c_str());
184         }
185
186     if (strcasecmp(node->getName(), "SourceStoreModule") == 0)
187         {
188         // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ StoreModule
189         //printfd(__FILE__, "StoreModule\n");
190
191         if (node->getValue(1))
192             {
193             // StoreModule ÄÏÌÖÅΠÉÍÅÔØ 1 ÁÔÒÉÂÕÔ
194             strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
195             return -1;
196             }
197
198         if (sourceStoreModulesCount)
199             {
200             // äÏÌÖÅΠÂÙÔØ ÔÏÌØËÏ ÏÄÉΠÍÏÄÕÌØ StoreModule!
201             strError = "Should be only one source StoreModule.";
202             return -1;
203             }
204         sourceStoreModulesCount++;
205
206         //storeModuleSettings.clear(); //TODO To make constructor
207         //printfd(__FILE__, "StoreModule %s\n", node->getValue());
208         sourceStoreModuleSettings.moduleName = node->getValue(0);
209         ParseModuleSettings(node, &sourceStoreModuleSettings.moduleParams);
210         }
211
212     if (strcasecmp(node->getName(), "DestStoreModule") == 0)
213         {
214         // íÙ ×ÎÕÔÒÉ ÓÅËÃÉÉ StoreModule
215         //printfd(__FILE__, "StoreModule\n");
216
217         if (node->getValue(1))
218             {
219             // StoreModule ÄÏÌÖÅΠÉÍÅÔØ 1 ÁÔÒÉÂÕÔ
220             strError = "Unexpected \'" + string(node->getValue(1)) + "\'.";
221             return -1;
222             }
223
224         if (destStoreModulesCount)
225             {
226             // äÏÌÖÅΠÂÙÔØ ÔÏÌØËÏ ÏÄÉΠÍÏÄÕÌØ StoreModule!
227             strError = "Should be only one dest StoreModule.";
228             return -1;
229             }
230         destStoreModulesCount++;
231
232         //storeModuleSettings.clear(); //TODO To make constructor
233         //printfd(__FILE__, "StoreModule %s\n", node->getValue());
234         destStoreModuleSettings.moduleName = node->getValue(0);
235         ParseModuleSettings(node, &destStoreModuleSettings.moduleParams);
236         }
237
238     node = node->getNextNode();
239     }
240
241 //sort(modulesSettings.begin(), modulesSettings.end());
242 //modulesSettings.erase(unique(modulesSettings.begin(), modulesSettings.end()), modulesSettings.end());
243
244 return 0;
245 }
246 //-----------------------------------------------------------------------------
247 int SETTINGS::Reload ()
248 {
249 return ReadSettings();
250 }
251 //-----------------------------------------------------------------------------
252 const MODULE_SETTINGS & SETTINGS::GetSourceStoreModuleSettings() const
253 {
254 return sourceStoreModuleSettings;
255 }
256 //-----------------------------------------------------------------------------
257 const MODULE_SETTINGS & SETTINGS::GetDestStoreModuleSettings() const
258 {
259 return destStoreModuleSettings;
260 }
261 //-----------------------------------------------------------------------------
262 const string & SETTINGS::GetModulesPath() const
263 {
264 return modulesPath;
265 }
266 //-----------------------------------------------------------------------------
267