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