BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
EvtParser.cc
Go to the documentation of this file.
1//--------------------------------------------------------------------------
2//
3// Environment:
4// This software is part of the EvtGen package developed jointly
5// for the BaBar and CLEO collaborations. If you use all or part
6// of it, please give an appropriate acknowledgement.
7//
8// Copyright Information: See EvtGen/COPYRIGHT
9// Copyright (C) 1998 Caltech, UCSB
10//
11// Module: EvtParser.cc
12//
13// Description: Reading the decay table and produce a list of tokens.
14//
15// Modification history:
16//
17// RYD Febuary 11, 1998 Module created
18//
19//------------------------------------------------------------------------
20//
23#include <fstream>
24#include <strstream>
25#include <string.h>
28using std::endl;
29using std::fstream;
30using std::ifstream;
31using std::istrstream;
32using std::strstream;
33
34#define MAXBUF 1024
35
37 _ntoken=0;
38 _lengthoftokenlist=0;
39 _tokenlist=0;
40 _linelist=0;
41}
42
44
45 delete [] _tokenlist;
46 delete [] _linelist;
47
48}
49
50
52
53 return _ntoken;
54
55}
56
57const std::string& EvtParser::getToken(int i){
58
59 return _tokenlist[i];
60
61}
62
64
65 return _linelist[i];
66
67}
68
69int EvtParser::Read(const std::string filename){
70 ifstream fin;
71
72 fin.open(filename.c_str());
73 if (!fin) {
74 report(ERROR,"EvtGen") << "Could not open file '"<<filename.c_str()<<"'"<<endl;
75 return -1;
76 }
77
78 char buf[MAXBUF];
79 char buf2[MAXBUF];
80 char c;
81
82 int line=0;
83 int i;
84
85 while(fin.peek() != EOF){
86 line++;
87
88 i=0;
89 while((c=fin.get()) != '\n' && i<MAXBUF) {
90 buf[i]=c;
91 i++;
92 }
93 if(i==MAXBUF) {
94 report(ERROR,"EvtGen") << "Error in EvtParser: line:"
95 <<line<<" to long"<<endl;
96 }
97 else {
98 buf[i] = '\0';
99 }
100
101 //search for '#' which indicates comment for rest of line!
102 i=0;
103 do{
104 if (buf[i]=='#') buf[i]=0;
105 i++;
106 }while(buf[i-1]!=0);
107
108 //read each token
109 istrstream ist(buf,strlen(buf));
110 while(ist>>buf2){
111 i=0;
112 int semicolon=0;
113 do{
114 if (buf2[i]==';') {
115 buf2[i]=0;
116 semicolon=1;
117 }
118 }while(buf2[i++]!=0);
119 if (buf2[0]!=0){
120 addToken(line,buf2);
121 }
122 if (semicolon) addToken(line,";");
123 }
124 }
125
126 fin.close();
127
128 return 0;
129
130}
131
132
133
134void EvtParser::addToken(int line,const std::string& string){
135
136 //report(INFO,"EvtGen") <<_ntoken<<" "<<line<<" "<<string<<endl;
137
138 if (_ntoken==_lengthoftokenlist) {
139
140 int new_length=1000+4*_lengthoftokenlist;
141
142
143
144 int* newlinelist= new int[new_length];
145 std::string* newtokenlist= new std::string[new_length];
146
147 int i;
148
149 for(i=0;i<_ntoken;i++){
150 newlinelist[i]=_linelist[i];
151 newtokenlist[i]=_tokenlist[i];
152 }
153
154 delete [] _tokenlist;
155 delete [] _linelist;
156
157 _tokenlist=newtokenlist;
158 _linelist=newlinelist;
159
160 _lengthoftokenlist=new_length;
161
162 }
163
164
165 _tokenlist[_ntoken]=string;
166
167 _linelist[_ntoken]=line;
168
169 _ntoken++;
170
171 //report(INFO,"EvtGen") << "First:"<<_tokenlist[0]<<" last:"<<_tokenlist[_ntoken-1]<<endl;
172
173}
174
#define MAXBUF
Definition: EvtParser.cc:34
ostream & report(Severity severity, const char *facility)
Definition: EvtReport.cc:36
@ ERROR
Definition: EvtReport.hh:49
int getLineofToken(int i)
Definition: EvtParser.cc:63
~EvtParser()
Definition: EvtParser.cc:43
int getNToken()
Definition: EvtParser.cc:51
const std::string & getToken(int i)
Definition: EvtParser.cc:57
int Read(const std::string filename)
Definition: EvtParser.cc:69