[FileChoice] Added extension stripping (optional)

This commit is contained in:
Raphael Coeffic
2021-06-06 10:13:01 +02:00
parent ae5a84cd6c
commit 9962dd0092
2 changed files with 46 additions and 44 deletions
+27 -26
View File
@@ -31,13 +31,18 @@
#define STR_NO_FILES_ON_SD "No files on SD Card!" #define STR_NO_FILES_ON_SD "No files on SD Card!"
#endif #endif
FileChoice::FileChoice(FormGroup * parent, const rect_t & rect, std::string folder, const char * extension, int maxlen, std::function<std::string()> getValue, std::function<void(std::string)> setValue): FileChoice::FileChoice(FormGroup *parent, const rect_t &rect,
ChoiceBase(parent, rect, CHOICE_TYPE_FOLDER), std::string folder, const char *extension, int maxlen,
folder(std::move(folder)), std::function<std::string()> getValue,
extension(extension), std::function<void(std::string)> setValue,
maxlen(maxlen), bool stripExtension) :
getValue(std::move(getValue)), ChoiceBase(parent, rect, CHOICE_TYPE_FOLDER),
setValue(std::move(setValue)) folder(std::move(folder)),
extension(extension),
maxlen(maxlen),
getValue(std::move(getValue)),
setValue(std::move(setValue)),
stripExtension(stripExtension)
{ {
} }
@@ -52,30 +57,28 @@ bool FileChoice::openMenu()
FILINFO fno; FILINFO fno;
DIR dir; DIR dir;
std::list<std::string> files; std::list<std::string> files;
const char * fnExt; const char *fnExt;
uint8_t fnLen, extLen; uint8_t fnLen, extLen;
FRESULT res = f_opendir(&dir, folder.c_str()); // Open the directory FRESULT res = f_opendir(&dir, folder.c_str()); // Open the directory
if (res == FR_OK) { if (res == FR_OK) {
bool firstTime = true; bool firstTime = true;
for (;;) { for (;;) {
res = sdReadDir(&dir, &fno, firstTime); res = sdReadDir(&dir, &fno, firstTime);
if (res != FR_OK || fno.fname[0] == 0) if (res != FR_OK || fno.fname[0] == 0)
break; // break on error or end of dir break; // break on error or end of dir
if (fno.fattrib & AM_DIR) if (fno.fattrib & AM_DIR) continue; // skip subfolders
continue; // skip subfolders if (fno.fattrib & AM_HID) continue; // skip hidden files
if (fno.fattrib & AM_HID) if (fno.fattrib & AM_SYS) continue; // skip system files
continue; // skip hidden files
if (fno.fattrib & AM_SYS)
continue; // skip system files
fnExt = getFileExtension(fno.fname, 0, 0, &fnLen, &extLen); fnExt = getFileExtension(fno.fname, 0, 0, &fnLen, &extLen);
// fnLen -= extLen;
if (!fnLen || fnLen > maxlen)
continue; // wrong size
if (extension && !isExtensionMatching(fnExt, extension)) if (extension && !isExtensionMatching(fnExt, extension))
continue; // wrong extension continue; // wrong extension
if (stripExtension) fnLen -= extLen;
if (!fnLen || fnLen > maxlen) continue; // wrong size
files.emplace_back(fno.fname, fnLen); files.emplace_back(fno.fname, fnLen);
} }
@@ -83,18 +86,16 @@ bool FileChoice::openMenu()
if (!files.empty()) { if (!files.empty()) {
// sort files // sort files
files.sort(compare_nocase); files.sort(compare_nocase);
files.push_front(""); files.push_front("");
auto menu = new Menu(this); auto menu = new Menu(this);
int count = 0; int count = 0;
int current = -1; int current = -1;
std::string value = getValue(); std::string value = getValue();
for (const auto &file: files) { for (const auto &file : files) {
menu->addLine(file, [=]() { menu->addLine(file, [=]() { setValue(file); });
setValue(file); // TRACE("%s %d %s %d", value.c_str(), value.size(), file.c_str(),
}); // file.size());
// TRACE("%s %d %s %d", value.c_str(), value.size(), file.c_str(), file.size());
if (value.compare(file) == 0) { if (value.compare(file) == 0) {
// TRACE("OK"); // TRACE("OK");
current = count; current = count;
+19 -18
View File
@@ -22,35 +22,36 @@
#include "choice.h" #include "choice.h"
#include <string> #include <string>
class FileChoice: public ChoiceBase class FileChoice : public ChoiceBase
{ {
public: public:
FileChoice(FormGroup * parent, const rect_t & rect, std::string folder, const char * extension, int maxlen, std::function<std::string()> getValue, std::function<void(std::string)> setValue); FileChoice(FormGroup* parent, const rect_t& rect, std::string folder,
const char* extension, int maxlen,
std::function<std::string()> getValue,
std::function<void(std::string)> setValue,
bool stripExtension=false);
#if defined(DEBUG_WINDOWS) #if defined(DEBUG_WINDOWS)
std::string getName() const override std::string getName() const override { return "FileChoice"; }
{
return "FileChoice";
}
#endif #endif
void paint(BitmapBuffer * dc) override; void paint(BitmapBuffer* dc) override;
#if defined(HARDWARE_KEYS) #if defined(HARDWARE_KEYS)
void onEvent(event_t event) override; void onEvent(event_t event) override;
#endif #endif
#if defined(HARDWARE_TOUCH) #if defined(HARDWARE_TOUCH)
bool onTouchEnd(coord_t x, coord_t y) override; bool onTouchEnd(coord_t x, coord_t y) override;
#endif #endif
protected: protected:
std::string folder; std::string folder;
const char * extension; const char* extension;
int maxlen; int maxlen;
std::function<std::string()> getValue; std::function<std::string()> getValue;
std::function<void(std::string)> setValue; std::function<void(std::string)> setValue;
bool stripExtension;
bool openMenu(); bool openMenu();
}; };