Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5014 in orxonox.OLD for orxonox/trunk/src


Ignore:
Timestamp:
Aug 14, 2005, 4:29:29 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: implemented a new kind of ini-parser
this parser first reads the file, and then one can search through without accessing the file anymore

Location:
orxonox/trunk/src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/lib/event/key_mapper.cc

    r4866 r5014  
    121121void KeyMapper::loadKeyBindings(IniParser* iniParser)
    122122{
    123   if( iniParser->getSection (CONFIG_SECTION_PLAYER "1") == -1)
     123  if( !iniParser->getSection (CONFIG_SECTION_PLAYER "1"))
    124124  {
    125125    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n");
    126126    return;
    127127  }
    128   // allocate empty lookup table
    129 
    130   char namebuf[256];
    131   char valuebuf[256];
    132   memset (namebuf, 0, 256);
    133   memset (valuebuf, 0, 256);
    134128  int* index;
    135129
    136   while( iniParser->nextVar (namebuf, valuebuf) != -1)
    137   {
    138     PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
    139     index = nameToIndex (valuebuf);
    140     this->mapKeys(namebuf, index[1]);
    141 
    142       /*
    143     switch( index[0])
    144     {
    145     case 0:
    146     this->mapKeys(namebuf, index[1]);
    147     break;
    148     case 1:
    149     this->mapKeys(namebuf, index[1]);
    150     break;
    151     default:
    152     break;
    153   }
    154       */
    155     memset (namebuf, 0, 256);
    156     memset (valuebuf, 0, 256);
     130  while(iniParser->nextVar())
     131  {
     132    PRINTF(3)("Keys: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
     133    index = nameToIndex (iniParser->getCurrentValue());
     134    this->mapKeys(iniParser->getCurrentName(), index[1]);
    157135  }
    158136
    159137
    160138  // PARSE MISC SECTION
    161   if( iniParser->getSection (CONFIG_SECTION_MISC_KEYS) == -1)
    162   {
    163     PRINTF(1)("Could not find key bindings\n");
     139  if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS))
     140  {
     141    PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n");
    164142    return;
    165143  }
    166144
    167   while( iniParser->nextVar (namebuf, valuebuf) != -1)
    168   {
    169     PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
    170     index = nameToIndex (valuebuf);
    171     this->mapKeys(namebuf, index[1]);
    172       /*
    173     switch( index[0])
    174     {
    175     case 0:
    176     this->mapKeys(namebuf, index[1]);
    177     break;
    178     case 1:
    179     this->mapKeys(namebuf, index[1]);
    180     break;
    181     default:
    182     break;
    183   }
    184       */
    185     memset (namebuf, 0, 256);
    186     memset (valuebuf, 0, 256);
     145  while( iniParser->nextVar())
     146  {
     147    PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
     148    index = nameToIndex (iniParser->getCurrentValue());
     149    this->mapKeys(iniParser->getCurrentName(), index[1]);
    187150  }
    188151}
     
    192155 * @param the name of the button
    193156*/
    194 int* KeyMapper::nameToIndex (char* name)
     157int* KeyMapper::nameToIndex (const char* name)
    195158{
    196159  coord[0] = -1;
     
    216179 * @param id of the key
    217180*/
    218 void KeyMapper::mapKeys(char* name, int keyID)
     181void KeyMapper::mapKeys(const char* name, int keyID)
    219182{
    220183  for(int i = 0; map[i].pValue != NULL; ++i )
  • orxonox/trunk/src/lib/event/key_mapper.h

    r4866 r5014  
    3535
    3636 private:
    37   int* nameToIndex (char* name);
    38   void mapKeys(char* name, int keyID);
     37  int* nameToIndex (const char* name);
     38  void mapKeys(const char* name, int keyID);
    3939
    4040 public:
  • orxonox/trunk/src/lib/graphics/importer/objModel.cc

    r4836 r5014  
    1 /* 
     1/*
    22   orxonox - the future of 3D-vertical-scrollers
    33
     
    6464{
    6565  PRINTF(4)("preparing to read in file: %s\n", fileName);
    66   // splitting the 
     66  // splitting the
    6767  char* split = NULL;
    6868
     
    103103      // line termiated with \0 not \n
    104104      if (buffer[strlen(buffer)-1] == '\n')
    105         buffer[strlen(buffer)-1] = '\0';
     105        buffer[strlen(buffer)-1] = '\0';
    106106
    107107      // case vertice
    108108      if (!strncmp(buffer, "v ", 2))
    109         {
    110           this->addVertex(buffer+2);
    111         }
     109        {
     110          this->addVertex(buffer+2);
     111        }
    112112
    113113      // case face
    114114      else if (!strncmp(buffer, "f ", 2))
    115         {
    116           this->addFace (buffer+2);
    117         }
    118      
     115        {
     116          this->addFace (buffer+2);
     117        }
     118
    119119      else if (!strncmp(buffer, "mtllib ", 7))
    120         {
    121           this->readMtlLib (buffer+7);
    122         }
     120        {
     121          this->readMtlLib (buffer+7);
     122        }
    123123
    124124      else if (!strncmp(buffer, "usemtl ", 7))
    125         {
    126           this->setMaterial (buffer+7);
    127         }
     125        {
     126          this->setMaterial (buffer+7);
     127        }
    128128
    129129      // case VertexNormal
    130130      else if (!strncmp(buffer, "vn ", 3))
    131         {
    132           this->addVertexNormal(buffer+3);
    133         }
    134      
     131        {
     132          this->addVertexNormal(buffer+3);
     133        }
     134
    135135      // case VertexTextureCoordinate
    136136      else if (!strncmp(buffer, "vt ", 3))
    137         {
    138           this->addVertexTexture(buffer+3);
    139         }
     137        {
     138          this->addVertexTexture(buffer+3);
     139        }
    140140      // case group
    141141      else if (!strncmp(buffer, "g ", 2))
    142         {
    143           this->addGroup (buffer+2);
    144         }
     142        {
     143          this->addGroup (buffer+2);
     144        }
    145145      else if (!strncmp(buffer, "s ", 2)) //! @todo smoothing groups have to be implemented
    146         {
    147           PRINTF(2)("smoothing groups not supportet yet. line: %s\n", buffer);
    148         }
     146        {
     147          PRINTF(2)("smoothing groups not supportet yet. line: %s\n", buffer);
     148        }
    149149    }
    150150  fclose (stream);
     
    152152}
    153153
    154 /** 
     154/**
    155155  *  Function to read in a mtl File.
    156156  * @param mtlFile The .mtl file to read
     
    183183      // line termiated with \0 not \n
    184184      if (buffer[strlen(buffer)-1] == '\n')
    185         buffer[strlen(buffer)-1] = '\0';
     185        buffer[strlen(buffer)-1] = '\0';
    186186
    187187      // create new Material
    188188      if (!strncmp(buffer, "newmtl ", 7))
    189         {
    190           tmpMat = this->addMaterial(buffer+7);//tmpMat->addMaterial(buffer+7);
    191         }
     189        {
     190          tmpMat = this->addMaterial(buffer+7);//tmpMat->addMaterial(buffer+7);
     191        }
    192192      // setting a illumMode
    193193      else if (!strncmp(buffer, "illum ", 6))
    194         {
    195           if (likely(tmpMat != NULL))
    196             tmpMat->setIllum(buffer+6);
    197 
    198         }
     194        {
     195          if (likely(tmpMat != NULL))
     196            tmpMat->setIllum(buffer+6);
     197
     198        }
    199199      // setting Diffuse Color
    200200      else if (!strncmp(buffer, "Kd ", 3))
    201         {
    202           if (likely(tmpMat != NULL))
    203             tmpMat->setDiffuse(buffer+3);
    204         }
     201        {
     202          if (likely(tmpMat != NULL))
     203            tmpMat->setDiffuse(buffer+3);
     204        }
    205205      // setting Ambient Color
    206206      else if (!strncmp(buffer, "Ka ", 3))
    207         {
    208           if (likely(tmpMat != NULL))
    209             tmpMat->setAmbient(buffer+3);
    210         }
     207        {
     208          if (likely(tmpMat != NULL))
     209            tmpMat->setAmbient(buffer+3);
     210        }
    211211      // setting Specular Color
    212212      else if (!strncmp(buffer, "Ks ", 3))
    213         {
    214           if (likely(tmpMat != NULL))
    215             tmpMat->setSpecular(buffer+3);
    216         }
     213        {
     214          if (likely(tmpMat != NULL))
     215            tmpMat->setSpecular(buffer+3);
     216        }
    217217      // setting The Specular Shininess
    218218      else if (!strncmp(buffer, "Ns ", 3))
    219         {
    220           if (likely(tmpMat != NULL))
    221             tmpMat->setShininess(buffer+3);
    222         }
     219        {
     220          if (likely(tmpMat != NULL))
     221            tmpMat->setShininess(buffer+3);
     222        }
    223223      // setting up transparency
    224224      else if (!strncmp(buffer, "d ", 2))
    225         {
    226           if (likely(tmpMat != NULL))
    227             tmpMat->setTransparency(buffer+2);
    228         }
     225        {
     226          if (likely(tmpMat != NULL))
     227            tmpMat->setTransparency(buffer+2);
     228        }
    229229      else if (!strncmp(buffer, "Tf ", 3))
    230         {
    231           if (likely(tmpMat != NULL))
    232             tmpMat->setTransparency(buffer+3);
    233         }
    234      
     230        {
     231          if (likely(tmpMat != NULL))
     232            tmpMat->setTransparency(buffer+3);
     233        }
     234
    235235      else if (!strncmp(buffer, "map_Kd ", 7))
    236         {
    237           if (likely(tmpMat != NULL))
    238             tmpMat->setDiffuseMap(buffer+7);
    239         }
     236        {
     237          if (likely(tmpMat != NULL))
     238            tmpMat->setDiffuseMap(buffer+7);
     239        }
    240240      else if (!strncmp(buffer, "map_Ka ", 7))
    241         {
    242           if (likely(tmpMat != NULL))
    243             tmpMat->setAmbientMap(buffer+7);
    244         }
     241        {
     242          if (likely(tmpMat != NULL))
     243            tmpMat->setAmbientMap(buffer+7);
     244        }
    245245      else if (!strncmp(buffer, "map_Ks ", 7))
    246         {
    247           if (likely(tmpMat != NULL))
    248             tmpMat->setSpecularMap(buffer+7);
    249         }
     246        {
     247          if (likely(tmpMat != NULL))
     248            tmpMat->setSpecularMap(buffer+7);
     249        }
    250250      else if (!strncmp(buffer, "bump ", 5))
    251         {
    252           if (likely(tmpMat != NULL))
    253             tmpMat->setBump(buffer+7);
    254         }
    255      
     251        {
     252          if (likely(tmpMat != NULL))
     253            tmpMat->setBump(buffer+7);
     254        }
     255
    256256
    257257    }
  • orxonox/trunk/src/lib/gui/gui_exec.cc

    r4836 r5014  
    2727
    2828#include "resource_manager.h"
     29#include "ini_parser.h"
    2930
    3031#include <string.h>
     
    3233#include <sys/stat.h>
    3334#include <sys/types.h>
     35
    3436
    3537#ifdef __WIN32__
     
    263265void GuiExec::readFromFile(Widget* widget)
    264266{
    265   this->CONFIG_FILE = fopen(this->confFile, "r");
    266   VarInfo varInfo;
    267   if(this->CONFIG_FILE)
    268     {
    269       Widget* groupWidget = widget;
    270       char Buffer[256] = "";
    271       char Variable[256]= "";
    272       char* Value;
    273       while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF)
    274         {
    275           // group-search //
    276           if(!strncmp(Buffer, "[", 1))
    277             {
    278               if((groupWidget = locateGroup(widget, Buffer, 1))==NULL)
    279                 {
    280                   PRINTF(2)("!!There is no group called %s in this GUI.\n First best Widget will get the Infos assigned.\n Config-File will be updated in next Save\n", Buffer);
    281                   groupWidget = widget;
    282                 }
    283               else
    284                 PRINT(5)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName);
    285             }
    286           // option-setting //
    287           if(!strcmp(Buffer, "="))
    288             {
    289               char* under2space;
    290               while(under2space = strchr(Variable, '_'))
    291                 {
    292                   sprintf(under2space, " %s", under2space+1);
    293                 }
    294 
    295               fscanf(this->CONFIG_FILE, "%s", Buffer);
    296               varInfo.variableName = Variable;
    297               varInfo.variableValue = Buffer;
    298               groupWidget->walkThrough(this->readFileText, &varInfo, 0);
    299               sprintf(Variable, "");
    300             }
    301           sprintf(Variable, "%s", Buffer);
    302         }
    303       widget->walkThrough(widget->setOptions, 0);
    304     }
    305     fclose(this->CONFIG_FILE);
     267  IniParser iniParser(this->confFile);
     268  if (!iniParser.isOpen())
     269    return;
     270
     271//   const char* name;
     272//   while (name = iniParser.nextSection())
     273//     printf("%s\n", name);
     274//
     275//
     276//   this->CONFIG_FILE = fopen(this->confFile, "r");
     277//   VarInfo varInfo;
     278//   if(this->CONFIG_FILE)
     279//     {
     280//       Widget* groupWidget = widget;
     281//       char Buffer[256] = "";
     282//       char Variable[256]= "";
     283//       char* Value;
     284//       while(fscanf(this->CONFIG_FILE, "%s", Buffer) != EOF)
     285//         {
     286//           // group-search //
     287//           if(!strncmp(Buffer, "[", 1))
     288//             {
     289//               if((groupWidget = locateGroup(widget, Buffer, 1))==NULL)
     290//                 {
     291//                   PRINTF(2)("!!There is no group called %s in this GUI.\n First best Widget will get the Infos assigned.\n Config-File will be updated in next Save\n", Buffer);
     292//                   groupWidget = widget;
     293//                 }
     294//               else
     295//                 PRINT(5)("Group %s located.\n", static_cast<Packer*>(groupWidget)->groupName);
     296//             }
     297//           // option-setting //
     298//           if(!strcmp(Buffer, "="))
     299//             {
     300//               char* under2space;
     301//               while(under2space = strchr(Variable, '_'))
     302//                 {
     303//                   sprintf(under2space, " %s", under2space+1);
     304//                 }
     305//
     306//               fscanf(this->CONFIG_FILE, "%s", Buffer);
     307//               varInfo.variableName = Variable;
     308//               varInfo.variableValue = Buffer;
     309//               groupWidget->walkThrough(this->readFileText, &varInfo, 0);
     310//               sprintf(Variable, "");
     311//             }
     312//           sprintf(Variable, "%s", Buffer);
     313//         }
     314//       widget->walkThrough(widget->setOptions, 0);
     315//     }
     316//     fclose(this->CONFIG_FILE);
    306317}
    307318
  • orxonox/trunk/src/lib/util/ini_parser.cc

    r4836 r5014  
    1010
    1111   ### 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.
    1418*/
    1519
    1620
    1721#include "ini_parser.h"
     22
     23#include "list.h"
     24#include "stdlibincl.h"
    1825
    1926#include "resource_manager.h"
     
    2633 * @param filename: the path and name of the file to parse
    2734*/
    28 IniParser::IniParser (const char* filename)
     35IniParser::IniParser (const char* fileName)
    2936{
    3037  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);
    3545}
    3646
     
    4050IniParser::~IniParser ()
    4151{
    42   if( stream != NULL) fclose (stream);
    43 }
    44 
    45 /**
    46  *  opens another file to parse
     52  deleteSections();
     53}
     54
     55void 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
    4787 * @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*/
     90bool 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;
    5297  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
    55102  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;
    64175}
    65176
    66177/**
    67178 *  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*/
     182bool 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 */
     205const 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;
    102218}
    103219
    104220/**
    105221 *  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 */
     226bool 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;
    147244}
    148245
    149246/**
    150247 *  directly acesses an entry in a section
    151  * @param name: the name of the entry to find
    152  * @param section: the section where the entry is to be found
    153  * @param defvalue: what should be returned in case the entry cannot be found
     248 * @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
    154251 * @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
    155252
     
    157254   lead to unwanted behaviour.
    158255*/
    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))
     256const 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)
    170269        {
    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();
    173277        }
    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
     294void 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  
    11/*!
    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 */
     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 */
    77
    88#ifndef _INI_PARSER_H
    99#define _INI_PARSER_H
    1010
    11 #include <stdio.h>
    12 #include <string.h>
    13 #include <stdlib.h>
     11#define PARSELINELENGHT     512       //!< how many chars to read at once
     12
    1413#include "base_object.h"
    1514
    16 #define PARSELINELENGHT     512       //!< how many chars to read at once
     15// FORWARD DEFINITION //
     16template<class T> class tList;
    1717
    1818//! ini-file parser
    1919/**
    20        This class can be used to load an initializer file and parse it's contents for variablename=value pairs.
    21 */
     20 * This class can be used to load an initializer file and parse it's contents for variablename=value pairs.
     21 */
    2222class IniParser : public BaseObject
    2323{
    24  public:
    25   IniParser (const char* filename);
    26   ~IniParser ();
     24  private:
     25    ////////////////////////////////////
     26    struct IniEntry
     27    {
     28      char*              name;
     29      char*              value;
     30    };
    2731
    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    ////////////////////////////////////
    3238
    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 ();
    3742
     43    bool openFile(const char* name);
    3844
     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;
    3965};
    4066
  • orxonox/trunk/src/orxonox.cc

    r4985 r5014  
    219219  PRINT(3)("initializing ResourceManager\n");
    220220
    221   // create parser
    222   if( this->iniParser->getSection (CONFIG_SECTION_DATA) == -1)
     221  const char* dataPath;
     222  if ((dataPath = this->iniParser->getVar(CONFIG_NAME_DATADIR, CONFIG_SECTION_DATA))!= NULL)
    223223  {
    224     PRINTF(1)("Could not find Section %s in %s\n", CONFIG_SECTION_DATA, DEFAULT_CONFIG_FILE);
    225     return -1;
    226   }
    227   char namebuf[256];
    228   char valuebuf[256];
    229   memset (namebuf, 0, 256);
    230   memset (valuebuf, 0, 256);
    231 
    232   while( this->iniParser->nextVar (namebuf, valuebuf) != -1)
    233   {
    234     if (!strcmp(namebuf, CONFIG_NAME_DATADIR))
     224    if (!ResourceManager::getInstance()->setDataDir(dataPath))
    235225    {
    236           //  printf("Not yet implemented\n");
    237       if (!ResourceManager::getInstance()->setDataDir(valuebuf))
    238       {
    239         PRINTF(1)("Data Could not be located\n");
    240         exit(-1);
    241       }
     226      PRINTF(1)("Data Could not be located\n");
     227      exit(-1);
    242228    }
    243 
    244     memset (namebuf, 0, 256);
    245     memset (valuebuf, 0, 256);
    246229  }
    247230
  • orxonox/trunk/src/util/track/track_manager.h

    r4836 r5014  
    11/*!
    2     \file track_manager.h
    3   *  manages all tracks defined in the world and the path the player takes
    4 
    5    it is a container for all tracks and all track-nodes. it manages the movement of
    6    the track helper-parent (that drives the player). it is responsable for calculating
    7    smooth curves etc.
     2 * @file track_manager.h
     3 *  manages all tracks defined in the world and the path the player takes
     4 *
     5 * it is a container for all tracks and all track-nodes. it manages the movement of
     6 * the track helper-parent (that drives the player). it is responsable for calculating
     7 * smooth curves etc.
    88*/
    99
Note: See TracChangeset for help on using the changeset viewer.