From 4765a247562a2e2532c862e1c0bc58229724c38f Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Fri, 21 May 2021 13:54:52 -1000 Subject: [PATCH] Use a default most-empty config file if config.yaml missing --- Grbl_Esp32/src/MachineConfig.cpp | 69 ++++++++++++++++++++------------ Grbl_Esp32/src/MachineConfig.h | 3 +- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/Grbl_Esp32/src/MachineConfig.cpp b/Grbl_Esp32/src/MachineConfig.cpp index b341ccef..71c44e6a 100644 --- a/Grbl_Esp32/src/MachineConfig.cpp +++ b/Grbl_Esp32/src/MachineConfig.cpp @@ -371,31 +371,18 @@ void MachineConfig::afterParse() { } } -bool MachineConfig::load(const char* filename) { +size_t MachineConfig::readFile(const char* filename, char*& buffer) { if (!SPIFFS.begin(true)) { - log_fatal("An error has occurred while mounting SPIFFS"); - return false; + log_info("An error has occurred while mounting SPIFFS"); + return 0; } FILE* file = fopen(filename, "rb"); if (!file) { - log_fatal("There was an error opening the config file for reading"); - return false; + log_info("There was an error opening the config file for reading"); + return 0; } - // Regardless of what we do next, we _always_ want a MachineConfig instance. - - // instance() is by reference, so we can just get rid of an old instance and - // create a new one here: - { - auto& machineConfig = instance(); - if (machineConfig != nullptr) { - delete machineConfig; - } - machineConfig = new MachineConfig(); - } - MachineConfig* machine = instance(); - // Let's just read the entire file in one chunk for now. If we get // in trouble with this, we can cut it in pieces and read it per chunk. fseek(file, 0, SEEK_END); @@ -403,7 +390,7 @@ bool MachineConfig::load(const char* filename) { log_debug("Configuration file is " << int(filesize) << " bytes."); fseek(file, 0, SEEK_SET); - char* buffer = new char[filesize + 1]; + buffer = new char[filesize + 1]; long pos = 0; while (pos < filesize) { @@ -423,14 +410,41 @@ bool MachineConfig::load(const char* filename) { delete[] buffer; log_error("There was an error reading the config file"); - return false; + return 0; } + return filesize; +} +char defaultConfig[] = "name: Default\nboard: None\n"; + +bool MachineConfig::load(const char* filename) { + // Regardless of what we do next, we _always_ want a MachineConfig instance. + + // instance() is by reference, so we can just get rid of an old instance and + // create a new one here: + { + auto& machineConfig = instance(); + if (machineConfig != nullptr) { + delete machineConfig; + } + machineConfig = new MachineConfig(); + } + MachineConfig* machine = instance(); + + char* buffer; + size_t filesize = readFile(filename, buffer); + StringRange* input; + + if (filesize > 0) { + input = new StringRange(buffer, buffer + filesize); + } else { + log_info("Using default configuration"); + input = new StringRange(defaultConfig); + } // Process file: - StringRange input(buffer, buffer + filesize); - bool succesful = false; + bool successful = false; try { - Configuration::Parser parser(input.begin(), input.end()); + Configuration::Parser parser(input->begin(), input->end()); Configuration::ParserHandler handler(parser); for (; !parser.isEndSection(); parser.moveNext()) { @@ -456,7 +470,7 @@ bool MachineConfig::load(const char* filename) { log_info("Done validating machine config."); - succesful = true; + successful = true; // TODO FIXME: If we get here, we want to make the settings live by saving them as // '/spiffs/config.yaml.new', storing it, and then deleting the .yaml and renaming it. @@ -480,8 +494,11 @@ bool MachineConfig::load(const char* filename) { } // Get rid of buffer and return - delete[] buffer; - return succesful; + if (buffer) { + delete[] buffer; + } + delete[] input; + return successful; } MachineConfig::~MachineConfig() { diff --git a/Grbl_Esp32/src/MachineConfig.h b/Grbl_Esp32/src/MachineConfig.h index 99ac7378..9f599d1c 100644 --- a/Grbl_Esp32/src/MachineConfig.h +++ b/Grbl_Esp32/src/MachineConfig.h @@ -366,7 +366,8 @@ public: void afterParse() override; void handle(Configuration::HandlerBase& handler) override; - bool load(const char* file = "/spiffs/config.yaml"); + size_t readFile(const char* file, char*& buffer); + bool load(const char* file = "/spiffs/config.yaml"); ~MachineConfig(); };