istreamwrapper.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
  44. Ch Peek() const {
  45. typename StreamType::int_type c = stream_.peek();
  46. return RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : static_cast<Ch>('\0');
  47. }
  48. Ch Take() {
  49. typename StreamType::int_type c = stream_.get();
  50. if (RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
  51. count_++;
  52. return static_cast<Ch>(c);
  53. }
  54. else
  55. return '\0';
  56. }
  57. // tellg() may return -1 when failed. So we count by ourself.
  58. size_t Tell() const { return count_; }
  59. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  60. void Put(Ch) { RAPIDJSON_ASSERT(false); }
  61. void Flush() { RAPIDJSON_ASSERT(false); }
  62. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  63. // For encoding detection only.
  64. const Ch* Peek4() const {
  65. RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
  66. int i;
  67. bool hasError = false;
  68. for (i = 0; i < 4; ++i) {
  69. typename StreamType::int_type c = stream_.get();
  70. if (c == StreamType::traits_type::eof()) {
  71. hasError = true;
  72. stream_.clear();
  73. break;
  74. }
  75. peekBuffer_[i] = static_cast<Ch>(c);
  76. }
  77. for (--i; i >= 0; --i)
  78. stream_.putback(peekBuffer_[i]);
  79. return !hasError ? peekBuffer_ : 0;
  80. }
  81. private:
  82. BasicIStreamWrapper(const BasicIStreamWrapper&);
  83. BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
  84. StreamType& stream_;
  85. size_t count_; //!< Number of characters read. Note:
  86. mutable Ch peekBuffer_[4];
  87. };
  88. typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
  89. typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
  90. #if defined(__clang__) || defined(_MSC_VER)
  91. RAPIDJSON_DIAG_POP
  92. #endif
  93. RAPIDJSON_NAMESPACE_END
  94. #endif // RAPIDJSON_ISTREAMWRAPPER_H_