Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
00010
00011 #include <stdlib.h>
00012 #include <strstream>
00013
00014
00015
00016
00017 std::vector<ALIFileIn*> ALIFileIn::theInstances;
00018
00019
00020
00021
00022
00023 ALIFileIn& ALIFileIn::getInstance( const ALIstring& filename )
00024 {
00025 std::vector<ALIFileIn*>::const_iterator vfcite;
00026 for( vfcite = theInstances.begin(); vfcite != theInstances.end(); vfcite++) {
00027 if( (*vfcite)->name() == filename) {
00028 return *(*vfcite);
00029 }
00030 }
00031
00032 ALIFileIn* instance = 0;
00033 if( vfcite == theInstances.end() ) {
00034 instance = new ALIFileIn( filename );
00035
00036 instance->theCurrentFile = -1;
00037 instance->openNewFile( filename.c_str() );
00038
00039 theInstances.push_back( instance );
00040 }
00041
00042 return *instance;
00043
00044 }
00045
00046
00047
00048 void ALIFileIn::openNewFile( const char* filename )
00049 {
00050 theCurrentFile++;
00051 std::ifstream* fin = new std::ifstream(filename);
00052 theFiles.push_back(fin);
00053
00054
00055
00056 theLineNo.push_back( 0 );
00057
00058 theNames.push_back( filename );
00059
00060 #ifndef OS_SUN_4_2
00061 if( !fin->is_open()) {
00062 std::cerr << "!!!! Input file does not exist: " << filename << std::endl;
00063 exit(1);
00064 }
00065 #endif
00066 }
00067
00068
00069
00070
00071
00072
00073 ALIFileIn& ALIFileIn::getInstanceOpened( const ALIstring& filename )
00074 {
00075 ALIFileIn& filein = ALIFileIn::getInstance(filename);
00076 if (filein.name() != filename ) {
00077 std::cerr << "Error: file not opened yet " << filename << std::endl;
00078 exit(0);
00079 } else {
00080 return filein;
00081 }
00082 }
00083
00084
00085
00086
00087
00088
00089
00090 ALIint ALIFileIn::getWordsInLine(std::vector<ALIstring>& wordlist)
00091 {
00092 ALIint isok = 1;
00093
00094
00095
00096
00097 ALIint wsiz = wordlist.size();
00098 ALIint ii;
00099 for (ii = 0; ii < wsiz; ii++) {
00100 wordlist.pop_back();
00101 }
00102
00103
00104 const ALIint NMAXLIN = 1000;
00105 char ltemp[NMAXLIN];
00106 for (;;) {
00107 (theLineNo[theCurrentFile])++;
00108 for( ii = 0; ii < NMAXLIN; ii++) ltemp[ii] = ' ';
00109 theFiles[theCurrentFile]->getline( ltemp, NMAXLIN );
00110
00111 ALIint ii;
00112 for ( ii=0; ii < NMAXLIN; ii++) {
00113 if ( ltemp[ii] == '\0' ) break;
00114 }
00115 if ( ii == NMAXLIN-1 ) {
00116 ErrorInLine();
00117 std::cerr << "!!!! line longer than " << NMAXLIN << " characters" <<
00118 std::endl << " please split it putting a '\\' at the end of line" << std::endl;
00119 exit(0);
00120 }
00121
00122
00123
00124 if ( eof() ) {
00125
00126 return 0;
00127 }
00128
00129
00130 std::istrstream istr_line(ltemp);
00131
00132
00133 ALIint NoWords = 0;
00134 char* tt = ltemp;
00135 ALIstring stemp(ltemp);
00136 do{
00137 if( *tt != ' ' && *(tt) != '\0' ) {
00138 if( tt == ltemp) {
00139 NoWords++;
00140
00141 } else if( *(tt-1) == ' ' || *(tt-1) == '\015' || *(tt-1) == '\t') {
00142 NoWords++;
00143
00144 }
00145 }
00146 tt++;
00147 }while(*tt != '\0' && stemp.length()!=0);
00148 ALIstring stempt (ltemp);
00149 if(stempt.length() == 0) NoWords = 0;
00150
00151
00152
00153 for( ii=0; ii < NoWords; ii++) {
00154 ALIstring stemp = "";
00155 istr_line >> stemp;
00156 if ( stemp.length() == 0 ) break;
00157 ALIint comment = stemp.find(ALIstring("//") );
00158
00159 if ( comment == 0 ) {
00160 break;
00161 } else if ( comment > 0 ) {
00162 stemp = stemp.substr( 0, comment );
00163 wordlist.push_back(stemp);
00164 break;
00165
00166 }
00167 wordlist.push_back(stemp);
00168 }
00169
00170
00171
00172
00173
00174
00175
00176
00177 if ( wordlist.size() != 0 ) {
00178 if( (*(wordlist.end()-1)).compare("\\") == 0 ) {
00179 wordlist.pop_back();
00180 } else {
00181 break;
00182 }
00183 }
00184 }
00185
00186
00187
00188
00189
00190
00191
00192 if( wordlist[0] == "#include" ) {
00193 if( wordlist.size() != 2 ) {
00194 ErrorInLine();
00195 std::cerr << "'#include' should have as second argument the filename " << std::endl;
00196 exit(0);
00197 }
00198
00199 openNewFile( wordlist[1].c_str() );
00200 isok = getWordsInLine( wordlist);
00201
00202 }
00203
00204 return isok;
00205 }
00206
00207
00208
00209
00210
00211 void ALIFileIn::ErrorInLine()
00212 {
00213 std::cerr << "!! EXITING: ERROR IN LINE No " << theLineNo[theCurrentFile] << " file: " << theNames[theCurrentFile] << " : ";
00214
00215 }
00216
00217
00218 ALIbool ALIFileIn::eof()
00219 {
00220 ALIbool isok = theFiles[theCurrentFile]->eof();
00221 if( isok ) {
00222
00223 theCurrentFile--;
00224 if( theCurrentFile != -1 ) close();
00225 }
00226
00227
00228 if( theCurrentFile != -1 ) {
00229 return 0;
00230 } else {
00231 return isok;
00232 }
00233 }
00234
00235
00236 void ALIFileIn::close()
00237 {
00238
00239
00240
00241
00242
00243
00244 theFiles[theCurrentFile+1]->close();
00245 theFiles.pop_back();
00246
00247 }