Geant4.10
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions
G4tgrFileIn Class Reference

#include <G4tgrFileIn.hh>

Public Member Functions

 G4tgrFileIn ()
 
 ~G4tgrFileIn ()
 
G4int GetWordsInLine (std::vector< G4String > &wl)
 
void ErrorInLine ()
 
G4int Nline ()
 
const G4StringGetName ()
 
void OpenNewFile (const char *filename)
 
G4bool EndOfFile ()
 
void Close ()
 
void DumpException (const G4String &sent)
 

Static Public Member Functions

static G4tgrFileInGetInstance (const G4String &name)
 
static G4tgrFileInGetInstanceOpened (const G4String &name)
 

Detailed Description

Definition at line 46 of file G4tgrFileIn.hh.

Constructor & Destructor Documentation

G4tgrFileIn::G4tgrFileIn ( )

Definition at line 50 of file G4tgrFileIn.cc.

Referenced by GetInstance().

51  : theCurrentFile(-1), theName("")
52 {
53  if (!theInstances) { theInstances = new std::vector<G4tgrFileIn*>; }
54 }
G4tgrFileIn::~G4tgrFileIn ( )

Definition at line 58 of file G4tgrFileIn.cc.

59 {
60  delete theInstances; theInstances=0;
61 /*
62  std::vector<G4tgrFileIn*>::const_iterator vfcite;
63  for( vfcite = theInstances->begin(); vfcite != theInstances->end(); vfcite++)
64  {
65  delete *vfcite;
66  }
67 */
68 }

Member Function Documentation

void G4tgrFileIn::Close ( )

Definition at line 416 of file G4tgrFileIn.cc.

References G4cout, G4endl, and G4tgrMessenger::GetVerboseLevel().

Referenced by EndOfFile().

417 {
418 #ifdef G4VERBOSE
420  {
421  G4cout << "G4tgrFileIn::Close() - "
422  << theCurrentFile << ", size " << theFiles.size() << G4endl;
423  }
424 #endif
425 
426  theFiles[theCurrentFile+1]->close();
427  theFiles.pop_back();
428 }
G4GLOB_DLL std::ostream G4cout
static G4int GetVerboseLevel()
#define G4endl
Definition: G4ios.hh:61
void G4tgrFileIn::DumpException ( const G4String sent)

Definition at line 432 of file G4tgrFileIn.cc.

References G4UIcommand::ConvertToString(), FatalException, and G4Exception().

Referenced by GetWordsInLine(), and G4tgrFileReader::ReadFiles().

433 {
434  G4String Err1 = sent + " in file " + theName;
435  G4String Err2 = " line No: "
436  + G4UIcommand::ConvertToString(theLineNo[theCurrentFile]);
437  G4String ErrMessage = Err1;
438  G4Exception("G4tgrFileIn::DumpException()", "FileError",
439  FatalException, ErrMessage);
440 }
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:357
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool G4tgrFileIn::EndOfFile ( )

Definition at line 377 of file G4tgrFileIn.cc.

References Close(), G4cout, G4endl, and G4tgrMessenger::GetVerboseLevel().

Referenced by GetWordsInLine().

378 {
379  G4bool isok = theFiles[theCurrentFile]->eof();
380  if( isok )
381  {
382 #ifdef G4VERBOSE
384  {
385  G4cout << " G4tgrFileIn::EndOfFile() - EOF: "
386  << theCurrentFile << G4endl;
387  }
388 #endif
389  theCurrentFile--;
390  if( theCurrentFile != -1 ) // Last file will be closed by the user
391  {
392  Close();
393  }
394  }
395 
396  // Only real closing if all files are closed
397 #ifdef G4VERBOSE
399  {
400  G4cout << " G4tgrFileIn::EndOfFile() - EOF: "
401  << isok << " " << theCurrentFile << G4endl;
402  }
403 #endif
404  if( theCurrentFile != -1 )
405  {
406  return 0;
407  }
408  else
409  {
410  return isok;
411  }
412 }
G4GLOB_DLL std::ostream G4cout
static G4int GetVerboseLevel()
bool G4bool
Definition: G4Types.hh:79
void Close()
Definition: G4tgrFileIn.cc:416
#define G4endl
Definition: G4ios.hh:61
void G4tgrFileIn::ErrorInLine ( )

Definition at line 368 of file G4tgrFileIn.cc.

References G4cerr.

Referenced by GetWordsInLine().

369 {
370  G4cerr << "!! EXITING: ERROR IN LINE No "
371  << theLineNo[theCurrentFile] << " file: "
372  << theNames[theCurrentFile] << " : ";
373 }
G4GLOB_DLL std::ostream G4cerr
G4tgrFileIn & G4tgrFileIn::GetInstance ( const G4String name)
static

Definition at line 72 of file G4tgrFileIn.cc.

References G4tgrFileIn(), and OpenNewFile().

Referenced by GetInstanceOpened(), and G4tgrFileReader::ReadFiles().

73 {
74  if (!theInstances) { theInstances = new std::vector<G4tgrFileIn*>; }
75 
76  std::vector<G4tgrFileIn*>::const_iterator vfcite;
77  for( vfcite = theInstances->begin(); vfcite != theInstances->end(); vfcite++)
78  {
79  if( (*vfcite)->GetName() == filename)
80  {
81  return *(*vfcite);
82  }
83  }
84 
85  G4tgrFileIn* instance = 0;
86  if( vfcite == theInstances->end() )
87  {
88  instance = new G4tgrFileIn( filename );
89 
90  instance->theCurrentFile = -1;
91  instance->OpenNewFile( filename.c_str() );
92 
93  theInstances->push_back( instance );
94  }
95 
96  return *instance;
97 }
void OpenNewFile(const char *filename)
Definition: G4tgrFileIn.cc:101
G4tgrFileIn & G4tgrFileIn::GetInstanceOpened ( const G4String name)
static

Definition at line 123 of file G4tgrFileIn.cc.

References FatalException, G4Exception(), GetInstance(), and GetName().

124 {
125  G4tgrFileIn& filein = G4tgrFileIn::GetInstance(filename);
126  if (filein.GetName() != filename )
127  {
128  G4String ErrMessage = "File not opened yet: " + filename;
129  G4Exception("G4tgrFileIn::GetInstanceOpened()",
130  "InvalidInput", FatalException, ErrMessage);
131  }
132  else
133  {
134  return filein;
135  }
136  return filein; // to avoid compilation warnings
137 }
static G4tgrFileIn & GetInstance(const G4String &name)
Definition: G4tgrFileIn.cc:72
const G4String & GetName()
Definition: G4tgrFileIn.hh:69
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
const G4String& G4tgrFileIn::GetName ( void  )
inline

Definition at line 69 of file G4tgrFileIn.hh.

Referenced by GetInstanceOpened().

69 { return theName; }
G4int G4tgrFileIn::GetWordsInLine ( std::vector< G4String > &  wl)

Definition at line 141 of file G4tgrFileIn.cc.

References G4String::append(), DumpException(), EndOfFile(), ErrorInLine(), FatalException, G4cout, G4endl, G4Exception(), G4tgrMessenger::GetVerboseLevel(), and OpenNewFile().

Referenced by G4tgrFileReader::ReadFiles().

142 {
143  G4int isok = 1;
144 
145  //---------- Read a line of file:
146  // NOTE: cannot be read with a istream_iterator,
147  // because it uses G4cout, and then doesn't read '\n'
148  //----- Clear wordlist
149  G4int wsiz = wordlist.size();
150  G4int ii;
151  for (ii = 0; ii < wsiz; ii++)
152  {
153  wordlist.pop_back();
154  }
155 
156  //---------- Loop lines while there is an ending '\' or line is blank
157  const G4int NMAXLIN = 1000;
158  char ltemp[NMAXLIN]; // there won't be lines longer than NMAXLIN characters
159  for (;;)
160  {
161  (theLineNo[theCurrentFile])++;
162  for ( ii = 0; ii < NMAXLIN; ii++) { ltemp[ii] = ' '; }
163  theFiles[theCurrentFile]->getline( ltemp, NMAXLIN );
164 
165  //---------- Check for lines longer than NMAXLIN character
166  for ( ii=0; ii < NMAXLIN; ii++)
167  {
168  if ( ltemp[ii] == '\0' ) { break; }
169  }
170  if ( ii == NMAXLIN-1 )
171  {
172  ErrorInLine();
173  G4String ErrMessage = "Too long line. Please split it "
174  + G4String("putting a '\\' at the end!");
175  G4Exception("G4tgrFileIn::GetWordsInLine()", "InvalidInput",
176  FatalException, ErrMessage);
177  }
178 
179  //---------- End of file
180  if ( EndOfFile() )
181  {
182  return 0;
183  }
184 
185  //---------- Convert line read to istrstream to split it in words
186  std::istringstream istr_line(ltemp);
187 
188  //--------- Count how many words are there in ltemp
189  // this shouln't be needed, but SUN compiler has problems...
190  G4int NoWords = 0;
191  char* tt = ltemp;
192 
193  G4String stemp(ltemp);
194  do
195  {
196  if( *tt != ' ' && *(tt) != '\0' )
197  {
198  if( tt == ltemp)
199  {
200  NoWords++;
201 #ifdef G4VERBOSE
203  {
204  G4cout << "G4tgrFileIn::GetWordsInLine() - NoWords"
205  << NoWords << ltemp << G4endl;
206  }
207 #endif
208  }
209  else if( *(tt-1) == ' ' || *(tt-1) == '\015' || *(tt-1) == '\t')
210  {
211  NoWords++;
212 #ifdef G4VERBOSE
214  {
215  G4cout << "G4tgrFileIn::GetWordsInLine() - NoWords"
216  << NoWords << ltemp << G4endl;
217  }
218 #endif
219  }
220  }
221  tt++;
222  } while((*tt != '\0') && (stemp.length()!=0));
223 
224  if(stemp.length() == 0) { NoWords = 0; }
225 
226  //--------- Read words from istr_line and write them into wordlist
227  for( ii=0; ii < NoWords; ii++)
228  {
229  stemp = "";
230  istr_line >> stemp;
231  if ( stemp.length() == 0 ) { break; }
232  G4int comment = stemp.find(G4String("//") );
233 #ifdef G4VERBOSE
235  {
236  G4cout << "!!!COMMENT" << comment << stemp.c_str() << G4endl;
237  }
238 #endif
239  if ( comment == 0 )
240  {
241  break;
242  }
243  else if ( comment > 0 )
244  {
245  stemp = stemp.substr( 0, comment );
246  wordlist.push_back(stemp);
247  break;
248  }
249  wordlist.push_back(stemp);
250  }
251 
252  // These two algorithms should be the more STL-like way, but they don't
253  // work for files whose lines end without '\015'=TAB (STL problem: doesn't
254  // find end of string??):
255  // istream_iterator<G4String, ptrdiff_t> G4String_iter(istr_line);
256  // istream_iterator<G4String, ptrdiff_t> eosl;
257  // copy(G4String_iter, eosl, back_inserter(wordlist));
258  // typedef istream_iterator<G4String, ptrdiff_t> G4String_iter;
259  // copy(G4String_iter(istr_line), G4String_iter(), back_inserter(wordlist));
260 
261  if ( wordlist.size() != 0 )
262  {
263  if( (*(wordlist.end()-1)).compare("\\") == 0 ) // use '\' to mark
264  { // continuing line
265  wordlist.pop_back();
266  }
267  else
268  {
269  break;
270  }
271  }
272  }
273 
274  //--------- A pair of double quotes delimits a word, therefore, look for the
275  // case where there is more than one word between two double quotes
276  std::vector<G4String> wordlist2;
277  G4String wordq = "";
278  unsigned int imerge = 0;
279  for( size_t jj = 0; jj < wordlist.size(); jj++)
280  {
281  if( wordlist[jj].substr(0,1) == "\"" )
282  {
283  imerge = 1;
284  }
285  if( wordlist[jj][ wordlist[jj].size()-1 ] == '\"' )
286  {
287  if( imerge != 1 )
288  {
289  G4String err1 = " word with trailing '\"' while there is no";
290  G4String err2 = " previous word with leading '\"' in line ";
291  G4String err = err1 + err2;
292  DumpException(err);
293  }
294  imerge = 2;
295  }
296  if( imerge == 0 )
297  {
298  wordlist2.push_back( wordlist[jj] );
299  }
300  else if( imerge == 1 )
301  {
302  if( wordq == "" )
303  {
304  wordq.append( wordlist[jj].substr(1,wordlist[jj].size()) );
305  }
306  else
307  {
308  wordq.append( wordlist[jj].substr(0,wordlist[jj].size()) );
309  }
310  wordq.append(" ");
311  }
312  else if( imerge == 2 )
313  {
314  if( wordq == "" )
315  {
316  wordq.append( wordlist[jj].substr(1,wordlist[jj].size()-2));
317  }
318  else
319  {
320  wordq.append( wordlist[jj].substr(0,wordlist[jj].size()-1) );
321  }
322  wordlist2.push_back( wordq );
323  wordq = "";
324  imerge = 0;
325  }
326  }
327  if( imerge == 1 )
328  {
329  G4String err1 = " word with leading '\"' in line while there is no";
330  G4String err2 = " later word with trailing '\"' in line ";
331  G4String err = err1 + err2;
332  DumpException(err);
333  }
334 
335  wordlist = wordlist2;
336 
337  // Or why not like this (?):
338  // typedef std::istream_iterator<G4String, ptrdiff_t> string_iter;
339  // std::copy(string_iter(istr_line), string_iter(), back_inserter(wordlist));
340 
341  // check if including a new file
342  if( wordlist[0] == "#include" )
343  {
344  if( wordlist.size() != 2 )
345  {
346  ErrorInLine();
347  G4String ErrMessage
348  = "'#include' should have as second argument, the filename !";
349  G4Exception("G4tgrFileIn::GetWordsInLine()", "InvalidInput",
350  FatalException, ErrMessage);
351  }
352 
353 #ifdef G4VERBOSE
355  {
356  G4cout << " G4tgrFileIn::GetWordsInLine() - Include found !" << G4endl;
357  }
358 #endif
359  OpenNewFile( wordlist[1].c_str() );
360  isok = GetWordsInLine( wordlist);
361  }
362 
363  return isok;
364 }
G4int GetWordsInLine(std::vector< G4String > &wl)
Definition: G4tgrFileIn.cc:141
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
static G4int GetVerboseLevel()
void DumpException(const G4String &sent)
Definition: G4tgrFileIn.cc:432
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
void OpenNewFile(const char *filename)
Definition: G4tgrFileIn.cc:101
G4String & append(const G4String &)
void ErrorInLine()
Definition: G4tgrFileIn.cc:368
G4bool EndOfFile()
Definition: G4tgrFileIn.cc:377
#define G4endl
Definition: G4ios.hh:61
G4int G4tgrFileIn::Nline ( )
inline

Definition at line 67 of file G4tgrFileIn.hh.

67 { return theLineNo[theCurrentFile]; }
void G4tgrFileIn::OpenNewFile ( const char *  filename)

Definition at line 101 of file G4tgrFileIn.cc.

References FatalException, and G4Exception().

Referenced by GetInstance(), and GetWordsInLine().

102 {
103  theCurrentFile++;
104  std::ifstream* fin = new std::ifstream(filename);
105  theFiles.push_back(fin);
106 
107  theLineNo.push_back( 0 );
108 
109  theNames.push_back( filename );
110 
111 #ifndef OS_SUN_4_2
112  if( !fin->is_open() )
113  {
114  G4String ErrMessage = "Input file does not exist: " + G4String(filename);
115  G4Exception("G4tgrFileIn::OpenNewFile()",
116  "InvalidInput", FatalException, ErrMessage);
117  }
118 #endif
119 }
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41

The documentation for this class was generated from the following files: