From 12b8a1b2a9b1ab299393ae00feb5d6239e4ffe16 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Fri, 29 Apr 2016 11:27:12 -0700 Subject: [PATCH] Change next-file naming functions to work on filename stems. This paves the way for writing metadata files with similar prefixes, and avoids confusing numbering of capture files with different extensions (e.g. BAD_0000.S16 and BBD_0000.S8). --- firmware/application/analog_audio_app.cpp | 8 +++--- firmware/application/analog_audio_app.hpp | 2 +- firmware/application/capture_app.cpp | 8 +++--- firmware/application/capture_app.hpp | 2 +- firmware/application/file.cpp | 35 ++++++++++------------- firmware/application/file.hpp | 2 +- firmware/application/ui_navigation.cpp | 6 ++-- 7 files changed, 29 insertions(+), 34 deletions(-) diff --git a/firmware/application/analog_audio_app.cpp b/firmware/application/analog_audio_app.cpp index d68556b1..3ba17582 100644 --- a/firmware/application/analog_audio_app.cpp +++ b/firmware/application/analog_audio_app.cpp @@ -316,13 +316,13 @@ void AnalogAudioView::on_record() { } void AnalogAudioView::record_start() { - const auto filename = next_filename_matching_pattern("AUD_????.S16"); - text_record_filename.set(filename); - if( filename.empty() ) { + const auto filename_stem = next_filename_stem_matching_pattern("AUD_????"); + text_record_filename.set(filename_stem); + if( filename_stem.empty() ) { return; } - capture_thread = std::make_unique(filename, 12, 2); + capture_thread = std::make_unique(filename_stem + ".S16", 12, 2); button_record.set_bitmap(&bitmap_stop); } diff --git a/firmware/application/analog_audio_app.hpp b/firmware/application/analog_audio_app.hpp index 4f988823..6e127fe3 100644 --- a/firmware/application/analog_audio_app.hpp +++ b/firmware/application/analog_audio_app.hpp @@ -150,7 +150,7 @@ private: }; Text text_record_filename { - { 3 * 8, 2 * 16, 12 * 8, 16 }, + { 3 * 8, 2 * 16, 8 * 8, 16 }, "", }; diff --git a/firmware/application/capture_app.cpp b/firmware/application/capture_app.cpp index 354c61e0..e14e8e50 100644 --- a/firmware/application/capture_app.cpp +++ b/firmware/application/capture_app.cpp @@ -115,14 +115,14 @@ void CaptureAppView::on_record() { capture_thread.reset(); button_record.set_bitmap(&bitmap_record); } else { - const auto filename = next_filename_matching_pattern("BBD_????.C16"); - text_record_filename.set(filename); + const auto filename_stem = next_filename_stem_matching_pattern("BBD_????"); + text_record_filename.set(filename_stem); text_record_dropped.set(""); - if( filename.empty() ) { + if( filename_stem.empty() ) { return; } - capture_thread = std::make_unique(filename, 14, 1); + capture_thread = std::make_unique(filename_stem + ".C16", 14, 1); button_record.set_bitmap(&bitmap_stop); } } diff --git a/firmware/application/capture_app.hpp b/firmware/application/capture_app.hpp index 3c7e2519..31aa3394 100644 --- a/firmware/application/capture_app.hpp +++ b/firmware/application/capture_app.hpp @@ -75,7 +75,7 @@ private: }; Text text_record_filename { - { 3 * 8, 2 * 16, 12 * 8, 16 }, + { 3 * 8, 2 * 16, 8 * 8, 16 }, "", }; diff --git a/firmware/application/file.cpp b/firmware/application/file.cpp index 288f5bde..871c246f 100644 --- a/firmware/application/file.cpp +++ b/firmware/application/file.cpp @@ -94,22 +94,16 @@ static std::string find_last_file_matching_pattern(const std::string& pattern) { return last_match; } -static std::string increment_filename_ordinal(const std::string& filename) { - std::string result { filename }; +static 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(); - // Back up past extension. - for(; it != result.rend(); ++it) { - if( *it == '.' ) { - ++it; - break; - } - } - if( it == result.rend() ) { - return { }; - } - // Increment decimal number before the extension. for(; it != result.rend(); ++it) { const auto c = *it; @@ -128,15 +122,16 @@ static std::string increment_filename_ordinal(const std::string& filename) { return result; } -std::string next_filename_matching_pattern(const std::string& filename_pattern) { - auto filename = find_last_file_matching_pattern(filename_pattern); - if( filename.empty() ) { - filename = filename_pattern; - std::replace(std::begin(filename), std::end(filename), '?', '0'); +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'); } else { - filename = increment_filename_ordinal(filename); + filename_stem = increment_filename_stem_ordinal(filename_stem); } - return filename; + return filename_stem; } namespace std { diff --git a/firmware/application/file.hpp b/firmware/application/file.hpp index 71e454be..7a45af5d 100644 --- a/firmware/application/file.hpp +++ b/firmware/application/file.hpp @@ -57,7 +57,7 @@ private: FIL f; }; -std::string next_filename_matching_pattern(const std::string& filename_pattern); +std::string next_filename_stem_matching_pattern(const std::string& filename_stem_pattern); namespace std { namespace filesystem { diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 002a8f47..14f4cc6f 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -81,12 +81,12 @@ void SystemStatusView::set_title(const std::string new_value) { } void SystemStatusView::on_camera() { - const auto filename = next_filename_matching_pattern("SCR_????.PNG"); - if( filename.empty() ) { + const auto filename_stem = next_filename_stem_matching_pattern("SCR_????"); + if( filename_stem.empty() ) { return; } - PNGWriter png { filename }; + PNGWriter png { filename_stem + ".PNG" }; for(int i=0; i<320; i++) { std::array row;