forked from btc08gh/sys-patch
-
Notifications
You must be signed in to change notification settings - Fork 17
/
minGlue.c
127 lines (103 loc) · 3.22 KB
/
minGlue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "minGlue.h"
#include <string.h>
static bool ini_open(const char* filename, struct NxFile* nxfile, u32 mode) {
Result rc = {0};
char filename_buf[FS_MAX_PATH] = {0};
if (R_FAILED(rc = fsOpenSdCardFileSystem(&nxfile->system))) {
return false;
}
strcpy(filename_buf, filename);
if (R_FAILED(rc = fsFsOpenFile(&nxfile->system, filename_buf, mode, &nxfile->file))) {
if (mode & FsOpenMode_Write) {
if (R_FAILED(rc = fsFsCreateFile(&nxfile->system, filename_buf, 0, 0))) {
fsFsClose(&nxfile->system);
return false;
} else {
if (R_FAILED(rc = fsFsOpenFile(&nxfile->system, filename_buf, 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);
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};
char src_buf[FS_MAX_PATH] = {0};
char dst_buf[FS_MAX_PATH] = {0};
if (R_FAILED(rc = fsOpenSdCardFileSystem(&fs))) {
return false;
}
strcpy(src_buf, src);
strcpy(dst_buf, dst);
rc = fsFsRenameFile(&fs, src_buf, dst_buf);
fsFsClose(&fs);
return R_SUCCEEDED(rc);
}
bool ini_remove(const char* filename) {
Result rc = {0};
FsFileSystem fs = {0};
char filename_buf[FS_MAX_PATH] = {0};
if (R_FAILED(rc = fsOpenSdCardFileSystem(&fs))) {
return false;
}
strcpy(filename_buf, filename);
rc = fsFsDeleteFile(&fs, filename_buf);
fsFsClose(&fs);
return R_SUCCEEDED(rc);
}