CMS 3D CMS Logo

ALIFileIn.cc
Go to the documentation of this file.
1 //
2 // COCOA class implementation file
3 //Id: ALIFileIn.cc
4 //CAT: Model
5 //
6 // History: v1.0
7 // Pedro Arce
8 
10 
11 #include <cstdlib>
12 #include <strstream>
13 //#include <strstream.h>
14 
15 //#include <algo.h>
16 
17 std::vector<ALIFileIn*> ALIFileIn::theInstances;
18 
19 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
20 //@@ get the instance of file with name filename
21 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
23  for (auto vfc : theInstances) {
24  if (vfc->name() == filename) {
25  return *vfc;
26  }
27  }
28 
30  instance->theCurrentFile = -1;
31  instance->openNewFile(filename.c_str());
32  theInstances.push_back(instance);
33 
34  return *instance;
35 }
36 
37 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
38 void ALIFileIn::openNewFile(const char* filename) {
40  std::ifstream* fin = new std::ifstream(filename);
41  theFiles.push_back(fin);
42 
43  //- ALIint lineno = new ALIint;
44  //- ALIint lineno = 0;
45  theLineNo.push_back(0);
46 
47  theNames.push_back(filename);
48 
49 #ifndef OS_SUN_4_2
50  if (!fin->is_open()) {
51  std::cerr << "!!!! Input file does not exist: " << filename << std::endl;
52  exit(1);
53  }
54 #endif
55 }
56 
57 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
58 //@@ get the Instance checking that the file is already opened
59 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
62  if (filein.name() != filename) {
63  std::cerr << "Error: file not opened yet " << filename << std::endl;
64  exit(0);
65  } else {
66  return filein;
67  }
68 }
69 
70 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
71 //@@ read a ilne and split it in words
72 //@@ returns
73 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
74 ALIint ALIFileIn::getWordsInLine(std::vector<ALIstring>& wordlist) {
75  ALIint isok = 1;
76 
77  //---------- Read a line of file:
78  //@@@@--- Cannot be read with a istream_iterator, becasuse it uses std::cout, and then doesn't read '\n'
79  //----- Clear wordlist
80  ALIint wsiz = wordlist.size();
81  ALIint ii;
82  for (ii = 0; ii < wsiz; ii++) {
83  wordlist.pop_back();
84  }
85 
86  //---------- Loop lines while there is an ending '\' or line is blank
87  const ALIint NMAXLIN = 1000;
88  char ltemp[NMAXLIN]; //there won't be lines longer than NMAXLIN characters
89  for (;;) {
91  for (ii = 0; ii < NMAXLIN; ii++)
92  ltemp[ii] = ' ';
93  theFiles[theCurrentFile]->getline(ltemp, NMAXLIN);
94  //---------- Check for lines longer than NMAXLIN character
95  ALIint ii;
96  for (ii = 0; ii < NMAXLIN; ii++) {
97  if (ltemp[ii] == '\0')
98  break;
99  }
100  if (ii == NMAXLIN - 1) {
101  ErrorInLine();
102  std::cerr << "!!!! line longer than " << NMAXLIN << " characters" << std::endl
103  << " please split it putting a '\\' at the end of line" << std::endl;
104  exit(0);
105  }
106 
107  //---------- End of file
108  //- if ( theFiles[theCurrentFile]->eof() ) {
109  if (eof()) {
110  //t exit(0);
111  return 0;
112  }
113 
114  //---------- Convert line read to istrstream to split it in words
115  std::istrstream istr_line(ltemp);
116 
117  //--------- count how many words are there in ltemp (this sohuld not be needed, but sun compiler has problems) !! this has to be nvestigated...
118  ALIint NoWords = 0;
119  char* tt = ltemp;
120  ALIstring stemp(ltemp);
121  do {
122  if (*tt != ' ' && *(tt) != '\0') {
123  if (tt == ltemp) {
124  NoWords++;
125  // std::cout << "dNoWords" << NoWords << ltemp << std::endl;
126  } else if (*(tt - 1) == ' ' || *(tt - 1) == '\015' || *(tt - 1) == '\t') {
127  NoWords++;
128  // std::cout << "NoWords" << NoWords << ltemp << std::endl;
129  }
130  }
131  tt++;
132  } while (*tt != '\0' && stemp.length() != 0);
133  ALIstring stempt(ltemp);
134  if (stempt.length() == 0)
135  NoWords = 0;
136 
137  //--------- Read words from istr_line and write them into wordlist
138  // ALIint stre = 1;
139  for (ii = 0; ii < NoWords; ii++) {
140  ALIstring stemp = "";
141  istr_line >> stemp; //?? gives warning in Insure++
142  if (stemp.length() == 0)
143  break;
144  ALIint comment = stemp.find(ALIstring("//"));
145  // std::cout << "!!!COMMENT" << comment << stemp.c_str() << std::endl;
146  if (comment == 0) {
147  break;
148  } else if (comment > 0) {
149  stemp = stemp.substr(0, comment);
150  wordlist.push_back(stemp);
151  break;
152  //- for( int jj=0; jj < stemp.length()-comment; jj++) stemp.pop_back();
153  }
154  wordlist.push_back(stemp);
155  }
156 
157  //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??)
158  // istream_iterator<ALIstring, ptrdiff_t> ALIstring_iter(istr_line);
159  // istream_iterator<ALIstring, ptrdiff_t> eosl;
160  // copy(ALIstring_iter, eosl, back_inserter(wordlist));
161  // typedef istream_iterator<ALIstring, ptrdiff_t> ALIstring_iter;
162  // copy(ALIstring_iter(istr_line), ALIstring_iter(), back_inserter(wordlist));
163 
164  if (!wordlist.empty()) {
165  if ((*(wordlist.end() - 1)) == "\\") { //use '\' to mark continuing line
166  wordlist.pop_back();
167  } else {
168  break;
169  }
170  }
171  }
172 
173  //or why not like this?:
174  //typedef istream_iterator<ALIstring, ptrdiff_t> string_iter;
175  //copy(string_iter(istr_line), string_iter(), back_inserter(wordlist));
176 
177  //- std::cout << " checking for include " << wordlist[0] << std::endl;
178  // check if including a new file
179  if (wordlist[0] == "#include") {
180  if (wordlist.size() != 2) {
181  ErrorInLine();
182  std::cerr << "'#include' should have as second argument the filename " << std::endl;
183  exit(0);
184  }
185  //- std::cout << "include found " << std::endl;
186  openNewFile(wordlist[1].c_str());
187  isok = getWordsInLine(wordlist);
188  }
189 
190  return isok;
191 }
192 
193 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
194 //@@
195 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
197  std::cerr << "!! EXITING: ERROR IN LINE No " << theLineNo[theCurrentFile] << " file: " << theNames[theCurrentFile]
198  << " : ";
199 }
200 
202  ALIbool isok = theFiles[theCurrentFile]->eof();
203  if (isok) {
204  //std::cout << " eof theCurrentFile " << theCurrentFile << std::endl;
205  theCurrentFile--;
206  if (theCurrentFile != -1)
207  close(); // last file will be closed by the user
208  }
209  //only real closing if all files are closed
210  //- std::cout << " eof " << isok << " " << theCurrentFile << std::endl;
211  if (theCurrentFile != -1) {
212  return false;
213  } else {
214  return isok;
215  }
216 }
217 
219  //- std::cout << " close " << theCurrentFile << " size " << theFiles.size() << std::endl;
220  /* if( theCurrentFile+1 != 0 ) {
221  ErrorInLine();
222  std::cerr << "trying to close file while reading other files included in it " << theCurrentFile+1 << std::endl;
223  // exit(0);
224  } else { */
225  theFiles[theCurrentFile + 1]->close();
226  theFiles.pop_back();
227  // }
228 }
ALIbool
bool ALIbool
Definition: CocoaGlobals.h:19
ALIFileIn::openNewFile
void openNewFile(const char *filename)
Definition: ALIFileIn.cc:38
ALIFileIn::theLineNo
std::vector< ALIint > theLineNo
Definition: ALIFileIn.h:54
ALIFileIn::theFiles
std::vector< std::ifstream * > theFiles
Definition: ALIFileIn.h:52
groupFilesInBlocks.tt
int tt
Definition: groupFilesInBlocks.py:144
ALIstring
std::string ALIstring
Definition: CocoaGlobals.h:9
ALIFileIn::getInstanceOpened
static ALIFileIn & getInstanceOpened(const ALIstring &name)
Definition: ALIFileIn.cc:60
ALIFileIn::theInstances
static std::vector< ALIFileIn * > theInstances
Definition: ALIFileIn.h:60
groupFilesInBlocks.fin
fin
Definition: groupFilesInBlocks.py:94
corrVsCorr.filename
filename
Definition: corrVsCorr.py:123
ALIFileIn::theNames
std::vector< ALIstring > theNames
Definition: ALIFileIn.h:55
ALIFileIn::getInstance
static ALIFileIn & getInstance(const ALIstring &name)
Definition: ALIFileIn.cc:22
ALIFileIn::ErrorInLine
void ErrorInLine()
Definition: ALIFileIn.cc:196
ALIFileIn::ALIFileIn
ALIFileIn()
Definition: ALIFileIn.h:22
ALIFileIn::close
void close()
Definition: ALIFileIn.cc:218
ALIFileIn::eof
ALIbool eof()
Definition: ALIFileIn.cc:201
instance
static PFTauRenderPlugin instance
Definition: PFTauRenderPlugin.cc:70
ALIint
int ALIint
Definition: CocoaGlobals.h:15
ALIFileIn::name
const ALIstring & name()
Definition: ALIFileIn.h:43
ALIFileIn::getWordsInLine
ALIint getWordsInLine(std::vector< ALIstring > &wl)
Definition: ALIFileIn.cc:74
beamvalidation.exit
def exit(msg="")
Definition: beamvalidation.py:53
EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.cerr
cerr
Definition: EcnaPython_AdcPeg12_S1_10_R170298_1_0_150_Dee0.py:8
ALIFileIn::theCurrentFile
int theCurrentFile
Definition: ALIFileIn.h:56
cuy.ii
ii
Definition: cuy.py:590
ALIFileIn.h
ALIFileIn
Definition: ALIFileIn.h:20