istreamwrapper.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <ios>
  19. #ifdef __clang__
  20. RAPIDJSON_DIAG_PUSH
  21. RAPIDJSON_DIAG_OFF(padded)
  22. #elif defined(_MSC_VER)
  23. RAPIDJSON_DIAG_PUSH
  24. RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
  25. #endif
  26. RAPIDJSON_NAMESPACE_BEGIN
  27. //! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
  28. /*!
  29. The classes can be wrapped including but not limited to:
  30. - \c std::istringstream
  31. - \c std::stringstream
  32. - \c std::wistringstream
  33. - \c std::wstringstream
  34. - \c std::ifstream
  35. - \c std::fstream
  36. - \c std::wifstream
  37. - \c std::wfstream
  38. \tparam StreamType Class derived from \c std::basic_istream.
  39. */
  40. template <typename StreamType>
  41. class BasicIStreamWrapper {
  42. public:
  43. typedef typename StreamType::char_type Ch;
  44. //! Constructor.
  45. /*!
  46. \param stream stream opened for read.
  47. */
  48. BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
  49. Read();
  50. }
  51. //! Constructor.
  52. /*!
  53. \param stream stream opened for read.
  54. \param buffer user-supplied buffer.
  55. \param bufferSize size of buffer in bytes. Must >=4 bytes.
  56. */
  57. BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
  58. RAPIDJSON_ASSERT(bufferSize >= 4);
  59. Read();
  60. }
  61. Ch Peek() const { return *current_; }
  62. Ch Take() { Ch c = *current_; Read(); return c; }
  63. size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
  64. // Not implemented
  65. void Put(Ch) { RAPIDJSON_ASSERT(false); }
  66. void Flush() { RAPIDJSON_ASSERT(false); }
  67. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  68. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  69. // For encoding detection only.
  70. const Ch* Peek4() const {
  71. return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
  72. }
  73. private:
  74. BasicIStreamWrapper();
  75. BasicIStreamWrapper(const BasicIStreamWrapper&);
  76. BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
  77. void Read() {
  78. if (current_ < bufferLast_)
  79. ++current_;
  80. else if (!eof_) {
  81. count_ += readCount_;
  82. readCount_ = bufferSize_;
  83. bufferLast_ = buffer_ + readCount_ - 1;
  84. current_ = buffer_;
  85. if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) {
  86. readCount_ = static_cast<size_t>(stream_.gcount());
  87. *(bufferLast_ = buffer_ + readCount_) = '\0';
  88. eof_ = true;
  89. }
  90. }
  91. }
  92. StreamType &stream_;
  93. Ch peekBuffer_[4], *buffer_;
  94. size_t bufferSize_;
  95. Ch *bufferLast_;
  96. Ch *current_;
  97. size_t readCount_;
  98. size_t count_; //!< Number of characters read
  99. bool eof_;
  100. };
  101. typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
  102. typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
  103. #if defined(__clang__) || defined(_MSC_VER)
  104. RAPIDJSON_DIAG_POP
  105. #endif
  106. RAPIDJSON_NAMESPACE_END
  107. #endif // RAPIDJSON_ISTREAMWRAPPER_H_