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