diff --git a/Utils/ApplicationUtils.h b/Utils/ApplicationUtils.h index e558915..a364645 100755 --- a/Utils/ApplicationUtils.h +++ b/Utils/ApplicationUtils.h @@ -14,27 +14,19 @@ #include #include -#ifdef _WIN32 - #include -#else - #include -#endif +#include "OSWrapper.h" namespace Utils { namespace ApplicationUtils { inline char *cwd() { - char *cwd = (char *) malloc(sizeof(char) * 4096); + char *cwd = (char *) malloc(4096 * sizeof(char)); if (cwd == NULL) { return NULL; } - #ifdef _WIN32 - cwd = _getcwd(NULL, sizeof(char) * 4096); - #else - getcwd(cwd, sizeof(char) * 4096); - #endif + getcwd(cwd, 4096 * sizeof(char)); return cwd; } @@ -42,43 +34,23 @@ namespace Utils { inline void chdir_application(char *cwd, char *arg) { - #ifdef _WIN32 - char *pos = strrchr(arg, '/'); - if (pos == NULL) { - pos = strrchr(arg, '\\'); - } + char *pos = strrchr(arg, '/'); + if (pos == NULL) { + pos = strrchr(arg, '\\'); + } - char *dir = (char *) calloc((pos - arg + 1), sizeof(char)); - if (!dir) { - return; - } + char* dir = (char *) calloc((pos - arg + 1), sizeof(char)); + if (!dir) { + return; + } - if (pos != NULL) { - memcpy(dir, arg, (pos - arg) * sizeof(char)); + if (pos != NULL) { + memcpy(dir, arg, (pos - arg) * sizeof(char)); - _chdir(dir); + chdir(dir); - free(dir); - } - #else - char *pos = strrchr(arg, '/'); - if (pos == NULL) { - pos = strrchr(arg, '\\'); - } - - char* dir = (char*) calloc((pos - arg + 1), sizeof(char)); - if (!dir) { - return; - } - - if (pos != NULL) { - memcpy(dir, arg, (pos - arg) * sizeof(char)); - - chdir(dir); - - free(dir); - } - #endif + free(dir); + } } inline diff --git a/Utils/FileUtils.h b/Utils/FileUtils.h index ad7632c..5f268eb 100755 --- a/Utils/FileUtils.h +++ b/Utils/FileUtils.h @@ -11,36 +11,25 @@ #define UTILS_FILE_UTILS_H #include +#include #include #ifdef _WIN32 #include - #include - #include #include - - #ifdef _MSC_VER - #include - #else - #include - #endif - - #include #else #include #endif +#include "OSWrapper.h" + namespace Utils { namespace FileUtils { inline bool file_exists (const char *filename) { #ifdef _WIN32 - #ifdef _MSC_VER - return _access_s(filename, 0) == 0; - #else - return access(filename, 0) == 0; - #endif + return access(filename, 0) == 0; #else struct stat buffer; return stat(filename, &buffer) == 0; @@ -123,7 +112,7 @@ namespace Utils { file.size = ftell(fp); fseek(fp, 0, SEEK_SET); - file.content = (char *) malloc(file.size); + file.content = (char *) malloc((file.size + 1) * sizeof(char)); if (!file.content) { fprintf(stderr, "CRITICAL: malloc failed"); @@ -131,6 +120,7 @@ namespace Utils { } fread(file.content, file.size, 1, fp); + file.content[file.size] = 0; fclose(fp); diff --git a/Utils/OSWrapper.h b/Utils/OSWrapper.h new file mode 100644 index 0000000..f13a845 --- /dev/null +++ b/Utils/OSWrapper.h @@ -0,0 +1,32 @@ +/** + * Karaka + * + * @package Utils + * @copyright Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link https://karaka.app + */ +#ifndef UTILS_OS_WRAPPER_H +#define UTILS_OS_WRAPPER_H + +#include +#include + +#ifdef _WIN32 + #include + #include + + typedef _chdir chdir; + typedef _getcwd getcwd; + + #ifdef _MSC_VER + typedef _access_s access; + #else + #include + #endif +#else + #include +#endif + +#endif diff --git a/Utils/Portability.h b/Utils/Portability.h deleted file mode 100755 index b057226..0000000 --- a/Utils/Portability.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Karaka - * - * @package Utils - * @copyright Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 - * @link https://karaka.app - */ -#ifndef UTILS_PORTABILITY_H -#define UTILS_PORTABILITY_H - -#ifdef __unix - #define fopen_s(pFile, filename, mode) ((*(pFile)) = fopen((filename), (mode))) == NULL -#endif - -#endif \ No newline at end of file