Configuration.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: Configuration.cpp
37 // Author: Kai Welke
38 // Date: 03.03.2005
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "Configuration.h"
49 
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 
54 
55 
56 // ****************************************************************************
57 // Constructors / Destructor
58 // ****************************************************************************
59 
61 {
62  m_pchFileName = 0;
63  m_nFileLength = 0;
64 
65  m_ppVariables = 0;
66  m_nVariableCount = 0;
67 }
68 
69 CConfiguration::CConfiguration(const char* pchFileName)
70 {
71  m_pchFileName = 0;
72  m_nFileLength = 0;
73 
74  m_ppVariables = 0;
75  m_nVariableCount = 0;
76 
77  SetFileName(pchFileName);
78 }
79 
81 {
82  if (m_pchFileName)
83  delete [] m_pchFileName;
84 
85  for(int v = 0 ; v < m_nVariableCount; v++)
86  {
87  delete [] m_ppVariables[v]->szName;
88  delete [] m_ppVariables[v]->szValue;
89 
90  delete m_ppVariables[v];
91  }
92 
93  delete m_ppVariables;
94 }
95 
96 
97 // ****************************************************************************
98 // Methods
99 // ****************************************************************************
100 
101 bool CConfiguration::Read(const char* pchFileName)
102 {
103  SetFileName(pchFileName);
104 
105  return Read();
106 }
107 
109 {
110  bool bResult;
111 
112  // open config file for reading
113  FILE* pCfgFile = fopen(m_pchFileName,"r");
114  if (!pCfgFile)
115  {
116  printf("error: could not open config file '%s'\n",m_pchFileName);
117  return false;
118  }
119 
120  // obtain file size.
121  fseek(pCfgFile , 0 , SEEK_END);
122  m_nFileLength = ftell(pCfgFile);
123  rewind(pCfgFile);
124 
125  // create file buffer
126  char *pchBuffer = new char[m_nFileLength];
127  if (!pchBuffer)
128  {
129  printf("error: out of memory\n");
130  fclose(pCfgFile);
131  return false;
132  }
133 
134  // read whole file
135  if (fread(pchBuffer, 1, m_nFileLength,pCfgFile) != m_nFileLength)
136  {
137  fclose(pCfgFile);
138  return false;
139  }
140 
141  // close config file
142  fclose(pCfgFile);
143 
144  // parse the file
145  bResult = ParseBuffer(pchBuffer);
146 
147  // release memory
148  delete [] pchBuffer;
149 
150  return bResult;
151 
152 }
153 
154 
155 void CConfiguration::SetFileName(const char* pchFileName)
156 {
157  if (m_pchFileName)
158  delete [] m_pchFileName;
159 
160  m_pchFileName = new char[strlen(pchFileName) + 1];
161  strcpy(m_pchFileName, pchFileName);
162 }
163 
164 
165 bool CConfiguration::GetString(char* szName, char*& szReturnString )
166 {
167  bool bResult = false;
168  char* szString;
169 
170  bResult = GetVarByName( szName, szString );
171 
172  if(bResult)
173  szReturnString = szString;
174 
175  return bResult;
176 }
177 
178 bool CConfiguration::GetInt(char* szName, int& nReturnInt )
179 {
180  bool bResult = false;
181  char* szInteger;
182 
183  bResult = GetVarByName( szName, szInteger );
184 
185  if(bResult) nReturnInt = atoi(szInteger);
186 
187  return bResult;
188 }
189 
190 bool CConfiguration::GetFloat(char* szName, float& fReturnFloat )
191 {
192  bool bResult = false;
193  char* szFloat;
194 
195  bResult = GetVarByName( szName, szFloat );
196  if (bResult) fReturnFloat = (float)atof(szFloat);
197 
198  return bResult;
199 }
200 
201 bool CConfiguration::GetDouble(char* szName, double& dReturnDouble )
202 {
203  bool bResult = false;
204  char* szDouble;
205 
206  bResult = GetVarByName( szName, szDouble );
207  if(bResult) dReturnDouble = atof(szDouble);
208 
209  return bResult;
210 }
211 
212 bool CConfiguration::GetBool(char* szName, bool& bReturnBool )
213 {
214  char szOn[] = "on";
215  char szTrue[] = "true";
216 
217  bool bResult = false;
218  char* szBool;
219 
220  bResult = GetVarByName( szName, szBool );
221 
222  if(bResult)
223  {
224  if(strcmp(szBool,szOn) == 0 ||
225  strcmp(szBool,szTrue) == 0)
226  {
227  bReturnBool = true;
228  } else {
229  bReturnBool = false;
230 
231  }
232  }
233  return bResult;
234 }
235 
236 
237 bool CConfiguration::ParseBuffer(char* pchBuffer)
238 {
239  // restart at beginning of buffer
240  bool fExit = false;
241  bool fError = false;
242  int cnBufferPosition = 0;
243  char* pchCurrentName = NULL;
244  char* pchCurrentValue = NULL;
245 
246  while(!fExit)
247  {
248  // check if we have control chars for parser
249  do
250  {
251  fExit = !SeekNextContent(pchBuffer,cnBufferPosition);
252  if(fExit) break;
253  } while(CheckControlCharacter(pchBuffer,cnBufferPosition));
254 
255  // read name
256  fError = !ExtractName(pchBuffer,cnBufferPosition, pchCurrentName);
257  if(fError)
258  {
259  printf("error: could not extract variable name in line %d\n", GetLineNumber(pchBuffer, cnBufferPosition));
260  return false;
261  }
262 
263  // seek to value
264  fExit = !SeekNextContent(pchBuffer,cnBufferPosition);
265 
266  // read value
267  fError = !ExtractValue(pchBuffer,cnBufferPosition, pchCurrentValue);
268  if (fError)
269  {
270  printf("error: could not extract value of variable '%s' in line %d\n", pchCurrentName, GetLineNumber(pchBuffer,cnBufferPosition));
271  return false;
272  }
273 
274  // add to map
275  AddVariable(pchCurrentName,pchCurrentValue);
276 
277  }
278 
279  return true;
280 }
281 
282 bool CConfiguration::CheckControlCharacter(char* pchBuffer, int& cnBufferPosition)
283 {
284  switch(pchBuffer[cnBufferPosition])
285  {
286  case '#':
287  while(cnBufferPosition < m_nFileLength && pchBuffer[cnBufferPosition] != '\n')
288  cnBufferPosition++;
289  return true;
290 
291  case '@':
292  while (cnBufferPosition < m_nFileLength && pchBuffer[cnBufferPosition] != '\n')
293  printf("%c", pchBuffer[cnBufferPosition++]);
294  printf("\n");
295  return true;
296 
297  default:
298  break;
299  }
300 
301  return false;
302 }
303 
304 bool CConfiguration::SeekNextContent(char* pchBuffer,int& cnBufferPosition)
305 {
306  while( cnBufferPosition < m_nFileLength &&
307  ( pchBuffer[cnBufferPosition] == ' ' ||
308  pchBuffer[cnBufferPosition] == '\t' ||
309  pchBuffer[cnBufferPosition] == '\r' ||
310  pchBuffer[cnBufferPosition] == '\n' ||
311  pchBuffer[cnBufferPosition] == '\x27'
312  )
313  )
314  cnBufferPosition++;
315 
316  return cnBufferPosition != m_nFileLength;
317 }
318 
319 bool CConfiguration::ExtractName(char* pchBuffer, int& cnBufferPosition, char*& pchResultName)
320 {
321  int nNameStart = cnBufferPosition;
322 
323  while( cnBufferPosition < m_nFileLength &&
324  ( pchBuffer[cnBufferPosition] != ' ' &&
325  pchBuffer[cnBufferPosition] != '\t' &&
326  pchBuffer[cnBufferPosition] != '\r' &&
327  pchBuffer[cnBufferPosition] != '\n' &&
328  pchBuffer[cnBufferPosition] != '\x27'
329  )
330  )
331  cnBufferPosition++;
332 
333  int nNameEnd = cnBufferPosition;
334 
335  // create new variable name
336  pchResultName = new char[nNameEnd - nNameStart + 1];
337  strncpy(pchResultName, pchBuffer + nNameStart, nNameEnd - nNameStart);
338  pchResultName[nNameEnd - nNameStart] = '\0';
339 
340  return true;
341 }
342 
343 bool CConfiguration::ExtractValue(char* pchBuffer, int& cnBufferPosition, char*& pchResultValue)
344 {
345  int nValueStart = cnBufferPosition;
346  bool fStringMode = false;
347 
348  while (cnBufferPosition < m_nFileLength &&
349  ( (pchBuffer[cnBufferPosition] != ' ' || fStringMode) &&
350  (pchBuffer[cnBufferPosition] != '\t' || fStringMode) &&
351  pchBuffer[cnBufferPosition] != '\r' &&
352  pchBuffer[cnBufferPosition] != '\n' &&
353  pchBuffer[cnBufferPosition] != '\x27' &&
354  !(pchBuffer[cnBufferPosition] == '\"' && fStringMode)
355  )
356  )
357  {
358  if (pchBuffer[cnBufferPosition] == '\"')
359  fStringMode = true;
360 
361  cnBufferPosition++;
362  }
363 
364  int nValueEnd = cnBufferPosition;
365  int nAdjust = 0;
366 
367  // skip hyphens if stringmode!
368  if (fStringMode)
369  {
370  nAdjust = 1;
371  cnBufferPosition++;
372  }
373 
374  // create new variable name
375  pchResultValue = new char[nValueEnd - nValueStart + 1 - nAdjust];
376  strncpy(pchResultValue, pchBuffer + nValueStart + nAdjust, nValueEnd - nValueStart - nAdjust);
377  pchResultValue[nValueEnd - nValueStart - nAdjust] = '\0';
378 
379  return true;
380 }
381 
382 void CConfiguration::AddVariable(char* pchCurrentName, char* pchCurrentValue)
383 {
384  if (pchCurrentName[0] == '\0' || pchCurrentValue[0] == '\0')
385  {
386  delete [] pchCurrentName;
387  delete [] pchCurrentValue;
388  return;
389  }
390 
392 
393  // allocate new array
394  TVariableMap **ppNewMap = new TVariableMap*[m_nVariableCount];
395 
396  // copy old pointers
397  if (m_ppVariables)
398  {
399  for (int v = 0 ; v < (m_nVariableCount - 1) ; v++)
400  ppNewMap[v] = m_ppVariables[v];
401 
402  // get rid of old array
403  delete [] m_ppVariables;
404  }
405 
406  // create new variable
407  TVariableMap *pVariable = new TVariableMap();
408 
409  pVariable->szName = pchCurrentName;
410  pVariable->szValue = pchCurrentValue;
411  ppNewMap[m_nVariableCount - 1] = pVariable;
412 
413  // reassign pointer
414  m_ppVariables = ppNewMap;
415 }
416 
417 int CConfiguration::GetLineNumber(char* pchBuffer, int cnBufferPosition)
418 {
419  int nLines = 0;
420 
421  for (int pos = 0 ; pos < cnBufferPosition ; pos++)
422  {
423  if (pchBuffer[pos] == '\r')
424  {
425  pos++;
426  nLines++;
427  }
428  else if (pchBuffer[pos] == '\n')
429  {
430  nLines++;
431  }
432  }
433 
434  return nLines+1;
435 }
436 
437 bool CConfiguration::GetVarByName(char* szName, char*& szReturnString)
438 {
439  for (int v = 0 ; v < m_nVariableCount ; v++)
440  {
441  if (strcmp(szName,m_ppVariables[v]->szName) == 0)
442  {
443  szReturnString = m_ppVariables[v]->szValue;
444  return true;
445  }
446  }
447 
448  return false;
449 }
void SetFileName(const char *szFileName)
TVariableMap ** m_ppVariables
bool ParseBuffer(char *pchBuffer)
void AddVariable(char *pchCurrentName, char *pchCurrentValue)
int GetLineNumber(char *pchBuffer, int cnBufferPosition)
bool GetVarByName(char *szName, char *&szReturnString)
Determine if variable with Name exists.
bool ExtractValue(char *pchBuffer, int &cnBufferPosition, char *&pchResultValue)
bool GetFloat(char *szName, float &fReturnFloat)
Get float from configutation file.
bool ExtractName(char *pchBuffer, int &cnBufferPosition, char *&pchResultName)
bool CheckControlCharacter(char *pchBuffer, int &cnBufferPosition)
struct CConfiguration::VarMap TVariableMap
char * m_pchFileName
bool GetBool(char *szName, bool &bReturnBool)
Get bool from configutation file.
bool SeekNextContent(char *pchBuffer, int &cnBufferPosition)
bool GetDouble(char *szName, double &dReturnDouble)
Get double from configutation file.
virtual ~CConfiguration()
Destructor.
bool GetString(char *szName, char *&szReturnString)
Get string from configutation file.
CConfiguration()
Construct a new Configuration object.
bool GetInt(char *szName, int &nReturnInt)
Get integer from configutation file.
const GLdouble * v
Definition: glext.h:3212


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27