Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/miloyip/rapidjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormiloyip@gmail.com <miloyip@gmail.com@c5894555-1306-4e8d-425f-1f6f381ee07c>2011-12-03 13:57:17 +0400
committermiloyip@gmail.com <miloyip@gmail.com@c5894555-1306-4e8d-425f-1f6f381ee07c>2011-12-03 13:57:17 +0400
commit7c914a9d4cb39de8e54fbe6c07a7d91072d9ef15 (patch)
tree7239e84879be57c67ab7f2a3c168fb214f19daea /include/rapidjson/encodedstream.h
parentf516a01769df9e5b7ec686b10e361220929c5630 (diff)
Added RAPIDJSON_STATIC_ASSERT() and applied it to check size of character types in encodings.
Modified API documentation to previous changes. Rename Stream to InputStream/OutputStream/InputByteStream/OutputByteStream, and stream to is/os according to the context. git-svn-id: https://rapidjson.googlecode.com/svn/trunk@49 c5894555-1306-4e8d-425f-1f6f381ee07c
Diffstat (limited to 'include/rapidjson/encodedstream.h')
-rw-r--r--include/rapidjson/encodedstream.h114
1 files changed, 74 insertions, 40 deletions
diff --git a/include/rapidjson/encodedstream.h b/include/rapidjson/encodedstream.h
index 37daaf65..efa0acfb 100644
--- a/include/rapidjson/encodedstream.h
+++ b/include/rapidjson/encodedstream.h
@@ -5,13 +5,18 @@
namespace rapidjson {
-//! Adapts an input byte stream with an specified encoding.
-template <typename Encoding, typename InputStream>
+//! Input byte stream wrapper with a statically bound encoding.
+/*!
+ \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
+ \tparam InputByteStream Type of input byte stream. For example, FileReadStream.
+*/
+template <typename Encoding, typename InputByteStream>
class EncodedInputStream {
+ RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
public:
typedef typename Encoding::Ch Ch;
- EncodedInputStream(InputStream& is) : is_(is) {
+ EncodedInputStream(InputByteStream& is) : is_(is) {
current_ = Encoding::TakeBOM(is_);
}
@@ -26,17 +31,22 @@ public:
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private:
- InputStream& is_;
+ InputByteStream& is_;
Ch current_;
};
-//! Adapts an output byte stream with an specified encoding.
-template <typename Encoding, typename OutputStream>
+//! Output byte stream wrapper with statically bound encoding.
+/*!
+ \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE.
+ \tparam InputByteStream Type of input byte stream. For example, FileWriteStream.
+*/
+template <typename Encoding, typename OutputByteStream>
class EncodedOutputStream {
+ RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
public:
typedef typename Encoding::Ch Ch;
- EncodedOutputStream(OutputStream& os, bool putBOM = true) : os_(os) {
+ EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) {
if (putBOM)
Encoding::PutBOM(os_);
}
@@ -52,24 +62,36 @@ public:
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
private:
- OutputStream& os_;
+ OutputByteStream& os_;
};
-#define ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
+#define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x
-template <typename CharType, typename InputStream>
+//! Input stream wrapper with dynamically bound encoding and automatic encoding detection.
+/*!
+ \tparam CharType Type of character for reading.
+ \tparam InputByteStream type of input byte stream to be wrapped.
+*/
+template <typename CharType, typename InputByteStream>
class AutoUTFInputStream {
+ RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1);
public:
typedef CharType Ch;
- AutoUTFInputStream(InputStream& is, UTFType type = kUTF8) : is_(is), type_(type) {
+ //! Constructor.
+ /*!
+ \param is input stream to be wrapped.
+ \param type UTF encoding type if it is not detected from the stream.
+ */
+ AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(is), type_(type), hasBOM_(false) {
DetectType(is);
- static const TakeFunc f[] = { ENCODINGS_FUNC(Take) };
+ static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) };
takeFunc_ = f[type_];
current_ = takeFunc_(is_);
}
UTFType GetType() const { return type_; }
+ bool HasBOM() const { return hasBOM_; }
Ch Peek() const { return current_; }
Ch Take() { Ch c = current_; current_ = takeFunc_(is_); return c; }
@@ -83,7 +105,7 @@ public:
private:
// Detect encoding type with BOM or RFC 4627
- void DetectType(InputStream& is) {
+ void DetectType(InputByteStream& is) {
// BOM (Byte Order Mark):
// 00 00 FE FF UTF-32BE
// FF FE 00 00 UTF-32LE
@@ -96,11 +118,12 @@ private:
return;
unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
- if (bom == 0xFFFE0000) { type_ = kUTF32BE; is.Take(); is.Take(); is.Take(); is.Take(); goto sizecheck; }
- else if (bom == 0x0000FEFF) { type_ = kUTF32LE; is.Take(); is.Take(); is.Take(); is.Take(); goto sizecheck; }
- else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; is.Take(); is.Take(); goto sizecheck; }
- else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; is.Take(); is.Take(); goto sizecheck; }
- else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; is.Take(); is.Take(); is.Take(); goto sizecheck; }
+ hasBOM_ = false;
+ if (bom == 0xFFFE0000) { type_ = kUTF32BE; is.Take(); is.Take(); is.Take(); is.Take(); hasBOM_ = true; }
+ else if (bom == 0x0000FEFF) { type_ = kUTF32LE; is.Take(); is.Take(); is.Take(); is.Take(); hasBOM_ = true; }
+ else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; is.Take(); is.Take(); hasBOM_ = true; }
+ else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; is.Take(); is.Take(); hasBOM_ = true; }
+ else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; is.Take(); is.Take(); is.Take(); hasBOM_ = true; }
// RFC 4627: Section 3
// "Since the first two characters of a JSON text will always be ASCII
@@ -113,16 +136,17 @@ private:
// xx 00 xx 00 UTF-16LE
// xx xx xx xx UTF-8
- unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
- switch (pattern) {
- case 0x08: type_ = kUTF32BE; break;
- case 0x0A: type_ = kUTF16BE; break;
- case 0x01: type_ = kUTF32LE; break;
- case 0x05: type_ = kUTF16LE; break;
- case 0x0F: type_ = kUTF8; break;
+ if (!hasBOM_) {
+ unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0);
+ switch (pattern) {
+ case 0x08: type_ = kUTF32BE; break;
+ case 0x0A: type_ = kUTF16BE; break;
+ case 0x01: type_ = kUTF32LE; break;
+ case 0x05: type_ = kUTF16LE; break;
+ case 0x0F: type_ = kUTF8; break;
+ }
}
- sizecheck:
// RUntime check whether the size of character type is sufficient. It only perform checks with assertion.
switch (type_) {
case kUTF16LE:
@@ -136,19 +160,32 @@ private:
}
}
- typedef Ch (*TakeFunc)(InputStream& is);
- InputStream& is_;
+ typedef Ch (*TakeFunc)(InputByteStream& is);
+ InputByteStream& is_;
UTFType type_;
Ch current_;
TakeFunc takeFunc_;
+ bool hasBOM_;
};
-template <typename CharType, typename OutputStream>
+//! Output stream wrapper with dynamically bound encoding and automatic encoding detection.
+/*!
+ \tparam CharType Type of character for writing.
+ \tparam InputByteStream type of output byte stream to be wrapped.
+*/
+template <typename CharType, typename OutputByteStream>
class AutoUTFOutputStream {
+ RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1);
public:
typedef CharType Ch;
- AutoUTFOutputStream(OutputStream& os, UTFType type, bool putBOM) : os_(os), type_(type) {
+ //! Constructor.
+ /*!
+ \param os output stream to be wrapped.
+ \param type UTF encoding type.
+ \param putBOM Whether to write BOM at the beginning of the stream.
+ */
+ AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(os), type_(type) {
// RUntime check whether the size of character type is sufficient. It only perform checks with assertion.
switch (type_) {
case kUTF16LE:
@@ -161,7 +198,7 @@ public:
break;
}
- static const PutFunc f[] = { ENCODINGS_FUNC(Put) };
+ static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) };
putFunc_ = f[type_];
if (putBOM)
@@ -170,10 +207,7 @@ public:
UTFType GetType() const { return type_; }
- void Put(Ch c) {
- putFunc_(os_, c);
- }
-
+ void Put(Ch c) { putFunc_(os_, c); }
void Flush() { os_.Flush(); }
// Not implemented
@@ -185,19 +219,19 @@ public:
private:
void PutBOM() {
- typedef void (*PutBOMFunc)(OutputStream&);
- static const PutBOMFunc f[] = { ENCODINGS_FUNC(PutBOM) };
+ typedef void (*PutBOMFunc)(OutputByteStream&);
+ static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) };
f[type_](os_);
}
- typedef void (*PutFunc)(OutputStream&, Ch);
+ typedef void (*PutFunc)(OutputByteStream&, Ch);
- OutputStream& os_;
+ OutputByteStream& os_;
UTFType type_;
PutFunc putFunc_;
};
-#undef ENCODINGS_FUNC
+#undef RAPIDJSON_ENCODINGS_FUNC
} // namespace rapidjson