istreamwrapper.h 3.5 KB

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