vsg  1.0.4
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/Options.h>
16 #include <vsg/io/Output.h>
17 
18 #include <algorithm>
19 #include <fstream>
20 
21 namespace vsg
22 {
23 
26  class VSG_DECLSPEC AsciiOutput : public vsg::Output
27  {
28  public:
29  explicit AsciiOutput(std::ostream& output, ref_ptr<const Options> in_options = {});
30 
31  std::ostream& indent()
32  {
33  _output.write(_indentationString, std::min(_indentation, _maximumIndentation));
34  return _output;
35  }
36 
38  void writePropertyName(const char* propertyName) override;
39 
41  void writeEndOfLine() override { _output << '\n'; }
42 
43  template<typename T>
44  void _write(size_t num, const T* value)
45  {
46  if (num == 1)
47  {
48  _output << ' ' << *value;
49  }
50  else
51  {
52  for (size_t numInRow = 1; num > 0; --num, ++value, ++numInRow)
53  {
54  _output << ' ' << *value;
55 
56  if (numInRow == _maximumNumbersPerLine && num > 1)
57  {
58  numInRow = 0;
59  writeEndOfLine();
60  indent();
61  }
62  }
63  }
64  }
65 
66  template<typename T>
67  void _write_real(size_t num, const T* value)
68  {
69  if (num == 1)
70  {
71  if (std::isfinite(*value))
72  _output << ' ' << *value;
73  else
74  _output << ' ' << 0.0; // fallback to using 0.0 when the value is NaN or Infinite to prevent problems when reading
75  }
76  else
77  {
78  for (size_t numInRow = 1; num > 0; --num, ++value, ++numInRow)
79  {
80  if (std::isfinite(*value))
81  _output << ' ' << *value;
82  else
83  _output << ' ' << 0.0; // fallback to using 0.0 when the value is NaN or Infinite to prevent problems when reading
84 
85  if (numInRow == _maximumNumbersPerLine && num > 1)
86  {
87  numInRow = 0;
88  writeEndOfLine();
89  indent();
90  }
91  }
92  }
93  }
94 
95  template<typename R, typename T>
96  void _write_withcast(size_t num, const T* value)
97  {
98  if (num == 1)
99  {
100  _output << ' ' << static_cast<R>(*value);
101  }
102  else
103  {
104  for (size_t numInRow = 1; num > 0; --num, ++value, ++numInRow)
105  {
106  _output << ' ' << static_cast<R>(*value);
107 
108  if (numInRow == _maximumNumbersPerLine && num > 1)
109  {
110  numInRow = 0;
111  writeEndOfLine();
112  indent();
113  }
114  }
115  }
116  }
117 
118  // write contiguous array of value(s)
119  void write(size_t num, const int8_t* value) override { _write_withcast<int16_t>(num, value); }
120  void write(size_t num, const uint8_t* value) override { _write_withcast<uint16_t>(num, value); }
121 
122  void write(size_t num, const int16_t* value) override { _write(num, value); }
123  void write(size_t num, const uint16_t* value) override { _write(num, value); }
124  void write(size_t num, const int32_t* value) override { _write(num, value); }
125  void write(size_t num, const uint32_t* value) override { _write(num, value); }
126  void write(size_t num, const int64_t* value) override { _write(num, value); }
127  void write(size_t num, const uint64_t* value) override { _write(num, value); }
128  void write(size_t num, const float* value) override { _write_real(num, value); }
129  void write(size_t num, const double* value) override { _write_real(num, value); }
130 
131  void _write(const std::string& str)
132  {
133  _output << '"';
134  for (auto c : str)
135  {
136  if (c == '"')
137  _output << "\\\"";
138  else
139  _output << c;
140  }
141  _output << '"';
142  }
143 
144  void write(size_t num, const std::string* value) override;
145  void write(size_t num, const Path* value) override;
146 
148  void write(const vsg::Object* object) override;
149 
150  protected:
151  std::ostream& _output;
152 
153  std::size_t _indentationStep = 2;
154  std::size_t _indentation = 0;
155  std::size_t _maximumIndentation = 0;
156  std::size_t _maximumNumbersPerLine = 12;
157  // 24 characters long enough for 12 levels of nesting
158  const char* _indentationString = " ";
159  };
160 
161 } // namespace vsg
Definition: AsciiOutput.h:27
void write(const vsg::Object *object) override
write object
void writeEndOfLine() override
write end of line as an
Definition: AsciiOutput.h:41
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:119
Definition: Object.h:42
Definition: Output.h:40
Definition: ref_ptr.h:22