Changeset 5014 in orxonox.OLD for orxonox/trunk/src/lib/util
- Timestamp:
- Aug 14, 2005, 4:29:29 PM (20 years ago)
- Location:
- orxonox/trunk/src/lib/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/trunk/src/lib/util/ini_parser.cc
r4836 r5014 10 10 11 11 ### File Specific: 12 main-programmer: Christian Meyer 13 co-programmer: ... 12 main-programmer: Benjamin Grauer 13 co-programmer: Christian Meyer 14 15 2005-08-14: complete reimplementation: 16 now the File is parsed at the initialisation, 17 and informations is gathered there. 14 18 */ 15 19 16 20 17 21 #include "ini_parser.h" 22 23 #include "list.h" 24 #include "stdlibincl.h" 18 25 19 26 #include "resource_manager.h" … … 26 33 * @param filename: the path and name of the file to parse 27 34 */ 28 IniParser::IniParser (const char* file name)35 IniParser::IniParser (const char* fileName) 29 36 { 30 37 this->setClassID(CL_INI_PARSER, "IniParser"); 31 32 stream = NULL; 33 bInSection = false; 34 this->openFile(filename); 38 this->setName(fileName); 39 40 this->currentEntry = NULL; 41 this->currentSection = NULL; 42 this->sections = NULL; 43 if (fileName != NULL) 44 this->openFile(fileName); 35 45 } 36 46 … … 40 50 IniParser::~IniParser () 41 51 { 42 if( stream != NULL) fclose (stream); 43 } 44 45 /** 46 * opens another file to parse 52 deleteSections(); 53 } 54 55 void IniParser::deleteSections() 56 { 57 if (this->sections) 58 { 59 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 60 IniSection* sectionEnum = sectionIt->nextElement(); 61 while (sectionEnum) 62 { 63 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 64 IniEntry* entryEnum = entryIt->nextElement(); 65 while (entryEnum) 66 { 67 delete []entryEnum->name; 68 delete []entryEnum->value; 69 delete entryEnum; 70 entryEnum = entryIt->nextElement(); 71 } 72 delete entryIt; 73 74 delete []sectionEnum->name; 75 delete sectionEnum->entries; 76 delete sectionEnum; 77 sectionEnum = sectionIt->nextElement(); 78 } 79 delete sectionIt; 80 } 81 delete this->sections; 82 this->sections = NULL; 83 } 84 85 /** 86 * opens another file to parse 47 87 * @param filename: path and name of the new file to parse 48 * @return zero on success or -1 if an error occured; 49 */ 50 int IniParser::openFile(const char* filename) 51 { 88 * @return true on success false otherwise; 89 */ 90 bool IniParser::openFile(const char* filename) 91 { 92 FILE* stream; //!< The stream we use to read the file. 93 94 95 if( filename == NULL) 96 return false; 52 97 char* tmpName = ResourceManager::homeDirCheck(filename); 53 if( filename == NULL) return -1; 54 if( stream != NULL) fclose (stream); 98 99 if (sections != NULL) 100 deleteSections(); 101 55 102 if( (stream = fopen (tmpName, "r")) == NULL) 56 { 57 PRINTF(1)("IniParser could not open %s\n", filename); 58 delete tmpName; 59 return -1; 60 } 61 bInSection = false; 62 delete tmpName; 63 return 0; 103 { 104 PRINTF(1)("IniParser could not open %s\n", filename); 105 delete tmpName; 106 return false; 107 } 108 else 109 { 110 this->currentEntry = NULL; 111 this->currentSection = NULL; 112 this->sections = new tList<IniSection>; 113 114 ///////////////////////////// 115 // READING IN THE INI-FILE // 116 ///////////////////////////// 117 char lineBuffer[PARSELINELENGHT]; 118 char buffer[PARSELINELENGHT]; 119 char* ptr; 120 121 rewind (stream); 122 while( !feof( stream)) 123 { 124 // get next line 125 fgets (lineBuffer, PARSELINELENGHT, stream); 126 // remove newline char, and \0-terminate 127 if( (ptr = strchr( lineBuffer, '\n')) != NULL) 128 *ptr = 0; 129 // check for section identifyer 130 if( sscanf (lineBuffer, "[%s", buffer) == 1) 131 { 132 if( (ptr = strchr( buffer, ']')) != NULL) 133 { 134 *ptr = 0; 135 IniSection* newSection = new IniSection; 136 newSection->name = new char[strlen(buffer)+1]; 137 strcpy(newSection->name, buffer); 138 newSection->entries = new tList<IniEntry>; 139 this->sections->add(newSection); 140 this->currentSection = newSection; 141 } 142 } 143 else if( (ptr = strchr( lineBuffer, '=')) != NULL) 144 { 145 if (currentSection == NULL) 146 { 147 PRINTF(2)("Not in a Section yet for %s\n", lineBuffer); 148 continue; 149 } 150 if( ptr == lineBuffer) 151 continue; 152 IniEntry* newEntry = new IniEntry; 153 154 char* valueBegin = ptr+1; 155 while ((*valueBegin == ' ' || *valueBegin == '\t') && valueBegin <= lineBuffer + strlen(lineBuffer)) 156 ++valueBegin; 157 newEntry->value = new char[strlen(valueBegin)+1]; 158 strcpy(newEntry->value, valueBegin); 159 char* nameEnd = ptr-1, *nameBegin = lineBuffer; 160 while ((*nameBegin == ' ' || *nameBegin == '\t') && nameBegin < ptr) 161 ++nameBegin; 162 while ((*nameEnd == ' ' || *nameEnd == '\t' ) && nameEnd >= nameBegin) 163 --nameEnd; 164 nameEnd[1] = '\0'; 165 newEntry->name = new char[strlen (nameBegin)]; 166 strcpy(newEntry->name, nameBegin); 167 168 this->currentSection->entries->add(newEntry); 169 } 170 171 } 172 } 173 fclose(stream); 174 return true; 64 175 } 65 176 66 177 /** 67 178 * set the parsing cursor to the specified section 68 * @param section: the name of the section to set the cursor to 69 * @return zero on success or -1 if the section could not be found 70 */ 71 int IniParser::getSection( const char* section) 72 { 73 bInSection = false; 74 if( stream == NULL) return -1; 75 76 char linebuffer[PARSELINELENGHT]; 77 char secbuffer[PARSELINELENGHT]; 78 char* ptr; 79 80 rewind (stream); 81 while( !feof( stream)) 82 { 83 // get next line 84 fgets (linebuffer, PARSELINELENGHT, stream); 85 // remove newline char 86 if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0; 87 // check for section identifyer 88 if( sscanf (linebuffer, "[%s", secbuffer) == 1) 89 { 90 if( (ptr = strchr( secbuffer, ']')) != NULL) 91 { 92 *ptr = 0; 93 if( !strcmp( secbuffer, section)) 94 { 95 bInSection = true; 96 return 0; 97 } 98 } 99 } 100 } 101 return -1; 179 * @param sectionName: the name of the section to set the cursor to 180 * @return true on success or false if the section could not be found 181 */ 182 bool IniParser::getSection( const char* sectionName) 183 { 184 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 185 IniSection* sectionEnum = sectionIt->nextElement(); 186 while (sectionEnum) 187 { 188 if (!strcmp(sectionEnum->name, sectionName)) 189 { 190 this->currentSection = sectionEnum; 191 delete sectionIt; 192 return true; 193 } 194 195 sectionEnum = sectionIt->nextElement(); 196 } 197 delete sectionIt; 198 return false; 199 } 200 201 /** 202 * searches the next section 203 * @returns the name of the section if found, NULL otherwise 204 */ 205 const char* IniParser::nextSection() 206 { 207 if (this->currentSection == NULL) 208 return NULL; 209 else 210 { 211 if (this->sections) 212 this->currentSection = sections->nextElement(this->currentSection); 213 } 214 if (this->currentSection != NULL) 215 return this->currentSection->name; 216 else 217 return NULL; 102 218 } 103 219 104 220 /** 105 221 * gets the next VarName=VarValue pair from the parsing stream 106 * @param name: a pointer to a buffer to store the name of the entry 107 * @param value: a pointer to a buffer to store the value of the entry 108 * @return zero if the buffers have been filled with data or -1 if there are no entries left in the current section 109 */ 110 int IniParser::nextVar( const char* name, const char* value) 111 { 112 if( stream == NULL) 113 { 114 bInSection = false; 115 return -1; 116 } 117 if( !bInSection) return -1; 118 119 char linebuffer[PARSELINELENGHT]; 120 char* ptr; 121 122 while( !feof( stream)) 123 { 124 // get next line 125 fgets (linebuffer, PARSELINELENGHT, stream); 126 // remove newline char 127 if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0; 128 if( linebuffer[0] == '[') 129 { 130 bInSection = false; 131 return -1; 132 } 133 sscanf(linebuffer, "%s = %s", name, value); 134 return 0; 135 /* 136 if( (ptr = strchr( tmpBuffer, '=')) != NULL) 137 { 138 if( ptr == linebuffer) continue; 139 strcpy (value, &ptr[1]); 140 strncpy (name, linebuffer, strlen (linebuffer) - strlen (value) - 1); 141 printf ("%s, %s\n", value, name); 142 return 0; 143 } 144 */ 145 } 146 return -1; 222 * @param name: a pointer to the Name of the next Var 223 * @param value: a pointer to the Value of the next Var 224 * @return true on success, false otherwise (in the latter case name and value will be NULL) 225 */ 226 bool IniParser::nextVar() 227 { 228 if (this->currentSection == NULL) 229 return false; 230 if(this->currentEntry == this->currentSection->entries->lastElement()) 231 { 232 this->currentEntry = NULL; 233 return false; 234 } 235 if (this->currentEntry == NULL) 236 this->currentEntry = this->currentSection->entries->firstElement(); 237 else 238 this->currentEntry = this->currentSection->entries->nextElement(this->currentEntry); 239 240 if (this->currentEntry == NULL) 241 return false; 242 else 243 return true; 147 244 } 148 245 149 246 /** 150 247 * directly acesses an entry in a section 151 * @param name: the name of the entry to find152 * @param section : the section where the entry is to be found153 * @param def value: what should be returned in case the entry cannot be found248 * @param entryName: the name of the entry to find 249 * @param sectionName: the section where the entry is to be found 250 * @param defaultValue: what should be returned in case the entry cannot be found 154 251 * @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 155 252 … … 157 254 lead to unwanted behaviour. 158 255 */ 159 const char* IniParser::getVar(const char* name, const char* section, const char* defvalue = "") 160 { 161 strcpy (internbuf, defvalue); 162 if( getSection (section) == -1) return internbuf; 163 164 char namebuf[PARSELINELENGHT]; 165 char valuebuf[PARSELINELENGHT]; 166 167 while( nextVar (namebuf, valuebuf) != -1) 168 { 169 if( !strcmp (name, namebuf)) 256 const char* IniParser::getVar(const char* entryName, const char* sectionName, const char* defaultValue) const 257 { 258 if (this->sections) 259 { 260 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 261 IniSection* sectionEnum = sectionIt->nextElement(); 262 while (sectionEnum) 263 { 264 if (!strcmp(sectionEnum->name, sectionName)) 265 { 266 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 267 IniEntry* entryEnum = entryIt->nextElement(); 268 while (entryEnum) 170 269 { 171 strcpy (internbuf, valuebuf); 172 return internbuf; 270 if (!strcmp(entryEnum->name, entryName)) 271 { 272 delete entryIt; 273 delete sectionIt; 274 return entryEnum->value; 275 } 276 entryEnum = entryIt->nextElement(); 173 277 } 174 } 175 return internbuf; 176 } 278 delete entryIt; 279 PRINTF(2)("Entry %s in section %s not found.\n", entryName, sectionName); 280 break; 281 } 282 sectionEnum = sectionIt->nextElement(); 283 } 284 delete sectionIt; 285 PRINTF(2)("Section %s that should be containing %s not found.\n", sectionName, entryName); 286 } 287 else 288 PRINTF(1)("%s not opened\n", this->getName()); 289 290 return defaultValue; 291 292 } 293 294 void IniParser::debug() const 295 { 296 PRINTF(0)("Iniparser %s - debug\n", this->getName()); 297 if (this->sections) 298 { 299 tIterator<IniSection>* sectionIt = this->sections->getIterator(); 300 IniSection* sectionEnum = sectionIt->nextElement(); 301 while (sectionEnum) 302 { 303 PRINTF(0)(" [%s]\n", sectionEnum->name); 304 305 tIterator<IniEntry>* entryIt = sectionEnum->entries->getIterator(); 306 IniEntry* entryEnum = entryIt->nextElement(); 307 while (entryEnum) 308 { 309 PRINTF(0)(" :%s: -> '%s'\n", entryEnum->name, entryEnum->value); 310 311 entryEnum = entryIt->nextElement(); 312 } 313 delete entryIt; 314 315 sectionEnum = sectionIt->nextElement(); 316 } 317 delete sectionIt; 318 } 319 else 320 PRINTF(1)("%s not opened\n", this->getName()); 321 } -
orxonox/trunk/src/lib/util/ini_parser.h
r4836 r5014 1 1 /*! 2 \file ini_parser.h3 *A small ini file parser4 5 Can be used to find a defined [Section] in an ini file and get the VarName=Value entries6 */2 * @file ini_parser.h 3 * A small ini file parser 4 * 5 * Can be used to find a defined [Section] in an ini file and get the VarName = Value entries 6 */ 7 7 8 8 #ifndef _INI_PARSER_H 9 9 #define _INI_PARSER_H 10 10 11 #include <stdio.h> 12 #include <string.h> 13 #include <stdlib.h> 11 #define PARSELINELENGHT 512 //!< how many chars to read at once 12 14 13 #include "base_object.h" 15 14 16 #define PARSELINELENGHT 512 //!< how many chars to read at once 15 // FORWARD DEFINITION // 16 template<class T> class tList; 17 17 18 18 //! ini-file parser 19 19 /** 20 21 */20 * This class can be used to load an initializer file and parse it's contents for variablename=value pairs. 21 */ 22 22 class IniParser : public BaseObject 23 23 { 24 public: 25 IniParser (const char* filename); 26 ~IniParser (); 24 private: 25 //////////////////////////////////// 26 struct IniEntry 27 { 28 char* name; 29 char* value; 30 }; 27 31 28 const char* getVar(const char* name, const char* section, const char* defvalue); 29 int openFile(const char* name); 30 int getSection( const char* section); 31 int nextVar( const char* name, const char* value); 32 struct IniSection 33 { 34 char* name; 35 tList<IniEntry>* entries; 36 }; 37 //////////////////////////////////// 32 38 33 private: 34 FILE* stream; //!< A FileStream to be loaded 35 bool bInSection; //!< if the requested parameter is in the section. 36 char internbuf[PARSELINELENGHT]; //!< a buffer 39 public: 40 IniParser (const char* filename = NULL); 41 ~IniParser (); 37 42 43 bool openFile(const char* name); 38 44 45 bool getSection( const char* sectionName); 46 const char* nextSection(); 47 48 bool isOpen() const { return (sections != NULL)?true:false; }; 49 50 const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; 51 bool nextVar(); 52 53 const char* getCurrentName() const { return (currentEntry!=NULL)?currentEntry->name:NULL; }; 54 const char* getCurrentValue() const { return (currentEntry!=NULL)?currentEntry->value:NULL; }; 55 56 void debug() const; 57 58 private: 59 void deleteSections(); 60 61 private: 62 tList<IniSection>* sections; 63 IniSection* currentSection; 64 IniEntry* currentEntry; 39 65 }; 40 66
Note: See TracChangeset
for help on using the changeset viewer.