comparison src/Support/Parameters.cc @ 0:582cbe817f2c

- Initial add of support code and modules. Not everything is working yet.
author tomwalters
date Fri, 12 Feb 2010 12:31:23 +0000
parents
children bc394a985042
comparison
equal deleted inserted replaced
-1:000000000000 0:582cbe817f2c
1 // Copyright 2006-2010, Willem van Engen
2 //
3 // AIM-C: A C++ implementation of the Auditory Image Model
4 // http://www.acousticscale.org/AIMC
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 //!
20 //! \file
21 //! \brief Main parameters store
22 //!
23 //! \author Willem van Engen <cnbh@willem.engen.nl>
24 //! \date created 2006/09/21
25 //! \version \$Id: Parameters.cc 4 2010-02-03 18:44:58Z tcw $
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "Support/Common.h"
32 #include "Support/Parameters.h"
33
34 namespace aimc {
35 const char *Parameters::m_SDefaultIniSection = "";
36
37 Parameters::Parameters() {
38 m_iNestCount = 0;
39 m_pIni = new CSimpleIniCase(false, false, true);
40 AIM_ASSERT(m_pIni);
41 }
42
43 Parameters::~Parameters() {
44 DELETE_IF_NONNULL(m_pIni);
45 }
46
47 const char * Parameters::DefaultString(const char* sName, const char* val) {
48 AIM_ASSERT(m_pIni);
49 if (!IsSet(sName)) {
50 m_pIni->SetValue(m_SDefaultIniSection, sName, val);
51 }
52 return m_pIni->GetString(sName);
53 }
54
55 int Parameters::DefaultInt(const char* sName, int val) {
56 AIM_ASSERT(m_pIni);
57 if (!IsSet(sName)) {
58 m_pIni->SetInt(m_SDefaultIniSection, sName, val);
59 }
60 return m_pIni->GetInt(sName);
61 }
62
63 unsigned int Parameters::DefaultUInt(const char* sName, unsigned int val) {
64 AIM_ASSERT(m_pIni);
65 if (!IsSet(sName)) {
66 m_pIni->SetUInt(m_SDefaultIniSection, sName, val);
67 }
68 return m_pIni->GetUInt(sName);
69 }
70
71 float Parameters::DefaultFloat(const char* sName, float val) {
72 AIM_ASSERT(m_pIni);
73 if (!IsSet(sName)) {
74 m_pIni->SetFloat(m_SDefaultIniSection, sName, val);
75 }
76 return m_pIni->GetFloat(sName);
77 }
78
79 bool Parameters::DefaultBool(const char* sName, bool val) {
80 AIM_ASSERT(m_pIni);
81 if (!IsSet(sName)) {
82 m_pIni->SetBool(m_SDefaultIniSection, sName, val);
83 }
84 return m_pIni->GetBool(sName);
85 }
86
87 void Parameters::SetString(const char *sName, const char *val) {
88 AIM_ASSERT(m_pIni);
89 m_pIni->SetValue(m_SDefaultIniSection, sName, val);
90 }
91
92 void Parameters::SetInt(const char *sName, int val) {
93 char sVal[20];
94 snprintf(sVal, sizeof(sVal)/sizeof(sVal[0]), "%d", val);
95 SetString(sName, sVal);
96 }
97
98 void Parameters::SetUInt(const char *sName, unsigned int val) {
99 char sVal[20];
100 snprintf(sVal, sizeof(sVal)/sizeof(sVal[0]), "%ud", val);
101 SetString(sName, sVal);
102 }
103
104 void Parameters::SetBool(const char *sName, bool val) {
105 SetString(sName, val ? "true" : "false");
106 }
107
108 void Parameters::SetFloat(const char *sName, float val) {
109 char sVal[20];
110 snprintf(sVal, sizeof(sVal)/sizeof(sVal[0]), "%f", val);
111 SetString(sName, sVal);
112 }
113
114 const char *Parameters::GetString(const char *sName) {
115 AIM_ASSERT(m_pIni);
116 const char *sVal = m_pIni->GetValue(m_SDefaultIniSection, sName, NULL);
117 if (!sVal) {
118 LOG_ERROR(_T("Parameter not found '%s'"), sName);
119 return "";
120 }
121 return sVal;
122 }
123
124 int Parameters::GetInt(const char *sName) {
125 return atoi(GetString(sName));
126 }
127
128 unsigned int Parameters::GetUInt(const char *sName) {
129 return atoi(GetString(sName));
130 }
131
132 float Parameters::GetFloat(const char *sName) {
133 return (float)atof(GetString(sName));
134 }
135
136 bool Parameters::GetBool(const char *sName) {
137 const char *sVal = GetString(sName);
138 if (strcmp(sVal, "true")==0 || strcmp(sVal, "on")==0 ||
139 strcmp(sVal, "yes")==0 || strcmp(sVal, "1")==0 ||
140 strcmp(sVal, "y")==0 || strcmp(sVal, "t")==0)
141 return true;
142 else
143 return false;
144 }
145
146 bool Parameters::IsSet(const char *sName) {
147 AIM_ASSERT(m_pIni);
148 return m_pIni->GetValue(m_SDefaultIniSection, sName, NULL)!=NULL;
149 }
150
151 bool Parameters::Parse(const char *sCmd) {
152 //! \todo There is some code duplication here from Parameters::Merge()
153
154 CSimpleIniCase *pIni2 = new CSimpleIniCase(false, false, true);
155 AIM_ASSERT(pIni2);
156 if (pIni2->Load(sCmd, strlen(sCmd)) < 0) {
157 LOG_ERROR(_T("Could not parse option '%s'"), sCmd);
158 delete pIni2;
159 return false;
160 }
161
162 // if there are keys and values...
163 const CSimpleIniCase::TKeyVal *pSectionData =
164 pIni2->GetSection(m_SDefaultIniSection);
165 if (pSectionData) {
166 // iterate over all keys and set them in the current parameter file
167 CSimpleIniCase::TKeyVal::const_iterator iKeyVal = pSectionData->begin();
168 for ( ;iKeyVal != pSectionData->end(); ++iKeyVal) {
169 m_pIni->SetValue(m_SDefaultIniSection,
170 iKeyVal->first.pItem,
171 iKeyVal->second);
172 }
173 }
174 delete pIni2;
175 return true;
176 }
177
178 bool Parameters::Delete(const char *sName) {
179 AIM_ASSERT(m_pIni);
180 return(m_pIni->Delete(m_SDefaultIniSection, sName));
181 }
182
183 bool Parameters::LoadFile(const char *sParamFilename) {
184 AIM_ASSERT(m_pIni);
185 SI_Error siErr;
186 bool bRet = true;
187
188 // Avoid inclusion loops
189 if (m_iNestCount >= m_iNestCountMaximum) {
190 LOG_ERROR(_T("Possible inclusion loop in file '%s' (%d times)"),
191 sParamFilename, m_iNestCount);
192 return false;
193 }
194 m_iNestCount++;
195
196 if ( (siErr=m_pIni->LoadFile(sParamFilename))<0 ) {
197 // Don't complain if file not found, but do return error
198 if (siErr!=SI_FILE)
199 LOG_ERROR(_T("Couldn't parse parameters from '%s'"), sParamFilename);
200 m_iNestCount--;
201 return false;
202 }
203
204 m_iNestCount--;
205 return bRet;
206 }
207
208 bool Parameters::MergeFile(const char *sParamFilename) {
209 Parameters *pParam2 = new Parameters();
210 AIM_ASSERT(pParam2);
211 if (!pParam2->LoadFile(sParamFilename)) {
212 LOG_ERROR(_T("Could not load parameter file '%s' for merging"),
213 sParamFilename);
214 delete pParam2;
215 return false;
216 }
217
218 // if there are keys and values...
219 const CSimpleIniCase::TKeyVal *pSectionData =
220 pParam2->m_pIni->GetSection(m_SDefaultIniSection);
221 if (pSectionData) {
222 // iterate over all keys and set them in the current parameter file
223 CSimpleIniCase::TKeyVal::const_iterator iKeyVal = pSectionData->begin();
224 for ( ;iKeyVal != pSectionData->end(); ++iKeyVal) {
225 m_pIni->SetValue(m_SDefaultIniSection,
226 iKeyVal->first.pItem,
227 iKeyVal->second);
228 }
229 }
230
231 delete pParam2;
232 return true;
233 }
234
235 bool Parameters::Merge(const char *sParamFilename) {
236 return MergeFile(sParamFilename);
237 }
238
239 bool Parameters::Load(const char *sParamFilename) {
240 bool bRet = true;
241 // Load parameter file
242 bRet &= Merge(sParamFilename);
243 return bRet;
244 }
245
246 bool Parameters::Save(const char *sParamFilename) {
247 AIM_ASSERT(m_pIni);
248 SI_Error siErr;
249 FILE *pSaveFile;
250
251 pSaveFile = fopen(sParamFilename, "wb");
252 if (pSaveFile == NULL) {
253 LOG_ERROR(_T("Couldn't create parameters file '%s' to save to"),
254 sParamFilename);
255 return false;
256 }
257
258 if ((siErr = m_pIni->SaveFile(pSaveFile)) < 0 ) {
259 LOG_ERROR(_T("Couldn't save parameters in file '%s'"), sParamFilename);
260 return false;
261 }
262
263 fclose(pSaveFile);
264
265 return true;
266 }
267 } // namespace aimc