Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
24dd65f0
Commit
24dd65f0
authored
Jun 19, 2011
by
Davis King
Browse files
Added a set_current_dir() function.
parent
8cd8cfe3
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
83 additions
and
2 deletions
+83
-2
dlib/misc_api/misc_api_kernel_1.cpp
dlib/misc_api/misc_api_kernel_1.cpp
+24
-0
dlib/misc_api/misc_api_kernel_1.h
dlib/misc_api/misc_api_kernel_1.h
+14
-0
dlib/misc_api/misc_api_kernel_2.cpp
dlib/misc_api/misc_api_kernel_2.cpp
+12
-0
dlib/misc_api/misc_api_kernel_2.h
dlib/misc_api/misc_api_kernel_2.h
+14
-0
dlib/misc_api/misc_api_kernel_abstract.h
dlib/misc_api/misc_api_kernel_abstract.h
+19
-2
No files found.
dlib/misc_api/misc_api_kernel_1.cpp
View file @
24dd65f0
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
#define DLIB_MISC_API_KERNEL_1_CPp_
#define DLIB_MISC_API_KERNEL_1_CPp_
#include "../platform.h"
#include "../platform.h"
#include "../threads.h"
#ifdef WIN32
#ifdef WIN32
...
@@ -30,9 +31,17 @@ namespace dlib
...
@@ -30,9 +31,17 @@ namespace dlib
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
namespace
{
mutex
cwd_mutex
;
}
std
::
string
get_current_dir
(
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
];
char
buf
[
1024
];
if
(
GetCurrentDirectoryA
(
sizeof
(
buf
),
buf
)
==
0
)
if
(
GetCurrentDirectoryA
(
sizeof
(
buf
),
buf
)
==
0
)
{
{
...
@@ -44,6 +53,21 @@ namespace dlib
...
@@ -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
::
uint64
timestamper
::
...
...
dlib/misc_api/misc_api_kernel_1.h
View file @
24dd65f0
...
@@ -27,6 +27,20 @@ namespace dlib
...
@@ -27,6 +27,20 @@ namespace dlib
std
::
string
get_current_dir
(
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
class
timestamper
...
...
dlib/misc_api/misc_api_kernel_2.cpp
View file @
24dd65f0
...
@@ -55,6 +55,18 @@ namespace dlib
...
@@ -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
::
uint64
timestamper
::
...
...
dlib/misc_api/misc_api_kernel_2.h
View file @
24dd65f0
...
@@ -27,6 +27,20 @@ namespace dlib
...
@@ -27,6 +27,20 @@ namespace dlib
std
::
string
get_current_dir
(
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
class
timestamper
...
...
dlib/misc_api/misc_api_kernel_abstract.h
View file @
24dd65f0
...
@@ -34,7 +34,7 @@ namespace dlib
...
@@ -34,7 +34,7 @@ namespace dlib
);
);
/*!
/*!
ensures
ensures
- if (no errors occur
r
) then
- if (no errors occur) then
- returns the path to the current working directory
- returns the path to the current working directory
- else
- else
- returns ""
- returns ""
...
@@ -42,6 +42,23 @@ namespace dlib
...
@@ -42,6 +42,23 @@ namespace dlib
- std::bad_alloc
- 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
{
class
dir_create_error
:
public
error
{
...
@@ -85,7 +102,7 @@ namespace dlib
...
@@ -85,7 +102,7 @@ namespace dlib
/*!
/*!
ensures
ensures
- returns a timestamp that measures the time in microseconds since an
- 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 arbitra
r
y point remains
the same between all calls to get_timestamp().
the same between all calls to get_timestamp().
!*/
!*/
};
};
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment