All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
filereadstream.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_FILEREADSTREAM_H_
22 #define RAPIDJSON_FILEREADSTREAM_H_
23 
24 #include "rapidjson.h"
25 #include <cstdio>
26 
27 namespace rapidjson {
28 
29 //! File byte stream for input using fread().
30 /*!
31  \note implements Stream concept
32 */
34 public:
35  typedef char Ch; //!< Character type (byte).
36 
37  //! Constructor.
38  /*!
39  \param fp File pointer opened for read.
40  \param buffer user-supplied buffer.
41  \param bufferSize size of buffer in bytes. Must >=4 bytes.
42  */
43  FileReadStream(FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
44  RAPIDJSON_ASSERT(fp_ != 0);
45  RAPIDJSON_ASSERT(bufferSize >= 4);
46  Read();
47  }
48 
49  Ch Peek() const { return *current_; }
50  Ch Take() { Ch c = *current_; Read(); return c; }
51  size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
52 
53  // Not implemented
54  void Put(Ch) { RAPIDJSON_ASSERT(false); }
55  void Flush() { RAPIDJSON_ASSERT(false); }
56  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
57  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
58 
59  // For encoding detection only.
60  const Ch* Peek4() const {
61  return (current_ + 4 <= bufferLast_) ? current_ : 0;
62  }
63 
64 private:
65  void Read() {
66  if (current_ < bufferLast_)
67  ++current_;
68  else if (!eof_) {
69  count_ += readCount_;
70  readCount_ = fread(buffer_, 1, bufferSize_, fp_);
71  bufferLast_ = buffer_ + readCount_ - 1;
72  current_ = buffer_;
73 
74  if (readCount_ < bufferSize_) {
75  buffer_[readCount_] = '\0';
76  ++bufferLast_;
77  eof_ = true;
78  }
79  }
80  }
81 
82  FILE* fp_;
83  Ch *buffer_;
84  size_t bufferSize_;
85  Ch *bufferLast_;
86  Ch *current_;
87  size_t readCount_;
88  size_t count_; //!< Number of characters read
89  bool eof_;
90 };
91 
92 } // namespace rapidjson
93 
94 #endif // RAPIDJSON_FILESTREAM_H_