mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-17 09:22:42 +01:00
Codechange: remove char* from base driver code
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
/** @file driver.cpp Base for all driver handling. */
|
||||
|
||||
#include "stdafx.h"
|
||||
#include "core/string_consumer.hpp"
|
||||
#include "debug.h"
|
||||
#include "error.h"
|
||||
#include "error_func.h"
|
||||
@@ -43,18 +44,18 @@ static const std::string HWACCELERATION_TEST_FILE = "hwaccel.dat"; ///< Filename
|
||||
* @param name The parameter name we're looking for.
|
||||
* @return The parameter value.
|
||||
*/
|
||||
const char *GetDriverParam(const StringList &parm, const char *name)
|
||||
std::optional<std::string_view> GetDriverParam(const StringList &parm, std::string_view name)
|
||||
{
|
||||
if (parm.empty()) return nullptr;
|
||||
if (parm.empty()) return std::nullopt;
|
||||
|
||||
size_t len = strlen(name);
|
||||
for (auto &p : parm) {
|
||||
if (p.compare(0, len, name) == 0) {
|
||||
if (p.length() == len) return "";
|
||||
if (p[len] == '=') return p.c_str() + len + 1;
|
||||
StringConsumer consumer{p};
|
||||
if (consumer.ReadIf(name)) {
|
||||
if (!consumer.AnyBytesLeft()) return "";
|
||||
if (consumer.ReadIf("=")) return consumer.GetLeftData();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,9 +64,9 @@ const char *GetDriverParam(const StringList &parm, const char *name)
|
||||
* @param name The parameter name we're looking for.
|
||||
* @return The parameter value.
|
||||
*/
|
||||
bool GetDriverParamBool(const StringList &parm, const char *name)
|
||||
bool GetDriverParamBool(const StringList &parm, std::string_view name)
|
||||
{
|
||||
return GetDriverParam(parm, name) != nullptr;
|
||||
return GetDriverParam(parm, name).has_value();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,13 +76,13 @@ bool GetDriverParamBool(const StringList &parm, const char *name)
|
||||
* @param def The default value if the parameter doesn't exist.
|
||||
* @return The parameter value.
|
||||
*/
|
||||
int GetDriverParamInt(const StringList &parm, const char *name, int def)
|
||||
int GetDriverParamInt(const StringList &parm, std::string_view name, int def)
|
||||
{
|
||||
const char *p = GetDriverParam(parm, name);
|
||||
if (p == nullptr) return def;
|
||||
auto value = ParseInteger<int>(p);
|
||||
auto p = GetDriverParam(parm, name);
|
||||
if (!p.has_value()) return def;
|
||||
auto value = ParseInteger<int>(*p);
|
||||
if (value.has_value()) return *value;
|
||||
UserError("Invalid value for driver parameter {}: {}", name, p);
|
||||
UserError("Invalid value for driver parameter {}: {}", name, *p);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -241,7 +242,7 @@ void DriverFactoryBase::GetDriversInfo(std::back_insert_iterator<std::string> &o
|
||||
* @param name The name of the driver.
|
||||
* @param description A long-ish description of the driver.
|
||||
*/
|
||||
DriverFactoryBase::DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description) :
|
||||
DriverFactoryBase::DriverFactoryBase(Driver::Type type, int priority, std::string_view name, std::string_view description) :
|
||||
type(type), priority(priority), name(name), description(description)
|
||||
{
|
||||
/* Prefix the name with driver type to make it unique */
|
||||
|
||||
Reference in New Issue
Block a user