EvtGen
2.2.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
Toggle main menu visibility
Loading...
Searching...
No Matches
src
EvtGenBase
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/EvtReport.hh
"
24
25
#include <fstream>
26
#include <sstream>
27
#include <string.h>
28
using namespace
std;
29
30
#define MAXBUF 1024
31
32
EvtParser::EvtParser
()
33
{
34
m_ntoken
= 0;
35
m_lengthoftokenlist
= 0;
36
m_tokenlist
=
nullptr
;
37
m_linelist
=
nullptr
;
38
}
39
40
EvtParser::~EvtParser
()
41
{
42
delete
[]
m_tokenlist
;
43
delete
[]
m_linelist
;
44
}
45
46
int
EvtParser::getNToken
()
47
{
48
return
m_ntoken
;
49
}
50
51
const
std::string&
EvtParser::getToken
(
int
i )
52
{
53
return
m_tokenlist
[i];
54
}
55
56
int
EvtParser::getLineofToken
(
int
i )
57
{
58
return
m_linelist
[i];
59
}
60
61
int
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
128
void
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
}
MAXBUF
#define MAXBUF
Definition
EvtParser.cpp:30
EvtParser.hh
EvtReport.hh
EvtGenReport
std::ostream & EvtGenReport(EvtGenSeverity severity, const char *facility=nullptr)
Definition
EvtReport.cpp:32
EVTGEN_ERROR
@ EVTGEN_ERROR
Definition
EvtReport.hh:49
EvtParser::m_tokenlist
std::string * m_tokenlist
Definition
EvtParser.hh:38
EvtParser::getLineofToken
int getLineofToken(int i)
Definition
EvtParser.cpp:56
EvtParser::~EvtParser
~EvtParser()
Definition
EvtParser.cpp:40
EvtParser::EvtParser
EvtParser()
Definition
EvtParser.cpp:32
EvtParser::m_linelist
int * m_linelist
Definition
EvtParser.hh:39
EvtParser::m_ntoken
int m_ntoken
Definition
EvtParser.hh:37
EvtParser::m_lengthoftokenlist
int m_lengthoftokenlist
Definition
EvtParser.hh:40
EvtParser::read
int read(const std::string filename)
Definition
EvtParser.cpp:61
EvtParser::addToken
void addToken(int line, const std::string &string)
Definition
EvtParser.cpp:128
EvtParser::getNToken
int getNToken()
Definition
EvtParser.cpp:46
EvtParser::getToken
const std::string & getToken(int i)
Definition
EvtParser.cpp:51
Generated by
1.17.0