2016-01-17 17:20:02 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
2016-08-26 02:11:24 -04:00
|
|
|
* Copyright (C) 2016 Furrtek
|
2016-01-17 17:20:02 -05:00
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "file.hpp"
|
2023-07-22 03:20:56 -04:00
|
|
|
#include "complex.hpp"
|
2016-01-17 17:20:02 -05:00
|
|
|
|
2016-04-20 12:57:17 -04:00
|
|
|
#include <algorithm>
|
2016-09-08 01:20:51 -04:00
|
|
|
#include <codecvt>
|
2023-06-11 14:47:13 -04:00
|
|
|
#include <cstring>
|
|
|
|
#include <locale>
|
2016-04-20 12:57:17 -04:00
|
|
|
|
2023-07-22 03:20:56 -04:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
static const fs::path c8_ext{u".C8"};
|
|
|
|
static const fs::path c16_ext{u".C16"};
|
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
Optional<File::Error> File::open_fatfs(const std::filesystem::path& filename, BYTE mode) {
|
2023-05-18 16:16:05 -04:00
|
|
|
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));
|
|
|
|
if (result != FR_OK) {
|
|
|
|
f_close(&f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == FR_OK) {
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
return {result};
|
|
|
|
}
|
2016-05-16 17:01:44 -04:00
|
|
|
}
|
|
|
|
|
2023-07-17 14:43:37 -04:00
|
|
|
Optional<File::Error> File::open(const std::filesystem::path& filename, bool read_only, bool create) {
|
2023-06-01 18:45:55 -04:00
|
|
|
BYTE mode = read_only ? FA_READ : FA_READ | FA_WRITE;
|
2023-07-17 14:43:37 -04:00
|
|
|
if (create)
|
|
|
|
mode |= FA_OPEN_ALWAYS;
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
return open_fatfs(filename, mode);
|
2016-05-16 17:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
Optional<File::Error> File::append(const std::filesystem::path& filename) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return open_fatfs(filename, FA_WRITE | FA_OPEN_ALWAYS);
|
2016-05-16 17:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-08-21 21:06:39 -04:00
|
|
|
Optional<File::Error> File::create(const std::filesystem::path& filename) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return open_fatfs(filename, FA_WRITE | FA_CREATE_ALWAYS);
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2016-04-30 18:09:34 -04:00
|
|
|
File::~File() {
|
2023-05-18 16:16:05 -04:00
|
|
|
f_close(&f);
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2024-04-23 23:18:12 -04:00
|
|
|
void File::close() {
|
|
|
|
f_close(&f);
|
|
|
|
}
|
|
|
|
|
2023-05-28 11:44:21 -04:00
|
|
|
File::Result<File::Size> File::read(void* data, Size bytes_to_read) {
|
2023-05-18 16:16:05 -04:00
|
|
|
UINT bytes_read = 0;
|
|
|
|
const auto result = f_read(&f, data, bytes_to_read, &bytes_read);
|
|
|
|
if (result == FR_OK) {
|
|
|
|
return {static_cast<size_t>(bytes_read)};
|
|
|
|
} else {
|
|
|
|
return {static_cast<Error>(result)};
|
|
|
|
}
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2023-05-28 11:44:21 -04:00
|
|
|
File::Result<File::Size> File::write(const void* data, Size bytes_to_write) {
|
2023-05-18 16:16:05 -04:00
|
|
|
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<File::Size>(bytes_written)};
|
|
|
|
} else {
|
|
|
|
return Error{FR_DISK_FULL};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {static_cast<Error>(result)};
|
|
|
|
}
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2023-06-03 22:26:39 -04:00
|
|
|
File::Offset File::tell() const {
|
|
|
|
return f_tell(&f);
|
|
|
|
}
|
|
|
|
|
2024-04-23 23:18:12 -04:00
|
|
|
File::Result<bool> File::eof() {
|
|
|
|
return f_eof(&f);
|
|
|
|
}
|
|
|
|
|
2023-05-28 11:44:21 -04:00
|
|
|
File::Result<File::Offset> File::seek(Offset new_position) {
|
2023-05-18 16:16:05 -04:00
|
|
|
/* NOTE: Returns *old* position, not new position */
|
2023-06-03 22:26:39 -04:00
|
|
|
const auto old_position = tell();
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto result = f_lseek(&f, new_position);
|
|
|
|
if (result != FR_OK) {
|
|
|
|
return {static_cast<Error>(result)};
|
|
|
|
}
|
|
|
|
if (f_tell(&f) != new_position) {
|
|
|
|
return {static_cast<Error>(FR_BAD_SEEK)};
|
|
|
|
}
|
|
|
|
return {static_cast<File::Offset>(old_position)};
|
2016-05-01 14:29:44 -04:00
|
|
|
}
|
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
File::Result<File::Offset> File::truncate() {
|
|
|
|
const auto position = f_tell(&f);
|
|
|
|
const auto result = f_truncate(&f);
|
|
|
|
if (result != FR_OK) {
|
|
|
|
return {static_cast<Error>(result)};
|
|
|
|
}
|
|
|
|
return {static_cast<File::Offset>(position)};
|
|
|
|
}
|
|
|
|
|
2023-05-22 16:08:59 -04:00
|
|
|
File::Size File::size() const {
|
|
|
|
return f_size(&f);
|
2017-04-06 19:41:32 -04:00
|
|
|
}
|
|
|
|
|
2016-07-24 23:39:21 -04:00
|
|
|
Optional<File::Error> File::write_line(const std::string& s) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto result_s = write(s.c_str(), s.size());
|
|
|
|
if (result_s.is_error()) {
|
|
|
|
return {result_s.error()};
|
|
|
|
}
|
2016-07-24 23:39:21 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto result_crlf = write("\r\n", 2);
|
|
|
|
if (result_crlf.is_error()) {
|
|
|
|
return {result_crlf.error()};
|
|
|
|
}
|
2016-07-24 23:39:21 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
return {};
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
Optional<File::Error> File::sync() {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto result = f_sync(&f);
|
|
|
|
if (result == FR_OK) {
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
return {result};
|
|
|
|
}
|
2016-01-17 17:20:02 -05:00
|
|
|
}
|
2016-04-20 12:56:35 -04:00
|
|
|
|
2023-06-11 14:47:13 -04:00
|
|
|
File::Result<std::string> File::read_file(const std::filesystem::path& filename) {
|
|
|
|
constexpr size_t buffer_size = 0x80;
|
|
|
|
char* buffer[buffer_size];
|
|
|
|
|
|
|
|
File f;
|
|
|
|
auto error = f.open(filename);
|
|
|
|
if (error)
|
|
|
|
return *error;
|
|
|
|
|
|
|
|
std::string content;
|
|
|
|
content.resize(f.size());
|
|
|
|
auto str = &content[0];
|
|
|
|
auto total_read = 0u;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
auto read = f.read(buffer, buffer_size);
|
|
|
|
if (!read)
|
|
|
|
return read.error();
|
|
|
|
|
|
|
|
memcpy(str, buffer, *read);
|
|
|
|
str += *read;
|
|
|
|
total_read += *read;
|
|
|
|
|
|
|
|
if (*read < buffer_size)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
content.resize(total_read);
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:08:07 -04:00
|
|
|
/* Range used for filename matching.
|
|
|
|
* Start and end are inclusive positions of "???" */
|
|
|
|
struct pattern_range {
|
2023-05-18 16:16:05 -04:00
|
|
|
size_t start;
|
|
|
|
size_t end;
|
2023-05-12 14:08:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Finds the last file matching the specified pattern that
|
|
|
|
* can be automatically incremented (digits in pattern).
|
|
|
|
* NB: assumes a patten with contiguous '?' like "FOO_???.txt". */
|
|
|
|
static std::filesystem::path find_last_ordinal_match(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& folder,
|
|
|
|
const std::filesystem::path& pattern,
|
|
|
|
pattern_range range) {
|
|
|
|
auto last_match = std::filesystem::path();
|
|
|
|
auto can_increment = [range](const auto& path) {
|
|
|
|
for (auto i = range.start; i <= range.end; ++i)
|
|
|
|
if (!isdigit(path.native()[i]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(folder, pattern)) {
|
|
|
|
if (std::filesystem::is_regular_file(entry.status()) && can_increment(entry.path())) {
|
|
|
|
const auto& match = entry.path();
|
|
|
|
if (match > last_match) {
|
|
|
|
last_match = match;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return last_match;
|
2016-04-20 12:57:17 -04:00
|
|
|
}
|
|
|
|
|
2023-05-12 14:08:07 -04:00
|
|
|
/* Given a file path like "FOO_0001.txt" increment it to "FOO_0002.txt". */
|
|
|
|
static std::filesystem::path increment_filename_ordinal(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& path,
|
|
|
|
pattern_range range) {
|
|
|
|
auto name = path.filename().native();
|
2016-04-20 12:57:17 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
for (auto i = range.end; i >= range.start; --i) {
|
|
|
|
auto& c = name[i];
|
2023-05-12 14:08:07 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
// Not a digit or would overflow the counter.
|
|
|
|
if (c < u'0' || c > u'9' || (c == u'9' && i == range.start))
|
|
|
|
return {};
|
2023-05-12 14:08:07 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
if (c == u'9')
|
|
|
|
c = '0';
|
|
|
|
else {
|
|
|
|
c++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 12:57:17 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
return {name};
|
2016-04-20 12:57:17 -04:00
|
|
|
}
|
|
|
|
|
2023-05-12 14:08:07 -04:00
|
|
|
std::filesystem::path next_filename_matching_pattern(const std::filesystem::path& filename_pattern) {
|
2023-05-18 16:16:05 -04:00
|
|
|
auto path = filename_pattern.parent_path();
|
|
|
|
auto pattern = filename_pattern.filename();
|
|
|
|
auto range = pattern_range{
|
|
|
|
pattern.native().find_first_of(u'?'),
|
|
|
|
pattern.native().find_last_of(u'?')};
|
2023-05-12 14:08:07 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto match = find_last_ordinal_match(path, pattern, range);
|
2023-05-12 14:08:07 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
if (match.empty()) {
|
|
|
|
auto pattern_str = pattern.native();
|
|
|
|
for (auto i = range.start; i <= range.end; ++i)
|
|
|
|
pattern_str[i] = u'0';
|
|
|
|
return path / pattern_str;
|
|
|
|
}
|
2023-05-12 14:08:07 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
auto next_name = increment_filename_ordinal(match, range);
|
|
|
|
return next_name.empty() ? next_name : path / next_name;
|
2016-04-20 12:57:17 -04:00
|
|
|
}
|
|
|
|
|
2017-02-01 18:31:16 -05:00
|
|
|
std::vector<std::filesystem::path> scan_root_files(const std::filesystem::path& directory,
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& extension) {
|
|
|
|
std::vector<std::filesystem::path> file_list{};
|
2023-07-11 16:48:36 -04:00
|
|
|
scan_root_files(directory, extension, [&file_list](const std::filesystem::path& p) {
|
|
|
|
file_list.push_back(p);
|
|
|
|
});
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
return file_list;
|
2016-08-26 02:11:24 -04:00
|
|
|
}
|
|
|
|
|
2017-03-21 23:21:06 -04:00
|
|
|
std::vector<std::filesystem::path> scan_root_directories(const std::filesystem::path& directory) {
|
2023-05-18 16:16:05 -04:00
|
|
|
std::vector<std::filesystem::path> directory_list{};
|
|
|
|
|
|
|
|
for (const auto& entry : std::filesystem::directory_iterator(directory, "*")) {
|
|
|
|
if (std::filesystem::is_directory(entry.status())) {
|
|
|
|
directory_list.push_back(entry.path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return directory_list;
|
2017-03-21 23:21:06 -04:00
|
|
|
}
|
|
|
|
|
2023-05-11 16:46:38 -04:00
|
|
|
std::filesystem::filesystem_error delete_file(const std::filesystem::path& file_path) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return {f_unlink(reinterpret_cast<const TCHAR*>(file_path.c_str()))};
|
2017-09-19 15:14:56 -04:00
|
|
|
}
|
|
|
|
|
2023-05-11 16:46:38 -04:00
|
|
|
std::filesystem::filesystem_error rename_file(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& file_path,
|
|
|
|
const std::filesystem::path& new_name) {
|
|
|
|
return {f_rename(reinterpret_cast<const TCHAR*>(file_path.c_str()), reinterpret_cast<const TCHAR*>(new_name.c_str()))};
|
2017-09-19 15:14:56 -04:00
|
|
|
}
|
|
|
|
|
2023-05-10 12:51:09 -04:00
|
|
|
std::filesystem::filesystem_error copy_file(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& file_path,
|
|
|
|
const std::filesystem::path& dest_path) {
|
2023-09-21 11:43:10 -04:00
|
|
|
constexpr size_t buffer_size = std::filesystem::max_file_block_size;
|
2023-05-26 04:02:17 -04:00
|
|
|
uint8_t buffer[buffer_size];
|
2023-05-18 16:16:05 -04:00
|
|
|
File src;
|
|
|
|
File dst;
|
2023-05-10 12:51:09 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
auto error = src.open(file_path);
|
2023-06-01 18:45:55 -04:00
|
|
|
if (error) return error.value();
|
2023-05-10 12:51:09 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
error = dst.create(dest_path);
|
2023-06-01 18:45:55 -04:00
|
|
|
if (error) return error.value();
|
2023-05-10 12:51:09 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
while (true) {
|
|
|
|
auto result = src.read(buffer, buffer_size);
|
|
|
|
if (result.is_error()) return result.error();
|
2023-05-10 12:51:09 -04:00
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
result = dst.write(buffer, *result);
|
2023-05-18 16:16:05 -04:00
|
|
|
if (result.is_error()) return result.error();
|
2023-05-10 12:51:09 -04:00
|
|
|
|
2023-06-01 18:45:55 -04:00
|
|
|
if (*result < buffer_size)
|
2023-05-18 16:16:05 -04:00
|
|
|
break;
|
|
|
|
}
|
2023-05-10 12:51:09 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
return {};
|
2023-05-10 12:51:09 -04:00
|
|
|
}
|
|
|
|
|
2018-01-09 16:12:19 -05:00
|
|
|
FATTimestamp file_created_date(const std::filesystem::path& file_path) {
|
2023-05-18 16:16:05 -04:00
|
|
|
FILINFO filinfo;
|
|
|
|
|
|
|
|
f_stat(reinterpret_cast<const TCHAR*>(file_path.c_str()), &filinfo);
|
|
|
|
|
|
|
|
return {filinfo.fdate, filinfo.ftime};
|
2018-01-09 16:12:19 -05:00
|
|
|
}
|
|
|
|
|
2023-10-09 06:04:46 -04:00
|
|
|
std::filesystem::filesystem_error file_update_date(const std::filesystem::path& file_path, FATTimestamp timestamp) {
|
|
|
|
FILINFO filinfo{};
|
|
|
|
|
|
|
|
filinfo.fdate = timestamp.FAT_date;
|
|
|
|
filinfo.ftime = timestamp.FAT_time;
|
|
|
|
return f_utime(reinterpret_cast<const TCHAR*>(file_path.c_str()), &filinfo);
|
|
|
|
}
|
|
|
|
|
2023-05-11 16:46:38 -04:00
|
|
|
std::filesystem::filesystem_error make_new_file(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& file_path) {
|
|
|
|
File f;
|
2023-07-11 16:48:36 -04:00
|
|
|
auto error = f.create(file_path);
|
|
|
|
if (error)
|
|
|
|
return *error;
|
|
|
|
|
|
|
|
return {};
|
2023-05-11 16:46:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::filesystem::filesystem_error make_new_directory(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& dir_path) {
|
|
|
|
return {f_mkdir(reinterpret_cast<const TCHAR*>(dir_path.c_str()))};
|
2017-09-19 15:14:56 -04:00
|
|
|
}
|
|
|
|
|
2023-05-12 14:08:07 -04:00
|
|
|
std::filesystem::filesystem_error ensure_directory(
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::filesystem::path& dir_path) {
|
|
|
|
if (dir_path.empty() || std::filesystem::file_exists(dir_path))
|
|
|
|
return {};
|
2023-05-12 14:08:07 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
auto result = ensure_directory(dir_path.parent_path());
|
|
|
|
if (result.code())
|
|
|
|
return result;
|
2023-05-12 14:08:07 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
return make_new_directory(dir_path);
|
2023-05-12 14:08:07 -04:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
namespace std {
|
|
|
|
namespace filesystem {
|
|
|
|
|
2016-05-12 21:19:28 -04:00
|
|
|
std::string filesystem_error::what() const {
|
2023-05-18 16:16:05 -04:00
|
|
|
switch (err) {
|
|
|
|
case FR_OK:
|
|
|
|
return "ok";
|
|
|
|
case FR_DISK_ERR:
|
|
|
|
return "disk error";
|
|
|
|
case FR_INT_ERR:
|
|
|
|
return "insanity detected";
|
|
|
|
case FR_NOT_READY:
|
|
|
|
return "SD card not ready";
|
|
|
|
case FR_NO_FILE:
|
|
|
|
return "no file";
|
|
|
|
case FR_NO_PATH:
|
|
|
|
return "no path";
|
|
|
|
case FR_INVALID_NAME:
|
|
|
|
return "invalid name";
|
|
|
|
case FR_DENIED:
|
|
|
|
return "denied";
|
|
|
|
case FR_EXIST:
|
|
|
|
return "exists";
|
|
|
|
case FR_INVALID_OBJECT:
|
|
|
|
return "invalid object";
|
|
|
|
case FR_WRITE_PROTECTED:
|
|
|
|
return "write protected";
|
|
|
|
case FR_INVALID_DRIVE:
|
|
|
|
return "invalid drive";
|
|
|
|
case FR_NOT_ENABLED:
|
|
|
|
return "not enabled";
|
|
|
|
case FR_NO_FILESYSTEM:
|
|
|
|
return "no filesystem";
|
|
|
|
case FR_MKFS_ABORTED:
|
|
|
|
return "mkfs aborted";
|
|
|
|
case FR_TIMEOUT:
|
|
|
|
return "timeout";
|
|
|
|
case FR_LOCKED:
|
|
|
|
return "locked";
|
|
|
|
case FR_NOT_ENOUGH_CORE:
|
|
|
|
return "not enough core";
|
|
|
|
case FR_TOO_MANY_OPEN_FILES:
|
|
|
|
return "too many open files";
|
|
|
|
case FR_INVALID_PARAMETER:
|
|
|
|
return "invalid parameter";
|
|
|
|
case FR_EOF:
|
|
|
|
return "end of file";
|
|
|
|
case FR_DISK_FULL:
|
|
|
|
return "disk full";
|
|
|
|
case FR_BAD_SEEK:
|
|
|
|
return "bad seek";
|
|
|
|
case FR_UNEXPECTED:
|
|
|
|
return "unexpected";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
2016-05-12 21:19:28 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 01:42:28 -04:00
|
|
|
path path::parent_path() const {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto index = _s.find_last_of(preferred_separator);
|
|
|
|
if (index == _s.npos) {
|
|
|
|
return {}; // NB: Deviation from STL.
|
|
|
|
} else {
|
|
|
|
return _s.substr(0, index);
|
|
|
|
}
|
2023-05-01 01:42:28 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
path path::extension() const {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto t = filename().native();
|
|
|
|
const auto index = t.find_last_of(u'.');
|
|
|
|
if (index == t.npos) {
|
|
|
|
return {};
|
|
|
|
} else {
|
|
|
|
return t.substr(index);
|
|
|
|
}
|
2016-09-08 15:57:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
path path::filename() const {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto index = _s.find_last_of(preferred_separator);
|
|
|
|
if (index == _s.npos) {
|
|
|
|
return _s;
|
|
|
|
} else {
|
|
|
|
return _s.substr(index + 1);
|
|
|
|
}
|
2016-09-08 15:57:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
path path::stem() const {
|
2023-05-18 16:16:05 -04:00
|
|
|
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);
|
|
|
|
}
|
2016-09-08 15:57:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string path::string() const {
|
2023-05-18 16:16:05 -04:00
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<path::value_type>, path::value_type> conv;
|
|
|
|
return conv.to_bytes(native());
|
2016-09-08 15:57:34 -04:00
|
|
|
}
|
|
|
|
|
2024-03-15 09:56:20 -04:00
|
|
|
// appends a string to the end of filename, but leaves the extension asd.txt + "fg" -> asdfg.txt
|
|
|
|
path& path::append_filename(const string_type& str) {
|
|
|
|
const auto t = extension().native();
|
|
|
|
_s.erase(_s.size() - t.size()); // remove extension
|
|
|
|
_s += str; // append string
|
|
|
|
_s += t; // add back extension
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
path& path::replace_extension(const path& replacement) {
|
2023-05-18 16:16:05 -04:00
|
|
|
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;
|
2016-09-08 15:57:34 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 01:42:28 -04:00
|
|
|
bool operator==(const path& lhs, const path& rhs) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return lhs.native() == rhs.native();
|
2023-05-01 01:42:28 -04:00
|
|
|
}
|
|
|
|
|
2023-05-03 16:13:15 -04:00
|
|
|
bool operator!=(const path& lhs, const path& rhs) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return !(lhs == rhs);
|
2023-05-03 16:13:15 -04:00
|
|
|
}
|
|
|
|
|
2016-10-01 13:44:11 -04:00
|
|
|
bool operator<(const path& lhs, const path& rhs) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return lhs.native() < rhs.native();
|
2016-10-01 13:44:11 -04:00
|
|
|
}
|
|
|
|
|
2016-09-08 15:57:34 -04:00
|
|
|
bool operator>(const path& lhs, const path& rhs) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return lhs.native() > rhs.native();
|
2016-09-08 01:20:51 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 12:25:32 -04:00
|
|
|
path operator+(const path& lhs, const path& rhs) {
|
2023-05-18 16:16:05 -04:00
|
|
|
path result = lhs;
|
|
|
|
result += rhs;
|
|
|
|
return result;
|
2023-05-01 12:25:32 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 01:42:28 -04:00
|
|
|
path operator/(const path& lhs, const path& rhs) {
|
2023-05-18 16:16:05 -04:00
|
|
|
path result = lhs;
|
|
|
|
result /= rhs;
|
|
|
|
return result;
|
2023-05-01 01:42:28 -04:00
|
|
|
}
|
|
|
|
|
2023-06-28 13:02:06 -04:00
|
|
|
bool path_iequal(
|
|
|
|
const path& lhs,
|
|
|
|
const path& rhs) {
|
|
|
|
const auto& lhs_str = lhs.native();
|
|
|
|
const auto& rhs_str = rhs.native();
|
|
|
|
|
|
|
|
// NB: Not correct for Unicode/locales.
|
|
|
|
if (lhs_str.length() == rhs_str.length()) {
|
|
|
|
for (size_t i = 0; i < lhs_str.length(); ++i)
|
|
|
|
if (towupper(lhs_str[i]) != towupper(rhs_str[i]))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:20:56 -04:00
|
|
|
bool is_cxx_capture_file(const path& filename) {
|
|
|
|
auto ext = filename.extension();
|
2023-07-23 02:52:23 -04:00
|
|
|
return path_iequal(c8_ext, ext) || path_iequal(c16_ext, ext);
|
2023-07-22 03:20:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t capture_file_sample_size(const path& filename) {
|
|
|
|
if (path_iequal(filename.extension(), c8_ext))
|
|
|
|
return sizeof(complex8_t);
|
|
|
|
if (path_iequal(filename.extension(), c16_ext))
|
|
|
|
return sizeof(complex16_t);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
directory_iterator::directory_iterator(
|
2023-07-11 16:48:36 -04:00
|
|
|
const std::filesystem::path& path,
|
|
|
|
const std::filesystem::path& wild)
|
|
|
|
: path_{path}, wild_{wild} {
|
2023-05-18 16:16:05 -04:00
|
|
|
impl = std::make_shared<Impl>();
|
2023-07-11 16:48:36 -04:00
|
|
|
auto result = f_findfirst(&impl->dir, &impl->filinfo,
|
|
|
|
path_.tchar(), wild_.tchar());
|
2023-05-18 16:16:05 -04:00
|
|
|
if (result != FR_OK || impl->filinfo.fname[0] == (TCHAR)'\0') {
|
|
|
|
impl.reset();
|
|
|
|
// TODO: Throw exception if/when I enable exceptions...
|
|
|
|
}
|
2016-04-20 12:56:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
directory_iterator& directory_iterator::operator++() {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto result = f_findnext(&impl->dir, &impl->filinfo);
|
|
|
|
if ((result != FR_OK) || (impl->filinfo.fname[0] == 0)) {
|
|
|
|
impl.reset();
|
|
|
|
}
|
|
|
|
return *this;
|
2016-04-20 12:56:35 -04:00
|
|
|
}
|
|
|
|
|
2016-10-01 13:44:11 -04:00
|
|
|
bool is_directory(const file_status s) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return (s & AM_DIR);
|
2016-10-01 13:44:11 -04:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
bool is_regular_file(const file_status s) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return !(s & AM_DIR);
|
2016-04-20 12:56:35 -04:00
|
|
|
}
|
|
|
|
|
2023-05-03 16:13:15 -04:00
|
|
|
bool file_exists(const path& file_path) {
|
2023-05-18 16:16:05 -04:00
|
|
|
FILINFO filinfo;
|
|
|
|
auto fr = f_stat(reinterpret_cast<const TCHAR*>(file_path.c_str()), &filinfo);
|
|
|
|
|
|
|
|
return fr == FR_OK;
|
2023-05-03 16:13:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_directory(const path& file_path) {
|
2023-05-18 16:16:05 -04:00
|
|
|
FILINFO filinfo;
|
|
|
|
auto fr = f_stat(reinterpret_cast<const TCHAR*>(file_path.c_str()), &filinfo);
|
|
|
|
|
|
|
|
return fr == FR_OK && is_directory(static_cast<file_status>(filinfo.fattrib));
|
2023-05-03 16:13:15 -04:00
|
|
|
}
|
|
|
|
|
2023-07-30 16:46:59 -04:00
|
|
|
bool is_empty_directory(const path& file_path) {
|
|
|
|
DIR dir;
|
|
|
|
FILINFO filinfo;
|
|
|
|
|
|
|
|
if (!is_directory(file_path))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto result = f_findfirst(&dir, &filinfo, reinterpret_cast<const TCHAR*>(file_path.c_str()), (const TCHAR*)u"*");
|
|
|
|
return !((result == FR_OK) && (filinfo.fname[0] != (TCHAR)'\0'));
|
|
|
|
}
|
|
|
|
|
2023-08-16 04:00:46 -04:00
|
|
|
int file_count(const path& directory) {
|
|
|
|
int count{0};
|
|
|
|
|
|
|
|
for (auto& entry : std::filesystem::directory_iterator(directory, (const TCHAR*)u"*")) {
|
|
|
|
(void)entry; // avoid unused warning
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:58:57 -04:00
|
|
|
space_info space(const path& p) {
|
2023-05-18 16:16:05 -04:00
|
|
|
DWORD free_clusters{0};
|
|
|
|
FATFS* fs;
|
|
|
|
if (f_getfree(reinterpret_cast<const TCHAR*>(p.c_str()), &free_clusters, &fs) == FR_OK) {
|
2016-05-11 13:58:57 -04:00
|
|
|
#if _MAX_SS != _MIN_SS
|
2023-05-18 16:16:05 -04:00
|
|
|
static_assert(false, "FatFs not configured for fixed sector size");
|
2016-05-11 13:58:57 -04:00
|
|
|
#else
|
2023-05-18 16:16:05 -04:00
|
|
|
const std::uintmax_t cluster_bytes = fs->csize * _MIN_SS;
|
|
|
|
return {
|
|
|
|
(fs->n_fatent - 2) * cluster_bytes,
|
|
|
|
free_clusters * cluster_bytes,
|
|
|
|
free_clusters * cluster_bytes,
|
|
|
|
};
|
2016-05-11 13:58:57 -04:00
|
|
|
#endif
|
2023-05-18 16:16:05 -04:00
|
|
|
} else {
|
|
|
|
return {0, 0, 0};
|
|
|
|
}
|
2016-05-11 13:58:57 -04:00
|
|
|
}
|
|
|
|
|
2016-04-20 12:56:35 -04:00
|
|
|
} /* namespace filesystem */
|
|
|
|
} /* namespace std */
|