00001
00011 #ifndef _INPUT_BASSO_H_
00012 #define _INPUT_BASSO_H_
00013
00014
00015 #include <iostream>
00016 #include <iomanip>
00017 #include <fstream>
00018 #include <list>
00019
00020
00021 #include "basso.h"
00022
00023 namespace basso {
00024
00035 class InputCard {
00036
00037 public:
00042 InputCard( char keyword='$', char comment='#' ) { fComment=comment; fKeyword=keyword; }
00043
00049 InputCard( istream &in, char keyword='$', char comment='#' )
00050 { fComment=comment; fKeyword=keyword; Read(in); }
00051
00053 void Read( istream &in );
00054
00056 int NumFields() const { return fCardFields.size()-1; }
00057
00059 const String &Keyword() const { return fCardFields[0]; }
00060
00065 const String &Field( int i ) const { return fCardFields[i+1]; }
00066
00071 int iField( int i ) const { return atoi( fCardFields[i+1].c_str() ); }
00072
00077 Numeric nField( int i ) const { return atof( fCardFields[i+1].c_str() ); }
00078
00079 void Print( ostream &out=StdOutput ) const;
00080
00081 protected:
00082
00085 void AddRecord( const String &record );
00086
00087
00088 protected:
00089 char fComment, fKeyword;
00090 Array<String> fCardFields;
00091
00092 };
00093
00094 void InputCard::Print( ostream &out ) const
00095 {
00096 Array<String>::const_iterator itr;
00097 out << "{ ";
00098 for ( itr=fCardFields.begin(); itr!=fCardFields.end(); ++itr )
00099 out << *itr << " ";
00100 out << "}";
00101 }
00102
00103 void InputCard::AddRecord( const String &record )
00104 {
00105
00106 String::size_type loc1, loc2;
00107 loc1 = record.find_first_not_of(" ");
00108 while ( 1 ) {
00109 loc2 = record.find_first_of(" ,",loc1);
00110 fCardFields.push_back( record.substr(loc1,loc2-loc1) );
00111 loc1 = record.find_first_not_of(" ", loc2 );
00112 if ( loc1 == string::npos )
00113 break;
00114 }
00115 }
00116
00117 void InputCard::Read( istream &inFile )
00118 {
00119 fCardFields.clear();
00120 String line;
00121 getline( inFile, line );
00122 while ( line[0]!=fKeyword ) {
00123 getline( inFile, line );
00124 if ( inFile.eof() )
00125 return;
00126 }
00127 line = line.substr(1,line.length());
00128
00129 String line2;
00130 char nc=inFile.peek();
00131 while ( nc!=fKeyword ) {
00132 getline( inFile, line2 );
00133 if ( inFile.eof() )
00134 break;
00135 if ( line2[0]!=fComment )
00136 line = line + " " + line2;
00137 nc=inFile.peek();
00138 }
00139 AddRecord(line);
00140 }
00141
00142 ostream &operator << ( std::ostream &out, const InputCard &card )
00143 {
00144 card.Print(out);
00145 return out;
00146 }
00147
00151 class Input {
00152
00153 public:
00154
00156 Input( ) { }
00157 Input( const String &file ) { SetFileName(file); }
00158
00159 ~Input() { }
00160
00161 void SetFileName( const String &file ) { fFileName=file; }
00162
00163 protected:
00164
00166 int OpenFile( ifstream &inFile ) const;
00167
00169 void CloseFile( ifstream &inFile ) const { inFile.close(); }
00170
00174 int GotoSection( ifstream &inFile, const char *section ) const;
00175
00178 int GotoNextSection( ifstream &inFile, const char *section ) const;
00179
00180 protected:
00181 String fFileName;
00182
00183 };
00184
00185
00186 int Input::OpenFile( ifstream &inFile ) const
00187 {
00188 inFile.open( fFileName.c_str(), ios::in );
00189 if ( !inFile ) {
00190 WarningMessage("Input::OpenFile()","Could not open file");
00191 return 1;
00192 }
00193 return 0;
00194 }
00195
00196 int Input::GotoSection( ifstream &inFile, const char *section ) const
00197 {
00198 inFile.seekg( 0 );
00199 return GotoNextSection( inFile, section );
00200 }
00201
00202
00203 int Input::GotoNextSection( ifstream &inFile, const char *section ) const
00204 {
00205
00206
00207 char wholeWord[40]="not", wholeLine[88];
00208
00209 while ( !inFile.eof() && strcmp(wholeWord,section) != 0 ) {
00210 inFile >> wholeWord;
00211
00212 if ( strncmp(wholeWord,"#",1) != 0 )
00213 inFile.getline( wholeLine,88);
00214 }
00215
00216 if ( inFile.eof() ) {
00217 WarningMessage("void Input::GotoNextSection",
00218 "Went to end of file. Did not find section");
00219 return 1;
00220 }
00221 return 0;
00222 }
00223
00224 }
00225
00226
00227 #endif