Cleaned up unicode code and removed fixed-size buffers

pull/2633/head
Ryan McCampbell 2019-07-09 02:17:45 -07:00
parent 727cf0c49f
commit 6c65b5b549
1 changed files with 40 additions and 34 deletions

View File

@ -61,12 +61,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using namespace Assimp; using namespace Assimp;
// maximum path length #ifdef _WIN32
// XXX http://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html static std::wstring Utf8ToWide(const char* in)
#ifdef PATH_MAX {
# define PATHLIMIT PATH_MAX int size = MultiByteToWideChar(CP_UTF8, 0, in, -1, nullptr, 0);
#else std::wstring out(size, L'\0');
# define PATHLIMIT 4096 MultiByteToWideChar(CP_UTF8, 0, in, -1, &out[0], size);
return out;
}
static std::string WideToUtf8(const wchar_t* in)
{
int size = WideCharToMultiByte(CP_UTF8, 0, in, -1, nullptr, 0, nullptr, nullptr);
std::string out(size, '\0');
WideCharToMultiByte(CP_UTF8, 0, in, -1, &out[0], size, nullptr, nullptr);
return out;
}
#endif #endif
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -74,11 +84,8 @@ using namespace Assimp;
bool DefaultIOSystem::Exists( const char* pFile) const bool DefaultIOSystem::Exists( const char* pFile) const
{ {
#if defined(_WIN32) && !defined(WindowsStore) #if defined(_WIN32) && !defined(WindowsStore)
wchar_t fileName16[PATHLIMIT];
MultiByteToWideChar(CP_UTF8, 0, pFile, -1, fileName16, PATHLIMIT);
struct __stat64 filestat; struct __stat64 filestat;
if (0 != _wstat64(fileName16, &filestat)) { if (0 != _wstat64(Utf8ToWide(pFile).c_str(), &filestat)) {
return false; return false;
} }
#else #else
@ -99,11 +106,7 @@ IOStream* DefaultIOSystem::Open( const char* strFile, const char* strMode)
ai_assert(NULL != strMode); ai_assert(NULL != strMode);
FILE* file; FILE* file;
#if defined(_WIN32) && !defined(WindowsStore) #if defined(_WIN32) && !defined(WindowsStore)
wchar_t fileName16[PATHLIMIT]; file = ::_wfopen(Utf8ToWide(strFile).c_str(), Utf8ToWide(strMode).c_str());
MultiByteToWideChar(CP_UTF8, 0, strFile, -1, fileName16, PATHLIMIT);
std::string mode8(strMode);
std::wstring mode16(mode8.begin(), mode8.end());
file = ::_wfopen(fileName16, mode16.c_str());
#else #else
file = ::fopen(strFile, strMode); file = ::fopen(strFile, strMode);
#endif #endif
@ -140,31 +143,39 @@ bool IOSystem::ComparePaths (const char* one, const char* second) const
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Convert a relative path into an absolute path // Convert a relative path into an absolute path
inline static void MakeAbsolutePath (const char* in, char* _out) inline static std::string MakeAbsolutePath (const char* in)
{ {
ai_assert(in && _out); ai_assert(in);
#if defined( _MSC_VER ) || defined( __MINGW32__ ) std::string out;
#ifdef _WIN32
#ifndef WindowsStore #ifndef WindowsStore
wchar_t in16[PATHLIMIT]; wchar_t* ret = ::_wfullpath(nullptr, Utf8ToWide(in).c_str(), 0);
wchar_t out16[PATHLIMIT];
MultiByteToWideChar(CP_UTF8, 0, in, -1, in16, PATHLIMIT);
wchar_t* ret = ::_wfullpath(out16, in16, PATHLIMIT);
if (ret) { if (ret) {
WideCharToMultiByte(CP_UTF8, 0, out16, -1, _out, PATHLIMIT, nullptr, nullptr); out = WideToUtf8(ret);
free(ret);
} }
#else #else
char* ret = ::_fullpath(_out, in, PATHLIMIT); char* ret = ::_fullpath(nullptr, in, 0);
if (ret) {
out = ret;
free(ret);
}
#endif #endif
#else #else
// use realpath // use realpath
char* ret = realpath(in, _out); char* ret = realpath(in, nullptr);
if (ret) {
out = ret;
free(ret);
}
#endif #endif
if(!ret) { if (!ret) {
// preserve the input path, maybe someone else is able to fix // preserve the input path, maybe someone else is able to fix
// the path before it is accessed (e.g. our file system filter) // the path before it is accessed (e.g. our file system filter)
ASSIMP_LOG_WARN_F("Invalid path: ", std::string(in)); ASSIMP_LOG_WARN_F("Invalid path: ", std::string(in));
strcpy(_out,in); out = in;
} }
return out;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -176,11 +187,8 @@ bool DefaultIOSystem::ComparePaths (const char* one, const char* second) const
if( !ASSIMP_stricmp(one,second) ) if( !ASSIMP_stricmp(one,second) )
return true; return true;
char temp1[PATHLIMIT]; std::string temp1 = MakeAbsolutePath(one);
char temp2[PATHLIMIT]; std::string temp2 = MakeAbsolutePath(second);
MakeAbsolutePath (one, temp1);
MakeAbsolutePath (second, temp2);
return !ASSIMP_stricmp(temp1,temp2); return !ASSIMP_stricmp(temp1,temp2);
} }
@ -213,5 +221,3 @@ std::string DefaultIOSystem::absolutePath( const std::string &path )
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#undef PATHLIMIT