changeset 18927:dcd4ce0e5ea9 draft

(svn r23777) -Codechange: refactor allocating memory and fetching strings into a single function for scripts
author rubidium <rubidium@openttd.org>
date Sun, 08 Jan 2012 21:48:05 +0000
parents 54a38a9a4fa9
children cb724321a831
files src/script/api/script_basestation.cpp src/script/api/script_bridge.cpp src/script/api/script_company.cpp src/script/api/script_engine.cpp src/script/api/script_event_types.cpp src/script/api/script_group.cpp src/script/api/script_industry.cpp src/script/api/script_industrytype.cpp src/script/api/script_object.cpp src/script/api/script_object.hpp src/script/api/script_rail.cpp src/script/api/script_sign.cpp src/script/api/script_town.cpp src/script/api/script_vehicle.cpp
diffstat 14 files changed, 27 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/src/script/api/script_basestation.cpp
+++ b/src/script/api/script_basestation.cpp
@@ -27,12 +27,8 @@
 {
 	if (!IsValidBaseStation(station_id)) return NULL;
 
-	static const int len = 64;
-	char *name = MallocT<char>(len);
-
 	::SetDParam(0, station_id);
-	::GetString(name, ::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME, &name[len - 1]);
-	return name;
+	return GetString(::Station::IsValidID(station_id) ? STR_STATION_NAME : STR_WAYPOINT_NAME);
 }
 
 /* static */ bool ScriptBaseStation::SetName(StationID station_id, Text *name)
--- a/src/script/api/script_bridge.cpp
+++ b/src/script/api/script_bridge.cpp
@@ -136,11 +136,7 @@
 {
 	if (!IsValidBridge(bridge_id)) return NULL;
 
-	static const int len = 64;
-	char *bridge_name = MallocT<char>(len);
-
-	::GetString(bridge_name, ::GetBridgeSpec(bridge_id)->transport_name[0], &bridge_name[len - 1]);
-	return bridge_name;
+	return GetString(::GetBridgeSpec(bridge_id)->transport_name[0]);
 }
 
 /* static */ int32 ScriptBridge::GetMaxSpeed(BridgeID bridge_id)
--- a/src/script/api/script_company.cpp
+++ b/src/script/api/script_company.cpp
@@ -55,12 +55,8 @@
 	company = ResolveCompanyID(company);
 	if (company == COMPANY_INVALID) return NULL;
 
-	static const int len = 64;
-	char *company_name = MallocT<char>(len);
-
 	::SetDParam(0, company);
-	::GetString(company_name, STR_COMPANY_NAME, &company_name[len - 1]);
-	return company_name;
+	return GetString(STR_COMPANY_NAME);
 }
 
 /* static */ bool ScriptCompany::SetPresidentName(Text *name)
--- a/src/script/api/script_engine.cpp
+++ b/src/script/api/script_engine.cpp
@@ -36,12 +36,8 @@
 {
 	if (!IsValidEngine(engine_id)) return NULL;
 
-	static const int len = 64;
-	char *engine_name = MallocT<char>(len);
-
 	::SetDParam(0, engine_id);
-	::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
-	return engine_name;
+	return GetString(STR_ENGINE_NAME);
 }
 
 /* static */ CargoID ScriptEngine::GetCargoType(EngineID engine_id)
--- a/src/script/api/script_event_types.cpp
+++ b/src/script/api/script_event_types.cpp
@@ -28,12 +28,9 @@
 char *ScriptEventEnginePreview::GetName()
 {
 	if (!this->IsEngineValid()) return NULL;
-	static const int len = 64;
-	char *engine_name = MallocT<char>(len);
 
 	::SetDParam(0, this->engine);
-	::GetString(engine_name, STR_ENGINE_NAME, &engine_name[len - 1]);
-	return engine_name;
+	return GetString(STR_ENGINE_NAME);
 }
 
 CargoID ScriptEventEnginePreview::GetCargoType()
--- a/src/script/api/script_group.cpp
+++ b/src/script/api/script_group.cpp
@@ -64,12 +64,8 @@
 {
 	if (!IsValidGroup(group_id)) return NULL;
 
-	static const int len = 64;
-	char *group_name = MallocT<char>(len);
-
 	::SetDParam(0, group_id);
-	::GetString(group_name, STR_GROUP_NAME, &group_name[len - 1]);
-	return group_name;
+	return GetString(STR_GROUP_NAME);
 }
 
 /* static */ bool ScriptGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable)
--- a/src/script/api/script_industry.cpp
+++ b/src/script/api/script_industry.cpp
@@ -38,13 +38,9 @@
 /* static */ char *ScriptIndustry::GetName(IndustryID industry_id)
 {
 	if (!IsValidIndustry(industry_id)) return NULL;
-	static const int len = 64;
-	char *industry_name = MallocT<char>(len);
 
 	::SetDParam(0, industry_id);
-	::GetString(industry_name, STR_INDUSTRY_NAME, &industry_name[len - 1]);
-
-	return industry_name;
+	return GetString(STR_INDUSTRY_NAME);
 }
 
 /* static */ ScriptIndustry::CargoAcceptState ScriptIndustry::IsCargoAccepted(IndustryID industry_id, CargoID cargo_id)
--- a/src/script/api/script_industrytype.cpp
+++ b/src/script/api/script_industrytype.cpp
@@ -51,12 +51,8 @@
 /* static */ char *ScriptIndustryType::GetName(IndustryType industry_type)
 {
 	if (!IsValidIndustryType(industry_type)) return NULL;
-	static const int len = 64;
-	char *industrytype_name = MallocT<char>(len);
 
-	::GetString(industrytype_name, ::GetIndustrySpec(industry_type)->name, &industrytype_name[len - 1]);
-
-	return industrytype_name;
+	return GetString(::GetIndustrySpec(industry_type)->name);
 }
 
 /* static */ ScriptList *ScriptIndustryType::GetProducedCargo(IndustryType industry_type)
--- a/src/script/api/script_object.cpp
+++ b/src/script/api/script_object.cpp
@@ -16,6 +16,8 @@
 #include "../../company_base.h"
 #include "../../network/network.h"
 #include "../../genworld.h"
+#include "../../string_func.h"
+#include "../../strings_func.h"
 
 #include "../script_storage.hpp"
 #include "../script_instance.hpp"
@@ -233,6 +235,13 @@
 	return GetStorage()->log_data;
 }
 
+/* static */ char *ScriptObject::GetString(StringID string)
+{
+	char buffer[64];
+	::GetString(buffer, string, lastof(buffer));
+	return ::strdup(buffer);
+}
+
 /* static */ void ScriptObject::SetCallbackVariable(int index, int value)
 {
 	if ((size_t)index >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
--- a/src/script/api/script_object.hpp
+++ b/src/script/api/script_object.hpp
@@ -236,6 +236,11 @@
 	 */
 	static void *&GetLogPointer();
 
+	/**
+	 * Get an allocated string with all control codes stripped off.
+	 */
+	static char *GetString(StringID string);
+
 private:
 	/**
 	 * Store a new_vehicle_id per company.
--- a/src/script/api/script_rail.cpp
+++ b/src/script/api/script_rail.cpp
@@ -25,11 +25,7 @@
 {
 	if (!IsRailTypeAvailable(rail_type)) return NULL;
 
-	static const int len = 64;
-	char *railtype_name = MallocT<char>(len);
-
-	::GetString(railtype_name, GetRailTypeInfo((::RailType)rail_type)->strings.menu_text, &railtype_name[len - 1]);
-	return railtype_name;
+	return GetString(GetRailTypeInfo((::RailType)rail_type)->strings.menu_text);
 }
 
 /* static */ bool ScriptRail::IsRailTile(TileIndex tile)
--- a/src/script/api/script_sign.cpp
+++ b/src/script/api/script_sign.cpp
@@ -48,13 +48,8 @@
 {
 	if (!IsValidSign(sign_id)) return NULL;
 
-	static const int len = 64;
-	char *sign_name = MallocT<char>(len);
-
 	::SetDParam(0, sign_id);
-	::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
-
-	return sign_name;
+	return GetString(STR_SIGN_NAME);
 }
 
 /* static */ TileIndex ScriptSign::GetLocation(SignID sign_id)
--- a/src/script/api/script_town.cpp
+++ b/src/script/api/script_town.cpp
@@ -14,6 +14,7 @@
 #include "script_map.hpp"
 #include "script_error.hpp"
 #include "../../town.h"
+#include "../../string_func.h"
 #include "../../strings_func.h"
 #include "../../station_base.h"
 #include "../../landscape.h"
@@ -32,13 +33,9 @@
 /* static */ char *ScriptTown::GetName(TownID town_id)
 {
 	if (!IsValidTown(town_id)) return NULL;
-	static const int len = 64;
-	char *town_name = MallocT<char>(len);
 
 	::SetDParam(0, town_id);
-	::GetString(town_name, STR_TOWN_NAME, &town_name[len - 1]);
-
-	return town_name;
+	return GetString(STR_TOWN_NAME);
 }
 
 /* static */ bool ScriptTown::SetText(TownID town_id, Text *text)
--- a/src/script/api/script_vehicle.cpp
+++ b/src/script/api/script_vehicle.cpp
@@ -270,12 +270,8 @@
 {
 	if (!IsValidVehicle(vehicle_id)) return NULL;
 
-	static const int len = 64;
-	char *vehicle_name = MallocT<char>(len);
-
 	::SetDParam(0, vehicle_id);
-	::GetString(vehicle_name, STR_VEHICLE_NAME, &vehicle_name[len - 1]);
-	return vehicle_name;
+	return GetString(STR_VEHICLE_NAME);
 }
 
 /* static */ int32 ScriptVehicle::GetAge(VehicleID vehicle_id)