summaryrefslogtreecommitdiff
path: root/src/Tangential_complex/benchmark/XML_exporter.h
blob: 16b62eb6b1f266fed814b5fdab73f75436fac4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*    This file is part of the Gudhi Library - https://gudhi.inria.fr/ - which is released under MIT.
 *    See file LICENSE or go to https://gudhi.inria.fr/licensing/ for full license details.
 *    Author(s):       Clement Jamin
 *
 *    Copyright (C) 2016 Inria
 *
 *    Modification(s):
 *      - YYYY/MM Author: Description of the modification
 */

#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <ctime>

template<typename value_type = std::string>
class Simple_XML_exporter {
 public:
  typedef value_type Value_type;
  typedef std::vector<value_type> Element;
  typedef std::map<std::string, value_type> Element_with_map;
  typedef std::vector<Element> List_of_elements;

  Simple_XML_exporter(
                      const std::string &list_name,
                      const std::string &element_name,
                      const std::vector<std::string> &subelement_names,
                      bool add_timestamp = true)
      : m_list_name(list_name),
      m_element_name(element_name),
      m_subelement_names(subelement_names),
      m_add_timestamp(add_timestamp) { }

  bool add_element(const Element &element) {
    if (element.size() == m_subelement_names.size()) {
      m_list_of_elements.push_back(element);
      return true;
    } else {
      std::cerr << "ERROR: element.size() == m_subelement_names.size()" << std::endl;
      return false;
    }
  }

  bool add_element(Element_with_map &element) {
    Element elt;

    std::vector<std::string>::const_iterator
    it_subelement_name = m_subelement_names.begin();
    std::vector<std::string>::const_iterator
    it_subelement_name_end = m_subelement_names.end();
    for (; it_subelement_name != it_subelement_name_end; ++it_subelement_name) {
      elt.push_back(element[*it_subelement_name]);
    }

    return add_element(elt);
  }

  bool export_to_xml(const std::string &filename) const {
    std::ofstream xmlfile;
    xmlfile.open(filename.c_str());
    xmlfile << "<?xml version='1.0'?>" << std::endl;
    xmlfile << "<" << m_list_name << ">" << std::endl;

    typename List_of_elements::const_iterator it_element = m_list_of_elements.begin();
    typename List_of_elements::const_iterator it_element_end = m_list_of_elements.end();
    for (int id = 1; it_element != it_element_end; ++it_element, ++id) {
      xmlfile << "  <" << m_element_name << ">" << std::endl;
      std::vector<std::string>::const_iterator
      it_subelement_name = m_subelement_names.begin();
      std::vector<std::string>::const_iterator
      it_subelement_name_end = m_subelement_names.end();

      if (m_add_timestamp)
        xmlfile << "    <id> " << time(NULL) << " </id>" << std::endl;

      for (int i = 0;
           it_subelement_name != it_subelement_name_end;
           ++it_subelement_name, ++i) {
        xmlfile
            << "    <" << *it_subelement_name << "> "
            << (*it_element)[i]
            << " </" << *it_subelement_name << ">" << std::endl;
      }
      xmlfile << "  </" << m_element_name << ">" << std::endl;
    }

    xmlfile << "</" << m_list_name << ">" << std::endl;
    xmlfile.close();
    return 0;

  }

 protected:
  std::string m_list_name;
  std::string m_element_name;
  std::vector<std::string> m_subelement_names;
  List_of_elements m_list_of_elements;
  bool m_add_timestamp;
};

template<typename value_type = std::string>
class Streaming_XML_exporter {
 public:
  typedef value_type Value_type;
  typedef std::vector<value_type> Element;
  typedef std::map<std::string, value_type> Element_with_map;
  typedef std::vector<Element> List_of_elements;

  Streaming_XML_exporter(
                         const std::string &filename,
                         const std::string &list_name,
                         const std::string &element_name,
                         const std::vector<std::string> &subelement_names,
                         bool add_timestamp = true)
      : m_list_name(list_name),
      m_element_name(element_name),
      m_subelement_names(subelement_names),
      m_add_timestamp(add_timestamp) {
    m_xml_fstream.open(filename.c_str());
    if (m_xml_fstream.good()) {
      m_xml_fstream << "<?xml version='1.0'?>" << std::endl;
      m_xml_fstream << "<" << m_list_name << ">" << std::endl;
    } else {
      std::cerr << "Could not open file '" << filename << "'." << std::endl;
    }
  }

  virtual ~Streaming_XML_exporter() {
    close_file();
  }

  void close_file() {
    m_xml_fstream.close();
  }

  bool add_element(const Element &element) {
    if (element.size() == m_subelement_names.size()) {
      m_xml_fstream << "  <" << m_element_name << ">" << std::endl;
      std::vector<std::string>::const_iterator
      it_subelement_name = m_subelement_names.begin();
      std::vector<std::string>::const_iterator
      it_subelement_name_end = m_subelement_names.end();

      if (m_add_timestamp) {
        m_xml_fstream << "    <id> " << time(NULL) << " </id>" << std::endl;
      }

      for (int i = 0;
           it_subelement_name != it_subelement_name_end;
           ++it_subelement_name, ++i) {
        m_xml_fstream
            << "    <" << *it_subelement_name << "> "
            << element[i]
            << " </" << *it_subelement_name << ">" << std::endl;
      }
      m_xml_fstream << "  </" << m_element_name << ">" << std::endl;

      // Save current pointer position
      std::ofstream::streampos pos = m_xml_fstream.tellp();
      // Close the XML file (temporarily) so that the XML file is always correct
      m_xml_fstream << "</" << m_list_name << ">" << std::endl;
      // Restore the pointer position so that the next "add_element" will overwrite
      // the end of the file
      m_xml_fstream.seekp(pos);

      m_xml_fstream.flush();
      return true;
    } else {
      std::cerr << "ERROR: element.size() == m_subelement_names.size()" << std::endl;
      return false;
    }
  }

  bool add_element(Element_with_map &element) {
    Element elt;

    std::vector<std::string>::const_iterator
    it_subelement_name = m_subelement_names.begin();
    std::vector<std::string>::const_iterator
    it_subelement_name_end = m_subelement_names.end();
    for (; it_subelement_name != it_subelement_name_end; ++it_subelement_name) {
      elt.push_back(element[*it_subelement_name]);
    }

    return add_element(elt);
  }

 protected:
  std::ofstream m_xml_fstream;
  std::string m_list_name;
  std::string m_element_name;
  std::vector<std::string> m_subelement_names;
  bool m_add_timestamp;
};