DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers istreamwrapper.h Source File

istreamwrapper.h

00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_ISTREAMWRAPPER_H_
00016 #define RAPIDJSON_ISTREAMWRAPPER_H_
00017 
00018 #include "stream.h"
00019 #include <iosfwd>
00020 
00021 #ifdef __clang__
00022 RAPIDJSON_DIAG_PUSH
00023 RAPIDJSON_DIAG_OFF(padded)
00024 #endif
00025 
00026 #ifdef _MSC_VER
00027 RAPIDJSON_DIAG_PUSH
00028 RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
00029 #endif
00030 
00031 RAPIDJSON_NAMESPACE_BEGIN
00032 
00033 //! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
00034 /*!
00035     The classes can be wrapped including but not limited to:
00036 
00037     - \c std::istringstream
00038     - \c std::stringstream
00039     - \c std::wistringstream
00040     - \c std::wstringstream
00041     - \c std::ifstream
00042     - \c std::fstream
00043     - \c std::wifstream
00044     - \c std::wfstream
00045 
00046     \tparam StreamType Class derived from \c std::basic_istream.
00047 */
00048    
00049 template <typename StreamType>
00050 class BasicIStreamWrapper {
00051 public:
00052     typedef typename StreamType::char_type Ch;
00053     BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
00054 
00055     Ch Peek() const { 
00056         typename StreamType::int_type c = stream_.peek();
00057         return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : '\0';
00058     }
00059 
00060     Ch Take() { 
00061         typename StreamType::int_type c = stream_.get();
00062         if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
00063             count_++;
00064             return static_cast<Ch>(c);
00065         }
00066         else
00067             return '\0';
00068     }
00069 
00070     // tellg() may return -1 when failed. So we count by ourself.
00071     size_t Tell() const { return count_; }
00072 
00073     Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
00074     void Put(Ch) { RAPIDJSON_ASSERT(false); }
00075     void Flush() { RAPIDJSON_ASSERT(false); }
00076     size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
00077 
00078     // For encoding detection only.
00079     const Ch* Peek4() const {
00080         RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
00081         int i;
00082         bool hasError = false;
00083         for (i = 0; i < 4; ++i) {
00084             typename StreamType::int_type c = stream_.get();
00085             if (c == StreamType::traits_type::eof()) {
00086                 hasError = true;
00087                 stream_.clear();
00088                 break;
00089             }
00090             peekBuffer_[i] = static_cast<Ch>(c);
00091         }
00092         for (--i; i >= 0; --i)
00093             stream_.putback(peekBuffer_[i]);
00094         return !hasError ? peekBuffer_ : 0;
00095     }
00096 
00097 private:
00098     BasicIStreamWrapper(const BasicIStreamWrapper&);
00099     BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
00100 
00101     StreamType& stream_;
00102     size_t count_;  //!< Number of characters read. Note:
00103     mutable Ch peekBuffer_[4];
00104 };
00105 
00106 typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
00107 typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
00108 
00109 #if defined(__clang__) || defined(_MSC_VER)
00110 RAPIDJSON_DIAG_POP
00111 #endif
00112 
00113 RAPIDJSON_NAMESPACE_END
00114 
00115 #endif // RAPIDJSON_ISTREAMWRAPPER_H_