1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-03 03:13:25 +02:00

Use a default most-empty config file if config.yaml missing

This commit is contained in:
Mitch Bradley
2021-05-21 13:54:52 -10:00
parent d1d74165f6
commit 4765a24756
2 changed files with 45 additions and 27 deletions

View File

@@ -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)) { if (!SPIFFS.begin(true)) {
log_fatal("An error has occurred while mounting SPIFFS"); log_info("An error has occurred while mounting SPIFFS");
return false; return 0;
} }
FILE* file = fopen(filename, "rb"); FILE* file = fopen(filename, "rb");
if (!file) { if (!file) {
log_fatal("There was an error opening the config file for reading"); log_info("There was an error opening the config file for reading");
return false; 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 // 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. // in trouble with this, we can cut it in pieces and read it per chunk.
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
@@ -403,7 +390,7 @@ bool MachineConfig::load(const char* filename) {
log_debug("Configuration file is " << int(filesize) << " bytes."); log_debug("Configuration file is " << int(filesize) << " bytes.");
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
char* buffer = new char[filesize + 1]; buffer = new char[filesize + 1];
long pos = 0; long pos = 0;
while (pos < filesize) { while (pos < filesize) {
@@ -423,14 +410,41 @@ bool MachineConfig::load(const char* filename) {
delete[] buffer; delete[] buffer;
log_error("There was an error reading the config file"); 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: // Process file:
StringRange input(buffer, buffer + filesize); bool successful = false;
bool succesful = false;
try { try {
Configuration::Parser parser(input.begin(), input.end()); Configuration::Parser parser(input->begin(), input->end());
Configuration::ParserHandler handler(parser); Configuration::ParserHandler handler(parser);
for (; !parser.isEndSection(); parser.moveNext()) { for (; !parser.isEndSection(); parser.moveNext()) {
@@ -456,7 +470,7 @@ bool MachineConfig::load(const char* filename) {
log_info("Done validating machine config."); 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 // 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. // '/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 // Get rid of buffer and return
delete[] buffer; if (buffer) {
return succesful; delete[] buffer;
}
delete[] input;
return successful;
} }
MachineConfig::~MachineConfig() { MachineConfig::~MachineConfig() {

View File

@@ -366,7 +366,8 @@ public:
void afterParse() override; void afterParse() override;
void handle(Configuration::HandlerBase& handler) 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(); ~MachineConfig();
}; };