webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
filereadstream.h
Go to the documentation of this file.
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 
28 
30 
34 public:
35  typedef char Ch;
36 
38 
43  FileReadStream(std::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  std::FILE* fp_;
83  Ch *buffer_;
84  size_t bufferSize_;
85  Ch *bufferLast_;
86  Ch *current_;
87  size_t readCount_;
88  size_t count_;
89  bool eof_;
90 };
91 
93 
94 #endif // RAPIDJSON_FILESTREAM_H_
Ch Take()
Definition: filereadstream.h:50
size_t Tell() const
Definition: filereadstream.h:51
int c
Definition: cpp_unittests.cpp:275
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
int FILE
Definition: antglob.py:49
char Ch
Character type (byte).
Definition: filereadstream.h:35
File byte stream for input using fread().
Definition: filereadstream.h:33
const Ch * Peek4() const
Definition: filereadstream.h:60
void Put(Ch)
Definition: filereadstream.h:54
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
size_t PutEnd(Ch *)
Definition: filereadstream.h:57
void Flush()
Definition: filereadstream.h:55
common definitions and configuration
#define false
Definition: float-mm.c:5
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:192
FileReadStream(std::FILE *fp, char *buffer, size_t bufferSize)
Constructor.
Definition: filereadstream.h:43
Ch * PutBegin()
Definition: filereadstream.h:56
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:315
Ch Peek() const
Definition: filereadstream.h:49