Repository navigation
Expand file tree
/
Copy pathSdTextFile.h
More file actions
81 lines (69 loc) · 2.39 KB
/
Copy pathSdTextFile.h
File metadata and controls
81 lines (69 loc) · 2.39 KB
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
#ifndef INCLUDE_SDTEXTFILE_H_
#define INCLUDE_SDTEXTFILE_H_
#include "Platform.h"
#include <cstddef>
#include <cstdint>
/**
* @brief RAII guard for FileSystem::FileHandle that closes the file on scope exit.
*/
class SdScopedFile
{
public:
SdScopedFile(FileSystem* fs, const char* path, FileSystem::FileOpenMode mode);
SdScopedFile(FileSystem* fs, FileSystem::FileHandle handle, bool isOpen);
~SdScopedFile();
SdScopedFile(const SdScopedFile&) = delete;
SdScopedFile& operator=(const SdScopedFile&) = delete;
explicit operator bool() const { return open_ && handle_ != nullptr; }
FileSystem::FileHandle handle() const { return handle_; }
private:
FileSystem* fs_;
FileSystem::FileHandle handle_;
bool open_;
};
namespace SdTextFile
{
/**
* @brief Open a file, retrying without a leading '/' if the initial open fails.
*/
std::tuple<bool, FileSystem::FileHandle> openWithFallback(
FileSystem* fs,
const char* path,
FileSystem::FileOpenMode mode = FileSystem::kRead);
/**
* @brief Read an entire text file into a caller-supplied buffer and null-terminate it.
* @return true if at least 1 byte was read.
*/
bool readAll(FileSystem* fs,
const char* path,
char* buf,
size_t bufSize,
size_t* outLen = nullptr);
/**
* @brief Pick a random word from a newline/delimiter-separated text file on SD without loading
* the whole file into RAM.
* @return true if a non-empty word was extracted into outWord.
*/
bool readRandomWord(FileSystem* fs,
const char* path,
uint32_t seed,
char* outWord,
size_t maxLen);
/**
* @brief Stream an INI-style `[key]` section from a text file on SD into outBuf using a small
* stack buffer (0 B permanent RAM). Ignores `#` comment lines and `\r`, preserves
* multi-line paragraphs and internal blank lines, trims trailing blank lines, and always
* NUL-terminates outBuf.
* @return true if the section was found and produced at least 1 character.
*/
bool readSection(FileSystem* fs,
const char* path,
const char* key,
char* outBuf,
size_t outSize);
/**
* @brief Remove all '\r' characters in-place from a NUL-terminated string.
*/
void stripCr(char* buf);
} // namespace SdTextFile
#endif /* INCLUDE_SDTEXTFILE_H_ */