Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:38:45

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 <strstream>
0013 //#include <strstream.h>

0014 
0015 //#include <algo.h>

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

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

0021 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

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

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

0044   //-  ALIint lineno = 0;

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

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

0059 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

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

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

0072 //@@ returns

0073 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

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

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

0079   //----- Clear wordlist

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

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

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

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

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

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

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

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

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

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

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

0138     //    ALIint stre = 1;

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

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

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

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

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

0159     // istream_iterator<ALIstring, ptrdiff_t> eosl;

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

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

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

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

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

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

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

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

0178   // check if including a new file

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

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

0194 //@@

0195 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

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

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

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

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

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

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

0221     ErrorInLine();

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

0223     //    exit(0);

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

0228 }