Skip to content

Commit

Permalink
Common: refactor convertation UNIX <-> WINDOWS file pathes
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleivg committed Nov 8, 2018
1 parent 6f4e19b commit 4a6e56a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 26 deletions.
5 changes: 0 additions & 5 deletions src/Common/FSMacros.hpp
Original file line number Diff line number Diff line change
@@ -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$"
Expand Down
13 changes: 13 additions & 0 deletions src/Common/PlatformLinux.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '\\'; //
}
3 changes: 3 additions & 0 deletions src/Common/PlatformWindows.inl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@

#include <windows.h>
#include <windowsx.h>

inline void convert_path_separators(char * path) {}
inline void restore_path_separators(char * path) {}
1 change: 1 addition & 0 deletions src/xrCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ list(APPEND DIRS
"Text"
"Threading"
"XML"
"../xrCommon"
)

add_dir("${DIRS}")
Expand Down
11 changes: 5 additions & 6 deletions src/xrCore/FS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down
17 changes: 7 additions & 10 deletions src/xrCore/LocatorAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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)
{
Expand All @@ -654,16 +652,14 @@ 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)
return false;
#elif defined(LINUX)
glob_t globbuf;

while (char* sep = strchr(scanPath, '\\'))
*sep = '/';

globbuf.gl_offs = 256;
int result = glob(scanPath, GLOB_NOSORT, NULL, &globbuf);

Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/xrCore/file_stream_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/xrCore/xr_ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4a6e56a

Please sign in to comment.