| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
1 |
// Tencent is pleased to support the open source community by making RapidJSON available. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
2 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
3 |
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
4 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
5 |
// Licensed under the MIT License (the "License"); you may not use this file except |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
6 |
// in compliance with the License. You may obtain a copy of the License at |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
7 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
8 |
// http://opensource.org/licenses/MIT |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
9 |
// |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
10 |
// Unless required by applicable law or agreed to in writing, software distributed |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
11 |
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
12 |
// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
13 |
// specific language governing permissions and limitations under the License. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
14 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
15 |
#ifndef CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
16 |
#define CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
17 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
18 |
#include "stream.h" |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
19 |
#include <iosfwd> |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
20 |
#include <ios> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
21 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
22 |
#ifdef __clang__ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
23 |
CEREAL_RAPIDJSON_DIAG_PUSH |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
24 |
CEREAL_RAPIDJSON_DIAG_OFF(padded) |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
25 |
#elif defined(_MSC_VER) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
26 |
CEREAL_RAPIDJSON_DIAG_PUSH |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
27 |
CEREAL_RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
28 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
29 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
30 |
CEREAL_RAPIDJSON_NAMESPACE_BEGIN |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
31 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
32 |
//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
33 |
/*! |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
34 |
The classes can be wrapped including but not limited to: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
35 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
36 |
- \c std::istringstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
37 |
- \c std::stringstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
38 |
- \c std::wistringstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
39 |
- \c std::wstringstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
40 |
- \c std::ifstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
41 |
- \c std::fstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
42 |
- \c std::wifstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
43 |
- \c std::wfstream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
44 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
45 |
\tparam StreamType Class derived from \c std::basic_istream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
46 |
*/ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
47 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
48 |
template <typename StreamType> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
49 |
class BasicIStreamWrapper { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
50 |
public: |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
51 |
typedef typename StreamType::char_type Ch; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
52 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
53 |
//! Constructor. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
54 |
/*! |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
55 |
\param stream stream opened for read. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
56 |
*/ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
57 |
BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
58 |
Read(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
59 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
60 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
61 |
//! Constructor. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
62 |
/*! |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
63 |
\param stream stream opened for read. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
64 |
\param buffer user-supplied buffer. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
65 |
\param bufferSize size of buffer in bytes. Must >=4 bytes. |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
66 |
*/ |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
67 |
BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
68 |
CEREAL_RAPIDJSON_ASSERT(bufferSize >= 4); |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
69 |
Read(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
70 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
71 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
72 |
Ch Peek() const { return *current_; } |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
73 |
Ch Take() { Ch c = *current_; Read(); return c; } |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
74 |
size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
75 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
76 |
// Not implemented |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
77 |
void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); } |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
78 |
void Flush() { CEREAL_RAPIDJSON_ASSERT(false); } |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
79 |
Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
80 |
size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
81 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
82 |
// For encoding detection only. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
83 |
const Ch* Peek4() const { |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
84 |
return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
85 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
86 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
87 |
private: |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
88 |
BasicIStreamWrapper(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
89 |
BasicIStreamWrapper(const BasicIStreamWrapper&); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
90 |
BasicIStreamWrapper& operator=(const BasicIStreamWrapper&); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
91 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
92 |
void Read() { |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
93 |
if (current_ < bufferLast_) |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
94 |
++current_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
95 |
else if (!eof_) { |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
96 |
count_ += readCount_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
97 |
readCount_ = bufferSize_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
98 |
bufferLast_ = buffer_ + readCount_ - 1; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
99 |
current_ = buffer_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
100 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
101 |
if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) { |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
102 |
readCount_ = static_cast<size_t>(stream_.gcount()); |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
103 |
*(bufferLast_ = buffer_ + readCount_) = '\0'; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
104 |
eof_ = true; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
105 |
} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
106 |
} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
107 |
} |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
108 |
|
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
109 |
StreamType &stream_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
110 |
Ch peekBuffer_[4], *buffer_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
111 |
size_t bufferSize_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
112 |
Ch *bufferLast_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
113 |
Ch *current_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
114 |
size_t readCount_; |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
115 |
size_t count_; //!< Number of characters read |
| 8f94a7f5 |
sago007 |
2020-05-10 10:26 |
116 |
bool eof_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
117 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
118 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
119 |
typedef BasicIStreamWrapper<std::istream> IStreamWrapper; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
120 |
typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
121 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
122 |
#if defined(__clang__) || defined(_MSC_VER) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
123 |
CEREAL_RAPIDJSON_DIAG_POP |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
124 |
#endif |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
125 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
126 |
CEREAL_RAPIDJSON_NAMESPACE_END |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
127 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
128 |
#endif // CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_ |
|
|
1970-01-01 00:00 |
129 |
|