#include "platform_compat.h" #include #ifdef _WIN32 #include #endif #ifdef _WIN32 #include #include #include #include #else #include #include #include #endif #ifdef _WIN32 #include #else #include #endif #include namespace fallout { int compat_stricmp(const char* string1, const char* string2) { return SDL_strcasecmp(string1, string2); } int compat_strnicmp(const char* string1, const char* string2, size_t size) { return SDL_strncasecmp(string1, string2, size); } char* compat_strupr(char* string) { return SDL_strupr(string); } char* compat_strlwr(char* string) { return SDL_strlwr(string); } 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) { #ifdef _WIN32 _splitpath(path, drive, dir, fname, ext); #else // Initialize outputs to empty strings if (drive) drive[0] = '\0'; if (dir) dir[0] = '\0'; if (fname) fname[0] = '\0'; if (ext) ext[0] = '\0'; const char* lastSlash = strrchr(path, '/'); const char* lastBackSlash = strrchr(path, '\\'); if (lastBackSlash && (!lastSlash || lastBackSlash > lastSlash)) { lastSlash = lastBackSlash; } const char* lastDot = strrchr((lastSlash ? lastSlash : path), '.'); // Handle drive letter for Windows-style paths on non-Windows platforms if (path[1] == ':') { if (drive) { drive[0] = path[0]; drive[1] = ':'; drive[2] = '\0'; } path += 2; } // Directory component if (lastSlash) { size_t length = lastSlash - path + 1; // Include slash if (dir) { strncpy(dir, path, length); dir[length] = '\0'; } path = lastSlash + 1; // Move past the slash or backslash } // Filename and extension if (lastDot && (lastDot > lastSlash)) { size_t length = lastDot - path; if (fname) { strncpy(fname, path, length); fname[length] = '\0'; } if (ext) { strcpy(ext, lastDot); } } else { if (fname) { strcpy(fname, path); } } #endif } void compat_makepath(char* path, const char* drive, const char* dir, const char* fname, const char* ext) { #ifdef _WIN32 _makepath(path, drive, dir, fname, ext); #else path[0] = '\0'; if (drive != nullptr) { if (*drive != '\0') { strcpy(path, drive); path = strchr(path, '\0'); if (path[-1] == '/') { path--; } else { *path = '/'; } } } if (dir != nullptr) { if (*dir != '\0') { if (*dir != '/' && *path == '/') { path++; } strcpy(path, dir); path = strchr(path, '\0'); if (path[-1] == '/') { path--; } else { *path = '/'; } } } if (fname != nullptr && *fname != '\0') { if (*fname != '/' && *path == '/') { path++; } strcpy(path, fname); path = strchr(path, '\0'); } else { if (*path == '/') { path++; } } if (ext != nullptr) { if (*ext != '\0') { if (*ext != '.') { *path++ = '.'; } strcpy(path, ext); path = strchr(path, '\0'); } } *path = '\0'; #endif } long compat_tell(int fd) { return lseek(fd, 0, SEEK_CUR); } long compat_filelength(int fd) { long originalOffset = lseek(fd, 0, SEEK_CUR); lseek(fd, 0, SEEK_SET); long filesize = lseek(fd, 0, SEEK_END); lseek(fd, originalOffset, SEEK_SET); return filesize; } int compat_mkdir(const char* path) { char nativePath[COMPAT_MAX_PATH]; strcpy(nativePath, path); compat_windows_path_to_native(nativePath); compat_resolve_path(nativePath); #ifdef _WIN32 return mkdir(nativePath); #else return mkdir(nativePath, 0755); #endif } unsigned int compat_timeGetTime() { #ifdef _WIN32 return timeGetTime(); #else static auto start = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now(); return static_cast(std::chrono::duration_cast(now - start).count()); #endif } FILE* compat_fopen(const char* path, const char* mode) { char nativePath[COMPAT_MAX_PATH]; strcpy(nativePath, path); compat_windows_path_to_native(nativePath); compat_resolve_path(nativePath); return fopen(nativePath, mode); } gzFile compat_gzopen(const char* path, const char* mode) { char nativePath[COMPAT_MAX_PATH]; strcpy(nativePath, path); compat_windows_path_to_native(nativePath); compat_resolve_path(nativePath); return gzopen(nativePath, mode); } char* compat_fgets(char* buffer, int maxCount, FILE* stream) { buffer = fgets(buffer, maxCount, stream); if (buffer != nullptr) { size_t len = strlen(buffer); if (len >= 2 && buffer[len - 1] == '\n' && buffer[len - 2] == '\r') { buffer[len - 2] = '\n'; buffer[len - 1] = '\0'; } } return buffer; } char* compat_gzgets(gzFile stream, char* buffer, int maxCount) { buffer = gzgets(stream, buffer, maxCount); if (buffer != nullptr) { size_t len = strlen(buffer); if (len >= 2 && buffer[len - 1] == '\n' && buffer[len - 2] == '\r') { buffer[len - 2] = '\n'; buffer[len - 1] = '\0'; } } return buffer; } int compat_remove(const char* path) { char nativePath[COMPAT_MAX_PATH]; strcpy(nativePath, path); compat_windows_path_to_native(nativePath); compat_resolve_path(nativePath); return remove(nativePath); } int compat_rename(const char* oldFileName, const char* newFileName) { char nativeOldFileName[COMPAT_MAX_PATH]; strcpy(nativeOldFileName, oldFileName); compat_windows_path_to_native(nativeOldFileName); compat_resolve_path(nativeOldFileName); char nativeNewFileName[COMPAT_MAX_PATH]; strcpy(nativeNewFileName, newFileName); compat_windows_path_to_native(nativeNewFileName); compat_resolve_path(nativeNewFileName); return rename(nativeOldFileName, nativeNewFileName); } void compat_windows_path_to_native(char* path) { #ifndef _WIN32 char* pch = path; while (*pch != '\0') { if (*pch == '\\') { *pch = '/'; } pch++; } #endif } void compat_resolve_path(char* path) { #ifndef _WIN32 char* pch = path; DIR* dir; if (pch[0] == '/') { dir = opendir("/"); pch++; } else { dir = opendir("."); } while (dir != nullptr) { char* sep = strchr(pch, '/'); size_t length; if (sep != nullptr) { length = sep - pch; } else { length = strlen(pch); } bool found = false; struct dirent* entry = readdir(dir); while (entry != nullptr) { if (strlen(entry->d_name) == length && compat_strnicmp(pch, entry->d_name, length) == 0) { strncpy(pch, entry->d_name, length); found = true; break; } entry = readdir(dir); } closedir(dir); dir = nullptr; if (!found) { break; } if (sep == nullptr) { break; } *sep = '\0'; dir = opendir(path); *sep = '/'; pch = sep + 1; } #endif } int compat_access(const char* path, int mode) { char nativePath[COMPAT_MAX_PATH]; strcpy(nativePath, path); compat_windows_path_to_native(nativePath); compat_resolve_path(nativePath); return access(nativePath, mode); } char* compat_strdup(const char* string) { return SDL_strdup(string); } // It's a replacement for compat_filelength(fileno(stream)) on platforms without // fileno defined. long getFileSize(FILE* stream) { long originalOffset = ftell(stream); fseek(stream, 0, SEEK_END); long filesize = ftell(stream); fseek(stream, originalOffset, SEEK_SET); return filesize; } } // namespace fallout