Joris Aerts / ConfigFile

Fork of ConfigFile by peter brier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ConfigFile.cpp Source File

ConfigFile.cpp

00001 /*
00002  * ConfigFile.cpp
00003  * Simple Config file reader class
00004  *
00005  * Copyright (c) 2011 Peter Brier
00006  *
00007  *   This file is part of the LaOS project (see: http://wiki.protospace.nl/index.php/LaOS)
00008  *
00009  *   LaOS is free software: you can redistribute it and/or modify
00010  *   it under the terms of the GNU General Public License as published by
00011  *   the Free Software Foundation, either version 3 of the License, or
00012  *   (at your option) any later version.
00013  *
00014  *   LaOS is distributed in the hope that it will be useful,
00015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *   GNU General Public License for more details.
00018  *
00019  *   You should have received a copy of the GNU General Public License
00020  *   along with LaOS.  If not, see <http://www.gnu.org/licenses/>.
00021  * 
00022  */
00023 #include "ConfigFile.h"
00024 
00025 // Make new config file object
00026 ConfigFile::ConfigFile(char *file) 
00027 {
00028  // printf("ConfigFile(%s)\n\r", file);
00029   fp = fopen(file,"rb");
00030 }
00031 
00032 // Destroy a config file (closes the file handle)
00033 ConfigFile::~ConfigFile() 
00034 {
00035  // printf("~ConfigFile()\n\r");
00036   if ( fp != NULL )
00037     fclose(fp);
00038 }
00039 
00040 // Read value
00041 bool ConfigFile::Value(char *key, char *value,  size_t maxlen, char *def)
00042 {
00043   int m=0,n=0,c,s=0;
00044   if (fp == NULL)
00045   {
00046     strncpy(value, def, maxlen);
00047     return false;
00048   }
00049   // printf("Value()\n\r");
00050   n = strlen(key);
00051   fseek(fp, 0L, SEEK_SET);
00052   while( s != 99  ) 
00053   {
00054     c = fgetc(fp);
00055     if ( c == EOF ) 
00056       break;
00057    // printf("%d: '%c'\n\r", s, c);
00058     switch( s  )// sate machine
00059     { 
00060       case 0: // (re) start: note: no break; fall through to case 1
00061         m=0; 
00062         s=1;
00063       case 1: // read key, skip spaces
00064         if ( c == key[m] ) 
00065           m++; 
00066         else 
00067           s = 0;
00068         if ( c == ';' ) 
00069           s = 10; 
00070         else if ( c == ' ' || c == '\t' || c == '\n' || c == '\r') 
00071         {
00072           if ( n == m ) // key found
00073           {
00074             s = 2;
00075             m = 0; 
00076           }
00077           else
00078             s = 0;
00079         }
00080         break;
00081       case 2: // key matched, skip whitepaces upto the first char
00082         if ( c == ';' ) s = 99;
00083         else if ( c != ' ' && c != '\t' ) 
00084         { 
00085           s = 3;
00086           m = 1;
00087           if ( m < maxlen) 
00088             *value++ = c;
00089         }
00090         break;
00091         
00092       case 3: // copy value content, upto eol or comment
00093         if ( m == maxlen || c == '\n' || c == '\r' || c == ';' ) 
00094           s = 99;
00095         else
00096         {
00097           m++;
00098           *value++ = c;
00099         }
00100         break;         
00101       case 10: // skip comments, upto eol or eof
00102         if ( c == '\n' || c == '\r' ) s = 0;
00103         break;
00104      }
00105   }
00106   if ( s == 99 && m >= 1) // key found, and value assigned
00107   {
00108      *value = 0; // terminate string
00109     return true; 
00110   }
00111   else
00112   {
00113     strncpy(value, def, maxlen);
00114     return false;
00115   } 
00116 }
00117 
00118 // Read int value
00119 bool ConfigFile::Value(char *key, int *value, int def)
00120 {
00121   char val[16];
00122   bool b = Value(key, val, 12, "");
00123   if ( b )
00124     *value = atoi(val);
00125   else
00126   {
00127      *value = def;
00128   }
00129   return b;
00130 }
00131 
00132 // Read float value
00133 bool ConfigFile::Value(char *key, float *value, float def)
00134 {
00135   char val[16];
00136   bool b = Value(key, val, 12, "");
00137   if ( b )
00138     *value = atof(val);
00139   else
00140   {
00141      *value = def;
00142   }
00143   return b;
00144 }