[apps/code] Handle too many Script default names used

When adding to many scripts without naming them, one can overflow the
number of default generated script names, in which case we force the
user to enter a name.
This commit is contained in:
Léa Saviot
2018-11-05 17:58:42 +01:00
committed by Émilie Feral
parent 0f9d1db851
commit e74e15eaf7
5 changed files with 32 additions and 20 deletions

View File

@@ -302,15 +302,22 @@ bool MenuController::textFieldDidFinishEditing(TextField * textField, const char
static constexpr int bufferSize = Script::k_defaultScriptNameMaxSize + 1 + ScriptStore::k_scriptExtensionLength; //"script99" + "." + "py"
char numberedDefaultName[bufferSize];
if (strlen(text) <= 1 + strlen(ScriptStore::k_scriptExtension)) {
if (strlen(text) > 1 + strlen(ScriptStore::k_scriptExtension)) {
newName = text;
} else {
// The user entered an empty name. Use a numbered default script name.
Script::DefaultName(numberedDefaultName, Script::k_defaultScriptNameMaxSize);
bool foundDefaultName = Script::DefaultName(numberedDefaultName, Script::k_defaultScriptNameMaxSize);
int defaultNameLength = strlen(numberedDefaultName);
numberedDefaultName[defaultNameLength++] = '.';
strlcpy(&numberedDefaultName[defaultNameLength], ScriptStore::k_scriptExtension, bufferSize - defaultNameLength);
/* If there are already scripts named script1.py, script2.py,... until
* Script::k_maxNumberOfDefaultScriptNames, we want to write the last tried
* default name and let the user modify it. */
if (!foundDefaultName) {
textField->setText(numberedDefaultName);
textField->setCursorLocation(defaultNameLength);
}
newName = const_cast<const char *>(numberedDefaultName);
} else {
newName = text;
}
Script::ErrorStatus error = Script::nameCompliant(newName) ? m_scriptStore->scriptAtIndex(m_selectableTableView.selectedRow()).setName(newName) : Script::ErrorStatus::NonCompliantName;
if (error == Script::ErrorStatus::None) {