Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/ini_parser.cc @ 5182

Last change on this file since 5182 was 5169, checked in by bensch, 19 years ago

orxonox/trunk: reverted silly changes

File size: 12.7 KB
RevLine 
[4597]1/*
[2064]2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
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 2, or (at your option)
9   any later version.
10
11   ### File Specific:
[5014]12   main-programmer: Benjamin Grauer
13   co-programmer: Christian Meyer
14
15   2005-08-14: complete reimplementation:
16               now the File is parsed at the initialisation,
17               and informations is gathered there.
[2064]18*/
19
20
21#include "ini_parser.h"
[4381]22
[5014]23#include "list.h"
[5031]24#include <stdlib.h>
25#include <string.h>
[4381]26#include "debug.h"
[2064]27
28using namespace std;
29
[2141]30/**
[4836]31 *  constructs an IniParser using a file
[5017]32 * @param fileName: the path and name of the file to parse
[2141]33*/
[5014]34IniParser::IniParser (const char* fileName)
[2064]35{
[5014]36  this->currentEntry = NULL;
37  this->currentSection = NULL;
38  this->sections = NULL;
[5031]39  this->fileName = NULL;
[5014]40  if (fileName != NULL)
[5018]41    this->readFile(fileName);
[2064]42}
43
[2141]44/**
[4836]45 *  removes the IniParser from memory
[2141]46*/
[2064]47IniParser::~IniParser ()
48{
[5014]49  deleteSections();
[2064]50}
51
[5015]52/**
53 * removes all the sections. This is like delete, but even cooler :)
54 */
[5014]55void IniParser::deleteSections()
[2064]56{
[5014]57  if (this->sections)
58  {
59    tIterator<IniSection>* sectionIt = this->sections->getIterator();
[5115]60    IniSection* sectionEnum = sectionIt->firstElement();
[5014]61    while (sectionEnum)
[3231]62    {
[5014]63      tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
[5115]64      IniEntry* entryEnum = entryIt->firstElement();
[5014]65      while (entryEnum)
66      {
67        delete []entryEnum->name;
68        delete []entryEnum->value;
69        delete entryEnum;
70        entryEnum = entryIt->nextElement();
71      }
72      delete entryIt;
73
74      delete []sectionEnum->name;
75      delete sectionEnum->entries;
76      delete sectionEnum;
77      sectionEnum = sectionIt->nextElement();
[3231]78    }
[5014]79    delete sectionIt;
80  }
81  delete this->sections;
[5015]82  this->currentEntry = NULL;
83  this->currentSection = NULL;
[5014]84  this->sections = NULL;
[5031]85  this->setFileName(NULL);
[2064]86}
87
[2141]88/**
[5014]89 * opens another file to parse
[5017]90 * @param fileName: path and name of the new file to parse
[5014]91 * @return true on success false otherwise;
[2141]92*/
[5018]93bool IniParser::readFile(const char* fileName)
[2064]94{
[5014]95  FILE*    stream;           //!< The stream we use to read the file.
96  if (sections != NULL)
97    deleteSections();
[5015]98  if( fileName == NULL)
99    return false;
[5031]100  this->setFileName(fileName);
[5014]101
[5015]102  if( (stream = fopen (fileName, "r")) == NULL)
[5014]103  {
[5015]104    PRINTF(1)("IniParser could not open %s\n", fileName);
[5014]105    return false;
106  }
107  else
108  {
109    this->currentEntry = NULL;
110    this->currentSection = NULL;
111    this->sections = new tList<IniSection>;
112
113    /////////////////////////////
114    // READING IN THE INI-FILE //
115    /////////////////////////////
116    char lineBuffer[PARSELINELENGHT];
117    char buffer[PARSELINELENGHT];
[5021]118    const char* lineBegin;
[5014]119    char* ptr;
120
121    while( !feof( stream))
[2551]122    {
123      // get next line
[5014]124      fgets (lineBuffer, PARSELINELENGHT, stream);
[5021]125      lineBegin = lineBuffer;
[5014]126      // remove newline char, and \0-terminate
127      if( (ptr = strchr( lineBuffer, '\n')) != NULL)
128        *ptr = 0;
[5021]129      // cut up to the beginning of the line.
130      while((*lineBegin == ' ' || *lineBegin == '\t') && lineBegin < lineBuffer + strlen(lineBuffer))
131        ++lineBegin;
132      if (strlen(lineBegin) <= 1 || *lineBegin == '#' || *lineBegin == ';')
133        continue;//printf("empty Line\n");
[2551]134      // check for section identifyer
[5021]135      else if( sscanf (lineBegin, "[%s", buffer) == 1)
[5014]136      {
137        if( (ptr = strchr( buffer, ']')) != NULL)
[4597]138        {
[5014]139          *ptr = 0;
[5020]140          this->addSection(buffer);
[4597]141        }
[5014]142      }
[5018]143      // check for Entry identifier (Entry = Value)
[5021]144      else if( (ptr = strchr( lineBegin, '=')) != NULL)
[5014]145      {
146        if (currentSection == NULL)
147        {
[5021]148          PRINTF(2)("Not in a Section yet for %s\n", lineBegin);
[5014]149          continue;
150        }
[5021]151        if( ptr == lineBegin)
[5014]152          continue;
153        char* valueBegin = ptr+1;
[5021]154        while ((*valueBegin == ' ' || *valueBegin == '\t') && valueBegin <= lineBegin + strlen(lineBegin))
[5014]155          ++valueBegin;
[5022]156        char* valueEnd = valueBegin + strlen(valueBegin)-1;
157        while ((*valueEnd == ' ' || *valueEnd == '\t') && valueEnd >= valueBegin)
158          --valueEnd;
159        valueEnd[1] = '\0';
[5018]160        char* nameEnd = ptr-1;
[5021]161        while ((*nameEnd == ' ' || *nameEnd == '\t' ) && nameEnd >= lineBegin)
[5014]162          --nameEnd;
163        nameEnd[1] = '\0';
[5018]164
[5021]165        this->addVar(lineBegin, valueBegin);
[5014]166      }
[2551]167    }
[5014]168  }
169  fclose(stream);
170  return true;
[2064]171}
172
[2141]173/**
[5020]174 * opens a file and writes to it
175 * @param fileName: path and name of the new file to write to
176 * @return true on success false otherwise
177 */
178bool IniParser::writeFile(const char* fileName)
179{
180  FILE*    stream;           //!< The stream we use to read the file.
181  if( fileName == NULL)
182    return false;
183
184  if( (stream = fopen (fileName, "w")) == NULL)
185  {
186    PRINTF(1)("IniParser could not open %s\n", fileName);
187    return false;
188  }
189  else
190  {
191    if (this->sections)
192    {
193      tIterator<IniSection>* sectionIt = this->sections->getIterator();
[5115]194      IniSection* sectionEnum = sectionIt->firstElement();
[5020]195      while (sectionEnum)
196      {
[5021]197        fprintf(stream, "\n [%s]\n", sectionEnum->name);
[5020]198
199        tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
[5115]200        IniEntry* entryEnum = entryIt->firstElement();
[5020]201        while (entryEnum)
202        {
[5021]203          fprintf(stream, "   %s = %s\n", entryEnum->name, entryEnum->value);
[5020]204
205          entryEnum = entryIt->nextElement();
206        }
207        delete entryIt;
208
209        sectionEnum = sectionIt->nextElement();
210      }
211      delete sectionIt;
212    }
213    else
214      PRINTF(1)("%s no sections defined yet\n", fileName);
215  }
216  fclose(stream);
217}
218
[5021]219/**
220 * adds a section to the list of Sections,
221 * if no Section list is availiable, it will create it
222 * @param sectionName the Name of the section to add
223 * @return true on success... there is only success or segfault :)
224 */
[5020]225bool IniParser::addSection(const char* sectionName)
226{
227  if (this->sections == NULL)
228    this->sections = new tList<IniSection>;
229
230  IniSection* newSection = new IniSection;
231  newSection->name = new char[strlen(sectionName)+1];
232  strcpy(newSection->name, sectionName);
233  newSection->entries = new tList<IniEntry>;
234  this->currentSection = newSection;
235  this->sections->add(newSection);
[5031]236  PRINTF(5)("Added Section %s\n", sectionName);
[5021]237  return true;
[5020]238}
239
240/**
[5014]241 *  set the parsing cursor to the specified section
242 * @param sectionName: the name of the section to set the cursor to
243 * @return true on success or false if the section could not be found
[2141]244*/
[5014]245bool IniParser::getSection( const char* sectionName)
[2064]246{
[5014]247  tIterator<IniSection>* sectionIt = this->sections->getIterator();
[5115]248  IniSection* sectionEnum = sectionIt->firstElement();
[5014]249  while (sectionEnum)
250  {
251    if (!strcmp(sectionEnum->name, sectionName))
[3231]252    {
[5014]253      this->currentSection = sectionEnum;
[5015]254      this->currentEntry = NULL;
[5014]255      delete sectionIt;
256      return true;
[3231]257    }
[4597]258
[5014]259    sectionEnum = sectionIt->nextElement();
260  }
261  delete sectionIt;
262  return false;
263}
[4597]264
[5014]265/**
[5015]266 * moves to the first section
267 */
268void IniParser::getFirstSection()
269{
270  if (this->sections)
271    this->currentSection = this->sections->firstElement();
272  else
273    this->currentSection = NULL;
274  this->currentEntry = NULL;
275}
276
277/**
[5014]278 * searches the next section
279 * @returns the name of the section if found, NULL otherwise
280 */
281const char* IniParser::nextSection()
282{
283  if (this->currentSection == NULL)
284    return NULL;
285  else
286  {
287    if (this->sections)
[5015]288    {
289      if (this->currentSection == this->sections->lastElement())
290        this->currentSection = NULL;
291      else
292        this->currentSection = this->sections->nextElement(this->currentSection);
293    }
[5014]294  }
[5015]295
[5014]296  if (this->currentSection != NULL)
297    return this->currentSection->name;
298  else
299    return NULL;
[2064]300}
301
[2141]302/**
[5015]303 * moves to the first Variable of the current Section
304 */
305void IniParser::getFirstVar()
306{
307  if (this->currentSection)
308    this->currentEntry = this->currentSection->entries->firstElement();
309  else
310    this->currentEntry = NULL;
311}
312
313/**
[5014]314 *  gets the next VarName=VarValue pair from the parsing stream
315 * @return true on success, false otherwise (in the latter case name and value will be NULL)
316 */
317bool IniParser::nextVar()
318{
[5015]319  if (this->currentSection == NULL
320      || this->currentEntry == NULL
321      || this->currentEntry == this->currentSection->entries->lastElement())
[5014]322  {
323    this->currentEntry = NULL;
324    return false;
325  }
[5015]326  this->currentEntry = this->currentSection->entries->nextElement(this->currentEntry);
[5014]327
328  if (this->currentEntry == NULL)
329    return false;
330  else
331    return true;
332}
333
334/**
[5020]335 * adds a new Entry to either the currentSection or the section called by sectionName
336 * @param entryName the Name of the Entry to add
337 * @param value the value to assign to this entry
338 * @param sectionName if NULL then this entry will be set to the currentSection
339 * otherwise to the section refered to by sectionName.
340 * If both are NULL no entry will be added
341 * @return true if everything is ok false on error
342 */
343bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName)
344{
345  IniSection* addSection = NULL;
346  if (sectionName != NULL)
347  {
348    tIterator<IniSection>* sectionIt = this->sections->getIterator();
[5115]349    IniSection* sectionEnum = sectionIt->firstElement();
[5020]350    while (sectionEnum)
351    {
352      if (!strcmp(sectionEnum->name, sectionName))
353      {
354        addSection = sectionEnum;
355        break;
356      }
357      sectionEnum = sectionIt->nextElement();
358    }
359    delete sectionIt;
360  }
361  else
362    addSection = this->currentSection;
363
364  if (addSection == NULL)
365  {
366    PRINTF(2)("section not found for value %s\n", entryName);
367    return false;
368  }
369  else
370  {
371    IniEntry* newEntry = new IniEntry;
372    newEntry->name = new char[strlen (entryName)+1];
373    strcpy(newEntry->name, entryName);
374    newEntry->value = new char[strlen(value)+1];
375    strcpy(newEntry->value, value);
376    this->currentSection->entries->add(newEntry);
[5031]377    PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n", newEntry->name, newEntry->name, addSection->name);
[5020]378    return true;
379  }
380}
381
382/**
[4836]383 *  directly acesses an entry in a section
[5014]384 * @param entryName: the name of the entry to find
385 * @param sectionName: the section where the entry is to be found
386 * @param defaultValue: what should be returned in case the entry cannot be found
[4836]387 * @return a pointer to a buffer conatining the value of the specified entry. This buffer will contain the data specified in defvalue in case the entry wasn't found
[5020]388 *
389 *  The returned pointer points to an internal buffer, so do not free it on your own. Do not give a NULL pointer to defvalue, this will certainly
390 * lead to unwanted behaviour.
[2141]391*/
[5014]392const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const
[2065]393{
[5014]394  if (this->sections)
395  {
396    tIterator<IniSection>* sectionIt = this->sections->getIterator();
[5115]397    IniSection* sectionEnum = sectionIt->firstElement();
[5014]398    while (sectionEnum)
[3231]399    {
[5014]400      if (!strcmp(sectionEnum->name, sectionName))
401      {
402        tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
[5115]403        IniEntry* entryEnum = entryIt->firstElement();
[5014]404        while (entryEnum)
[4597]405        {
[5014]406          if (!strcmp(entryEnum->name, entryName))
407          {
408            delete entryIt;
409            delete sectionIt;
410            return entryEnum->value;
411          }
412          entryEnum = entryIt->nextElement();
[4597]413        }
[5014]414         delete entryIt;
415         PRINTF(2)("Entry %s in section %s not found.\n", entryName, sectionName);
416         break;
417      }
418      sectionEnum = sectionIt->nextElement();
[3231]419    }
[5014]420    delete sectionIt;
421    PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName);
422  }
423  else
[5031]424    PRINTF(1)("%s not opened\n", fileName);
[5014]425
426  return defaultValue;
427
[2065]428}
[5014]429
[5031]430
431void IniParser::setFileName(const char* fileName)
432{
433  if (this->fileName)
434    delete []this->fileName;
435  if (fileName)
436  {
437    this->fileName = new char[strlen(fileName)+1];
438    strcpy(this->fileName, fileName);
439  }
440  else
441    this->fileName = NULL;
442}
443
444
[5017]445/**
446 * output the whole tree in a nice and easy way.
447 */
[5014]448void IniParser::debug() const
449{
[5031]450  PRINTF(0)("Iniparser %s - debug\n", this->fileName);
[5014]451  if (this->sections)
452  {
453    tIterator<IniSection>* sectionIt = this->sections->getIterator();
[5115]454    IniSection* sectionEnum = sectionIt->firstElement();
[5014]455    while (sectionEnum)
456    {
457      PRINTF(0)(" [%s]\n", sectionEnum->name);
458
459      tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator();
[5115]460      IniEntry* entryEnum = entryIt->firstElement();
[5014]461      while (entryEnum)
462      {
463        PRINTF(0)("   :%s: -> '%s'\n", entryEnum->name, entryEnum->value);
464
465        entryEnum = entryIt->nextElement();
466      }
467      delete entryIt;
468
469      sectionEnum = sectionIt->nextElement();
470    }
471    delete sectionIt;
472  }
473  else
[5031]474    PRINTF(1)("%s not opened\n", fileName);
[5014]475}
Note: See TracBrowser for help on using the repository browser.