ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_DescriptionWriter.H
Go to the documentation of this file.
1 #ifndef ERF_DESCRIPTION_WRITER_H_
2 #define ERF_DESCRIPTION_WRITER_H_
3 
4 #include <AMReX_BLassert.H>
5 
6 #include <cstddef>
7 #include <ostream>
8 #include <string>
9 #include <vector>
10 
11 namespace erf_description {
12 
13 enum class Format { JSON, YAML };
14 
15 /** Fixed-structure JSON/YAML writer with no parser dependency. YAML output uses block
16  * mappings, JSON flow arrays and double-quoted keys and strings, so YAML 1.1 parsers
17  * never read them as booleans or numbers.
18  */
19 class Writer
20 {
21 public:
22  Writer (std::ostream& output, Format format) : os(output), yaml(format == Format::YAML)
23  {
24  if (!yaml) os << '{';
25  }
26 
27  void begin_object (const char* name)
28  {
29  key(name, true);
30  os << (yaml ? '\n' : '{');
31  first.push_back(true);
32  }
33 
34  void end_object ()
35  {
36  AMREX_ASSERT(first.size() > 1);
37  first.pop_back();
38  if (!yaml) os << '}';
39  }
40 
41  void finish (bool newline = true)
42  {
43  AMREX_ASSERT(first.size() == 1);
44  if (!yaml) { os << '}'; if (newline) os << '\n'; }
45  }
46 
47  void value (const char* name, std::nullptr_t)
48  {
49  key(name); os << "null"; end_value();
50  }
51 
52  void value (const char* name, bool v)
53  {
54  key(name); os << (v ? "true" : "false"); end_value();
55  }
56 
57  void value (const char* name, int v)
58  {
59  key(name); os << std::to_string(v); end_value();
60  }
61 
62  void value (const char* name, const char* v)
63  {
64  key(name);
65  if (v == nullptr) os << "null";
66  else quoted(v);
67  end_value();
68  }
69 
70  // AMReX build-info getters return "" for unknown values.
71  void optional_string (const char* name, const char* v)
72  {
73  value(name, v != nullptr && *v != '\0' ? v : nullptr);
74  }
75 
76  void strings (const char* name, const std::vector<std::string>& values)
77  {
78  key(name);
79  os << '[';
80  for (std::size_t i = 0; i < values.size(); ++i) {
81  if (i) os << ", ";
82  quoted(values[i].c_str());
83  }
84  os << ']';
85  end_value();
86  }
87 
88  template <class T, class Emit>
89  void objects (const char* name, const std::vector<T>& values, Emit emit)
90  {
91  key(name);
92  os << '[';
93  for (std::size_t i = 0; i < values.size(); ++i) {
94  if (i) os << ", ";
95  Writer item(os, Format::JSON);
96  emit(item, values[i]);
97  item.finish(false);
98  }
99  os << ']';
100  end_value();
101  }
102 
103 private:
104  std::ostream& os;
105  bool yaml;
106  std::vector<bool> first{true};
107 
108  void key (const char* name, bool object = false)
109  {
110  if (yaml) {
111  os << std::string((first.size() - 1) * 2, ' ');
112  quoted(name);
113  os << ':';
114  if (!object) os << ' ';
115  } else {
116  if (!first.back()) os << ',';
117  quoted(name);
118  os << ':';
119  }
120  first.back() = false;
121  }
122 
123  void end_value () { if (yaml) os << '\n'; }
124 
125  void quoted (const char* value)
126  {
127  constexpr char hex[] = "0123456789abcdef";
128  os << '"';
129  for (const auto* c = reinterpret_cast<const unsigned char*>(value); *c; ++c) {
130  if (*c == '"' || *c == '\\') {
131  os << '\\' << static_cast<char>(*c);
132  } else if (*c < 0x20 || *c == 0x7f) {
133  os << "\\u00" << hex[*c >> 4] << hex[*c & 15];
134  } else if (*c == 0xc2 && c[1] >= 0x80 && c[1] <= 0x9f) {
135  // C1 controls (incl. NEL, a YAML line break)
136  ++c;
137  os << "\\u00" << hex[*c >> 4] << hex[*c & 15];
138  } else if (*c == 0xe2 && c[1] == 0x80 && (c[2] == 0xa8 || c[2] == 0xa9)) {
139  // U+2028/9, line breaks to YAML 1.1 parsers
140  os << "\\u202" << (c[2] == 0xa8 ? '8' : '9');
141  c += 2;
142  } else {
143  os << static_cast<char>(*c);
144  }
145  }
146  os << '"';
147  }
148 };
149 
150 } // namespace erf_description
151 #endif
erf_description::Format format
Definition: ERF_InputPreflight.cpp:30
std::string name
Definition: ERF_Plotfile2DCatalog.cpp:139
Definition: ERF_DescriptionWriter.H:20
void finish(bool newline=true)
Definition: ERF_DescriptionWriter.H:41
void value(const char *name, bool v)
Definition: ERF_DescriptionWriter.H:52
bool yaml
Definition: ERF_DescriptionWriter.H:105
Writer(std::ostream &output, Format format)
Definition: ERF_DescriptionWriter.H:22
void value(const char *name, std::nullptr_t)
Definition: ERF_DescriptionWriter.H:47
void value(const char *name, int v)
Definition: ERF_DescriptionWriter.H:57
void optional_string(const char *name, const char *v)
Definition: ERF_DescriptionWriter.H:71
void objects(const char *name, const std::vector< T > &values, Emit emit)
Definition: ERF_DescriptionWriter.H:89
std::vector< bool > first
Definition: ERF_DescriptionWriter.H:106
void quoted(const char *value)
Definition: ERF_DescriptionWriter.H:125
void key(const char *name, bool object=false)
Definition: ERF_DescriptionWriter.H:108
void begin_object(const char *name)
Definition: ERF_DescriptionWriter.H:27
void strings(const char *name, const std::vector< std::string > &values)
Definition: ERF_DescriptionWriter.H:76
std::ostream & os
Definition: ERF_DescriptionWriter.H:104
void end_value()
Definition: ERF_DescriptionWriter.H:123
void end_object()
Definition: ERF_DescriptionWriter.H:34
void value(const char *name, const char *v)
Definition: ERF_DescriptionWriter.H:62
Definition: ERF_DescriptionWriter.H:11
Format
Definition: ERF_DescriptionWriter.H:13