RetroArch
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 
28 
33 };
34 
36 
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 
49 
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 
61 
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 
73 
77  return *this;
78  }
79 
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  (void)copy;
96  return Base::WriteString(str, length);
97  }
98 
99  bool String(const Ch* str, SizeType length, bool copy = false) {
100  (void)copy;
102  return Base::WriteString(str, length);
103  }
104 
105 #if RAPIDJSON_HAS_STDSTRING
106  bool String(const std::basic_string<Ch>& str) {
107  return String(str.data(), SizeType(str.size()));
108  }
109 #endif
110 
111  bool StartObject() {
113  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(false);
114  return Base::WriteStartObject();
115  }
116 
117  bool Key(const Ch* str, SizeType length, bool copy = false) { return String(str, length, copy); }
118 
119 #if RAPIDJSON_HAS_STDSTRING
120  bool Key(const std::basic_string<Ch>& str) {
121  return Key(str.data(), SizeType(str.size()));
122  }
123 #endif
124 
125  bool EndObject(SizeType memberCount = 0) {
126  (void)memberCount;
127  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
128  RAPIDJSON_ASSERT(!Base::level_stack_.template Top<typename Base::Level>()->inArray);
129  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
130 
131  if (!empty) {
132  Base::os_->Put('\n');
133  WriteIndent();
134  }
135  bool ret = Base::WriteEndObject();
136  (void)ret;
137  RAPIDJSON_ASSERT(ret == true);
138  if (Base::level_stack_.Empty()) // end of json text
139  Base::os_->Flush();
140  return true;
141  }
142 
143  bool StartArray() {
145  new (Base::level_stack_.template Push<typename Base::Level>()) typename Base::Level(true);
146  return Base::WriteStartArray();
147  }
148 
149  bool EndArray(SizeType memberCount = 0) {
150  (void)memberCount;
151  RAPIDJSON_ASSERT(Base::level_stack_.GetSize() >= sizeof(typename Base::Level));
152  RAPIDJSON_ASSERT(Base::level_stack_.template Top<typename Base::Level>()->inArray);
153  bool empty = Base::level_stack_.template Pop<typename Base::Level>(1)->valueCount == 0;
154 
155  if (!empty && !(formatOptions_ & kFormatSingleLineArray)) {
156  Base::os_->Put('\n');
157  WriteIndent();
158  }
159  bool ret = Base::WriteEndArray();
160  (void)ret;
161  RAPIDJSON_ASSERT(ret == true);
162  if (Base::level_stack_.Empty()) // end of json text
163  Base::os_->Flush();
164  return true;
165  }
166 
168 
171 
173  bool String(const Ch* str) { return String(str, internal::StrLen(str)); }
174  bool Key(const Ch* str) { return Key(str, internal::StrLen(str)); }
175 
177 
179 
188 
189 protected:
191  (void)type;
192  if (Base::level_stack_.GetSize() != 0) { // this value is not at root
193  typename Base::Level* level = Base::level_stack_.template Top<typename Base::Level>();
194 
195  if (level->inArray) {
196  if (level->valueCount > 0) {
197  Base::os_->Put(','); // add comma if it is not the first element in array
199  Base::os_->Put(' ');
200  }
201 
203  Base::os_->Put('\n');
204  WriteIndent();
205  }
206  }
207  else { // in object
208  if (level->valueCount > 0) {
209  if (level->valueCount % 2 == 0) {
210  Base::os_->Put(',');
211  Base::os_->Put('\n');
212  }
213  else {
214  Base::os_->Put(':');
215  Base::os_->Put(' ');
216  }
217  }
218  else
219  Base::os_->Put('\n');
220 
221  if (level->valueCount % 2 == 0)
222  WriteIndent();
223  }
224  if (!level->inArray && level->valueCount % 2 == 0)
225  RAPIDJSON_ASSERT(type == kStringType); // if it's in object, then even number should be a name
226  level->valueCount++;
227  }
228  else {
229  RAPIDJSON_ASSERT(!Base::hasRoot_); // Should only has one and only one root.
230  Base::hasRoot_ = true;
231  }
232  }
233 
234  void WriteIndent() {
235  size_t count = (Base::level_stack_.GetSize() / sizeof(typename Base::Level)) * indentCharCount_;
236  PutN(*Base::os_, static_cast<typename TargetEncoding::Ch>(indentChar_), count);
237  }
238 
242 
243 private:
244  // Prohibit copy constructor & assignment operator.
245  PrettyWriter(const PrettyWriter&);
247 };
248 
250 
251 #ifdef __GNUC__
252 RAPIDJSON_DIAG_POP
253 #endif
254 
255 #endif // RAPIDJSON_RAPIDJSON_H_
Ch indentChar_
Definition: prettywriter.h:239
bool WriteInt64(int64_t i64)
Definition: writer.h:299
Writer with indentation and spacing.
Definition: fwd.h:100
true
Definition: rapidjson.h:606
PrettyWriter & SetFormatOptions(PrettyFormatOptions options)
Set pretty writer formatting options.
Definition: prettywriter.h:75
Format arrays on a single line.
Definition: prettywriter.h:32
bool Uint64(uint64_t u64)
Definition: prettywriter.h:90
array
Definition: rapidjson.h:608
OutputStream * os_
Definition: writer.h:464
string
Definition: rapidjson.h:609
bool WriteDouble(double d)
Definition: writer.h:317
bool Bool(bool b)
Definition: prettywriter.h:86
PrettyWriter & SetIndent(Ch indentChar, unsigned indentCharCount)
Set custom indentation.
Definition: prettywriter.h:65
bool StartArray()
Definition: prettywriter.h:143
bool WriteEndObject()
Definition: writer.h:424
PrettyWriter(StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Definition: prettywriter.h:57
false
Definition: rapidjson.h:605
bool Int64(int64_t i64)
Definition: prettywriter.h:89
object
Definition: rapidjson.h:607
static const char json[]
Definition: namespacetest.cpp:32
bool RawValue(const Ch *json, size_t length, Type type)
Write a raw JSON value.
Definition: prettywriter.h:187
size_t GetSize() const
Definition: stack.h:176
PrettyFormatOptions formatOptions_
Definition: prettywriter.h:241
bool Key(const Ch *str)
Definition: prettywriter.h:174
SizeType StrLen(const Ch *s)
Custom strlen() which works on different character types.
Definition: strfunc.h:30
bool EndArray(SizeType memberCount=0)
Definition: prettywriter.h:149
typedef void(__stdcall *PFN_DESTRUCTION_CALLBACK)(void *pData)
bool Double(double d)
Definition: prettywriter.h:91
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
GLboolean GLboolean GLboolean b
Definition: glext.h:6844
GLuint GLuint GLsizei count
Definition: glext.h:6292
null
Definition: rapidjson.h:604
Default pretty formatting.
Definition: prettywriter.h:31
bool Key(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:117
bool String(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:99
PrettyWriter(OutputStream &os, StackAllocator *allocator=0, size_t levelDepth=Base::kDefaultLevelDepth)
Constructor.
Definition: prettywriter.h:53
GLenum type
Definition: glext.h:6233
bool hasRoot_
Definition: writer.h:467
bool WriteString(const Ch *str, SizeType length)
Definition: writer.h:345
GLint level
Definition: glext.h:6293
number
Definition: rapidjson.h:610
internal::Stack< StackAllocator > level_stack_
Definition: writer.h:465
bool WriteStartArray()
Definition: writer.h:425
SourceEncoding::Ch Ch
Definition: writer.h:89
bool RawNumber(const Ch *str, SizeType length, bool copy=false)
Definition: prettywriter.h:93
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
bool WriteBool(bool b)
Definition: writer.h:269
bool StartObject()
Definition: prettywriter.h:111
void WriteIndent()
Definition: prettywriter.h:234
Unknown compiler Device disconnected from port File already exists Saving to backup buffer Got connection Public address Setting disk in tray You have left the game You have joined with input devices *s *s has joined as player u A netplay connection attempt failed because the peer is not running or is running an old version of RetroArch use the same version use the same version This core does not support inter architecture netplay Incorrect password A netplay client has disconnected You do not have permission to play The input devices requested are not available Netplay peer s paused Give hardware rendered cores their own private context Avoids having to assume hardware state changes inbetween frames Adjusts menu screen appearance settings Improves performance at the cost of latency and more video stuttering Use only if you cannot obtain full speed otherwise Autodetect Capabilities Connecting to port Password Username Accounts List Endpoint Achievements Scan Content Import content Ask Block Frames نظام تشغيل الصوت Audio Enable Turbo Deadzone Audio Maximum Timing Skew Audio Output Dynamic Audio Rate Control الصوت Audio Volume Level(dB)" ) MSG_HASH( MENU_ENUM_LABEL_VALUE_AUDIO_WASAPI_EXCLUSIVE_MODE
bool EndObject(SizeType memberCount=0)
Definition: prettywriter.h:125
bool Int(int i)
Definition: prettywriter.h:87
bool WriteNull()
Definition: writer.h:264
PrettyWriter & operator=(const PrettyWriter &)
void PrettyPrefix(Type type)
Definition: prettywriter.h:190
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
uint64_t u64
64bit unsigned integer
Definition: gctypes.h:20
bool WriteEndArray()
Definition: writer.h:426
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:94
PrettyFormatOptions
Combination of PrettyWriter format flags.
Definition: prettywriter.h:30
bool WriteInt(int i)
Definition: writer.h:281
unsigned indentCharCount_
Definition: prettywriter.h:240
bool Uint(unsigned u)
Definition: prettywriter.h:88
JSON writer.
Definition: fwd.h:95
signed __int64 int64_t
Definition: stdint.h:135
Writer< OutputStream, SourceEncoding, TargetEncoding, StackAllocator > Base
Definition: prettywriter.h:45
bool Null()
Definition: prettywriter.h:85
Type
Type of JSON value.
Definition: rapidjson.h:603
bool WriteUint(unsigned u)
Definition: writer.h:290
bool String(const Ch *str)
Simpler but slower overload.
Definition: prettywriter.h:173
static const size_t kDefaultLevelDepth
Definition: writer.h:262
bool WriteRawValue(const Ch *json, size_t length)
Definition: writer.h:428
Information for each nested level.
Definition: writer.h:256
bool WriteUint64(uint64_t u64)
Definition: writer.h:308
unsigned __int64 uint64_t
Definition: stdint.h:136
GLenum GLuint GLenum GLsizei length
Definition: glext.h:6233
bf_uint8_t options
Definition: connect_ps4.c:78
const char *const str
Definition: portlistingparse.c:18
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:380
Base::Ch Ch
Definition: prettywriter.h:46
bool WriteStartObject()
Definition: writer.h:423