From de592ee5fdd34fcc1a004aa9589f92f203140cb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 5 Oct 2018 16:24:47 +0200 Subject: [PATCH] [apps/graph] Generate default function names --- apps/graph/storage_cartesian_function.cpp | 25 +++++++++++++++++++- apps/graph/storage_cartesian_function.h | 2 +- apps/shared/storage_expression_model_store.h | 8 ++++--- poincare/include/poincare/global_context.h | 2 +- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/apps/graph/storage_cartesian_function.cpp b/apps/graph/storage_cartesian_function.cpp index 931923fc3..fcfedb911 100644 --- a/apps/graph/storage_cartesian_function.cpp +++ b/apps/graph/storage_cartesian_function.cpp @@ -9,6 +9,30 @@ using namespace Shared; namespace Graph { +void StorageCartesianFunction::DefaultName(char buffer[], size_t bufferSize) { + /* a default name is "f[number].func", for instance "f12.func", that does not + * exist yet in the storage */ + size_t constantNameSize = 1 + 1 + strlen(GlobalContext::funcExtension) + 1; // 'f', '.', extension, null-terminating char + assert(bufferSize > constantNameSize); + // Write the f + buffer[0] = 'f'; + // Find the next available number + int currentNumber = 0; + int dotCharIndex = -1; + while (currentNumber < bufferSize - constantNameSize) { + dotCharIndex = 1 + Poincare::Integer(currentNumber).serialize(&buffer[1], bufferSize - constantNameSize + 1); + if (GlobalContext::RecordWithName(buffer).isNull()) { + // Name found + break; + } + currentNumber++; + } + // Write the extension + assert(dotCharIndex > 1); + buffer[dotCharIndex] = Ion::Storage::k_dotChar; + strlcpy(&buffer[dotCharIndex+1], GlobalContext::funcExtension, bufferSize - (dotCharIndex+1)); +} + StorageCartesianFunction::StorageCartesianFunction(const char * text, KDColor color) : StorageFunction(text, color), m_displayDerivative(false) //TODO @@ -56,5 +80,4 @@ Expression::Coordinate2D StorageCartesianFunction::nextIntersectionFrom(double s return reducedExp.nextIntersection(symbol(), start, step, max, *context, Preferences::sharedPreferences()->angleUnit(), reducedExp); } - } diff --git a/apps/graph/storage_cartesian_function.h b/apps/graph/storage_cartesian_function.h index 98701712c..b7b1485aa 100644 --- a/apps/graph/storage_cartesian_function.h +++ b/apps/graph/storage_cartesian_function.h @@ -10,7 +10,7 @@ namespace Graph { class StorageCartesianFunction : public Shared::StorageFunction { public: static const char * Extension() { return Poincare::GlobalContext::funcExtension; } - static StorageCartesianFunction EmptyModel() { return StorageCartesianFunction("function.func", KDColorRed); } + static void DefaultName(char buffer[], size_t bufferSize); StorageCartesianFunction(const char * text = nullptr, KDColor color = KDColorBlack); StorageCartesianFunction(Ion::Storage::Record record); bool operator==(const StorageCartesianFunction & other) const { return record() == other.record(); } diff --git a/apps/shared/storage_expression_model_store.h b/apps/shared/storage_expression_model_store.h index 72de487c2..09f4e5ec8 100644 --- a/apps/shared/storage_expression_model_store.h +++ b/apps/shared/storage_expression_model_store.h @@ -51,10 +51,12 @@ public: // Add and Remove T addEmptyModel() { - T newModel = T::EmptyModel(); + char nameBuffer[100]; + T::DefaultName(nameBuffer, 100); // TODO: create the record data (color, isActive and expression) - Ion::Storage::sharedStorage()->createRecordWithFullName(newModel.name(), nullptr, 0); //TODO - return newModel; + // T::EmptyModelData + Ion::Storage::sharedStorage()->createRecordWithFullName(nameBuffer, nullptr, 0); + return T(Ion::Storage::sharedStorage()->recordNamed(nameBuffer)); } void removeModel(T f) { f.destroy(); diff --git a/poincare/include/poincare/global_context.h b/poincare/include/poincare/global_context.h index c46746e26..94585c171 100644 --- a/poincare/include/poincare/global_context.h +++ b/poincare/include/poincare/global_context.h @@ -19,13 +19,13 @@ public: * Otherwise, we would need the context and the angle unit to evaluate it */ const Expression expressionForSymbol(const Symbol & symbol) override; void setExpressionForSymbol(const Expression & expression, const Symbol & symbol, Context & context) override; + static Ion::Storage::Record RecordWithName(const char * name); static bool storageMemoryFull(); //TODO static constexpr uint16_t k_maxNumberOfSequences = 10; private: static constexpr char expExtension[] = "exp"; //static constexpr char seqExtension[] = "seq"; static const char * ExtensionForExpression(const Expression & exp); - static Ion::Storage::Record RecordWithName(const char * name); }; }