The Parametric Pseudo-Manifold (PPS) Library 1.0
lexer.cpp
Go to the documentation of this file.
00001 
00026 #include "lexer.h"
00027 
00028 #include <sstream>
00029 #include <cctype>
00030 #include <cstdlib>
00031 #include <cassert>
00032 
00033 
00047 namespace offlib {
00048 
00056   bool
00057   Lexer::get_string( std::string& s ) 
00058   {
00059     if ( !skip_space() ) {
00060       return false;
00061     }
00062 
00063     char c;
00064     if ( !_realstr.get( c ) ) {
00065       return false ;
00066     }
00067 
00068     if ( isalpha( c ) ) {
00069       char str[ 256 ] ;
00070       int i = 0 ;
00071       do {
00072         str[i] = c ;
00073 
00074         if ( !_realstr.get( c ) ) {
00075           return false ;
00076         }
00077 
00078         increment_char() ;
00079 
00080         ++i ;
00081       } 
00082       while ( isalnum( c ) && ( i < 255 ) ) ;
00083 
00084       str[ i ] = '\0' ;
00085 
00086       assert( !isalnum( c ) ) ;
00087 
00088       s = str ;
00089       _realstr.putback( c ) ;
00090     }
00091     else {
00092       _realstr.putback( c ) ;
00093 
00094       return false ;
00095     }
00096 
00097     return true ;
00098   }
00099 
00100 
00108   bool
00109   Lexer::get_integer( int& x )
00110   {
00111     if ( !skip_space() ) {
00112       return false ;
00113     }
00114 
00115     char c ;
00116     if ( !_realstr.get( c ) ) {
00117       return false ;
00118     }
00119 
00120     if ( isdigit( c ) ) {
00121       char str[ 256 ] ;
00122       int i = 0 ;
00123       do {
00124         str[ i ] = c ;
00125 
00126         if ( !_realstr.get( c ) ) {
00127           return false ;
00128         }
00129 
00130         increment_char() ;
00131 
00132         ++i ;
00133       } 
00134       while ( isdigit( c ) && ( i < 255 ) ) ;
00135 
00136       str[ i ] = '\0' ;
00137 
00138       assert( !isdigit( c ) ) ;
00139 
00140       x = atoi( str ) ;
00141 
00142       _realstr.putback( c ) ;
00143     }
00144     else {
00145       _realstr.putback( c ) ;
00146 
00147       return false;
00148     }
00149 
00150     return true;
00151   }
00152 
00153 
00161   bool
00162   Lexer::get_double( double& d )
00163   {
00164     if ( !skip_space() ) {
00165       return false ;
00166     }
00167 
00168     char c ;
00169     if ( !_realstr.get( c ) ) {
00170       return false ;
00171     }
00172    
00173     _realstr.putback( c ) ;
00174 
00175     assert( ( _realstr >> d ) ) ;
00176 
00177     return true;
00178   }
00179 
00180 
00187   bool
00188   Lexer::skip_space() 
00189   {
00190     char c ;
00191 
00192     for ( ; ; ) {
00193       if ( !_realstr.get( c ) ) {
00194         return false ;
00195       }
00196 
00197       increment_char() ;
00198 
00199       if ( c == '\n' ) {
00200         increment_line() ;
00201       }
00202 
00203       if ( !isspace( c ) ) {
00204         _realstr.putback( c ) ;
00205         decrement_char() ;
00206         return true ;
00207       }
00208     }
00209 
00210     return true ;
00211   }
00212 
00213 }
00214  //end of group class.