• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "absl/strings/internal/str_format/output.h"
16 
17 #include <errno.h>
18 #include <cstring>
19 
20 namespace absl {
21 ABSL_NAMESPACE_BEGIN
22 namespace str_format_internal {
23 
24 namespace {
25 struct ClearErrnoGuard {
ClearErrnoGuardabsl::str_format_internal::__anon03d0c7a20111::ClearErrnoGuard26   ClearErrnoGuard() : old_value(errno) { errno = 0; }
~ClearErrnoGuardabsl::str_format_internal::__anon03d0c7a20111::ClearErrnoGuard27   ~ClearErrnoGuard() {
28     if (!errno) errno = old_value;
29   }
30   int old_value;
31 };
32 }  // namespace
33 
Write(string_view v)34 void BufferRawSink::Write(string_view v) {
35   size_t to_write = std::min(v.size(), size_);
36   std::memcpy(buffer_, v.data(), to_write);
37   buffer_ += to_write;
38   size_ -= to_write;
39   total_written_ += v.size();
40 }
41 
Write(string_view v)42 void FILERawSink::Write(string_view v) {
43   while (!v.empty() && !error_) {
44     // Reset errno to zero in case the libc implementation doesn't set errno
45     // when a failure occurs.
46     ClearErrnoGuard guard;
47 
48     if (size_t result = std::fwrite(v.data(), 1, v.size(), output_)) {
49       // Some progress was made.
50       count_ += result;
51       v.remove_prefix(result);
52     } else {
53       if (errno == EINTR) {
54         continue;
55       } else if (errno) {
56         error_ = errno;
57       } else if (std::ferror(output_)) {
58         // Non-POSIX compliant libc implementations may not set errno, so we
59         // have check the streams error indicator.
60         error_ = EBADF;
61       } else {
62         // We're likely on a non-POSIX system that encountered EINTR but had no
63         // way of reporting it.
64         continue;
65       }
66     }
67   }
68 }
69 
70 }  // namespace str_format_internal
71 ABSL_NAMESPACE_END
72 }  // namespace absl
73