istreamwrapper.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #ifndef RAPIDJSON_ISTREAMWRAPPER_H_
  15. #define RAPIDJSON_ISTREAMWRAPPER_H_
  16. #include "stream.h"
  17. #include <iosfwd>
  18. #ifdef __clang__
  19. RAPIDJSON_DIAG_PUSH
  20. RAPIDJSON_DIAG_OFF(padded)
  21. #elif defined(_MSC_VER)
  22. RAPIDJSON_DIAG_PUSH
  23. RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
  24. #endif
  25. RAPIDJSON_NAMESPACE_BEGIN
  26. //! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
  27. /*!
  28. The classes can be wrapped including but not limited to:
  29. - \c std::istringstream
  30. - \c std::stringstream
  31. - \c std::wistringstream
  32. - \c std::wstringstream
  33. - \c std::ifstream
  34. - \c std::fstream
  35. - \c std::wifstream
  36. - \c std::wfstream
  37. \tparam StreamType Class derived from \c std::basic_istream.
  38. */
  39. template <typename StreamType>
  40. class BasicIStreamWrapper {
  41. public:
  42. typedef typename StreamType::char_type Ch;
  43. //! Constructor.
  44. /*!
  45. \param stream stream opened for read.
  46. */
  47. BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
  48. Read();
  49. }
  50. //! Constructor.
  51. /*!
  52. \param stream stream opened for read.
  53. \param buffer user-supplied buffer.
  54. \param bufferSize size of buffer in bytes. Must >=4 bytes.
  55. */
  56. BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
  57. RAPIDJSON_ASSERT(bufferSize >= 4);
  58. Read();
  59. }
  60. Ch Peek() const { return *current_; }
  61. Ch Take() { Ch c = *current_; Read(); return c; }
  62. size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
  63. // Not implemented
  64. void Put(Ch) { RAPIDJSON_ASSERT(false); }
  65. void Flush() { RAPIDJSON_ASSERT(false); }
  66. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  67. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  68. // For encoding detection only.
  69. const Ch* Peek4() const {
  70. return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
  71. }
  72. private:
  73. BasicIStreamWrapper();
  74. BasicIStreamWrapper(const BasicIStreamWrapper&);
  75. BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
  76. void Read() {
  77. if (current_ < bufferLast_)
  78. ++current_;
  79. else if (!eof_) {
  80. count_ += readCount_;
  81. readCount_ = bufferSize_;
  82. bufferLast_ = buffer_ + readCount_ - 1;
  83. current_ = buffer_;
  84. if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) {
  85. readCount_ = static_cast<size_t>(stream_.gcount());
  86. *(bufferLast_ = buffer_ + readCount_) = '\0';
  87. eof_ = true;
  88. }
  89. }
  90. }
  91. StreamType &stream_;
  92. Ch peekBuffer_[4], *buffer_;
  93. size_t bufferSize_;
  94. Ch *bufferLast_;
  95. Ch *current_;
  96. size_t readCount_;
  97. size_t count_; //!< Number of characters read
  98. bool eof_;
  99. };
  100. typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
  101. typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
  102. #if defined(__clang__) || defined(_MSC_VER)
  103. RAPIDJSON_DIAG_POP
  104. #endif
  105. RAPIDJSON_NAMESPACE_END
  106. #endif // RAPIDJSON_ISTREAMWRAPPER_H_