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>
 | 
					#include <Grid.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					using namespace Grid;
 | 
				
			||||||
 | 
					using namespace std;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Grid {
 | 
					 | 
				
			||||||
// Writer implementation ///////////////////////////////////////////////////////
 | 
					// Writer implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
BinaryWriter::BinaryWriter(const std::string &fileName)
 | 
					BinaryWriter::BinaryWriter(const string &fileName)
 | 
				
			||||||
: file_(fileName, std::ios::binary|std::ios::out)
 | 
					: file_(fileName, ios::binary|ios::out)
 | 
				
			||||||
{}
 | 
					{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template <>
 | 
					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);
 | 
					  writeDefault(s, sx);
 | 
				
			||||||
  for (uint64_t i = 0; i < sz; ++i)
 | 
					 | 
				
			||||||
  {
 | 
					 | 
				
			||||||
    write("", output[i]);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Reader implementation ///////////////////////////////////////////////////////
 | 
					// Reader implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
BinaryReader::BinaryReader(const std::string &fileName)
 | 
					BinaryReader::BinaryReader(const string &fileName)
 | 
				
			||||||
: file_(fileName, std::ios::binary|std::ios::in)
 | 
					: file_(fileName, ios::binary|ios::in)
 | 
				
			||||||
{}
 | 
					{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template <>
 | 
					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);
 | 
					    read("", sz);
 | 
				
			||||||
  output.reserve(sz);
 | 
					    output.resize(sz);
 | 
				
			||||||
  file_.read((char *)output.data(), sz);
 | 
					    file_.read((char *)output.data(), sz);
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,6 +22,7 @@ namespace Grid {
 | 
				
			|||||||
    void writeDefault(const std::string &s, const U &x);
 | 
					    void writeDefault(const std::string &s, const U &x);
 | 
				
			||||||
    template <typename U>
 | 
					    template <typename U>
 | 
				
			||||||
    void writeDefault(const std::string &s, const std::vector<U> &x);
 | 
					    void writeDefault(const std::string &s, const std::vector<U> &x);
 | 
				
			||||||
 | 
					    void writeDefault(const std::string &s, const char *x);
 | 
				
			||||||
  private:
 | 
					  private:
 | 
				
			||||||
    std::ofstream file_;
 | 
					    std::ofstream file_;
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
@@ -48,6 +49,9 @@ namespace Grid {
 | 
				
			|||||||
    file_.write((char *)&x, sizeof(U));
 | 
					    file_.write((char *)&x, sizeof(U));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
 | 
					  template <>
 | 
				
			||||||
 | 
					  void BinaryWriter::writeDefault(const std::string &s, const std::string &x);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  template <typename U>
 | 
					  template <typename U>
 | 
				
			||||||
  void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
 | 
					  void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
@@ -67,6 +71,9 @@ namespace Grid {
 | 
				
			|||||||
    file_.read((char *)&output, sizeof(U));
 | 
					    file_.read((char *)&output, sizeof(U));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
 | 
					  template <>
 | 
				
			||||||
 | 
					  void BinaryReader::readDefault(const std::string &s, std::string &output);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  template <typename U>
 | 
					  template <typename U>
 | 
				
			||||||
  void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
					  void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,12 +1,14 @@
 | 
				
			|||||||
#include <Grid.h>
 | 
					#include <Grid.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Grid {
 | 
					using namespace Grid;
 | 
				
			||||||
 | 
					using namespace std;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Writer implementation ///////////////////////////////////////////////////////
 | 
					// Writer implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
TextWriter::TextWriter(const std::string &fileName)
 | 
					TextWriter::TextWriter(const string &fileName)
 | 
				
			||||||
: file_(fileName, std::ios::out)
 | 
					: file_(fileName, ios::out)
 | 
				
			||||||
{}
 | 
					{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TextWriter::push(const std::string &s)
 | 
					void TextWriter::push(const string &s)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  level_++;
 | 
					  level_++;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@@ -25,11 +27,11 @@ void TextWriter::indent(void)
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Reader implementation ///////////////////////////////////////////////////////
 | 
					// Reader implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
TextReader::TextReader(const std::string &fileName)
 | 
					TextReader::TextReader(const string &fileName)
 | 
				
			||||||
: file_(fileName, std::ios::in)
 | 
					: file_(fileName, ios::in)
 | 
				
			||||||
{}
 | 
					{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void TextReader::push(const std::string &s)
 | 
					void TextReader::push(const string &s)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  level_++;
 | 
					  level_++;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@@ -48,9 +50,9 @@ void TextReader::checkIndent(void)
 | 
				
			|||||||
    file_.get(c);
 | 
					    file_.get(c);
 | 
				
			||||||
    if (c != '\t')
 | 
					    if (c != '\t')
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      std::cerr << "mismatch on tab " << c << " level " << level_;
 | 
					      cerr << "mismatch on tab " << c << " level " << level_;
 | 
				
			||||||
      std::cerr << " i "<< i <<std::endl;
 | 
					      cerr << " i "<< i << endl;
 | 
				
			||||||
      std::abort();
 | 
					      abort();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -58,8 +60,7 @@ void TextReader::checkIndent(void)
 | 
				
			|||||||
template <>
 | 
					template <>
 | 
				
			||||||
void TextReader::readDefault(const std::string &s, std::string &output)
 | 
					void TextReader::readDefault(const std::string &s, std::string &output)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  checkIndent();
 | 
					    checkIndent();
 | 
				
			||||||
  output.clear();
 | 
					    output.clear();
 | 
				
			||||||
  getline(file_, output);
 | 
					    getline(file_, output);
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,9 +20,9 @@ namespace Grid
 | 
				
			|||||||
    void push(const std::string &s);
 | 
					    void push(const std::string &s);
 | 
				
			||||||
    void pop(void);
 | 
					    void pop(void);
 | 
				
			||||||
    template <typename U>
 | 
					    template <typename U>
 | 
				
			||||||
    void writeDefault(const std::string &s, const U &output);
 | 
					    void writeDefault(const std::string &s, const U &x);
 | 
				
			||||||
    template <typename U>
 | 
					    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:
 | 
					  private:
 | 
				
			||||||
    void indent(void);
 | 
					    void indent(void);
 | 
				
			||||||
  private:
 | 
					  private:
 | 
				
			||||||
@@ -50,21 +50,21 @@ namespace Grid
 | 
				
			|||||||
  
 | 
					  
 | 
				
			||||||
  // Writer template implementation ////////////////////////////////////////////
 | 
					  // Writer template implementation ////////////////////////////////////////////
 | 
				
			||||||
  template <typename U>
 | 
					  template <typename U>
 | 
				
			||||||
  void TextWriter::writeDefault(const std::string &s, const U &output)
 | 
					  void TextWriter::writeDefault(const std::string &s, const U &x)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    indent();
 | 
					    indent();
 | 
				
			||||||
    file_ << std::boolalpha << output << std::endl;
 | 
					    file_ << std::boolalpha << x << std::endl;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  template <typename U>
 | 
					  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);
 | 
					    write(s, sz);
 | 
				
			||||||
    for (uint64_t i = 0; i < sz; ++i)
 | 
					    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);
 | 
					    fromString(output, buf);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
 | 
					  template <>
 | 
				
			||||||
 | 
					  void TextReader::readDefault(const std::string &s, std::string &output);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  template <typename U>
 | 
					  template <typename U>
 | 
				
			||||||
  void TextReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
					  void TextReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,10 @@
 | 
				
			|||||||
#include <Grid.h>
 | 
					#include <Grid.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Grid {
 | 
					using namespace Grid;
 | 
				
			||||||
 | 
					using namespace std;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Writer implementation ///////////////////////////////////////////////////////
 | 
					// Writer implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
XmlWriter::XmlWriter(const std::string &fileName)
 | 
					XmlWriter::XmlWriter(const string &fileName)
 | 
				
			||||||
: fileName_(fileName)
 | 
					: fileName_(fileName)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  node_ = doc_.append_child();
 | 
					  node_ = doc_.append_child();
 | 
				
			||||||
@@ -14,7 +16,7 @@ XmlWriter::~XmlWriter(void)
 | 
				
			|||||||
  doc_.save_file(fileName_.c_str(), "  ");
 | 
					  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());
 | 
					  node_ = node_.append_child(s.c_str());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -25,22 +27,22 @@ void XmlWriter::pop(void)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Reader implementation ///////////////////////////////////////////////////////
 | 
					// Reader implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
XmlReader::XmlReader(const std::string &fileName)
 | 
					XmlReader::XmlReader(const string &fileName)
 | 
				
			||||||
: fileName_(fileName)
 | 
					: fileName_(fileName)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  pugi::xml_parse_result result = doc_.load_file(fileName_.c_str());
 | 
					  pugi::xml_parse_result result = doc_.load_file(fileName_.c_str());
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  if ( !result )
 | 
					  if ( !result )
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    std::cerr << "XML error description: " << result.description() << "\n";
 | 
					    cerr << "XML error description: " << result.description() << "\n";
 | 
				
			||||||
    std::cerr << "XML error offset     : " << result.offset        << "\n";
 | 
					    cerr << "XML error offset     : " << result.offset        << "\n";
 | 
				
			||||||
    std::abort();
 | 
					    abort();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  node_ = doc_.child("grid");
 | 
					  node_ = doc_.child("grid");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void XmlReader::push(const std::string &s)
 | 
					void XmlReader::push(const string &s)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  node_ = node_.child(s.c_str());
 | 
					  node_ = node_.child(s.c_str());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -51,8 +53,7 @@ void XmlReader::pop(void)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template <>
 | 
					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();
 | 
					  output = node_.child(s.c_str()).first_child().value();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -81,6 +81,9 @@ namespace Grid
 | 
				
			|||||||
    fromString(output, buf);
 | 
					    fromString(output, buf);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
 | 
					  template <>
 | 
				
			||||||
 | 
					  void XmlReader::readDefault(const std::string &s, std::string &output);
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  template <typename U>
 | 
					  template <typename U>
 | 
				
			||||||
  void XmlReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
					  void XmlReader::readDefault(const std::string &s, std::vector<U> &output)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user