This repository was archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_json_helpers.h
More file actions
158 lines (133 loc) · 2.96 KB
/
native_json_helpers.h
File metadata and controls
158 lines (133 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef NATIVE_JSON_HELPERS_H
#define NATIVE_JSON_HELPERS_H
#include <string>
#include <istream>
#include "rapidjson/rapidjson.h"
namespace utils {
class StringWriteStream
{
std::string &buf_;
public:
typedef char Ch; //!< Character type. Only support char.
StringWriteStream(std::string &buf) : buf_(buf)
{
}
void Put(char c)
{
buf_.push_back(c);
}
void PutN(char c, size_t n)
{
buf_.append(n, c);
}
void Flush()
{
}
};
class StdStreamReadStream
{
public:
typedef char Ch; //!< Character type. Only support char.
StdStreamReadStream(std::istream &str) : str_(str)
{
rdbuf_ = str.rdbuf();
}
~StdStreamReadStream()
{
}
const static int eof = -1;
char Peek() const
{
int res=rdbuf_->sgetc();
if (res!=eof)
return res;
throw std::bad_exception();
}
char Take()
{
int res=rdbuf_->sgetc();
if (res==eof)
throw std::bad_exception();
int bumped=rdbuf_->sbumpc();
if (bumped==eof)
read_more();
return res;
}
void read_more()
{
str_.sync();
}
size_t Tell() const
{
return str_.tellg();
}
std::string GetSubSequence(size_t pos, size_t ln) const
{
str_.seekg(pos, std::ios_base::beg);
if (str_.fail() || str_.eof())
return "";
std::string res;
res.resize(ln);
str_.read(&res[0], ln);
if (str_.eof()) //Got EOF
res.resize(str_.gcount());
return res;
}
// Not implemented
void Put(char c) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); }
char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
private:
std::istream &str_;
std::basic_streambuf<char> *rdbuf_;
};
class BufReadStream
{
public:
typedef char Ch; //!< Character type. Only support char.
BufReadStream(char* buffer, size_t bufferSize) :
buffer_(buffer),
bufferLast_(buffer+bufferSize), current_(buffer_),
eof_(false)
{
}
char Peek() const { return *current_; }
char Take() { char c = *current_; Read(); return c; }
size_t Tell() const { return (current_ - buffer_); }
std::string GetSubSequence(size_t pos, size_t ln) const
{
size_t sz=(bufferLast_-buffer_);
if (pos>=sz)
return "";
if (ln >= sz-pos)
ln = sz-pos;
return jstring_t(buffer_+pos, ln);
}
// Not implemented
void Put(char c) { RAPIDJSON_ASSERT(false); }
void Flush() { RAPIDJSON_ASSERT(false); }
char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
private:
void Read()
{
if (current_ < bufferLast_)
++current_;
else
eof_ = true;
}
char *buffer_;
char *bufferLast_;
char *current_;
bool eof_;
};
}; //namespace utils
namespace rapidjson {
//! Implement specialized version of PutN() with memset() for better performance.
template<>
inline void PutN(utils::StringWriteStream& stream, char c, size_t n) {
stream.PutN(c, n);
}
}; //namespace rapidjson
#endif //NATIVE_JSON_HELPERS_H