cphot 0.1
A C++ tool for computing photometry from spectra.
prettywriter.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_PRETTYWRITER_H_
16 #define RAPIDJSON_PRETTYWRITER_H_
17 
18 #include "writer.h"
19 
20 #ifdef __GNUC__
21 RAPIDJSON_DIAG_PUSH
22 RAPIDJSON_DIAG_OFF(effc++)
23 #endif
24 
26 
27 //! Combination of PrettyWriter format flags.
28 /*! \see PrettyWriter::SetFormatOptions
29  */
31  kFormatDefault = 0, //!< Default pretty formatting.
32  kFormatSingleLineArray = 1 //!< Format arrays on a single line.
33 };
34 
35 //! Writer with indentation and spacing.
36 /*!
37  \tparam OutputStream Type of ouptut os.
38  \tparam SourceEncoding Encoding of source string.
39  \tparam TargetEncoding Encoding of output stream.
40  \tparam StackAllocator Type of allocator for allocating memory of stack.
41 */
42 template<typename OutputStream, typename SourceEncoding = UTF8<>, typename TargetEncoding = UTF8<>, typename StackAllocator = CrtAllocator, unsigned writeFlags = kWriteDefaultFlags>
43 class PrettyWriter : public Writer<OutputStream, SourceEncoding, TargetEncoding, StackAllocator, writeFlags> {
44 public:
46  typedef typename Base::Ch Ch;
47 
48  //! Constructor
49  /*! \param os Output stream.
50  \param allocator User supplied allocator. If it is null, it will create a private one.
51  \param levelDepth Initial capacity of stack.
52  */
53  explicit PrettyWriter(OutputStream& os, StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
54  Base(os, allocator, levelDepth), indentChar_(' '), indentCharCount_(4), formatOptions_(kFormatDefault) {}
55 
56 
57  explicit PrettyWriter(StackAllocator* allocator = 0, size_t levelDepth = Base::kDefaultLevelDepth) :
58  Base(allocator, levelDepth), indentChar_(' '), indentCharCount_(4) {}
59 
60  //! Set custom indentation.
61  /*! \param indentChar Character for indentation. Must be whitespace character (' ', '\\t', '\\n', '\\r').
62  \param indentCharCount Number of indent characters for each indentation level.
63  \note The default indentation is 4 spaces.
64  */
65  PrettyWriter& SetIndent(Ch indentChar, unsigned indentCharCount) {
66  RAPIDJSON_ASSERT(indentChar == ' ' || indentChar == '\t' || indentChar == '\n' || indentChar == '\r');
67  indentChar_ = indentChar;
68  indentCharCount_ = indentCharCount;
69  return *this;
70  }
71 
72  //! Set pretty writer formatting options.
73  /*! \param options Formatting options.
74  */
76  formatOptions_ = options;
77  return *this;
78  }
79 
80  /*! @name Implementation of Handler
81  \see Handler
82  */
83  //@{
84 
85  bool Null() { PrettyPrefix(kNullType); return Base::WriteNull(); }
86  bool Bool(bool b) { PrettyPrefix(b ? kTrueType : kFalseType); return Base::WriteBool(b); }
87  bool Int(int i) { PrettyPrefix(kNumberType); return Base::WriteInt(i); }
88  bool Uint(unsigned u) { PrettyPrefix(kNumberType); return Base::WriteUint(u); }
89  bool Int64(int64_t i64) { PrettyPrefix(kNumberType); return Base::WriteInt64(i64); }
91  bool Double(double d) { PrettyPrefix(kNumberType); return Base::WriteDouble(d); }
92 
93  bool RawNumber(const Ch* str, SizeType length, bool copy = false) {
94  RAPIDJSON_ASSERT(str != 0);
95  (void)copy;
97  return Base::WriteString(str, length);
98  }
99 
100  bool String(const Ch* str, SizeType length, bool copy = false) {
101  RAPIDJSON_ASSERT(str != 0);
102  (void)copy;
104  return Base::WriteString(str, length);
105  }
106 
107 #if RAPIDJSON_HAS_STDSTRING
108  bool String(const std::basic_string<Ch>& str) {
109  return String(str.data(), SizeType(str.size()));
110  }
111 #endif
112 
113  bool StartObject() {
115  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
116  return Base::WriteStartObject();
117  }
118 
119  bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
120 
121 #if RAPIDJSON_HAS_STDSTRING
122  bool Key(const std::basic_string<Ch>& str) {
123  return Key(str.data(), SizeType(str.size()));
124  }
125 #endif
126 
127  bool EndObject(SizeType memberCount = 0) {
128  (void)memberCount;
129  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
130  RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray);
131  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
132 
133  if (!empty) {
134  Base::os_->Put('\n');
135  WriteIndent();
136  }
137  bool ret = Base::WriteEndObject();
138  (void)ret;
139  RAPIDJSON_ASSERT(ret == true);
140  if (Base::level_stack_.Empty()) // end of json text
141  Base::os_->Flush();
142  return true;
143  }
144 
145  bool StartArray() {
147  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
148  return Base::WriteStartArray();
149  }
150 
151  bool EndArray(SizeType memberCount = 0) {
152  (void)memberCount;
153  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
154  RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
155  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
156 
157  if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
158  Base::os_->Put('\n');
159  WriteIndent();
160  }
161  bool ret = Base::WriteEndArray();
162  (void)ret;
163  RAPIDJSON_ASSERT(ret == true);
164  if (Base::level_stack_.Empty()) // end of json text
165  Base::os_->Flush();
166  return true;
167  }
168 
169  //@}
170 
171  /*! @name Convenience extensions */
172  //@{
173 
174  //! Simpler but slower overload.
175  bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
176  bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
177 
178  //@}
179 
180  //! Write a raw JSON value.
181  /*!
182  For user to write a stringified JSON as a value.
183 
184  \param json A well-formed JSON value. It should not contain null character within [0, length - 1] range.
185  \param length Length of the json.
186  \param type Type of the root of json.
187  \note When using PrettyWriter::RawValue(), the result json may not be indented correctly.
188  */
189  bool RawValue(const Ch* json, size_t length, Type type) {
190  RAPIDJSON_ASSERT(json != 0);
191  PrettyPrefix(type);
192  return Base::WriteRawValue(json, length);
193  }
194 
195 protected:
196  void PrettyPrefix(Type type) {
197  (void)type;
198  if (Base::level_stack_.GetSize() != 0) { // this value is not at root
199  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
200 
201  if (level->inArray) {
202  if (level->valueCount > 0) {
203  Base::os_->Put(','); // add comma if it is not the first element in array
205  Base::os_->Put(' ');
206  }
207 
209  Base::os_->Put('\n');
210  WriteIndent();
211  }
212  }
213  else { // in object
214  if (level->valueCount > 0) {
215  if (level->valueCount % 2 == 0) {
216  Base::os_->Put(',');
217  Base::os_->Put('\n');
218  }
219  else {
220  Base::os_->Put(':');
221  Base::os_->Put(' ');
222  }
223  }
224  else
225  Base::os_->Put('\n');
226 
227  if (level->valueCount % 2 == 0)
228  WriteIndent();
229  }
230  if (!level->inArray && level->valueCount % 2 == 0)
231  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
232  level->valueCount++;
233  }
234  else {
235  RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root.
236  Base::hasRoot_ = true;
237  }
238  }
239 
240  void WriteIndent() {
241  size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
242  PutN(*Base::os_, static_cast<typename TargetEncoding::Ch>(indentChar_), count);
243  }
244 
248 
249 private:
250  // Prohibit copy constructor & assignment operator.
251  PrettyWriter(const PrettyWriter&);
252  PrettyWriter& operator=(const PrettyWriter&);
253 };
254 
256 
257 #ifdef __GNUC__
258 RAPIDJSON_DIAG_POP
259 #endif
260 
261 #endif // RAPIDJSON_RAPIDJSON_H_
RAPIDJSON_NAMESPACE_END
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
Writer::WriteRawValue
bool WriteRawValue(const Ch *json, size_t length)
Definition: writer.h:434
kNullType
@ kNullType
null
Definition: rapidjson.h:604
PrettyWriter::EndArray
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:151
RAPIDJSON_NAMESPACE_BEGIN
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
Writer::WriteDouble
bool WriteDouble(double d)
Definition: writer.h:323
PrettyWriter::String
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:100
kFalseType
@ kFalseType
false
Definition: rapidjson.h:605
kArrayType
@ kArrayType
array
Definition: rapidjson.h:608
PrettyWriter::Int64
bool Int64(int64_t i64)
Definition: prettywriter.h:89
Type
Type
Type of JSON value.
Definition: rapidjson.h:603
Writer::hasRoot_
bool hasRoot_
Definition: writer.h:473
PrettyWriter::SetIndent
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:65
PrettyWriter::indentChar_
Ch indentChar_
Definition: prettywriter.h:245
PrettyWriter::StartArray
bool StartArray()
Definition: prettywriter.h:145
kObjectType
@ kObjectType
object
Definition: rapidjson.h:607
Writer::WriteEndArray
bool WriteEndArray()
Definition: writer.h:432
Writer::Level::valueCount
size_t valueCount
number of values in this level
Definition: writer.h:264
Writer::WriteUint64
bool WriteUint64(uint64_t u64)
Definition: writer.h:314
PrettyWriter::StartObject
bool StartObject()
Definition: prettywriter.h:113
Writer::WriteString
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:351
kFormatSingleLineArray
@ kFormatSingleLineArray
Format arrays on a single line.
Definition: prettywriter.h:32
SizeType
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:380
Writer::WriteUint
bool WriteUint(unsigned u)
Definition: writer.h:296
PrettyWriter::Base
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > Base
Definition: prettywriter.h:45
kStringType
@ kStringType
string
Definition: rapidjson.h:609
PrettyFormatOptions
PrettyFormatOptions
Combination of PrettyWriter format flags.
Definition: prettywriter.h:30
Writer::WriteStartArray
bool WriteStartArray()
Definition: writer.h:431
Writer
JSON writer.
Definition: fwd.h:95
RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
PrettyWriter::Null
bool Null()
Definition: prettywriter.h:85
PrettyWriter
Writer with indentation and spacing.
Definition: fwd.h:100
Writer::WriteBool
bool WriteBool(bool b)
Definition: writer.h:275
uint64_t
unsigned __int64 uint64_t
Definition: stdint.h:136
kNumberType
@ kNumberType
number
Definition: rapidjson.h:610
PrettyWriter::Bool
bool Bool(bool b)
Definition: prettywriter.h:86
Writer::WriteInt64
bool WriteInt64(int64_t i64)
Definition: writer.h:305
PrettyWriter::WriteIndent
void WriteIndent()
Definition: prettywriter.h:240
Writer::WriteEndObject
bool WriteEndObject()
Definition: writer.h:430
PrettyWriter::SetFormatOptions
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
Definition: prettywriter.h:75
internal::Stack::GetSize
size_t GetSize() const
Definition: stack.h:176
PrettyWriter::Uint64
bool Uint64(uint64_t u64)
Definition: prettywriter.h:90
Writer::Level::inArray
bool inArray
true if in array, otherwise in object
Definition: writer.h:265
Writer::level_stack_
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:471
PrettyWriter::Int
bool Int(int i)
Definition: prettywriter.h:87
PrettyWriter::PrettyWriter
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:53
Writer::os_
OutputStream * os_
Definition: writer.h:470
int64_t
signed __int64 int64_t
Definition: stdint.h:135
PrettyWriter::Key
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:119
PrettyWriter::Double
bool Double(double d)
Definition: prettywriter.h:91
PrettyWriter::Key
bool Key(const Ch *str)
Definition: prettywriter.h:176
PrettyWriter::Uint
bool Uint(unsigned u)
Definition: prettywriter.h:88
Writer::WriteStartObject
bool WriteStartObject()
Definition: writer.h:429
Writer::Ch
SourceEncoding::Ch Ch
Definition: writer.h:89
PrettyWriter::PrettyPrefix
void PrettyPrefix(Type type)
Definition: prettywriter.h:196
kTrueType
@ kTrueType
true
Definition: rapidjson.h:606
PrettyWriter::String
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:175
kFormatDefault
@ kFormatDefault
Default pretty formatting.
Definition: prettywriter.h:31
Writer::kDefaultLevelDepth
static const size_t kDefaultLevelDepth
Definition: writer.h:268
PrettyWriter::EndObject
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:127
Writer::WriteNull
bool WriteNull()
Definition: writer.h:270
PrettyWriter::RawValue
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
Definition: prettywriter.h:189
Writer::WriteInt
bool WriteInt(int i)
Definition: writer.h:287
PrettyWriter::indentCharCount_
unsigned indentCharCount_
Definition: prettywriter.h:246
PrettyWriter::Ch
Base::Ch Ch
Definition: prettywriter.h:46
Writer::Level
Information for each nested level.
Definition: writer.h:262
PrettyWriter::formatOptions_
PrettyFormatOptions formatOptions_
Definition: prettywriter.h:247
PrettyWriter::PrettyWriter
PrettyWriter(StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Definition: prettywriter.h:57
PrettyWriter::RawNumber
bool RawNumber(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:93
PutN
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:94
internal::StrLen
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:30
writer.h