summaryrefslogtreecommitdiff
path: root/src/vehicle_cmd.cpp
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2020-05-17 23:31:59 +0200
committerMichael Lutz <michi@icosahedron.de>2020-05-21 20:02:34 +0200
commit63ccb36ef3ecd53a503c0e9fea87f0784802af0f (patch)
treed3e1134029fd437d1ad2d948f83592b930e9456f /src/vehicle_cmd.cpp
parent9b6f5e3bb850b6cff02cc761767fcdf49c681645 (diff)
downloadopenttd-63ccb36ef3ecd53a503c0e9fea87f0784802af0f.tar.xz
Codechange: Use std::string for most of the user-settable custom names.
Diffstat (limited to 'src/vehicle_cmd.cpp')
-rw-r--r--src/vehicle_cmd.cpp49
1 files changed, 30 insertions, 19 deletions
diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp
index 2f0a49ca2..bb1574d4f 100644
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -30,6 +30,8 @@
#include "newgrf.h"
#include "company_base.h"
#include "core/random_func.hpp"
+#include <sstream>
+#include <iomanip>
#include "table/strings.h"
@@ -749,7 +751,7 @@ CommandCost CmdDepotMassAutoReplace(TileIndex tile, DoCommandFlag flags, uint32
static bool IsUniqueVehicleName(const char *name)
{
for (const Vehicle *v : Vehicle::Iterate()) {
- if (v->name != nullptr && strcmp(v->name, name) == 0) return false;
+ if (!v->name.empty() && v->name == name) return false;
}
return true;
@@ -762,42 +764,48 @@ static bool IsUniqueVehicleName(const char *name)
*/
static void CloneVehicleName(const Vehicle *src, Vehicle *dst)
{
- char buf[256];
+ std::string buf;
/* Find the position of the first digit in the last group of digits. */
size_t number_position;
- for (number_position = strlen(src->name); number_position > 0; number_position--) {
+ for (number_position = src->name.length(); number_position > 0; number_position--) {
/* The design of UTF-8 lets this work simply without having to check
* for UTF-8 sequences. */
if (src->name[number_position - 1] < '0' || src->name[number_position - 1] > '9') break;
}
/* Format buffer and determine starting number. */
- int num;
+ long num;
byte padding = 0;
- if (number_position == strlen(src->name)) {
+ if (number_position == src->name.length()) {
/* No digit at the end, so start at number 2. */
- strecpy(buf, src->name, lastof(buf));
- strecat(buf, " ", lastof(buf));
- number_position = strlen(buf);
+ buf = src->name;
+ buf += " ";
+ number_position = buf.length();
num = 2;
} else {
/* Found digits, parse them and start at the next number. */
- strecpy(buf, src->name, lastof(buf));
- buf[number_position] = '\0';
- char *endptr;
- num = strtol(&src->name[number_position], &endptr, 10) + 1;
- padding = endptr - &src->name[number_position];
+ buf = src->name.substr(0, number_position);
+
+ auto num_str = src->name.substr(number_position);
+ padding = (byte)num_str.length();
+
+ std::istringstream iss(num_str);
+ iss >> num;
+ num++;
}
/* Check if this name is already taken. */
for (int max_iterations = 1000; max_iterations > 0; max_iterations--, num++) {
+ std::ostringstream oss;
+
/* Attach the number to the temporary name. */
- seprintf(&buf[number_position], lastof(buf), "%0*d", padding, num);
+ oss << buf << std::setw(padding) << std::setfill('0') << std::internal << num;
/* Check the name is unique. */
- if (IsUniqueVehicleName(buf)) {
- dst->name = stredup(buf);
+ auto new_name = oss.str();
+ if (IsUniqueVehicleName(new_name.c_str())) {
+ dst->name = new_name;
break;
}
}
@@ -973,7 +981,7 @@ CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
DoCommand(0, w_front->index | (p2 & 1 ? CO_SHARE : CO_COPY) << 30, v_front->index, flags, CMD_CLONE_ORDER);
/* Now clone the vehicle's name, if it has one. */
- if (v_front->name != nullptr) CloneVehicleName(v_front, w_front);
+ if (!v_front->name.empty()) CloneVehicleName(v_front, w_front);
}
/* Since we can't estimate the cost of cloning a vehicle accurately we must
@@ -1074,8 +1082,11 @@ CommandCost CmdRenameVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uin
}
if (flags & DC_EXEC) {
- free(v->name);
- v->name = reset ? nullptr : stredup(text);
+ if (reset) {
+ v->name.clear();
+ } else {
+ v->name = text;
+ }
InvalidateWindowClassesData(GetWindowClassForVehicleType(v->type), 1);
MarkWholeScreenDirty();
}