Change slurp to return a unique_ptr
This commit is contained in:
parent
dc9a11d202
commit
1d59bae113
8
io.cpp
8
io.cpp
@ -33,7 +33,7 @@ using namespace std;
|
||||
using namespace util;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
uint8_t *
|
||||
std::unique_ptr<char []>
|
||||
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 <uint8_t[]> buffer (new uint8_t[size + 1]);
|
||||
unique_ptr <char []> 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;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
3
io.hpp
3
io.hpp
@ -22,6 +22,7 @@
|
||||
|
||||
#include "annotations.hpp"
|
||||
#include "types.hpp"
|
||||
#include "memory.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdint>
|
||||
@ -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<char []>
|
||||
slurp (const boost::filesystem::path&) mustuse;
|
||||
|
||||
/// A simple RAII wrapper for file descriptors
|
||||
|
@ -255,8 +255,10 @@ is_integer (const json::node &node) {
|
||||
//
|
||||
|
||||
std::unique_ptr<json::node>
|
||||
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 <const char *> (data.get ()));
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<json::node>
|
||||
|
Loading…
Reference in New Issue
Block a user