fixed cstring;
moved some wchar_t mappers back to fileutil.cpp
This commit is contained in:
Родитель
b0aef3dd2f
Коммит
f3cc47bc20
|
@ -78,6 +78,7 @@ OACR_WARNING_DISABLE(POTENTIAL_ARGUMENT_TYPE_MISMATCH, "Not level1 or level2_sec
|
|||
#include <map>
|
||||
#include <stdexcept>
|
||||
#include <locale> // std::wstring_convert
|
||||
#include <string>
|
||||
#ifdef _MSC_VER
|
||||
#include <codecvt> // 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<typename C> struct basic_cstring : std::basic_string<C>
|
||||
template<typename C> struct basic_cstring : public std::basic_string<C>
|
||||
{
|
||||
template<typename S> basic_cstring (S p) : basic_string (p) { }
|
||||
operator const char * () const { return c_str(); }
|
||||
template<typename S> basic_cstring (S p) : std::basic_string<C> (p) { }
|
||||
operator const C * () const { return this->c_str(); }
|
||||
};
|
||||
typedef basic_cstring<char> cstring;
|
||||
typedef basic_cstring<wchar_t> 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<typename FUNCTION> 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 */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<wstring> & 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());
|
||||
|
|
Загрузка…
Ссылка в новой задаче