Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:05

0001 //

0002 //   COCOA class implementation file

0003 //Id:  ALIFileIn.cc

0004 //CAT: Model

0005 //

0006 //   History: v1.0

0007 //   Pedro Arce

0008 
0009 #include "Alignment/CocoaUtilities/interface/ALIFileIn.h"
0010 
0011 #include <cstdlib>
0012 #include <sstream>
0013 
0014 //#include <algo.h>

0015 
0016 std::vector<ALIFileIn*> ALIFileIn::theInstances;
0017 
0018 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0019 //@@ get the instance of file with name filename

0020 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0021 ALIFileIn& ALIFileIn::getInstance(const ALIstring& filename) {
0022   for (auto vfc : theInstances) {
0023     if (vfc->name() == filename) {
0024       return *vfc;
0025     }
0026   }
0027 
0028   ALIFileIn* instance = new ALIFileIn(filename);
0029   instance->theCurrentFile = -1;
0030   instance->openNewFile(filename.c_str());
0031   theInstances.push_back(instance);
0032 
0033   return *instance;
0034 }
0035 
0036 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0037 void ALIFileIn::openNewFile(const char* filename) {
0038   theCurrentFile++;
0039   std::ifstream* fin = new std::ifstream(filename);
0040   theFiles.push_back(fin);
0041 
0042   //-  ALIint lineno = new ALIint;

0043   //-  ALIint lineno = 0;

0044   theLineNo.push_back(0);
0045 
0046   theNames.push_back(filename);
0047 
0048 #ifndef OS_SUN_4_2
0049   if (!fin->is_open()) {
0050     std::cerr << "!!!! Input file does not exist: " << filename << std::endl;
0051     exit(1);
0052   }
0053 #endif
0054 }
0055 
0056 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0057 //@@ get the Instance checking that the file is already opened

0058 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0059 ALIFileIn& ALIFileIn::getInstanceOpened(const ALIstring& filename) {
0060   ALIFileIn& filein = ALIFileIn::getInstance(filename);
0061   if (filein.name() != filename) {
0062     std::cerr << "Error: file not opened yet " << filename << std::endl;
0063     exit(0);
0064   } else {
0065     return filein;
0066   }
0067 }
0068 
0069 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0070 //@@ read a ilne and split it in words

0071 //@@ returns

0072 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0073 ALIint ALIFileIn::getWordsInLine(std::vector<ALIstring>& wordlist) {
0074   ALIint isok = 1;
0075 
0076   //---------- Read a line of file:

0077   //@@@@--- Cannot be read with a istream_iterator, becasuse it uses std::cout, and then doesn't read '\n'

0078   //----- Clear wordlist

0079   ALIint wsiz = wordlist.size();
0080   ALIint ii;
0081   for (ii = 0; ii < wsiz; ii++) {
0082     wordlist.pop_back();
0083   }
0084 
0085   //---------- Loop lines while there is an ending '\' or line is blank

0086   const ALIint NMAXLIN = 1000;
0087   char ltemp[NMAXLIN];  //there won't be lines longer than NMAXLIN characters

0088   for (;;) {
0089     (theLineNo[theCurrentFile])++;
0090     for (ii = 0; ii < NMAXLIN; ii++)
0091       ltemp[ii] = ' ';
0092     theFiles[theCurrentFile]->getline(ltemp, NMAXLIN);
0093     //---------- Check for lines longer than NMAXLIN character

0094     ALIint ii;
0095     for (ii = 0; ii < NMAXLIN; ii++) {
0096       if (ltemp[ii] == '\0')
0097         break;
0098     }
0099     if (ii == NMAXLIN - 1) {
0100       ErrorInLine();
0101       std::cerr << "!!!! line longer than " << NMAXLIN << " characters" << std::endl
0102                 << " please split it putting a '\\' at the end of line" << std::endl;
0103       exit(0);
0104     }
0105 
0106     //---------- End of file

0107     //-    if ( theFiles[theCurrentFile]->eof() ) {

0108     if (eof()) {
0109       //t          exit(0);

0110       return 0;
0111     }
0112 
0113     //---------- Convert line read to istrstream to split it in words

0114     std::istringstream istr_line(ltemp);
0115 
0116     //--------- count how many words are there in ltemp (this sohuld not be needed, but sun compiler has problems) !! this has to be nvestigated...

0117     ALIint NoWords = 0;
0118     char* tt = ltemp;
0119     ALIstring stemp(ltemp);
0120     do {
0121       if (*tt != ' ' && *(tt) != '\0') {
0122         if (tt == ltemp) {
0123           NoWords++;
0124           //     std::cout << "dNoWords" << NoWords << ltemp << std::endl;

0125         } else if (*(tt - 1) == ' ' || *(tt - 1) == '\015' || *(tt - 1) == '\t') {
0126           NoWords++;
0127           //     std::cout << "NoWords" << NoWords << ltemp << std::endl;

0128         }
0129       }
0130       tt++;
0131     } while (*tt != '\0' && stemp.length() != 0);
0132     ALIstring stempt(ltemp);
0133     if (stempt.length() == 0)
0134       NoWords = 0;
0135 
0136     //--------- Read words from istr_line and write them into wordlist

0137     //    ALIint stre = 1;

0138     for (ii = 0; ii < NoWords; ii++) {
0139       ALIstring stemp = "";
0140       istr_line >> stemp;  //?? gives warning in Insure++

0141       if (stemp.length() == 0)
0142         break;
0143       ALIint comment = stemp.find(ALIstring("//"));
0144       //    std::cout << "!!!COMMENT" << comment << stemp.c_str() << std::endl;

0145       if (comment == 0) {
0146         break;
0147       } else if (comment > 0) {
0148         stemp = stemp.substr(0, comment);
0149         wordlist.push_back(stemp);
0150         break;
0151         //-   for( int jj=0; jj < stemp.length()-comment; jj++) stemp.pop_back();

0152       }
0153       wordlist.push_back(stemp);
0154     }
0155 
0156     //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??)

0157     // istream_iterator<ALIstring, ptrdiff_t> ALIstring_iter(istr_line);

0158     // istream_iterator<ALIstring, ptrdiff_t> eosl;

0159     // copy(ALIstring_iter, eosl, back_inserter(wordlist));

0160     // typedef istream_iterator<ALIstring, ptrdiff_t> ALIstring_iter;

0161     // copy(ALIstring_iter(istr_line), ALIstring_iter(), back_inserter(wordlist));

0162 
0163     if (!wordlist.empty()) {
0164       if ((*(wordlist.end() - 1)) == "\\") {  //use '\' to mark continuing line

0165         wordlist.pop_back();
0166       } else {
0167         break;
0168       }
0169     }
0170   }
0171 
0172   //or why not like this?:

0173   //typedef istream_iterator<ALIstring, ptrdiff_t> string_iter;

0174   //copy(string_iter(istr_line), string_iter(), back_inserter(wordlist));

0175 
0176   //-  std::cout << " checking for include " << wordlist[0] << std::endl;

0177   // check if including a new file

0178   if (wordlist[0] == "#include") {
0179     if (wordlist.size() != 2) {
0180       ErrorInLine();
0181       std::cerr << "'#include' should have as second argument the filename " << std::endl;
0182       exit(0);
0183     }
0184     //-    std::cout << "include found " << std::endl;

0185     openNewFile(wordlist[1].c_str());
0186     isok = getWordsInLine(wordlist);
0187   }
0188 
0189   return isok;
0190 }
0191 
0192 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0193 //@@

0194 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

0195 void ALIFileIn::ErrorInLine() {
0196   std::cerr << "!! EXITING: ERROR IN LINE No " << theLineNo[theCurrentFile] << " file: " << theNames[theCurrentFile]
0197             << " : ";
0198 }
0199 
0200 ALIbool ALIFileIn::eof() {
0201   ALIbool isok = theFiles[theCurrentFile]->eof();
0202   if (isok) {
0203     //std::cout << " eof theCurrentFile " << theCurrentFile << std::endl;

0204     theCurrentFile--;
0205     if (theCurrentFile != -1)
0206       close();  // last file will be closed by the user

0207   }
0208   //only real closing if all files are closed

0209   //-  std::cout << " eof " << isok << " " << theCurrentFile << std::endl;

0210   if (theCurrentFile != -1) {
0211     return false;
0212   } else {
0213     return isok;
0214   }
0215 }
0216 
0217 void ALIFileIn::close() {
0218   //-  std::cout << " close " << theCurrentFile << " size " << theFiles.size() << std::endl;

0219   /*  if( theCurrentFile+1 != 0 ) {

0220     ErrorInLine();

0221     std::cerr << "trying to close file while reading other files included in it " << theCurrentFile+1 << std::endl;

0222     //    exit(0);

0223     } else { */
0224   theFiles[theCurrentFile + 1]->close();
0225   theFiles.pop_back();
0226   //  }

0227 }