Skip to content

Commit 2e88d58

Browse files
committed
Merge branch 'feature/fs' into esp8266
2 parents 40b9247 + 76ab2cc commit 2e88d58

File tree

9 files changed

+1058
-9
lines changed

9 files changed

+1058
-9
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
FS.cpp - file system wrapper
3+
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include "FS.h"
22+
#include "FSImpl.h"
23+
24+
static bool sflags(const char* mode, OpenMode& om, AccessMode& am);
25+
26+
size_t File::write(uint8_t c) {
27+
if (!_p)
28+
return 0;
29+
30+
_p->write(&c, 1);
31+
}
32+
33+
size_t File::write(const uint8_t *buf, size_t size) {
34+
if (!_p)
35+
return 0;
36+
37+
_p->write(buf, size);
38+
}
39+
40+
int File::available() {
41+
if (!_p)
42+
return false;
43+
44+
return _p->position() < _p->size();
45+
}
46+
47+
int File::read() {
48+
if (!_p)
49+
return -1;
50+
51+
uint8_t result;
52+
if (_p->read(&result, 1) != 1) {
53+
return -1;
54+
}
55+
56+
return result;
57+
}
58+
59+
size_t File::read(uint8_t* buf, size_t size) {
60+
if (!_p)
61+
return -1;
62+
63+
return _p->read(buf, size);
64+
}
65+
66+
int File::peek() {
67+
if (!_p)
68+
return -1;
69+
70+
size_t curPos = _p->position();
71+
int result = read();
72+
seek(curPos, SeekSet);
73+
return result;
74+
}
75+
76+
void File::flush() {
77+
if (!_p)
78+
return;
79+
80+
_p->flush();
81+
}
82+
83+
bool File::seek(uint32_t pos, SeekMode mode) {
84+
if (!_p)
85+
return false;
86+
87+
return _p->seek(pos, mode);
88+
}
89+
90+
size_t File::position() const {
91+
if (!_p)
92+
return 0;
93+
94+
return _p->position();
95+
}
96+
97+
size_t File::size() const {
98+
if (!_p)
99+
return 0;
100+
101+
return _p->size();
102+
}
103+
104+
void File::close() {
105+
if (_p) {
106+
_p->close();
107+
_p = nullptr;
108+
}
109+
}
110+
111+
File::operator bool() const {
112+
return !!_p;
113+
}
114+
115+
File Dir::openFile(const char* mode) {
116+
if (!_impl) {
117+
return File();
118+
}
119+
120+
OpenMode om;
121+
AccessMode am;
122+
if (!sflags(mode, om, am)) {
123+
DEBUGV("Dir::openFile: invalid mode `%s`\r\n", mode);
124+
return File();
125+
}
126+
127+
return File(_impl->openFile(om, am));
128+
}
129+
130+
String Dir::fileName() {
131+
if (!_impl) {
132+
return String();
133+
}
134+
135+
return _impl->fileName();
136+
}
137+
138+
bool Dir::next() {
139+
if (!_impl) {
140+
return false;
141+
}
142+
143+
return _impl->next();
144+
}
145+
146+
147+
148+
File FS::open(const char* path, const char* mode) {
149+
if (!_impl) {
150+
return File();
151+
}
152+
153+
OpenMode om;
154+
AccessMode am;
155+
if (!sflags(mode, om, am)) {
156+
DEBUGV("FS::open: invalid mode `%s`\r\n", mode);
157+
return File();
158+
}
159+
160+
return File(_impl->open(path, om, am));
161+
}
162+
163+
File FS::open(const String& path, const char* mode) {
164+
return FS::open(path.c_str(), mode);
165+
}
166+
167+
Dir FS::openDir(const char* path) {
168+
if (!_impl) {
169+
return Dir();
170+
}
171+
172+
return Dir(_impl->openDir(path));
173+
}
174+
175+
Dir FS::openDir(const String& path) {
176+
return FS::openDir(path.c_str());
177+
}
178+
179+
struct MountEntry {
180+
FSImplPtr fs;
181+
String path;
182+
MountEntry* next;
183+
};
184+
185+
static MountEntry* s_mounted = nullptr;
186+
187+
template<>
188+
bool mount<FS>(FS& fs, const char* mountPoint) {
189+
FSImplPtr p = fs._impl;
190+
if (!p || !p->mount()) {
191+
DEBUGV("FSImpl mount failed\r\n");
192+
return false;
193+
}
194+
195+
MountEntry* entry = new MountEntry;
196+
entry->fs = p;
197+
entry->path = mountPoint;
198+
entry->next = s_mounted;
199+
s_mounted = entry;
200+
return true;
201+
}
202+
203+
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
204+
switch (mode[0]) {
205+
case 'r':
206+
am = AM_READ;
207+
om = OM_DEFAULT;
208+
break;
209+
case 'w':
210+
am = AM_WRITE;
211+
om = (OpenMode) (OM_CREATE | OM_TRUNCATE);
212+
break;
213+
case 'a':
214+
am = AM_WRITE;
215+
om = (OpenMode) (OM_CREATE | OM_APPEND);
216+
break;
217+
default:
218+
return false;
219+
}
220+
switch(mode[1]) {
221+
case '+':
222+
am = (AccessMode) (AM_WRITE | AM_READ);
223+
break;
224+
case 0:
225+
break;
226+
default:
227+
return false;
228+
}
229+
return true;
230+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
FS.h - file system wrapper
3+
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
4+
This file is part of the esp8266 core for Arduino environment.
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#ifndef FS_H
22+
#define FS_H
23+
24+
#include <Arduino.h>
25+
#include <memory>
26+
27+
class FileImpl;
28+
typedef std::shared_ptr<FileImpl> FileImplPtr;
29+
class FSImpl;
30+
typedef std::shared_ptr<FSImpl> FSImplPtr;
31+
class DirImpl;
32+
typedef std::shared_ptr<DirImpl> DirImplPtr;
33+
34+
template <typename Tfs>
35+
bool mount(Tfs& fs, const char* mountPoint);
36+
37+
enum SeekMode {
38+
SeekSet = 0,
39+
SeekCur = 1,
40+
SeekEnd = 2
41+
};
42+
43+
class File : public Stream
44+
{
45+
public:
46+
File(FileImplPtr p = FileImplPtr()) : _p(p) {}
47+
48+
// Print methods:
49+
size_t write(uint8_t) override;
50+
size_t write(const uint8_t *buf, size_t size) override;
51+
52+
// Stream methods:
53+
int available() override;
54+
int read() override;
55+
int peek() override;
56+
void flush() override;
57+
58+
size_t read(uint8_t* buf, size_t size);
59+
bool seek(uint32_t pos, SeekMode mode);
60+
size_t position() const;
61+
size_t size() const;
62+
void close();
63+
operator bool() const;
64+
65+
protected:
66+
FileImplPtr _p;
67+
};
68+
69+
class Dir {
70+
public:
71+
Dir(DirImplPtr impl = DirImplPtr()): _impl(impl) { }
72+
73+
File openFile(const char* mode);
74+
String fileName();
75+
bool next();
76+
77+
protected:
78+
DirImplPtr _impl;
79+
};
80+
81+
class FS
82+
{
83+
public:
84+
FS(FSImplPtr impl) : _impl(impl) { }
85+
File open(const char* path, const char* mode);
86+
File open(const String& path, const char* mode);
87+
Dir openDir(const char* path);
88+
Dir openDir(const String& path);
89+
90+
protected:
91+
FSImplPtr _impl;
92+
93+
template <typename Tfs>
94+
friend bool mount(Tfs& fs, const char* mountPoint);
95+
};
96+
97+
extern FS SPIFFS;
98+
99+
template<>
100+
bool mount<FS>(FS& fs, const char* mountPoint);
101+
102+
File open(const char* path, const char* mode);
103+
104+
Dir openDir(const char* path);
105+
106+
#endif //FS_H

0 commit comments

Comments
 (0)