mirror of
				https://github.com/paboyle/Grid.git
				synced 2025-11-04 05:54:32 +00:00 
			
		
		
		
	XML interface polish, XML fragments can be pushed into a writer
This commit is contained in:
		@@ -248,7 +248,6 @@ class GridLimeReader : public BinaryIO {
 | 
				
			|||||||
  template<class serialisable_object>
 | 
					  template<class serialisable_object>
 | 
				
			||||||
  void readLimeObject(serialisable_object &object,std::string object_name,std::string record_name)
 | 
					  void readLimeObject(serialisable_object &object,std::string object_name,std::string record_name)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    std::string xmlstring;
 | 
					 | 
				
			||||||
    // should this be a do while; can we miss a first record??
 | 
					    // should this be a do while; can we miss a first record??
 | 
				
			||||||
    while ( limeReaderNextRecord(LimeR) == LIME_SUCCESS ) { 
 | 
					    while ( limeReaderNextRecord(LimeR) == LIME_SUCCESS ) { 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -262,7 +261,8 @@ class GridLimeReader : public BinaryIO {
 | 
				
			|||||||
	limeReaderReadData((void *)&xmlc[0], &nbytes, LimeR);    
 | 
						limeReaderReadData((void *)&xmlc[0], &nbytes, LimeR);    
 | 
				
			||||||
	//	std::cout << GridLogMessage<< " readLimeObject matches XML " << &xmlc[0] <<std::endl;
 | 
						//	std::cout << GridLogMessage<< " readLimeObject matches XML " << &xmlc[0] <<std::endl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	XmlReader RD(&xmlc[0],"");
 | 
					  std::string xmlstring(&xmlc[0]);
 | 
				
			||||||
 | 
						XmlReader RD(xmlstring, true, "");
 | 
				
			||||||
	read(RD,object_name,object);
 | 
						read(RD,object_name,object);
 | 
				
			||||||
	return;
 | 
						return;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@@ -698,9 +698,11 @@ class IldgReader : public GridLimeReader {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	//////////////////////////////////
 | 
						//////////////////////////////////
 | 
				
			||||||
	// ILDG format record
 | 
						// ILDG format record
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  std::string xmlstring(&xmlc[0]);
 | 
				
			||||||
	if ( !strncmp(limeReaderType(LimeR), ILDG_FORMAT,strlen(ILDG_FORMAT)) ) { 
 | 
						if ( !strncmp(limeReaderType(LimeR), ILDG_FORMAT,strlen(ILDG_FORMAT)) ) { 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	  XmlReader RD(&xmlc[0],"");
 | 
						  XmlReader RD(xmlstring, true, "");
 | 
				
			||||||
	  read(RD,"ildgFormat",ildgFormat_);
 | 
						  read(RD,"ildgFormat",ildgFormat_);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	  if ( ildgFormat_.precision == 64 ) format = std::string("IEEE64BIG");
 | 
						  if ( ildgFormat_.precision == 64 ) format = std::string("IEEE64BIG");
 | 
				
			||||||
@@ -715,13 +717,13 @@ class IldgReader : public GridLimeReader {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ( !strncmp(limeReaderType(LimeR), ILDG_DATA_LFN,strlen(ILDG_DATA_LFN)) ) {
 | 
						if ( !strncmp(limeReaderType(LimeR), ILDG_DATA_LFN,strlen(ILDG_DATA_LFN)) ) {
 | 
				
			||||||
	  FieldMetaData_.ildg_lfn = std::string(&xmlc[0]);
 | 
						  FieldMetaData_.ildg_lfn = xmlstring;
 | 
				
			||||||
	  found_ildgLFN = 1;
 | 
						  found_ildgLFN = 1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ( !strncmp(limeReaderType(LimeR), GRID_FORMAT,strlen(ILDG_FORMAT)) ) { 
 | 
						if ( !strncmp(limeReaderType(LimeR), GRID_FORMAT,strlen(ILDG_FORMAT)) ) { 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	  XmlReader RD(&xmlc[0],"");
 | 
						  XmlReader RD(xmlstring, true, "");
 | 
				
			||||||
	  read(RD,"FieldMetaData",FieldMetaData_);
 | 
						  read(RD,"FieldMetaData",FieldMetaData_);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	  format = FieldMetaData_.floating_point;
 | 
						  format = FieldMetaData_.floating_point;
 | 
				
			||||||
@@ -735,18 +737,17 @@ class IldgReader : public GridLimeReader {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ( !strncmp(limeReaderType(LimeR), SCIDAC_RECORD_XML,strlen(SCIDAC_RECORD_XML)) ) { 
 | 
						if ( !strncmp(limeReaderType(LimeR), SCIDAC_RECORD_XML,strlen(SCIDAC_RECORD_XML)) ) { 
 | 
				
			||||||
	  std::string xmls(&xmlc[0]);
 | 
					 | 
				
			||||||
	  // is it a USQCD info field
 | 
						  // is it a USQCD info field
 | 
				
			||||||
	  if ( xmls.find(std::string("usqcdInfo")) != std::string::npos ) { 
 | 
						  if ( xmlstring.find(std::string("usqcdInfo")) != std::string::npos ) { 
 | 
				
			||||||
	    //	    std::cout << GridLogMessage<<"...found a usqcdInfo field"<<std::endl;
 | 
						    //	    std::cout << GridLogMessage<<"...found a usqcdInfo field"<<std::endl;
 | 
				
			||||||
	    XmlReader RD(&xmlc[0],"");
 | 
						    XmlReader RD(xmlstring, true, "");
 | 
				
			||||||
	    read(RD,"usqcdInfo",usqcdInfo_);
 | 
						    read(RD,"usqcdInfo",usqcdInfo_);
 | 
				
			||||||
	    found_usqcdInfo = 1;
 | 
						    found_usqcdInfo = 1;
 | 
				
			||||||
	  }
 | 
						  }
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if ( !strncmp(limeReaderType(LimeR), SCIDAC_CHECKSUM,strlen(SCIDAC_CHECKSUM)) ) { 
 | 
						if ( !strncmp(limeReaderType(LimeR), SCIDAC_CHECKSUM,strlen(SCIDAC_CHECKSUM)) ) { 
 | 
				
			||||||
	  XmlReader RD(&xmlc[0],"");
 | 
						  XmlReader RD(xmlstring, true, "");
 | 
				
			||||||
	  read(RD,"scidacChecksum",scidacChecksum_);
 | 
						  read(RD,"scidacChecksum",scidacChecksum_);
 | 
				
			||||||
	  found_scidacChecksum = 1;
 | 
						  found_scidacChecksum = 1;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,6 +31,17 @@ Author: paboyle <paboyle@ph.ed.ac.uk>
 | 
				
			|||||||
using namespace Grid;
 | 
					using namespace Grid;
 | 
				
			||||||
using namespace std;
 | 
					using namespace std;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void Grid::xmlCheckParse(const pugi::xml_parse_result &result, const std::string name)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (!result) 
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    std::cerr << "XML parsing error for " << name << std::endl;
 | 
				
			||||||
 | 
					    std::cerr << "XML error description: " << result.description() << std::endl;
 | 
				
			||||||
 | 
					    std::cerr << "XML error offset     : " << result.offset << std::endl;
 | 
				
			||||||
 | 
					    abort();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Writer implementation ///////////////////////////////////////////////////////
 | 
					// Writer implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
XmlWriter::XmlWriter(const string &fileName, string toplev) : fileName_(fileName)
 | 
					XmlWriter::XmlWriter(const string &fileName, string toplev) : fileName_(fileName)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -56,7 +67,14 @@ void XmlWriter::push(const string &s)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void XmlWriter::pushXmlString(const std::string &s)
 | 
					void XmlWriter::pushXmlString(const std::string &s)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  pugi::xml_document doc;
 | 
				
			||||||
 | 
					  auto               result = doc.load_buffer(s.c_str(), s.size());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  xmlCheckParse(result, "fragment\n'" + s +"'");
 | 
				
			||||||
 | 
					  for (pugi::xml_node child = doc.first_child(); child; child = child.next_sibling())
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					      node_ = node_.append_copy(child);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void XmlWriter::pop(void)
 | 
					void XmlWriter::pop(void)
 | 
				
			||||||
@@ -71,8 +89,23 @@ std::string XmlWriter::XmlString(void)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Reader implementation ///////////////////////////////////////////////////////
 | 
					// Reader implementation ///////////////////////////////////////////////////////
 | 
				
			||||||
void XmlReader::initDoc(const std::string &toplev)
 | 
					XmlReader::XmlReader(const std::string &s,  const bool isBuffer, 
 | 
				
			||||||
 | 
					                     std::string toplev) 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  pugi::xml_parse_result result;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  if (isBuffer)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    fileName_ = "<string>";
 | 
				
			||||||
 | 
					    result    = doc_.load_string(s.c_str());
 | 
				
			||||||
 | 
					    xmlCheckParse(result, "string\n'" + s + "'");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  else
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    fileName_ = s;
 | 
				
			||||||
 | 
					    result    = doc_.load_file(s.c_str());
 | 
				
			||||||
 | 
					    xmlCheckParse(result, "file '" + fileName_ + "'");
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
  if ( toplev == std::string("") ) {
 | 
					  if ( toplev == std::string("") ) {
 | 
				
			||||||
  node_ = doc_;
 | 
					  node_ = doc_;
 | 
				
			||||||
  } else { 
 | 
					  } else { 
 | 
				
			||||||
@@ -80,36 +113,6 @@ void XmlReader::initDoc(const std::string &toplev)
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
XmlReader::XmlReader(const char *xmlstring, const std::string toplev) 
 | 
					 | 
				
			||||||
: fileName_("")
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
  auto result = doc_.load_string(xmlstring);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if ( !result ) {
 | 
					 | 
				
			||||||
    std::cerr << "XML error description (from char *): " 
 | 
					 | 
				
			||||||
              << result.description() << "\nXML\n"<< xmlstring << "\n";
 | 
					 | 
				
			||||||
    std::cerr << "XML error offset      (from char *) " 
 | 
					 | 
				
			||||||
              << result.offset         << "\nXML\n"<< xmlstring << std::endl;
 | 
					 | 
				
			||||||
    abort();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  initDoc(toplev);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
XmlReader::XmlReader(const std::string &fileName, std::string toplev) 
 | 
					 | 
				
			||||||
: fileName_(fileName)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
  auto result = doc_.load_file(fileName_.c_str());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if ( !result ) {
 | 
					 | 
				
			||||||
    std::cerr << "XML error description: " 
 | 
					 | 
				
			||||||
              << result.description() <<" "<< fileName_ <<"\n";
 | 
					 | 
				
			||||||
    std::cerr << "XML error offset     : " 
 | 
					 | 
				
			||||||
              << result.offset        <<" "<< fileName_ << std::endl;
 | 
					 | 
				
			||||||
    abort();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  initDoc(toplev);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
bool XmlReader::push(const std::string &s)
 | 
					bool XmlReader::push(const std::string &s)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  if (node_.child(s.c_str()))
 | 
					  if (node_.child(s.c_str()))
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -43,6 +43,7 @@ Author: paboyle <paboyle@ph.ed.ac.uk>
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
namespace Grid
 | 
					namespace Grid
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  void xmlCheckParse(const pugi::xml_parse_result &result, const std::string name);
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
  class XmlWriter: public Writer<XmlWriter>
 | 
					  class XmlWriter: public Writer<XmlWriter>
 | 
				
			||||||
  {    
 | 
					  {    
 | 
				
			||||||
@@ -66,8 +67,8 @@ namespace Grid
 | 
				
			|||||||
  class XmlReader: public Reader<XmlReader>
 | 
					  class XmlReader: public Reader<XmlReader>
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
  public:
 | 
					  public:
 | 
				
			||||||
    XmlReader(const char *xmlstring, std::string toplev = std::string("grid") );
 | 
					    XmlReader(const std::string &fileName, const bool isBuffer = false, 
 | 
				
			||||||
    XmlReader(const std::string &fileName, std::string toplev = std::string("grid") );
 | 
					              std::string toplev = std::string("grid") );
 | 
				
			||||||
    virtual ~XmlReader(void) = default;
 | 
					    virtual ~XmlReader(void) = default;
 | 
				
			||||||
    bool push(const std::string &s);
 | 
					    bool push(const std::string &s);
 | 
				
			||||||
    void pop(void);
 | 
					    void pop(void);
 | 
				
			||||||
@@ -77,7 +78,7 @@ namespace Grid
 | 
				
			|||||||
    template <typename U>
 | 
					    template <typename U>
 | 
				
			||||||
    void readDefault(const std::string &s, std::vector<U> &output);
 | 
					    void readDefault(const std::string &s, std::vector<U> &output);
 | 
				
			||||||
  private:
 | 
					  private:
 | 
				
			||||||
    void initDoc(const std::string &toplev);
 | 
					    void checkParse(const pugi::xml_parse_result &result, const std::string name);
 | 
				
			||||||
  private:
 | 
					  private:
 | 
				
			||||||
    pugi::xml_document doc_;
 | 
					    pugi::xml_document doc_;
 | 
				
			||||||
    pugi::xml_node     node_;
 | 
					    pugi::xml_node     node_;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user