Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

configsource.cpp

Go to the documentation of this file.
00001 // configsource.cpp: implementation of the ConfigSource class.
00002 //
00004 
00005 #include "configsource.h"
00006 
00007 int ConfigSource::INVALID_INT    = INT_MIN;
00008 long ConfigSource::INVALID_LONG  = LONG_MIN;
00009 double ConfigSource::INVALID_DOUBLE = DBL_MIN;
00010 
00012 // Construction/Destruction
00014 
00015 ConfigSource::ConfigSource()
00016 {
00017    ReadMode = CS_MODE_COMMAND;
00018    strcpy( Token, "" );
00019    strcpy( CurrentLine, "" );
00020    TokenIsNull    = true;
00021    LineParseIndex = 0;
00022    LastError      = 0;
00023    strcpy( LastMessage, "" );
00024 }
00025 //---------------------------------------------------------------------------
00026 ConfigSource::~ConfigSource()
00027 {
00028    Close();
00029 }
00030 //---------------------------------------------------------------------------
00031 char* ConfigSource::NextToken()
00032 {
00033    // Initialize token as empty string
00034    Token[0] = '\0';
00035    TokenIsNull = true;
00036 
00037         LastError = 0;
00038    strcpy( LastMessage, "" );
00039 
00040    bool _started  = false  // token start found, parsing token
00041       , _isquoted = false  // parsing a quoted token
00042       , _tokenend = false  // end of token found, leave parsing loop
00043       ;
00044 
00045    LineParseIndex++; // Advance off last location (whitespace) in CurrentLine
00046 
00047 
00048    int _tokenChIndex = 0 ;
00049 
00050    // Scan for beginning of token.
00051    // Note that first char in the buffer (CurrentLine) will
00052    // have been set to a space (token separator) which
00053    // is why we start with an increment of TokenParseIndex
00054    //
00055    int _linelen = strlen( CurrentLine );
00056 
00057         for( ; (! _tokenend) && LineParseIndex < _linelen ; LineParseIndex++ )
00058         {
00059                 switch( CurrentLine[LineParseIndex] )
00060       {
00061         case ' ':
00062         case '\t':
00063         case ',':
00064              if ( _started )
00065              {
00066                 if ( _isquoted )
00067                 {
00068                    // space, tab or comma within quoted token, include the char
00069                    Token[_tokenChIndex++] = CurrentLine[LineParseIndex];
00070                    TokenIsNull = false;
00071                 }
00072                 else
00073                 {
00074                    // space, tab or comma while parsing non-quoted token -- end of token
00075                    _tokenend = true;
00076                    // Step back line parse index to negate increment at loop termination
00077                    LineParseIndex--;
00078                 }
00079              }
00080              // else  token not started, this is whitespace
00081              break;
00082 
00083         case '#':
00084              // comment started, assure no further parsing of this line
00085              LineParseIndex = _linelen;
00086 
00087         case '\n':
00088              if ( ! _started )
00089              {
00090                 // end of line before token start found
00091                 Token[0] = 0;  // token is empty string
00092                 TokenIsNull = true;
00093              }
00094              // else  end of line after token started
00095              //       this is end of token (even if started with quote)
00096 
00097              _tokenend = true;
00098 
00099              break;
00100 
00101         case '"':
00102              if ( _isquoted )
00103              {
00104                 // Already parsing a quoted token, this is the end thereof
00105                 _tokenend = true;
00106              }
00107              else
00108              {
00109                 // not yet parsing quoted token
00110 
00111                 if ( _tokenChIndex == 0 )
00112                 {
00113                    // First char in the token parsing,
00114                    // start a new token
00115                    _isquoted = true;
00116                    _started  = true;
00117 //                   TokenIsNull = false;
00118                 }
00119                 // else  Not the first char in the token.
00120                 //       In theory embed quote within token,
00121                 //       but better to just skip it
00122              }
00123              break;
00124 
00125         default:
00126              // regular character, add to token (fld)
00127              Token[_tokenChIndex++] = CurrentLine[LineParseIndex];
00128              _started  = true;
00129              TokenIsNull = false;
00130                      break;
00131       }
00132    }
00133 
00134 
00135    Token[_tokenChIndex] = 0;  // null-terminate string
00136 
00137    return Token;
00138 
00139 }
00140 //---------------------------------------------------------------------------
00141 // Get fixed position token, strips leading and trailing blanks
00142 char* ConfigSource::GetToken(int n, int off) {
00143         unsigned int j;
00144         char ch;
00145         char fld[1024];
00146         int j1 = off;
00147         unsigned int j2 = off + n;
00148         if( j2 > strlen( CurrentLine ) )
00149    {
00150                 j2 = strlen( CurrentLine );
00151         }
00152    for( j = j1 ; j < j2 ; j++ ) {
00153                 ch = CurrentLine[j];
00154                 if(ch == ' ')
00155                         continue;
00156                 break;
00157         }
00158         int nfld = 0;
00159         int jj = 0;
00160         for( ; j < j2 ; j++ ) {
00161                 ch = CurrentLine[j];
00162                 fld[jj++] = ch;
00163                 if(ch == ' ')
00164                         break;
00165                 nfld = jj;
00166         }
00167         fld[nfld] = 0;
00168         strcpy( Token, fld );
00169         LineParseIndex = j2 - 1;
00170         return Token;
00171 }
00172 //---------------------------------------------------------------------------
00173 char* ConfigSource::GetToken(int n) {
00174         return GetToken( n, LineParseIndex + 1 );
00175 }
00176 //---------------------------------------------------------------------------
00177 // Load : Load string into command buffer, initialize for parse.
00178 int ConfigSource::Load( const char * p_cmd)
00179 {
00180    if ( p_cmd == NULL || MAX_LINE_LENGTH < strlen(p_cmd) )
00181    {
00182       return 0;
00183    }
00184         LastError = 0;
00185    strcpy( LastMessage, "" );
00186         strcpy( CurrentLine, p_cmd );
00187         LineParseIndex = -1;
00188         ReadMode = CS_MODE_COMMAND;
00189         return strlen( CurrentLine );
00190 }
00191 //---------------------------------------------------------------------------
00192 int ConfigSource::Error( char** r_textual )
00193 {
00194         int k = LastError;
00195    if ( *r_textual != NULL )
00196    {
00197       *r_textual = LastMessage;
00198    }
00199         LastError = 0;
00200         return k;
00201 }
00202 //---------------------------------------------------------------------------
00203 char* ConfigSource::String()
00204 {
00205         return ConfigSource::NextToken();
00206 }
00207 //---------------------------------------------------------------------------
00208 int ConfigSource::Int()
00209 {
00210    NextToken();
00211    if( 0 < strlen( Token ) )
00212    {
00213           return atoi(Token);
00214    }
00215    return INVALID_INT;
00216 }
00217 //---------------------------------------------------------------------------
00218 int ConfigSource::Int(int n) {
00219    return Int( n, LineParseIndex + 1 );
00220 }
00221 //---------------------------------------------------------------------------
00222 int ConfigSource::Int(int n, int off) {
00223    GetToken(n, off);
00224    if( 0 < strlen( Token ) )
00225    {
00226       return atoi(Token);
00227    }
00228    return INVALID_INT;
00229 }
00230 //---------------------------------------------------------------------------
00231 long ConfigSource::Long()
00232 {
00233         NextToken();
00234         if( 0 < strlen( Token ) )
00235    {
00236                 return atol(Token);
00237    }
00238         return INVALID_LONG;
00239 }
00240 //---------------------------------------------------------------------------
00241 long ConfigSource::Long(int n) {
00242         return Long( n, LineParseIndex + 1 );
00243 }
00244 //---------------------------------------------------------------------------
00245 long ConfigSource::Long(int n, int off) {
00246         GetToken(n, off);
00247         if( 0 < strlen( Token ) )
00248    {
00249       return atol(Token);
00250    }
00251         return INVALID_INT;
00252 }
00253 //---------------------------------------------------------------------------
00254 double ConfigSource::Double()
00255 {
00256         NextToken();
00257         if( 0 < strlen( Token ) )
00258    {
00259                 return atof(Token);
00260         }
00261    return INVALID_DOUBLE;
00262 }
00263 //---------------------------------------------------------------------------
00264 double ConfigSource::Double(int n) {
00265         return Double( n, LineParseIndex + 1 );
00266 }
00267 //---------------------------------------------------------------------------
00268 double ConfigSource::Double(int n, int off) {
00269         GetToken(n, off);
00270         if( 0 < strlen( Token ) )
00271    {
00272            return atof(Token);
00273    }
00274         return INVALID_DOUBLE;
00275 }
00276 //---------------------------------------------------------------------------
00277 bool ConfigSource::Its(const char* p_str)
00278 {
00279         return ( strcmp( Token, p_str ) == 0 );
00280 }
00281 //---------------------------------------------------------------------------
00282 

Generated on Tue May 6 09:16:00 2003 for Earthworm Libs by doxygen1.3-rc3