Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/parser/ini_parser/ini_parser.cc @ 5946

Last change on this file since 5946 was 5946, checked in by bensch, 18 years ago

orxonox/trunk: adding comments (simple)

File size: 17.4 KB
Line 
1/*
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:
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.
18*/
19
20
21#include "ini_parser.h"
22
23#include <stdlib.h>
24#include <string.h>
25
26#if HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30#ifdef DEBUG
31 #include "../../../defs/debug.h"
32#else
33 #define PRINTF(x) printf
34#endif
35
36using namespace std;
37
38/**
39 * @brief constructs an IniParser using a file
40 * @param fileName: the path and name of the file to parse
41 */
42IniParser::IniParser (const char* fileName)
43{
44  this->fileName = NULL;
45  this->comment = NULL;
46
47  if (fileName != NULL)
48    this->readFile(fileName);
49}
50
51
52/**
53 * @brief removes the IniParser from memory
54 */
55IniParser::~IniParser ()
56{
57  this->deleteSections();
58}
59
60
61/**
62 * @brief removes all the sections. This is like delete, but even cooler :)
63 */
64void IniParser::deleteSections()
65{
66  // in all sections
67  while(!this->sections.empty())
68  {
69     IniSection section = this->sections.front();
70
71    // in all entries of the sections
72    while(!section.entries.empty())
73    {
74      // delete all strings of entries.
75      IniEntry entry = section.entries.front();
76      delete []entry.name;
77      delete []entry.value;
78      section.entries.pop_front();
79    }
80    // delete all Sections
81    delete []section.name;
82    this->sections.pop_front();
83  }
84  this->currentSection = this->sections.end();
85  this->setFileName(NULL);
86}
87
88
89/**
90 * @brief sets the Name of the input-file
91 * @param fileName The new FileName to set to the IniParser
92 * If fileName is NULL the new Name will be set to NULL too.
93 */
94void IniParser::setFileName(const char* fileName)
95{
96  if (this->fileName)
97    delete []this->fileName;
98  if (fileName != NULL)
99  {
100    this->fileName = new char[strlen(fileName)+1];
101    strcpy(this->fileName, fileName);
102  }
103  else
104    this->fileName = NULL;
105}
106
107
108/**
109 * @brief opens a file to parse
110 * @param fileName: path and name of the new file to parse
111 * @return true on success false otherwise;
112 *
113 * If there was already an opened file, the file will be closed,
114 * and the new one will be opened.
115 */
116bool IniParser::readFile(const char* fileName)
117{
118  FILE*    stream;           //< The stream we use to read the file.
119  int      lineCount = 0;    //< The Count of lines.
120
121
122  if (this->fileName != NULL)
123    this->deleteSections();
124  if( fileName == NULL)
125    return false;
126
127  if( (stream = fopen (fileName, "r")) == NULL)
128  {
129    PRINTF(1)("IniParser could not open %s\n", fileName);
130    return false;
131  }
132  else
133  {
134    this->setFileName(fileName);
135
136    /////////////////////////////
137    // READING IN THE INI-FILE //
138    /////////////////////////////
139    char lineBuffer[PARSELINELENGHT];
140    char buffer[PARSELINELENGHT];
141    const char* lineBegin;
142    char* ptr;
143
144    while( fgets (lineBuffer, PARSELINELENGHT, stream))
145    {
146      lineBegin = lineBuffer;
147      // remove newline char, and \0-terminate
148      if( (ptr = strchr( lineBuffer, '\n')) != NULL)
149        *ptr = 0;
150      // cut up to the beginning of the line.
151      while((*lineBegin == ' ' || *lineBegin == '\t') && lineBegin < lineBuffer + strlen(lineBuffer))
152        ++lineBegin;
153
154      // check if we have a FileComment
155      if ( (*lineBegin == '#' || *lineBegin == ';'))
156      {
157        printf("___________%d_____%s\n", lineCount,lineBegin);
158        char* newCommenLine = new char[strlen(lineBegin)+1];
159        strcpy(newCommenLine, lineBegin);
160        this->commentList.push_back(newCommenLine);
161        continue;
162      }
163      if (lineCount == 0 && !this->commentList.empty())
164      {
165        this->setFileComment();
166        lineCount++;
167      }
168
169      // check for section identifyer
170      else if( sscanf (lineBegin, "[%s", buffer) == 1)
171      {
172        if( (ptr = strchr( buffer, ']')) != NULL)
173        {
174          *ptr = 0;
175          this->addSection(buffer);
176          this->setSectionComment();
177        }
178      }
179      // check for Entry identifier (Entry = Value)
180      else if( (ptr = strchr( lineBegin, '=')) != NULL)
181      {
182        if (currentSection == NULL)
183        {
184          PRINTF(2)("Not in a Section yet for %s\n", lineBegin);
185          lineCount++;
186          continue;
187        }
188        if( ptr == lineBegin) {
189          lineCount++;
190          continue;
191        }
192        char* valueBegin = ptr+1;
193        while ((*valueBegin == ' ' || *valueBegin == '\t') && valueBegin <= lineBegin + strlen(lineBegin))
194          ++valueBegin;
195        char* valueEnd = valueBegin + strlen(valueBegin)-1;
196        while ((*valueEnd == ' ' || *valueEnd == '\t') && valueEnd >= valueBegin)
197          --valueEnd;
198        valueEnd[1] = '\0';
199        char* nameEnd = ptr-1;
200        while ((*nameEnd == ' ' || *nameEnd == '\t' ) && nameEnd >= lineBegin)
201          --nameEnd;
202        nameEnd[1] = '\0';
203
204        this->addVar(lineBegin, valueBegin);
205        this->setEntryComment();
206
207        lineCount++;
208      }
209    }
210  }
211  this->currentSection = this->sections.begin();
212  if (!this->sections.empty())
213    this->currentEntry = (*this->currentSection).entries.begin();
214
215  fclose(stream);
216  return true;
217}
218
219
220/**
221 * @brief opens a file and writes to it
222 * @param fileName: path and name of the new file to write to
223 * @return true on success false otherwise
224 */
225bool IniParser::writeFile(const char* fileName) const
226{
227  FILE*    stream;           //!< The stream we use to read the file.
228  if( fileName == NULL && (fileName = this->fileName) == NULL )
229    return false;
230
231  if( (stream = fopen (fileName, "w")) == NULL)
232  {
233    PRINTF(1)("IniParser could not open %s\n", fileName);
234    return false;
235  }
236  else
237  {
238    std::list<IniSection>::const_iterator section;
239    for (section = this->sections.begin(); section != this->sections.end(); section++)
240      {
241        fprintf(stream, "\n [%s]\n", (*section).name);
242
243        std::list<IniEntry>::const_iterator entry;
244        for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
245          fprintf(stream, "   %s = %s\n", (*entry).name, (*entry).value);
246      }
247  }
248  fclose(stream);
249}
250
251void IniParser::setFileComment(const char* fileComment)
252{
253  if (this->comment != NULL)
254    delete this->comment;
255
256  if (fileComment != NULL)
257  {
258    this->comment = new char[strlen(fileComment)+1];
259    strcpy(this->comment, fileComment);
260  }
261  else
262    this->comment = NULL;
263}
264
265
266/**
267 * @brief adds a section to the list of Sections,
268 * if no Section list is availiable, it will create it
269 * @param sectionName the Name of the section to add
270 * @return true on success... there is only success or segfault :)
271 */
272bool IniParser::addSection(const char* sectionName)
273{
274  this->sections.push_back(IniSection());
275  this->sections.back().comment = NULL;
276  this->sections.back().name = new char[strlen(sectionName)+1];
277  strcpy(this->sections.back().name, sectionName);
278
279  this->currentSection = --this->sections.end();
280  if (!this->sections.empty())
281      this->currentEntry = (*this->currentSection).entries.begin();
282  PRINTF(5)("Added Section %s\n", sectionName);
283  return true;
284}
285
286
287/**
288 * @brief Set the parsing cursor to the specified section
289 * @param sectionName: the name of the section to set the cursor to
290 * @return true on success or false if the section could not be found
291 */
292bool IniParser::getSection(const char* sectionName)
293{
294  std::list<IniSection>::iterator section;
295  for (section = this->sections.begin(); section != this->sections.end(); section++)
296    if (!strcmp((*section).name, sectionName))
297      {
298        this->currentSection = section;
299        this->currentEntry = (*this->currentSection).entries.begin();
300        return true;
301      }
302  return false;
303}
304
305
306void IniParser::setSectionComment(const char* comment, const char* sectionName)
307{
308
309}
310
311
312const char* IniParser::getSectionComment(const char* sectionName) const
313{
314
315}
316
317
318/**
319 * @brief moves to the first section
320 */
321void IniParser::firstSection()
322{
323  this->currentSection = this->sections.begin();
324  if (!this->sections.empty())
325    this->currentEntry = (*this->currentSection).entries.begin();
326}
327
328
329/**
330 * @brief searches the next section
331 * @returns the name of the section if found, NULL otherwise
332 */
333const char* IniParser::nextSection()
334{
335  if (this->currentSection == this->sections.end())
336    return NULL;
337
338  this->currentSection++;
339
340  if (this->currentSection != this->sections.end())
341    {
342      this->currentEntry = (*this->currentSection).entries.begin();
343      return this->currentSection->name;
344    }
345  else
346    return NULL;
347}
348
349
350/**
351 * @brief adds a new Entry to either the currentSection or the section called by sectionName
352 * @param entryName the Name of the Entry to add
353 * @param value the value to assign to this entry
354 * @param sectionName if NULL then this entry will be set to the currentSection
355 * otherwise to the section refered to by sectionName.
356 * If both are NULL no entry will be added
357 * @return true if everything is ok false on error
358 */
359bool IniParser::addVar(const char* entryName, const char* value, const char* sectionName)
360{
361  std::list<IniSection>::iterator section;
362
363  if (sectionName != NULL)
364  {
365    for (section = this->sections.begin(); section != this->sections.end(); section++)
366      if (!strcmp((*section).name, sectionName))
367        break;
368  }
369  else
370    section = this->currentSection;
371
372  if (section == this->sections.end())
373    return false;
374
375  if (section == this->sections.end())
376  {
377    PRINTF(2)("section '%s' not found for value '%s'\n", sectionName, entryName);
378    return false;
379  }
380  else
381  {
382    (*section).entries.push_back(IniEntry());
383    (*section).entries.back().comment = NULL;
384    (*section).entries.back().name = new char[strlen(entryName)+1];
385    strcpy((*section).entries.back().name, entryName);
386    (*section).entries.back().value = new char[strlen(value)+1];
387    strcpy((*section).entries.back().value, value);
388    PRINTF(5)("Added Entry %s with Value '%s' to Section %s\n",
389              (*section).entries.back().name,
390              (*section).entries.back().value,
391              (*section).name);
392    this->currentEntry = --(*section).entries.end();
393    return true;
394  }
395}
396
397
398/**
399 * @brief directly acesses an entry in a section
400 * @param entryName: the name of the entry to find
401 * @param sectionName: the section where the entry is to be found
402 * @param defaultValue: what should be returned in case the entry cannot be found
403 * @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
404 *
405 *  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
406 * lead to unwanted behaviour.
407*/
408const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const
409{
410  if (this->fileName != NULL)
411  {
412    std::list<IniSection>::const_iterator section = this->getSectionIT(sectionName);
413
414  if (section == this->sections.end())
415    {
416      PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName);
417      return (defaultValue);
418    }
419
420    std::list<IniEntry>::const_iterator entry;
421    for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
422      if (!strcmp((*entry).name, entryName))
423        return (*entry).value;
424    PRINTF(2)("Entry '%s' in section '%s' not found.\n", entryName, sectionName);
425
426  }
427  else
428    PRINTF(2)("%s not opened\n", fileName);
429
430  return defaultValue;
431
432}
433
434
435const char* IniParser::setEntryComment(const char* comment, const char* entryName, const char* sectionName)
436{
437
438
439}
440
441
442const char* IniParser::getEntryComment(const char* entryName, const char* sectionName) const
443{
444
445
446}
447
448
449/**
450 * @brief moves to the first Variable of the current Section
451 */
452void IniParser::firstVar()
453{
454  if (!this->sections.empty() &&
455       this->currentSection != this->sections.end())
456    this->currentEntry = (*this->currentSection).entries.begin();
457}
458
459
460/**
461 * @brief gets the next VarName = VarValue pair from the parsing stream
462 * @return true on success, false otherwise (in the latter case name and value will be NULL)
463 */
464bool IniParser::nextVar()
465{
466  if ( this->sections.empty()
467       || this->currentSection == this->sections.end()
468       || this->currentEntry == (*this->currentSection).entries.end())
469    return false;
470
471  this->currentEntry++;
472
473  if (this->currentEntry == (*this->currentSection).entries.end())
474    return false;
475  else
476    return true;
477}
478
479
480
481/**
482 * @returns the name of the Current selected Section
483 */
484const char* IniParser::getCurrentSection() const
485{
486  if (!this->sections.empty() &&
487      this->currentSection != this->sections.end())
488    return this->currentSection->name;
489  else
490    return NULL;
491 }
492
493
494/**
495 * @returns the current entries Name, or NULL if we havn't selected a Entry
496 */
497const char* IniParser::getCurrentName() const
498{
499 if (!this->sections.empty() &&
500     this->currentSection != this->sections.end() &&
501     this->currentEntry != (*this->currentSection).entries.end())
502   return (*this->currentEntry).name;
503 else
504   return NULL;
505}
506
507/**
508 * @returns the current entries Value, or NULL if we havn't selected a Entry
509 */
510const char* IniParser::getCurrentValue() const
511{
512  if (!this->sections.empty() &&
513      this->currentSection != this->sections.end() &&
514      this->currentEntry != (*this->currentSection).entries.end())
515    return (*this->currentEntry).value;
516  else
517    return NULL;
518}
519
520
521std::list<IniParser::IniSection>::const_iterator IniParser::getSectionIT(const char* sectionName) const
522{
523  std::list<IniSection>::const_iterator section;
524  if (sectionName != NULL)
525  {
526    for (section = this->sections.begin(); section != this->sections.end(); section++)
527    {
528      if (!strcmp((*section).name, sectionName))
529      {
530        break;
531      }
532    }
533  }
534  else
535    section = this->currentSection;
536
537  return section;
538}
539
540
541std::list<IniParser::IniEntry>::const_iterator IniParser::getEntryIT(const char* entryName, const char* sectionName) const
542{
543  if (entryName == NULL)
544    return this->currentEntry;
545
546}
547
548
549
550void IniParser::setFileComment()
551{
552  if (this->comment != NULL)
553    delete[] this->comment;
554
555  if (this->commentList.empty()) {
556    this->comment = NULL;
557    return;
558  }
559
560  unsigned int stringLength = 1;
561  std::list<char*>::iterator comment;
562  for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++)
563    stringLength += strlen((*comment)) +1;
564
565  this->comment = new char[stringLength];
566  this->comment[0] = '\0';
567  while (!this->commentList.empty())
568  {
569    if (*this->comment != '\0')
570      strcat(this->comment, "\n");
571    strcat(this->comment, this->commentList.front());
572    delete[] this->commentList.front();
573    this->commentList.pop_front();
574    printf("this->comment:: %s\n", this->comment);
575  }
576}
577
578void IniParser::setSectionComment()
579{
580  if ((*this->currentSection).comment != NULL)
581    delete[] (*this->currentSection).comment;
582
583  if (this->commentList.empty()) {
584    (*this->currentSection).comment = NULL;
585    return;
586  }
587
588  unsigned int stringLength = 1;
589  std::list<char*>::iterator comment;
590  for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++)
591    stringLength += strlen((*comment)) +1;
592
593  (*this->currentSection).comment = new char[stringLength];
594  (*this->currentSection).comment[0] = '\0';
595  while (!this->commentList.empty())
596  {
597    if (*(*this->currentSection).comment != '\0')
598      strcat((*this->currentSection).comment, "\n");
599    strcat((*this->currentSection).comment, this->commentList.front());
600    delete[] this->commentList.front();
601    this->commentList.pop_front();
602    printf("this->comment:: %s", (*this->currentSection).comment);
603  }
604}
605
606void IniParser::setEntryComment()
607{
608  if ((*this->currentEntry).comment != NULL)
609    delete[] (*this->currentEntry).comment;
610
611  if (this->commentList.empty()) {
612    (*this->currentEntry).comment = NULL;
613    return;
614  }
615
616  unsigned int stringLength = 1;
617  std::list<char*>::iterator comment;
618  for (comment = this->commentList.begin(); comment != this->commentList.end(); comment++)
619    stringLength += strlen((*comment)) +1;
620
621  (*this->currentEntry).comment = new char[stringLength];
622  (*this->currentEntry).comment[0] = '\0';
623  while (!this->commentList.empty())
624  {
625    if (*(*this->currentEntry).comment != '\0')
626      strcat((*this->currentEntry).comment, "\n");
627    strcat((*this->currentEntry).comment, this->commentList.front());
628    delete[] this->commentList.front();
629    this->commentList.pop_front();
630    printf("=======this->comment:: %s\n", (*this->currentEntry).comment);
631  }
632
633}
634
635
636/**
637 * @brief output the whole tree in a nice and easy way.
638 */
639void IniParser::debug() const
640{
641  PRINTF(0)("Iniparser %s - debug\n", this->fileName);
642  if (this->comment != NULL)
643    PRINTF(0)("FileComment:\n %s\n\n", this->comment);
644
645  if (this->fileName != NULL)
646  {
647    std::list<IniSection>::const_iterator section;
648    for (section = this->sections.begin(); section != this->sections.end(); section++)
649    {
650      if ((*section).comment != NULL)
651        PRINTF(0)(" %s\n", (*section).comment);
652      PRINTF(0)(" [%s]\n", (*section).name);
653
654      std::list<IniEntry>::const_iterator entry;
655      for (entry = (*section).entries.begin(); entry != (*section).entries.end(); entry++)
656      {
657        if ((*entry).comment != NULL)
658          PRINTF(0)(" %s\n", (*entry).comment);
659        PRINTF(0)("   '%s' -> '%s'\n", (*entry).name, (*entry).value);
660      }
661    }
662  }
663  else
664    PRINTF(1)("no opened ini-file.\n");
665}
666
Note: See TracBrowser for help on using the repository browser.