RetroArch
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 "stream.h"
19 #include <cstdio>
20 
21 #ifdef __clang__
22 RAPIDJSON_DIAG_PUSH
23 RAPIDJSON_DIAG_OFF(unreachable-code)
24 #endif
25 
27 
29 
33 public:
34  typedef char Ch;
35 
36  FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
37  RAPIDJSON_ASSERT(fp_ != 0);
38  }
39 
40  void Put(char c) {
41  if (current_ >= bufferEnd_)
42  Flush();
43 
44  *current_++ = c;
45  }
46 
47  void PutN(char c, size_t n) {
48  size_t avail = static_cast<size_t>(bufferEnd_ - current_);
49  while (n > avail) {
50  std::memset(current_, c, avail);
51  current_ += avail;
52  Flush();
53  n -= avail;
54  avail = static_cast<size_t>(bufferEnd_ - current_);
55  }
56 
57  if (n > 0) {
59  current_ += n;
60  }
61  }
62 
63  void Flush() {
64  if (current_ != buffer_) {
65  size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
66  if (result < static_cast<size_t>(current_ - buffer_)) {
67  // failure deliberately ignored at this time
68  // added to avoid warn_unused_result build errors
69  }
70  current_ = buffer_;
71  }
72  }
73 
74  // Not implemented
75  char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
76  char Take() { RAPIDJSON_ASSERT(false); return 0; }
77  size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
78  char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
79  size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
80 
81 private:
82  // Prohibit copy constructor & assignment operator.
85 
87  char *buffer_;
88  char *bufferEnd_;
89  char *current_;
90 };
91 
93 template<>
94 inline void PutN(FileWriteStream& stream, char c, size_t n) {
95  stream.PutN(c, n);
96 }
97 
99 
100 #ifdef __clang__
101 RAPIDJSON_DIAG_POP
102 #endif
103 
104 #endif // RAPIDJSON_FILESTREAM_H_
char * current_
Definition: filewritestream.h:89
char Peek() const
Definition: filewritestream.h:75
void Put(char c)
Definition: filewritestream.h:40
char Take()
Definition: filewritestream.h:76
char Ch
Character type. Only support char.
Definition: filewritestream.h:34
FileWriteStream & operator=(const FileWriteStream &)
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
const GLubyte * c
Definition: glext.h:9812
char * bufferEnd_
Definition: filewritestream.h:88
FileWriteStream(std::FILE *fp, char *buffer, size_t bufferSize)
Definition: filewritestream.h:36
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
size_t Tell() const
Definition: filewritestream.h:77
GLuint64EXT * result
Definition: glext.h:12211
#define fwrite
Definition: file_stream_transforms.h:59
Definition: inftrees.h:27
#define FILE
Definition: file_stream_transforms.h:35
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:94
GLuint GLuint stream
Definition: glext.h:8189
size_t PutEnd(char *)
Definition: filewritestream.h:79
void Flush()
Definition: filewritestream.h:63
void PutN(char c, size_t n)
Definition: filewritestream.h:47
char * buffer_
Definition: filewritestream.h:87
Wrapper of C file stream for input using fread().
Definition: filewritestream.h:32
Definition: video4linux2.c:51
void * memset(void *b, int c, size_t len)
Definition: string.c:7
GLdouble n
Definition: glext.h:8396
char * PutBegin()
Definition: filewritestream.h:78
std::FILE * fp_
Definition: filewritestream.h:86