FatFs: Enable long file name support.

Lots of re-plumbing to make this work, including a bunch of Unicode stuff now in the binary. Bloat City, I'm sure.

TODO: FatFs using unsigned (uint16_t) for UTF16 representation is kinda inconvenient. Lots of reinterpret_cast<>().
This commit is contained in:
Jared Boone 2016-08-21 18:06:39 -07:00
parent 43a11ba048
commit f7bfde73b6
20 changed files with 68 additions and 58 deletions

View File

@ -331,7 +331,7 @@ AISAppView::AISAppView(NavigationView&) {
logger = std::make_unique<AISLogger>();
if( logger ) {
logger->append("ais.txt");
logger->append(u"ais.txt");
}
}

View File

@ -95,7 +95,7 @@ using AISRecentEntries = RecentEntries<ais::Packet, AISRecentEntry>;
class AISLogger {
public:
Optional<File::Error> append(const std::string& filename) {
Optional<File::Error> append(const std::filesystem::path& filename) {
return log_file.append(filename);
}

View File

@ -143,7 +143,7 @@ private:
RecordView record_view {
{ 0 * 8, 2 * 16, 30 * 8, 1 * 16 },
"AUD_????", RecordView::FileType::WAV, 4096, 4
u"AUD_????", RecordView::FileType::WAV, 4096, 4
};
spectrum::WaterfallWidget waterfall;

View File

@ -89,7 +89,7 @@ private:
RecordView record_view {
{ 0 * 8, 1 * 16, 30 * 8, 1 * 16 },
"BBD_????", RecordView::FileType::RawS16, 16384, 3
u"BBD_????", RecordView::FileType::RawS16, 16384, 3
};
spectrum::WaterfallWidget waterfall;

View File

@ -146,7 +146,7 @@ ERTAppView::ERTAppView(NavigationView&) {
logger = std::make_unique<ERTLogger>();
if( logger ) {
logger->append("ert.txt");
logger->append(u"ert.txt");
}
}

View File

@ -90,7 +90,7 @@ struct ERTRecentEntry {
class ERTLogger {
public:
Optional<File::Error> append(const std::string& filename) {
Optional<File::Error> append(const std::filesystem::path& filename) {
return log_file.append(filename);
}

View File

@ -101,7 +101,7 @@
*/
#define _USE_LFN 0
#define _USE_LFN 2
#define _MAX_LFN 255
/* The _USE_LFN switches the support of long file name (LFN).
/
@ -119,7 +119,7 @@
/ ff_memfree(), must be added to the project. */
#define _LFN_UNICODE 0
#define _LFN_UNICODE 1
/* This option switches character encoding on the API. (0:ANSI/OEM or 1:UTF-16)
/ To use Unicode string for the path name, enable LFN and set _LFN_UNICODE = 1.
/ This option also affects behavior of string I/O functions. */

View File

@ -30,8 +30,8 @@ static_assert(sizeof(FIL::err) == 1, "FatFs FIL::err size not expected.");
#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));
@ -48,15 +48,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);
}
@ -124,9 +124,9 @@ 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.c_str())) {
if( std::filesystem::is_regular_file(entry.status()) ) {
const auto match = entry.path();
if( match > last_match ) {
@ -137,13 +137,13 @@ static std::string find_last_file_matching_pattern(const std::string& pattern) {
return last_match;
}
static std::string remove_filename_extension(const std::string& filename) {
static std::filesystem::path remove_filename_extension(const std::filesystem::path& 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 };
static std::filesystem::path increment_filename_stem_ordinal(const std::filesystem::path& filename_stem) {
std::filesystem::path result { filename_stem };
auto it = result.rbegin();
@ -165,8 +165,8 @@ static std::string increment_filename_stem_ordinal(const std::string& filename_s
return result;
}
std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern) {
const auto filename = find_last_file_matching_pattern(filename_stem_pattern + ".*");
std::filesystem::path next_filename_stem_matching_pattern(const std::filesystem::path& filename_stem_pattern) {
const auto filename = find_last_file_matching_pattern(filename_stem_pattern + u".*");
auto filename_stem = remove_filename_extension(filename);
if( filename_stem.empty() ) {
filename_stem = filename_stem_pattern;
@ -211,11 +211,11 @@ std::string filesystem_error::what() const {
}
directory_iterator::directory_iterator(
const char* path,
const char* wild
const std::filesystem::path::value_type* path,
const std::filesystem::path::value_type* 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), reinterpret_cast<const TCHAR*>(wild));
if( result != FR_OK ) {
impl.reset();
// TODO: Throw exception if/when I enable exceptions...
@ -237,7 +237,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

View File

@ -33,8 +33,6 @@
#include <memory>
#include <iterator>
std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern);
namespace std {
namespace filesystem {
@ -66,9 +64,12 @@ private:
uint32_t err;
};
using path = std::string;
using path = std::u16string;
using file_status = BYTE;
static_assert(sizeof(path::value_type) == 2, "sizeof(std::filesystem::path::value_type) != 2");
static_assert(sizeof(path::value_type) == sizeof(TCHAR), "FatFs TCHAR size != std::filesystem::path::value_type");
struct space_info {
static_assert(sizeof(std::uintmax_t) >= 8, "std::uintmax_t too small (<uint64_t)");
@ -82,7 +83,7 @@ struct directory_entry : public FILINFO {
return fattrib;
}
const std::string path() const noexcept { return fname; };
const std::filesystem::path path() const noexcept { return reinterpret_cast<const std::filesystem::path::value_type*>(fname); };
};
class directory_iterator {
@ -107,7 +108,7 @@ public:
using iterator_category = std::input_iterator_tag;
directory_iterator() noexcept { };
directory_iterator(const char* path, const char* wild);
directory_iterator(const std::filesystem::path::value_type* path, const std::filesystem::path::value_type* wild);
~directory_iterator() { }
@ -131,6 +132,8 @@ space_info space(const path& p);
} /* namespace filesystem */
} /* namespace std */
std::filesystem::path next_filename_stem_matching_pattern(const std::filesystem::path& filename_stem_pattern);
class File {
public:
using Error = std::filesystem::filesystem_error;
@ -193,9 +196,9 @@ public:
File& operator=(const File&) = delete;
// TODO: Return Result<>.
Optional<Error> open(const std::string& filename);
Optional<Error> append(const std::string& filename);
Optional<Error> create(const std::string& filename);
Optional<Error> open(const std::filesystem::path& filename);
Optional<Error> append(const std::filesystem::path& filename);
Optional<Error> create(const std::filesystem::path& filename);
Result<size_t> read(void* const data, const size_t bytes_to_read);
Result<size_t> write(const void* const data, const size_t bytes_to_write);
@ -215,7 +218,7 @@ public:
private:
FIL f;
Optional<Error> open_fatfs(const std::string& filename, BYTE mode);
Optional<Error> open_fatfs(const std::filesystem::path& filename, BYTE mode);
};
#endif/*__FILE_H__*/

View File

@ -31,7 +31,7 @@ using namespace lpc43xx;
class LogFile {
public:
Optional<File::Error> append(const std::string& filename) {
Optional<File::Error> append(const std::filesystem::path& filename) {
return file.append(filename);
}

View File

@ -36,7 +36,7 @@ Status status_ { Status::NotPresent };
FATFS fs;
FRESULT mount() {
return f_mount(&fs, "", 0);
return f_mount(&fs, reinterpret_cast<const TCHAR*>(_T("")), 0);
}
} /* namespace */

View File

@ -193,7 +193,7 @@ TPMSAppView::TPMSAppView(NavigationView&) {
logger = std::make_unique<TPMSLogger>();
if( logger ) {
logger->append("tpms.txt");
logger->append(u"tpms.txt");
}
}

View File

@ -76,7 +76,7 @@ using TPMSRecentEntries = RecentEntries<tpms::Reading, TPMSRecentEntry>;
class TPMSLogger {
public:
Optional<File::Error> append(const std::string& filename) {
Optional<File::Error> append(const std::filesystem::path& filename) {
return log_file.append(filename);
}

View File

@ -81,13 +81,13 @@ void SystemStatusView::set_title(const std::string new_value) {
}
void SystemStatusView::on_camera() {
const auto filename_stem = next_filename_stem_matching_pattern("SCR_????");
const auto filename_stem = next_filename_stem_matching_pattern(u"SCR_????");
if( filename_stem.empty() ) {
return;
}
PNGWriter png;
auto create_error = png.create(filename_stem + ".PNG");
auto create_error = png.create(filename_stem + u".PNG");
if( create_error.is_valid() ) {
return;
}

View File

@ -32,6 +32,9 @@ using namespace portapack;
#include <cstdint>
#include <locale>
#include <codecvt>
class FileWriter : public Writer {
public:
FileWriter() = default;
@ -41,7 +44,7 @@ public:
FileWriter(FileWriter&& file) = delete;
FileWriter& operator=(FileWriter&&) = delete;
Optional<File::Error> create(const std::string& filename) {
Optional<File::Error> create(const std::filesystem::path& filename) {
return file.create(filename);
}
@ -79,7 +82,7 @@ public:
}
Optional<File::Error> create(
const std::string& filename
const std::filesystem::path& filename
) {
const auto create_error = FileWriter::create(filename);
if( create_error.is_valid() ) {
@ -164,7 +167,7 @@ namespace ui {
RecordView::RecordView(
const Rect parent_rect,
std::string filename_stem_pattern,
std::filesystem::path filename_stem_pattern,
const FileType file_type,
const size_t write_size,
const size_t buffer_count
@ -251,7 +254,7 @@ void RecordView::start() {
sampling_rate
);
auto create_error = p->create(
filename_stem + ".WAV"
filename_stem + u".WAV"
);
if( create_error.is_valid() ) {
handle_error(create_error.value());
@ -263,7 +266,7 @@ void RecordView::start() {
case FileType::RawS16:
{
const auto metadata_file_error = write_metadata_file(filename_stem + ".TXT");
const auto metadata_file_error = write_metadata_file(filename_stem + u".TXT");
if( metadata_file_error.is_valid() ) {
handle_error(metadata_file_error.value());
return;
@ -271,7 +274,7 @@ void RecordView::start() {
auto p = std::make_unique<RawFileWriter>();
auto create_error = p->create(
filename_stem + ".C16"
filename_stem + u".C16"
);
if( create_error.is_valid() ) {
handle_error(create_error.value());
@ -286,7 +289,10 @@ void RecordView::start() {
};
if( writer ) {
text_record_filename.set(filename_stem);
std::wstring_convert<std::codecvt_utf8_utf16<std::filesystem::path::value_type>, std::filesystem::path::value_type> conv;
const auto filename_stem_s = conv.to_bytes(filename_stem);
text_record_filename.set(filename_stem_s);
button_record.set_bitmap(&bitmap_stop);
capture_thread = std::make_unique<CaptureThread>(
std::move(writer),
@ -314,7 +320,7 @@ void RecordView::stop() {
update_status_display();
}
Optional<File::Error> RecordView::write_metadata_file(const std::string& filename) {
Optional<File::Error> RecordView::write_metadata_file(const std::filesystem::path& filename) {
File file;
const auto create_error = file.create(filename);
if( create_error.is_valid() ) {
@ -344,7 +350,7 @@ void RecordView::update_status_display() {
}
if( sampling_rate ) {
const auto space_info = std::filesystem::space("");
const auto space_info = std::filesystem::space(u"");
const uint32_t bytes_per_second = file_type == FileType::WAV ? (sampling_rate * 2) : (sampling_rate * 4);
const uint32_t available_seconds = space_info.free / bytes_per_second;
const uint32_t seconds = available_seconds % 60;

View File

@ -46,7 +46,7 @@ public:
RecordView(
const Rect parent_rect,
std::string filename_stem_pattern,
std::filesystem::path filename_stem_pattern,
FileType file_type,
const size_t write_size,
const size_t buffer_count
@ -64,7 +64,7 @@ public:
private:
void toggle();
Optional<File::Error> write_metadata_file(const std::string& filename);
Optional<File::Error> write_metadata_file(const std::filesystem::path& filename);
void on_tick_second();
void update_status_display();
@ -72,7 +72,7 @@ private:
void handle_capture_thread_done(const File::Error error);
void handle_error(const File::Error error);
const std::string filename_stem_pattern;
const std::filesystem::path filename_stem_pattern;
const FileType file_type;
const size_t write_size;
const size_t buffer_count;

View File

@ -95,7 +95,7 @@ private:
}
Result run() {
const std::string filename { "_PPTEST_.DAT" };
const std::filesystem::path filename { u"_PPTEST_.DAT" };
const auto write_result = write(filename);
if( write_result != Result::OK ) {
@ -115,7 +115,7 @@ private:
return read_result;
}
f_unlink(filename.c_str());
f_unlink(reinterpret_cast<const TCHAR*>(filename.c_str()));
if( _stats.read_bytes < bytes_to_read ) {
return Result::FailReadIncomplete;
@ -128,7 +128,7 @@ private:
return Result::OK;
}
Result write(const std::string& filename) {
Result write(const std::filesystem::path& filename) {
const auto buffer = std::make_unique<std::array<uint8_t, write_size>>();
if( !buffer ) {
return Result::FailHeap;
@ -175,7 +175,7 @@ private:
return Result::OK;
}
Result read(const std::string& filename) {
Result read(const std::filesystem::path& filename) {
const auto buffer = std::make_unique<std::array<uint8_t, write_size>>();
if( !buffer ) {
return Result::FailHeap;

View File

@ -3,6 +3,7 @@ set(FATFSSRC
${CHIBIOS_PORTAPACK}/os/various/fatfs_bindings/fatfs_diskio.c
${CHIBIOS_PORTAPACK}/os/various/fatfs_bindings/fatfs_syscall.c
${CHIBIOS_PORTAPACK}/ext/fatfs/src/ff.c
${CHIBIOS_PORTAPACK}/ext/fatfs/src/option/unicode.c
)
set(FATFSINC

View File

@ -50,7 +50,7 @@ static constexpr std::array<uint8_t, 12> png_iend { {
} };
Optional<File::Error> PNGWriter::create(
const std::string& filename
const std::filesystem::path& filename
) {
const auto create_error = file.create(filename);
if( create_error.is_valid() ) {

View File

@ -35,7 +35,7 @@ class PNGWriter {
public:
~PNGWriter();
Optional<File::Error> create(const std::string& filename);
Optional<File::Error> create(const std::filesystem::path& filename);
void write_scanline(const std::array<ui::ColorRGB888, 240>& scanline);