OSCR
Open Source Cartridge Reader
Loading...
Searching...
No Matches
Storage.h
1/********************************************************************
2* Open Source Cartridge Reader *
3********************************************************************/
4#pragma once
5#ifndef OSCR_STORAGE_H_
6# define OSCR_STORAGE_H_
7
8# include "syslibinc.h"
9# include "config.h"
10# include "common/Types.h"
11
12namespace OSCR
13{
14 namespace Storage
15 {
16 constexpr size_t const kMaxPathDepth = PATH_MAX_DEPTH;
17 constexpr size_t const kMaxNameLength = UI_FILE_BROWSER_FILENAME_MAX;
18 constexpr size_t const kMaxPathLength = (kMaxPathDepth * (kMaxNameLength + 1)) + 1;
19 constexpr uint8_t const kFileNameLength = 100;
20 constexpr uint8_t const kFilePathnameLength = 132;
21 constexpr size_t const kBufferSize = 512;
22# if (OPTION_UNIQUE_DIRECTORY_METHOD == UNQDIR_INCREMENT)
23 constexpr uint8_t const kFolderIncrementLength = 5; // IIII = 4 + NUL
24# elif (OPTION_UNIQUE_DIRECTORY_METHOD == UNQDIR_RTC)
25 constexpr uint8_t const kFolderIncrementLength = 11; // YYMMDDHHMM = 10 + NUL
26# elif (OPTION_UNIQUE_DIRECTORY_METHOD == UNQDIR_BOTH)
27 constexpr uint8_t const kFolderIncrementLength = 15; // YYMMDDHHMMIIII = 14 + NUL
28# endif
29
30 extern char const PROGMEM AllowedSymbols[];
31 extern char const PROGMEM FilenameTemplate[];
32 extern char const PROGMEM FilenameTemplateP[];
33 extern char const PROGMEM FilenameTemplatePP[];
34
35 enum class CRC32State : uint8_t
36 {
37 None,
38 Summing,
39 Summed,
40 };
41
42 extern SdFs sd;
43
44 extern void setup();
45
46 extern uint8_t copyFileName(char const * const src, uint8_t const srcMaxLen, char * const dest, uint8_t const destMaxLen);
47 extern uint8_t copyFileName_P(char const * const src, char * const dest, uint8_t const destMaxLen);
48
49# if defined(ENABLE_RTC)
50 extern void dateTimeCB(uint16_t * date, uint16_t * time);
51# endif
52
53 extern bool exists(__StringHelper const * path);
54 extern bool exists(char const * path);
55
56 extern bool isDir(char const * path);
57 extern bool isDir(__StringHelper const * path);
58
59 extern bool isFile(char const * path);
60 extern bool isFile(__StringHelper const * path);
61
62 extern bool createFolder(char const * path, bool recursive = true);
63 extern bool createFolder(__StringHelper const * path, bool recursive = true);
64
65 class Path
66 {
67 public:
68 Path() = default;
69
70 bool getPath(char buffer[], size_t bufferSize = kMaxPathLength) const;
71
72 String getPath() const;
73
74 void cd();
75 void cd(char const * dir);
76 void cd(__StringHelper const * dir);
77
78 void chdir();
79
80 uint8_t getDepth();
81
82 protected:
83 uint8_t depth = 0;
84 char path[kMaxPathDepth][kMaxNameLength];
85 };
86
87 class File
88 {
89 public:
90 File();
91
92 File(uint8_t * fileBuffer, uint16_t fileBufferSize);
93
94 File(char const * fileName, oflag_t oflag);
95 File(__StringHelper const * fileName, oflag_t oflag);
96
97 void assignBuffer(uint8_t * fileBuffer, uint16_t fileBufferSize);
98 void unassignBuffer();
99 bool hasBuffer() const;
100
101 bool open(char const * fileName, oflag_t oflag);
102 bool open(__StringHelper const * fileName, oflag_t oflag);
103 bool open(uint32_t index, oflag_t oflag);
104
105 bool isOpen();
106
107 bool close();
108
109 void sync();
110
111 bool rename(char const * newPath);
112 bool rename(__StringHelper const * newPath);
113
114 bool writeRaw(uint8_t byte);
115 size_t writeRaw(size_t count);
116
117 size_t writeRaw(uint8_t const * buf, size_t count);
118
119 size_t write();
120 size_t write(size_t count);
121
122 bool write(uint8_t byte);
123
124 size_t write(char const * buf);
125 size_t write(char const * buf, size_t count);
126 size_t write(uint8_t const * buf, size_t count);
127
128 size_t writeNewLine();
129
130 uint8_t peek();
131
132 size_t readRaw();
133 size_t readRaw(size_t count);
134 size_t readRaw(uint8_t * buf, size_t count);
135
136 size_t fill();
137
138 int16_t read();
139 size_t read(size_t count);
140 size_t read(uint8_t * buf, size_t count);
141
142 bool find(uint8_t const * target);
143 bool find(char const * target);
144 bool find(__StringHelper const * target);
145
146 size_t readBytesUntil(char terminator, uint8_t * buf, size_t count);
147 size_t readBytesUntil(char terminator, char * buf, size_t count);
148 size_t readBytesUntil(char terminator, size_t count);
149
150 size_t readBytesUntil(__StringHelper const * terminator, uint8_t * buf, size_t count);
151 size_t readBytesUntil(__StringHelper const * terminator, char * buf, size_t count);
152 size_t readBytesUntil(__StringHelper const * terminator, size_t count);
153
154 size_t readBytesUntil_P(uint8_t const * terminator, uint8_t * buf, size_t count);
155 size_t readBytesUntil_P(uint8_t const * terminator, char * buf, size_t count);
156 size_t readBytesUntil_P(uint8_t const * terminator, size_t count);
157
158 uint32_t fileSize() const;
159 uint64_t fileSize64() const;
160
161 uint32_t available() const;
162 uint64_t available64() const;
163
164 bool remove();
165
166 uint64_t curPosition() const;
167 bool seekCur(int64_t offset);
168 bool seekSet(uint64_t pos);
169 bool seekEnd(int64_t offset = 0);
170 void rewind();
171
172 void crcReset();
173 OSCR::CRC32::crc32_t getCRC32();
174
175 void enableFatalErrors();
176 void disableFatalErrors();
177
178 bool getName(char * name, size_t len);
179
180 protected:
181 FsFile file;
182 oflag_t fileOflag;
183 bool fatalErrors = true;
184 bool opened = false;
185 CRC32State crcState = CRC32State::None;
187
188 uint8_t * buffer = nullptr;
189 uint16_t bufferSize = 0;
190
191 void bufferCheck(size_t count);
192 void postOpen();
193 void postClose();
194 void sumCRC32();
195 };
196
197 namespace Shared
198 {
199 extern File sharedFile;
200 extern Path sharedFilePath;
201
202 extern __constinit uint8_t buffer[kBufferSize];
203
204 extern __constinit char sharedFileName[kFileNameLength];
205 extern __constinit char sharedFilePathname[kFilePathnameLength];
206
207 extern __constinit uint16_t folderIncrement;
208 extern __constinit char folderIncrementStr[kFolderIncrementLength];
209
213 extern void incrementFolder();
214
223 extern void createFolder(char const * system, char const * subfolder, char const * gameName);
224
233 extern void createFolder(__StringHelper const * system, __StringHelper const * subfolder, char const * gameName);
234
244 extern void createFile(char const * system, char const * subfolder, char const * gameName, char const * fileSuffix = NULL);
245
255 extern void createFile(__StringHelper const * system, __StringHelper const * subfolder, char const * gameName, __StringHelper const * fileSuffix = NULL);
256
266 extern void createFile(__StringHelper const * system, __StringHelper const * subfolder, char const * gameName, uint16_t fileSuffix);
267
275 extern void createFileCWD(char const * gameName, char const * fileSuffix = NULL);
276
284 extern void createFileCWD(char const * gameName, __StringHelper const * fileSuffix = NULL);
285
293 extern void createFileCWD(char const * gameName, uint16_t fileSuffix);
294
298 extern void open(oflag_t oflag);
299
303 extern void openRW();
304
308 extern void openRWC();
309
313 extern void openRO();
314
318 extern void cd();
319
329 extern void cd(char const * dir);
330
340 extern void cd(__StringHelper const * dir);
341
350 extern void rename(char const * newName);
351
360 extern void rename(__StringHelper const * newName);
361
370 extern bool rename_P(char const * baseName, char const * fileExt);
371
380 extern bool rename_P(char const * baseName, __StringHelper const * fileExt);
381
390 extern bool renameTemplate_P(char const * pathTemplate, char const * newName);
391
400 extern bool renameTemplate_P(char const * pathTemplate, __StringHelper const * newName);
401
411 extern bool renameTemplate_P(char const * pathTemplate, char const * baseName, char const * newExt);
412
422 extern bool renameTemplate_P(char const * pathTemplate, char const * baseName, __StringHelper const * newExt);
423
432 extern uint32_t getSize();
433
442 extern uint32_t available();
443
450 extern size_t fill();
451
457 extern size_t dump();
458
462 extern void rewind();
463
473 extern size_t readBuffer(size_t length = kBufferSize);
474
483 extern size_t writeBuffer(size_t length = kBufferSize);
484
488 extern void crcReset();
489
493 extern OSCR::CRC32::crc32_t getCRC32();
494
498 extern void close();
499 } /* namespace Shared */
500 } /* namespace Storage */
501} /* namespace OSCR */
502
503#endif /* OSCR_STORAGE_H_ */
#define NULL
Definition Storage.h:88
Definition Storage.h:66
Main program.
Definition Storage.h:13
Interface for handling CRC32 values.
Definition crc32_t.h:14