Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: setClassID implemented in all files

File size: 4.6 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: Christian Meyer
13   co-programmer: ...
14*/
15
16
17#include "ini_parser.h"
18
19#include "resource_manager.h"
20#include "debug.h"
21
22using namespace std;
23
24/**
25   \brief constructs an IniParser using a file
26   \param filename: the path and name of the file to parse
27*/
28IniParser::IniParser (const char* filename)
29{
30  this->setClassID(CL_INI_PARSER, "IniParser");
31
32  stream = NULL;
33  bInSection = false;
34  this->openFile(filename);
35}
36
37/**
38   \brief removes the IniParser from memory
39*/
40IniParser::~IniParser ()
41{
42  if( stream != NULL) fclose (stream);
43}
44
45/**
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;
49*/
50int IniParser::openFile(const char* filename)
51{
52  char* tmpName = ResourceManager::homeDirCheck(filename);
53  if( filename == NULL) return -1;
54  if( stream != NULL)   fclose (stream);
55  if( (stream = fopen (tmpName, "r")) == NULL)
56    {
57      PRINTF(1)("IniParser could not open %s\n", filename);
58      delete tmpName;
59      return -1;
60    }
61  bInSection = false;
62  delete tmpName;
63  return 0;
64}
65
66/**
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
70*/
71int IniParser::getSection( char* section)
72{
73  bInSection = false;
74  if( stream == NULL) return -1;
75
76  char linebuffer[PARSELINELENGHT];
77  char secbuffer[PARSELINELENGHT];
78  char* ptr;
79
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)
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        }
100    }
101  return -1;
102}
103
104/**
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
109*/
110int IniParser::nextVar( char* name, char* value)
111{
112  if( stream == NULL)
113    {
114      bInSection = false;
115      return -1;
116    }
117  if( !bInSection) return -1;
118
119  char linebuffer[PARSELINELENGHT];
120  char* ptr;
121
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] == '[')
129        {
130          bInSection = false;
131          return -1;
132        }
133      sscanf(linebuffer, "%s = %s", name, value);
134      return 0;
135      /*
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        }
144      */
145    }
146  return -1;
147}
148
149/**
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
155
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.
158*/
159char* IniParser::getVar(const char* name, char* section, char* defvalue = "")
160{
161  strcpy (internbuf, defvalue);
162  if( getSection (section) == -1) return internbuf;
163
164  char namebuf[PARSELINELENGHT];
165  char valuebuf[PARSELINELENGHT];
166
167  while( nextVar (namebuf, valuebuf) != -1)
168    {
169      if( !strcmp (name, namebuf))
170        {
171          strcpy (internbuf, valuebuf);
172          return internbuf;
173        }
174    }
175  return internbuf;
176}
Note: See TracBrowser for help on using the repository browser.