filewritestream.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef RAPIDJSON_FILEWRITESTREAM_H_
  15. #define RAPIDJSON_FILEWRITESTREAM_H_
  16. #include "stream.h"
  17. #include <cstdio>
  18. #ifdef __clang__
  19. RAPIDJSON_DIAG_PUSH
  20. RAPIDJSON_DIAG_OFF(unreachable-code)
  21. #endif
  22. RAPIDJSON_NAMESPACE_BEGIN
  23. //! Wrapper of C file stream for output using fwrite().
  24. /*!
  25. \note implements Stream concept
  26. */
  27. class FileWriteStream {
  28. public:
  29. typedef char Ch; //!< Character type. Only support char.
  30. FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
  31. RAPIDJSON_ASSERT(fp_ != 0);
  32. }
  33. void Put(char c) {
  34. if (current_ >= bufferEnd_)
  35. Flush();
  36. *current_++ = c;
  37. }
  38. void PutN(char c, size_t n) {
  39. size_t avail = static_cast<size_t>(bufferEnd_ - current_);
  40. while (n > avail) {
  41. std::memset(current_, c, avail);
  42. current_ += avail;
  43. Flush();
  44. n -= avail;
  45. avail = static_cast<size_t>(bufferEnd_ - current_);
  46. }
  47. if (n > 0) {
  48. std::memset(current_, c, n);
  49. current_ += n;
  50. }
  51. }
  52. void Flush() {
  53. if (current_ != buffer_) {
  54. size_t result = std::fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
  55. if (result < static_cast<size_t>(current_ - buffer_)) {
  56. // failure deliberately ignored at this time
  57. // added to avoid warn_unused_result build errors
  58. }
  59. current_ = buffer_;
  60. }
  61. }
  62. // Not implemented
  63. char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
  64. char Take() { RAPIDJSON_ASSERT(false); return 0; }
  65. size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
  66. char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  67. size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
  68. private:
  69. // Prohibit copy constructor & assignment operator.
  70. FileWriteStream(const FileWriteStream&);
  71. FileWriteStream& operator=(const FileWriteStream&);
  72. std::FILE* fp_;
  73. char *buffer_;
  74. char *bufferEnd_;
  75. char *current_;
  76. };
  77. //! Implement specialized version of PutN() with memset() for better performance.
  78. template<>
  79. inline void PutN(FileWriteStream& stream, char c, size_t n) {
  80. stream.PutN(c, n);
  81. }
  82. RAPIDJSON_NAMESPACE_END
  83. #ifdef __clang__
  84. RAPIDJSON_DIAG_POP
  85. #endif
  86. #endif // RAPIDJSON_FILESTREAM_H_