Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3238 in orxonox.OLD for orxonox/branches/sound/src/ini_parser.cc


Ignore:
Timestamp:
Dec 20, 2004, 2:42:54 AM (20 years ago)
Author:
bensch
Message:

orxonox/branches: updated branches: buerli, nico, sound. And moved bezierTrack to old.bezierTrack. Conflicts resolved in a usefull order.
Conflics mostly resolved in favor of trunk
merge.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/sound/src/ini_parser.cc

    r2551 r3238  
    2525IniParser::IniParser (char* filename)
    2626{
    27         stream = NULL;
    28         bInSection = false;
    29         open_file (filename);
     27  stream = NULL;
     28  bInSection = false;
     29  openFile(filename);
    3030}
    3131
     
    3535IniParser::~IniParser ()
    3636{
    37         if( stream != NULL) fclose (stream);
     37  if( stream != NULL) fclose (stream);
    3838}
    3939
    4040/**
    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;
     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;
    4444*/
    45 int IniParser::open_file( char* filename)
     45int IniParser::openFile( char* filename)
    4646{
    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;
     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;
    5656}
    5757
    5858/**
    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
     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
    6262*/
    63 int IniParser::get_section( char* section)
     63int IniParser::getSection( char* section)
    6464{
    6565  bInSection = false;
     
    9595
    9696/**
    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
     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
    101101*/
    102 int IniParser::next_var( char* name, char* value)
     102int IniParser::nextVar( char* name, char* value)
    103103{
    104         if( stream == NULL)
     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] == '[')
    105121        {
    106                 bInSection = false;
    107                 return -1;
     122          bInSection = false;
     123          return -1;
    108124        }
    109         if( !bInSection) return -1;
    110        
    111         char linebuffer[PARSELINELENGHT];
    112         char* ptr;
    113 
    114         while( !feof( stream))
     125      if( (ptr = strchr( linebuffer, '=')) != NULL)
    115126        {
    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                 }
     127          if( ptr == linebuffer) continue;
     128          strcpy (value, &ptr[1]);
     129          strncpy (name, linebuffer, strlen (linebuffer) - strlen (value) - 1);
     130          return 0;
    132131        }
    133         return -1;     
     132    }
     133  return -1;   
    134134}
    135135
    136136/**
    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.
     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.
    145145*/
    146 char* IniParser::get_var( char* name, char* section, char* defvalue = "")
     146char* IniParser::getVar( char* name, char* section, char* defvalue = "")
    147147{
    148         strcpy (internbuf, defvalue);
    149         if( get_section (section) == -1) return internbuf;
    150        
    151         char namebuf[PARSELINELENGHT];
    152         char valuebuf[PARSELINELENGHT];
    153        
    154         while( next_var (namebuf, valuebuf) != -1)
     148  strcpy (internbuf, defvalue);
     149  if( getSection (section) == -1) return internbuf;
     150 
     151  char namebuf[PARSELINELENGHT];
     152  char valuebuf[PARSELINELENGHT];
     153 
     154  while( nextVar (namebuf, valuebuf) != -1)
     155    {
     156      if( !strcmp (name, namebuf))
    155157        {
    156                 if( !strcmp (name, namebuf))
    157                 {
    158                         strcpy (internbuf, valuebuf);
    159                         return internbuf;
    160                 }
     158          strcpy (internbuf, valuebuf);
     159          return internbuf;
    161160        }
    162         return internbuf;
     161    }
     162  return internbuf;
    163163}
Note: See TracChangeset for help on using the changeset viewer.