From 90953b85d1351a765b31603066cb1ef48eafd25f Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sun, 23 Nov 2025 17:23:35 +0900 Subject: [PATCH] add more methods --- core/directory.c | 25 ++++++++++++++ core/file.c | 65 ++++++++++++++++++++++++++++++++++++ core/string.c | 8 +++++ include/MDE/Core/Directory.h | 4 +++ include/MDE/Core/File.h | 4 +++ include/MDE/Core/String.h | 2 ++ 6 files changed, 108 insertions(+) diff --git a/core/directory.c b/core/directory.c index 523839c..d7db29e 100644 --- a/core/directory.c +++ b/core/directory.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -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); +} diff --git a/core/file.c b/core/file.c index d5cced7..d8d4c92 100644 --- a/core/file.c +++ b/core/file.c @@ -1,4 +1,6 @@ #include +#include +#include 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; +} diff --git a/core/string.c b/core/string.c index 3f429ea..29bef70 100644 --- a/core/string.c +++ b/core/string.c @@ -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; +} diff --git a/include/MDE/Core/Directory.h b/include/MDE/Core/Directory.h index 19d76cf..fc56b49 100644 --- a/include/MDE/Core/Directory.h +++ b/include/MDE/Core/Directory.h @@ -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 diff --git a/include/MDE/Core/File.h b/include/MDE/Core/File.h index bf663ac..f521df1 100644 --- a/include/MDE/Core/File.h +++ b/include/MDE/Core/File.h @@ -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 diff --git a/include/MDE/Core/String.h b/include/MDE/Core/String.h index 83bb78d..5b31ad0 100644 --- a/include/MDE/Core/String.h +++ b/include/MDE/Core/String.h @@ -9,6 +9,8 @@ extern "C" { char* MDEStringDuplicate(const char* src); +char* MDEStringConcatenate(const char* str1, const char* str2); + #ifdef __cplusplus } #endif