Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4569 was 4381, checked in by bensch, 20 years ago

orxonox/trunk: made include more local. stdincl.h not in base_object.h anymore

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