mdelibs/core/file.c

98 lines
1.7 KiB
C
Raw Normal View History

2025-11-22 02:29:49 +09:00
#include <MDE/Core/File.h>
2025-11-23 17:23:35 +09:00
#include <MDE/Core/String.h>
#include <MDE/Core/Directory.h>
2025-11-16 11:24:56 +09:00
2025-11-25 11:08:13 +09:00
#ifdef _WIN32
#include <windows.h>
#endif
2025-11-17 18:20:56 +09:00
void MDEFileCopy(const char* src, const char* dst) {
char buffer[4096];
2025-11-16 11:24:56 +09:00
FILE* in;
FILE* out;
2025-11-17 18:20:56 +09:00
int r;
2025-11-16 11:24:56 +09:00
if((in = fopen(src, "rb")) == NULL) return;
2025-11-17 18:20:56 +09:00
if((out = fopen(dst, "wb")) == NULL) {
2025-11-16 11:24:56 +09:00
fclose(in);
return;
}
2025-11-17 18:20:56 +09:00
do {
2025-11-16 11:24:56 +09:00
r = fread(buffer, 1, sizeof(buffer), in);
fwrite(buffer, 1, r, out);
2025-11-17 18:20:56 +09:00
} while(r == sizeof(buffer));
2025-11-16 11:24:56 +09:00
fclose(in);
fclose(out);
}
2025-11-23 17:23:35 +09:00
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) {
2025-11-25 11:08:13 +09:00
#ifdef _WIN32
char* r = malloc(MAX_PATH);
GetFullPathName(path, MAX_PATH, r, NULL);
return r;
#else
2025-11-23 17:23:35 +09:00
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;
2025-11-25 11:08:13 +09:00
#endif
2025-11-23 17:23:35 +09:00
}