From 653ada4bfd882b1c5c561295ffffbcaa2a83df38 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 11 Jun 2015 19:28:03 +1000 Subject: [PATCH] io: change slurp return type to vector --- io.cpp | 8 ++++---- io.hpp | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/io.cpp b/io.cpp index 4023e88c..bb5436a2 100644 --- a/io.cpp +++ b/io.cpp @@ -53,7 +53,7 @@ access_to_cflags (access_t a) { } //---------------------------------------------------------------------------- -std::unique_ptr +std::vector util::slurp (const boost::filesystem::path& path) { fd out (path, ACCESS_READ); @@ -67,12 +67,12 @@ util::slurp (const boost::filesystem::path& path) { // Allocate a buffer, and keep reading until it's full. We provide a null // padding at the tail as a 'just in case' measure for string manipulation. - unique_ptr buffer (new char[size + 1]); - buffer.get()[size] = '\0'; + std::vector buffer (size + 1); + buffer.data ()[size] = '\0'; CHECK_GE (size, 0); size_t remaining = (size_t)size; - char *cursor = buffer.get(); + char *cursor = buffer.data (); while (remaining) { ssize_t consumed = ::read (out, cursor, remaining); diff --git a/io.hpp b/io.hpp index 3d3e03f2..ddb1c1ff 100644 --- a/io.hpp +++ b/io.hpp @@ -39,11 +39,13 @@ namespace util { ACCESS_READWRITE = ACCESS_READ | ACCESS_WRITE }; + /// Reads an entire file into memory. Caller frees the result. Guarantees a /// null trailing byte. - std::unique_ptr + std::vector slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&); + template void write (const boost::filesystem::path &, const T *data, size_t len);