Goodbye genLinkedList - welcome std::stack

it decrease ESP32 binary size of few bytes (-256) - when it increase esp8266 binary of few bytes (+56) , but make code more standard so it is fine IMHO
This commit is contained in:
Luc 2022-08-23 08:55:46 +08:00
parent 6a82412878
commit 159b9e6f47
16 changed files with 71 additions and 297 deletions

View File

@ -1,223 +0,0 @@
/*
genlinkedlist.h - ESP3D simple generic linked list class
* inspired by great https://github.com/ivanseidel/LinkedList
Copyright (c) 2018 Luc Lebosse. All rights reserved.
This code is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with This code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _GENLINKEDLIST_H
#define _GENLINKEDLIST_H
template<class T> struct GenNode {
T data;
GenNode<T> *next;
GenNode<T> *prev;
};
template <typename T> class GenLinkedList
{
public:
GenLinkedList();
~GenLinkedList();
//Clear list
void clear();
//number of GenNodes
size_t count();
//add data at the end of list
bool push(T data);
//get data at end of list and remove from list
T pop();
//add data at the begining of list
bool shift(T data);
//get data from begining of list and remove from list
T unshift();
//get data from index position
T get(size_t index);
//get head data
T getFirst();
//get tail data
T getLast();
private:
//number of GenNode
size_t _nb;
//First GenNode
GenNode<T> *_head;
//Last GenNode
GenNode<T> *_tail;
};
//Constructor
template <typename T>GenLinkedList<T>::GenLinkedList()
{
_nb = 0;
_head = nullptr;
_tail = nullptr;
}
//Destructor
template <typename T>GenLinkedList<T>::~GenLinkedList()
{
clear();
}
//clear list of all GenNodes
template<typename T>void GenLinkedList<T>::clear()
{
GenNode<T> *current;
//delete from first to last
while (_head != nullptr) {
current = _head;
_head = _head->next;
current->data = T();
delete current;
}
_tail = _head;
_nb = 0;
}
//Number of GenNodes
template<typename T>size_t GenLinkedList<T>::count()
{
return _nb;
}
//add to end of list
template<typename T> bool GenLinkedList<T>::push (T data)
{
GenNode<T> *ptr = new GenNode<T>();
if (!ptr) {
return false;
}
ptr->data = data;
ptr->next = nullptr;
ptr->prev = nullptr;
_nb++;
//Check if already have element in list
if (_head) {
_tail->next = ptr;
ptr->prev = _tail;
_tail = ptr;
} else { // no element _head = _tail
_head = ptr;
_tail = _head;
}
return true;
}
//take out from end of list
template<typename T>T GenLinkedList<T>::pop()
{
if (_head == nullptr) {
return T();
}
T data = _tail->data;
_nb--;
if (_head == _tail) {
_head->data = T();
delete (_head);
_head = nullptr;
_tail = _head;
} else {
GenNode<T> *ptr = _tail;
_tail = _tail->prev;
_tail->next = nullptr;
ptr->data = T();
delete (ptr);
}
return data;
}
//add to head of list
template<typename T> bool GenLinkedList<T>::shift (T data)
{
GenNode<T> *ptr = new GenNode<T>();
if (!ptr) {
return false;
}
ptr->data = data;
ptr->next = nullptr;
ptr->prev = nullptr;
_nb++;
//Check if already have element in list
if (_head) {
ptr->next = _head;
_head->prev = ptr;
_head = ptr;
} else { // no element _head = _tail
_head = ptr;
_tail = _head;
}
return true;
}
//take out from Head
template<typename T>T GenLinkedList<T>::unshift()
{
if (_head == nullptr) {
return T();
}
T data = _head->data;
_nb--;
if (_head == _tail) {
_head->data = T();
delete (_head);
_head = nullptr;
_tail = _head;
} else {
GenNode<T> *ptr = _head;
_head = _head->next;
_head->prev = nullptr;
ptr->data = T();
delete (ptr);
}
return data;
}
//get data from position (do not remove from list)
template<typename T>T GenLinkedList<T>::get(size_t index)
{
if ((_head == nullptr) || (index > _nb)) {
return T();
}
GenNode<T> *ptr = _head;
for (size_t pos = 0; pos < index; pos++) {
ptr = ptr->next;
}
return ptr->data;
}
//get head data (do not remove from list)
template<typename T>T GenLinkedList<T>::getFirst()
{
if (_head == nullptr) {
return T();
}
return _head->data;
}
//get tail data (do not remove from list)
template<typename T>T GenLinkedList<T>::getLast()
{
if (_head == nullptr) {
return T();
}
return _tail->data;
}
#endif //_GENLINKEDLIST_H

View File

@ -22,7 +22,7 @@
#define _VERSION_ESP3D_H #define _VERSION_ESP3D_H
//version and sources location //version and sources location
#define FW_VERSION "3.0.0.a207" #define FW_VERSION "3.0.0.a208"
#define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0"
#endif //_VERSION_ESP3D_H #endif //_VERSION_ESP3D_H

View File

@ -21,7 +21,6 @@
#include "../../include/esp3d_config.h" #include "../../include/esp3d_config.h"
#if defined(GLOBAL_FILESYSTEM_FEATURE) #if defined(GLOBAL_FILESYSTEM_FEATURE)
#include "esp_globalFS.h" #include "esp_globalFS.h"
//#include "../../core/genLinkedList.h"
//to verify FS is accessible //to verify FS is accessible
bool ESP_GBFS::isavailable(uint8_t FS) bool ESP_GBFS::isavailable(uint8_t FS)

View File

@ -21,7 +21,6 @@
#include "../../include/esp3d_config.h" #include "../../include/esp3d_config.h"
#ifdef SD_DEVICE #ifdef SD_DEVICE
#include "esp_sd.h" #include "esp_sd.h"
#include "../../core/genLinkedList.h"
#include <time.h> #include <time.h>
#define ESP_MAX_SD_OPENHANDLE 4 #define ESP_MAX_SD_OPENHANDLE 4

View File

@ -21,7 +21,7 @@ fat_esp32_filesystem.cpp - ESP3D fat filesystem configuration class
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if (FILESYSTEM_FEATURE == ESP_FAT_FILESYSTEM) #if (FILESYSTEM_FEATURE == ESP_FAT_FILESYSTEM)
#include "../esp_filesystem.h" #include "../esp_filesystem.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include <FS.h> #include <FS.h>
#include "FFat.h" #include "FFat.h"
@ -156,10 +156,10 @@ bool ESP_FileSystem::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
File dir = FFat.open(pathlist.getLast().c_str()); File dir = FFat.open(pathlist.top().c_str());
File f = dir.openNextFile(); File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f) { while (f) {
@ -176,15 +176,15 @@ bool ESP_FileSystem::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = FFat.rmdir(pathlist.getLast().c_str()); res = FFat.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -21,7 +21,7 @@ littlefs_esp32_filesystem.cpp - ESP3D littlefs filesystem configuration class
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if (FILESYSTEM_FEATURE == ESP_LITTLEFS_FILESYSTEM) && defined(ARDUINO_ARCH_ESP32) #if (FILESYSTEM_FEATURE == ESP_LITTLEFS_FILESYSTEM) && defined(ARDUINO_ARCH_ESP32)
#include "../esp_filesystem.h" #include "../esp_filesystem.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include <FS.h> #include <FS.h>
#include <LittleFS.h> #include <LittleFS.h>
@ -163,10 +163,10 @@ bool ESP_FileSystem::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String> pathlist;
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
File dir = LittleFS.open(pathlist.getLast().c_str()); File dir = LittleFS.open(pathlist.top().c_str());
File f = dir.openNextFile(); File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f) { while (f) {
@ -183,15 +183,15 @@ bool ESP_FileSystem::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = LittleFS.rmdir(pathlist.getLast().c_str()); res = LittleFS.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -20,7 +20,7 @@ littlefs_esp8266_filesystem.cpp - ESP3D littlefs filesystem configuration class
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if (FILESYSTEM_FEATURE == ESP_LITTLEFS_FILESYSTEM) && defined(ARDUINO_ARCH_ESP8266) #if (FILESYSTEM_FEATURE == ESP_LITTLEFS_FILESYSTEM) && defined(ARDUINO_ARCH_ESP8266)
#include "../esp_filesystem.h" #include "../esp_filesystem.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include <FS.h> #include <FS.h>
#include <LittleFS.h> #include <LittleFS.h>
@ -157,7 +157,7 @@ bool ESP_FileSystem::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String> pathlist;
String spath = path; String spath = path;
spath.trim(); spath.trim();
if (spath[spath.length()-1] != '/') { if (spath[spath.length()-1] != '/') {
@ -167,15 +167,15 @@ bool ESP_FileSystem::rmdir(const char *path)
spath ="/" + spath; spath ="/" + spath;
} }
pathlist.push(spath); pathlist.push(spath);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
spath=pathlist.getLast(); spath=pathlist.top();
bool candelete = true; bool candelete = true;
if (LittleFS.exists(spath.c_str())) { if (LittleFS.exists(spath.c_str())) {
Dir dir = LittleFS.openDir(pathlist.getLast().c_str()); Dir dir = LittleFS.openDir(pathlist.top().c_str());
while (dir.next()) { while (dir.next()) {
if (dir.isDirectory()) { if (dir.isDirectory()) {
candelete = false; candelete = false;
String newdir = pathlist.getLast() + dir.fileName() + "/"; String newdir = pathlist.top() + dir.fileName() + "/";
pathlist.push(newdir); pathlist.push(newdir);
} else { } else {
log_esp3d("remove %s", dir.fileName().c_str()); log_esp3d("remove %s", dir.fileName().c_str());

View File

@ -21,7 +21,7 @@
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if (FILESYSTEM_FEATURE == ESP_SPIFFS_FILESYSTEM) && defined(ARDUINO_ARCH_ESP32) #if (FILESYSTEM_FEATURE == ESP_SPIFFS_FILESYSTEM) && defined(ARDUINO_ARCH_ESP32)
#include "../esp_filesystem.h" #include "../esp_filesystem.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include <FS.h> #include <FS.h>
#include <SPIFFS.h> #include <SPIFFS.h>
extern File tFile_handle[ESP_MAX_OPENHANDLE]; extern File tFile_handle[ESP_MAX_OPENHANDLE];

View File

@ -20,7 +20,6 @@
#include "../../../include/esp3d_config.h" #include "../../../include/esp3d_config.h"
#if (FILESYSTEM_FEATURE == ESP_SPIFFS_FILESYSTEM) && defined (ARDUINO_ARCH_ESP8266) #if (FILESYSTEM_FEATURE == ESP_SPIFFS_FILESYSTEM) && defined (ARDUINO_ARCH_ESP8266)
#include "../esp_filesystem.h" #include "../esp_filesystem.h"
#include "../../../core/genLinkedList.h"
#include <FS.h> #include <FS.h>
Dir tDir_handle[ESP_MAX_OPENHANDLE]; Dir tDir_handle[ESP_MAX_OPENHANDLE];
extern File tFile_handle[ESP_MAX_OPENHANDLE]; extern File tFile_handle[ESP_MAX_OPENHANDLE];

View File

@ -21,7 +21,7 @@ sd_native_esp32.cpp - ESP3D sd support class
#if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE) #if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SD_NATIVE) #if (SD_DEVICE == ESP_SD_NATIVE)
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#include "FS.h" #include "FS.h"
#include "SD.h" #include "SD.h"
@ -213,14 +213,14 @@ bool ESP_SD::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
String p = path; String p = path;
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove( p.length() - 1,1); p.remove( p.length() - 1,1);
} }
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
File dir = SD.open(pathlist.getLast().c_str()); File dir = SD.open(pathlist.top().c_str());
File f = dir.openNextFile(); File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f) { while (f) {
@ -237,15 +237,15 @@ bool ESP_SD::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = SD.rmdir(pathlist.getLast().c_str()); res = SD.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -22,7 +22,7 @@ sd_native_esp8266.cpp - ESP3D sd support class
#if (SD_DEVICE == ESP_SD_NATIVE) #if (SD_DEVICE == ESP_SD_NATIVE)
#define FS_NO_GLOBALS #define FS_NO_GLOBALS
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#include <SD.h> #include <SD.h>
#include <SDFS.h> #include <SDFS.h>
@ -256,11 +256,11 @@ bool ESP_SD::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
String p = path; String p = path;
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
File dir = SD.open(pathlist.getLast().c_str()); File dir = SD.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
File f = dir.openNextFile(); File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
@ -279,15 +279,15 @@ bool ESP_SD::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = SD.rmdir(pathlist.getLast().c_str()); res = SD.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -21,7 +21,7 @@ sd_sdfat2_esp32.cpp - ESP3D sd support class
#if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE) #if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SDFAT2) #if (SD_DEVICE == ESP_SDFAT2)
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#include <SdFat.h> #include <SdFat.h>
#include <sdios.h> #include <sdios.h>
@ -366,11 +366,11 @@ bool ESP_SD::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
String p = path; String p = path;
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
File dir = SD.open(pathlist.getLast().c_str()); File dir = SD.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
File f = dir.openNextFile(); File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
@ -394,15 +394,15 @@ bool ESP_SD::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = SD.rmdir(pathlist.getLast().c_str()); res = SD.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -23,7 +23,7 @@ sd_sdfat2_esp8266.cpp - ESP3D sd support class
#if (SD_DEVICE == ESP_SDFAT2) #if (SD_DEVICE == ESP_SDFAT2)
#define FS_NO_GLOBALS #define FS_NO_GLOBALS
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#define NO_GLOBAL_SD #define NO_GLOBAL_SD
#include <SdFat.h> #include <SdFat.h>
@ -351,11 +351,11 @@ bool ESP_SD::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
String p = path; String p = path;
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
sdfat::File dir = SD.open(pathlist.getLast().c_str()); sdfat::File dir = SD.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
sdfat::File f = dir.openNextFile(); sdfat::File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
@ -379,15 +379,15 @@ bool ESP_SD::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = SD.rmdir(pathlist.getLast().c_str()); res = SD.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -21,7 +21,7 @@ sd_sdfat_esp32.cpp - ESP3D sd support class
#if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE) #if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SDFAT) #if (SD_DEVICE == ESP_SDFAT)
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#include <SdFat.h> #include <SdFat.h>
extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE]; extern File tSDFile_handle[ESP_MAX_SD_OPENHANDLE];
@ -677,11 +677,11 @@ bool ESP_SD::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
String p = path; String p = path;
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
File dir = SD.open(pathlist.getLast().c_str()); File dir = SD.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
File f = dir.openNextFile(); File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
@ -705,15 +705,15 @@ bool ESP_SD::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = SD.rmdir(pathlist.getLast().c_str()); res = SD.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -22,7 +22,7 @@ sd_sdfat_esp8266.cpp - ESP3D sd support class
#if (SD_DEVICE == ESP_SDFAT) #if (SD_DEVICE == ESP_SDFAT)
#define FS_NO_GLOBALS #define FS_NO_GLOBALS
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#define NO_GLOBAL_SD #define NO_GLOBAL_SD
#include <SdFat.h> #include <SdFat.h>
@ -684,11 +684,11 @@ bool ESP_SD::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
String p = path; String p = path;
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
sdfat::File dir = SD.open(pathlist.getLast().c_str()); sdfat::File dir = SD.open(pathlist.top().c_str());
dir.rewindDirectory(); dir.rewindDirectory();
sdfat::File f = dir.openNextFile(); sdfat::File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
@ -712,15 +712,15 @@ bool ESP_SD::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = SD.rmdir(pathlist.getLast().c_str()); res = SD.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }

View File

@ -21,7 +21,7 @@ sdio_esp32.cpp - ESP3D sd support class
#if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE) #if defined (ARDUINO_ARCH_ESP32) && defined(SD_DEVICE)
#if (SD_DEVICE == ESP_SDIO) #if (SD_DEVICE == ESP_SDIO)
#include "../esp_sd.h" #include "../esp_sd.h"
#include "../../../core/genLinkedList.h" #include <stack>
#include "../../../core/settings_esp3d.h" #include "../../../core/settings_esp3d.h"
#include "FS.h" #include "FS.h"
#include "SD_MMC.h" #include "SD_MMC.h"
@ -237,14 +237,14 @@ bool ESP_SD::rmdir(const char *path)
return false; return false;
} }
bool res = true; bool res = true;
GenLinkedList<String > pathlist; std::stack <String > pathlist;
String p = path; String p = path;
if (p.endsWith("/")) { if (p.endsWith("/")) {
p.remove( p.length() - 1,1); p.remove( p.length() - 1,1);
} }
pathlist.push(p); pathlist.push(p);
while (pathlist.count() > 0) { while (pathlist.size() > 0) {
File dir = SD_MMC.open(pathlist.getLast().c_str()); File dir = SD_MMC.open(pathlist.top().c_str());
File f = dir.openNextFile(); File f = dir.openNextFile();
bool candelete = true; bool candelete = true;
while (f) { while (f) {
@ -261,15 +261,15 @@ bool ESP_SD::rmdir(const char *path)
} }
} }
if (candelete) { if (candelete) {
if (pathlist.getLast() !="/") { if (pathlist.top() !="/") {
res = SD_MMC.rmdir(pathlist.getLast().c_str()); res = SD_MMC.rmdir(pathlist.top().c_str());
} }
pathlist.pop(); pathlist.pop();
} }
dir.close(); dir.close();
} }
p = String(); p = String();
log_esp3d("count %d", pathlist.count()); log_esp3d("count %d", pathlist.size());
return res; return res;
} }