83 delete [] m_pchFileName;
85 for(
int v = 0 ; v < m_nVariableCount; v++)
87 delete [] m_ppVariables[v]->szName;
88 delete [] m_ppVariables[v]->szValue;
90 delete m_ppVariables[v];
113 FILE* pCfgFile = fopen(m_pchFileName,
"r");
116 printf(
"error: could not open config file '%s'\n",m_pchFileName);
121 fseek(pCfgFile , 0 , SEEK_END);
122 m_nFileLength = ftell(pCfgFile);
126 char *pchBuffer =
new char[m_nFileLength];
129 printf(
"error: out of memory\n");
135 if (fread(pchBuffer, 1, m_nFileLength,pCfgFile) != m_nFileLength)
145 bResult = ParseBuffer(pchBuffer);
158 delete [] m_pchFileName;
160 m_pchFileName =
new char[strlen(pchFileName) + 1];
161 strcpy(m_pchFileName, pchFileName);
167 bool bResult =
false;
170 bResult = GetVarByName( szName, szString );
173 szReturnString = szString;
180 bool bResult =
false;
183 bResult = GetVarByName( szName, szInteger );
185 if(bResult) nReturnInt = atoi(szInteger);
192 bool bResult =
false;
195 bResult = GetVarByName( szName, szFloat );
196 if (bResult) fReturnFloat = (float)atof(szFloat);
203 bool bResult =
false;
206 bResult = GetVarByName( szName, szDouble );
207 if(bResult) dReturnDouble = atof(szDouble);
215 char szTrue[] =
"true";
217 bool bResult =
false;
220 bResult = GetVarByName( szName, szBool );
224 if(strcmp(szBool,szOn) == 0 ||
225 strcmp(szBool,szTrue) == 0)
237 bool CConfiguration::ParseBuffer(
char* pchBuffer)
242 int cnBufferPosition = 0;
243 char* pchCurrentName = NULL;
244 char* pchCurrentValue = NULL;
251 fExit = !SeekNextContent(pchBuffer,cnBufferPosition);
253 }
while(CheckControlCharacter(pchBuffer,cnBufferPosition));
256 fError = !ExtractName(pchBuffer,cnBufferPosition, pchCurrentName);
259 printf(
"error: could not extract variable name in line %d\n", GetLineNumber(pchBuffer, cnBufferPosition));
264 fExit = !SeekNextContent(pchBuffer,cnBufferPosition);
267 fError = !ExtractValue(pchBuffer,cnBufferPosition, pchCurrentValue);
270 printf(
"error: could not extract value of variable '%s' in line %d\n", pchCurrentName, GetLineNumber(pchBuffer,cnBufferPosition));
275 AddVariable(pchCurrentName,pchCurrentValue);
282 bool CConfiguration::CheckControlCharacter(
char* pchBuffer,
int& cnBufferPosition)
284 switch(pchBuffer[cnBufferPosition])
287 while(cnBufferPosition < m_nFileLength && pchBuffer[cnBufferPosition] !=
'\n')
292 while (cnBufferPosition < m_nFileLength && pchBuffer[cnBufferPosition] !=
'\n')
293 printf(
"%c", pchBuffer[cnBufferPosition++]);
304 bool CConfiguration::SeekNextContent(
char* pchBuffer,
int& cnBufferPosition)
306 while( cnBufferPosition < m_nFileLength &&
307 ( pchBuffer[cnBufferPosition] ==
' ' ||
308 pchBuffer[cnBufferPosition] ==
'\t' ||
309 pchBuffer[cnBufferPosition] ==
'\r' ||
310 pchBuffer[cnBufferPosition] ==
'\n' ||
311 pchBuffer[cnBufferPosition] ==
'\x27'
316 return cnBufferPosition != m_nFileLength;
319 bool CConfiguration::ExtractName(
char* pchBuffer,
int& cnBufferPosition,
char*& pchResultName)
321 int nNameStart = cnBufferPosition;
323 while( cnBufferPosition < m_nFileLength &&
324 ( pchBuffer[cnBufferPosition] !=
' ' &&
325 pchBuffer[cnBufferPosition] !=
'\t' &&
326 pchBuffer[cnBufferPosition] !=
'\r' &&
327 pchBuffer[cnBufferPosition] !=
'\n' &&
328 pchBuffer[cnBufferPosition] !=
'\x27'
333 int nNameEnd = cnBufferPosition;
336 pchResultName =
new char[nNameEnd - nNameStart + 1];
337 strncpy(pchResultName, pchBuffer + nNameStart, nNameEnd - nNameStart);
338 pchResultName[nNameEnd - nNameStart] =
'\0';
343 bool CConfiguration::ExtractValue(
char* pchBuffer,
int& cnBufferPosition,
char*& pchResultValue)
345 int nValueStart = cnBufferPosition;
346 bool fStringMode =
false;
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)
358 if (pchBuffer[cnBufferPosition] ==
'\"')
364 int nValueEnd = cnBufferPosition;
375 pchResultValue =
new char[nValueEnd - nValueStart + 1 - nAdjust];
376 strncpy(pchResultValue, pchBuffer + nValueStart + nAdjust, nValueEnd - nValueStart - nAdjust);
377 pchResultValue[nValueEnd - nValueStart - nAdjust] =
'\0';
382 void CConfiguration::AddVariable(
char* pchCurrentName,
char* pchCurrentValue)
384 if (pchCurrentName[0] ==
'\0' || pchCurrentValue[0] ==
'\0')
386 delete [] pchCurrentName;
387 delete [] pchCurrentValue;
394 TVariableMap **ppNewMap =
new TVariableMap*[m_nVariableCount];
399 for (
int v = 0 ; v < (m_nVariableCount - 1) ; v++)
400 ppNewMap[v] = m_ppVariables[v];
403 delete [] m_ppVariables;
407 TVariableMap *pVariable =
new TVariableMap();
409 pVariable->szName = pchCurrentName;
410 pVariable->szValue = pchCurrentValue;
411 ppNewMap[m_nVariableCount - 1] = pVariable;
414 m_ppVariables = ppNewMap;
417 int CConfiguration::GetLineNumber(
char* pchBuffer,
int cnBufferPosition)
421 for (
int pos = 0 ; pos < cnBufferPosition ; pos++)
423 if (pchBuffer[pos] ==
'\r')
428 else if (pchBuffer[pos] ==
'\n')
437 bool CConfiguration::GetVarByName(
char* szName,
char*& szReturnString)
439 for (
int v = 0 ; v < m_nVariableCount ; v++)
441 if (strcmp(szName,m_ppVariables[v]->szName) == 0)
443 szReturnString = m_ppVariables[v]->szValue;
bool GetDouble(char *szName, double &dReturnDouble)
Get double from configutation file.
bool GetFloat(char *szName, float &fReturnFloat)
Get float from configutation file.
bool GetInt(char *szName, int &nReturnInt)
Get integer from configutation file.
CConfiguration()
Construct a new Configuration object.
bool GetString(char *szName, char *&szReturnString)
Get string from configutation file.
virtual ~CConfiguration()
Destructor.
void SetFileName(const char *szFileName)
bool GetBool(char *szName, bool &bReturnBool)
Get bool from configutation file.