Give fd_ref a fs::path constructor

This commit is contained in:
Danny Robson 2012-04-26 18:19:12 +10:00
parent 1d59bae113
commit cf63a35e94
2 changed files with 18 additions and 2 deletions

10
io.cpp
View File

@ -35,7 +35,7 @@ using namespace util;
//----------------------------------------------------------------------------
std::unique_ptr<char []>
util::slurp (const boost::filesystem::path& path) {
fd_ref fd(open (path.string ().c_str (), O_RDONLY)); // | O_CLOEXEC));
fd_ref fd(path);
// Calculate the total file size
off_t size = lseek (fd, 0, SEEK_END);
@ -77,6 +77,14 @@ fd_ref::fd_ref (int _fd):
}
fd_ref::fd_ref (const boost::filesystem::path &path):
fd (open (path.string ().c_str (), O_RDONLY))
{
if (fd < 0)
throw path_error (path);
}
fd_ref::~fd_ref () {
check (fd >= 0);
close (fd);

10
io.hpp
View File

@ -49,7 +49,8 @@ namespace util {
public:
int fd;
fd_ref (int _fd);
explicit fd_ref (int _fd);
explicit fd_ref (const boost::filesystem::path &);
~fd_ref ();
operator int (void) const;
@ -100,6 +101,13 @@ namespace util {
size_t size (void) const;
};
#endif
class path_error : public std::runtime_error {
public:
path_error (const boost::filesystem::path &path):
runtime_error ("Invalid path " + path.string ())
{ ; }
};
}
#endif