io: add more write overloads
This commit is contained in:
parent
61c9e40605
commit
75a1f1e5e7
39
io.cpp
39
io.cpp
@ -88,16 +88,20 @@ util::slurp (const boost::filesystem::path& path) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
void
|
||||
util::write (const boost::filesystem::path &path, const T *data, size_t len) {
|
||||
CHECK_GT (len, 0);
|
||||
CHECK (data);
|
||||
util::write (const fd &out,
|
||||
const T *restrict first,
|
||||
const T *restrict last)
|
||||
{
|
||||
CHECK (first);
|
||||
CHECK (last);
|
||||
CHECK_LE (first, last);
|
||||
|
||||
fd out (path, ACCESS_WRITE);
|
||||
const char *cursor = reinterpret_cast<const char*> (data);
|
||||
size_t remaining = len * sizeof (T);
|
||||
const char *restrict cursor = reinterpret_cast<const char*> (first);
|
||||
size_t remaining = sizeof (T) * (last - first);
|
||||
|
||||
while (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<uint8_t> (const boost::filesystem::path&, const uint8_t*, size_t);
|
||||
template void util::write (const fd&, const char*, const char*);
|
||||
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
36
io.hpp
@ -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
|
||||
struct fd {
|
||||
@ -67,12 +56,13 @@ namespace util {
|
||||
};
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
#ifdef PLATFORM_WIN32
|
||||
struct handle_ref : util::nocopy {
|
||||
struct handle : util::nocopy {
|
||||
public:
|
||||
explicit handle_ref (HANDLE);
|
||||
explicit handle_ref ();
|
||||
~handle_ref ();
|
||||
explicit handle (HANDLE);
|
||||
explicit handle ();
|
||||
~handle ();
|
||||
|
||||
void reset (HANDLE);
|
||||
|
||||
@ -83,6 +73,22 @@ namespace util {
|
||||
#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 {
|
||||
protected:
|
||||
|
12
io.ipp
12
io.ipp
@ -6,12 +6,23 @@
|
||||
#endif
|
||||
|
||||
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>
|
||||
indented<T>::indented (const T &_data):
|
||||
data (_data)
|
||||
{ ; }
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
indented<T>
|
||||
make_indented (const T &_data) {
|
||||
@ -19,6 +30,7 @@ namespace util {
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
std::ostream&
|
||||
operator<< (std::ostream &os, const util::indented<T> &&v) {
|
||||
|
Loading…
Reference in New Issue
Block a user