Repository navigation
Expand file tree
/
Copy pathFileSystemAdapter.cpp
More file actions
125 lines (107 loc) · 3.9 KB
/
Copy pathFileSystemAdapter.cpp
File metadata and controls
125 lines (107 loc) · 3.9 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// NOLINTBEGIN
#ifdef FEATURE_SYNTH
#include "FileSystemAdapter.h"
#include "SdPaths.h"
#include "config.h"
#include "cr_section_macros.h"
#include <algorithm>
#include <cassert>
#include <cstring>
namespace FileSystemAdapterBuffers
{
__NOINIT(EXTERNAL_RAM) static uint8_t fileNames[kMaxFilesListLength * (kMaxFileNameLen + 1)];
__NOINIT(EXTERNAL_RAM) static uint8_t listDataName[kMaxFilesListLength * sizeof(char**)];
}
char* FileSystemAdapter::fileNamesBuffer = nullptr;
char** FileSystemAdapter::listDataName = nullptr;
FileSystemAdapter::FileSystemAdapter(const std::string& pathPrefix,
::FileSystem* platformFileSystem)
: platformFileSystem(platformFileSystem)
, pathPrefix(pathPrefix)
{
}
void FileSystemAdapter::staticInit()
{
#ifdef DEBUG
static int instanceCounter = 0;
assert(instanceCounter++ == 0);
#endif
fileNamesBuffer = (char*)FileSystemAdapterBuffers::fileNames;
listDataName = (char**)FileSystemAdapterBuffers::listDataName;
}
std::vector<std::string_view> FileSystemAdapter::listFiles(
const std::string& path,
std::function<bool(const ::std::string& name)> matcher,
tiny_optional<int> /*maxFiles*/)
{
memset(listDataName, 0, kMaxFilesListLength);
auto wrappedMatcher = [&matcher](const ::std::string& name)
{
if (name.empty() || name[0] == '.')
{
return false;
}
return matcher ? matcher(name) : true;
};
fileCount = kMaxFilesListLength;
platformFileSystem->listDirContents((pathPrefix + path).c_str(),
::FileSystem::DirContentType::kFiles,
wrappedMatcher,
fileNamesBuffer,
kMaxFilesListLength * (kMaxFileNameLen + 1),
kMaxFileNameLen,
fileCount,
listDataName);
std::vector<std::string_view> fileList;
std::ranges::transform(std::span(listDataName, fileCount),
std::back_inserter(fileList),
[](char* file) { return std::string_view(file); });
return fileList;
}
std::optional<polysynth::FileSystem::FileHandle> FileSystemAdapter::open(
const std::string& path,
polysynth::FileSystem::OpenOptions options)
{
const auto mode =
(static_cast<int>(options) & static_cast<int>(FileSystem::OpenOptions::kWrite)) != 0
? ::FileSystem::FileOpenMode::kWriteCreateNew
: ::FileSystem::FileOpenMode::kRead;
if (!path.empty() && path[0] == '/')
{
auto [absResult, absFile] = platformFileSystem->openFile(path.c_str(), mode);
if (absResult)
{
return std::optional<polysynth::FileSystem::FileHandle>(absFile);
}
}
auto [result, file] = platformFileSystem->openFile((pathPrefix + path).c_str(), mode);
if (!result && mode == ::FileSystem::FileOpenMode::kRead)
{
std::tie(result, file) = platformFileSystem->openFile(("/" + path).c_str(), mode);
}
return result ? std::optional<polysynth::FileSystem::FileHandle>(file) : std::nullopt;
}
void FileSystemAdapter::close(FileHandle file)
{
platformFileSystem->closeFile(file);
}
bool FileSystemAdapter::write(FileHandle file, std::span<const uint8_t> data)
{
return platformFileSystem->writeFile(data.data(), data.size(), file) ==
static_cast<int32_t>(data.size());
}
int32_t FileSystemAdapter::read(FileHandle file, std::span<uint8_t> buf)
{
return platformFileSystem->readFile(buf.data(), buf.size(), file);
}
bool FileSystemAdapter::seek(FileHandle file, int offset)
{
return platformFileSystem->seekFileCur(offset, file);
}
void FileSystemAdapter::createFolder(const std::string& path)
{
std::string fullPath = pathPrefix + path;
SdPaths::ensureDir(platformFileSystem, fullPath);
}
#endif
// NOLINTEND