output.cc
Go to the documentation of this file.
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 
16 
17 #include <errno.h>
18 #include <cstring>
19 
20 namespace absl {
21 namespace str_format_internal {
22 
23 namespace {
24 struct ClearErrnoGuard {
25  ClearErrnoGuard() : old_value(errno) { errno = 0; }
26  ~ClearErrnoGuard() {
27  if (!errno) errno = old_value;
28  }
29  int old_value;
30 };
31 } // namespace
32 
34  size_t to_write = std::min(v.size(), size_);
35  std::memcpy(buffer_, v.data(), to_write);
36  buffer_ += to_write;
37  size_ -= to_write;
38  total_written_ += v.size();
39 }
40 
42  while (!v.empty() && !error_) {
43  // Reset errno to zero in case the libc implementation doesn't set errno
44  // when a failure occurs.
45  ClearErrnoGuard guard;
46 
47  if (size_t result = std::fwrite(v.data(), 1, v.size(), output_)) {
48  // Some progress was made.
49  count_ += result;
50  v.remove_prefix(result);
51  } else {
52  if (errno == EINTR) {
53  continue;
54  } else if (errno) {
55  error_ = errno;
56  } else if (std::ferror(output_)) {
57  // Non-POSIX compliant libc implementations may not set errno, so we
58  // have check the streams error indicator.
59  error_ = EBADF;
60  } else {
61  // We're likely on a non-POSIX system that encountered EINTR but had no
62  // way of reporting it.
63  continue;
64  }
65  }
66  }
67 }
68 
69 } // namespace str_format_internal
70 } // namespace absl
int v
Definition: variant_test.cc:81
void remove_prefix(size_type n)
Definition: string_view.h:310
int old_value
Definition: output.cc:29
Definition: algorithm.h:29
constexpr size_type size() const noexcept
Definition: string_view.h:260
constexpr bool empty() const noexcept
Definition: string_view.h:277
constexpr const_pointer data() const noexcept
Definition: string_view.h:302
int size_
Definition: arg.cc:107


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:19