mirror of
https://github.com/Karaka-Management/cOMS.git
synced 2026-02-09 23:28:41 +00:00
update os wrapper
This commit is contained in:
parent
7db531e62f
commit
b8a4b5ab8a
|
|
@ -14,27 +14,19 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#include "OSWrapper.h"
|
||||||
#include <direct.h>
|
|
||||||
#else
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
namespace ApplicationUtils {
|
namespace ApplicationUtils {
|
||||||
inline
|
inline
|
||||||
char *cwd()
|
char *cwd()
|
||||||
{
|
{
|
||||||
char *cwd = (char *) malloc(sizeof(char) * 4096);
|
char *cwd = (char *) malloc(4096 * sizeof(char));
|
||||||
if (cwd == NULL) {
|
if (cwd == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
getcwd(cwd, 4096 * sizeof(char));
|
||||||
cwd = _getcwd(NULL, sizeof(char) * 4096);
|
|
||||||
#else
|
|
||||||
getcwd(cwd, sizeof(char) * 4096);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return cwd;
|
return cwd;
|
||||||
}
|
}
|
||||||
|
|
@ -42,43 +34,23 @@ namespace Utils {
|
||||||
inline
|
inline
|
||||||
void chdir_application(char *cwd, char *arg)
|
void chdir_application(char *cwd, char *arg)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
char *pos = strrchr(arg, '/');
|
||||||
char *pos = strrchr(arg, '/');
|
if (pos == NULL) {
|
||||||
if (pos == NULL) {
|
pos = strrchr(arg, '\\');
|
||||||
pos = strrchr(arg, '\\');
|
}
|
||||||
}
|
|
||||||
|
|
||||||
char *dir = (char *) calloc((pos - arg + 1), sizeof(char));
|
char* dir = (char *) calloc((pos - arg + 1), sizeof(char));
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pos != NULL) {
|
if (pos != NULL) {
|
||||||
memcpy(dir, arg, (pos - arg) * sizeof(char));
|
memcpy(dir, arg, (pos - arg) * sizeof(char));
|
||||||
|
|
||||||
_chdir(dir);
|
chdir(dir);
|
||||||
|
|
||||||
free(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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline
|
inline
|
||||||
|
|
|
||||||
|
|
@ -11,36 +11,25 @@
|
||||||
#define UTILS_FILE_UTILS_H
|
#define UTILS_FILE_UTILS_H
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
|
||||||
#include <io.h>
|
|
||||||
#else
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
|
||||||
#else
|
#else
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "OSWrapper.h"
|
||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
namespace FileUtils {
|
namespace FileUtils {
|
||||||
inline
|
inline
|
||||||
bool file_exists (const char *filename)
|
bool file_exists (const char *filename)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#ifdef _MSC_VER
|
return access(filename, 0) == 0;
|
||||||
return _access_s(filename, 0) == 0;
|
|
||||||
#else
|
|
||||||
return access(filename, 0) == 0;
|
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
struct stat buffer;
|
struct stat buffer;
|
||||||
return stat(filename, &buffer) == 0;
|
return stat(filename, &buffer) == 0;
|
||||||
|
|
@ -123,7 +112,7 @@ namespace Utils {
|
||||||
file.size = ftell(fp);
|
file.size = ftell(fp);
|
||||||
fseek(fp, 0, SEEK_SET);
|
fseek(fp, 0, SEEK_SET);
|
||||||
|
|
||||||
file.content = (char *) malloc(file.size);
|
file.content = (char *) malloc((file.size + 1) * sizeof(char));
|
||||||
if (!file.content) {
|
if (!file.content) {
|
||||||
fprintf(stderr, "CRITICAL: malloc failed");
|
fprintf(stderr, "CRITICAL: malloc failed");
|
||||||
|
|
||||||
|
|
@ -131,6 +120,7 @@ namespace Utils {
|
||||||
}
|
}
|
||||||
|
|
||||||
fread(file.content, file.size, 1, fp);
|
fread(file.content, file.size, 1, fp);
|
||||||
|
file.content[file.size] = 0;
|
||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
|
|
|
||||||
32
Utils/OSWrapper.h
Normal file
32
Utils/OSWrapper.h
Normal 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
|
||||||
|
|
@ -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
|
|
||||||
Loading…
Reference in New Issue
Block a user