Ariles
istreamwrapper.h
Go to the documentation of this file.
1 /**
2  @file
3  @author See below
4 
5  @copyright See below
6 
7  @brief Copied from a newer version of RapidJSON to addd this functionality to older versions.
8 */
9 
10 #pragma once
11 
12 
13 // Tencent is pleased to support the open source community by making RapidJSON available.
14 //
15 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
16 //
17 // Licensed under the MIT License (the "License"); you may not use this file except
18 // in compliance with the License. You may obtain a copy of the License at
19 //
20 // http://opensource.org/licenses/MIT
21 //
22 // Unless required by applicable law or agreed to in writing, software distributed
23 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
24 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
25 // specific language governing permissions and limitations under the License.
26 
27 
28 #include <iosfwd>
29 
30 
31 namespace ariles
32 {
33  namespace ns_rapidjson
34  {
35  //! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
36  /*!
37  The classes can be wrapped including but not limited to:
38 
39  - \c std::istringstream
40  - \c std::stringstream
41  - \c std::wistringstream
42  - \c std::wstringstream
43  - \c std::ifstream
44  - \c std::fstream
45  - \c std::wifstream
46  - \c std::wfstream
47 
48  \tparam StreamType Class derived from \c std::basic_istream.
49  */
50 
51  template <typename StreamType>
52  class ARILES_LIB_LOCAL BasicIStreamWrapper
53  {
54  public:
55  typedef typename StreamType::char_type Ch;
56  explicit BasicIStreamWrapper(StreamType &stream) : stream_(stream), count_(), peekBuffer_()
57  {
58  }
59 
60  Ch Peek() const
61  {
62  typename StreamType::int_type c = stream_.peek();
63  return (c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : static_cast<Ch>('\0');
64  }
65 
66  Ch Take()
67  {
68  typename StreamType::int_type c = stream_.get();
69  if (c != StreamType::traits_type::eof())
70  {
71  count_++;
72  return static_cast<Ch>(c);
73  }
74  else
75  return '\0';
76  }
77 
78  // tellg() may return -1 when failed. So we count by ourself.
79  size_t Tell() const
80  {
81  return count_;
82  }
83 
85  {
86  RAPIDJSON_ASSERT(false);
87  return 0;
88  }
89  void Put(Ch)
90  {
91  RAPIDJSON_ASSERT(false);
92  }
93  void Flush()
94  {
95  RAPIDJSON_ASSERT(false);
96  }
97  size_t PutEnd(Ch *)
98  {
99  RAPIDJSON_ASSERT(false);
100  return 0;
101  }
102 
103  // For encoding detection only.
104  const Ch *Peek4() const
105  {
106  RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
107  int i;
108  bool hasError = false;
109  for (i = 0; i < 4; ++i)
110  {
111  typename StreamType::int_type c = stream_.get();
112  if (c == StreamType::traits_type::eof())
113  {
114  hasError = true;
115  stream_.clear();
116  break;
117  }
118  peekBuffer_[i] = static_cast<Ch>(c);
119  }
120  for (--i; i >= 0; --i)
121  stream_.putback(peekBuffer_[i]);
122  return !hasError ? peekBuffer_ : 0;
123  }
124 
125  private:
127  BasicIStreamWrapper &operator=(const BasicIStreamWrapper &);
128 
129  StreamType &stream_;
130  size_t count_; //!< Number of characters read. Note:
131  mutable Ch peekBuffer_[4];
132  };
133 
136 
137  } // namespace ns_rapidjson
138 } // namespace ariles
BasicIStreamWrapper< std::wistream > WIStreamWrapper
size_t count_
Number of characters read. Note:
Wrapper of std::basic_istream into RapidJSON's Stream concept.
BasicIStreamWrapper< std::istream > IStreamWrapper
Definition: basic.h:17