mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-04 05:54:32 +00:00 
			
		
		
		
	IO: code cleaning and string binary IO fix
This commit is contained in:
		@@ -1,36 +1,43 @@
 | 
			
		||||
#include <Grid.h>
 | 
			
		||||
 | 
			
		||||
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("", output[i]);
 | 
			
		||||
        write("", x[i]);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BinaryWriter::writeDefault(const string &s, const char *x)
 | 
			
		||||
{
 | 
			
		||||
  string sx(x);
 | 
			
		||||
  
 | 
			
		||||
  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;
 | 
			
		||||
  
 | 
			
		||||
    read("", sz);
 | 
			
		||||
  output.reserve(sz);
 | 
			
		||||
    output.resize(sz);
 | 
			
		||||
    file_.read((char *)output.data(), sz);
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,7 @@ namespace Grid {
 | 
			
		||||
    void writeDefault(const std::string &s, const U &x);
 | 
			
		||||
    template <typename U>
 | 
			
		||||
    void writeDefault(const std::string &s, const std::vector<U> &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 <typename U>
 | 
			
		||||
  void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &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 <typename U>
 | 
			
		||||
  void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
			
		||||
  {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,14 @@
 | 
			
		||||
#include <Grid.h>
 | 
			
		||||
 | 
			
		||||
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 <<std::endl;
 | 
			
		||||
      std::abort();
 | 
			
		||||
      cerr << "mismatch on tab " << c << " level " << level_;
 | 
			
		||||
      cerr << " i "<< i << endl;
 | 
			
		||||
      abort();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -62,4 +64,3 @@ void TextReader::readDefault(const std::string &s, std::string &output)
 | 
			
		||||
    output.clear();
 | 
			
		||||
    getline(file_, output);
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -20,9 +20,9 @@ namespace Grid
 | 
			
		||||
    void push(const std::string &s);
 | 
			
		||||
    void pop(void);
 | 
			
		||||
    template <typename U>
 | 
			
		||||
    void writeDefault(const std::string &s, const U &output);
 | 
			
		||||
    void writeDefault(const std::string &s, const U &x);
 | 
			
		||||
    template <typename U>
 | 
			
		||||
    void writeDefault(const std::string &s, const std::vector<U> &output);
 | 
			
		||||
    void writeDefault(const std::string &s, const std::vector<U> &x);
 | 
			
		||||
  private:
 | 
			
		||||
    void indent(void);
 | 
			
		||||
  private:
 | 
			
		||||
@@ -50,21 +50,21 @@ namespace Grid
 | 
			
		||||
  
 | 
			
		||||
  // Writer template implementation ////////////////////////////////////////////
 | 
			
		||||
  template <typename U>
 | 
			
		||||
  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 <typename U>
 | 
			
		||||
  void TextWriter::writeDefault(const std::string &s, const std::vector<U> &output)
 | 
			
		||||
  void TextWriter::writeDefault(const std::string &s, const std::vector<U> &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 <typename U>
 | 
			
		||||
  void TextReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
			
		||||
  {
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,10 @@
 | 
			
		||||
#include <Grid.h>
 | 
			
		||||
 | 
			
		||||
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();
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -81,6 +81,9 @@ namespace Grid
 | 
			
		||||
    fromString(output, buf);
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  template <>
 | 
			
		||||
  void XmlReader::readDefault(const std::string &s, std::string &output);
 | 
			
		||||
  
 | 
			
		||||
  template <typename U>
 | 
			
		||||
  void XmlReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
			
		||||
  {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user