00001 //-------------------------------------------------------------------------- 00002 // 00003 // Environment: 00004 // This software is part of the EvtGen package developed jointly 00005 // for the BaBar and CLEO collaborations. If you use all or part 00006 // of it, please give an appropriate acknowledgement. 00007 // 00008 // Copyright Information: See EvtGen/COPYRIGHT 00009 // Copyright (C) 1998 Caltech, UCSB 00010 // 00011 // Module: EvtParser.cc 00012 // 00013 // Description: Reading the decay table and produce a list of tokens. 00014 // 00015 // Modification history: 00016 // 00017 // RYD Febuary 11, 1998 Module created 00018 // 00019 //------------------------------------------------------------------------ 00020 // 00021 #include "EvtGenBase/EvtPatches.hh" 00022 #include "EvtGenBase/EvtPatches.hh" 00023 #include <fstream> 00024 #include <strstream> 00025 #include <string.h> 00026 #include "EvtGenBase/EvtParser.hh" 00027 #include "EvtGenBase/EvtReport.hh" 00028 using std::endl; 00029 using std::fstream; 00030 using std::ifstream; 00031 using std::istrstream; 00032 using std::strstream; 00033 00034 #define MAXBUF 1024 00035 00036 EvtParser::EvtParser(){ 00037 _ntoken=0; 00038 _lengthoftokenlist=0; 00039 _tokenlist=0; 00040 _linelist=0; 00041 } 00042 00043 EvtParser::~EvtParser(){ 00044 00045 delete [] _tokenlist; 00046 delete [] _linelist; 00047 00048 } 00049 00050 00051 int EvtParser::getNToken(){ 00052 00053 return _ntoken; 00054 00055 } 00056 00057 const std::string& EvtParser::getToken(int i){ 00058 00059 return _tokenlist[i]; 00060 00061 } 00062 00063 int EvtParser::getLineofToken(int i){ 00064 00065 return _linelist[i]; 00066 00067 } 00068 00069 int EvtParser::Read(const std::string filename){ 00070 ifstream fin; 00071 00072 fin.open(filename.c_str()); 00073 if (!fin) { 00074 report(ERROR,"EvtGen") << "Could not open file '"<<filename.c_str()<<"'"<<endl; 00075 return -1; 00076 } 00077 00078 char buf[MAXBUF]; 00079 char buf2[MAXBUF]; 00080 char c; 00081 00082 int line=0; 00083 int i; 00084 00085 while(fin.peek() != EOF){ 00086 line++; 00087 00088 i=0; 00089 while((c=fin.get()) != '\n' && i<MAXBUF) { 00090 buf[i]=c; 00091 i++; 00092 } 00093 if(i==MAXBUF) { 00094 report(ERROR,"EvtGen") << "Error in EvtParser: line:" 00095 <<line<<" to long"<<endl; 00096 } 00097 else { 00098 buf[i] = '\0'; 00099 } 00100 00101 //search for '#' which indicates comment for rest of line! 00102 i=0; 00103 do{ 00104 if (buf[i]=='#') buf[i]=0; 00105 i++; 00106 }while(buf[i-1]!=0); 00107 00108 //read each token 00109 istrstream ist(buf,strlen(buf)); 00110 while(ist>>buf2){ 00111 i=0; 00112 int semicolon=0; 00113 do{ 00114 if (buf2[i]==';') { 00115 buf2[i]=0; 00116 semicolon=1; 00117 } 00118 }while(buf2[i++]!=0); 00119 if (buf2[0]!=0){ 00120 addToken(line,buf2); 00121 } 00122 if (semicolon) addToken(line,";"); 00123 } 00124 } 00125 00126 fin.close(); 00127 00128 return 0; 00129 00130 } 00131 00132 00133 00134 void EvtParser::addToken(int line,const std::string& string){ 00135 00136 //report(INFO,"EvtGen") <<_ntoken<<" "<<line<<" "<<string<<endl; 00137 00138 if (_ntoken==_lengthoftokenlist) { 00139 00140 int new_length=1000+4*_lengthoftokenlist; 00141 00142 00143 00144 int* newlinelist= new int[new_length]; 00145 std::string* newtokenlist= new std::string[new_length]; 00146 00147 int i; 00148 00149 for(i=0;i<_ntoken;i++){ 00150 newlinelist[i]=_linelist[i]; 00151 newtokenlist[i]=_tokenlist[i]; 00152 } 00153 00154 delete [] _tokenlist; 00155 delete [] _linelist; 00156 00157 _tokenlist=newtokenlist; 00158 _linelist=newlinelist; 00159 00160 _lengthoftokenlist=new_length; 00161 00162 } 00163 00164 00165 _tokenlist[_ntoken]=string; 00166 00167 _linelist[_ntoken]=line; 00168 00169 _ntoken++; 00170 00171 //report(INFO,"EvtGen") << "First:"<<_tokenlist[0]<<" last:"<<_tokenlist[_ntoken-1]<<endl; 00172 00173 } 00174