webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
filewritestream.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_FILEWRITESTREAM_H_
22 #define RAPIDJSON_FILEWRITESTREAM_H_
23 
24 #include "rapidjson.h"
25 #include <cstdio>
26 
28 
30 
34 public:
35  typedef char Ch;
36 
37  FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
38  RAPIDJSON_ASSERT(fp_ != 0);
39  }
40 
41  void Put(char c) {
42  if (current_ >= bufferEnd_)
43  Flush();
44 
45  *current_++ = c;
46  }
47 
48  void PutN(char c, size_t n) {
49  size_t avail = static_cast<size_t>(bufferEnd_ - current_);
50  while (n > avail) {
51  std::memset(current_, c, avail);
52  current_ += avail;
53  Flush();
54  n -= avail;
55  avail = static_cast<size_t>(bufferEnd_ - current_);
56  }
57 
58  if (n > 0) {
59  std::memset(current_, c, n);
60  current_ += n;
61  }
62  }
63 
64  void Flush() {
65  if (current_ != buffer_) {
66  fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
67  current_ = buffer_;
68  }
69  }
70 
71  // Not implemented
72  char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
73  char Take() { RAPIDJSON_ASSERT(false); return 0; }
74  size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
75  char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
76  size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
77 
78 private:
79  // Prohibit copy constructor & assignment operator.
81  FileWriteStream& operator=(const FileWriteStream&);
82 
83  std::FILE* fp_;
84  char *buffer_;
85  char *bufferEnd_;
86  char *current_;
87 };
88 
90 template<>
91 inline void PutN(FileWriteStream& stream, char c, size_t n) {
92  stream.PutN(c, n);
93 }
94 
96 
97 #endif // RAPIDJSON_FILESTREAM_H_
void PutN(char c, size_t n)
Definition: filewritestream.h:48
char Ch
Character type. Only support char.
Definition: filewritestream.h:35
EGLStreamKHR stream
Definition: eglext.h:340
int c
Definition: cpp_unittests.cpp:275
char Peek() const
Definition: filewritestream.h:72
FileWriteStream(std::FILE *fp, char *buffer, size_t bufferSize)
Definition: filewritestream.h:37
void Put(char c)
Definition: filewritestream.h:41
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
int FILE
Definition: antglob.py:49
size_t Tell() const
Definition: filewritestream.h:74
EGLStreamKHR EGLint n
Definition: eglext.h:984
size_t PutEnd(char *)
Definition: filewritestream.h:76
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
char Take()
Definition: filewritestream.h:73
char * PutBegin()
Definition: filewritestream.h:75
void Flush()
Definition: filewritestream.h:64
common definitions and configuration
EGLContext EGLenum EGLClientBuffer buffer
Definition: eglext.h:192
Wrapper of C file stream for input using fread().
Definition: filewritestream.h:33
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:315