#include "aaspi_hist_file.h" #include "aaspi_framework.h" #include "aaspi_file_info.h" bool AASPI_HistoryFile::unpipe() { char *rawin=new char[102400]; cin.read(rawin,102400); int nchar=cin.gcount(); if (cin.eof()) { string(rawin,nchar); iss.str(rawin); ifp=&iss; is_string_temp=true; } else { // Copy to temp file string fname=AASPI_FileInfo::get_temp_file("./","Unpipe_"); file.open(fname.c_str(),ios_base::out); if (file.good()) { file.write(rawin,nchar); } while (!cin.eof()) { cin.read(rawin,102400); nchar=cin.gcount(); file.write(rawin,nchar); } file.close(); mode=ios_base::in; file.open(fname.c_str(),mode); is_file_temp=true; file_name=fname; ifp=&file; } delete [] rawin; process_file(); if (ifp->fail()) last_error=errno; // Maybe this is good enough? return !ifp->fail(); } bool AASPI_HistoryFile::Close() { if (!is_string_temp) { file.close(); if (file.fail()) last_error=errno; } return true; } void AASPI_HistoryFile::process_file() { if (ifp==0) return; // Safety valve AASPI_Lexer inlex(*ifp); AASPI_Token tok; while (1) { tok=inlex.lex(); if (tok.typ == AASPI_Lexer::FailEnd) break; dict.set_string(tok.Name,tok.Val); } } bool AASPI_HistoryFile::OpenIn(const string &fname) { if (fname == "stdin") { return unpipe(); } else if (fname == "stdout") { cerr << "Cannot open stdout for input"; return false; } else { mode=ios_base::in; file.open(fname.c_str(),mode); if (file.good()) { file_name=fname; ifp=&file; process_file(); } else { // Find out why the open failed AASPI_FileInfo afi(fname); if (!afi.exists()) { cerr << "Error: " << fname << " does not exist." << endl; return false; } if (!afi.isReadable()) { cerr << "Error: "<< fname << " is not readable." << endl; return false; } } bool rv= ! file.bad(); if (file.fail()) last_error=errno; return rv; } return false; // Should never get here } bool AASPI_HistoryFile::OpenOut(const string &fname) { if (fname == "stdout") { ofp=&cout; return true; } else if (fname == "stdin") { cerr << "Cannot open stdin for output"; return false; } else { mode=ios_base::out; file.open(fname.c_str(),mode); bool fg=file.good(); if (fg) { file_name=fname; ofp=&file; } else { AASPI_FileInfo afi(fname); if (!afi.exists()) { cerr << "Error: "<< fname << " does not exist." << endl; return false; } if (!afi.isWritable()) { cerr << "Error: "<< fname << " is not writable." << endl; return false; } } if (file.fail()) last_error=errno; return fg; } return false; // Should never get here } static string format_string(const string &ins) { string outs="\""; string::const_iterator it; for (it=ins.begin(); it != ins.end(); ++it) { if (*it == '\"') outs += '\"'; outs += (*it); } outs += '\"'; return outs; } void AASPI_HistoryFile::put_par_base(const string &tag, const string &val, const string &sl, const string &el) { if (!ofp) return; // bad file pointer dict.set_string(tag,val); if (!stamped) write_stamp(); (*ofp) << sl << tag << '=' << val << el; if (el == "\n") ofp->flush(); } void AASPI_HistoryFile::put_par(const string &tag, const string &val, const string &sl, const string &el) { string ostr=format_string(val); put_par_base(tag,ostr,sl,el); //cerr << "Hist file put_par string:" << ostr << endl; } void AASPI_HistoryFile::put_par(const string &tag, const int val, const string &sl, const string &el) { ostringstream oss; oss << val; put_par_base(tag,oss.str(),sl,el); } void AASPI_HistoryFile::put_par(const string &tag, const aaspi_int64 val, const string &sl, const string &el) { ostringstream oss; oss << val; put_par_base(tag,oss.str(),sl,el); } void AASPI_HistoryFile::put_par(const string &tag, const float val, const string &sl, const string &el) { ostringstream oss; //oss.precision(9); // Thang Ha: 20170824: no need to specify precision. oss << val; put_par_base(tag,oss.str(),sl,el); } void AASPI_HistoryFile::put_par(const string &tag, const double val, const string &sl, const string &el) { ostringstream oss; oss.precision(12); // Thang Ha: 20181123: for double values, we need a higher precision than the default of 6. Right now 12 is good enough. oss << val; put_par_base(tag,oss.str(),sl,el); } void AASPI_HistoryFile::put_par_bool(const string &tag, const bool val, const string &sl, const string &el) { string out_str; if (val) out_str="\"y\""; else out_str="\"n\""; put_par_base(tag,out_str,sl,el); } void AASPI_HistoryFile::write_comment(const string &str) { if (!stamped) write_stamp(); (*ofp) << "#\t" << str << endl; } void AASPI_HistoryFile::write_stamp() { if (!ofp) return; // has bad file pointer if (stamped) return; string thisprog; if (afw) { thisprog=afw->progName(); } else { thisprog="Unknown"; } string userstr=AASPI_Framework::getUserName()+"@" +AASPI_Framework::getHostName(); string datestr=AASPI_Framework::getTime(); (*ofp) << endl << thisprog << ": " << userstr << " " << datestr << endl; stamped=true; } int AASPI_HistoryFile::get_num_axes() { int na=1,i,n; for (i=9; i>0; --i) { ostringstream oss; oss <<'n'<clear(); hf.ifp->seekg(0,ios_base::beg); hf.ifp->clear(); while (hf.ifp->good()) { hf.ifp->read(tbuff,4096); ofp->write(tbuff,hf.ifp->gcount()); } if (!stamped) write_stamp(); if (hf.ifp->bad()) { last_error=errno; return false; } return true; }