vsg  1.1.0
VulkanSceneGraph library
AsciiOutput.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2018 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8 
9 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10 
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 </editor-fold> */
14 
15 #include <vsg/io/Logger.h>
16 #include <vsg/io/Options.h>
17 #include <vsg/io/Output.h>
18 
19 #include <algorithm>
20 #include <fstream>
21 
22 namespace vsg
23 {
24 
27  class VSG_DECLSPEC AsciiOutput : public vsg::Output
28  {
29  public:
30  explicit AsciiOutput(std::ostream& output, ref_ptr<const Options> in_options = {});
31 
32  std::ostream& indent()
33  {
34  _output.write(_indentationString, std::min(_indentation, _maximumIndentation));
35  return _output;
36  }
37 
39  void writePropertyName(const char* propertyName) override;
40 
42  void writeEndOfLine() override { _output << '\n'; }
43 
44  template<typename T>
45  void _write(size_t num, const T* value)
46  {
47  if (num == 1)
48  {
49  _output << ' ' << *value;
50  }
51  else
52  {
53  for (size_t numInRow = 1; num > 0; --num, ++value, ++numInRow)
54  {
55  _output << ' ' << *value;
56 
57  if (numInRow == _maximumNumbersPerLine && num > 1)
58  {
59  numInRow = 0;
60  writeEndOfLine();
61  indent();
62  }
63  }
64  }
65  }
66 
67  template<typename T>
68  void _write_real(size_t num, const T* value)
69  {
70  if (num == 1)
71  {
72  if (std::isfinite(*value))
73  _output << ' ' << *value;
74  else
75  _output << ' ' << 0.0; // fallback to using 0.0 when the value is NaN or Infinite to prevent problems when reading
76  }
77  else
78  {
79  for (size_t numInRow = 1; num > 0; --num, ++value, ++numInRow)
80  {
81  if (std::isfinite(*value))
82  _output << ' ' << *value;
83  else
84  _output << ' ' << 0.0; // fallback to using 0.0 when the value is NaN or Infinite to prevent problems when reading
85 
86  if (numInRow == _maximumNumbersPerLine && num > 1)
87  {
88  numInRow = 0;
89  writeEndOfLine();
90  indent();
91  }
92  }
93  }
94  }
95 
96  template<typename R, typename T>
97  void _write_withcast(size_t num, const T* value)
98  {
99  if (num == 1)
100  {
101  _output << ' ' << static_cast<R>(*value);
102  }
103  else
104  {
105  for (size_t numInRow = 1; num > 0; --num, ++value, ++numInRow)
106  {
107  _output << ' ' << static_cast<R>(*value);
108 
109  if (numInRow == _maximumNumbersPerLine && num > 1)
110  {
111  numInRow = 0;
112  writeEndOfLine();
113  indent();
114  }
115  }
116  }
117  }
118 
119  // write contiguous array of value(s)
120  void write(size_t num, const int8_t* value) override { _write_withcast<int16_t>(num, value); }
121  void write(size_t num, const uint8_t* value) override { _write_withcast<uint16_t>(num, value); }
122 
123  void write(size_t num, const int16_t* value) override { _write(num, value); }
124  void write(size_t num, const uint16_t* value) override { _write(num, value); }
125  void write(size_t num, const int32_t* value) override { _write(num, value); }
126  void write(size_t num, const uint32_t* value) override { _write(num, value); }
127  void write(size_t num, const int64_t* value) override { _write(num, value); }
128  void write(size_t num, const uint64_t* value) override { _write(num, value); }
129  void write(size_t num, const float* value) override
130  {
131  _output.precision(float_precision);
132  _write_real(num, value);
133  }
134  void write(size_t num, const double* value) override
135  {
136  _output.precision(double_precision);
137  _write_real(num, value);
138  }
139 
140  void _write(const std::string& str)
141  {
142  _output << '"';
143  for (auto c : str)
144  {
145  if (c == '"')
146  _output << "\\\"";
147  else
148  _output << c;
149  }
150  _output << '"';
151  }
152 
153  void _write(const std::wstring& str);
154 
155  void write(size_t num, const std::string* value) override;
156  void write(size_t num, const std::wstring* value) override;
157  void write(size_t num, const Path* value) override;
158 
160  void write(const vsg::Object* object) override;
161 
162  int float_precision = 6;
163  int double_precision = 12;
164 
165  protected:
166  std::ostream& _output;
167  std::size_t _indentationStep = 2;
168  std::size_t _indentation = 0;
169  std::size_t _maximumIndentation = 0;
170  std::size_t _maximumNumbersPerLine = 12;
171  // 24 characters long enough for 12 levels of nesting
172  const char* _indentationString = " ";
173  };
174 
175 } // namespace vsg
Definition: AsciiOutput.h:28
void write(const vsg::Object *object) override
write object
void writeEndOfLine() override
write end of line as an
Definition: AsciiOutput.h:42
void writePropertyName(const char *propertyName) override
write property name if appropriate for format
void write(size_t num, const int8_t *value) override
write contiguous array of value(s)
Definition: AsciiOutput.h:120
Definition: Object.h:42
Definition: Output.h:41
Definition: ref_ptr.h:22