1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Output extension hooks for the Format library.
16 // `internal::InvokeFlush` calls the appropriate flush function for the
17 // specified output argument.
18 // `BufferRawSink` is a simple output sink for a char buffer. Used by SnprintF.
19 // `FILERawSink` is a std::FILE* based sink. Used by PrintF and FprintF.
20
21 #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
22 #define ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
23
24 #include <cstdio>
25 #include <ostream>
26 #include <string>
27
28 #include "absl/base/port.h"
29 #include "absl/strings/string_view.h"
30
31 namespace absl {
32 ABSL_NAMESPACE_BEGIN
33
34 class Cord;
35
36 namespace str_format_internal {
37
38 // RawSink implementation that writes into a char* buffer.
39 // It will not overflow the buffer, but will keep the total count of chars
40 // that would have been written.
41 class BufferRawSink {
42 public:
BufferRawSink(char * buffer,size_t size)43 BufferRawSink(char* buffer, size_t size) : buffer_(buffer), size_(size) {}
44
total_written()45 size_t total_written() const { return total_written_; }
46 void Write(string_view v);
47
48 private:
49 char* buffer_;
50 size_t size_;
51 size_t total_written_ = 0;
52 };
53
54 // RawSink implementation that writes into a FILE*.
55 // It keeps track of the total number of bytes written and any error encountered
56 // during the writes.
57 class FILERawSink {
58 public:
FILERawSink(std::FILE * output)59 explicit FILERawSink(std::FILE* output) : output_(output) {}
60
61 void Write(string_view v);
62
count()63 size_t count() const { return count_; }
error()64 int error() const { return error_; }
65
66 private:
67 std::FILE* output_;
68 int error_ = 0;
69 size_t count_ = 0;
70 };
71
72 // Provide RawSink integration with common types from the STL.
AbslFormatFlush(std::string * out,string_view s)73 inline void AbslFormatFlush(std::string* out, string_view s) {
74 out->append(s.data(), s.size());
75 }
AbslFormatFlush(std::ostream * out,string_view s)76 inline void AbslFormatFlush(std::ostream* out, string_view s) {
77 out->write(s.data(), s.size());
78 }
79
80 template <class AbslCord, typename = typename std::enable_if<
81 std::is_same<AbslCord, absl::Cord>::value>::type>
AbslFormatFlush(AbslCord * out,string_view s)82 inline void AbslFormatFlush(AbslCord* out, string_view s) {
83 out->Append(s);
84 }
85
AbslFormatFlush(FILERawSink * sink,string_view v)86 inline void AbslFormatFlush(FILERawSink* sink, string_view v) {
87 sink->Write(v);
88 }
89
AbslFormatFlush(BufferRawSink * sink,string_view v)90 inline void AbslFormatFlush(BufferRawSink* sink, string_view v) {
91 sink->Write(v);
92 }
93
94 template <typename T>
95 auto InvokeFlush(T* out, string_view s)
96 -> decltype(str_format_internal::AbslFormatFlush(out, s)) {
97 str_format_internal::AbslFormatFlush(out, s);
98 }
99
100 } // namespace str_format_internal
101 ABSL_NAMESPACE_END
102 } // namespace absl
103
104 #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
105