Skip to content

Commit 4a6e56a

Browse files
committed
Common: refactor convertation UNIX <-> WINDOWS file pathes
1 parent 6f4e19b commit 4a6e56a

File tree

8 files changed

+32
-26
lines changed

8 files changed

+32
-26
lines changed

src/Common/FSMacros.hpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
#pragma once
22

3-
//#if defined(LINUX)
4-
//#define _DELIMITER '/' //for looking
5-
//#define DELIMITER "/" // for insert
6-
//#elif defined(WINDOWS)
73
#define _DELIMITER '\\' //for looking
84
#define DELIMITER "\\" // for insert
9-
//#endif
105

116
// game path definition
127
#define _game_data_ "$game_data$"

src/Common/PlatformLinux.inl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,16 @@ typedef void *HIC;
10001000

10011001
inline BOOL SwitchToThread() { return (0 == pthread_yield()); }
10021002

1003+
inline void convert_path_separators(char * path)
1004+
{
1005+
while (char* sep = strchr(path, '\\')) *sep = '/';
1006+
}
1007+
1008+
/** For backward compability of FS, for real filesystem delimiter set to back
1009+
* @brief restore_path_separators
1010+
* @param path
1011+
*/
1012+
inline void restore_path_separators(char * path)
1013+
{
1014+
while (char* sep = strchr(path, '/')) *sep = '\\'; //
1015+
}

src/Common/PlatformWindows.inl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,6 @@
3232

3333
#include <windows.h>
3434
#include <windowsx.h>
35+
36+
inline void convert_path_separators(char * path) {}
37+
inline void restore_path_separators(char * path) {}

src/xrCore/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ list(APPEND DIRS
1212
"Text"
1313
"Threading"
1414
"XML"
15+
"../xrCommon"
1516
)
1617

1718
add_dir("${DIRS}")

src/xrCore/FS.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ void VerifyPath(LPCSTR path)
8888
tmp[i] = 0;
8989
_mkdir(tmp);
9090
}
91-
#ifdef LINUX
92-
while (char* sep = strchr((char *)path, '\\')) *sep = '/';
93-
#endif
91+
92+
convert_path_separators((char *)path);
9493
}
9594

9695
#ifdef _EDITOR
@@ -111,10 +110,10 @@ bool file_handle_internal(LPCSTR file_name, u32& size, int& hFile)
111110
#else // EDITOR
112111
static int open_internal(LPCSTR fn, int& handle)
113112
{
113+
convert_path_separators((char *)fn);
114114
#if defined(WINDOWS)
115115
return (_sopen_s(&handle, fn, _O_RDONLY | _O_BINARY, _SH_DENYNO, _S_IREAD));
116116
#elif defined(LINUX)
117-
while (char* sep = strchr((char *)fn, '\\')) *sep = '/';
118117
handle = open(fn, _O_RDONLY);
119118

120119
return (handle == -1);
@@ -511,6 +510,7 @@ CCompressedReader::CCompressedReader(const char* name, const char* sign)
511510
CCompressedReader::~CCompressedReader() { xr_free(data); };
512511
CVirtualFileRW::CVirtualFileRW(const char* cFileName)
513512
{
513+
convert_path_separators((char *)cFileName);
514514
#if defined(WINDOWS)
515515
// Open the file
516516
hSrcFile = CreateFile(cFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
@@ -524,7 +524,6 @@ CVirtualFileRW::CVirtualFileRW(const char* cFileName)
524524
data = (char*)MapViewOfFile(hSrcMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
525525
R_ASSERT3(data, cFileName, xrDebug::ErrorToString(GetLastError()));
526526
#elif defined(LINUX)
527-
while (char* sep = strchr((char *)cFileName, '\\')) *sep = '/';
528527
hSrcFile = ::open(cFileName, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); //за такое использование указателя нужно убивать, но пока пусть будет
529528
R_ASSERT3(hSrcFile != -1, cFileName, xrDebug::ErrorToString(GetLastError()));
530529
struct stat file_info;
@@ -556,6 +555,7 @@ CVirtualFileRW::~CVirtualFileRW()
556555

557556
CVirtualFileReader::CVirtualFileReader(const char* cFileName)
558557
{
558+
convert_path_separators((char *)cFileName);
559559
#if defined(WINDOWS)
560560
// Open the file
561561
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)
568568

569569
data = (char*)MapViewOfFile(hSrcMap, FILE_MAP_READ, 0, 0, 0);
570570
#elif defined(LINUX)
571-
while (char* sep = strchr((char *)cFileName, '\\')) *sep = '/';
572571
hSrcFile = ::open(cFileName, O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); //за такое использование указателя нужно убивать, но пока пусть будет
573572
R_ASSERT3(hSrcFile != -1, cFileName, xrDebug::ErrorToString(GetLastError()));
574573
struct stat file_info;

src/xrCore/LocatorAPI.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,7 @@ const CLocatorAPI::file* CLocatorAPI::Register(
196196
string256 temp_file_name;
197197
xr_strcpy(temp_file_name, sizeof temp_file_name, name);
198198

199-
#ifdef LINUX
200-
while (char* sep = strchr(temp_file_name, '/')) *sep = '\\'; // For backward compability of FS, for real filesystem delimiter set to back
201-
#endif
199+
restore_path_separators(temp_file_name);
202200
//Msg("Register[%d] [%s]",vfs,temp_file_name);
203201

204202
// Register file
@@ -444,6 +442,7 @@ void CLocatorAPI::LoadArchive(archive& A, pcstr entrypoint)
444442

445443
void CLocatorAPI::archive::open()
446444
{
445+
convert_path_separators((char *)*path);
447446
#if defined(WINDOWS)
448447
// Open the file
449448
if (hSrcFile && hSrcMap)
@@ -459,7 +458,6 @@ void CLocatorAPI::archive::open()
459458
if (hSrcFile)
460459
return;
461460

462-
while (char* sep = strchr((char *)*path, '\\')) *sep = '/';
463461
hSrcFile = ::open(*path, O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
464462
R_ASSERT(hSrcFile != -1);
465463
struct stat file_info;
@@ -620,6 +618,7 @@ bool ignore_name(const char* _name)
620618

621619
bool ignore_path(const char* _path)
622620
{
621+
convert_path_separators((char *)_path);
623622
#if defined(WINDOWS)
624623
HANDLE h = CreateFile(_path, 0, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY | FILE_FLAG_NO_BUFFERING, nullptr);
625624

@@ -631,7 +630,6 @@ bool ignore_path(const char* _path)
631630
else
632631
return true;
633632
#elif defined(LINUX)
634-
while (char* sep = strchr((char *)_path, '\\')) *sep = '/';
635633
int h = ::open(_path, O_RDONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
636634
if (h != -1)
637635
{
@@ -654,16 +652,14 @@ bool CLocatorAPI::Recurse(pcstr path)
654652
xr_strcpy(scanPath, sizeof scanPath, path);
655653
xr_strcat(scanPath, "*");
656654
_finddata_t findData;
655+
convert_path_separators(scanPath);
657656
#ifdef WINDOWS
658657
intptr_t handle = _findfirst(scanPath, &findData);
659658
if (handle == -1)
660659
return false;
661660
#elif defined(LINUX)
662661
glob_t globbuf;
663662

664-
while (char* sep = strchr(scanPath, '\\'))
665-
*sep = '/';
666-
667663
globbuf.gl_offs = 256;
668664
int result = glob(scanPath, GLOB_NOSORT, NULL, &globbuf);
669665

@@ -689,7 +685,7 @@ bool CLocatorAPI::Recurse(pcstr path)
689685
case S_IFREG: findData.attrib = 0; break; // File
690686
default: findData.attrib = _A_HIDDEN; break; // Skip
691687
}
692-
while (char* sep = strchr(findData.name, '/')) *sep = '\\';
688+
restore_path_separators(findData.name);
693689
#endif
694690
string1024 fullPath;
695691
bool ignore = false;
@@ -720,8 +716,9 @@ bool CLocatorAPI::Recurse(pcstr path)
720716
_findclose(handle);
721717
#elif defined(LINUX)
722718
globfree(&globbuf);
723-
while (char* sep = strchr((char *)path, '/')) *sep = '\\';
724719
#endif
720+
restore_path_separators((char *)path);
721+
725722
size_t newSize = rec_files.size();
726723
if (newSize > oldSize)
727724
{

src/xrCore/file_stream_reader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void CFileStreamReader::construct(LPCSTR file_name, const u32& window_size)
1919
#elif defined(LINUX)
2020
char path[PATH_MAX] = { 0 };
2121
strcpy(path, file_name);
22-
while (char* sep = strchr(path, '\\')) *sep = '/';
22+
convert_path_separators(path);
2323
m_file_handle = ::open(path, O_RDONLY);
2424
VERIFY(m_file_handle != -1);
2525
struct stat file_info;

src/xrCore/xr_ini.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,8 @@ bool CInifile::save_as(pcstr new_fname)
634634
if (new_fname && new_fname[0])
635635
xr_strcpy(m_file_name, sizeof m_file_name, new_fname);
636636

637-
R_ASSERT(m_file_name && m_file_name[0]);
638-
#ifdef LINUX
639-
while (char* sep = strchr(m_file_name, '\\')) *sep = '/';
640-
#endif
637+
R_ASSERT(m_file_name[0]);
638+
convert_path_separators(m_file_name);
641639
IWriter* F = FS.w_open_ex(m_file_name);
642640
if (!F)
643641
return false;

0 commit comments

Comments
 (0)