Change slurp to return a unique_ptr

This commit is contained in:
Danny Robson 2012-04-26 18:18:09 +10:00
parent dc9a11d202
commit 1d59bae113
3 changed files with 10 additions and 7 deletions

8
io.cpp
View File

@ -33,7 +33,7 @@ using namespace std;
using namespace util; using namespace util;
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
uint8_t * std::unique_ptr<char []>
util::slurp (const boost::filesystem::path& path) { util::slurp (const boost::filesystem::path& path) {
fd_ref fd(open (path.string ().c_str (), O_RDONLY)); // | O_CLOEXEC)); 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 // 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. // padding at the tail as a 'just in case' measure for string manipulation.
unique_ptr <uint8_t[]> buffer (new uint8_t[size + 1]); unique_ptr <char []> buffer (new char[size + 1]);
buffer.get()[size] = '\0'; buffer.get()[size] = '\0';
check_hard (size >= 0); check_hard (size >= 0);
size_t remaining = (size_t)size; size_t remaining = (size_t)size;
uint8_t *cursor = buffer.get(); char *cursor = buffer.get();
while (remaining) { while (remaining) {
ssize_t consumed = read (fd, cursor, remaining); ssize_t consumed = read (fd, cursor, remaining);
@ -65,7 +65,7 @@ util::slurp (const boost::filesystem::path& path) {
cursor += consumed; cursor += consumed;
} }
return buffer.release(); return buffer;
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------

3
io.hpp
View File

@ -22,6 +22,7 @@
#include "annotations.hpp" #include "annotations.hpp"
#include "types.hpp" #include "types.hpp"
#include "memory.hpp"
#include <cstdio> #include <cstdio>
#include <cstdint> #include <cstdint>
@ -40,7 +41,7 @@ namespace util {
/// Reads an entire file into memory. Caller frees the result. Guarantees a /// Reads an entire file into memory. Caller frees the result. Guarantees a
/// null trailing byte. /// null trailing byte.
uint8_t * std::unique_ptr<char []>
slurp (const boost::filesystem::path&) mustuse; slurp (const boost::filesystem::path&) mustuse;
/// A simple RAII wrapper for file descriptors /// A simple RAII wrapper for file descriptors

View File

@ -255,8 +255,10 @@ is_integer (const json::node &node) {
// //
std::unique_ptr<json::node> std::unique_ptr<json::node>
json::parse (const boost::filesystem::path &path) json::parse (const boost::filesystem::path &path) {
{ return parse ((const char *)slurp (path)); } auto data = slurp (path);
return parse (static_cast <const char *> (data.get ()));
}
std::unique_ptr<json::node> std::unique_ptr<json::node>