00001 #include "tcparser.h"
00002 #include <sstream>
00003 #include <iostream>
00004
00005 using namespace std;
00006
00007 TcParser::TcParser() : pInFile(0)
00008 {
00009
00010 }
00011
00012 TcParser::~TcParser()
00013 {
00014 if ( pInFile ) {
00015 pInFile->close();
00016 delete pInFile;
00017 }
00018 }
00019
00020 void TcParser::set_file(string file_name)
00021 {
00022 if ( pInFile ) {
00023
00024 cleanup();
00025 }
00026 pInFile = new ifstream( file_name.c_str() );
00027 line_num = 0;
00028 }
00029
00030 bool TcParser::parse_line(string& name, std::vector<double>& tc)
00031 {
00032 if ( pInFile ) {
00033 string line;
00034 if ( getline(*pInFile, line) ) {
00035 line_num++;
00036 istringstream is(line);
00037
00038 if ( !(is>>name) ) {
00039 cerr<<"TcParser::parse_line(), input error"<<endl;
00040 cerr<<"line "<<line_num<<": "<<line<<"\nis ill formated"<<endl;
00041 cleanup();
00042 return false;
00043 }
00044 string tmp;
00045 double val;
00046 int num_parsed(0);
00047 while ( is>>tmp ) {
00048
00049 if ( tmp == "NA" ) {
00050 val = -1.0;
00051 }
00052 else {
00053 istringstream is2(tmp);
00054 if ( !(is2>>val) ) {
00055 cerr<<"TcParser::parse_line(), input error"<<endl;
00056 cerr<<"line "<<line_num<<": "<<line<<"\nis ill formated"<<endl;
00057 cleanup();
00058 return false;
00059 }
00060 }
00061 tc.push_back(val);
00062 num_parsed++;
00063
00064
00065
00066 }
00067
00068 return true;
00069 }
00070
00071 cleanup();
00072 }
00073 else {
00074 cerr<<"No file handle in TcParser::parse_line()"<<endl;
00075 }
00076 return false;
00077 }
00078
00079 void TcParser::cleanup()
00080 {
00081 if (pInFile) {
00082
00083 pInFile->close();
00084 delete pInFile;
00085 pInFile = 0;
00086
00087 }
00088
00089 }
00090