Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/nico/src/ini_parser.cc @ 3542

Last change on this file since 3542 was 3238, checked in by bensch, 20 years ago

orxonox/branches: updated branches: buerli, nico, sound. And moved bezierTrack to old.bezierTrack. Conflicts resolved in a usefull order.
Conflics mostly resolved in favor of trunk
merge.

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