Cantera  2.1.2
XML_Writer.h
1 #ifndef CT_XML_WRITER
2 #define CT_XML_WRITER
3 
4 // Note: this class is only used by TransportFactory, and is likely to
5 // go away a a future date.
6 
7 #include <iostream>
8 
9 namespace Cantera
10 {
11 
12 ////////////////////// XML_Writer //////////////////////////
13 
14 class XML_Writer
15 {
16 public:
17  XML_Writer(std::ostream& output_) :
18  m_s(output_), _indent(" "), _level(0) {}
19  virtual ~XML_Writer() {}
20  std::ostream& m_s;
21 
22  std::string _indent;
23  int _level;
24 
25  std::ostream& output() {
26  return m_s;
27  }
28 
29  inline std::string XML_filter(const std::string& name) {
30  int ns = static_cast<int>(name.size());
31  std::string nm(name);
32  for (int m = 0; m < ns; m++)
33  if (name[m] == ' '
34  || name[m] == '('
35  || name[m] == ')') {
36  nm[m] = '_';
37  }
38  return nm;
39  }
40 
41  /**
42  * XML_comment()
43  *
44  * Add a comment element to the current XML output file
45  * Comment elements start with <!-- and end with -->
46  * Comments are indented according to the current lvl,
47  * _level
48  *
49  * input
50  * ---------
51  * s : Output stream containing the XML file
52  * comment : Reference to a string containing the comment
53  */
54  inline void XML_comment(std::ostream& s, const std::string& comment) {
55  for (int n = 0; n < _level; n++) {
56  s << _indent;
57  }
58  s << "<!--" << comment << "-->" << std::endl;
59  }
60 
61  inline void XML_open(std::ostream& s, const std::string& tag, const std::string& p = "") {
62  for (int n = 0; n < _level; n++) {
63  s << _indent;
64  }
65  _level++;
66  s << "<" << XML_filter(tag) << p << ">" << std::endl;
67  }
68 
69  inline void XML_close(std::ostream& s, const std::string& tag) {
70  _level--;
71  for (int n = 0; n < _level; n++) {
72  s << _indent;
73  }
74  s << "</" << XML_filter(tag) << ">" << std::endl;
75  }
76 
77  template<class T>
78  void XML_item(std::ostream& s, const std::string& tag, T value) {
79  for (int n = 0; n < _level; n++) {
80  s << _indent;
81  }
82  s << "<" << XML_filter(tag) << ">"
83  << value << "</" << tag << ">" << std::endl;
84  }
85 
86  template<class iter>
87  void XML_writeVector(std::ostream& s, const std::string& indent,
88  const std::string& name, int vsize, iter v) {
89  int ni;
90  for (ni = 0; ni < _level; ni++) {
91  s << _indent;
92  }
93  s << "<" << XML_filter(name) << "> ";
94 
95  int n = vsize;
96  int n5 = n/5;
97  int i, j, k = 0;
98  for (j = 0; j < n5; j++) {
99  for (i = 0; i < 5; i++) {
100  s << v[k] << (k < n - 1 ? ", " : "");
101  k++;
102  }
103  if (j < n5-1) {
104  s << std::endl;
105  for (ni = 0; ni < _level; ni++) {
106  s << _indent;
107  }
108  }
109  }
110  for (i = k; i < n; i++) {
111  s << v[k] << (k < n - 1 ? ", " : "");
112  k++;
113  }
114 
115  s << "</" << XML_filter(name) << ">" << std::endl;
116  }
117 };
118 
119 }
120 
121 #endif