CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Alignment/CocoaUtilities/src/ALIFileIn.cc

Go to the documentation of this file.
00001 //
00002 //   COCOA class implementation file
00003 //Id:  ALIFileIn.cc
00004 //CAT: Model
00005 //
00006 //   History: v1.0 
00007 //   Pedro Arce
00008 
00009 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
00010 
00011 #include <stdlib.h>
00012 #include <strstream>
00013 //#include <strstream.h>
00014 
00015 //#include <algo.h>
00016 
00017 std::vector<ALIFileIn*> ALIFileIn::theInstances;
00018 
00019 
00020 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00021 //@@ get the instance of file with name filename
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   //-  ALIint lineno = new ALIint;
00055   //-  ALIint lineno = 0;
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 //@@ get the Instance checking that the file is already opened
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 //@@ read a ilne and split it in words 
00088 //@@ returns 
00089 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
00090 ALIint ALIFileIn::getWordsInLine(std::vector<ALIstring>& wordlist)
00091 {
00092   ALIint isok = 1;
00093 
00094   //---------- Read a line of file:
00095   //@@@@--- Cannot be read with a istream_iterator, becasuse it uses std::cout, and then doesn't read '\n'
00096   //----- Clear wordlist
00097   ALIint wsiz = wordlist.size();
00098   ALIint ii;
00099   for (ii = 0; ii < wsiz; ii++) {
00100     wordlist.pop_back();
00101   } 
00102 
00103   //---------- Loop lines while there is an ending '\' or line is blank   
00104   const ALIint NMAXLIN = 1000;
00105   char ltemp[NMAXLIN]; //there won't be lines longer than NMAXLIN characters
00106   for (;;) {
00107     (theLineNo[theCurrentFile])++;
00108     for( ii = 0; ii < NMAXLIN; ii++) ltemp[ii] = ' ';
00109     theFiles[theCurrentFile]->getline( ltemp, NMAXLIN ); 
00110     //---------- Check for lines longer than NMAXLIN character
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     //---------- End of file
00123     //-    if ( theFiles[theCurrentFile]->eof() ) {
00124     if ( eof() ) {
00125       //t          exit(0);
00126       return 0;
00127     }
00128     
00129     //---------- Convert line read to istrstream to split it in words 
00130     std::istrstream istr_line(ltemp);
00131      
00132     //--------- count how many words are there in ltemp (this sohuld not be needed, but sun compiler has problems) !! this has to be nvestigated...
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           //     std::cout << "dNoWords" << NoWords << ltemp << std::endl;
00141         } else if( *(tt-1) == ' ' ||  *(tt-1) == '\015' ||  *(tt-1) == '\t') {
00142           NoWords++; 
00143           //     std::cout << "NoWords" << NoWords << ltemp << std::endl;
00144         }
00145       }
00146       tt++;
00147     }while(*tt != '\0' && stemp.length()!=0);
00148     ALIstring stempt (ltemp);
00149     if(stempt.length() == 0) NoWords = 0;
00150     
00151     //--------- Read words from istr_line and write them into wordlist
00152     //    ALIint stre = 1;
00153     for( ii=0; ii < NoWords; ii++) {
00154       ALIstring stemp = "";
00155       istr_line >> stemp;   //?? gives warning in Insure++
00156       if ( stemp.length() == 0 ) break;
00157       ALIint comment = stemp.find(ALIstring("//") );
00158       //    std::cout << "!!!COMMENT" << comment << stemp.c_str() << std::endl;
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         //-   for( int jj=0; jj < stemp.length()-comment; jj++) stemp.pop_back();
00166       } 
00167       wordlist.push_back(stemp);
00168     }
00169     
00170     //These two algorithms should be the more STL-like way, but they don't work for files whose lines end without '\015'=TAB (STL problem: doesn't find end of string??)
00171     // istream_iterator<ALIstring, ptrdiff_t> ALIstring_iter(istr_line);
00172     // istream_iterator<ALIstring, ptrdiff_t> eosl;
00173     // copy(ALIstring_iter, eosl, back_inserter(wordlist));
00174     // typedef istream_iterator<ALIstring, ptrdiff_t> ALIstring_iter;
00175     // copy(ALIstring_iter(istr_line), ALIstring_iter(), back_inserter(wordlist));
00176     
00177     if ( wordlist.size() != 0 ) {
00178       if( (*(wordlist.end()-1)).compare("\\") == 0 ) {   //use '\' to mark continuing line  
00179         wordlist.pop_back();
00180       } else {
00181         break;
00182       }
00183     }
00184   }
00185   
00186   //or why not like this?:
00187   //typedef istream_iterator<ALIstring, ptrdiff_t> string_iter;
00188   //copy(string_iter(istr_line), string_iter(), back_inserter(wordlist));
00189   
00190   //-  std::cout << " checking for include " << wordlist[0] << std::endl;
00191   // check if including a new file
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     //-    std::cout << "include found " << std::endl;
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     //std::cout << " eof theCurrentFile " << theCurrentFile << std::endl;
00223     theCurrentFile--;
00224     if( theCurrentFile != -1 ) close();  // last file will be closed by the user
00225   }
00226   //only real closing if all files are closed
00227   //-  std::cout << " eof " << isok << " " << theCurrentFile << std::endl;
00228   if( theCurrentFile != -1 ) { 
00229     return 0;
00230   } else {
00231     return isok;
00232   }
00233 }
00234 
00235 
00236 void ALIFileIn::close()
00237 {
00238   //-  std::cout << " close " << theCurrentFile << " size " << theFiles.size() << std::endl;
00239   /*  if( theCurrentFile+1 != 0 ) {
00240     ErrorInLine();
00241     std::cerr << "trying to close file while reading other files included in it " << theCurrentFile+1 << std::endl;
00242     //    exit(0);
00243     } else { */
00244     theFiles[theCurrentFile+1]->close();
00245     theFiles.pop_back();
00246     //  }
00247 }