openModeller  Version 1.4.0
FileParser.cpp
Go to the documentation of this file.
00001 
00027 #include <openmodeller/FileParser.hh>
00028 #include <string.h>
00029 #include <stdio.h>
00030 
00031 #if 0
00032 // Need an ostream inserter for the special icstring type.
00033 inline std::ostream& operator<<( std::ostream& strm, const FileParser::icstring& s )
00034 {
00035   return strm << std::string(s.data(),s.length() );
00036 }
00037 #endif
00038 
00039 /****************************************************************/
00040 /**************************** File Parser ***********************/
00041 
00042 static char error[256];
00043 
00044 /******************/
00045 /*** constructor ***/
00046 
00047 FileParser::FileParser( const std::string & file )
00048 {
00049   if ( ! load( file ) ) {
00050 
00051     sprintf( error, "File '%s' was not found.\n", file.c_str() );
00052     fprintf( stderr, "%s", error );
00053     throw error;
00054   }
00055 }
00056 
00057 
00058 /******************/
00059 /*** destructor ***/
00060 
00061 FileParser::~FileParser()
00062 {
00063 }
00064 
00065 /************/
00066 /*** load ***/
00067 int
00068 FileParser::load( const std::string & file )
00069 {
00070   FILE *fd = fopen( file.c_str(), "r" );
00071   if ( ! fd )
00072     return 0;
00073 
00074   f_lst.clear();
00075 
00076   const int size = 1024;
00077   char line[size];
00078 
00079   while ( fgets( line, size, fd ) ) {
00080     // Find the first # which indicates the start of comment.
00081     char *sep = strchr( line,'#' );
00082     // If it's at the beginning of the line
00083     // loop.
00084     if ( sep == line )
00085       continue;
00086 
00087     // If it's not at the beginning of the line,
00088     // assign it to "null", terminating the string,
00089     // and effectively commenting to end of line.
00090     if ( sep )
00091       *sep = '\0';
00092 
00093     // Find the start of the key.
00094     // Trim the whitespace from the front of the line.
00095     char *start_key = line;
00096     while ( isspace( *start_key ) && (*start_key != '\0') ) {
00097       start_key++;
00098     }
00099 
00100     // Nothing but whitespace.  Loop.
00101     if ( *start_key == '\0' )
00102       continue;
00103 
00104     // Separate key and value
00105     sep = strchr( line, '=' );
00106     if ( sep ) {
00107       // Find the start of the value.
00108       char *start_val = sep+1;
00109       // Left trim the whitespace.
00110       while ( isspace( *start_val ) && ( *start_val != '\0' ) ) {
00111   start_val++;
00112       }
00113       // No value?  loop.
00114       if ( *start_val == '\0' )
00115   continue;
00116 
00117       // Null terminate the key.
00118       *sep-- = '\0';
00119       // and right trim it.
00120       while( isspace( *sep ) )
00121   *sep-- = '\0';
00122 
00123       // Right trim the value.
00124       sep = start_val + strlen( start_val ) - 1;
00125       // Remember the \0 we substitued for the '='?
00126       // use that to terminate the loop.
00127       while ( isspace( *sep ) && ( *sep != '\0' ) )
00128   *sep-- = '\0';
00129       
00130       if ( *sep == '\0' )
00131   continue;
00132       
00133       f_lst.push_back( std::make_pair( start_key, start_val ) );
00134     }
00135   }
00136   
00137   fclose( fd );
00138   return 1;
00139 }
00140 
00141 
00142 /***********/
00143 /*** get ***/
00144 std::string
00145 FileParser::get( const std::string & key ) const
00146 {
00147   ItemList::const_iterator it = f_lst.begin();
00148 
00149   while ( it != f_lst.end() ) {
00150 
00151     if ( (*it).first == key.c_str() ) {
00152 
00153       return it->second;
00154     }
00155 
00156     ++it;
00157   }
00158   return "";
00159 }
00160 
00161 
00162 /*************/
00163 /*** count ***/
00164 int
00165 FileParser::count( const std::string & key ) const
00166 {
00167   int n = 0;
00168 
00169   ItemList::const_iterator it = f_lst.begin();
00170 
00171   while ( it != f_lst.end() ) {
00172 
00173     if (  it->first == key.c_str() ) {
00174 
00175       ++n;
00176     }
00177     ++it;
00178   }
00179   return n;
00180 }
00181 
00182 /***************/
00183 /*** get All ***/
00184 std::vector<std::string>
00185 FileParser::getAll( const std::string & key ) const
00186 {
00187   std::vector<std::string> values;
00188   ItemList::const_iterator it = f_lst.begin();
00189 
00190   while ( it != f_lst.end() ) {
00191 
00192     if ( it->first == key.c_str() ) {
00193 
00194       values.push_back( it->second );
00195     }
00196 
00197     ++it;
00198   }
00199   return values;
00200 }