From 1d59bae113f8871687e217cf2acd68084fd5e5bd Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Thu, 26 Apr 2012 18:18:09 +1000 Subject: [PATCH] Change slurp to return a unique_ptr --- io.cpp | 8 ++++---- io.hpp | 3 ++- json.cpp.rl | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/io.cpp b/io.cpp index c09be54d..69e8eae0 100644 --- a/io.cpp +++ b/io.cpp @@ -33,7 +33,7 @@ using namespace std; using namespace util; //---------------------------------------------------------------------------- -uint8_t * +std::unique_ptr util::slurp (const boost::filesystem::path& path) { fd_ref fd(open (path.string ().c_str (), O_RDONLY)); // | O_CLOEXEC)); @@ -47,12 +47,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 uint8_t[size + 1]); + unique_ptr buffer (new char[size + 1]); buffer.get()[size] = '\0'; check_hard (size >= 0); size_t remaining = (size_t)size; - uint8_t *cursor = buffer.get(); + char *cursor = buffer.get(); while (remaining) { ssize_t consumed = read (fd, cursor, remaining); @@ -65,7 +65,7 @@ util::slurp (const boost::filesystem::path& path) { cursor += consumed; } - return buffer.release(); + return buffer; } //---------------------------------------------------------------------------- diff --git a/io.hpp b/io.hpp index b7258b57..b7003bc9 100644 --- a/io.hpp +++ b/io.hpp @@ -22,6 +22,7 @@ #include "annotations.hpp" #include "types.hpp" +#include "memory.hpp" #include #include @@ -40,7 +41,7 @@ namespace util { /// Reads an entire file into memory. Caller frees the result. Guarantees a /// null trailing byte. - uint8_t * + std::unique_ptr slurp (const boost::filesystem::path&) mustuse; /// A simple RAII wrapper for file descriptors diff --git a/json.cpp.rl b/json.cpp.rl index 10f99915..8d311ebc 100644 --- a/json.cpp.rl +++ b/json.cpp.rl @@ -255,8 +255,10 @@ is_integer (const json::node &node) { // std::unique_ptr -json::parse (const boost::filesystem::path &path) - { return parse ((const char *)slurp (path)); } +json::parse (const boost::filesystem::path &path) { + auto data = slurp (path); + return parse (static_cast (data.get ())); +} std::unique_ptr