File indexing completed on 2025-02-21 10:13:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #ifndef RAPIDJSON_FILEWRITESTREAM_H_
0016 #define RAPIDJSON_FILEWRITESTREAM_H_
0017
0018 #include "stream.h"
0019 #include <cstdio>
0020
0021 #ifdef __clang__
0022 RAPIDJSON_DIAG_PUSH
0023 RAPIDJSON_DIAG_OFF(unreachable-code)
0024 #endif
0025
0026 RAPIDJSON_NAMESPACE_BEGIN
0027
0028
0029
0030
0031
0032 class FileWriteStream {
0033 public:
0034 typedef char Ch;
0035
0036 FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
0037 RAPIDJSON_ASSERT(fp_ != 0);
0038 }
0039
0040 void Put(char c) {
0041 if (current_ >= bufferEnd_)
0042 Flush();
0043
0044 *current_++ = c;
0045 }
0046
0047 void PutN(char c, size_t n) {
0048 size_t avail = static_cast<size_t>(bufferEnd_ - current_);
0049 while (n > avail) {
0050 std::memset(current_, c, avail);
0051 current_ += avail;
0052 Flush();
0053 n -= avail;
0054 avail = static_cast<size_t>(bufferEnd_ - current_);
0055 }
0056
0057 if (n > 0) {
0058 std::memset(current_, c, n);
0059 current_ += n;
0060 }
0061 }
0062
0063 void Flush() {
0064 if (current_ != buffer_) {
0065 size_t result = std::fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
0066 if (result < static_cast<size_t>(current_ - buffer_)) {
0067
0068
0069 }
0070 current_ = buffer_;
0071 }
0072 }
0073
0074
0075 char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
0076 char Take() { RAPIDJSON_ASSERT(false); return 0; }
0077 size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
0078 char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
0079 size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
0080
0081 private:
0082
0083 FileWriteStream(const FileWriteStream&);
0084 FileWriteStream& operator=(const FileWriteStream&);
0085
0086 std::FILE* fp_;
0087 char *buffer_;
0088 char *bufferEnd_;
0089 char *current_;
0090 };
0091
0092
0093 template<>
0094 inline void PutN(FileWriteStream& stream, char c, size_t n) {
0095 stream.PutN(c, n);
0096 }
0097
0098 RAPIDJSON_NAMESPACE_END
0099
0100 #ifdef __clang__
0101 RAPIDJSON_DIAG_POP
0102 #endif
0103
0104 #endif