EvtGen 2.2.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
Loading...
Searching...
No Matches
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
22
24
25#include <fstream>
26#include <sstream>
27#include <string.h>
28using namespace std;
29
30#define MAXBUF 1024
31
33{
34 m_ntoken = 0;
36 m_tokenlist = nullptr;
37 m_linelist = nullptr;
38}
39
41{
42 delete[] m_tokenlist;
43 delete[] m_linelist;
44}
45
47{
48 return m_ntoken;
49}
50
51const std::string& EvtParser::getToken( int i )
52{
53 return m_tokenlist[i];
54}
55
57{
58 return m_linelist[i];
59}
60
61int EvtParser::read( const std::string filename )
62{
63 ifstream fin;
64
65 fin.open( filename.c_str() );
66 if ( !fin ) {
67 EvtGenReport( EVTGEN_ERROR, "EvtGen" )
68 << "Could not open file '" << filename.c_str() << "'" << endl;
69 return -1;
70 }
71
72 char buf[MAXBUF];
73 char buf2[MAXBUF];
74 char c;
75
76 int line = 0;
77 int i;
78
79 while ( fin.peek() != EOF ) {
80 line++;
81
82 i = 0;
83 while ( ( c = fin.get() ) != '\n' && c != EOF && i < MAXBUF ) {
84 buf[i] = c;
85 i++;
86 }
87 if ( i == MAXBUF ) {
88 EvtGenReport( EVTGEN_ERROR, "EvtGen" )
89 << "Error in EvtParser: line:" << line << " to long" << endl;
90 } else {
91 buf[i] = '\0';
92 }
93
94 //search for '#' which indicates comment for rest of line!
95 i = 0;
96 do {
97 if ( buf[i] == '#' )
98 buf[i] = 0;
99 i++;
100 } while ( buf[i - 1] != 0 );
101
102 string tmp( buf, strlen( buf ) );
103
104 //read each token
105 istringstream ist( tmp );
106 while ( ist >> buf2 ) {
107 i = 0;
108 int semicolon = 0;
109 do {
110 if ( buf2[i] == ';' ) {
111 buf2[i] = 0;
112 semicolon = 1;
113 }
114 } while ( buf2[i++] != 0 );
115 if ( buf2[0] != 0 ) {
116 addToken( line, buf2 );
117 }
118 if ( semicolon )
119 addToken( line, ";" );
120 }
121 }
122
123 fin.close();
124
125 return 0;
126}
127
128void EvtParser::addToken( int line, const std::string& string )
129{
130 //EvtGenReport(EVTGEN_INFO,"EvtGen") <<m_ntoken<<" "<<line<<" "<<string<<endl;
131
132 if ( m_ntoken == m_lengthoftokenlist ) {
133 int new_length = 1000 + 4 * m_lengthoftokenlist;
134
135 int* newlinelist = new int[new_length];
136 std::string* newtokenlist = new std::string[new_length];
137
138 int i;
139
140 for ( i = 0; i < m_ntoken; i++ ) {
141 newlinelist[i] = m_linelist[i];
142 newtokenlist[i] = m_tokenlist[i];
143 }
144
145 delete[] m_tokenlist;
146 delete[] m_linelist;
147
148 m_tokenlist = newtokenlist;
149 m_linelist = newlinelist;
150
151 m_lengthoftokenlist = new_length;
152 }
153
154 m_tokenlist[m_ntoken] = string;
155
156 m_linelist[m_ntoken] = line;
157
158 m_ntoken++;
159
160 //EvtGenReport(EVTGEN_INFO,"EvtGen") << "First:"<<m_tokenlist[0]<<" last:"<<m_tokenlist[m_ntoken-1]<<endl;
161}
#define MAXBUF
Definition EvtParser.cpp:30
std::ostream & EvtGenReport(EvtGenSeverity severity, const char *facility=nullptr)
Definition EvtReport.cpp:32
@ EVTGEN_ERROR
Definition EvtReport.hh:49
std::string * m_tokenlist
Definition EvtParser.hh:38
int getLineofToken(int i)
Definition EvtParser.cpp:56
int * m_linelist
Definition EvtParser.hh:39
int m_ntoken
Definition EvtParser.hh:37
int m_lengthoftokenlist
Definition EvtParser.hh:40
int read(const std::string filename)
Definition EvtParser.cpp:61
void addToken(int line, const std::string &string)
int getNToken()
Definition EvtParser.cpp:46
const std::string & getToken(int i)
Definition EvtParser.cpp:51