15 #ifndef RAPIDJSON_ENCODEDSTREAM_H_
16 #define RAPIDJSON_ENCODEDSTREAM_H_
22 RAPIDJSON_DIAG_OFF(effc++)
25 RAPIDJSON_NAMESPACE_BEGIN
32 template <
typename Encoding,
typename InputByteStream>
36 typedef typename Encoding::Ch Ch;
39 current_ = Encoding::TakeBOM(is_);
42 Ch Peek()
const {
return current_; }
43 Ch Take() { Ch c = current_; current_ = Encoding::Take(is_);
return c; }
44 size_t Tell()
const {
return is_.Tell(); }
65 template <
typename Encoding,
typename OutputByteStream>
69 typedef typename Encoding::Ch Ch;
73 Encoding::PutBOM(os_);
76 void Put(Ch c) { Encoding::Put(os_, c); }
77 void Flush() { os_.Flush(); }
90 OutputByteStream& os_;
93 #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
100 template <
typename CharType,
typename InputByteStream>
114 static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
115 takeFunc_ = f[type_];
116 current_ = takeFunc_(*is_);
119 UTFType GetType()
const {
return type_; }
120 bool HasBOM()
const {
return hasBOM_; }
122 Ch Peek()
const {
return current_; }
123 Ch Take() { Ch c = current_; current_ = takeFunc_(*is_);
return c; }
124 size_t Tell()
const {
return is_->Tell(); }
133 AutoUTFInputStream(
const AutoUTFInputStream&);
134 AutoUTFInputStream& operator=(
const AutoUTFInputStream&);
145 const unsigned char* c = (
const unsigned char *)is_->Peek4();
149 unsigned bom =
static_cast<unsigned>(c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24));
151 if (bom == 0xFFFE0000) { type_ =
kUTF32BE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
152 else if (bom == 0x0000FEFF) { type_ =
kUTF32LE; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); }
153 else if ((bom & 0xFFFF) == 0xFFFE) { type_ =
kUTF16BE; hasBOM_ =
true; is_->Take(); is_->Take(); }
154 else if ((bom & 0xFFFF) == 0xFEFF) { type_ =
kUTF16LE; hasBOM_ =
true; is_->Take(); is_->Take(); }
155 else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ =
kUTF8; hasBOM_ =
true; is_->Take(); is_->Take(); is_->Take(); }
169 unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
175 case 0x0F: type_ =
kUTF8;
break;
181 if (type_ == kUTF16LE || type_ == kUTF16BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 2);
182 if (type_ == kUTF32LE || type_ == kUTF32BE)
RAPIDJSON_ASSERT(
sizeof(Ch) >= 4);
185 typedef Ch (*TakeFunc)(InputByteStream& is);
186 InputByteStream* is_;
198 template <
typename CharType,
typename OutputByteStream>
217 static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
224 UTFType GetType()
const {
return type_; }
226 void Put(Ch c) { putFunc_(*os_, c); }
227 void Flush() { os_->Flush(); }
237 AutoUTFOutputStream(
const AutoUTFOutputStream&);
238 AutoUTFOutputStream& operator=(
const AutoUTFOutputStream&);
241 typedef void (*PutBOMFunc)(OutputByteStream&);
242 static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
246 typedef void (*PutFunc)(OutputByteStream&, Ch);
248 OutputByteStream* os_;
253 #undef RAPIDJSON_ENCODINGS_FUNC
255 RAPIDJSON_NAMESPACE_END
261 #endif // RAPIDJSON_FILESTREAM_H_
UTF-16 little endian.
Definition: encodings.h:540
AutoUTFOutputStream(OutputByteStream &os, UTFType type, bool putBOM)
Constructor.
Definition: encodedstream.h:210
UTF-32 little endian.
Definition: encodings.h:542
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:375
Output byte stream wrapper with statically bound encoding.
Definition: encodedstream.h:66
UTF-8.
Definition: encodings.h:539
UTF-16 big endian.
Definition: encodings.h:541
common definitions and configuration
Output stream wrapper with dynamically bound encoding and automatic encoding detection.
Definition: encodedstream.h:199
UTF-32 big endian.
Definition: encodings.h:543
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
UTFType
Runtime-specified UTF encoding type of a stream.
Definition: encodings.h:538