add more methods

This commit is contained in:
Nishi 2025-11-23 17:23:35 +09:00
commit 90953b85d1
Signed by: nishi
GPG key ID: 27EF69B208EB9343
6 changed files with 108 additions and 0 deletions

View file

@ -1,4 +1,5 @@
#include <MDE/Core/Directory.h>
#include <MDE/Core/String.h>
#include <dirent.h>
#include <sys/stat.h>
@ -26,3 +27,27 @@ void MDEDirectoryScan(const char* path, void (*call)(const char* name, int dir,
closedir(dir);
}
}
void MDEDirectoryCreate(const char* path, int mode) {
int i;
char* r = malloc(strlen(path) + 1);
r[0] = 0;
for(i = 0;; i++) {
char b[2];
b[0] = path[i];
b[1] = 0;
strcat(r, b);
if(path[i] == '/' || path[i] == 0) {
mkdir(r, mode);
if(path[i] == 0) break;
}
}
free(r);
}
char* MDEDirectoryCurrentPath(void) {
char p[4097]; /* your system is crazy if it allows more than 4096 letters */
getcwd(p, 4096);
return MDEStringDuplicate(p);
}

View file

@ -1,4 +1,6 @@
#include <MDE/Core/File.h>
#include <MDE/Core/String.h>
#include <MDE/Core/Directory.h>
void MDEFileCopy(const char* src, const char* dst) {
char buffer[4096];
@ -19,3 +21,66 @@ void MDEFileCopy(const char* src, const char* dst) {
fclose(in);
fclose(out);
}
char* MDEFileOptimizeAbsolutePath(const char* path) {
char* r = MDEStringDuplicate(path);
char* buf = MDEStringDuplicate(path);
int i;
r[0] = buf[0] = 0;
for(i = 0;; i++) {
if(path[i] == '/' || path[i] == 0) {
if(strcmp(buf, ".") == 0) {
} else if(strcmp(buf, "..") == 0) {
char* p;
if((p = strrchr(r, '/')) != NULL) p[0] = 0;
if((p = strrchr(r, '/')) != NULL) {
p[0] = 0;
strcat(r, "/");
}
if(p == NULL) {
strcat(r, "/");
}
} else {
strcat(r, buf);
if(path[i] == '/') strcat(r, "/");
}
buf[0] = 0;
if(path[i] == 0) break;
} else {
char cbuf[2];
cbuf[0] = path[i];
cbuf[1] = 0;
strcat(buf, cbuf);
}
}
free(buf);
return r;
}
char* MDEFileAbsolutePath(const char* path) {
char* p;
char* r;
if(path[0] == '/') return MDEStringDuplicate(path);
p = MDEDirectoryCurrentPath();
r = malloc(strlen(p) + 1 + strlen(path) + 1);
strcpy(r, p);
if(p[strlen(p) - 1] != '/') strcat(r, "/");
strcat(r, path);
free(p);
p = r;
r = MDEFileOptimizeAbsolutePath(p);
free(p);
return r;
}

View file

@ -7,3 +7,11 @@ char* MDEStringDuplicate(const char* src) {
return s;
}
char* MDEStringConcatenate(const char* str1, const char* str2) {
char* r = malloc(strlen(str1) + strlen(str2) + 1);
strcpy(r, str1);
strcat(r, str2);
return r;
}

View file

@ -9,6 +9,10 @@ extern "C" {
void MDEDirectoryScan(const char* path, void (*call)(const char* name, int dir, int symlink, void* user), void* user);
void MDEDirectoryCreate(const char* path, int mode);
char* MDEDirectoryCurrentPath(void);
#ifdef __cplusplus
}
#endif

View file

@ -9,6 +9,10 @@ extern "C" {
void MDEFileCopy(const char* src, const char* dst);
char* MDEFileAbsolutePath(const char* path);
char* MDEFileOptimizeAbsolutePath(const char* path);
#ifdef __cplusplus
}
#endif

View file

@ -9,6 +9,8 @@ extern "C" {
char* MDEStringDuplicate(const char* src);
char* MDEStringConcatenate(const char* str1, const char* str2);
#ifdef __cplusplus
}
#endif