15 #ifndef RAPIDJSON_ENCODEDSTREAM_H_ 16 #define RAPIDJSON_ENCODEDSTREAM_H_ 19 #include "memorystream.h" 23 RAPIDJSON_DIAG_OFF(effc++)
28 RAPIDJSON_DIAG_OFF(padded)
38 template <
typename Encoding,
typename InputByteStream>
40 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename InputByteStream::Ch) == 1);
42 typedef typename Encoding::Ch Ch;
45 current_ = Encoding::TakeBOM(is_);
48 Ch Peek()
const {
return current_; }
49 Ch Take() { Ch c = current_; current_ = Encoding::Take(is_);
return c; }
50 size_t Tell()
const {
return is_.Tell(); }
70 typedef UTF8<>::Ch Ch;
73 if (static_cast<unsigned char>(is_.Peek()) == 0xEFu) is_.Take();
74 if (static_cast<unsigned char>(is_.Peek()) == 0xBBu) is_.Take();
75 if (static_cast<unsigned char>(is_.Peek()) == 0xBFu) is_.Take();
77 Ch Peek()
const {
return is_.Peek(); }
78 Ch Take() {
return is_.Take(); }
79 size_t Tell()
const {
return is_.Tell(); }
84 Ch* PutBegin() {
return 0; }
85 size_t PutEnd(Ch*) {
return 0; }
99 template <
typename Encoding,
typename OutputByteStream>
101 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename OutputByteStream::Ch) == 1);
103 typedef typename Encoding::Ch Ch;
107 Encoding::PutBOM(os_);
110 void Put(Ch c) { Encoding::Put(os_, c); }
111 void Flush() { os_.Flush(); }
124 OutputByteStream& os_;
127 #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x 134 template <
typename CharType,
typename InputByteStream>
136 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename InputByteStream::Ch) == 1);
145 AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) {
148 static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
149 takeFunc_ = f[type_];
150 current_ = takeFunc_(*is_);
153 UTFType GetType()
const {
return type_; }
154 bool HasBOM()
const {
return hasBOM_; }
156 Ch Peek()
const {
return current_; }
157 Ch Take() { Ch c = current_; current_ = takeFunc_(*is_);
return c; }
158 size_t Tell()
const {
return is_->Tell(); }
179 const unsigned char* c =
reinterpret_cast<const unsigned char *
>(is_->Peek4());
183 unsigned bom =
static_cast<unsigned>(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
185 if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
186 else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
187 else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ =
true; is_->Take(); is_->Take(); }
188 else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ =
true; is_->Take(); is_->Take(); }
189 else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); }
203 int pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
205 case 0x08: type_ = kUTF32BE;
break;
206 case 0x0A: type_ = kUTF16BE;
break;
207 case 0x01: type_ = kUTF32LE;
break;
208 case 0x05: type_ = kUTF16LE;
break;
209 case 0x0F: type_ = kUTF8;
break;
215 if (type_ == kUTF16LE || type_ == kUTF16BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 2);
216 if (type_ == kUTF32LE || type_ == kUTF32BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 4);
219 typedef Ch (*TakeFunc)(InputByteStream& is);
220 InputByteStream* is_;
232 template <
typename CharType,
typename OutputByteStream>
234 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename OutputByteStream::Ch) == 1);
248 if (type_ == kUTF16LE || type_ == kUTF16BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 2);
249 if (type_ == kUTF32LE || type_ == kUTF32BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 4);
251 static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
258 UTFType GetType()
const {
return type_; }
260 void Put(Ch c) { putFunc_(*os_, c); }
261 void Flush() { os_->Flush(); }
275 typedef void (*PutBOMFunc)(OutputByteStream&);
276 static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
280 typedef void (*PutFunc)(OutputByteStream&, Ch);
282 OutputByteStream* os_;
287 #undef RAPIDJSON_ENCODINGS_FUNC 299 #endif // RAPIDJSON_FILESTREAM_H_
Output byte stream wrapper with statically bound encoding.
Definition: encodedstream.h:100
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
Represents an in-memory input byte stream.
Definition: memorystream.h:40
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
Output stream wrapper with dynamically bound encoding and automatic encoding detection.
Definition: encodedstream.h:233
AutoUTFOutputStream(OutputByteStream &os, UTFType type, bool putBOM)
Constructor.
Definition: encodedstream.h:244
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:437
UTF-8 encoding.
Definition: encodings.h:96