Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/sound/src/ini_parser.cc @ 2974

Last change on this file since 2974 was 2551, checked in by patrick, 20 years ago

orxonox/trunk: minor changes - enhanced sc controll, fixed uncontrolled rotation effect, added some debug outputs for testing purposes, reformatted some src files from win style but not all

File size: 3.9 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
19using namespace std;
20
21/**
22        \brief constructs an IniParser using a file
23        \param filename: the path and name of the file to parse
24*/
25IniParser::IniParser (char* filename)
26{
27        stream = NULL;
28        bInSection = false;
29        open_file (filename);
30}
31
32/**
33        \brief removes the IniParser from memory
34*/
35IniParser::~IniParser ()
36{
37        if( stream != NULL) fclose (stream);
38}
39
40/**
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;
44*/
45int IniParser::open_file( char* filename)
46{
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;
56}
57
58/**
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
62*/
63int IniParser::get_section( char* section)
64{
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)
81        {
82          if( (ptr = strchr( secbuffer, ']')) != NULL)
83            {
84              *ptr = 0;
85              if( !strcmp( secbuffer, section))
86                {
87                  bInSection = true;
88                  return 0;
89                }
90            }
91        }
92    }
93  return -1;
94}
95
96/**
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
101*/
102int IniParser::next_var( char* name, char* value)
103{
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] == '[')
121                {
122                        bInSection = false;
123                        return -1;
124                }
125                if( (ptr = strchr( linebuffer, '=')) != NULL)
126                {
127                        if( ptr == linebuffer) continue;
128                        strcpy (value, &ptr[1]);
129                        strncpy (name, linebuffer, strlen (linebuffer) - strlen (value) - 1);
130                        return 0;
131                }
132        }
133        return -1;     
134}
135
136/**
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.
145*/
146char* IniParser::get_var( char* name, char* section, char* defvalue = "")
147{
148        strcpy (internbuf, defvalue);
149        if( get_section (section) == -1) return internbuf;
150       
151        char namebuf[PARSELINELENGHT];
152        char valuebuf[PARSELINELENGHT];
153       
154        while( next_var (namebuf, valuebuf) != -1)
155        {
156                if( !strcmp (name, namebuf))
157                {
158                        strcpy (internbuf, valuebuf);
159                        return internbuf;
160                }
161        }
162        return internbuf;
163}
Note: See TracBrowser for help on using the repository browser.