-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFatFileSystem.cpp
185 lines (142 loc) · 3.48 KB
/
FatFileSystem.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "FatFileSystem.h"
#include "HardwareSerial.h"
#include <stdio.h>
#include <string.h>
#include "Arduino.h"
namespace FAT {
FileSystem::FileSystem() :
_mmcInit(false)
{}
FileSystem::~FileSystem() {
_result = f_mount(NULL, _TEXT(""), 0);
}
void FileSystem::initialize() {
if (_mmcInit)
return;
_result = f_mount(&_fatFs, _TEXT(""), 0);
if (_result == FR_OK)
_mmcInit = true;
}
void FileSystem::make_dir(const TCHAR *path) {
_result = f_mkdir(path);
}
void FileSystem::unlink(const TCHAR* path) {
_result = f_unlink(path);
}
void FileSystem::rename(const TCHAR* oldpath, const TCHAR* newpath) {
_result = f_rename(oldpath, newpath);
}
File::File(void) {
memset(&_File, 0, sizeof(_File));
}
File::File(const TCHAR *path, BYTE mode) {
memset(&_File, 0, sizeof(_File));
_result = f_open(&_File, path, mode);
}
File::~File() {
_result = FR_OK;
if (_File.fs)
_result = f_close(&_File);
}
bool File::exists(const TCHAR *path) {
FileInfo info(path);
if (info.result() == FR_OK)
return true;
return false;
}
DWORD File::size(const TCHAR* path) {
FileInfo info(path);
if (info.result() == FR_OK)
return info.size();
return 0;
}
void File::open(const TCHAR *path, BYTE mode) {
if (_File.fs) {
_result = f_close(&_File);
if (_result != FR_OK)
return;
memset(&_File, 0, sizeof(_File));
}
_result = f_open(&_File, path, mode);
}
void File::close(void) {
_result = FR_OK;
if (_File.fs) {
_result = f_close(&_File);
memset(&_File, 0, sizeof(_File));
}
}
bool File::eof(void) {
return f_eof(&_File);
}
DWORD File::size(void) {
return f_size(&_File);
}
void File::lseek(DWORD ofs) {
_result = f_lseek(&_File, ofs);
}
DWORD File::tell(void) {
return f_tell(&_File);
}
UINT File::write(const BYTE* buf_p, UINT len) {
_result = f_write(&_File, buf_p, len, &len);
return len;
}
UINT File::read(void* buf_p, UINT len) {
_result = f_read(&_File, buf_p, len, &len);
return len;
}
void File::truncate(void) {
_result = f_truncate(&_File);
}
void File::sync(void) {
_result = f_sync(&_File);
}
FileInfo::FileInfo() {
memset(&_fileInfo, 0, sizeof(_fileInfo));
}
FileInfo::FileInfo(const TCHAR* path) {
memset(&_fileInfo, 0, sizeof(_fileInfo));
_result = f_stat(path, &_fileInfo);
}
Directory::Directory(void) {
memset(&_Dir, 0, sizeof(_Dir));
}
Directory::Directory(const TCHAR *path) {
memset(&_Dir, 0, sizeof(_Dir));
_result = f_opendir(&_Dir, path);
}
Directory::~Directory() {
_result = FR_OK;
if (_Dir.fs)
_result = f_closedir(&_Dir);
}
void Directory::open(const TCHAR *path) {
if (_Dir.fs) {
_result = f_closedir(&_Dir);
if (_result != FR_OK)
return;
memset(&_Dir, 0, sizeof(_Dir));
}
_result = f_opendir(&_Dir, path);
}
void Directory::close(void) {
_result = FR_OK;
if (_Dir.fs) {
_result = f_closedir(&_Dir);
memset(&_Dir, 0, sizeof(_Dir));
}
}
FileInfo* Directory::next_entry(void) {
FileInfo *fi = new FileInfo;
_result = f_readdir(&_Dir, &(fi->_fileInfo));
if (_result == FR_OK) {
if (fi->_fileInfo.fname[0] == 0) // no more entries
_result = f_readdir(&_Dir, NULL); // so rewind read index
else
return fi; // otherwise return it
}
delete fi;
return NULL;
}
}; // namespace FAT