Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/chris/src/ini_parser.cc @ 2624

Last change on this file since 2624 was 2141, checked in by chris, 20 years ago

orxonox/branches/chris: added lots and lots of doxygen tags

File size: 3.9 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{
27        stream = NULL;
28        bInSection = false;
[2066]29        open_file (filename);
[2064]30}
31
[2141]32/**
33        \brief removes the IniParser from memory
34*/
[2064]35IniParser::~IniParser ()
36{
37        if( stream != NULL) fclose (stream);
38}
39
[2141]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*/
[2066]45int IniParser::open_file( char* filename)
[2064]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
[2141]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*/
[2066]63int IniParser::get_section( char* section)
[2064]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                {
[2065]82                        if( (ptr = strchr( secbuffer, ']')) != NULL)
[2064]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
[2141]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*/
[2066]102int IniParser::next_var( char* name, char* value)
[2064]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
[2141]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*/
[2066]146char* IniParser::get_var( char* name, char* section, char* defvalue = "")
[2065]147{
148        strcpy (internbuf, defvalue);
[2066]149        if( get_section (section) == -1) return internbuf;
[2065]150       
151        char namebuf[PARSELINELENGHT];
152        char valuebuf[PARSELINELENGHT];
153       
[2066]154        while( next_var (namebuf, valuebuf) != -1)
[2065]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.