Darwin  1.10(beta)
drwnXMLUtils.h
Go to the documentation of this file.
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2017, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnXMLUtils.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
21 #pragma once
22 
23 #include "Eigen/Core"
24 
25 #include "drwnLogger.h"
26 #include "drwnXMLParser.h"
27 
28 // drwnBase64 ----------------------------------------------------------------
29 
30 namespace drwnBase64 {
32  extern const char *LUT;
34  extern const char FILL;
36  extern bool INSERT_NEWLINES;
37 
40  char *encode(const unsigned char *data, size_t length);
43  unsigned char *decode(const char *data);
44 };
45 
46 // drwnXMLUtils --------------------------------------------------------------
47 
48 typedef enum _drwnXMLEncoderType {
51 } drwnXMLEncoderType;
52 
53 namespace drwnXMLUtils {
55  extern drwnXMLEncoderType DEFAULT_ENCODER;
57  drwnXMLEncoderType getEncoderType(const char *str);
58 
60  drwnXMLNode& serialize(drwnXMLNode& xml, const char *buffer, size_t length);
62  drwnXMLNode& serialize(drwnXMLNode& xml, const Eigen::VectorXd& v);
64  drwnXMLNode& serialize(drwnXMLNode& xml, const Eigen::MatrixXd& m);
65 
67  drwnXMLNode& deserialize(drwnXMLNode& xml, char *buffer, size_t length);
69  drwnXMLNode& deserialize(drwnXMLNode& xml, Eigen::VectorXd& v);
71  drwnXMLNode& deserialize(drwnXMLNode& xml, Eigen::MatrixXd& m);
72 
74  template<typename T>
75  struct save_node { void operator()(const T& o, drwnXMLNode& node) { o.save(node); }; };
76 
78  template<typename T>
79  struct save_node<T *> { void operator()(T* const& o, drwnXMLNode& node) { o->save(node); }; };
80 
83  template<class RandomAccessIterator>
84  void save(drwnXMLNode& xml, const char *tag,
85  RandomAccessIterator first, RandomAccessIterator last) {
87  while (first != last) {
88  drwnXMLNode *node = xml.document()->allocate_node(rapidxml::node_element,
89  xml.document()->allocate_string(tag));
90  xml.append_node(node);
91  functor(*first, *node);
92  ++first;
93  }
94  }
95 
98  template<class Container>
99  void save(drwnXMLNode& xml, const char *tag, const Container& container) {
100  save(xml, tag, container.begin(), container.end());
101  }
102 
105  template<class Container>
106  void write(const char *filename, const char *root, const char *tag,
107  const Container& container) {
108  DRWN_ASSERT(filename != NULL);
109  drwnXMLDoc xml;
110  drwnXMLNode *node = xml.allocate_node(rapidxml::node_element, xml.allocate_string(root));
111  xml.append_node(node);
112  drwnXMLUtils::save(*node, tag, container);
113  ofstream ofs(filename);
114  ofs << xml << endl;
115  ofs.close();
116  }
117 
118 
120  template<typename T>
121  struct load_node {
122  T operator()(drwnXMLNode& node) { T o; o.load(node); return o; };
123  };
124 
127  template<typename T>
128  struct load_node<T *> {
129  T* operator()(drwnXMLNode& node) { T *o = new T(); o->load(node); return o; };
130  };
131 
135  template<class Container>
136  void load(drwnXMLNode& xml, const char *tag, Container& container) {
137  typename Container::iterator it(container.end());
139 
140  for (drwnXMLNode *node = xml.first_node(tag); node != NULL; node = node->next_sibling(tag)) {
141  // assumes default constructable value type
142  it = container.insert(it, functor(*node));
143  ++it;
144  }
145  }
146 
149  template<class Container>
150  void read(const char *filename, const char *root, const char *tag,
151  Container& container) {
152  DRWN_ASSERT(filename != NULL);
153  drwnXMLDoc xml;
154  drwnParseXMLFile(xml, filename, root);
155  DRWN_ASSERT(!drwnIsXMLEmpty(xml));
156  load(xml, tag, container);
157  }
158 
160  void dump(drwnXMLNode& xml);
161 };
void save(drwnXMLNode &xml, const char *tag, const Container &container)
serialize entire container to xml node. Contained type must itself have, or be a pointer to a class w...
Definition: drwnXMLUtils.h:99
const char FILL
base 64 filler
Definition: drwnXMLUtils.cpp:30
void read(const char *filename, const char *root, const char *tag, Container &container)
de-serialize an xml file into container. The container type must be default constructable and have a ...
Definition: drwnXMLUtils.h:150
_drwnXMLEncoderType
Definition: drwnXMLUtils.h:48
Definition: drwnXMLUtils.h:30
Definition: drwnXMLUtils.h:53
char * encode(const unsigned char *data, size_t length)
Encodes a binary sequence as a NULL terminated base64 string. The calling function takes ownership an...
Definition: drwnXMLUtils.cpp:33
encode binary objects using Base-64
Definition: drwnXMLUtils.h:50
void dump(drwnXMLNode &xml)
prints an XML node to standard output for debugging
Definition: drwnXMLUtils.cpp:363
helper class for loading xml nodes into containers of objects
Definition: drwnXMLUtils.h:121
bool drwnIsXMLEmpty(drwnXMLNode &xml)
checks whether an xml document is empty
Definition: drwnXMLParser.cpp:55
void save(drwnXMLNode &xml, const char *tag, RandomAccessIterator first, RandomAccessIterator last)
save container contents in range [first, last). Contained type must itself have, or be a pointer to a...
Definition: drwnXMLUtils.h:84
drwnXMLNode * drwnParseXMLFile(drwnXMLDoc &xml, const char *filename, const char *tag=NULL)
parse an xml file into xml (loads all data into memory) and return a pointer to the first node (with ...
Definition: drwnXMLParser.cpp:22
encode binary objects as ASCII text
Definition: drwnXMLUtils.h:49
unsigned char * decode(const char *data)
Decode a NULL terminated base64 string to binary. The calling function takes ownership and must delet...
Definition: drwnXMLUtils.cpp:93
void write(const char *filename, const char *root, const char *tag, const Container &container)
serialize entire container to an xml file. Contained type must itself have, or be a pointer to a clas...
Definition: drwnXMLUtils.h:106
bool INSERT_NEWLINES
insert newlines to produce 80-column output when encoding
Definition: drwnXMLUtils.cpp:31
Provides XML parsing functionality for serializing and deserializing objects and containers of object...
helper class for saving objects to xml nodes
Definition: drwnXMLUtils.h:75
const char * LUT
base 64 encoding lookup table
Definition: drwnXMLUtils.cpp:28
void load(drwnXMLNode &xml, const char *tag, Container &container)
de-serialize an xml node into container. The container type must itself be, or be a pointer to a type...
Definition: drwnXMLUtils.h:136