filewritestream.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
  51. current_ = buffer_;
  52. }
  53. }
  54. // Not implemented
  55. char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
  56. char Take() { RAPIDJSON_ASSERT(false); return 0; }
  57. size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
  58. char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  59. size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
  60. private:
  61. // Prohibit copy constructor & assignment operator.
  62. FileWriteStream(const FileWriteStream&);
  63. FileWriteStream& operator=(const FileWriteStream&);
  64. std::FILE* fp_;
  65. char *buffer_;
  66. char *bufferEnd_;
  67. char *current_;
  68. };
  69. //! Implement specialized version of PutN() with memset() for better performance.
  70. template<>
  71. inline void PutN(FileWriteStream& stream, char c, size_t n) {
  72. stream.PutN(c, n);
  73. }
  74. RAPIDJSON_NAMESPACE_END
  75. #endif // RAPIDJSON_FILESTREAM_H_