diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2020-12-02 14:00:43 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2020-12-02 14:17:27 +0100 |
commit | faa31f40846f7a4dbc2487d000b112a6aef69d1b (patch) | |
tree | 37f34b2378c81d9f2bb8fc79046d12b4e0592647 /src/libutil/util.cc | |
parent | aa684861127eabc18a0d7386a66c5b75d4962897 (diff) |
Sink: Use std::string_view
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r-- | src/libutil/util.cc | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc index 98a069c3e..3d4fc4c25 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -346,7 +346,7 @@ void writeFile(const Path & path, Source & source, mode_t mode) while (true) { try { auto n = source.read(buf.data(), buf.size()); - writeFull(fd.get(), (unsigned char *) buf.data(), n); + writeFull(fd.get(), {(char *) buf.data(), n}); } catch (EndOfFile &) { break; } } } catch (Error & e) { @@ -648,27 +648,19 @@ void readFull(int fd, unsigned char * buf, size_t count) } -void writeFull(int fd, const unsigned char * buf, size_t count, bool allowInterrupts) +void writeFull(int fd, std::string_view s, bool allowInterrupts) { - while (count) { + while (!s.empty()) { if (allowInterrupts) checkInterrupt(); - ssize_t res = write(fd, (char *) buf, count); + ssize_t res = write(fd, s.data(), s.size()); if (res == -1 && errno != EINTR) throw SysError("writing to file"); - if (res > 0) { - count -= res; - buf += res; - } + if (res > 0) + s.remove_prefix(res); } } -void writeFull(int fd, std::string_view s, bool allowInterrupts) -{ - writeFull(fd, (const unsigned char *) s.data(), s.size(), allowInterrupts); -} - - string drainFD(int fd, bool block, const size_t reserveSize) { StringSink sink(reserveSize); @@ -705,7 +697,7 @@ void drainFD(int fd, Sink & sink, bool block) throw SysError("reading from file"); } else if (rd == 0) break; - else sink(buf.data(), rd); + else sink({(char *) buf.data(), (size_t) rd}); } } @@ -1153,7 +1145,7 @@ void runProgram2(const RunOptions & options) } catch (EndOfFile &) { break; } - writeFull(in.writeSide.get(), buf.data(), n); + writeFull(in.writeSide.get(), {(char *) buf.data(), n}); } promise.set_value(); } catch (...) { |