15 #ifndef GENUA_XMLELEMENT_H
16 #define GENUA_XMLELEMENT_H
19 #include "sharedvector.h"
28 #include <boost/shared_array.hpp>
70 typedef std::map<std::string, std::string> StringMap;
72 typedef ElementList::const_iterator const_iterator;
73 typedef ElementList::iterator mutable_iterator;
74 typedef StringMap::const_iterator attr_iterator;
83 XmlElement(
const std::string & tg,
const std::string & content)
88 m_tag(std::move(a.
m_tag)), m_txt(std::move(a.m_txt)),
101 m_tag = std::move(a.m_tag);
102 m_txt = std::move(a.m_txt);
106 m_blob = std::move(a.m_blob);
132 const std::string &
attribute(
const std::string & key)
const;
135 const std::string &
attribute(
const std::string & key,
136 const std::string & strDefault)
const;
145 double attr2float(
const std::string & key,
double x)
const;
148 int attr2int(
const std::string & key,
int x)
const;
151 bool attr2bool(
const std::string & key,
bool x)
const;
154 template <
class Streamable>
161 std::stringstream ss;
175 const std::string &
text()
const {
return m_txt;}
184 void text(
const std::string & s) {
192 void text(
const char *ptr,
size_t n) {
196 m_txt.assign(ptr, ptr+n);
206 const_iterator
findChild(
const std::string & s)
const;
237 const std::string &childText = std::string());
240 template <
class ElementType>
242 size_t nvalues,
const ElementType values[],
246 childElement[
"count"] = std::to_string(nvalues);
247 childElement.
asBinary(nvalues, values, share);
248 size_t idx = this->
append( std::move(childElement) );
267 template <
typename ScalarType>
268 void asBinary(
size_t nval,
const ScalarType a[],
bool share =
false) {
269 TypeCode tc = TypeCode::of<ScalarType>();
271 m_nbytes = nval *
sizeof(ScalarType);
276 memcpy(&
m_blob[0], a, m_nbytes);
287 void fetch(
size_t n,
int a[])
const {
292 void fetch(
size_t n, uint a[])
const {
297 void fetch(
size_t n, int8_t a[])
const {
302 void fetch(
size_t n, uint8_t a[])
const {
307 void fetch(
size_t n, int16_t a[])
const {
312 void fetch(
size_t n, uint16_t a[])
const {
317 void fetch(
size_t n, int64_t a[])
const {
322 void fetch(
size_t n, uint64_t a[])
const {
327 void fetch(
size_t n,
float a[])
const {
332 void fetch(
size_t n,
double a[])
const {
337 void fetch(
size_t n, std::complex<float> a[])
const {
342 void fetch(
size_t n, std::complex<double> a[])
const {
365 bool read(std::istream &in,
int format);
368 bool write(std::ostream &os,
int format)
const;
371 void read(
const std::string & fname);
380 BinFileNodePtr
toGbf(
bool share =
true)
const;
383 void fromGbf(
const BinFileNodePtr &bfp,
bool share=
true);
393 void array2text(
size_t n,
const double a[]) {
asBinary(n, a,
false);}
396 void array2text(
size_t n,
const float a[]) {
asBinary(n, a,
false);}
399 void array2text(
size_t n,
const int a[]) {
asBinary(n, a,
false);}
402 void array2text(
size_t n,
const uint a[]) {
asBinary(n, a,
false);}
405 size_t text2array(
size_t n,
double a[])
const {
fetch(n,a);
return n;}
408 size_t text2array(
size_t n,
float a[])
const {
fetch(n,a);
return n;}
411 size_t text2array(
size_t n,
int a[])
const {
fetch(n,a);
return n;}
414 size_t text2array(
size_t n, uint a[])
const {
fetch(n,a);
return n;}
417 void zwrite(
const std::string & zfile,
int compression = 1)
const;
420 void zread(
const std::string & zfile);
425 void zwrite(std::ostream & xs,
ZipFile & zf, uint indent)
const;
431 void xwrite(
const std::string & fname)
const;
434 void xread(std::istream & in);
437 void xwrite(std::ostream & os, uint indent=0)
const;
440 template <
typename Scalar>
446 throw Error(
"XmlElement::fetch() Requested more data than stored in this node.");
449 throw Error(std::string(
"XmlElement::fetch() Type mismatch: ")
450 +tc.
toString()+
" != "+TypeCode::of<Scalar>().toString());
452 }
else if (not m_txt.empty()) {
453 const char *pos = m_txt.c_str();
456 for (i=0; i<n; ++i) {
462 std::string msg =
"XmlElement::fetch(int) failed to find "
463 "enough elements in CDATA.";
464 msg +=
" Node: " +
name() +
" Expected: " + std::to_string(n)
465 +
" Found: " + std::to_string(i);
466 msg +=
" Text: " + std::string(pos).substr(0, 6) +
"...";
Minimal frontend to zlib.
Definition: zipfile.h:34
XmlElement(XmlElement &&a)
move constructor (turns out MSVC doesn't auto-generate this one)
Definition: xmlelement.h:87
mutable_iterator mbegin()
iterate over children
Definition: xmlelement.h:215
BlobType blob() const
access shared pointer to block of binary data
Definition: xmlelement.h:356
void fetchAnything(size_t n, Scalar a[]) const
extract array from text content
Definition: xmlelement.h:441
void xread(std::istream &in)
read node recursively from plain text file, ignore binary data
Definition: xmlelement.cpp:379
void fetch(size_t n, int16_t a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:307
std::string & text()
change text
Definition: xmlelement.h:178
const XmlElement * findNode(const std::string &path) const
find first node matching path, return 0 if not found
Definition: xmlelement.cpp:55
const char * blobPointer() const
access pointer to binary payload
Definition: xmlelement.h:350
bool write(std::ostream &os, int format) const
write to stream in specified format
Definition: xmlelement.cpp:211
int m_typecode
type of binary data included
Definition: xmlelement.h:487
void binaryToText(std::ostream &os) const
dump binary data to text stream
Definition: xmlelement.cpp:481
int value() const
access value
Definition: typecode.h:66
XmlElement & operator=(XmlElement &&a)
move assignment
Definition: xmlelement.h:99
double attr2float(const std::string &key, double x) const
if attribute present, convert to float, else return default
Definition: xmlelement.cpp:139
Group in HDF5 file.
Definition: hdf5file.h:251
void fetch(size_t n, int64_t a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:317
void fetch(size_t n, uint a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:292
void rename(const std::string &s)
change tag name
Definition: xmlelement.h:125
const_iterator findChild(const std::string &s) const
find first child with tag "tg"
Definition: xmlelement.cpp:46
int blobType() const
typecode for binary payload
Definition: xmlelement.h:347
void fromGbf(const BinFileNodePtr &bfp, bool share=true)
construct from GBF representation
Definition: xmlelement.cpp:759
bool empty() const
an element is empty if there are no children, attributes, nor payload
Definition: xmlelement.h:116
boost::shared_ptr< char[]> BlobType
storage for binary data
Definition: xmlelement.h:68
static TypeCode fromString(const std::string &s)
create type code from string
Definition: typecode.cpp:31
void fetch(size_t n, int8_t a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:297
size_t append(const XmlElement &c)
append a child element
Definition: xmlelement.h:221
attr_iterator attrEnd() const
attribute iterator access
Definition: xmlelement.h:142
XmlElement(const std::string &tg, const std::string &content)
shortcut constructor for simple string elements
Definition: xmlelement.h:83
std::string & attribute(const std::string &key)
access attribute
Definition: xmlelement.h:169
void fetch(size_t n, std::complex< double > a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:342
void detach()
make a deep copy of contained data
Definition: xmlelement.cpp:180
bool fromAttribute(const std::string &key, Streamable &sth) const
use std::stringstream to set attrbute only when present
Definition: xmlelement.h:155
void fetch(size_t n, uint16_t a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:312
void zread(const std::string &zfile)
read from zip file
Definition: xmlelement.cpp:609
BinFileNodePtr toGbf(bool share=true) const
return GBF representation
Definition: xmlelement.cpp:725
attr_iterator attrBegin() const
attribute iterator access
Definition: xmlelement.h:139
mutable_iterator mend()
iterate over children
Definition: xmlelement.h:218
uint children() const
count child elements
Definition: xmlelement.h:253
void shrink()
shrink members to minimum size
Definition: xmlelement.cpp:166
void replace(uint k, const XmlElement &xe)
replace child element
Definition: xmlelement.h:261
Exception base class.
Definition: xcept.h:48
const std::string & name() const
access tag
Definition: xmlelement.h:122
std::string & operator[](const std::string &key)
access attribute
Definition: xmlelement.h:172
bool attr2bool(const std::string &key, bool x) const
if attribute present, convert to bool, else return default
Definition: xmlelement.cpp:157
bool extract(const char *raw, ValueType &x) const
convert from memory location to value
Definition: typecode.h:149
size_t append(XmlElement &&c)
append a child element
Definition: xmlelement.h:227
void xwrite(const std::string &fname) const
write root element to plain ascii xml file
Definition: xmlelement.cpp:248
bool hasAttribute(const std::string &key) const
check if attribute exists
Definition: xmlelement.h:128
int attr2int(const std::string &key, int x) const
if attribute present, convert to int, else return default
Definition: xmlelement.cpp:148
int width() const
width of the type in bytes
Definition: typecode.h:69
void eraseChild(uint k)
erase child element k
Definition: xmlelement.h:256
void fetch(size_t n, float a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:327
XML Element.
Definition: xmlelement.h:60
void text(const char *ptr, size_t n)
change text
Definition: xmlelement.h:192
StorageFormat
supported on-disk file formats
Definition: xmlelement.h:65
bool read(std::istream &in, int format)
read from stream in specified format
Definition: xmlelement.cpp:190
const XmlElement * findAnyTag(const std::string &tag) const
find first element matching tag, using depth-first search
Definition: xmlelement.cpp:86
XmlElement(std::string &&tg)
create an element named tg, common case with temporary as tag
Definition: xmlelement.h:80
size_t m_nbytes
number of bytes stored
Definition: xmlelement.h:493
void fetch(size_t n, int a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:287
const std::string & attribute(const std::string &key) const
access attribute
Definition: xmlelement.cpp:120
size_t replaceAppend(const XmlElement &c)
replace if already present, else append a child element
Definition: xmlelement.cpp:100
const char * toString() const
string representation of current code value
Definition: typecode.cpp:23
Integer type code.
Definition: typecode.h:38
BlobType m_blob
storage for binary data
Definition: xmlelement.h:490
StringMap m_attributes
attribute map
Definition: xmlelement.h:481
void zwrite(const std::string &zfile, int compression=1) const
store into zip file
Definition: xmlelement.cpp:526
const std::string & text() const
access text
Definition: xmlelement.h:175
void asBinary(size_t nval, const ScalarType a[], bool share=false)
register binary instead of character data payload, copy or link content
Definition: xmlelement.h:268
void fetch(size_t n, uint8_t a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:302
std::string m_tag
tag name and enclosed text
Definition: xmlelement.h:478
XmlElement(const std::string &tg="")
create an element named tg
Definition: xmlelement.h:77
void fetch(size_t n, std::complex< float > a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:337
const_iterator begin() const
iterate over children
Definition: xmlelement.h:200
size_t blobBytes() const
number of bytes in blob
Definition: xmlelement.h:353
void text(const std::string &s)
change text
Definition: xmlelement.h:184
void fetch(size_t n, double a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:332
XmlElement & append(const std::string &childTag, size_t nvalues, const ElementType values[], bool share=false)
for convenience: Create an labeled child element containing vector data
Definition: xmlelement.h:241
void fetch(size_t n, uint64_t a[]) const
universal access functions which use binary or text (exception on size mismatch)
Definition: xmlelement.h:322
const_iterator end() const
iterate over children
Definition: xmlelement.h:203
ElementList m_siblings
children
Definition: xmlelement.h:484