From 8a4fa58cd42b7cca4a86fe2d9913b839b554bf10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?eray=20or=C3=A7unus?= Date: Mon, 11 May 2020 05:55:57 +0300 Subject: Linux build support --- src/skel/crossplatform.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 120 insertions(+), 5 deletions(-) (limited to 'src/skel/crossplatform.cpp') diff --git a/src/skel/crossplatform.cpp b/src/skel/crossplatform.cpp index f9464bb6..9971d2ae 100644 --- a/src/skel/crossplatform.cpp +++ b/src/skel/crossplatform.cpp @@ -1,5 +1,4 @@ #include "common.h" -#define USEALTERNATIVEWINFUNCS #include "crossplatform.h" // For internal use @@ -20,7 +19,8 @@ void GetLocalTime_CP(SYSTEMTIME *out) { tmToSystemTime(localTm, out); } -#if !defined _WIN32 || defined __MINGW32__ +// Compatible with Linux/POSIX and MinGW on Windows +#ifndef _WIN32 HANDLE FindFirstFile(const char* pathname, WIN32_FIND_DATA* firstfile) { char newpathname[32]; strncpy(newpathname, pathname, 32); @@ -34,7 +34,7 @@ HANDLE FindFirstFile(const char* pathname, WIN32_FIND_DATA* firstfile) { strncpy(firstfile->extension, "", sizeof(firstfile->extension)); HANDLE d; - if ((d = opendir(path)) == NULL || !FindNextFile(d, firstfile)) + if ((d = (HANDLE)opendir(path)) == NULL || !FindNextFile(d, firstfile)) return NULL; return d; @@ -45,7 +45,7 @@ bool FindNextFile(HANDLE d, WIN32_FIND_DATA* finddata) { static struct stat fileStats; static char path[PATH_MAX], relativepath[NAME_MAX + sizeof(finddata->folder) + 1]; int extensionLen = strlen(finddata->extension); - while ((file = readdir(d)) != NULL) { + while ((file = readdir((DIR*)d)) != NULL) { // We only want "DT_REG"ular Files, but reportedly some FS and OSes gives DT_UNKNOWN as type. if ((file->d_type == DT_UNKNOWN || file->d_type == DT_REG) && @@ -78,4 +78,119 @@ void FileTimeToSystemTime(time_t* writeTime, SYSTEMTIME* out) { tm *ptm = gmtime(writeTime); tmToSystemTime(ptm, out); } -#endif \ No newline at end of file +#endif + +#ifndef _WIN32 +char *strupr(char *s) { + char* tmp = s; + + for (;*tmp;++tmp) { + *tmp = toupper((unsigned char) *tmp); + } + + return s; +} +char *strlwr(char *s) { + char* tmp = s; + + for (;*tmp;++tmp) { + *tmp = tolower((unsigned char) *tmp); + } + + return s; +} + +char *trim(char *s) { + char *ptr; + if (!s) + return NULL; // handle NULL string + if (!*s) + return s; // handle empty string + for (ptr = s + strlen(s) - 1; (ptr >= s) && isspace(*ptr); --ptr); + ptr[1] = '\0'; + return s; +} + +// Case-insensitivity on linux (from https://github.com/OneSadCookie/fcaseopen) +// r must have strlen(path) + 2 bytes +int casepath(char const *path, char *r) +{ + size_t l = strlen(path); + char *p = (char*)alloca(l + 1); + strcpy(p, path); + + // my addon: change \'s with / + char *nextBs; + while(nextBs = strstr(p, "\\")){ + *nextBs = '/'; + } + + // my addon: linux doesn't handle filenames with spaces at the end nicely + p = trim(p); + + size_t rl = 0; + + DIR *d; + if (p[0] == '/') + { + d = opendir("/"); + p = p + 1; + } + else + { + d = opendir("."); + r[0] = '.'; + r[1] = 0; + rl = 1; + } + + int last = 0; + char *c = strsep(&p, "/"); + while (c) + { + if (!d) + { + return 0; + } + + if (last) + { + closedir(d); + return 0; + } + + r[rl] = '/'; + rl += 1; + r[rl] = 0; + + struct dirent *e = readdir(d); + while (e) + { + if (strcasecmp(c, e->d_name) == 0) + { + strcpy(r + rl, e->d_name); + rl += strlen(e->d_name); + + closedir(d); + d = opendir(r); + + break; + } + + e = readdir(d); + } + + if (!e) + { + strcpy(r + rl, c); + rl += strlen(c); + last = 1; + } + + c = strsep(&p, "/"); + } + + if (d) closedir(d); + return 1; +} +#endif -- cgit v1.2.3 From 36e2bc95d385c79075495d96e066d697727449d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?eray=20or=C3=A7unus?= Date: Mon, 11 May 2020 20:10:01 +0300 Subject: Fix Windows build and premake --- src/skel/crossplatform.cpp | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/skel/crossplatform.cpp') diff --git a/src/skel/crossplatform.cpp b/src/skel/crossplatform.cpp index 9971d2ae..40f4f053 100644 --- a/src/skel/crossplatform.cpp +++ b/src/skel/crossplatform.cpp @@ -1,6 +1,9 @@ #include "common.h" #include "crossplatform.h" +// Codes compatible with Windows and Linux +#ifndef _WIN32 + // For internal use // wMilliseconds is not needed void tmToSystemTime(const tm *tm, SYSTEMTIME *out) { @@ -18,6 +21,7 @@ void GetLocalTime_CP(SYSTEMTIME *out) { tm *localTm = localtime(×tamp); tmToSystemTime(localTm, out); } +#endif // Compatible with Linux/POSIX and MinGW on Windows #ifndef _WIN32 @@ -80,6 +84,7 @@ void FileTimeToSystemTime(time_t* writeTime, SYSTEMTIME* out) { } #endif +// Funcs/features from Windows that we need on other platforms #ifndef _WIN32 char *strupr(char *s) { char* tmp = s; -- cgit v1.2.3