diff --git a/apps/graph/list/storage_list_controller.cpp b/apps/graph/list/storage_list_controller.cpp index da9f23889..6beb1cd82 100644 --- a/apps/graph/list/storage_list_controller.cpp +++ b/apps/graph/list/storage_list_controller.cpp @@ -64,11 +64,11 @@ bool StorageListController::textFieldDidFinishEditing(TextField * textField, con } // Delete any variable with the same name - GlobalContext::DestroyRecordsBaseNamedWithoutExtension(baseName, GlobalContext::funcExtension /*TODO store elsewhere?*/); + GlobalContext::DestroyRecordsBaseNamedWithoutExtension(baseName, Ion::Storage::funcExtension); // Set the name StorageFunction::NameNotCompliantError nameError = StorageFunction::NameNotCompliantError::None; - Ion::Storage::Record::ErrorStatus error = StorageFunction::BaseNameCompliant(baseName, &nameError) ? modelStore()->recordAtIndex(m_selectableTableView.selectedRow()).setBaseNameWithExtension(baseName, GlobalContext::funcExtension /*TODO store elsewhere?*/) : Ion::Storage::Record::ErrorStatus::NonCompliantName; + Ion::Storage::Record::ErrorStatus error = StorageFunction::BaseNameCompliant(baseName, &nameError) ? modelStore()->recordAtIndex(m_selectableTableView.selectedRow()).setBaseNameWithExtension(baseName, Ion::Storage::funcExtension) : Ion::Storage::Record::ErrorStatus::NonCompliantName; // Handle any error if (error == Ion::Storage::Record::ErrorStatus::None) { diff --git a/apps/graph/storage_cartesian_function_store.h b/apps/graph/storage_cartesian_function_store.h index ee786cfcb..0f21a9cf1 100644 --- a/apps/graph/storage_cartesian_function_store.h +++ b/apps/graph/storage_cartesian_function_store.h @@ -15,7 +15,7 @@ public: char unknownSymbol() const override { return Poincare::Symbol::SpecialSymbols::UnknownX; } private: Ion::Storage::Record::ErrorStatus addEmptyModel() override; - const char * modelExtension() const override { return Shared::GlobalContext::funcExtension; } + const char * modelExtension() const override { return Ion::Storage::funcExtension; } void setMemoizedModelAtIndex(int cacheIndex, Ion::Storage::Record record) const override; Shared::SingleExpressionModelHandle * memoizedModelAtIndex(int cacheIndex) const override; mutable Shared::StorageCartesianFunction m_functions[k_maxNumberOfMemoizedModels]; diff --git a/apps/sequence/sequence_store.cpp b/apps/sequence/sequence_store.cpp index 718a3e3d3..6ede5a26a 100644 --- a/apps/sequence/sequence_store.cpp +++ b/apps/sequence/sequence_store.cpp @@ -14,7 +14,7 @@ const char * SequenceStore::firstAvailableName(int * nameIndex) { int currentNameIndex = 0; while (currentNameIndex < MaxNumberOfSequences) { const char * name = k_sequenceNames[currentNameIndex]; - if (Ion::Storage::sharedStorage()->recordBaseNamedWithExtension(name, Shared::GlobalContext::seqExtension).isNull()) { + if (Ion::Storage::sharedStorage()->recordBaseNamedWithExtension(name, Ion::Storage::seqExtension).isNull()) { if (nameIndex) { *nameIndex = currentNameIndex; } diff --git a/apps/sequence/sequence_store.h b/apps/sequence/sequence_store.h index 383499e6a..3db947705 100644 --- a/apps/sequence/sequence_store.h +++ b/apps/sequence/sequence_store.h @@ -30,7 +30,7 @@ public: }; private: - const char * modelExtension() const override { return Shared::GlobalContext::seqExtension; } + const char * modelExtension() const override { return Ion::Storage::seqExtension; } /* We don't really use model memoization as the number of Sequence is limited * and we keep enough Sequences to store them all. */ void setMemoizedModelAtIndex(int cacheIndex, Ion::Storage::Record record) const override; diff --git a/apps/shared/global_context.cpp b/apps/shared/global_context.cpp index 48386b2a5..9904d433d 100644 --- a/apps/shared/global_context.cpp +++ b/apps/shared/global_context.cpp @@ -10,9 +10,6 @@ using namespace Poincare; 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) { @@ -23,10 +20,10 @@ Poincare::Expression GlobalContext::ExpressionFromRecord(Ion::Storage::Record re if (record.isNull() || record.value().size == 0) { return Expression(); } - if (Ion::Storage::FullNameHasExtension(record.fullName(), expExtension, strlen(expExtension))) { + if (Ion::Storage::FullNameHasExtension(record.fullName(), Ion::Storage::expExtension, strlen(Ion::Storage::expExtension))) { return ExpressionFromSymbolRecord(record); } - assert(Ion::Storage::FullNameHasExtension(record.fullName(), funcExtension, strlen(funcExtension))); + assert(Ion::Storage::FullNameHasExtension(record.fullName(), Ion::Storage::funcExtension, strlen(Ion::Storage::funcExtension))); return ExpressionFromFunctionRecord(record); } @@ -34,7 +31,7 @@ Poincare::Expression GlobalContext::ExpressionFromSymbolRecord(Ion::Storage::Rec if (record.isNull() || record.value().size == 0) { return Expression(); } - assert(Ion::Storage::FullNameHasExtension(record.fullName(), expExtension, strlen(expExtension))); + assert(Ion::Storage::FullNameHasExtension(record.fullName(), Ion::Storage::expExtension, strlen(Ion::Storage::expExtension))); // An expression record value is the expression itself Ion::Storage::Record::Data d = record.value(); return Expression::ExpressionFromAddress(d.buffer, d.size); @@ -43,7 +40,7 @@ Poincare::Expression GlobalContext::ExpressionFromFunctionRecord(Ion::Storage::R if (record.isNull() || record.value().size == 0) { return Expression(); } - if (!Ion::Storage::FullNameHasExtension(record.fullName(), funcExtension, strlen(funcExtension))) { + if (!Ion::Storage::FullNameHasExtension(record.fullName(), Ion::Storage::funcExtension, strlen(Ion::Storage::funcExtension))) { return Expression(); } /* An function record value has metadata before the expression. To get the @@ -94,7 +91,7 @@ const Expression GlobalContext::ExpressionForSymbolAndRecord(const SymbolAbstrac const Expression GlobalContext::ExpressionForActualSymbol(const SymbolAbstract & symbol, Ion::Storage::Record r) { assert(symbol.type() == ExpressionNode::Type::Symbol); - if (r.isNull() || !Ion::Storage::FullNameHasExtension(r.fullName(), expExtension, strlen(expExtension))) { + if (r.isNull() || !Ion::Storage::FullNameHasExtension(r.fullName(), Ion::Storage::expExtension, strlen(Ion::Storage::expExtension))) { return Expression(); } // Look up the file system for symbol @@ -102,7 +99,7 @@ const Expression GlobalContext::ExpressionForActualSymbol(const SymbolAbstract & } Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForActualSymbol(const Expression & expression, const SymbolAbstract & symbol, Ion::Storage::Record previousRecord) { - if (!previousRecord.isNull() && Ion::Storage::FullNameHasExtension(previousRecord.fullName(), funcExtension, strlen(funcExtension))) { + if (!previousRecord.isNull() && Ion::Storage::FullNameHasExtension(previousRecord.fullName(), Ion::Storage::funcExtension, strlen(Ion::Storage::funcExtension))) { /* A function can overwrite a variable, but a variable cannot be created if * it has the same name as an existing function. */ // TODO Pop up "Name taken for a function" @@ -110,13 +107,13 @@ Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForActualSymbol(co } // Delete any record with same name (as it is going to be overriden) previousRecord.destroy(); - return Ion::Storage::sharedStorage()->createRecordWithExtension(symbol.name(), expExtension, expression.addressInPool(), expression.size()); + return Ion::Storage::sharedStorage()->createRecordWithExtension(symbol.name(), Ion::Storage::expExtension, expression.addressInPool(), expression.size()); } Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForFunctionRecord(Expression expressionToStore, Ion::Storage::Record previousRecord, const char * baseName) { Ion::Storage::Record recordToSet = previousRecord; Ion::Storage::Record::ErrorStatus error = Ion::Storage::Record::ErrorStatus::None; - if (!Ion::Storage::FullNameHasExtension(previousRecord.fullName(), funcExtension, strlen(funcExtension))) { + if (!Ion::Storage::FullNameHasExtension(previousRecord.fullName(), Ion::Storage::funcExtension, strlen(Ion::Storage::funcExtension))) { // The previous record was not a function. Destroy it and create the new record. previousRecord.destroy(); StorageCartesianFunction newModel = StorageCartesianFunction::NewModel(&error, baseName); @@ -134,8 +131,7 @@ Ion::Storage::Record::ErrorStatus GlobalContext::SetExpressionForFunction(const } Ion::Storage::Record GlobalContext::SymbolAbstractRecordWithBaseName(const char * name) { - const char * extensions[2] = {expExtension, funcExtension/*, seqExtension*/}; - return Ion::Storage::sharedStorage()->recordBaseNamedWithExtensions(name, extensions, 2); + return Ion::Storage::sharedStorage()->recordBaseNamedWithExtensions(name, k_extensions, k_numberOfExtensions); } } diff --git a/apps/shared/global_context.h b/apps/shared/global_context.h index 10a0c8d09..617eca3c5 100644 --- a/apps/shared/global_context.h +++ b/apps/shared/global_context.h @@ -14,10 +14,7 @@ namespace Shared { class GlobalContext final : public Poincare::Context { public: 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"; + static constexpr const char * k_extensions[] = {Ion::Storage::expExtension, Ion::Storage::funcExtension}; // Storage information static bool SymbolAbstractNameIsFree(const char * baseName); diff --git a/apps/shared/storage_cartesian_function.cpp b/apps/shared/storage_cartesian_function.cpp index ad76296f0..4ce38c7fe 100644 --- a/apps/shared/storage_cartesian_function.cpp +++ b/apps/shared/storage_cartesian_function.cpp @@ -53,7 +53,7 @@ StorageCartesianFunction StorageCartesianFunction::NewModel(Ion::Storage::Record DefaultName(nameBuffer, SymbolAbstract::k_maxNameSize); baseName = nameBuffer; } - *error = Ion::Storage::sharedStorage()->createRecordWithExtension(baseName, GlobalContext::funcExtension, &data, sizeof(data)); + *error = Ion::Storage::sharedStorage()->createRecordWithExtension(baseName, Ion::Storage::funcExtension, &data, sizeof(data)); // Return if error if (*error != Ion::Storage::Record::ErrorStatus::None) { @@ -61,7 +61,7 @@ StorageCartesianFunction StorageCartesianFunction::NewModel(Ion::Storage::Record } // Return the StorageCartesianFunction withthe new record - return StorageCartesianFunction(Ion::Storage::sharedStorage()->recordBaseNamedWithExtension(baseName, GlobalContext::funcExtension)); + return StorageCartesianFunction(Ion::Storage::sharedStorage()->recordBaseNamedWithExtension(baseName, Ion::Storage::funcExtension)); } int StorageCartesianFunction::derivativeNameWithArgument(char * buffer, size_t bufferSize, char arg) { diff --git a/apps/solver/equation.cpp b/apps/solver/equation.cpp index 7352ba583..52041e04e 100644 --- a/apps/solver/equation.cpp +++ b/apps/solver/equation.cpp @@ -12,8 +12,6 @@ using namespace Shared; namespace Solver { -constexpr char Equation::extension[]; - Equation::Equation(Ion::Storage::Record record) : SingleExpressionModelHandle(record) { diff --git a/apps/solver/equation.h b/apps/solver/equation.h index 6db7e8249..65bda4052 100644 --- a/apps/solver/equation.h +++ b/apps/solver/equation.h @@ -14,7 +14,6 @@ public: Poincare::Expression standardForm(Poincare::Context * context) const { return m_handle.standardForm(this, context); } bool containsIComplex(Poincare::Context * context) const; - static constexpr char extension[] = "eq"; // TODO: store this elsewhere? private: class Handle : public Shared::ExpressionModelHandle { public: diff --git a/apps/solver/equation_store.cpp b/apps/solver/equation_store.cpp index 3cbe557bd..be1491cf5 100644 --- a/apps/solver/equation_store.cpp +++ b/apps/solver/equation_store.cpp @@ -35,13 +35,13 @@ Ion::Storage::Record::ErrorStatus EquationStore::addEmptyModel() { int currentNumber = 0; while (currentNumber < k_maxNumberOfEquations) { name[1] = '0'+currentNumber; - if (Ion::Storage::sharedStorage()->recordBaseNamedWithExtension(name, Equation::extension).isNull()) { + if (Ion::Storage::sharedStorage()->recordBaseNamedWithExtension(name, Ion::Storage::eqExtension).isNull()) { break; } currentNumber++; } assert(currentNumber < k_maxNumberOfEquations); - return Ion::Storage::sharedStorage()->createRecordWithExtension(name, Equation::extension, nullptr, 0); + return Ion::Storage::sharedStorage()->createRecordWithExtension(name, Ion::Storage::eqExtension, nullptr, 0); } void EquationStore::setMemoizedModelAtIndex(int cacheIndex, Ion::Storage::Record record) const { diff --git a/apps/solver/equation_store.h b/apps/solver/equation_store.h index 2d03d4a05..835da0ec2 100644 --- a/apps/solver/equation_store.h +++ b/apps/solver/equation_store.h @@ -78,7 +78,7 @@ private: static constexpr int k_maxNumberOfEquations = Poincare::Expression::k_maxNumberOfVariables; // Enable the same number of equations as the number of unknown variables // StorageExpressionModelStore - const char * modelExtension() const override { return Equation::extension; } + const char * modelExtension() const override { return Ion::Storage::eqExtension; } /* We don't really use model memoization as the number of Equation is limited * and we keep enough Equations to store them all. */ void setMemoizedModelAtIndex(int cacheIndex, Ion::Storage::Record record) const override; diff --git a/apps/variable_box_controller.cpp b/apps/variable_box_controller.cpp index e27c06a23..086947b52 100644 --- a/apps/variable_box_controller.cpp +++ b/apps/variable_box_controller.cpp @@ -77,9 +77,9 @@ int VariableBoxController::numberOfRows() { case Page::RootMenu: return k_numberOfMenuRows; case Page::Expression: - return Storage::sharedStorage()->numberOfRecordsWithExtension(GlobalContext::expExtension); + return Storage::sharedStorage()->numberOfRecordsWithExtension(Ion::Storage::expExtension); case Page::Function: - return Storage::sharedStorage()->numberOfRecordsWithExtension(GlobalContext::funcExtension); + return Storage::sharedStorage()->numberOfRecordsWithExtension(Ion::Storage::funcExtension); default: return 0; } @@ -243,7 +243,7 @@ Layout VariableBoxController::expressionLayoutForRecord(Storage::Record record, const char * VariableBoxController::extension() const { assert(m_currentPage != Page::RootMenu); - return m_currentPage == Page::Function ? GlobalContext::funcExtension : GlobalContext::expExtension; + return m_currentPage == Page::Function ? Ion::Storage::funcExtension : Ion::Storage::expExtension; } Storage::Record VariableBoxController::recordAtIndex(int rowIndex) { diff --git a/ion/include/ion/storage.h b/ion/include/ion/storage.h index 26867768c..403c4d186 100644 --- a/ion/include/ion/storage.h +++ b/ion/include/ion/storage.h @@ -20,6 +20,11 @@ public: static Storage * sharedStorage(); constexpr static char k_dotChar = '.'; + static constexpr char eqExtension[] = "eq"; + static constexpr char expExtension[] = "exp"; + static constexpr char funcExtension[] = "func"; + static constexpr char seqExtension[] = "seq"; + class Record { /* A Record is identified by the CRC32 on its fullName because: * - A record is identified by its fullName, which is unique @@ -103,7 +108,7 @@ public: Record recordWithExtensionAtIndex(const char * extension, int index); Record recordNamed(const char * fullName); Record recordBaseNamedWithExtension(const char * baseName, const char * extension); - Record recordBaseNamedWithExtensions(const char * baseName, const char * extension[], size_t numberOfExtensions); + Record recordBaseNamedWithExtensions(const char * baseName, const char * const extension[], size_t numberOfExtensions); // Record destruction void destroyAllRecords(); diff --git a/ion/src/shared/storage.cpp b/ion/src/shared/storage.cpp index 7e007851f..0ad38552f 100644 --- a/ion/src/shared/storage.cpp +++ b/ion/src/shared/storage.cpp @@ -22,6 +22,11 @@ namespace Ion { uint32_t staticStorageArea[sizeof(Storage)/sizeof(uint32_t)] = {0}; +constexpr char Storage::expExtension[]; +constexpr char Storage::funcExtension[]; +constexpr char Storage::seqExtension[]; +constexpr char Storage::eqExtension[]; + Storage * Storage::sharedStorage() { static Storage * storage = nullptr; if (storage == nullptr) { @@ -220,7 +225,7 @@ Storage::Record Storage::recordBaseNamedWithExtension(const char * baseName, con return recordBaseNamedWithExtensions(baseName, extensions, 1); } -Storage::Record Storage::recordBaseNamedWithExtensions(const char * baseName, const char * extensions[], size_t numberOfExtensions) { +Storage::Record Storage::recordBaseNamedWithExtensions(const char * baseName, const char * const extensions[], size_t numberOfExtensions) { size_t nameLength = strlen(baseName); for (char * p : *this) { const char * currentName = fullNameOfRecordStarting(p);