Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

File size: 4.6 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:
12   main-programmer: Christian Meyer
13   co-programmer: ...
14*/
15
16
17#include "ini_parser.h"
[4381]18
[4084]19#include "resource_manager.h"
[4381]20#include "debug.h"
[2064]21
22using namespace std;
23
[2141]24/**
[4084]25   \brief constructs an IniParser using a file
26   \param filename: the path and name of the file to parse
[2141]27*/
[4028]28IniParser::IniParser (const char* filename)
[2064]29{
[4597]30  this->setClassID(CL_INI_PARSER, "IniParser");
31
[3231]32  stream = NULL;
33  bInSection = false;
[4117]34  this->openFile(filename);
[2064]35}
36
[2141]37/**
[4084]38   \brief removes the IniParser from memory
[2141]39*/
[2064]40IniParser::~IniParser ()
41{
[3231]42  if( stream != NULL) fclose (stream);
[2064]43}
44
[2141]45/**
[3231]46   \brief opens another file to parse
47   \param filename: path and name of the new file to parse
48   \return zero on success or -1 if an error occured;
[2141]49*/
[4028]50int IniParser::openFile(const char* filename)
[2064]51{
[4084]52  char* tmpName = ResourceManager::homeDirCheck(filename);
[3231]53  if( filename == NULL) return -1;
[4597]54  if( stream != NULL)   fclose (stream);
[4084]55  if( (stream = fopen (tmpName, "r")) == NULL)
[3231]56    {
[4084]57      PRINTF(1)("IniParser could not open %s\n", filename);
58      delete tmpName;
[3231]59      return -1;
60    }
61  bInSection = false;
[4084]62  delete tmpName;
[3231]63  return 0;
[2064]64}
65
[2141]66/**
[3231]67   \brief set the parsing cursor to the specified section
68   \param section: the name of the section to set the cursor to
69   \return zero on success or -1 if the section could not be found
[2141]70*/
[3231]71int IniParser::getSection( char* section)
[2064]72{
[2551]73  bInSection = false;
74  if( stream == NULL) return -1;
[4597]75
[2551]76  char linebuffer[PARSELINELENGHT];
77  char secbuffer[PARSELINELENGHT];
78  char* ptr;
[4597]79
[2551]80  rewind (stream);
81  while( !feof( stream))
82    {
83      // get next line
84      fgets (linebuffer, PARSELINELENGHT, stream);
85      // remove newline char
86      if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0;
87      // check for section identifyer
88      if( sscanf (linebuffer, "[%s", secbuffer) == 1)
[4597]89        {
90          if( (ptr = strchr( secbuffer, ']')) != NULL)
91            {
92              *ptr = 0;
93              if( !strcmp( secbuffer, section))
94                {
95                  bInSection = true;
96                  return 0;
97                }
98            }
99        }
[2551]100    }
101  return -1;
[2064]102}
103
[2141]104/**
[3231]105   \brief gets the next VarName=VarValue pair from the parsing stream
106   \param name: a pointer to a buffer to store the name of the entry
107   \param value: a pointer to a buffer to store the value of the entry
108   \return zero if the buffers have been filled with data or -1 if there are no entries left in the current section
[2141]109*/
[3231]110int IniParser::nextVar( char* name, char* value)
[2064]111{
[3231]112  if( stream == NULL)
113    {
114      bInSection = false;
115      return -1;
116    }
117  if( !bInSection) return -1;
[4597]118
[3231]119  char linebuffer[PARSELINELENGHT];
120  char* ptr;
[4597]121
[3231]122  while( !feof( stream))
123    {
124      // get next line
125      fgets (linebuffer, PARSELINELENGHT, stream);
126      // remove newline char
127      if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0;
128      if( linebuffer[0] == '[')
[4597]129        {
130          bInSection = false;
131          return -1;
132        }
[4084]133      sscanf(linebuffer, "%s = %s", name, value);
134      return 0;
135      /*
[4597]136        if( (ptr = strchr( tmpBuffer, '=')) != NULL)
137        {
138        if( ptr == linebuffer) continue;
139        strcpy (value, &ptr[1]);
140        strncpy (name, linebuffer, strlen (linebuffer) - strlen (value) - 1);
141        printf ("%s, %s\n", value, name);
142        return 0;
143        }
[4084]144      */
[3231]145    }
[4597]146  return -1;
[2064]147}
148
[2141]149/**
[3231]150   \brief directly acesses an entry in a section
151   \param name: the name of the entry to find
152   \param section: the section where the entry is to be found
153   \param defvalue: what should be returned in case the entry cannot be found
154   \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
[4597]155
[3231]156   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
157   lead to unwanted behaviour.
[2141]158*/
[4028]159char* IniParser::getVar(const char* name, char* section, char* defvalue = "")
[2065]160{
[3231]161  strcpy (internbuf, defvalue);
162  if( getSection (section) == -1) return internbuf;
[4597]163
[3231]164  char namebuf[PARSELINELENGHT];
165  char valuebuf[PARSELINELENGHT];
[4597]166
[3231]167  while( nextVar (namebuf, valuebuf) != -1)
168    {
169      if( !strcmp (name, namebuf))
[4597]170        {
171          strcpy (internbuf, valuebuf);
172          return internbuf;
173        }
[3231]174    }
175  return internbuf;
[2065]176}
Note: See TracBrowser for help on using the repository browser.