Commit 24dd65f0 authored by Davis King's avatar Davis King

Added a set_current_dir() function.

parent 8cd8cfe3
......@@ -4,6 +4,7 @@
#define DLIB_MISC_API_KERNEL_1_CPp_
#include "../platform.h"
#include "../threads.h"
#ifdef WIN32
......@@ -30,9 +31,17 @@ namespace dlib
// ----------------------------------------------------------------------------------------
namespace
{
mutex cwd_mutex;
}
std::string get_current_dir (
)
{
// need to lock a mutex here because getting and setting the
// current working directory is not thread safe on windows.
auto_mutex lock(cwd_mutex);
char buf[1024];
if (GetCurrentDirectoryA(sizeof(buf),buf) == 0)
{
......@@ -44,6 +53,21 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
void set_current_dir (
const std::string& new_dir
)
{
// need to lock a mutex here because getting and setting the
// current working directory is not thread safe on windows.
auto_mutex lock(cwd_mutex);
if (SetCurrentDirectory(new_dir.c_str()) == 0)
{
throw set_current_dir_error("Error changing current dir to '" + new_dir + "'" + GetLastError());
}
}
// ----------------------------------------------------------------------------------------
uint64 timestamper::
......
......@@ -27,6 +27,20 @@ namespace dlib
std::string get_current_dir (
);
// ----------------------------------------------------------------------------------------
class set_current_dir_error : public error
{
public:
set_current_dir_error(
const std::string& a
): error(a) {}
};
void set_current_dir (
const std::string& new_dir
);
// ----------------------------------------------------------------------------------------
class timestamper
......
......@@ -55,6 +55,18 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
void set_current_dir (
const std::string& new_dir
)
{
if (chdir(new_dir.c_str()))
{
throw set_current_dir_error("Error changing current dir to '" + new_dir + "'");
}
}
// ----------------------------------------------------------------------------------------
uint64 timestamper::
......
......@@ -27,6 +27,20 @@ namespace dlib
std::string get_current_dir (
);
// ----------------------------------------------------------------------------------------
class set_current_dir_error : public error
{
public:
set_current_dir_error(
const std::string& a
): error(a) {}
};
void set_current_dir (
const std::string& new_dir
);
// ----------------------------------------------------------------------------------------
class timestamper
......
......@@ -34,7 +34,7 @@ namespace dlib
);
/*!
ensures
- if (no errors occurr) then
- if (no errors occur) then
- returns the path to the current working directory
- else
- returns ""
......@@ -42,6 +42,23 @@ namespace dlib
- std::bad_alloc
!*/
// ----------------------------------------------------------------------------------------
class set_current_dir_error : public error;
void set_current_dir (
const std::string& new_dir
);
/*!
ensures
- sets the current working directory to new_dir
throws
- std::bad_alloc
- set_current_dir_error
This exception is thrown if there is an error when attempting
to change the current working directory.
!*/
// ----------------------------------------------------------------------------------------
class dir_create_error : public error {
......@@ -85,7 +102,7 @@ namespace dlib
/*!
ensures
- returns a timestamp that measures the time in microseconds since an
arbitrary point in the past. Note that this arbitray point remains
arbitrary point in the past. Note that this arbitrary point remains
the same between all calls to get_timestamp().
!*/
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment