From 442e0a17ab8bba2fa5be1072c3b317d3ec4fa068 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Sat, 28 May 2022 23:17:48 +0300 Subject: [PATCH] Provide splitpath/makepath compatibility layer See #17 --- src/db.cc | 8 +++--- src/platform_compat.cc | 55 ++++++++++++++++++++++++++++++++++++++++++ src/platform_compat.h | 7 ++++++ src/xfile.cc | 30 +++++++++++------------ 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/db.cc b/src/db.cc index 7964cad..b00d767 100644 --- a/src/db.cc +++ b/src/db.cc @@ -635,10 +635,10 @@ int fileNameListInit(const char* pattern, char*** fileNameListPtr, int a3, int a for (int index = 0; index < fileNamesLength; index += 1) { const char* name = xlist->fileNames[index]; - char dir[_MAX_DIR]; - char fileName[_MAX_FNAME]; - char extension[_MAX_EXT]; - _splitpath(name, NULL, dir, fileName, extension); + char dir[COMPAT_MAX_DIR]; + char fileName[COMPAT_MAX_FNAME]; + char extension[COMPAT_MAX_EXT]; + compat_splitpath(name, NULL, dir, fileName, extension); bool v2 = false; if (v1) { diff --git a/src/platform_compat.cc b/src/platform_compat.cc index 67adaf4..7429292 100644 --- a/src/platform_compat.cc +++ b/src/platform_compat.cc @@ -1,6 +1,9 @@ #include "platform_compat.h" #include +#include + +#include int compat_stricmp(const char* string1, const char* string2) { @@ -26,3 +29,55 @@ char* compat_itoa(int value, char* buffer, int radix) { return SDL_itoa(value, buffer, radix); } + +void compat_splitpath(const char* path, char* drive, char* dir, char* fname, char* ext) +{ +#if defined(_WIN32) + _splitpath(path, drive, dir, fname, ext); +#else + std::filesystem::path p(path); + + if (drive != NULL) { + strcpy(drive, p.root_name().string().substr(0, COMPAT_MAX_DRIVE - 1).c_str()); + } + + if (dir != NULL) { + strcpy(dir, p.parent_path().string().substr(0, COMPAT_MAX_DIR - 1).c_str()); + } + + if (fname != NULL) { + strcpy(fname, p.stem().string().substr(0, COMPAT_MAX_FNAME - 1).c_str()); + } + + if (ext != NULL) { + strcpy(ext, p.extension().string().substr(0, COMPAT_MAX_EXT - 1).c_str()); + } +#endif +} + +void compat_makepath(char* path, const char* drive, const char* dir, const char* fname, const char* ext) +{ +#if defined(_WIN32) + _makepath(path, drive, dir, fname, ext); +#else + std::filesystem::path p; + + if (drive != NULL) { + p.append(drive); + } + + if (dir != NULL) { + p.append(dir); + } + + if (fname != NULL) { + p.append(fname); + } + + if (ext != NULL) { + p.replace_extension(ext); + } + + strcpy(path, p.string().substr(0, COMPAT_MAX_PATH - 1).c_str()); +#endif +} diff --git a/src/platform_compat.h b/src/platform_compat.h index 8154913..a46bfd7 100644 --- a/src/platform_compat.h +++ b/src/platform_compat.h @@ -12,10 +12,17 @@ // represent paths across the codebase. #define COMPAT_MAX_PATH 260 +#define COMPAT_MAX_DRIVE 3 +#define COMPAT_MAX_DIR 256 +#define COMPAT_MAX_FNAME 256 +#define COMPAT_MAX_EXT 256 + int compat_stricmp(const char* string1, const char* string2); int compat_strnicmp(const char* string1, const char* string2, size_t size); char* compat_strupr(char* string); char* compat_strlwr(char* string); char* compat_itoa(int value, char* buffer, int radix); +void compat_splitpath(const char* path, char* drive, char* dir, char* fname, char* ext); +void compat_makepath(char* path, const char* drive, const char* dir, const char* fname, const char* ext); #endif /* PLATFORM_COMPAT_H */ diff --git a/src/xfile.cc b/src/xfile.cc index ed667ae..5524b91 100644 --- a/src/xfile.cc +++ b/src/xfile.cc @@ -55,9 +55,9 @@ XFile* xfileOpen(const char* filePath, const char* mode) memset(stream, 0, sizeof(*stream)); // NOTE: Compiled code uses different lengths. - char drive[_MAX_DRIVE]; - char dir[_MAX_DIR]; - _splitpath(filePath, drive, dir, NULL, NULL); + char drive[COMPAT_MAX_DRIVE]; + char dir[COMPAT_MAX_DIR]; + compat_splitpath(filePath, drive, dir, NULL, NULL); char path[COMPAT_MAX_PATH]; if (drive[0] != '\0' || dir[0] == '\\' || dir[0] == '/' || dir[0] == '.') { @@ -541,11 +541,11 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList context.xlist = xlist; - char drive[_MAX_DRIVE]; - char dir[_MAX_DIR]; - char fileName[_MAX_FNAME]; - char extension[_MAX_EXT]; - _splitpath(pattern, drive, dir, fileName, extension); + char drive[COMPAT_MAX_DRIVE]; + char dir[COMPAT_MAX_DIR]; + char fileName[COMPAT_MAX_FNAME]; + char extension[COMPAT_MAX_EXT]; + compat_splitpath(pattern, drive, dir, fileName, extension); if (drive[0] != '\0' || dir[0] == '\\' || dir[0] == '/' || dir[0] == '.') { if (fileFindFirst(pattern, &directoryFileFindData)) { do { @@ -562,7 +562,7 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList context.type = XFILE_ENUMERATION_ENTRY_TYPE_FILE; } - _makepath(context.name, drive, dir, entryName, NULL); + compat_makepath(context.name, drive, dir, entryName, NULL); if (!handler(&context)) { break; @@ -607,7 +607,7 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList context.type = XFILE_ENUMERATION_ENTRY_TYPE_FILE; } - _makepath(context.name, drive, dir, entryName, NULL); + compat_makepath(context.name, drive, dir, entryName, NULL); if (!handler(&context)) { break; @@ -619,7 +619,7 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList xbase = xbase->next; } - _splitpath(pattern, drive, dir, fileName, extension); + compat_splitpath(pattern, drive, dir, fileName, extension); if (fileFindFirst(pattern, &directoryFileFindData)) { do { bool isDirectory = fileFindIsDirectory(&directoryFileFindData); @@ -635,7 +635,7 @@ bool xlistEnumerate(const char* pattern, XListEnumerationHandler* handler, XList context.type = XFILE_ENUMERATION_ENTRY_TYPE_FILE; } - _makepath(context.name, drive, dir, entryName, NULL); + compat_makepath(context.name, drive, dir, entryName, NULL); if (!handler(&context)) { break; @@ -678,9 +678,9 @@ int xbaseMakeDirectory(const char* filePath) return -1; } - char drive[_MAX_DRIVE]; - char dir[_MAX_DIR]; - _splitpath(filePath, drive, dir, NULL, NULL); + char drive[COMPAT_MAX_DRIVE]; + char dir[COMPAT_MAX_DIR]; + compat_splitpath(filePath, drive, dir, NULL, NULL); char path[COMPAT_MAX_PATH]; if (drive[0] != '\0' || dir[0] == '\\' || dir[0] == '/' || dir[0] == '.') {