1 #ifndef RAPIDJSON_ENCODEDSTREAM_H_
2 #define RAPIDJSON_ENCODEDSTREAM_H_
8 RAPIDJSON_DIAG_OFF(effc++)
18 template <
typename Encoding,
typename InputByteStream>
20 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename InputByteStream::Ch) == 1);
22 typedef typename Encoding::Ch Ch;
25 current_ = Encoding::TakeBOM(is_);
28 Ch Peek()
const {
return current_; }
29 Ch Take() { Ch c = current_; current_ = Encoding::Take(is_);
return c; }
30 size_t Tell()
const {
return is_.Tell(); }
51 template <
typename Encoding,
typename OutputByteStream>
53 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename OutputByteStream::Ch) == 1);
55 typedef typename Encoding::Ch Ch;
59 Encoding::PutBOM(os_);
62 void Put(Ch c) { Encoding::Put(os_, c); }
63 void Flush() { os_.Flush(); }
76 OutputByteStream& os_;
79 #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
86 template <
typename CharType,
typename InputByteStream>
88 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename InputByteStream::Ch) == 1);
99 static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
100 takeFunc_ = f[type_];
101 current_ = takeFunc_(*is_);
104 UTFType GetType()
const {
return type_; }
105 bool HasBOM()
const {
return hasBOM_; }
107 Ch Peek()
const {
return current_; }
108 Ch Take() { Ch c = current_; current_ = takeFunc_(*is_);
return c; }
109 size_t Tell()
const {
return is_->Tell(); }
130 const unsigned char* c = (
const unsigned char *)is_->Peek4();
134 unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
136 if (bom == 0xFFFE0000) { type_ =
kUTF32BE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
137 else if (bom == 0x0000FEFF) { type_ =
kUTF32LE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
138 else if ((bom & 0xFFFF) == 0xFFFE) { type_ =
kUTF16BE; hasBOM_ =
true; is_->Take(); is_->Take(); }
139 else if ((bom & 0xFFFF) == 0xFEFF) { type_ =
kUTF16LE; hasBOM_ =
true; is_->Take(); is_->Take(); }
140 else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ =
kUTF8; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); }
154 unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
160 case 0x0F: type_ =
kUTF8;
break;
183 typedef Ch (*TakeFunc)(InputByteStream& is);
184 InputByteStream* is_;
196 template <
typename CharType,
typename OutputByteStream>
198 RAPIDJSON_STATIC_ASSERT(
sizeof(
typename OutputByteStream::Ch) == 1);
226 static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
233 UTFType GetType()
const {
return type_; }
235 void Put(Ch c) { putFunc_(*os_, c); }
236 void Flush() { os_->Flush(); }
250 typedef void (*PutBOMFunc)(OutputByteStream&);
251 static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
255 typedef void (*PutFunc)(OutputByteStream&, Ch);
257 OutputByteStream* os_;
262 #undef RAPIDJSON_ENCODINGS_FUNC
270 #endif // RAPIDJSON_FILESTREAM_H_
UTF-16 little endian.
Definition: encodings.h:525
AutoUTFOutputStream(OutputByteStream &os, UTFType type, bool putBOM)
Constructor.
Definition: encodedstream.h:208
UTF-32 little endian.
Definition: encodings.h:527
Output byte stream wrapper with statically bound encoding.
Definition: encodedstream.h:52
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:146
UTF-8.
Definition: encodings.h:524
UTF-16 big endian.
Definition: encodings.h:526
common definitions and configuration
Output stream wrapper with dynamically bound encoding and automatic encoding detection.
Definition: encodedstream.h:197
UTF-32 big endian.
Definition: encodings.h:528
UTFType
Runtime-specified UTF encoding type of a stream.
Definition: encodings.h:523