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