DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers filewritestream.h Source File

filewritestream.h

00001 // Tencent is pleased to support the open source community by making RapidJSON available.
00002 // 
00003 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
00004 //
00005 // Licensed under the MIT License (the "License"); you may not use this file except
00006 // in compliance with the License. You may obtain a copy of the License at
00007 //
00008 // http://opensource.org/licenses/MIT
00009 //
00010 // Unless required by applicable law or agreed to in writing, software distributed 
00011 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
00012 // CONDITIONS OF ANY KIND, either express or implied. See the License for the 
00013 // specific language governing permissions and limitations under the License.
00014 
00015 #ifndef RAPIDJSON_FILEWRITESTREAM_H_
00016 #define RAPIDJSON_FILEWRITESTREAM_H_
00017 
00018 #include "stream.h"
00019 #include <cstdio>
00020 
00021 #ifdef __clang__
00022 RAPIDJSON_DIAG_PUSH
00023 RAPIDJSON_DIAG_OFF(unreachable-code)
00024 #endif
00025 
00026 RAPIDJSON_NAMESPACE_BEGIN
00027 
00028 //! Wrapper of C file stream for input using fread().
00029 /*!
00030     \note implements Stream concept
00031 */
00032 class FileWriteStream {
00033 public:
00034     typedef char Ch;    //!< Character type. Only support char.
00035 
00036     FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { 
00037         RAPIDJSON_ASSERT(fp_ != 0);
00038     }
00039 
00040     void Put(char c) { 
00041         if (current_ >= bufferEnd_)
00042             Flush();
00043 
00044         *current_++ = c;
00045     }
00046 
00047     void PutN(char c, size_t n) {
00048         size_t avail = static_cast<size_t>(bufferEnd_ - current_);
00049         while (n > avail) {
00050             std::memset(current_, c, avail);
00051             current_ += avail;
00052             Flush();
00053             n -= avail;
00054             avail = static_cast<size_t>(bufferEnd_ - current_);
00055         }
00056 
00057         if (n > 0) {
00058             std::memset(current_, c, n);
00059             current_ += n;
00060         }
00061     }
00062 
00063     void Flush() {
00064         if (current_ != buffer_) {
00065             size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
00066             if (result < static_cast<size_t>(current_ - buffer_)) {
00067                 // failure deliberately ignored at this time
00068                 // added to avoid warn_unused_result build errors
00069             }
00070             current_ = buffer_;
00071         }
00072     }
00073 
00074     // Not implemented
00075     char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
00076     char Take() { RAPIDJSON_ASSERT(false); return 0; }
00077     size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
00078     char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
00079     size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
00080 
00081 private:
00082     // Prohibit copy constructor & assignment operator.
00083     FileWriteStream(const FileWriteStream&);
00084     FileWriteStream& operator=(const FileWriteStream&);
00085 
00086     std::FILE* fp_;
00087     char *buffer_;
00088     char *bufferEnd_;
00089     char *current_;
00090 };
00091 
00092 //! Implement specialized version of PutN() with memset() for better performance.
00093 template<>
00094 inline void PutN(FileWriteStream& stream, char c, size_t n) {
00095     stream.PutN(c, n);
00096 }
00097 
00098 RAPIDJSON_NAMESPACE_END
00099 
00100 #ifdef __clang__
00101 RAPIDJSON_DIAG_POP
00102 #endif
00103 
00104 #endif // RAPIDJSON_FILESTREAM_H_