2
0

filewritestream.h 3.0 KB

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