evtgen is hosted by Hepforge, IPPP Durham
EvtGen  2.0.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
EvtParser.cpp
Go to the documentation of this file.
1 
2 /***********************************************************************
3 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors *
4 * *
5 * This file is part of EvtGen. *
6 * *
7 * EvtGen is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * EvtGen is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with EvtGen. If not, see <https://www.gnu.org/licenses/>. *
19 ***********************************************************************/
20 
21 #include "EvtGenBase/EvtParser.hh"
22 
23 #include "EvtGenBase/EvtPatches.hh"
24 #include "EvtGenBase/EvtReport.hh"
25 
26 #include <fstream>
27 #include <sstream>
28 #include <string.h>
29 using namespace std;
30 
31 #define MAXBUF 1024
32 
34 {
35  _ntoken = 0;
36  _lengthoftokenlist = 0;
37  _tokenlist = 0;
38  _linelist = 0;
39 }
40 
42 {
43  delete[] _tokenlist;
44  delete[] _linelist;
45 }
46 
48 {
49  return _ntoken;
50 }
51 
52 const std::string& EvtParser::getToken( int i )
53 {
54  return _tokenlist[i];
55 }
56 
58 {
59  return _linelist[i];
60 }
61 
62 int EvtParser::read( const std::string filename )
63 {
64  ifstream fin;
65 
66  fin.open( filename.c_str() );
67  if ( !fin ) {
68  EvtGenReport( EVTGEN_ERROR, "EvtGen" )
69  << "Could not open file '" << filename.c_str() << "'" << endl;
70  return -1;
71  }
72 
73  char buf[MAXBUF];
74  char buf2[MAXBUF];
75  char c;
76 
77  int line = 0;
78  int i;
79 
80  while ( fin.peek() != EOF ) {
81  line++;
82 
83  i = 0;
84  while ( ( c = fin.get() ) != '\n' && c != EOF && i < MAXBUF ) {
85  buf[i] = c;
86  i++;
87  }
88  if ( i == MAXBUF ) {
89  EvtGenReport( EVTGEN_ERROR, "EvtGen" )
90  << "Error in EvtParser: line:" << line << " to long" << endl;
91  } else {
92  buf[i] = '\0';
93  }
94 
95  //search for '#' which indicates comment for rest of line!
96  i = 0;
97  do {
98  if ( buf[i] == '#' )
99  buf[i] = 0;
100  i++;
101  } while ( buf[i - 1] != 0 );
102 
103  string tmp( buf, strlen( buf ) );
104 
105  //read each token
106  istringstream ist( tmp );
107  while ( ist >> buf2 ) {
108  i = 0;
109  int semicolon = 0;
110  do {
111  if ( buf2[i] == ';' ) {
112  buf2[i] = 0;
113  semicolon = 1;
114  }
115  } while ( buf2[i++] != 0 );
116  if ( buf2[0] != 0 ) {
117  addToken( line, buf2 );
118  }
119  if ( semicolon )
120  addToken( line, ";" );
121  }
122  }
123 
124  fin.close();
125 
126  return 0;
127 }
128 
129 void EvtParser::addToken( int line, const std::string& string )
130 {
131  //EvtGenReport(EVTGEN_INFO,"EvtGen") <<_ntoken<<" "<<line<<" "<<string<<endl;
132 
133  if ( _ntoken == _lengthoftokenlist ) {
134  int new_length = 1000 + 4 * _lengthoftokenlist;
135 
136  int* newlinelist = new int[new_length];
137  std::string* newtokenlist = new std::string[new_length];
138 
139  int i;
140 
141  for ( i = 0; i < _ntoken; i++ ) {
142  newlinelist[i] = _linelist[i];
143  newtokenlist[i] = _tokenlist[i];
144  }
145 
146  delete[] _tokenlist;
147  delete[] _linelist;
148 
149  _tokenlist = newtokenlist;
150  _linelist = newlinelist;
151 
152  _lengthoftokenlist = new_length;
153  }
154 
155  _tokenlist[_ntoken] = string;
156 
157  _linelist[_ntoken] = line;
158 
159  _ntoken++;
160 
161  //EvtGenReport(EVTGEN_INFO,"EvtGen") << "First:"<<_tokenlist[0]<<" last:"<<_tokenlist[_ntoken-1]<<endl;
162 }
std::ostream & EvtGenReport(EvtGenSeverity severity, const char *facility=0)
Definition: EvtReport.cpp:33
int read(const std::string filename)
Definition: EvtParser.cpp:62
#define MAXBUF
Definition: EvtParser.cpp:31
void addToken(int line, const std::string &string)
Definition: EvtParser.cpp:129
int getLineofToken(int i)
Definition: EvtParser.cpp:57
int getNToken()
Definition: EvtParser.cpp:47
const std::string & getToken(int i)
Definition: EvtParser.cpp:52