From 4a6e56a694ba6478d4b52d3feb9b542363e6cd79 Mon Sep 17 00:00:00 2001 From: Ilya Orlov Date: Fri, 2 Nov 2018 14:22:34 +0300 Subject: [PATCH] Common: refactor convertation UNIX <-> WINDOWS file pathes --- src/Common/FSMacros.hpp | 5 ----- src/Common/PlatformLinux.inl | 13 +++++++++++++ src/Common/PlatformWindows.inl | 3 +++ src/xrCore/CMakeLists.txt | 1 + src/xrCore/FS.cpp | 11 +++++------ src/xrCore/LocatorAPI.cpp | 17 +++++++---------- src/xrCore/file_stream_reader.cpp | 2 +- src/xrCore/xr_ini.cpp | 6 ++---- 8 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/Common/FSMacros.hpp b/src/Common/FSMacros.hpp index a560ced7e37..4aecb23accd 100644 --- a/src/Common/FSMacros.hpp +++ b/src/Common/FSMacros.hpp @@ -1,12 +1,7 @@ #pragma once -//#if defined(LINUX) -//#define _DELIMITER '/' //for looking -//#define DELIMITER "/" // for insert -//#elif defined(WINDOWS) #define _DELIMITER '\\' //for looking #define DELIMITER "\\" // for insert -//#endif // game path definition #define _game_data_ "$game_data$" diff --git a/src/Common/PlatformLinux.inl b/src/Common/PlatformLinux.inl index df93646acb9..c435671a11a 100644 --- a/src/Common/PlatformLinux.inl +++ b/src/Common/PlatformLinux.inl @@ -1000,3 +1000,16 @@ typedef void *HIC; inline BOOL SwitchToThread() { return (0 == pthread_yield()); } +inline void convert_path_separators(char * path) +{ + while (char* sep = strchr(path, '\\')) *sep = '/'; +} + +/** For backward compability of FS, for real filesystem delimiter set to back + * @brief restore_path_separators + * @param path + */ +inline void restore_path_separators(char * path) +{ + while (char* sep = strchr(path, '/')) *sep = '\\'; // +} diff --git a/src/Common/PlatformWindows.inl b/src/Common/PlatformWindows.inl index 06dc2f3888e..8b03a969ba0 100644 --- a/src/Common/PlatformWindows.inl +++ b/src/Common/PlatformWindows.inl @@ -32,3 +32,6 @@ #include #include + +inline void convert_path_separators(char * path) {} +inline void restore_path_separators(char * path) {} diff --git a/src/xrCore/CMakeLists.txt b/src/xrCore/CMakeLists.txt index 7c5ddf978c1..277131dcd0d 100644 --- a/src/xrCore/CMakeLists.txt +++ b/src/xrCore/CMakeLists.txt @@ -12,6 +12,7 @@ list(APPEND DIRS "Text" "Threading" "XML" + "../xrCommon" ) add_dir("${DIRS}") diff --git a/src/xrCore/FS.cpp b/src/xrCore/FS.cpp index 7a7f090c4db..e175a60b77f 100644 --- a/src/xrCore/FS.cpp +++ b/src/xrCore/FS.cpp @@ -88,9 +88,8 @@ void VerifyPath(LPCSTR path) tmp[i] = 0; _mkdir(tmp); } -#ifdef LINUX - while (char* sep = strchr((char *)path, '\\')) *sep = '/'; -#endif + + convert_path_separators((char *)path); } #ifdef _EDITOR @@ -111,10 +110,10 @@ bool file_handle_internal(LPCSTR file_name, u32& size, int& hFile) #else // EDITOR static int open_internal(LPCSTR fn, int& handle) { + convert_path_separators((char *)fn); #if defined(WINDOWS) return (_sopen_s(&handle, fn, _O_RDONLY | _O_BINARY, _SH_DENYNO, _S_IREAD)); #elif defined(LINUX) - while (char* sep = strchr((char *)fn, '\\')) *sep = '/'; handle = open(fn, _O_RDONLY); return (handle == -1); @@ -511,6 +510,7 @@ CCompressedReader::CCompressedReader(const char* name, const char* sign) CCompressedReader::~CCompressedReader() { xr_free(data); }; CVirtualFileRW::CVirtualFileRW(const char* cFileName) { + convert_path_separators((char *)cFileName); #if defined(WINDOWS) // Open the file hSrcFile = CreateFile(cFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); @@ -524,7 +524,6 @@ CVirtualFileRW::CVirtualFileRW(const char* cFileName) data = (char*)MapViewOfFile(hSrcMap, FILE_MAP_ALL_ACCESS, 0, 0, 0); R_ASSERT3(data, cFileName, xrDebug::ErrorToString(GetLastError())); #elif defined(LINUX) - while (char* sep = strchr((char *)cFileName, '\\')) *sep = '/'; hSrcFile = ::open(cFileName, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); //за такое использование указателя нужно убивать, но пока пусть будет R_ASSERT3(hSrcFile != -1, cFileName, xrDebug::ErrorToString(GetLastError())); struct stat file_info; @@ -556,6 +555,7 @@ CVirtualFileRW::~CVirtualFileRW() CVirtualFileReader::CVirtualFileReader(const char* cFileName) { + convert_path_separators((char *)cFileName); #if defined(WINDOWS) // Open the file hSrcFile = CreateFile(cFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); @@ -568,7 +568,6 @@ CVirtualFileReader::CVirtualFileReader(const char* cFileName) data = (char*)MapViewOfFile(hSrcMap, FILE_MAP_READ, 0, 0, 0); #elif defined(LINUX) - while (char* sep = strchr((char *)cFileName, '\\')) *sep = '/'; hSrcFile = ::open(cFileName, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); //за такое использование указателя нужно убивать, но пока пусть будет R_ASSERT3(hSrcFile != -1, cFileName, xrDebug::ErrorToString(GetLastError())); struct stat file_info; diff --git a/src/xrCore/LocatorAPI.cpp b/src/xrCore/LocatorAPI.cpp index 5c6a4b8e5dd..8fffed1f559 100644 --- a/src/xrCore/LocatorAPI.cpp +++ b/src/xrCore/LocatorAPI.cpp @@ -196,9 +196,7 @@ const CLocatorAPI::file* CLocatorAPI::Register( string256 temp_file_name; xr_strcpy(temp_file_name, sizeof temp_file_name, name); -#ifdef LINUX - while (char* sep = strchr(temp_file_name, '/')) *sep = '\\'; // For backward compability of FS, for real filesystem delimiter set to back -#endif + restore_path_separators(temp_file_name); //Msg("Register[%d] [%s]",vfs,temp_file_name); // Register file @@ -444,6 +442,7 @@ void CLocatorAPI::LoadArchive(archive& A, pcstr entrypoint) void CLocatorAPI::archive::open() { + convert_path_separators((char *)*path); #if defined(WINDOWS) // Open the file if (hSrcFile && hSrcMap) @@ -459,7 +458,6 @@ void CLocatorAPI::archive::open() if (hSrcFile) return; - while (char* sep = strchr((char *)*path, '\\')) *sep = '/'; hSrcFile = ::open(*path, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); R_ASSERT(hSrcFile != -1); struct stat file_info; @@ -620,6 +618,7 @@ bool ignore_name(const char* _name) bool ignore_path(const char* _path) { + convert_path_separators((char *)_path); #if defined(WINDOWS) HANDLE h = CreateFile(_path, 0, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_NO_BUFFERING, nullptr); @@ -631,7 +630,6 @@ bool ignore_path(const char* _path) else return true; #elif defined(LINUX) - while (char* sep = strchr((char *)_path, '\\')) *sep = '/'; int h = ::open(_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (h != -1) { @@ -654,6 +652,7 @@ bool CLocatorAPI::Recurse(pcstr path) xr_strcpy(scanPath, sizeof scanPath, path); xr_strcat(scanPath, "*"); _finddata_t findData; + convert_path_separators(scanPath); #ifdef WINDOWS intptr_t handle = _findfirst(scanPath, &findData); if (handle == -1) @@ -661,9 +660,6 @@ bool CLocatorAPI::Recurse(pcstr path) #elif defined(LINUX) glob_t globbuf; - while (char* sep = strchr(scanPath, '\\')) - *sep = '/'; - globbuf.gl_offs = 256; int result = glob(scanPath, GLOB_NOSORT, NULL, &globbuf); @@ -689,7 +685,7 @@ bool CLocatorAPI::Recurse(pcstr path) case S_IFREG: findData.attrib = 0; break; // File default: findData.attrib = _A_HIDDEN; break; // Skip } - while (char* sep = strchr(findData.name, '/')) *sep = '\\'; + restore_path_separators(findData.name); #endif string1024 fullPath; bool ignore = false; @@ -720,8 +716,9 @@ bool CLocatorAPI::Recurse(pcstr path) _findclose(handle); #elif defined(LINUX) globfree(&globbuf); - while (char* sep = strchr((char *)path, '/')) *sep = '\\'; #endif + restore_path_separators((char *)path); + size_t newSize = rec_files.size(); if (newSize > oldSize) { diff --git a/src/xrCore/file_stream_reader.cpp b/src/xrCore/file_stream_reader.cpp index 8bcef4c8b48..53a3422bc9a 100644 --- a/src/xrCore/file_stream_reader.cpp +++ b/src/xrCore/file_stream_reader.cpp @@ -19,7 +19,7 @@ void CFileStreamReader::construct(LPCSTR file_name, const u32& window_size) #elif defined(LINUX) char path[PATH_MAX] = { 0 }; strcpy(path, file_name); - while (char* sep = strchr(path, '\\')) *sep = '/'; + convert_path_separators(path); m_file_handle = ::open(path, O_RDONLY); VERIFY(m_file_handle != -1); struct stat file_info; diff --git a/src/xrCore/xr_ini.cpp b/src/xrCore/xr_ini.cpp index 1f780542c47..02b98d581a9 100644 --- a/src/xrCore/xr_ini.cpp +++ b/src/xrCore/xr_ini.cpp @@ -634,10 +634,8 @@ bool CInifile::save_as(pcstr new_fname) if (new_fname && new_fname[0]) xr_strcpy(m_file_name, sizeof m_file_name, new_fname); - R_ASSERT(m_file_name && m_file_name[0]); -#ifdef LINUX - while (char* sep = strchr(m_file_name, '\\')) *sep = '/'; -#endif + R_ASSERT(m_file_name[0]); + convert_path_separators(m_file_name); IWriter* F = FS.w_open_ex(m_file_name); if (!F) return false;