Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4152 was 4117, checked in by bensch, 20 years ago

orxonox/trunk: model should load again on Windows

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