[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" |
---|
[4381] | 18 | |
---|
[4084] | 19 | #include "resource_manager.h" |
---|
[4381] | 20 | #include "debug.h" |
---|
[2064] | 21 | |
---|
| 22 | using namespace std; |
---|
| 23 | |
---|
[2141] | 24 | /** |
---|
[4084] | 25 | \brief constructs an IniParser using a file |
---|
| 26 | \param filename: the path and name of the file to parse |
---|
[2141] | 27 | */ |
---|
[4028] | 28 | IniParser::IniParser (const char* filename) |
---|
[2064] | 29 | { |
---|
[3231] | 30 | stream = NULL; |
---|
| 31 | bInSection = false; |
---|
[4117] | 32 | this->openFile(filename); |
---|
[2064] | 33 | } |
---|
| 34 | |
---|
[2141] | 35 | /** |
---|
[4084] | 36 | \brief removes the IniParser from memory |
---|
[2141] | 37 | */ |
---|
[2064] | 38 | IniParser::~IniParser () |
---|
| 39 | { |
---|
[3231] | 40 | if( stream != NULL) fclose (stream); |
---|
[2064] | 41 | } |
---|
| 42 | |
---|
[2141] | 43 | /** |
---|
[3231] | 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; |
---|
[2141] | 47 | */ |
---|
[4028] | 48 | int IniParser::openFile(const char* filename) |
---|
[2064] | 49 | { |
---|
[4084] | 50 | char* tmpName = ResourceManager::homeDirCheck(filename); |
---|
[3231] | 51 | if( filename == NULL) return -1; |
---|
| 52 | if( stream != NULL) fclose (stream); |
---|
[4084] | 53 | if( (stream = fopen (tmpName, "r")) == NULL) |
---|
[3231] | 54 | { |
---|
[4084] | 55 | PRINTF(1)("IniParser could not open %s\n", filename); |
---|
| 56 | delete tmpName; |
---|
[3231] | 57 | return -1; |
---|
| 58 | } |
---|
| 59 | bInSection = false; |
---|
[4084] | 60 | delete tmpName; |
---|
[3231] | 61 | return 0; |
---|
[2064] | 62 | } |
---|
| 63 | |
---|
[2141] | 64 | /** |
---|
[3231] | 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 |
---|
[2141] | 68 | */ |
---|
[3231] | 69 | int IniParser::getSection( char* section) |
---|
[2064] | 70 | { |
---|
[2551] | 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) |
---|
[2064] | 87 | { |
---|
[2551] | 88 | if( (ptr = strchr( secbuffer, ']')) != NULL) |
---|
| 89 | { |
---|
| 90 | *ptr = 0; |
---|
| 91 | if( !strcmp( secbuffer, section)) |
---|
[2064] | 92 | { |
---|
[2551] | 93 | bInSection = true; |
---|
| 94 | return 0; |
---|
[2064] | 95 | } |
---|
[2551] | 96 | } |
---|
[2064] | 97 | } |
---|
[2551] | 98 | } |
---|
| 99 | return -1; |
---|
[2064] | 100 | } |
---|
| 101 | |
---|
[2141] | 102 | /** |
---|
[3231] | 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 |
---|
[2141] | 107 | */ |
---|
[3231] | 108 | int IniParser::nextVar( char* name, char* value) |
---|
[2064] | 109 | { |
---|
[3231] | 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] == '[') |
---|
[2064] | 127 | { |
---|
[3231] | 128 | bInSection = false; |
---|
| 129 | return -1; |
---|
[2064] | 130 | } |
---|
[4084] | 131 | sscanf(linebuffer, "%s = %s", name, value); |
---|
| 132 | return 0; |
---|
| 133 | /* |
---|
| 134 | if( (ptr = strchr( tmpBuffer, '=')) != NULL) |
---|
[2064] | 135 | { |
---|
[4084] | 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; |
---|
[2064] | 141 | } |
---|
[4084] | 142 | */ |
---|
[3231] | 143 | } |
---|
| 144 | return -1; |
---|
[2064] | 145 | } |
---|
| 146 | |
---|
[2141] | 147 | /** |
---|
[3231] | 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. |
---|
[2141] | 156 | */ |
---|
[4028] | 157 | char* IniParser::getVar(const char* name, char* section, char* defvalue = "") |
---|
[2065] | 158 | { |
---|
[3231] | 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)) |
---|
[2065] | 168 | { |
---|
[3231] | 169 | strcpy (internbuf, valuebuf); |
---|
| 170 | return internbuf; |
---|
[2065] | 171 | } |
---|
[3231] | 172 | } |
---|
| 173 | return internbuf; |
---|
[2065] | 174 | } |
---|