io: give fd_ref a flags argument

This commit is contained in:
Danny Robson 2014-07-15 19:47:15 +10:00
parent fba15db34b
commit a9af10ec42
2 changed files with 9 additions and 7 deletions

12
io.cpp
View File

@ -38,7 +38,7 @@ using namespace util;
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
std::unique_ptr<char []> std::unique_ptr<char []>
util::slurp (const boost::filesystem::path& path) { util::slurp (const boost::filesystem::path& path) {
fd_ref fd(path); fd_ref fd(path, O_RDONLY);
// Calculate the total file size // Calculate the total file size
off_t size = lseek (fd, 0, SEEK_END); off_t size = lseek (fd, 0, SEEK_END);
@ -77,7 +77,7 @@ util::write (const boost::filesystem::path &path, const char *data, size_t len)
CHECK_SOFT (len > 0); CHECK_SOFT (len > 0);
CHECK_HARD (data); CHECK_HARD (data);
fd_ref fd (path); fd_ref fd (path, O_WRONLY);
const char *cursor = data; const char *cursor = data;
size_t remaining = len; size_t remaining = len;
@ -100,11 +100,11 @@ fd_ref::fd_ref (int _fd):
} }
fd_ref::fd_ref (const boost::filesystem::path &path): fd_ref::fd_ref (const boost::filesystem::path &path, int flags):
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
fd (open (path.string ().c_str (), O_RDONLY | O_BINARY)) fd (open (path.native ().c_str (), flags | O_BINARY))
#else #else
fd (open (path.string ().c_str (), O_RDONLY)) fd (open (path.native ().c_str (), flags))
#endif #endif
{ {
if (fd < 0) if (fd < 0)
@ -183,7 +183,7 @@ util::set_cwd (const boost::filesystem::path &path) {
mapped_file::mapped_file (const boost::filesystem::path &_path): mapped_file::mapped_file (const boost::filesystem::path &_path):
m_fd (open (_path.native ().c_str (), O_RDWR)) m_fd (_path, O_RDWR)
{ load_fd (); } { load_fd (); }

4
io.hpp
View File

@ -46,13 +46,15 @@ namespace util {
void void
write (const boost::filesystem::path &, const char *data, size_t len); write (const boost::filesystem::path &, const char *data, size_t len);
///------------------------------------------------------------------------
/// A simple RAII wrapper for file descriptors /// A simple RAII wrapper for file descriptors
struct fd_ref { struct fd_ref {
public: public:
int fd; int fd;
explicit fd_ref (int _fd); explicit fd_ref (int _fd);
explicit fd_ref (const boost::filesystem::path &); explicit fd_ref (const boost::filesystem::path&, int flags);
~fd_ref (); ~fd_ref ();
operator int (void) const; operator int (void) const;