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