TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filewritestream.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_FILEWRITESTREAM_H_
16 #define RAPIDJSON_FILEWRITESTREAM_H_
17 
18 #include "rapidjson.h"
19 #include <cstdio>
20 
22 
24 
28 public:
29  typedef char Ch;
30 
31  FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
32  RAPIDJSON_ASSERT(fp_ != 0);
33  }
34 
35  void Put(char c) {
36  if (current_ >= bufferEnd_)
37  Flush();
38 
39  *current_++ = c;
40  }
41 
42  void PutN(char c, size_t n) {
43  size_t avail = static_cast<size_t>(bufferEnd_ - current_);
44  while (n > avail) {
45  std::memset(current_, c, avail);
46  current_ += avail;
47  Flush();
48  n -= avail;
49  avail = static_cast<size_t>(bufferEnd_ - current_);
50  }
51 
52  if (n > 0) {
53  std::memset(current_, c, n);
54  current_ += n;
55  }
56  }
57 
58  void Flush() {
59  if (current_ != buffer_) {
60  fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
61  current_ = buffer_;
62  }
63  }
64 
65  // Not implemented
66  char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
67  char Take() { RAPIDJSON_ASSERT(false); return 0; }
68  size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
69  char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
70  size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
71 
72 private:
73  // Prohibit copy constructor & assignment operator.
76 
77  std::FILE* fp_;
78  char *buffer_;
79  char *bufferEnd_;
80  char *current_;
81 };
82 
84 template<>
85 inline void PutN(FileWriteStream& stream, char c, size_t n) {
86  stream.PutN(c, n);
87 }
88 
90 
91 #endif // RAPIDJSON_FILESTREAM_H_
size_t PutEnd(char *)
Definition: filewritestream.h:70
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
const size_t bufferSize
Definition: RASession.h:31
void PutN(char c, size_t n)
Definition: filewritestream.h:42
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
char Take()
Definition: filewritestream.h:67
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:85
void Flush()
Definition: filewritestream.h:58
Wrapper of C file stream for input using fread().
Definition: filewritestream.h:27
char Ch
Character type. Only support char.
Definition: filewritestream.h:29
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
FileWriteStream & operator=(const FileWriteStream &)
void Put(char c)
Definition: filewritestream.h:35
char * current_
Definition: filewritestream.h:80
FileWriteStream(std::FILE *fp, char *buffer, size_t bufferSize)
Definition: filewritestream.h:31
char Peek() const
Definition: filewritestream.h:66
char * PutBegin()
Definition: filewritestream.h:69
char * bufferEnd_
Definition: filewritestream.h:79
char * buffer_
Definition: filewritestream.h:78
common definitions and configuration
size_t Tell() const
Definition: filewritestream.h:68
std::FILE * fp_
Definition: filewritestream.h:77