mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-05-14 20:52:12 -04:00
Merge 'upstream/master' - At least it builds...
This commit is contained in:
commit
5e40669cbc
298 changed files with 8122 additions and 4685 deletions
|
@ -23,16 +23,11 @@
|
|||
#include "file.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
/* Values added to FatFs FRESULT enum, values outside the FRESULT data type */
|
||||
static_assert(sizeof(FIL::err) == 1, "FatFs FIL::err size not expected.");
|
||||
#define FR_DISK_FULL (0x100)
|
||||
#define FR_EOF (0x101)
|
||||
#define FR_BAD_SEEK (0x102)
|
||||
#define FR_UNEXPECTED (0x103)
|
||||
|
||||
Optional<File::Error> File::open_fatfs(const std::string& filename, BYTE mode) {
|
||||
auto result = f_open(&f, filename.c_str(), mode);
|
||||
Optional<File::Error> File::open_fatfs(const std::filesystem::path& filename, BYTE mode) {
|
||||
auto result = f_open(&f, reinterpret_cast<const TCHAR*>(filename.c_str()), mode);
|
||||
if( result == FR_OK ) {
|
||||
if( mode & FA_OPEN_ALWAYS ) {
|
||||
const auto result = f_lseek(&f, f_size(&f));
|
||||
|
@ -49,15 +44,15 @@ Optional<File::Error> File::open_fatfs(const std::string& filename, BYTE mode) {
|
|||
}
|
||||
}
|
||||
|
||||
Optional<File::Error> File::open(const std::string& filename) {
|
||||
Optional<File::Error> File::open(const std::filesystem::path& filename) {
|
||||
return open_fatfs(filename, FA_READ);
|
||||
}
|
||||
|
||||
Optional<File::Error> File::append(const std::string& filename) {
|
||||
Optional<File::Error> File::append(const std::filesystem::path& filename) {
|
||||
return open_fatfs(filename, FA_WRITE | FA_OPEN_ALWAYS);
|
||||
}
|
||||
|
||||
Optional<File::Error> File::create(const std::string& filename) {
|
||||
Optional<File::Error> File::create(const std::filesystem::path& filename) {
|
||||
return open_fatfs(filename, FA_WRITE | FA_CREATE_ALWAYS);
|
||||
}
|
||||
|
||||
|
@ -65,7 +60,7 @@ File::~File() {
|
|||
f_close(&f);
|
||||
}
|
||||
|
||||
File::Result<size_t> File::read(void* const data, const size_t bytes_to_read) {
|
||||
File::Result<File::Size> File::read(void* const data, const Size bytes_to_read) {
|
||||
UINT bytes_read = 0;
|
||||
const auto result = f_read(&f, data, bytes_to_read, &bytes_read);
|
||||
if( result == FR_OK ) {
|
||||
|
@ -75,12 +70,12 @@ File::Result<size_t> File::read(void* const data, const size_t bytes_to_read) {
|
|||
}
|
||||
}
|
||||
|
||||
File::Result<size_t> File::write(const void* const data, const size_t bytes_to_write) {
|
||||
File::Result<File::Size> File::write(const void* const data, const Size bytes_to_write) {
|
||||
UINT bytes_written = 0;
|
||||
const auto result = f_write(&f, data, bytes_to_write, &bytes_written);
|
||||
if( result == FR_OK ) {
|
||||
if( bytes_to_write == bytes_written ) {
|
||||
return { static_cast<size_t>(bytes_written) };
|
||||
return { static_cast<File::Size>(bytes_written) };
|
||||
} else {
|
||||
return Error { FR_DISK_FULL };
|
||||
}
|
||||
|
@ -89,7 +84,7 @@ File::Result<size_t> File::write(const void* const data, const size_t bytes_to_w
|
|||
}
|
||||
}
|
||||
|
||||
File::Result<uint64_t> File::seek(const uint64_t new_position) {
|
||||
File::Result<File::Offset> File::seek(const Offset new_position) {
|
||||
/* NOTE: Returns *old* position, not new position */
|
||||
const auto old_position = f_tell(&f);
|
||||
const auto result = f_lseek(&f, new_position);
|
||||
|
@ -99,7 +94,7 @@ File::Result<uint64_t> File::seek(const uint64_t new_position) {
|
|||
if( f_tell(&f) != new_position ) {
|
||||
return { static_cast<Error>(FR_BAD_SEEK) };
|
||||
}
|
||||
return { static_cast<uint64_t>(old_position) };
|
||||
return { static_cast<File::Offset>(old_position) };
|
||||
}
|
||||
|
||||
Optional<File::Error> File::write_line(const std::string& s) {
|
||||
|
@ -125,11 +120,11 @@ Optional<File::Error> File::sync() {
|
|||
}
|
||||
}
|
||||
|
||||
static std::string find_last_file_matching_pattern(const std::string& pattern) {
|
||||
std::string last_match;
|
||||
for(const auto& entry : std::filesystem::directory_iterator("", pattern.c_str())) {
|
||||
static std::filesystem::path find_last_file_matching_pattern(const std::filesystem::path& pattern) {
|
||||
std::filesystem::path last_match;
|
||||
for(const auto& entry : std::filesystem::directory_iterator(u"", pattern)) {
|
||||
if( std::filesystem::is_regular_file(entry.status()) ) {
|
||||
const auto match = entry.path();
|
||||
const auto& match = entry.path();
|
||||
if( match > last_match ) {
|
||||
last_match = match;
|
||||
}
|
||||
|
@ -138,18 +133,12 @@ static std::string find_last_file_matching_pattern(const std::string& pattern) {
|
|||
return last_match;
|
||||
}
|
||||
|
||||
std::string remove_filename_extension(const std::string& filename) {
|
||||
const auto extension_index = filename.find_last_of('.');
|
||||
return filename.substr(0, extension_index);
|
||||
}
|
||||
|
||||
static std::string increment_filename_stem_ordinal(const std::string& filename_stem) {
|
||||
std::string result { filename_stem };
|
||||
|
||||
auto it = result.rbegin();
|
||||
static std::filesystem::path increment_filename_stem_ordinal(std::filesystem::path path) {
|
||||
auto t = path.replace_extension().native();
|
||||
auto it = t.rbegin();
|
||||
|
||||
// Increment decimal number before the extension.
|
||||
for(; it != result.rend(); ++it) {
|
||||
for(; it != t.rend(); ++it) {
|
||||
const auto c = *it;
|
||||
if( c < '0' ) {
|
||||
return { };
|
||||
|
@ -163,36 +152,35 @@ static std::string increment_filename_stem_ordinal(const std::string& filename_s
|
|||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return t;
|
||||
}
|
||||
|
||||
std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern) {
|
||||
const auto filename = find_last_file_matching_pattern(filename_stem_pattern + ".*");
|
||||
auto filename_stem = remove_filename_extension(filename);
|
||||
if( filename_stem.empty() ) {
|
||||
filename_stem = filename_stem_pattern;
|
||||
std::replace(std::begin(filename_stem), std::end(filename_stem), '?', '0');
|
||||
std::filesystem::path next_filename_stem_matching_pattern(std::filesystem::path filename_pattern) {
|
||||
const auto next_filename = find_last_file_matching_pattern(filename_pattern.replace_extension(u".*"));
|
||||
if( next_filename.empty() ) {
|
||||
auto pattern_s = filename_pattern.replace_extension().native();
|
||||
std::replace(std::begin(pattern_s), std::end(pattern_s), '?', '0');
|
||||
return pattern_s;
|
||||
} else {
|
||||
filename_stem = increment_filename_stem_ordinal(filename_stem);
|
||||
return increment_filename_stem_ordinal(next_filename);
|
||||
}
|
||||
return filename_stem;
|
||||
}
|
||||
|
||||
std::vector<std::string> scan_root_files(const std::string& directory, const std::string& extension) {
|
||||
std::vector<std::string> file_list { };
|
||||
std::string fname;
|
||||
std::vector<std::filesystem::path> scan_root_files(const TCHAR* directory, const std::string& extension) {
|
||||
std::vector<std::filesystem::path> file_list { };
|
||||
std::filesystem::path file_path;
|
||||
FRESULT res;
|
||||
DIR dir;
|
||||
static FILINFO fno;
|
||||
|
||||
res = f_opendir(&dir, directory.c_str());
|
||||
res = f_opendir(&dir, directory);
|
||||
if (res == FR_OK) {
|
||||
for (;;) {
|
||||
res = f_readdir(&dir, &fno);
|
||||
if (res != FR_OK || fno.fname[0] == 0) break;
|
||||
fname.assign(fno.fname);
|
||||
if (fname.find(extension) != std::string::npos)
|
||||
file_list.push_back(fname);
|
||||
file_path = fno.fname;
|
||||
if (file_path.extension().string() == extension)
|
||||
file_list.push_back(file_path);
|
||||
}
|
||||
f_closedir(&dir);
|
||||
}
|
||||
|
@ -233,12 +221,67 @@ std::string filesystem_error::what() const {
|
|||
}
|
||||
}
|
||||
|
||||
path path::extension() const {
|
||||
const auto t = filename().native();
|
||||
const auto index = t.find_last_of(u'.');
|
||||
if( index == t.npos ) {
|
||||
return { };
|
||||
} else {
|
||||
return t.substr(index);
|
||||
}
|
||||
}
|
||||
|
||||
path path::filename() const {
|
||||
const auto index = _s.find_last_of(preferred_separator);
|
||||
if( index == _s.npos ) {
|
||||
return _s;
|
||||
} else {
|
||||
return _s.substr(index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
path path::stem() const {
|
||||
const auto t = filename().native();
|
||||
const auto index = t.find_last_of(u'.');
|
||||
if( index == t.npos ) {
|
||||
return t;
|
||||
} else {
|
||||
return t.substr(0, index);
|
||||
}
|
||||
}
|
||||
|
||||
std::string path::string() const {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<path::value_type>, path::value_type> conv;
|
||||
return conv.to_bytes(native());
|
||||
}
|
||||
|
||||
path& path::replace_extension(const path& replacement) {
|
||||
const auto t = extension().native();
|
||||
_s.erase(_s.size() - t.size());
|
||||
if( !replacement._s.empty() ) {
|
||||
if( replacement._s.front() != u'.' ) {
|
||||
_s += u'.';
|
||||
}
|
||||
_s += replacement._s;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator<(const path& lhs, const path& rhs) {
|
||||
return lhs.native() < rhs.native();
|
||||
}
|
||||
|
||||
bool operator>(const path& lhs, const path& rhs) {
|
||||
return lhs.native() > rhs.native();
|
||||
}
|
||||
|
||||
directory_iterator::directory_iterator(
|
||||
const char* path,
|
||||
const char* wild
|
||||
) {
|
||||
std::filesystem::path path,
|
||||
std::filesystem::path wild
|
||||
) : pattern { wild }
|
||||
{
|
||||
impl = std::make_shared<Impl>();
|
||||
const auto result = f_findfirst(&impl->dir, &impl->filinfo, path, wild);
|
||||
const auto result = f_findfirst(&impl->dir, &impl->filinfo, reinterpret_cast<const TCHAR*>(path.c_str()), reinterpret_cast<const TCHAR*>(pattern.c_str()));
|
||||
if( result != FR_OK ) {
|
||||
impl.reset();
|
||||
// TODO: Throw exception if/when I enable exceptions...
|
||||
|
@ -253,6 +296,10 @@ directory_iterator& directory_iterator::operator++() {
|
|||
return *this;
|
||||
}
|
||||
|
||||
bool is_directory(const file_status s) {
|
||||
return (s & AM_DIR);
|
||||
}
|
||||
|
||||
bool is_regular_file(const file_status s) {
|
||||
return !(s & AM_DIR);
|
||||
}
|
||||
|
@ -260,7 +307,7 @@ bool is_regular_file(const file_status s) {
|
|||
space_info space(const path& p) {
|
||||
DWORD free_clusters { 0 };
|
||||
FATFS* fs;
|
||||
if( f_getfree(p.c_str(), &free_clusters, &fs) == FR_OK ) {
|
||||
if( f_getfree(reinterpret_cast<const TCHAR*>(p.c_str()), &free_clusters, &fs) == FR_OK ) {
|
||||
#if _MAX_SS != _MIN_SS
|
||||
static_assert(false, "FatFs not configured for fixed sector size");
|
||||
#else
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue