io: add more write overloads

This commit is contained in:
Danny Robson 2015-07-02 16:34:17 +10:00
parent 61c9e40605
commit 75a1f1e5e7
3 changed files with 63 additions and 24 deletions

39
io.cpp
View File

@ -88,16 +88,20 @@ util::slurp (const boost::filesystem::path& path) {
return buffer; return buffer;
} }
//----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template <typename T> template <typename T>
void void
util::write (const boost::filesystem::path &path, const T *data, size_t len) { util::write (const fd &out,
CHECK_GT (len, 0); const T *restrict first,
CHECK (data); const T *restrict last)
{
CHECK (first);
CHECK (last);
CHECK_LE (first, last);
fd out (path, ACCESS_WRITE); const char *restrict cursor = reinterpret_cast<const char*> (first);
const char *cursor = reinterpret_cast<const char*> (data); size_t remaining = sizeof (T) * (last - first);
size_t remaining = len * sizeof (T);
while (remaining) { while (remaining) {
ssize_t consumed = ::write (out, cursor, remaining); ssize_t consumed = ::write (out, cursor, remaining);
@ -110,8 +114,25 @@ util::write (const boost::filesystem::path &path, const T *data, size_t len) {
} }
template void util::write<char> (const boost::filesystem::path&, const char*, size_t); template void util::write (const fd&, const char*, const char*);
template void util::write<uint8_t> (const boost::filesystem::path&, const uint8_t*, size_t); template void util::write (const fd&, const int8_t*, const int8_t*);
template void util::write (const fd&, const uint8_t*, const uint8_t*);
//-----------------------------------------------------------------------------
template <typename T>
void
util::write (const boost::filesystem::path &path,
const T *restrict first,
const T *restrict last)
{
write (fd (path, ACCESS_WRITE), first, last);
}
template void util::write (const boost::filesystem::path&, const char*, const char*);
template void util::write (const boost::filesystem::path&, const int8_t*, const int8_t*);
template void util::write (const boost::filesystem::path&, const uint8_t*, const uint8_t*);
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------

36
io.hpp
View File

@ -40,17 +40,6 @@ namespace util {
}; };
/// Reads an entire file into memory. Caller frees the result. Guarantees a
/// null trailing byte.
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);
///------------------------------------------------------------------------ ///------------------------------------------------------------------------
/// A simple RAII wrapper for file descriptors /// A simple RAII wrapper for file descriptors
struct fd { struct fd {
@ -67,12 +56,13 @@ namespace util {
}; };
//-------------------------------------------------------------------------
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
struct handle_ref : util::nocopy { struct handle : util::nocopy {
public: public:
explicit handle_ref (HANDLE); explicit handle (HANDLE);
explicit handle_ref (); explicit handle ();
~handle_ref (); ~handle ();
void reset (HANDLE); void reset (HANDLE);
@ -83,6 +73,22 @@ namespace util {
#endif #endif
/// Reads an entire file into memory. Caller frees the result. Guarantees a
/// null trailing byte.
std::vector<char>
slurp [[gnu::warn_unused_result]] (const boost::filesystem::path&);
template <typename T>
void write (const fd&, const T *restrict data, size_t count);
template <typename T>
void write (const fd&, const T *restrict first, const T *restrict last);
template <typename T>
void write (const boost::filesystem::path &, const T *restrict first, const T *restrict last);
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class indenter : public std::streambuf { class indenter : public std::streambuf {
protected: protected:

12
io.ipp
View File

@ -6,12 +6,23 @@
#endif #endif
namespace util { namespace util {
//-------------------------------------------------------------------------
template <typename T>
void
write (const fd &_fd, const T *restrict data, size_t count)
{
auto first = reinterpret_cast<const char*> (data);
write (_fd, first, first + sizeof (T) * count);
}
//-------------------------------------------------------------------------
template <typename T> template <typename T>
indented<T>::indented (const T &_data): indented<T>::indented (const T &_data):
data (_data) data (_data)
{ ; } { ; }
//-------------------------------------------------------------------------
template <typename T> template <typename T>
indented<T> indented<T>
make_indented (const T &_data) { make_indented (const T &_data) {
@ -19,6 +30,7 @@ namespace util {
} }
//-------------------------------------------------------------------------
template <typename T> template <typename T>
std::ostream& std::ostream&
operator<< (std::ostream &os, const util::indented<T> &&v) { operator<< (std::ostream &os, const util::indented<T> &&v) {