Line data Source code
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 : //
21 : #include "EvtGenBase/EvtPatches.hh"
22 : #include "EvtGenBase/EvtPatches.hh"
23 : #include <fstream>
24 : #include <sstream>
25 : #include <string.h>
26 : #include "EvtGenBase/EvtParser.hh"
27 : #include "EvtGenBase/EvtReport.hh"
28 : using namespace std;
29 :
30 : #define MAXBUF 1024
31 :
32 0 : EvtParser::EvtParser(){
33 0 : _ntoken=0;
34 0 : _lengthoftokenlist=0;
35 0 : _tokenlist=0;
36 0 : _linelist=0;
37 0 : }
38 :
39 0 : EvtParser::~EvtParser(){
40 :
41 0 : delete [] _tokenlist;
42 0 : delete [] _linelist;
43 :
44 0 : }
45 :
46 :
47 : int EvtParser::getNToken(){
48 :
49 0 : return _ntoken;
50 :
51 : }
52 :
53 : const std::string& EvtParser::getToken(int i){
54 :
55 0 : return _tokenlist[i];
56 :
57 : }
58 :
59 : int EvtParser::getLineofToken(int i){
60 :
61 0 : return _linelist[i];
62 :
63 : }
64 :
65 : int EvtParser::read(const std::string filename){
66 0 : ifstream fin;
67 :
68 0 : fin.open(filename.c_str());
69 0 : if (!fin) {
70 0 : report(Severity::Error,"EvtGen") << "Could not open file '"<<filename.c_str()<<"'"<<endl;
71 0 : return -1;
72 : }
73 :
74 0 : char buf[MAXBUF];
75 0 : char buf2[MAXBUF];
76 : char c;
77 :
78 : int line=0;
79 : int i;
80 :
81 0 : while(fin.peek() != EOF){
82 0 : line++;
83 :
84 : i=0;
85 0 : while((c=fin.get()) != '\n' && i<MAXBUF) {
86 0 : buf[i]=c;
87 0 : i++;
88 : }
89 0 : if(i==MAXBUF) {
90 0 : report(Severity::Error,"EvtGen") << "Error in EvtParser: line:"
91 0 : <<line<<" to long"<<endl;
92 : }
93 : else {
94 0 : buf[i] = '\0';
95 : }
96 :
97 : //search for '#' which indicates comment for rest of line!
98 : i=0;
99 0 : do{
100 0 : if (buf[i]=='#') buf[i]=0;
101 0 : i++;
102 0 : }while(buf[i-1]!=0);
103 :
104 0 : string tmp(buf,strlen(buf));
105 :
106 : //read each token
107 0 : istringstream ist(tmp);
108 0 : while(ist>>buf2){
109 : i=0;
110 : int semicolon=0;
111 0 : do{
112 0 : if (buf2[i]==';') {
113 0 : buf2[i]=0;
114 : semicolon=1;
115 0 : }
116 0 : }while(buf2[i++]!=0);
117 0 : if (buf2[0]!=0){
118 0 : addToken(line,buf2);
119 0 : }
120 0 : if (semicolon) addToken(line,";");
121 : }
122 0 : }
123 :
124 0 : fin.close();
125 :
126 : return 0;
127 :
128 0 : }
129 :
130 :
131 :
132 : void EvtParser::addToken(int line,const std::string& string){
133 :
134 : //report(Severity::Info,"EvtGen") <<_ntoken<<" "<<line<<" "<<string<<endl;
135 :
136 0 : if (_ntoken==_lengthoftokenlist) {
137 :
138 0 : int new_length=1000+4*_lengthoftokenlist;
139 :
140 :
141 :
142 0 : int* newlinelist= new int[new_length];
143 0 : std::string* newtokenlist= new std::string[new_length];
144 :
145 : int i;
146 :
147 0 : for(i=0;i<_ntoken;i++){
148 0 : newlinelist[i]=_linelist[i];
149 0 : newtokenlist[i]=_tokenlist[i];
150 : }
151 :
152 0 : delete [] _tokenlist;
153 0 : delete [] _linelist;
154 :
155 0 : _tokenlist=newtokenlist;
156 0 : _linelist=newlinelist;
157 :
158 0 : _lengthoftokenlist=new_length;
159 :
160 0 : }
161 :
162 :
163 0 : _tokenlist[_ntoken]=string;
164 :
165 0 : _linelist[_ntoken]=line;
166 :
167 0 : _ntoken++;
168 :
169 : //report(Severity::Info,"EvtGen") << "First:"<<_tokenlist[0]<<" last:"<<_tokenlist[_ntoken-1]<<endl;
170 :
171 0 : }
172 :
|