mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2025-08-14 08:07:37 +00:00
Add edit support for Notepad (#1093)
* WIP file editing * WIP file editing * Add "on_pop" handler to navigation. * WIP Editing * WIP for draft * Fix mock and unit tests, support +newline at end. * Clean up Painter API and use string_view * Fix optional rvalue functions * Fix Result 'take' to be more standard * FileWrapper stack buffer reads * Grasping at straws * Nit * Move set_on_pop impl to cpp * Workaround "Open" when file not dirty. --------- Co-authored-by: kallanreed <kallanreed@outlook.com>
This commit is contained in:
@@ -44,8 +44,9 @@ Optional<File::Error> File::open_fatfs(const std::filesystem::path& filename, BY
|
||||
}
|
||||
}
|
||||
|
||||
Optional<File::Error> File::open(const std::filesystem::path& filename) {
|
||||
return open_fatfs(filename, FA_READ);
|
||||
Optional<File::Error> File::open(const std::filesystem::path& filename, bool read_only) {
|
||||
BYTE mode = read_only ? FA_READ : FA_READ | FA_WRITE;
|
||||
return open_fatfs(filename, mode);
|
||||
}
|
||||
|
||||
Optional<File::Error> File::append(const std::filesystem::path& filename) {
|
||||
@@ -97,6 +98,15 @@ File::Result<File::Offset> File::seek(Offset new_position) {
|
||||
return {static_cast<File::Offset>(old_position)};
|
||||
}
|
||||
|
||||
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)};
|
||||
}
|
||||
|
||||
File::Size File::size() const {
|
||||
return f_size(&f);
|
||||
}
|
||||
@@ -248,19 +258,19 @@ std::filesystem::filesystem_error copy_file(
|
||||
File dst;
|
||||
|
||||
auto error = src.open(file_path);
|
||||
if (error.is_valid()) return error.value();
|
||||
if (error) return error.value();
|
||||
|
||||
error = dst.create(dest_path);
|
||||
if (error.is_valid()) return error.value();
|
||||
if (error) return error.value();
|
||||
|
||||
while (true) {
|
||||
auto result = src.read(buffer, buffer_size);
|
||||
if (result.is_error()) return result.error();
|
||||
|
||||
result = dst.write(buffer, result.value());
|
||||
result = dst.write(buffer, *result);
|
||||
if (result.is_error()) return result.error();
|
||||
|
||||
if (result.value() < buffer_size)
|
||||
if (*result < buffer_size)
|
||||
break;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user