123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #ifndef RAPIDJSON_FILEWRITESTREAM_H_
- #define RAPIDJSON_FILEWRITESTREAM_H_
- #include "stream.h"
- #include <cstdio>
- #ifdef __clang__
- RAPIDJSON_DIAG_PUSH
- RAPIDJSON_DIAG_OFF(unreachable-code)
- #endif
- RAPIDJSON_NAMESPACE_BEGIN
- class FileWriteStream {
- public:
- typedef char Ch;
- FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
- RAPIDJSON_ASSERT(fp_ != 0);
- }
- void Put(char c) {
- if (current_ >= bufferEnd_)
- Flush();
- *current_++ = c;
- }
- void PutN(char c, size_t n) {
- size_t avail = static_cast<size_t>(bufferEnd_ - current_);
- while (n > avail) {
- std::memset(current_, c, avail);
- current_ += avail;
- Flush();
- n -= avail;
- avail = static_cast<size_t>(bufferEnd_ - current_);
- }
- if (n > 0) {
- std::memset(current_, c, n);
- current_ += n;
- }
- }
- void Flush() {
- if (current_ != buffer_) {
- size_t result = std::fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
- if (result < static_cast<size_t>(current_ - buffer_)) {
-
-
- }
- current_ = buffer_;
- }
- }
-
- char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
- char Take() { RAPIDJSON_ASSERT(false); return 0; }
- size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
- char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
- size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
- private:
-
- FileWriteStream(const FileWriteStream&);
- FileWriteStream& operator=(const FileWriteStream&);
- std::FILE* fp_;
- char *buffer_;
- char *bufferEnd_;
- char *current_;
- };
- template<>
- inline void PutN(FileWriteStream& stream, char c, size_t n) {
- stream.PutN(c, n);
- }
- RAPIDJSON_NAMESPACE_END
- #ifdef __clang__
- RAPIDJSON_DIAG_POP
- #endif
- #endif
|