diff --git a/apps/code/menu_controller.cpp b/apps/code/menu_controller.cpp index fe2ff2793..900258ebb 100644 --- a/apps/code/menu_controller.cpp +++ b/apps/code/menu_controller.cpp @@ -316,7 +316,7 @@ bool MenuController::textFieldDidFinishEditing(TextField * textField, const char } else { newName = text; } - Script::ErrorStatus error = m_scriptStore->scriptAtIndex(m_selectableTableView.selectedRow()).setName(newName); + Script::ErrorStatus error = Script::nameCompliant(newName) ? m_scriptStore->scriptAtIndex(m_selectableTableView.selectedRow()).setName(newName) : Script::ErrorStatus::NonCompliantName; if (error == Script::ErrorStatus::None) { updateAddScriptRowDisplay(); textField->setText(newName); diff --git a/apps/code/script.cpp b/apps/code/script.cpp index 6606bd519..0006b29cd 100644 --- a/apps/code/script.cpp +++ b/apps/code/script.cpp @@ -25,4 +25,17 @@ const char * Script::readContent() const { return (const char *)d.buffer+k_importationStatusSize; } +bool Script::nameCompliant(const char * name) { + /* The name format is [a-z0-9_.]* */ + const char * currentChar = name; + while (*currentChar != 0) { + if ((*currentChar >= 'a' && *currentChar <= 'z') || *currentChar == '_' || (*currentChar >= '0' && *currentChar <= '9') || *currentChar == '.') { + currentChar++; + continue; + } + return false; + } + return name != currentChar; } + +} \ No newline at end of file diff --git a/apps/code/script.h b/apps/code/script.h index ba74540dd..57f88a732 100644 --- a/apps/code/script.h +++ b/apps/code/script.h @@ -17,6 +17,8 @@ public: const char * readContent() const; + static bool nameCompliant(const char * name); + constexpr static size_t k_importationStatusSize = 1; }; diff --git a/apps/code/script_store.cpp b/apps/code/script_store.cpp index 35caedfa1..b64038aa8 100644 --- a/apps/code/script_store.cpp +++ b/apps/code/script_store.cpp @@ -121,6 +121,7 @@ const char * ScriptStore::contentOfScript(const char * name) { Script::ErrorStatus ScriptStore::addScriptFromTemplate(const ScriptTemplate * scriptTemplate) { size_t valueSize = strlen(scriptTemplate->content())+1+1;// scriptcontent size + 1 char for the importation status + assert(Script::nameCompliant(scriptTemplate->name())); Script::ErrorStatus err = Ion::Storage::sharedStorage()->createRecord(scriptTemplate->name(), scriptTemplate->value(), valueSize); assert(err != Script::ErrorStatus::NonCompliantName); return err; diff --git a/ion/include/ion/storage.h b/ion/include/ion/storage.h index 83e36a7ad..21af7156a 100644 --- a/ion/include/ion/storage.h +++ b/ion/include/ion/storage.h @@ -86,7 +86,7 @@ private: size_t overrideValueAtPosition(char * position, const void * data, record_size_t size); bool isNameTaken(const char * name, Record * recordToExclude = nullptr); - bool nameCompliant(const char * name) const; + static bool nameCompliant(const char * name); char * endBuffer(); size_t sizeOfRecord(const char * name, size_t size) const; bool slideBuffer(char * position, int delta); diff --git a/ion/src/shared/storage.cpp b/ion/src/shared/storage.cpp index d1eadacf5..87a536c94 100644 --- a/ion/src/shared/storage.cpp +++ b/ion/src/shared/storage.cpp @@ -278,25 +278,22 @@ bool Storage::isNameTaken(const char * name, Record * recordToExclude) { return false; } -bool Storage::nameCompliant(const char * name) const { - /* The name format is [a-z0-9_]*(\.)?[a-z0-9_]+ */ +bool Storage::nameCompliant(const char * name) { + /* The name format is .*(\.)?[a-z]+ */ bool dot = false; const char * currentChar = name; while (*currentChar != 0) { if (*currentChar == '.') { - if (dot) { - return false; - } else { - dot = true; - } + dot = true; + currentChar++; } - if ((*currentChar >= 'a' && *currentChar <= 'z') || *currentChar == '_' || (*currentChar >= '0' && *currentChar <= '9') || *currentChar == '.') { + if (!dot || (*currentChar >= 'a' && *currentChar <= 'z')) { currentChar++; continue; } return false; } - return name != currentChar; + return name != currentChar && dot; } char * Storage::endBuffer() {