From c1aa0f7a3d42f0b3747f9fa8fbb6fb1254d65af9 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 12 Sep 2017 14:18:34 +1000 Subject: [PATCH] io: slurp should default to providing bytes, not chars std::byte has less potential for abuse. if you want an array of chars you should ask for it. --- backtrace_execinfo.cpp | 2 +- io.cpp | 34 +++++++++++++++++++++++++++------- io.hpp | 10 +++++++--- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/backtrace_execinfo.cpp b/backtrace_execinfo.cpp index 4bb0c8d4..40176d78 100644 --- a/backtrace_execinfo.cpp +++ b/backtrace_execinfo.cpp @@ -60,7 +60,7 @@ addr2line (const void *addr) ); // inefficient to copy from vector to string, but it's not a high priority path - auto data = util::slurp (stream.get ()); + auto data = util::slurp (stream.get ()); return std::string (data.cbegin (), data.cend ()); #else diff --git a/io.cpp b/io.cpp index f984633b..218e3a10 100644 --- a/io.cpp +++ b/io.cpp @@ -32,10 +32,15 @@ using namespace util; -//---------------------------------------------------------------------------- -std::vector +////////////////////////////////////////////////////////////////////////////// +template +std::vector util::slurp (const std::experimental::filesystem::path &path) { + static_assert ( + sizeof (T) == 1, + "slurp is designed for grabbing bytes, not complex structures" + ); posix::fd out (path, O_RDONLY | O_BINARY); // Calculate the total file size @@ -47,11 +52,11 @@ util::slurp (const std::experimental::filesystem::path &path) throw errno_error (); // Allocate a buffer, and keep reading until it's full. - std::vector buffer (size); + std::vector buffer (size); CHECK_GE (size, 0); size_t remaining = (size_t)size; - char *cursor = buffer.data (); + T *cursor = buffer.data (); while (remaining) { ssize_t consumed = ::read (out, cursor, remaining); @@ -69,9 +74,20 @@ util::slurp (const std::experimental::filesystem::path &path) //----------------------------------------------------------------------------- -std::vector +template std::vector util::slurp (const std::experimental::filesystem::path&); +template std::vector util::slurp (const std::experimental::filesystem::path&); + + +/////////////////////////////////////////////////////////////////////////////// +template +std::vector util::slurp (FILE *stream) { + static_assert ( + sizeof (T) == 1, + "slurp is designed for grabbing bytes, not complex structures" + ); + // find how much data is in this file const int desc = fileno (stream); if (desc < 0) @@ -81,7 +97,7 @@ util::slurp (FILE *stream) if (fstat (desc, &meta) < 0) errno_error::throw_code (); - std::vector buf; + std::vector buf; // we think we know the size, so try to do a simple read if (meta.st_size) { @@ -113,8 +129,12 @@ util::slurp (FILE *stream) } - //----------------------------------------------------------------------------- +template std::vector util::slurp (FILE*); +template std::vector util::slurp (FILE*); + + +/////////////////////////////////////////////////////////////////////////////// void util::write (const posix::fd &out, const void *restrict data, diff --git a/io.hpp b/io.hpp index 521109b5..f3bbe42b 100644 --- a/io.hpp +++ b/io.hpp @@ -20,8 +20,9 @@ #include "platform.hpp" #include "posix/fd.hpp" -#include +#include #include +#include #include #include #include @@ -35,8 +36,11 @@ namespace util { //------------------------------------------------------------------------- /// Reads an entire file into memory. - std::vector slurp (const std::experimental::filesystem::path&); - std::vector slurp (FILE *); + template + std::vector slurp (const std::experimental::filesystem::path&); + + template + std::vector slurp (FILE *); //-------------------------------------------------------------------------