CMS 3D CMS Logo

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