#include "minGlue.h" #include static bool ini_open(const char* filename, struct NxFile* nxfile, u32 mode) { Result rc = {0}; if (R_FAILED(rc = fsOpenSdCardFileSystem(&nxfile->system))) { return false; } if (R_FAILED(rc = fsFsOpenFile(&nxfile->system, filename, mode, &nxfile->file))) { if (mode & FsOpenMode_Write) { if (R_FAILED(rc = fsFsCreateFile(&nxfile->system, filename, 0, 0))) { fsFsClose(&nxfile->system); return false; } else { if (R_FAILED(rc = fsFsOpenFile(&nxfile->system, filename, mode, &nxfile->file))) { fsFsClose(&nxfile->system); return false; } } } else { fsFsClose(&nxfile->system); return false; } } nxfile->offset = 0; return true; } bool ini_openread(const char* filename, struct NxFile* nxfile) { return ini_open(filename, nxfile, FsOpenMode_Read); } bool ini_openwrite(const char* filename, struct NxFile* nxfile) { return ini_open(filename, nxfile, FsOpenMode_Write|FsOpenMode_Append); } bool ini_openrewrite(const char* filename, struct NxFile* nxfile) { return ini_open(filename, nxfile, FsOpenMode_Read|FsOpenMode_Write|FsOpenMode_Append); } bool ini_close(struct NxFile* nxfile) { fsFileClose(&nxfile->file); fsFsCommit(&nxfile->system); fsFsClose(&nxfile->system); return true; } bool ini_read(char* buffer, u64 size, struct NxFile* nxfile) { u64 bytes_read = {0}; if (R_FAILED(fsFileRead(&nxfile->file, nxfile->offset, buffer, size, FsReadOption_None, &bytes_read))) { return false; } if (!bytes_read) { return false; } char *eol = {0}; if ((eol = strchr(buffer, '\n')) == NULL) { eol = strchr(buffer, '\r'); } if (eol != NULL) { *++eol = '\0'; bytes_read = eol - buffer; } nxfile->offset += bytes_read; return true; } bool ini_write(const char* buffer, struct NxFile* nxfile) { const size_t size = strlen(buffer); if (R_FAILED(fsFileWrite(&nxfile->file, nxfile->offset, buffer, size, FsWriteOption_None))) { return false; } nxfile->offset += size; return true; } bool ini_tell(struct NxFile* nxfile, s64* pos) { *pos = nxfile->offset; return true; } bool ini_seek(struct NxFile* nxfile, s64* pos) { nxfile->offset = *pos; return true; } bool ini_rename(const char* src, const char* dst) { Result rc = {0}; FsFileSystem fs = {0}; if (R_FAILED(rc = fsOpenSdCardFileSystem(&fs))) { return false; } rc = fsFsRenameFile(&fs, src, dst); fsFsClose(&fs); return R_SUCCEEDED(rc); } bool ini_remove(const char* filename) { Result rc = {0}; FsFileSystem fs = {0}; if (R_FAILED(rc = fsOpenSdCardFileSystem(&fs))) { return false; } rc = fsFsDeleteFile(&fs, filename); fsFsClose(&fs); return R_SUCCEEDED(rc); }