2022-05-28 01:57:32 -07:00
|
|
|
#include "platform_compat.h"
|
|
|
|
|
|
|
|
#include <SDL.h>
|
2022-05-28 13:17:48 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <filesystem>
|
2022-05-28 01:57:32 -07:00
|
|
|
|
2022-05-29 08:15:48 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#define NOMINMAX
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-28 14:22:40 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2022-05-29 08:15:48 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <timeapi.h>
|
|
|
|
#else
|
2022-06-02 11:11:23 -07:00
|
|
|
#include <chrono>
|
2022-05-29 08:15:48 -07:00
|
|
|
#endif
|
|
|
|
|
2022-05-28 01:57:32 -07:00
|
|
|
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);
|
|
|
|
}
|
2022-05-28 13:17:48 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-05-28 14:22:40 -07:00
|
|
|
|
2022-05-29 14:40:09 -07:00
|
|
|
int compat_read(int fileHandle, void* buf, unsigned int size)
|
|
|
|
{
|
|
|
|
return read(fileHandle, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int compat_write(int fileHandle, const void* buf, unsigned int size)
|
|
|
|
{
|
|
|
|
return write(fileHandle, buf, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
long compat_lseek(int fileHandle, long offset, int origin)
|
|
|
|
{
|
|
|
|
return lseek(fileHandle, offset, origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
long compat_tell(int fd)
|
|
|
|
{
|
|
|
|
return lseek(fd, 0, SEEK_CUR);
|
|
|
|
}
|
|
|
|
|
2022-05-28 14:22:40 -07:00
|
|
|
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;
|
|
|
|
}
|
2022-05-29 08:08:55 -07:00
|
|
|
|
|
|
|
int compat_mkdir(const char* path)
|
|
|
|
{
|
2022-06-18 00:58:15 -07:00
|
|
|
char nativePath[COMPAT_MAX_PATH];
|
|
|
|
strcpy(nativePath, path);
|
|
|
|
compat_windows_path_to_native(nativePath);
|
|
|
|
|
2022-05-29 08:08:55 -07:00
|
|
|
std::error_code ec;
|
2022-06-18 00:58:15 -07:00
|
|
|
if (std::filesystem::create_directory(std::filesystem::path(nativePath), ec)) {
|
2022-05-29 08:08:55 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ec.value();
|
|
|
|
}
|
2022-05-29 08:15:48 -07:00
|
|
|
|
|
|
|
unsigned int compat_timeGetTime()
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
return timeGetTime();
|
|
|
|
#else
|
2022-06-02 11:11:23 -07:00
|
|
|
static auto start = std::chrono::steady_clock::now();
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
return static_cast<unsigned int>(std::chrono::duration_cast<std::chrono::milliseconds>(now - start).count());
|
2022-05-29 08:15:48 -07:00
|
|
|
#endif
|
|
|
|
}
|
2022-06-18 00:58:15 -07:00
|
|
|
|
|
|
|
FILE* compat_fopen(const char* path, const char* mode)
|
|
|
|
{
|
|
|
|
char nativePath[COMPAT_MAX_PATH];
|
|
|
|
strcpy(nativePath, path);
|
|
|
|
compat_windows_path_to_native(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);
|
|
|
|
return gzopen(nativePath, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
int compat_remove(const char* path)
|
|
|
|
{
|
|
|
|
char nativePath[COMPAT_MAX_PATH];
|
|
|
|
strcpy(nativePath, path);
|
|
|
|
compat_windows_path_to_native(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);
|
|
|
|
|
|
|
|
char nativeNewFileName[COMPAT_MAX_PATH];
|
|
|
|
strcpy(nativeNewFileName, newFileName);
|
|
|
|
compat_windows_path_to_native(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
|
|
|
|
}
|