#include "ATOOLS/Org/Read_Write_Base.H" #include "ATOOLS/Org/MyStrStream.H" #include "ATOOLS/Math/MathTools.H" #include "ATOOLS/Org/Message.H" #include "ATOOLS/Org/My_Limits.H" #include "ATOOLS/Phys/NLO_Types.H" using namespace ATOOLS; Buffer_Map Read_Write_Base::s_buffermap; Read_Write_Base::Read_Write_Base(const unsigned int infiles, const unsigned int outfiles): File_IO_Base(infiles,outfiles), m_comment(1,defaultcom), m_wordsep(1,defaultwsep), m_linesep(1,defaultlsep), m_filecontent(infiles) { Init(); } Read_Write_Base::Read_Write_Base (const unsigned int infiles,const unsigned int outfiles, const std::string &wordsep,const std::string &linesep, const std::string &comment,const std::string &ignore): File_IO_Base(infiles,outfiles), m_comment(1,comment), m_wordsep(1,wordsep), m_linesep(1,linesep), m_filecontent(infiles) { if (ignore!=nullstring) m_ignore.push_back(ignore); Init(); } Read_Write_Base::~Read_Write_Base() { delete p_interpreter; } void Read_Write_Base::Init() { p_interpreter = new Algebra_Interpreter(); m_blank.push_back(defaultblank); m_blank.push_back(defaulttab); m_vectortype=vtc::horizontal; m_matrixtype=mtc::transposed; m_allownans=false; m_ignorecase=false; m_ignoreblanks=false; m_exactmatch=true; m_interprete=true; m_cmode=false; m_occurrence=std::string::npos; m_escape='\\'; m_namesplit='|'; } void Read_Write_Base::SplitInFileName(const size_t &i) { std::string name(InputFile(i)); size_t sb(name.find(m_namesplit)); if (sb==std::string::npos) return; size_t se(name.find(m_namesplit,sb+1)); if (se==std::string::npos) return; SetInputFile(name.substr(0,sb),i); m_filebegin=String_Vector(1,name.substr(sb+1,se-sb-1)); m_fileend=String_Vector(1,name.substr(se+1)); msg_IODebugging()<'"<length(),*bit)==0) return bit->length(); return false; } size_t Read_Write_Base::IsWordSeparator(const std::string &ch) const { for (String_Vector::const_iterator bit(m_wordsep.begin()); bit!=m_wordsep.end();++bit) if (ch.compare(0,bit->length(),*bit)==0) return bit->length(); return false; } size_t Read_Write_Base::IsLineSeparator(const std::string &ch) const { for (String_Vector::const_iterator bit(m_linesep.begin()); bit!=m_linesep.end();++bit) if (ch.compare(0,bit->length(),*bit)==0) return bit->length(); return false; } size_t Read_Write_Base::Find(std::string input,std::string parameter, size_t &length) const { if (m_ignorecase) { for (size_t i=0;i0) { size_t i(0); for (;i0) return buffer[line][--pos]; while (line>0) if (buffer[--line].length()>0) return buffer[line][pos=buffer[line].length()-1]; return (char)0; } char Read_Write_Base::NextChar(String_Vector &buffer, int &line,int &pos) const { if (++pos<(int)buffer[line].length()) return buffer[line][pos]; while (++line<(int)buffer.size()) if (buffer[line].length()>0) return buffer[line][pos=0]; return (char)0; } void Read_Write_Base::InterpreteBuffer(String_Vector &buffer, int &line,int &pos, const int level,const bool keep) { if (buffer.empty() || line>(int)buffer.size() || (line==(int)buffer.size() && pos>=(int)buffer.back().length())) return; int oldline(line), brackets(0); char cur, blank(m_blank.size()?m_blank.front():defaultblank); bool ifcandidate(true), elsecandidate(false), res(true); bool first(true), bracketed(false); while ((cur=NextChar(buffer,line,pos))!=(char)0) { if (line>oldline) { if (brackets==0 && level>0) { PrevChar(buffer,line,pos); return; } ifcandidate=true; oldline=line; } std::string rem(buffer[line].substr(pos)); size_t bl; if ((bl=IsLineSeparator(rem))>0) { if (!keep) for (size_t i(0);i0) return; ifcandidate=true; continue; } if (cur=='{') { if (brackets==0 && level>0 && first) bracketed=true; if ((bracketed && brackets==0) || !keep) buffer[line][pos]=blank; ++brackets; ifcandidate=true; continue; } if (cur=='}') { if (brackets>0) --brackets; if ((bracketed &&brackets==0) || !keep) buffer[line][pos]=blank; if (bracketed && brackets==0 && level>0) { PrevChar(buffer,line,pos); return; } ifcandidate=true; continue; } bool noblank(!IsBlank(cur)); if (noblank) first=false; if (!keep) { buffer[line][pos]=blank; continue; } if (ifcandidate && buffer[line].substr(pos,2)=="if") { int sline(line), spos(pos++); std::string arg; while ((cur=NextChar(buffer,line,pos))!=(char)0) if (!IsBlank(cur)) break; if (cur!='(') continue; while ((cur=NextChar(buffer,line,pos))!=(char)0) { if (cur==')') break; else arg+=cur; } if (cur!=')') continue; res=ToType(p_interpreter->Interprete(arg)); while (slinepos && input[pos]==Escape()) next=pos+1; } return input; } std::string &Read_Write_Base::KillBlanks(std::string &buffer) const { while (buffer.length()>0 && IsBlank(buffer[0])) buffer.erase(0,1); while (buffer.length()>0 && IsBlank(buffer[buffer.length()-1])) buffer.erase(buffer.length()-1,1); return buffer; } std::string &Read_Write_Base::KillComments(std::string &buffer) const { size_t pos; for (unsigned int i=0;i0 && buffer[pos-1]==Escape()) next=pos+m_comment[i].length(); else buffer=buffer.substr(0,pos); } } return KillBlanks(buffer); } std::string &Read_Write_Base::KillIgnored(std::string &buffer) const { size_t pos; char blank=Blank().size()?Blank().front():defaultblank; for (unsigned int i=0; i0 && buffer[pos-1]==Escape()) next=pos+m_ignore[i].length(); else { buffer=buffer.substr(0,pos)+blank+ buffer.substr(pos+m_ignore[i].length()); } } } return KillBlanks(buffer); } void Read_Write_Base::AddFileContent(std::string line,const unsigned int i) { KillComments(line); KillIgnored(line); size_t length; bool lastword(false); for (int j(0);j<(int)line.length();++j) { std::string rem(line.substr(j)); if ((length=IsWordSeparator(rem))>0) { if (j>0) { if (lastword) m_filecontent[i].back().push_back(line.substr(0,j)); else m_filecontent[i].push_back(String_Vector(1,line.substr(0,j))); lastword=true; } KillBlanks(line=rem.substr(length)); j=-1; } else if ((length=IsLineSeparator(rem))>0) { if (j>0) { if (lastword) m_filecontent[i].back().push_back(line.substr(0,j)); else m_filecontent[i].push_back(String_Vector(1,line.substr(0,j))); } lastword=false; KillBlanks(line=rem.substr(length)); j=-1; } } if (line.length()>0) { if (lastword) m_filecontent[i].back().push_back(line); else m_filecontent[i].push_back(String_Vector(1,line)); } } bool Read_Write_Base::OpenInFile(const unsigned int i,const int mode) { if (InputPath(i)+InputFile(i)==nullstring) { return false; } if (InFileMode(i)==fom::unknown) SetInFileMode(fom::permanent); if (!m_filecontent[i].empty()&&mode==0) return true; SplitInFileName(i); std::string lastline, file(InputPath(i)+InputFile(i)); file+="|"; for (size_t j(0);j "<0) buffer.push_back(lastline); getline(*infile,lastline); } while (*infile); } } m_filecontent[i].clear(); bool checkbegin=(bool)(m_filebegin.size()>0); bool checkend=(bool)(m_fileend.size()>0); int filebegin=0; unsigned int occurrence=0; if (!buffer.empty()) { for (size_t ln(0);ln0) cbuffer.push_back(lastline); } else if (lastline.length()>0) { if (lastline.length()>0) cbuffer.push_back(lastline); } } InterpreteBuffer(cbuffer); for (size_t j(0);j ("<<&s_buffermap[file]<<")\n"; s_buffermap.erase(s_buffermap.find(file)); } infile.Close(); } bool Read_Write_Base::OpenOutFile(const unsigned int i) { if (OutputFile(i)==nullstring) return false; if (OutFileMode(i)==fom::unknown) SetOutFileMode(fom::permanent); My_Out_File &outfile(OutFile(i)); if (outfile()==NULL) { outfile.Open(); if (m_filebegin.size()>0 && !outfile->bad()) { (*outfile)<bad(); } void Read_Write_Base::CloseOutFile(const unsigned int i,const int mode) { My_Out_File &outfile(OutFile(i)); if (outfile()==NULL) return; if ((outfile.Mode()&fom::permanent) && !mode) return; if (m_fileend.size()>0 && !outfile->bad()) { (*outfile)< Type Read_Write_Base::Default() const { return std::numeric_limits::max(); } template int Read_Write_Base::Default() const; template unsigned int Read_Write_Base::Default() const; template long int Read_Write_Base::Default() const; template unsigned long int Read_Write_Base::Default() const; template float Read_Write_Base::Default() const; template double Read_Write_Base::Default() const; template cs_itype::type Read_Write_Base::Default() const; template sbt::subtype Read_Write_Base::Default() const; template <> bool Read_Write_Base::Default() const { return false; } template <> std::string Read_Write_Base::Default() const { return ""; } }