apply RAII principle on the lsDive recursion limiter

This commit is contained in:
Alex Voinea 2021-02-09 15:00:46 +02:00
parent c739aa9003
commit 4fcbf95db6
No known key found for this signature in database
GPG Key ID: F5034E7CFCF2F973
1 changed files with 10 additions and 3 deletions

View File

@ -71,14 +71,22 @@ char *createFilename(char *buffer,const dir_t &p) //buffer>12characters
void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) { void CardReader::lsDive(const char *prepend, SdFile parent, const char * const match/*=NULL*/) {
static uint8_t recursionCnt = 0; static uint8_t recursionCnt = 0;
// RAII incrementer for the recursionCnt
class _incrementer
{
public:
_incrementer() {recursionCnt++;}
~_incrementer() {recursionCnt--;}
} recursionCntIncrementer;
dir_t p; dir_t p;
uint8_t cnt = 0; uint8_t cnt = 0;
// Read the next entry from a directory // Read the next entry from a directory
while (parent.readDir(p, longFilename) > 0) { while (parent.readDir(p, longFilename) > 0) {
if (recursionCnt >= MAX_DIR_DEPTH) if (recursionCnt > MAX_DIR_DEPTH)
return; return;
else if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // If the entry is a directory and the action is LS_SerialPrint else if (DIR_IS_SUBDIR(&p) && lsAction != LS_Count && lsAction != LS_GetFilename) { // If the entry is a directory and the action is LS_SerialPrint
recursionCnt++;
// Get the short name for the item, which we know is a folder // Get the short name for the item, which we know is a folder
char lfilename[FILENAME_LENGTH]; char lfilename[FILENAME_LENGTH];
createFilename(lfilename, p); createFilename(lfilename, p);
@ -112,7 +120,6 @@ void CardReader::lsDive(const char *prepend, SdFile parent, const char * const m
if (lsAction == LS_SerialPrint_LFN) if (lsAction == LS_SerialPrint_LFN)
puts_P(PSTR("DIR_EXIT")); puts_P(PSTR("DIR_EXIT"));
recursionCnt--;
} }
else { else {
uint8_t pn0 = p.name[0]; uint8_t pn0 = p.name[0];