Index: orxonox/trunk/src/lib/util/ini_parser.cc
===================================================================
--- orxonox/trunk/src/lib/util/ini_parser.cc	(revision 3484)
+++ orxonox/trunk/src/lib/util/ini_parser.cc	(revision 3484)
@@ -0,0 +1,163 @@
+/* 
+   orxonox - the future of 3D-vertical-scrollers
+
+   Copyright (C) 2004 orx
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   ### File Specific:
+   main-programmer: Christian Meyer
+   co-programmer: ...
+*/
+
+
+#include "ini_parser.h"
+
+using namespace std;
+
+/**
+	\brief constructs an IniParser using a file
+	\param filename: the path and name of the file to parse
+*/
+IniParser::IniParser (char* filename)
+{
+  stream = NULL;
+  bInSection = false;
+  openFile(filename);
+}
+
+/**
+	\brief removes the IniParser from memory
+*/
+IniParser::~IniParser ()
+{
+  if( stream != NULL) fclose (stream);
+}
+
+/**
+   \brief opens another file to parse
+   \param filename: path and name of the new file to parse
+   \return zero on success or -1 if an error occured;
+*/
+int IniParser::openFile( char* filename)
+{
+  if( filename == NULL) return -1;
+  if( stream != NULL)	fclose (stream);
+  if( (stream = fopen (filename, "r")) == NULL)
+    {
+      printf("IniParser could not open %s\n", filename);
+      return -1;
+    }
+  bInSection = false;
+  return 0;
+}
+
+/**
+   \brief set the parsing cursor to the specified section
+   \param section: the name of the section to set the cursor to
+   \return zero on success or -1 if the section could not be found
+*/
+int IniParser::getSection( char* section)
+{
+  bInSection = false;
+  if( stream == NULL) return -1;
+  
+  char linebuffer[PARSELINELENGHT];
+  char secbuffer[PARSELINELENGHT];
+  char* ptr;
+  
+  rewind (stream);
+  while( !feof( stream))
+    {
+      // get next line
+      fgets (linebuffer, PARSELINELENGHT, stream);
+      // remove newline char
+      if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0;
+      // check for section identifyer
+      if( sscanf (linebuffer, "[%s", secbuffer) == 1)
+	{
+	  if( (ptr = strchr( secbuffer, ']')) != NULL)
+	    {
+	      *ptr = 0;
+	      if( !strcmp( secbuffer, section))
+		{
+		  bInSection = true;
+		  return 0;
+		}
+	    }
+	}
+    }
+  return -1;
+}
+
+/**
+   \brief gets the next VarName=VarValue pair from the parsing stream
+   \param name: a pointer to a buffer to store the name of the entry
+   \param value: a pointer to a buffer to store the value of the entry
+   \return zero if the buffers have been filled with data or -1 if there are no entries left in the current section
+*/
+int IniParser::nextVar( char* name, char* value)
+{
+  if( stream == NULL)
+    {
+      bInSection = false;
+      return -1;
+    }
+  if( !bInSection) return -1;
+  
+  char linebuffer[PARSELINELENGHT];
+  char* ptr;
+  
+  while( !feof( stream))
+    {
+      // get next line
+      fgets (linebuffer, PARSELINELENGHT, stream);
+      // remove newline char
+      if( (ptr = strchr( linebuffer, '\n')) != NULL) *ptr = 0;
+      if( linebuffer[0] == '[')
+	{
+	  bInSection = false;
+	  return -1;
+	}
+      if( (ptr = strchr( linebuffer, '=')) != NULL)
+	{
+	  if( ptr == linebuffer) continue;
+	  strcpy (value, &ptr[1]);
+	  strncpy (name, linebuffer, strlen (linebuffer) - strlen (value) - 1);
+	  return 0;
+	}
+    }
+  return -1;	
+}
+
+/**
+   \brief directly acesses an entry in a section
+   \param name: the name of the entry to find
+   \param section: the section where the entry is to be found
+   \param defvalue: what should be returned in case the entry cannot be found
+   \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
+   
+   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
+   lead to unwanted behaviour.
+*/
+char* IniParser::getVar( char* name, char* section, char* defvalue = "")
+{
+  strcpy (internbuf, defvalue);
+  if( getSection (section) == -1) return internbuf;
+  
+  char namebuf[PARSELINELENGHT];
+  char valuebuf[PARSELINELENGHT];
+  
+  while( nextVar (namebuf, valuebuf) != -1)
+    {
+      if( !strcmp (name, namebuf))
+	{
+	  strcpy (internbuf, valuebuf);
+	  return internbuf;
+	}
+    }
+  return internbuf;
+}
Index: orxonox/trunk/src/lib/util/ini_parser.h
===================================================================
--- orxonox/trunk/src/lib/util/ini_parser.h	(revision 3484)
+++ orxonox/trunk/src/lib/util/ini_parser.h	(revision 3484)
@@ -0,0 +1,37 @@
+/*!
+    \file ini_parser.h
+    \brief A small ini file parser
+    
+    Can be used to find a defined [Section] in an ini file and get the VarName=Value entries
+*/
+
+#ifndef _INI_PARSER_H
+#define _INI_PARSER_H
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#define PARSELINELENGHT 512
+
+//! ini-file parser
+/**
+	This class can be used to load an initializer file and parse it's contents for variablename=value pairs.
+*/
+class IniParser {
+ private:
+	FILE* stream;
+	bool bInSection;
+	char internbuf[PARSELINELENGHT];
+	
+ public:
+  IniParser (char* filename);
+  ~IniParser ();
+  
+  char* getVar( char* name, char* section, char* defvalue);
+	int openFile( char* name);
+  int getSection( char* section);
+  int nextVar( char* name, char* value);
+};
+
+#endif /* _INI_PARSER_H */
