Add a scoped CWD restorer, and set method

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

19
io.cpp
View File

@ -131,6 +131,24 @@ indenter::~indenter ()
m_owner->rdbuf (m_dest); m_owner->rdbuf (m_dest);
} }
//----------------------------------------------------------------------------
scoped_cwd::scoped_cwd ():
m_original (getcwd (nullptr, 0))
{
if (!m_original)
throw errno_error ();
}
scoped_cwd::~scoped_cwd () {
set_cwd (m_original.data ());
}
void
util::set_cwd (const boost::filesystem::path &path) {
chdir (path.string ().c_str ());
}
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
#if defined(HAVE_MMAP) #if defined(HAVE_MMAP)
@ -189,4 +207,3 @@ mapped_file::data (void) const {
} }
#endif #endif

12
io.hpp
View File

@ -75,6 +75,18 @@ namespace util {
virtual ~indenter (); virtual ~indenter ();
}; };
class scoped_cwd {
public:
scoped_cwd ();
~scoped_cwd ();
protected:
scoped_malloc<char> m_original;
};
void set_cwd (const boost::filesystem::path &);
#if defined(HAVE_MMAP) #if defined(HAVE_MMAP)