TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filereadstream.h
Go to the documentation of this file.
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 
15 #ifndef RAPIDJSON_FILEREADSTREAM_H_
16 #define RAPIDJSON_FILEREADSTREAM_H_
17 
18 #include "rapidjson.h"
19 #include <cstdio>
20 
22 
24 
28 public:
29  typedef char Ch;
30 
32 
37  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) {
38  RAPIDJSON_ASSERT(fp_ != 0);
39  RAPIDJSON_ASSERT(bufferSize >= 4);
40  Read();
41  }
42 
43  Ch Peek() const { return *current_; }
44  Ch Take() { Ch c = *current_; Read(); return c; }
45  size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
46 
47  // Not implemented
48  void Put(Ch) { RAPIDJSON_ASSERT(false); }
49  void Flush() { RAPIDJSON_ASSERT(false); }
50  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
51  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
52 
53  // For encoding detection only.
54  const Ch* Peek4() const {
55  return (current_ + 4 <= bufferLast_) ? current_ : 0;
56  }
57 
58 private:
59  void Read() {
60  if (current_ < bufferLast_)
61  ++current_;
62  else if (!eof_) {
63  count_ += readCount_;
64  readCount_ = fread(buffer_, 1, bufferSize_, fp_);
66  current_ = buffer_;
67 
68  if (readCount_ < bufferSize_) {
69  buffer_[readCount_] = '\0';
70  ++bufferLast_;
71  eof_ = true;
72  }
73  }
74  }
75 
76  std::FILE* fp_;
77  Ch *buffer_;
78  size_t bufferSize_;
80  Ch *current_;
81  size_t readCount_;
82  size_t count_;
83  bool eof_;
84 };
85 
87 
88 #endif // RAPIDJSON_FILESTREAM_H_
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
const size_t bufferSize
Definition: RASession.h:31
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
void Flush()
Definition: filereadstream.h:49
size_t count_
Number of characters read.
Definition: filereadstream.h:82
#define false
Definition: CascPort.h:18
Ch Take()
Definition: filereadstream.h:44
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
Ch * buffer_
Definition: filereadstream.h:77
FileReadStream(std::FILE *fp, char *buffer, size_t bufferSize)
Constructor.
Definition: filereadstream.h:37
Ch Peek() const
Definition: filereadstream.h:43
bool eof_
Definition: filereadstream.h:83
Ch * bufferLast_
Definition: filereadstream.h:79
char Ch
Character type (byte).
Definition: filereadstream.h:29
size_t bufferSize_
Definition: filereadstream.h:78
std::FILE * fp_
Definition: filereadstream.h:76
void Put(Ch)
Definition: filereadstream.h:48
size_t PutEnd(Ch *)
Definition: filereadstream.h:51
File byte stream for input using fread().
Definition: filereadstream.h:27
common definitions and configuration
size_t Tell() const
Definition: filereadstream.h:45
void Read()
Definition: filereadstream.h:59
size_t readCount_
Definition: filereadstream.h:81
Ch * PutBegin()
Definition: filereadstream.h:50
Ch * current_
Definition: filereadstream.h:80
const Ch * Peek4() const
Definition: filereadstream.h:54