diff --git a/lib/serialisation/BinaryIO.cc b/lib/serialisation/BinaryIO.cc index f546ddb4..250285e5 100644 --- a/lib/serialisation/BinaryIO.cc +++ b/lib/serialisation/BinaryIO.cc @@ -1,36 +1,43 @@ #include +using namespace Grid; +using namespace std; -namespace Grid { // Writer implementation /////////////////////////////////////////////////////// -BinaryWriter::BinaryWriter(const std::string &fileName) -: file_(fileName, std::ios::binary|std::ios::out) +BinaryWriter::BinaryWriter(const string &fileName) +: file_(fileName, ios::binary|ios::out) {} template <> -void BinaryWriter::writeDefault(const std::string &s, const std::string &output) +void BinaryWriter::writeDefault(const string &s, const string &x) { - uint64_t sz = output.size(); + uint64_t sz = x.size(); + + write("", sz); + for (uint64_t i = 0; i < sz; ++i) + { + write("", x[i]); + } +} + +void BinaryWriter::writeDefault(const string &s, const char *x) +{ + string sx(x); - write("", sz); - for (uint64_t i = 0; i < sz; ++i) - { - write("", output[i]); - } + writeDefault(s, sx); } // Reader implementation /////////////////////////////////////////////////////// -BinaryReader::BinaryReader(const std::string &fileName) -: file_(fileName, std::ios::binary|std::ios::in) +BinaryReader::BinaryReader(const string &fileName) +: file_(fileName, ios::binary|ios::in) {} template <> -void BinaryReader::readDefault(const std::string &s, std::string &output) +void BinaryReader::readDefault(const string &s, string &output) { - uint64_t sz; + uint64_t sz; - read("", sz); - output.reserve(sz); - file_.read((char *)output.data(), sz); -} + read("", sz); + output.resize(sz); + file_.read((char *)output.data(), sz); } diff --git a/lib/serialisation/BinaryIO.h b/lib/serialisation/BinaryIO.h index 004ab9a8..f0cfb941 100644 --- a/lib/serialisation/BinaryIO.h +++ b/lib/serialisation/BinaryIO.h @@ -22,6 +22,7 @@ namespace Grid { void writeDefault(const std::string &s, const U &x); template void writeDefault(const std::string &s, const std::vector &x); + void writeDefault(const std::string &s, const char *x); private: std::ofstream file_; }; @@ -48,6 +49,9 @@ namespace Grid { file_.write((char *)&x, sizeof(U)); } + template <> + void BinaryWriter::writeDefault(const std::string &s, const std::string &x); + template void BinaryWriter::writeDefault(const std::string &s, const std::vector &x) { @@ -67,6 +71,9 @@ namespace Grid { file_.read((char *)&output, sizeof(U)); } + template <> + void BinaryReader::readDefault(const std::string &s, std::string &output); + template void BinaryReader::readDefault(const std::string &s, std::vector &output) { diff --git a/lib/serialisation/TextIO.cc b/lib/serialisation/TextIO.cc index 8b9c4f01..a1caa8db 100644 --- a/lib/serialisation/TextIO.cc +++ b/lib/serialisation/TextIO.cc @@ -1,12 +1,14 @@ #include -namespace Grid { +using namespace Grid; +using namespace std; + // Writer implementation /////////////////////////////////////////////////////// -TextWriter::TextWriter(const std::string &fileName) -: file_(fileName, std::ios::out) +TextWriter::TextWriter(const string &fileName) +: file_(fileName, ios::out) {} -void TextWriter::push(const std::string &s) +void TextWriter::push(const string &s) { level_++; }; @@ -25,11 +27,11 @@ void TextWriter::indent(void) }; // Reader implementation /////////////////////////////////////////////////////// -TextReader::TextReader(const std::string &fileName) -: file_(fileName, std::ios::in) +TextReader::TextReader(const string &fileName) +: file_(fileName, ios::in) {} -void TextReader::push(const std::string &s) +void TextReader::push(const string &s) { level_++; }; @@ -48,9 +50,9 @@ void TextReader::checkIndent(void) file_.get(c); if (c != '\t') { - std::cerr << "mismatch on tab " << c << " level " << level_; - std::cerr << " i "<< i < void TextReader::readDefault(const std::string &s, std::string &output) { - checkIndent(); - output.clear(); - getline(file_, output); -} + checkIndent(); + output.clear(); + getline(file_, output); } diff --git a/lib/serialisation/TextIO.h b/lib/serialisation/TextIO.h index 9b69b188..a60931d1 100644 --- a/lib/serialisation/TextIO.h +++ b/lib/serialisation/TextIO.h @@ -20,9 +20,9 @@ namespace Grid void push(const std::string &s); void pop(void); template - void writeDefault(const std::string &s, const U &output); + void writeDefault(const std::string &s, const U &x); template - void writeDefault(const std::string &s, const std::vector &output); + void writeDefault(const std::string &s, const std::vector &x); private: void indent(void); private: @@ -50,21 +50,21 @@ namespace Grid // Writer template implementation //////////////////////////////////////////// template - void TextWriter::writeDefault(const std::string &s, const U &output) + void TextWriter::writeDefault(const std::string &s, const U &x) { indent(); - file_ << std::boolalpha << output << std::endl; + file_ << std::boolalpha << x << std::endl; } template - void TextWriter::writeDefault(const std::string &s, const std::vector &output) + void TextWriter::writeDefault(const std::string &s, const std::vector &x) { - uint64_t sz = output.size(); + uint64_t sz = x.size(); write(s, sz); for (uint64_t i = 0; i < sz; ++i) { - write(s, output[i]); + write(s, x[i]); } } @@ -78,6 +78,9 @@ namespace Grid fromString(output, buf); } + template <> + void TextReader::readDefault(const std::string &s, std::string &output); + template void TextReader::readDefault(const std::string &s, std::vector &output) { diff --git a/lib/serialisation/XmlIO.cc b/lib/serialisation/XmlIO.cc index b5c6e5bd..8dd93de7 100644 --- a/lib/serialisation/XmlIO.cc +++ b/lib/serialisation/XmlIO.cc @@ -1,8 +1,10 @@ #include -namespace Grid { +using namespace Grid; +using namespace std; + // Writer implementation /////////////////////////////////////////////////////// -XmlWriter::XmlWriter(const std::string &fileName) +XmlWriter::XmlWriter(const string &fileName) : fileName_(fileName) { node_ = doc_.append_child(); @@ -14,7 +16,7 @@ XmlWriter::~XmlWriter(void) doc_.save_file(fileName_.c_str(), " "); } -void XmlWriter::push(const std::string &s) +void XmlWriter::push(const string &s) { node_ = node_.append_child(s.c_str()); } @@ -25,22 +27,22 @@ void XmlWriter::pop(void) } // Reader implementation /////////////////////////////////////////////////////// -XmlReader::XmlReader(const std::string &fileName) +XmlReader::XmlReader(const string &fileName) : fileName_(fileName) { pugi::xml_parse_result result = doc_.load_file(fileName_.c_str()); if ( !result ) { - std::cerr << "XML error description: " << result.description() << "\n"; - std::cerr << "XML error offset : " << result.offset << "\n"; - std::abort(); + cerr << "XML error description: " << result.description() << "\n"; + cerr << "XML error offset : " << result.offset << "\n"; + abort(); } node_ = doc_.child("grid"); } -void XmlReader::push(const std::string &s) +void XmlReader::push(const string &s) { node_ = node_.child(s.c_str()); } @@ -51,8 +53,7 @@ void XmlReader::pop(void) } template <> -void XmlReader::readDefault(const std::string &s, std::string &output) +void XmlReader::readDefault(const string &s, string &output) { output = node_.child(s.c_str()).first_child().value(); } -} diff --git a/lib/serialisation/XmlIO.h b/lib/serialisation/XmlIO.h index ca34ee59..15bb0206 100644 --- a/lib/serialisation/XmlIO.h +++ b/lib/serialisation/XmlIO.h @@ -81,6 +81,9 @@ namespace Grid fromString(output, buf); } + template <> + void XmlReader::readDefault(const std::string &s, std::string &output); + template void XmlReader::readDefault(const std::string &s, std::vector &output) {