YAWM-ModMii-Edition/source/fileops.c

77 lines
1.2 KiB
C
Raw Normal View History

#include <stdio.h>
2023-02-27 12:57:14 +01:00
#include <string.h>
#include "fileops.h"
#include "malloc.h"
2024-03-02 00:07:58 +01:00
static struct stat st;
bool FSOPFileExists(const char* file)
{
2024-03-02 00:07:58 +01:00
return !stat(file, &st) && !S_ISDIR(st.st_mode);
}
bool FSOPFolderExists(const char* path)
{
2024-03-02 00:07:58 +01:00
return !stat(path, &st) && S_ISDIR(st.st_mode);
}
size_t FSOPGetFileSizeBytes(const char* path)
{
2024-03-02 00:07:58 +01:00
if (stat(path, &st) < 0) return 0;
2024-03-02 00:07:58 +01:00
return st.st_size;
}
void FSOPDeleteFile(const char* file)
{
2024-03-02 00:07:58 +01:00
if (FSOPFileExists(file))
remove(file);
}
void FSOPMakeFolder(const char* path)
{
if (FSOPFolderExists(path))
return;
2023-02-27 12:57:14 +01:00
char* pos = strchr(path, '/');
s32 current = pos - path;
current++;
pos = strchr(path + current, '/');
while (pos)
{
*pos = 0;
mkdir(path, S_IREAD | S_IWRITE);
*pos = '/';
current = pos - path;
current++;
pos = strchr(path + current, '/');
}
mkdir(path, S_IREAD | S_IWRITE);
}
s32 FSOPReadOpenFile(FILE* fp, void* buffer, u32 offset, u32 length)
{
fseek(fp, offset, SEEK_SET);
return fread(buffer, length, 1, fp);
}
s32 FSOPReadOpenFileA(FILE* fp, void** buffer, u32 offset, u32 length)
{
*buffer = memalign32(length);
if (!*buffer)
return -1;
s32 ret = FSOPReadOpenFile(fp, *buffer, offset, length);
2024-03-02 00:07:58 +01:00
if (ret <= 0)
{
free(*buffer);
2024-03-02 00:07:58 +01:00
*buffer = NULL;
}
return ret;
}