Skip to content

Commit 87367d6

Browse files
Make sure Keys and Items are proper length
1 parent ad7e713 commit 87367d6

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

qtfred/src/mission/dialogs/VariableDialogModel.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,6 @@ SCP_string VariableDialogModel::copyContainer(int index)
11791179
}
11801180

11811181
_containerItems.back().name = newName;
1182-
_containerItems.back().name = _containerItems.back().name.substr(0, TOKEN_LENGTH - 1);
11831182
return _containerItems.back().name;
11841183
}
11851184

@@ -1193,6 +1192,7 @@ SCP_string VariableDialogModel::changeContainerName(int index, SCP_string newNam
11931192
}
11941193

11951194
// We cannot have two containers with the same name, but we need to check that somewhere else (like on accept attempt).
1195+
// Otherwise editing variables and containers becomes super annoying.
11961196
container->name = newName.substr(0, TOKEN_LENGTH - 1);
11971197
return container->name;
11981198
}
@@ -1258,7 +1258,7 @@ SCP_string VariableDialogModel::addListItem(int index, SCP_string item)
12581258
}
12591259

12601260
if (container->string) {
1261-
container->stringValues.push_back(item);
1261+
container->stringValues.push_back(item.substr(0, TOKEN_LENGTH - 1));
12621262
return container->stringValues.back();
12631263
} else {
12641264
auto temp = trimIntegerString(item);
@@ -1332,6 +1332,7 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::addMapItem(int index)
13321332
return ret;
13331333
}
13341334

1335+
// Overload for specified key and/or Value
13351336
std::pair<SCP_string, SCP_string> VariableDialogModel::addMapItem(int index, SCP_string key, SCP_string value)
13361337
{
13371338
auto container = lookupContainer(index);
@@ -1368,7 +1369,7 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::addMapItem(int index, SCP
13681369
} while (conflict && count < 101);
13691370
} else {
13701371
if (container->stringKeys){
1371-
newKey = key;
1372+
newKey = key.substr(0, TOKEN_LENGTH - 1);
13721373
} else {
13731374
newKey = trimIntegerString(key);
13741375
}
@@ -1382,7 +1383,7 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::addMapItem(int index, SCP
13821383
container->keys.push_back(ret.first);
13831384

13841385
if (container->string) {
1385-
ret.second = value;
1386+
ret.second = value.substr(0, TOKEN_LENGTH - 1);
13861387
container->stringValues.push_back(ret.second);
13871388
container->numberValues.push_back(0);
13881389
} else {
@@ -1436,7 +1437,7 @@ SCP_string VariableDialogModel::changeListItem(int containerIndex, int index, SC
14361437
return "";
14371438
}
14381439

1439-
*listItem = newString;
1440+
*listItem = newString.substr(0, TOKEN_LENGTH - 1);
14401441

14411442
} else {
14421443
auto listItem = lookupContainerNumberItem(containerIndex, index);
@@ -1500,7 +1501,7 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::copyMapItem(int index, in
15001501

15011502
auto key = lookupContainerKey(index, mapIndex);
15021503

1503-
if (!key) {
1504+
if (key == nullptr) {
15041505
return std::make_pair("", "");
15051506
}
15061507

@@ -1509,13 +1510,14 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::copyMapItem(int index, in
15091510
if (container->string){
15101511
auto value = lookupContainerStringItem(index, mapIndex);
15111512

1512-
// no valid value.
1513-
if (!value){
1513+
// not a valid value.
1514+
if (value == nullptr){
15141515
return std::make_pair("", "");
15151516
}
15161517

15171518
SCP_string copyValue = *value;
1518-
SCP_string newKey = *key + "0";
1519+
SCP_string baseNewKey = key->substr(0, TOKEN_LENGTH - 4);
1520+
SCP_string newKey = baseNewKey + "0";
15191521
int count = 0;
15201522

15211523
bool found;
@@ -1531,10 +1533,10 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::copyMapItem(int index, in
15311533

15321534
// attempt did not work, try next number
15331535
if (found) {
1534-
sprintf(newKey, "%s%i", key->c_str(), ++count);
1536+
sprintf(newKey, "%s%i", baseNewKey.c_str(), ++count);
15351537
}
15361538

1537-
} while (found && count < 100);
1539+
} while (found && count < 999);
15381540

15391541
// we could not generate a new key .... somehow.
15401542
if (found){
@@ -1557,7 +1559,8 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::copyMapItem(int index, in
15571559
}
15581560

15591561
int copyValue = *value;
1560-
SCP_string newKey = *key + "0";
1562+
SCP_string baseNewKey = key->substr(0, TOKEN_LENGTH - 4);
1563+
SCP_string newKey = baseNewKey + "0";
15611564
int count = 0;
15621565

15631566
bool found;
@@ -1573,7 +1576,7 @@ std::pair<SCP_string, SCP_string> VariableDialogModel::copyMapItem(int index, in
15731576

15741577
// attempt did not work, try next number
15751578
if (found) {
1576-
sprintf(newKey, "%s%i", key->c_str(), ++count);
1579+
sprintf(newKey, "%s%i", baseNewKey.c_str(), ++count);
15771580
}
15781581

15791582
} while (found && count < 100);
@@ -1706,7 +1709,7 @@ SCP_string VariableDialogModel::changeMapItemKey(int index, int keyRow, SCP_stri
17061709
}
17071710

17081711
if (container->stringKeys){
1709-
container->keys[keyRow] = newKey;
1712+
container->keys[keyRow] = newKey.substr(0, TOKEN_LENGTH - 1);
17101713
} else {
17111714
container->keys[keyRow] = trimIntegerString(newKey);
17121715
}
@@ -1723,7 +1726,7 @@ SCP_string VariableDialogModel::changeMapItemStringValue(int index, int itemInde
17231726
return "";
17241727
}
17251728

1726-
*item = newValue;
1729+
*item = newValue.substr(0, TKEN_LENGTH - 1);
17271730

17281731
return *item;
17291732
}
@@ -1936,13 +1939,15 @@ const SCP_vector<std::array<SCP_string, 3>> VariableDialogModel::getVariableValu
19361939
if (!safeToAlterVariable(item)){
19371940
notes = "Referenced";
19381941
} else if (item.deleted){
1939-
notes = "Deleted";
1942+
notes = "To Be Deleted";
19401943
} else if (item.originalName == ""){
19411944
notes = "New";
1942-
} else if (item.name != item.originalName){
1943-
notes = "Renamed";
19441945
} else if ((item.string && item.stringValue == "") || (!item.string && item.numberValue == 0)){
19451946
notes = "Default Value";
1947+
} else if (item.name != item.originalName){
1948+
notes = "Renamed";
1949+
} else {
1950+
notes = "Unreferenced";
19461951
}
19471952

19481953
SCP_string temp;
@@ -2091,7 +2096,7 @@ const SCP_vector<std::array<SCP_string, 3>> VariableDialogModel::getContainerNam
20912096
if (!safeToAlterContainer(item)){
20922097
notes = "Referenced";
20932098
} else if (item.deleted) {
2094-
notes = "Deleted";
2099+
notes = "To Be Deleted";
20952100
} else if (item.originalName == "") {
20962101
notes = "New";
20972102
} else if (item.name != item.originalName){
@@ -2100,6 +2105,8 @@ const SCP_vector<std::array<SCP_string, 3>> VariableDialogModel::getContainerNam
21002105
notes = "Empty Map";
21012106
} else if (item.list && ((item.string && item.stringValues.empty()) || (!item.string && item.numberValues.empty()))){
21022107
notes = "Empty List";
2108+
} else {
2109+
notes = "Unreferenced";
21032110
}
21042111

21052112
outStrings.push_back(std::array<SCP_string, 3>{item.name, type, notes});

qtfred/src/ui/dialogs/VariableDialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class VariableDialog : public QDialog {
8484
SCP_string _currentContainerItemCol1 = "";
8585
SCP_string _currentContainerItemCol2 = "";
8686

87-
void override reject()
87+
void reject() override
8888
{
8989
QMessageBox msgBox;
9090
msgBox.setText("Are you sure you want to discard your changes?");

0 commit comments

Comments
 (0)