File indexing completed on 2025-12-13 10:29:01
0001 #ifndef ATOOLS_Org_Read_Write_Base_H
0002 #define ATOOLS_Org_Read_Write_Base_H
0003
0004 #include "ATOOLS/Org/File_IO_Base.H"
0005 #include "ATOOLS/Math/Algebra_Interpreter.H"
0006
0007 #include <map>
0008
0009 #include <algorithm> // for find function
0010
0011 namespace ATOOLS {
0012
0013 const std::string defaultwsep(" ");
0014 const std::string defaultlsep(";");
0015 const std::string defaultcom("#");
0016
0017 const char defaultblank(' ');
0018 const char defaulttab('\t');
0019
0020 struct vtc {
0021
0022 enum code {
0023 vertical = 1,
0024 horizontal = 2,
0025 unknown = 99
0026 };
0027
0028 };
0029
0030 struct mtc {
0031
0032 enum code {
0033 normal = 1,
0034 transposed = 2,
0035 unknown = 99
0036 };
0037
0038 };
0039
0040 class Algebra_Interpreter;
0041
0042 typedef std::vector<char> Char_Vector;
0043
0044 typedef std::vector<std::string> String_Vector;
0045 typedef std::vector<String_Vector> String_Matrix;
0046 typedef std::vector<String_Matrix> String_Cube;
0047
0048 typedef std::map<std::string,String_Vector> Buffer_Map;
0049 typedef std::map<std::string,std::string> String_Map;
0050
0051 class Read_Write_Base: public File_IO_Base {
0052 private:
0053
0054 static Buffer_Map s_buffermap;
0055
0056 String_Vector m_comment, m_wordsep, m_linesep;
0057 String_Vector m_ignore, m_filebegin, m_fileend;
0058 Char_Vector m_blank;
0059 String_Cube m_filecontent;
0060
0061 char m_escape, m_namesplit;
0062
0063 vtc::code m_vectortype;
0064 mtc::code m_matrixtype;
0065
0066 bool m_ignorecase, m_ignoreblanks, m_exactmatch, m_interprete, m_cmode;
0067 bool m_allownans;
0068
0069 size_t m_occurrence;
0070
0071 Algebra_Interpreter *p_interpreter;
0072
0073 void Init();
0074
0075 protected:
0076
0077 template <class Type> Type Default() const;
0078
0079 size_t Find(std::string input,std::string parameter,
0080 size_t &length) const;
0081
0082 std::string &KillBlanks(std::string &buffer) const;
0083 std::string &KillComments(std::string &buffer) const;
0084 std::string &KillIgnored(std::string &buffer) const;
0085
0086 inline String_Matrix &FileContent(const size_t i=0)
0087 { return m_filecontent[i]; }
0088
0089 char PrevChar(String_Vector &buffer,
0090 int &line,int &pos) const;
0091 char NextChar(String_Vector &buffer,
0092 int &line,int &pos) const;
0093 void InterpreteBuffer(String_Vector &buffer,
0094 int &line,int &pos,
0095 const int level,const bool keep);
0096 void InterpreteBuffer(String_Vector &buffer);
0097 void SplitInFileName(const size_t &i);
0098
0099 public:
0100
0101
0102 Read_Write_Base(const unsigned int infiles,
0103 const unsigned int outfiles);
0104 Read_Write_Base(const unsigned int infiles,const unsigned int outfiles,
0105 const std::string &wordsep,const std::string &linesep,
0106 const std::string &comment,const std::string &ignore="");
0107
0108
0109 ~Read_Write_Base();
0110
0111
0112 bool OpenInFile(const unsigned int i=0,const int mode=0);
0113 bool OpenOutFile(const unsigned int i=0);
0114
0115 void CloseInFile(const unsigned int i=0,const int mode=0);
0116 void CloseOutFile(const unsigned int i=0,const int mode=0);
0117
0118 std::string StripEscapes(const std::string &buffer) const;
0119
0120 void AddFileContent(std::string line,const unsigned int i=0);
0121
0122 bool IsBlank(const char &ch) const;
0123
0124 size_t IsComment(const std::string &ch) const;
0125 size_t IsWordSeparator(const std::string &ch) const;
0126 size_t IsLineSeparator(const std::string &ch) const;
0127
0128 size_t Find(std::string input,std::string parameter) const;
0129
0130
0131 inline void SetBlank(const char blank)
0132 { m_blank.clear(); m_blank.push_back(blank); }
0133 inline void SetBlank(const Char_Vector &blank)
0134 { m_blank=blank; }
0135 inline void AddBlank(const char blank)
0136 { m_blank.push_back(blank); }
0137 inline void AddBlank(const Char_Vector &blank)
0138 { m_blank.insert(m_blank.end(),blank.begin(),blank.end()); }
0139
0140 inline void SetComment(const std::string comment)
0141 { m_comment.clear(); m_comment.push_back(comment); }
0142 inline void SetComment(const String_Vector &comment)
0143 { m_comment=comment; }
0144 inline void AddComment(const std::string comment)
0145 { m_comment.push_back(comment); }
0146 inline void AddComment(const String_Vector &comment)
0147 { m_comment.insert(m_comment.end(),comment.begin(),comment.end()); }
0148
0149 inline void SetWordSeparator(const std::string separator)
0150 { m_wordsep.clear(); m_wordsep.push_back(separator); }
0151 inline void SetWordSeparator(const String_Vector &separator)
0152 { m_wordsep=separator; }
0153 inline void AddWordSeparator(const std::string separator)
0154 { m_wordsep.push_back(separator); }
0155 inline void AddWordSeparator(const String_Vector &separator)
0156 { m_wordsep.insert(m_wordsep.end(),
0157 separator.begin(),separator.end()); }
0158
0159 inline void SetLineSeparator(const std::string separator)
0160 { m_linesep.clear(); m_linesep.push_back(separator); }
0161 inline void SetLineSeparator(const String_Vector &separator)
0162 { m_linesep=separator; }
0163 inline void AddLineSeparator(const std::string separator)
0164 { m_linesep.push_back(separator); }
0165 inline void AddLineSeparator(const String_Vector &separator)
0166 { m_linesep.insert(m_linesep.end(),
0167 separator.begin(),separator.end()); }
0168
0169 inline void SetIgnore(const std::string ignore)
0170 { m_ignore.clear(); m_ignore.push_back(ignore); }
0171 inline void SetIgnore(const String_Vector &ignore)
0172 { m_ignore=ignore; }
0173 inline void AddIgnore(const std::string ignore)
0174 { m_ignore.push_back(ignore); }
0175 inline void AddIgnore(const String_Vector &ignore)
0176 { m_ignore.insert(m_ignore.end(),ignore.begin(),ignore.end()); }
0177
0178 inline void SetOccurrence(const size_t occurrence)
0179 { m_occurrence=occurrence; }
0180
0181 inline void SetVectorType(const vtc::code vectortype)
0182 { m_vectortype=vectortype; }
0183 inline void SetMatrixType(const mtc::code matrixtype)
0184 { m_matrixtype=matrixtype; }
0185
0186 inline void SetAllowNans(const bool allownans)
0187 { m_allownans=allownans; }
0188
0189 inline void SetIgnoreCase(const bool ignorecase)
0190 { m_ignorecase=ignorecase; }
0191 inline void SetIgnoreBlanks(const bool ignoreblanks)
0192 { m_ignoreblanks=ignoreblanks; }
0193
0194 inline void SetExactMatch(const bool exact)
0195 { m_exactmatch=exact; }
0196 inline void SetInterprete(const bool interprete)
0197 { m_interprete=interprete; }
0198 inline void SetCMode(const bool cmode)
0199 { m_cmode=cmode; }
0200
0201 inline void SetEscape(const char escape)
0202 { m_escape=escape; }
0203 inline void SetNameSplit(const char namesplit)
0204 { m_namesplit=namesplit; }
0205
0206
0207 inline const Char_Vector &Blank() const
0208 { return m_blank; }
0209
0210 inline const String_Vector &Comment() const
0211 { return m_comment; }
0212 inline const String_Vector &WordSeparator() const
0213 { return m_wordsep; }
0214 inline const String_Vector &LineSeparator() const
0215 { return m_linesep; }
0216
0217 inline const String_Vector &Ignore() const
0218 { return m_ignore; }
0219 inline const String_Vector &FileBegin() const
0220 { return m_filebegin; }
0221 inline const String_Vector &FileEnd() const
0222 { return m_fileend; }
0223
0224 inline size_t Occurrence() const
0225 { return m_occurrence; }
0226
0227 inline vtc::code VectorType() const
0228 { return m_vectortype; }
0229 inline mtc::code MatrixType() const
0230 { return m_matrixtype; }
0231
0232 inline bool AllowNans() const
0233 { return m_allownans; }
0234
0235 inline bool IgnoreCase() const
0236 { return m_ignorecase; }
0237 inline bool IgnoreBlanks() const
0238 { return m_ignoreblanks; }
0239
0240 inline bool ExactMatch() const
0241 { return m_exactmatch; }
0242 inline bool Interprete() const
0243 { return m_interprete; }
0244 inline bool CMode() const
0245 { return m_cmode; }
0246
0247 inline char Escape() const
0248 { return m_escape; }
0249 inline char NameSplit() const
0250 { return m_namesplit; }
0251
0252 inline bool RereadInFile(const unsigned int i=0)
0253 { CloseInFile(i,1); return OpenInFile(i,1); }
0254 inline bool RescanInFile(const unsigned int i=0)
0255 { CloseInFile(i,0); return OpenInFile(i,2); }
0256
0257 inline Algebra_Interpreter *Interpreter() const
0258 { return p_interpreter; }
0259
0260 inline const String_Vector &BufferContent(const size_t i=0) const
0261 { return s_buffermap[InputPath(i)+InputFile(i)]; }
0262
0263 };
0264
0265 }
0266
0267 #endif