io: change slurp return type to vector

This commit is contained in:
Danny Robson 2015-06-11 19:28:03 +10:00
parent 33b3b48c74
commit 653ada4bfd
2 changed files with 7 additions and 5 deletions

8
io.cpp
View File

@ -53,7 +53,7 @@ access_to_cflags (access_t a) {
}
//----------------------------------------------------------------------------
std::unique_ptr<char []>
std::vector<char>
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 <char []> buffer (new char[size + 1]);
buffer.get()[size] = '\0';
std::vector<char> 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);

4
io.hpp
View File

@ -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<char []>
std::vector<char>
slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&);
template <typename T>
void
write (const boost::filesystem::path &, const T *data, size_t len);