diff --git a/apps/graph/list/storage_list_controller.cpp b/apps/graph/list/storage_list_controller.cpp index 7c747a2d1..ea29df9ec 100644 --- a/apps/graph/list/storage_list_controller.cpp +++ b/apps/graph/list/storage_list_controller.cpp @@ -54,6 +54,9 @@ bool StorageListController::textFieldDidFinishEditing(TextField * textField, con strlcpy(baseName, text, textLength - argumentLength + 1); } + // Delete any variable with the same name + GlobalContext::DestroyRecordsBaseNamedWithoutExtension(baseName, GlobalContext::funcExtension /*TODO store elsewhere?*/); + // Set the name Ion::Storage::Record::ErrorStatus error = StorageFunction::BaseNameCompliant(baseName) ? modelStore()->recordAtIndex(m_selectableTableView.selectedRow()).setBaseNameWithExtension(baseName, GlobalContext::funcExtension /*TODO store elsewhere?*/) : Ion::Storage::Record::ErrorStatus::NonCompliantName; diff --git a/apps/shared/global_context.cpp b/apps/shared/global_context.cpp index a738bc9b6..49012c719 100644 --- a/apps/shared/global_context.cpp +++ b/apps/shared/global_context.cpp @@ -13,6 +13,7 @@ namespace Shared { constexpr char GlobalContext::expExtension[]; constexpr char GlobalContext::funcExtension[]; //constexpr char GlobalContext::seqExtension[]; +constexpr const char * GlobalContext::k_extensions[]; bool GlobalContext::SymbolAbstractNameIsFree(const char * baseName) { return SymbolAbstractRecordWithBaseName(baseName).isNull(); @@ -51,6 +52,14 @@ Poincare::Expression GlobalContext::ExpressionFromFunctionRecord(Ion::Storage::R return f.expressionWithSymbol(); } +void GlobalContext::DestroyRecordsBaseNamedWithoutExtension(const char * baseName, const char * extension) { + for (int i = 0; i < k_numberOfExtensions; i++) { + if (strcmp(k_extensions[i], extension) != 0) { + Ion::Storage::sharedStorage()->destroyRecordWithBaseNameAndExtension(baseName, k_extensions[i]); + } + } +} + const Expression GlobalContext::expressionForSymbol(const SymbolAbstract & symbol) { Ion::Storage::Record r = SymbolAbstractRecordWithBaseName(symbol.name()); return ExpressionForSymbolAndRecord(symbol, r); diff --git a/apps/shared/global_context.h b/apps/shared/global_context.h index 6327c5c69..436cf9f12 100644 --- a/apps/shared/global_context.h +++ b/apps/shared/global_context.h @@ -15,8 +15,10 @@ class Integer; class GlobalContext final : public Poincare::Context { public: - static constexpr char funcExtension[] = "func"; // TODO: store this elsewhere? + static constexpr int k_numberOfExtensions = 2; static constexpr char expExtension[] = "exp"; // TODO: store this elsewhere? + static constexpr char funcExtension[] = "func"; // TODO: store this elsewhere? + static constexpr const char * k_extensions[] = {expExtension, funcExtension}; //static constexpr char seqExtension[] = "seq"; // Storage information @@ -30,6 +32,9 @@ public: // Set expression for record static Ion::Storage::Record::ErrorStatus SetExpressionForFunctionRecord(Poincare::Expression e, Ion::Storage::Record record, const char * baseName); + // Destroy records + static void DestroyRecordsBaseNamedWithoutExtension(const char * baseName, const char * extension); + /* Expression for symbol * The expression recorded in global context is already an expression. * Otherwise, we would need the context and the angle unit to evaluate it */ diff --git a/ion/include/ion/storage.h b/ion/include/ion/storage.h index 465e8e6b9..c118fe123 100644 --- a/ion/include/ion/storage.h +++ b/ion/include/ion/storage.h @@ -98,6 +98,7 @@ public: Record recordBaseNamedWithExtensions(const char * baseName, const char * extension[], size_t numberOfExtensions); // Record destruction + void destroyRecordWithBaseNameAndExtension(const char * baseName, const char * extension); void destroyRecordsWithExtension(const char * extension); private: diff --git a/ion/src/shared/storage.cpp b/ion/src/shared/storage.cpp index 9cdec8290..0cd79147e 100644 --- a/ion/src/shared/storage.cpp +++ b/ion/src/shared/storage.cpp @@ -216,6 +216,10 @@ Storage::Record Storage::recordBaseNamedWithExtensions(const char * baseName, co return Record(); } +void Storage::destroyRecordWithBaseNameAndExtension(const char * baseName, const char * extension) { + recordBaseNamedWithExtension(baseName, extension).destroy(); +} + void Storage::destroyRecordsWithExtension(const char * extension) { size_t extensionLength = strlen(extension); char * currentRecordStart = (char *)m_buffer;