All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
memorystream.h
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_MEMORYSTREAM_H_
22 #define RAPIDJSON_MEMORYSTREAM_H_
23 
24 #include "rapidjson.h"
25 
26 namespace rapidjson {
27 
28 //! Represents an in-memory input byte stream.
29 /*!
30  This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream.
31 
32  It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file.
33 
34  Differences between MemoryStream and StringStream:
35  1. StringStream has encoding but MemoryStream is a byte stream.
36  2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source.
37  3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4().
38  \note implements Stream concept
39 */
40 struct MemoryStream {
41  typedef char Ch; // byte
42 
43  MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
44 
45  Ch Peek() const { return (src_ == end_) ? '\0' : *src_; }
46  Ch Take() { return (src_ == end_) ? '\0' : *src_++; }
47  size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
48 
49  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
50  void Put(Ch) { RAPIDJSON_ASSERT(false); }
51  void Flush() { RAPIDJSON_ASSERT(false); }
52  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
53 
54  // For encoding detection only.
55  const Ch* Peek4() const {
56  return Tell() + 4 <= size_ ? src_ : 0;
57  }
58 
59  const Ch* src_; //!< Current read position.
60  const Ch* begin_; //!< Original head of the string.
61  const Ch* end_; //!< End of stream.
62  size_t size_; //!< Size of the stream.
63 };
64 
65 } // namespace rapidjson
66 
67 #endif // RAPIDJSON_MEMORYBUFFER_H_
Represents an in-memory input byte stream.
Definition: memorystream.h:40
size_t size_
Size of the stream.
Definition: memorystream.h:62
const Ch * end_
End of stream.
Definition: memorystream.h:61
const Ch * begin_
Original head of the string.
Definition: memorystream.h:60
const Ch * src_
Current read position.
Definition: memorystream.h:59
common definitions and configuration
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:269