From f3cc47bc20a5dae34ace35e984f40b372eb73250 Mon Sep 17 00:00:00 2001 From: Frank Seide Date: Fri, 31 Oct 2014 11:58:24 -0700 Subject: [PATCH] fixed cstring; moved some wchar_t mappers back to fileutil.cpp --- Common/Include/basetypes.h | 14 ++++++-------- Common/fileutil.cpp | 21 ++++++++++----------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/Common/Include/basetypes.h b/Common/Include/basetypes.h index 9506999e1..ef75fcad7 100644 --- a/Common/Include/basetypes.h +++ b/Common/Include/basetypes.h @@ -78,6 +78,7 @@ OACR_WARNING_DISABLE(POTENTIAL_ARGUMENT_TYPE_MISMATCH, "Not level1 or level2_sec #include #include #include // std::wstring_convert +#include #ifdef _MSC_VER #include // std::codecvt_utf8 #endif @@ -112,10 +113,10 @@ using namespace std; namespace msra { namespace strfun { // a class that can return a std::string with auto-convert into a const char* - template struct basic_cstring : std::basic_string + template struct basic_cstring : public std::basic_string { - template basic_cstring (S p) : basic_string (p) { } - operator const char * () const { return c_str(); } + template basic_cstring (S p) : std::basic_string (p) { } + operator const C * () const { return this->c_str(); } }; typedef basic_cstring cstring; typedef basic_cstring wcstring; @@ -145,6 +146,8 @@ msra::strfun::cstring charpath (const std::wstring & p) static inline FILE* _wfopen(const wchar_t * path, const wchar_t * mode) { return fopen(charpath(path), charpath(mode)); } // --- basic string functions static inline wchar_t* wcstok_s(wchar_t* s, const wchar_t* delim, wchar_t** ptr) { return ::wcstok(s, delim, ptr); } +// -- other +static inline void Sleep (size_t ms) { std::this_thread::sleep_for (std::chrono::milliseconds (ms)); } #endif // ---------------------------------------------------------------------------- @@ -926,12 +929,7 @@ template static void attempt (int retries, const FUNCTION & b if (attempt >= retries) throw; // failed N times --give up and rethrow the error fprintf (stderr, "attempt: %s, retrying %d-th time out of %d...\n", e.what(), attempt+1, retries); -#ifndef LINUX ::Sleep (1000); // wait a little, then try again -#else - std::chrono::milliseconds dura(1000); - std::this_thread::sleep_for(dura); -#endif /* LINUX */ } } } diff --git a/Common/fileutil.cpp b/Common/fileutil.cpp index d012c3838..d8a15f631 100644 --- a/Common/fileutil.cpp +++ b/Common/fileutil.cpp @@ -44,6 +44,15 @@ using namespace std; +// ---------------------------------------------------------------------------- +// some mappings for non-Windows builds +// ---------------------------------------------------------------------------- + +#ifndef _MSC_VER // add some functions that are VS-only +static int _wunlink (const wchar_t * p) { return unlink (charpath (p)); } +static int _wmkdir (const wchar_t * p) { return mkdir (charpath (p), 0777/*correct?*/); } +#endif + template <> const wchar_t* GetScanFormatString(char) {return L" %hc";} template <> const wchar_t* GetScanFormatString(wchar_t) {return L" %lc";} template <> const wchar_t* GetScanFormatString(short) {return L" %hi";} @@ -372,9 +381,6 @@ void unlinkOrDie (const std::string & pathname) if (unlink (pathname.c_str()) != 0 && errno != ENOENT) // if file is missing that's what we want RuntimeError ("error deleting file '%s': %s", pathname.c_str(), strerror (errno)); } -#ifndef _MSC_VER -static int _wunlink (const wchar_t * p) { return unlink (msra::strfun::wcstombs (p).c_str()); } -#endif void unlinkOrDie (const std::wstring & pathname) { if (_wunlink (pathname.c_str()) != 0 && errno != ENOENT) // if file is missing that's what we want @@ -1401,7 +1407,7 @@ bool getfiletime (const wstring & path, FILETIME & time) int result; // Get data associated with "crt_stat.c": - result = stat(msra::strfun::wcstombs(path).c_str(), &buf); + result = stat(charpath(path), &buf); // Check if statistics are valid: if (result != 0) return false; @@ -1512,13 +1518,6 @@ void expand_wildcards (const wstring & path, vector & paths) // make_intermediate_dirs() -- make all intermediate dirs on a path // ---------------------------------------------------------------------------- -#ifndef _MSC_VER // _wmkdir() is VS proprietary -static int _wmkdir (const wchar_t * p) -{ - return mkdir (msra::strfun::wcstombs (p).c_str(), 0777/*correct?*/); -} -#endif - static void mkdir (const wstring & path) { int rc = _wmkdir (path.c_str());