1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-09 21:50:45 +01:00

minor serial IO fixes, XML now issues warning when trying to read absent nodes, these becomes

This commit is contained in:
Antonin Portelli 2017-12-01 19:44:07 +00:00
parent 25f73018f4
commit 2427a21428
4 changed files with 41 additions and 18 deletions

View File

@ -25,7 +25,7 @@
See the full license in the file "LICENSE" in the top level distribution directory See the full license in the file "LICENSE" in the top level distribution directory
*************************************************************************************/ *************************************************************************************/
/* END LEGAL */ /* END LEGAL */
#include <Grid.h> #include <Grid/Grid.h>
using namespace Grid; using namespace Grid;
using namespace std; using namespace std;

View File

@ -125,7 +125,11 @@ static inline void write(Writer<T> &WR,const std::string &s, const cname &obj){
}\ }\
template <typename T>\ template <typename T>\
static inline void read(Reader<T> &RD,const std::string &s, cname &obj){ \ static inline void read(Reader<T> &RD,const std::string &s, cname &obj){ \
push(RD,s);\ if (!push(RD,s))\
{\
std::cout << Grid::GridLogWarning << "IO: Cannot open node '" << s << "'" << std::endl;\
return;\
};\
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_READ_MEMBER,__VA_ARGS__)) \ GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_READ_MEMBER,__VA_ARGS__)) \
pop(RD);\ pop(RD);\
}\ }\

View File

@ -100,13 +100,16 @@ XmlReader::XmlReader(const string &fileName,string toplev) : fileName_(fileName)
bool XmlReader::push(const string &s) bool XmlReader::push(const string &s)
{ {
if (node_.child(s.c_str()))
{
node_ = node_.child(s.c_str());
if (node_.child(s.c_str()) == NULL ) return true;
}
else
{
return false; return false;
}
node_ = node_.child(s.c_str());
return true;
} }
void XmlReader::pop(void) void XmlReader::pop(void)
@ -117,20 +120,30 @@ void XmlReader::pop(void)
bool XmlReader::nextElement(const std::string &s) bool XmlReader::nextElement(const std::string &s)
{ {
if (node_.next_sibling(s.c_str())) if (node_.next_sibling(s.c_str()))
{ {
node_ = node_.next_sibling(s.c_str()); node_ = node_.next_sibling(s.c_str());
return true; return true;
} }
else else
{ {
return false; return false;
} }
} }
template <> template <>
void XmlReader::readDefault(const string &s, string &output) void XmlReader::readDefault(const string &s, string &output)
{ {
output = node_.child(s.c_str()).first_child().value(); if (node_.child(s.c_str()))
{
output = node_.child(s.c_str()).first_child().value();
}
else
{
std::cout << GridLogWarning << "XML: cannot open node '" << s << "'";
std::cout << std::endl;
output = "";
}
} }

View File

@ -39,6 +39,7 @@ Author: paboyle <paboyle@ph.ed.ac.uk>
#include <cassert> #include <cassert>
#include <Grid/pugixml/pugixml.h> #include <Grid/pugixml/pugixml.h>
#include <Grid/GridCore.h>
namespace Grid namespace Grid
{ {
@ -119,7 +120,6 @@ namespace Grid
std::string buf; std::string buf;
readDefault(s, buf); readDefault(s, buf);
// std::cout << s << " " << buf << std::endl;
fromString(output, buf); fromString(output, buf);
} }
@ -132,7 +132,13 @@ namespace Grid
std::string buf; std::string buf;
unsigned int i = 0; unsigned int i = 0;
push(s); if (!push(s))
{
std::cout << GridLogWarning << "XML: cannot open node '" << s << "'";
std::cout << std::endl;
return;
}
while (node_.child("elem")) while (node_.child("elem"))
{ {
output.resize(i + 1); output.resize(i + 1);