cphot 0.1
A C++ tool for computing photometry from spectra.
filewritestream.h
Go to the documentation of this file.
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 
15 #ifndef RAPIDJSON_FILEWRITESTREAM_H_
16 #define RAPIDJSON_FILEWRITESTREAM_H_
17 
18 #include "stream.h"
19 #include <cstdio>
20 
21 #ifdef __clang__
22 RAPIDJSON_DIAG_PUSH
23 RAPIDJSON_DIAG_OFF(unreachable-code)
24 #endif
25 
27 
28 //! Wrapper of C file stream for input using fread().
29 /*!
30  \note implements Stream concept
31 */
33 public:
34  typedef char Ch; //!< Character type. Only support char.
35 
36  FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) {
37  RAPIDJSON_ASSERT(fp_ != 0);
38  }
39 
40  void Put(char c) {
41  if (current_ >= bufferEnd_)
42  Flush();
43 
44  *current_++ = c;
45  }
46 
47  void PutN(char c, size_t n) {
48  size_t avail = static_cast<size_t>(bufferEnd_ - current_);
49  while (n > avail) {
50  std::memset(current_, c, avail);
51  current_ += avail;
52  Flush();
53  n -= avail;
54  avail = static_cast<size_t>(bufferEnd_ - current_);
55  }
56 
57  if (n > 0) {
58  std::memset(current_, c, n);
59  current_ += n;
60  }
61  }
62 
63  void Flush() {
64  if (current_ != buffer_) {
65  size_t result = fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_);
66  if (result < static_cast<size_t>(current_ - buffer_)) {
67  // failure deliberately ignored at this time
68  // added to avoid warn_unused_result build errors
69  }
70  current_ = buffer_;
71  }
72  }
73 
74  // Not implemented
75  char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
76  char Take() { RAPIDJSON_ASSERT(false); return 0; }
77  size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
78  char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
79  size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
80 
81 private:
82  // Prohibit copy constructor & assignment operator.
84  FileWriteStream& operator=(const FileWriteStream&);
85 
86  std::FILE* fp_;
87  char *buffer_;
88  char *bufferEnd_;
89  char *current_;
90 };
91 
92 //! Implement specialized version of PutN() with memset() for better performance.
93 template<>
94 inline void PutN(FileWriteStream& stream, char c, size_t n) {
95  stream.PutN(c, n);
96 }
97 
99 
100 #ifdef __clang__
101 RAPIDJSON_DIAG_POP
102 #endif
103 
104 #endif // RAPIDJSON_FILESTREAM_H_
RAPIDJSON_NAMESPACE_END
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
FileWriteStream
Wrapper of C file stream for input using fread().
Definition: filewritestream.h:32
RAPIDJSON_NAMESPACE_BEGIN
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
FileWriteStream::Put
void Put(char c)
Definition: filewritestream.h:40
FileWriteStream::Peek
char Peek() const
Definition: filewritestream.h:75
RAPIDJSON_ASSERT
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:402
FileWriteStream::PutEnd
size_t PutEnd(char *)
Definition: filewritestream.h:79
FileWriteStream::FileWriteStream
FileWriteStream(std::FILE *fp, char *buffer, size_t bufferSize)
Definition: filewritestream.h:36
c
constexpr QSpeed c
Definition: rquantities.hpp:351
stream.h
FileWriteStream::Flush
void Flush()
Definition: filewritestream.h:63
FileWriteStream::PutBegin
char * PutBegin()
Definition: filewritestream.h:78
FileWriteStream::Tell
size_t Tell() const
Definition: filewritestream.h:77
FileWriteStream::Take
char Take()
Definition: filewritestream.h:76
FileWriteStream::PutN
void PutN(char c, size_t n)
Definition: filewritestream.h:47
PutN
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:94
FileWriteStream::Ch
char Ch
Character type.
Definition: filewritestream.h:34