1
1
#include " RamFile.h"
2
2
3
- RamFile::RamFile (uint8_t * data, size_t size) : data(data), dataSize(size){
3
+ RamFile::RamFile (uint8_t * data, size_t size, bool readonly ) : data(data), dataSize(size), readonly(readonly ){
4
4
5
5
}
6
6
7
7
RamFile::~RamFile (){
8
8
RamFile::close ();
9
9
}
10
10
11
- fs::File RamFile::open (uint8_t * data, size_t size){
12
- return File (std::make_shared<RamFile>(data, size));
11
+ fs::File RamFile::open (uint8_t * data, size_t size, bool readonly ){
12
+ return File (std::make_shared<RamFile>(data, size, readonly ));
13
13
}
14
14
15
- fs::File RamFile::open (fs::File file){
15
+ fs::File RamFile::open (fs::File& file, bool readonly ){
16
16
uint8_t * data;
17
17
#ifdef CONFIG_SPIRAM_SUPPORT
18
18
data = static_cast <uint8_t *>(ps_malloc (file.size ()));
@@ -23,7 +23,7 @@ fs::File RamFile::open(fs::File file){
23
23
file.seek (0 );
24
24
file.readBytes (reinterpret_cast <char *>(data), file.size ());
25
25
26
- auto f = std::make_shared<RamFile>(data, file.size ());
26
+ auto f = std::make_shared<RamFile>(data, file.size (), readonly );
27
27
28
28
String name = file.name ();
29
29
char * fstr = static_cast <char *>(malloc (name.length () + 1 ));
@@ -35,17 +35,37 @@ fs::File RamFile::open(fs::File file){
35
35
}
36
36
37
37
size_t RamFile::write (uint8_t data){
38
+ if (readonly) return 0 ;
39
+
40
+ if (cursor == dataSize){
41
+ dataSize++;
42
+ }
43
+
38
44
this ->data [cursor++] = data;
39
45
return 1 ;
40
46
}
41
47
42
48
size_t RamFile::write (const uint8_t * buf, size_t size){
49
+ if (readonly) return 0 ;
50
+ if (size == 0 ) return 0 ;
51
+
52
+ if (cursor + size > dataSize){
53
+ dataSize = size + cursor;
54
+ #ifdef CONFIG_SPIRAM_SUPPORT
55
+ data = static_cast <uint8_t *>(ps_realloc (data, dataSize));
56
+ #else
57
+ data = static_cast <uint8_t *>(realloc (data, dataSize));
58
+ #endif
59
+ }
60
+
43
61
memcpy (data + cursor, buf, size);
44
62
cursor += size;
63
+
64
+ return size;
45
65
}
46
66
47
67
int RamFile::available (){
48
- return cursor < dataSize ;
68
+ return dataSize - cursor ;
49
69
}
50
70
51
71
int RamFile::read (){
@@ -61,16 +81,12 @@ void RamFile::flush(){
61
81
}
62
82
63
83
bool RamFile::seek (uint32_t pos, fs::SeekMode mode){
64
- switch (mode){
65
- case fs::SeekSet:
66
- cursor = pos;
67
- break ;
68
- case fs::SeekEnd:
69
- cursor = dataSize - pos - 1 ;
70
- break ;
71
- case fs::SeekCur:
72
- cursor += pos;
73
- break ;
84
+ if (mode == fs::SeekSet){
85
+ cursor = pos;
86
+ }else if (mode == fs::SeekEnd){
87
+ cursor = dataSize - pos;
88
+ }else if (mode == fs::SeekCur){
89
+ cursor += pos;
74
90
}
75
91
}
76
92
0 commit comments