Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
Adafruit_LittleFS.h
Go to the documentation of this file.
1
5#ifndef FAKE_LITTLEFS_H
6#define FAKE_LITTLEFS_H
7
8#include <cstdio>
9#include <unistd.h>
10
11namespace Adafruit_LittleFS_Namespace {
12static constexpr uint8_t _placeholder_littlefs = 0;
13
14} // namespace Adafruit_LittleFS_Namespace
15
17{
18 bool begin()
19 {
20 // fprintf(stderr, "info: InternalFS.begin called\n");
21 return true;
22 }
23
24 void end() {}
25
26 void format() { fprintf(stderr, "warning: InternalFS.format called\n"); }
27};
28
29InternalFSTy InternalFS {};
30
31#define FILE_O_READ "r+"
32#define FILE_O_WRITE "w+"
33
34struct File
35{
36 File(InternalFSTy& InternalFS) : file {nullptr}, filemode {nullptr}, InternalFS {InternalFS} {}
37
38 bool open(const char* fname, const char* mode)
39 {
40 // fprintf(stderr, "fopen: %s %s\n", fname, mode);
41
42 if (file != nullptr)
43 {
44 fprintf(stderr, "fopen: opening two files?\n");
45 filemode = nullptr;
46 fclose(file);
47 }
48
49 snprintf(filename, sizeof(filename), ".%s", fname);
50 filemode = mode;
51 file = fopen(filename, mode);
52 if (file == nullptr)
53 {
54 fprintf(stderr, "fopen: failed with %s %s\n", filename, mode);
55 }
56 return file != nullptr;
57 }
58
59 bool isOpen() { return file != nullptr; }
60
61 bool available() { return file != nullptr; }
62
63 void close()
64 {
65 if (file != nullptr)
66 {
67 fclose(file);
68 file = nullptr;
69 filemode = nullptr;
70 }
71 else
72 {
73 fprintf(stderr, "error: closing a closed file!\n");
74 }
75 }
76
77 size_t size()
78 {
79 if (file != nullptr)
80 {
81 size_t pos = ftell(file);
82 fseek(file, 0L, SEEK_END);
83 size_t size = ftell(file);
84 fseek(file, pos, SEEK_SET);
85 return size;
86 }
87
88 fprintf(stderr, "error: measuring size on a closed file!\n");
89 return 0;
90 }
91
92 size_t write(uint8_t* in, size_t sz)
93 {
94 if (file != nullptr)
95 {
96 // uncomment this to log all filesystem write
97#if 0
98 fprintf(stderr, "write: (%d) ", sz);
99 for (size_t I = 0; I < sz; ++I)
100 {
101 fprintf(stderr, "%02x", in[I]);
102 }
103 fprintf(stderr, "\n");
104#endif
105
106 return fwrite(in, sizeof(uint8_t), sz, file);
107 }
108
109 fprintf(stderr, "error: writing on a closed file!\n");
110 return 0;
111 }
112
113 size_t read(uint8_t* out, size_t sz)
114 {
115 if (file != nullptr)
116 {
117 return fread(out, sizeof(uint8_t), sz, file);
118 }
119
120 fprintf(stderr, "error: reading a closed file!\n");
121 return 0;
122 }
123
124 bool seek(uint8_t sz)
125 {
126 if (file != nullptr)
127 {
128 if (filemode != nullptr)
129 {
130 ::fseek(file, sz, 0);
131 return true;
132 }
133 else
134 {
135 fprintf(stderr, "error: invalid seek filename\n");
136 }
137 }
138 else
139 {
140 fprintf(stderr, "error: seek closed file\n");
141 }
142 return false;
143 }
144
145 void truncate(uint8_t sz)
146 {
147 if (file != nullptr)
148 {
149 fclose(file);
150 file = nullptr;
151
152 if (filemode != nullptr)
153 {
154 ::truncate(filename, sz);
155 }
156 else
157 {
158 fprintf(stderr, "error: invalid truncate filename\n");
159 }
160
161 if (filemode != nullptr)
162 {
163 file = fopen(filename, filemode);
164 }
165 else
166 {
167 fprintf(stderr, "error: invalid truncate fopen\n");
168 }
169 }
170 else
171 {
172 fprintf(stderr, "error: truncate closed file\n");
173 }
174 }
175
176 operator bool() const { return file != nullptr; }
177
178 FILE* file;
179 char filename[256];
180 const char* filemode;
181 InternalFSTy& InternalFS;
182};
183
184#endif
Definition: Adafruit_LittleFS.h:35
Definition: Adafruit_LittleFS.h:17