update os wrapper

This commit is contained in:
Dennis Eichhorn 2022-12-07 20:02:26 +01:00
parent 7db531e62f
commit b8a4b5ab8a
4 changed files with 54 additions and 77 deletions

View File

@ -14,27 +14,19 @@
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <direct.h>
#else
#include <unistd.h>
#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

View File

@ -11,36 +11,25 @@
#define UTILS_FILE_UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#ifdef _MSC_VER
#include <io.h>
#else
#include <unistd.h>
#endif
#include <stdbool.h>
#else
#include <sys/stat.h>
#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);

32
Utils/OSWrapper.h Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <direct.h>
#include <io.h>
typedef _chdir chdir;
typedef _getcwd getcwd;
#ifdef _MSC_VER
typedef _access_s access;
#else
#include <unistd.h>
#endif
#else
#include <unistd.h>
#endif
#endif

View File

@ -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