diff options
Diffstat (limited to 'ml/dlib/dlib/misc_api')
-rw-r--r-- | ml/dlib/dlib/misc_api/misc_api_kernel_1.cpp | 149 | ||||
-rw-r--r-- | ml/dlib/dlib/misc_api/misc_api_kernel_1.h | 110 | ||||
-rw-r--r-- | ml/dlib/dlib/misc_api/misc_api_kernel_2.cpp | 123 | ||||
-rw-r--r-- | ml/dlib/dlib/misc_api/misc_api_kernel_2.h | 81 | ||||
-rw-r--r-- | ml/dlib/dlib/misc_api/misc_api_kernel_abstract.h | 159 | ||||
-rw-r--r-- | ml/dlib/dlib/misc_api/misc_api_shared.h | 57 | ||||
-rw-r--r-- | ml/dlib/dlib/misc_api/posix.h | 6 | ||||
-rw-r--r-- | ml/dlib/dlib/misc_api/windows.h | 6 |
8 files changed, 691 insertions, 0 deletions
diff --git a/ml/dlib/dlib/misc_api/misc_api_kernel_1.cpp b/ml/dlib/dlib/misc_api/misc_api_kernel_1.cpp new file mode 100644 index 000000000..f17d850e1 --- /dev/null +++ b/ml/dlib/dlib/misc_api/misc_api_kernel_1.cpp @@ -0,0 +1,149 @@ +// Copyright (C) 2004 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_KERNEL_1_CPp_ +#define DLIB_MISC_API_KERNEL_1_CPp_ + +#include "../platform.h" +#include "../threads.h" + +#ifdef WIN32 + +#include "misc_api_kernel_1.h" + +#include "../windows_magic.h" +#include <mmsystem.h> +#include <windows.h> + +// tell visual studio to link to the library needed to call timeGetTime() +#ifdef _MSC_VER +#pragma comment (lib, "winmm.lib") +#endif + +#ifdef __BORLANDC__ +// Apparently the borland compiler doesn't define this. +#define INVALID_FILE_ATTRIBUTES ((DWORD)-1) +#endif + +namespace dlib +{ +// ---------------------------------------------------------------------------------------- + + void sleep ( + unsigned long milliseconds + ) + { + ::Sleep(milliseconds); + } + +// ---------------------------------------------------------------------------------------- + + namespace + { + mutex& cwd_mutex() + { + static mutex m; + return m; + } + // Make sure the above mutex gets constructed before main() + // starts. This way we can be pretty sure it will be constructed + // before any threads could possibly call set_current_dir() or + // get_current_dir() simultaneously. + struct construct_cwd_mutex + { + construct_cwd_mutex() + { + cwd_mutex(); + } + } oaimvweoinvwe; + } + + 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) + { + return std::string(); + } + else + { + return std::string(buf); + } + } + +// ---------------------------------------------------------------------------------------- + + 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 (SetCurrentDirectoryA(new_dir.c_str()) == 0) + { + throw set_current_dir_error("Error changing current dir to '" + new_dir + "'"); + } + } + +// ---------------------------------------------------------------------------------------- + + uint64 timestamper:: + get_timestamp ( + ) const + { + unsigned long temp = timeGetTime(); + if (temp >= last_time) + { + last_time = temp; + return (offset + temp)*1000; + } + else + { + last_time = temp; + + // there was overflow since the last call so we need to make the offset + // bigger to account for that + offset += dword_max; + return (offset + temp)*1000; + } + } + +// ---------------------------------------------------------------------------------------- + + void create_directory ( + const std::string& dir + ) + { + if (CreateDirectoryA(dir.c_str(),0) == 0) + { + // an error has occurred + if (GetLastError() == ERROR_ALREADY_EXISTS) + { + // make sure this is actually a directory + DWORD attribs = GetFileAttributesA(dir.c_str()); + if (attribs == INVALID_FILE_ATTRIBUTES || + (attribs&FILE_ATTRIBUTE_DIRECTORY) == 0) + { + // it isn't a directory + throw dir_create_error(dir); + } + } + else + { + throw dir_create_error(dir); + } + } + } + +// ---------------------------------------------------------------------------------------- + +} + +#endif // WIN32 + +#endif // DLIB_MISC_API_KERNEL_1_CPp_ + diff --git a/ml/dlib/dlib/misc_api/misc_api_kernel_1.h b/ml/dlib/dlib/misc_api/misc_api_kernel_1.h new file mode 100644 index 000000000..a500e992a --- /dev/null +++ b/ml/dlib/dlib/misc_api/misc_api_kernel_1.h @@ -0,0 +1,110 @@ +// Copyright (C) 2004 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_KERNEl_1_ +#define DLIB_MISC_API_KERNEl_1_ + +#ifdef DLIB_ISO_CPP_ONLY +#error "DLIB_ISO_CPP_ONLY is defined so you can't use this OS dependent code. Turn DLIB_ISO_CPP_ONLY off if you want to use it." +#endif + + +#include "misc_api_kernel_abstract.h" +#include "../algs.h" +#include <string> +#include "../uintn.h" + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + void sleep ( + unsigned long milliseconds + ); + +// ---------------------------------------------------------------------------------------- + + 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 + { + /*! + INITIAL VALUE + - last_time == 0 + - offset == 0 + - dword_max == 2^32 + + CONVENTION + - last_time == the time returned by GetTickCount() the last time we + called it. + - offset == the number of microseconds we should add to the result of + GetTickCount() so that it is correct. + - dword_max == 2^32. + This is the number of values representable by a DWORD. + !*/ + + mutable unsigned long last_time; + mutable uint64 offset; + mutable uint64 dword_max; + + public: + timestamper( + ) : + last_time(0), + offset(0) + { + dword_max = 0xFFFFFFFF; + ++dword_max; + } + + uint64 get_timestamp ( + ) const; + }; + +// ---------------------------------------------------------------------------------------- + + class dir_create_error : public error + { + public: + dir_create_error( + const std::string& dir_name + ) : + error(EDIR_CREATE,"Error creating directory '" + dir_name + "'."), + name(dir_name) + {} + + ~dir_create_error() throw() {} + const std::string name; + }; + + void create_directory ( + const std::string& dir + ); + +// ---------------------------------------------------------------------------------------- + +} + +#ifdef NO_MAKEFILE +#include "misc_api_kernel_1.cpp" +#endif + +#endif // DLIB_MISC_API_KERNEl_1_ + diff --git a/ml/dlib/dlib/misc_api/misc_api_kernel_2.cpp b/ml/dlib/dlib/misc_api/misc_api_kernel_2.cpp new file mode 100644 index 000000000..e6dc772da --- /dev/null +++ b/ml/dlib/dlib/misc_api/misc_api_kernel_2.cpp @@ -0,0 +1,123 @@ +// Copyright (C) 2004 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_KERNEL_2_CPp_ +#define DLIB_MISC_API_KERNEL_2_CPp_ +#include "../platform.h" + +#ifdef POSIX + +#include <unistd.h> +#include "misc_api_kernel_2.h" +#include <sys/time.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <errno.h> + +namespace dlib +{ +// ---------------------------------------------------------------------------------------- + + void sleep ( + unsigned long milliseconds + ) + { + // in HP-UX you can only usleep for less than a second +#ifdef HPUX + if (milliseconds >= 1000) + { + ::sleep(milliseconds/1000); + unsigned long remaining = milliseconds%1000; + if (remaining > 0) + ::usleep(remaining*1000); + } + else + { + ::usleep(milliseconds*1000); + } +#else + ::usleep(milliseconds*1000); +#endif + } + +// ---------------------------------------------------------------------------------------- + + std::string get_current_dir ( + ) + { + char buf[1024]; + if (getcwd(buf,sizeof(buf)) == 0) + { + return std::string(); + } + else + { + return std::string(buf); + } + } + +// ---------------------------------------------------------------------------------------- + + 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:: + get_timestamp ( + ) const + { + uint64 ts; + timeval curtime; + gettimeofday(&curtime,0); + + ts = curtime.tv_sec; + ts *= 1000000; + ts += curtime.tv_usec; + return ts; + } + +// ---------------------------------------------------------------------------------------- + + void create_directory ( + const std::string& dir + ) + { + if (mkdir(dir.c_str(),0777)) + { + // an error has occurred + if (errno == EEXIST) + { + struct stat buffer; + // now check that this is actually a valid directory + if (::stat(dir.c_str(),&buffer)) + { + // the directory was not found + throw dir_create_error(dir); + } + else if (S_ISDIR(buffer.st_mode) == 0) + { + // It is not a directory + throw dir_create_error(dir); + } + } + else + { + throw dir_create_error(dir); + } + } + } + +// ---------------------------------------------------------------------------------------- +} + +#endif // POSIX + +#endif // DLIB_MISC_API_KERNEL_2_CPp_ + diff --git a/ml/dlib/dlib/misc_api/misc_api_kernel_2.h b/ml/dlib/dlib/misc_api/misc_api_kernel_2.h new file mode 100644 index 000000000..86e8a7f5b --- /dev/null +++ b/ml/dlib/dlib/misc_api/misc_api_kernel_2.h @@ -0,0 +1,81 @@ +// Copyright (C) 2004 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_KERNEl_2_ +#define DLIB_MISC_API_KERNEl_2_ + +#ifdef DLIB_ISO_CPP_ONLY +#error "DLIB_ISO_CPP_ONLY is defined so you can't use this OS dependent code. Turn DLIB_ISO_CPP_ONLY off if you want to use it." +#endif + + +#include "misc_api_kernel_abstract.h" +#include "../algs.h" +#include <string> +#include "../uintn.h" + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + void sleep ( + unsigned long milliseconds + ); + +// ---------------------------------------------------------------------------------------- + + 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 + { + public: + uint64 get_timestamp ( + ) const; + }; + +// ---------------------------------------------------------------------------------------- + + class dir_create_error : public error + { + public: + dir_create_error( + const std::string& dir_name + ) : + error(EDIR_CREATE,"Error creating directory '" + dir_name + "'."), + name(dir_name) + {} + const std::string& name; + }; + + + void create_directory ( + const std::string& dir + ); + +// ---------------------------------------------------------------------------------------- + +} + +#ifdef NO_MAKEFILE +#include "misc_api_kernel_2.cpp" +#endif + +#endif // DLIB_MISC_API_KERNEl_2_ + diff --git a/ml/dlib/dlib/misc_api/misc_api_kernel_abstract.h b/ml/dlib/dlib/misc_api/misc_api_kernel_abstract.h new file mode 100644 index 000000000..47749b91b --- /dev/null +++ b/ml/dlib/dlib/misc_api/misc_api_kernel_abstract.h @@ -0,0 +1,159 @@ +// Copyright (C) 2004 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#undef DLIB_MISC_API_KERNEl_ABSTRACT_ +#ifdef DLIB_MISC_API_KERNEl_ABSTRACT_ + +#include <string> +#include "../uintn.h" +#include "../algs.h" + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + /*! + GENERAL COMMENTS + This file just contains miscellaneous api functions + !*/ + +// ---------------------------------------------------------------------------------------- + + void sleep ( + unsigned long milliseconds + ); + /*! + ensures + - causes the calling thread to sleep for the given number of + milliseconds. + !*/ + +// ---------------------------------------------------------------------------------------- + + std::string get_current_dir ( + ); + /*! + ensures + - if (no errors occur) then + - returns the path to the current working directory + - else + - returns "" + throws + - 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 locally_change_current_dir : noncopyable + { + /*! + WHAT THIS OBJECT REPRESENTS + This object is a RAII tool for safely switching the current directory + to a new directory and then automatically switching back to the original + directory upon this object's destruction. + !*/ + public: + explicit locally_change_current_dir ( + const std::string& new_dir + ); + /*! + ensures + - calls set_current_dir(new_dir) + - #old_dir() == The value of get_current_dir() prior to switching to new_dir. + !*/ + + const std::string& old_dir ( + ) const; + /*! + ensures + - returns the directory we switch back to once this object is destructed. + !*/ + + ~locally_change_current_dir( + ); + /*! + ensures + - if (revert() hasn't already been called) then + - calls set_current_dir(old_dir()) + !*/ + + void revert ( + ); + /*! + ensures + - if (revert() hasn't already been called) then + - calls set_current_dir(old_dir()) + !*/ + }; + +// ---------------------------------------------------------------------------------------- + + class dir_create_error : public error { + public: + const std::string name + }; + + void create_directory ( + const std::string& dir + ); + /*! + ensures + - if (dir does not already exist) then + - creates the given directory. + - else + - the call to create_directory() has no effect. + throws + - dir_create_error + This exception is thrown if we were unable to create the requested + directory and it didn't already exist. The type member of the exception + will bet set to EDIR_CREATE and the name member will be set to dir. + !*/ + +// ---------------------------------------------------------------------------------------- + + class timestamper + { + /*! + WHAT THIS OBJECT REPRESENTS + This object represents a timer that is capable of returning + timestamps. + + Note that the time is measured in microseconds but you are not + guaranteed to have that level of resolution. The actual resolution + is implementation dependent. + !*/ + + public: + uint64 get_timestamp ( + ) const; + /*! + ensures + - returns a timestamp that measures the time in microseconds since an + arbitrary point in the past. Note that this arbitrary point remains + the same between all calls to get_timestamp(). + !*/ + }; + +// ---------------------------------------------------------------------------------------- + +} + +#endif // DLIB_MISC_API_KERNEl_ABSTRACT_ + diff --git a/ml/dlib/dlib/misc_api/misc_api_shared.h b/ml/dlib/dlib/misc_api/misc_api_shared.h new file mode 100644 index 000000000..6b84dd64c --- /dev/null +++ b/ml/dlib/dlib/misc_api/misc_api_shared.h @@ -0,0 +1,57 @@ +// Copyright (C) 2014 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_ShARED_Hh_ +#define DLIB_MISC_API_ShARED_Hh_ + +#include <string> +#include "../noncopyable.h" + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + class locally_change_current_dir : noncopyable + { + public: + explicit locally_change_current_dir ( + const std::string& new_dir + ) + { + reverted = false; + _old_dir = get_current_dir(); + set_current_dir(new_dir); + } + + ~locally_change_current_dir() + { + revert(); + } + + const std::string& old_dir ( + ) const + { + return _old_dir; + } + + void revert ( + ) + { + if (!reverted) + { + set_current_dir(_old_dir); + reverted = true; + } + } + + private: + bool reverted; + std::string _old_dir; + }; + +// ---------------------------------------------------------------------------------------- + +} + +#endif // DLIB_MISC_API_ShARED_Hh_ + diff --git a/ml/dlib/dlib/misc_api/posix.h b/ml/dlib/dlib/misc_api/posix.h new file mode 100644 index 000000000..1dbb38031 --- /dev/null +++ b/ml/dlib/dlib/misc_api/posix.h @@ -0,0 +1,6 @@ +// Copyright (C) 2004 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_KERNEl_1_ +#include "misc_api_kernel_2.h" +#endif + diff --git a/ml/dlib/dlib/misc_api/windows.h b/ml/dlib/dlib/misc_api/windows.h new file mode 100644 index 000000000..0817c2aaf --- /dev/null +++ b/ml/dlib/dlib/misc_api/windows.h @@ -0,0 +1,6 @@ +// Copyright (C) 2004 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_MISC_API_KERNEl_2_ +#include "misc_api_kernel_1.h" +#endif + |