From 7ccce750a6855b12740d984c5e6551692ca52a1c Mon Sep 17 00:00:00 2001 From: Ted John Date: Sat, 25 Feb 2017 15:23:47 +0000 Subject: [PATCH] Use std::string instead of char * for UriHandler --- src/openrct2/cmdline/UriHandler.cpp | 72 ++++++++++++++--------------- src/openrct2/core/String.cpp | 30 ++++-------- src/openrct2/core/String.hpp | 3 +- 3 files changed, 47 insertions(+), 58 deletions(-) diff --git a/src/openrct2/cmdline/UriHandler.cpp b/src/openrct2/cmdline/UriHandler.cpp index 041f6c0bb9..b935e36439 100644 --- a/src/openrct2/cmdline/UriHandler.cpp +++ b/src/openrct2/cmdline/UriHandler.cpp @@ -18,14 +18,11 @@ #include "../core/Memory.hpp" #include "../core/String.hpp" #include "../network/network.h" +#include "../OpenRCT2.h" #include "CommandLine.hpp" -extern "C" -{ - #include "../openrct2.h" -} - -static exitcode_t HandleUri(const utf8 * uri); +static exitcode_t HandleUri(const std::string &uri); +static bool TryParseHostnamePort(const std::string &hostnamePort, std::string * outHostname, sint32 * outPort, sint32 defaultPort); exitcode_t CommandLine::HandleCommandUri(CommandLineArgEnumerator * enumerator) { @@ -43,36 +40,22 @@ exitcode_t CommandLine::HandleCommandUri(CommandLineArgEnumerator * enumerator) return EXITCODE_FAIL; } -static exitcode_t HandleUri(const utf8 * uri) +static exitcode_t HandleUri(const std::string &uri) { - utf8 * * args; - size_t numArgs = String::Split(&args, uri, '/'); - if (numArgs > 0) + auto args = String::Split(uri, "/"); + if (args.size() > 0) { - utf8 * arg = args[0]; - if (String::Equals(arg, "join")) + std::string arg = args[0]; + if (arg == "join") { - if (numArgs > 1) + std::string hostname; + sint32 port; + if (args.size() > 1 && TryParseHostnamePort(args[1], &hostname, &port, NETWORK_DEFAULT_PORT)) { - utf8 * hostnamePort = args[1]; - - // Argument is in hostname:port format, so we need to split - utf8 * hostname = String::Duplicate(hostnamePort); - sint32 port = NETWORK_DEFAULT_PORT; - size_t colonIndex = String::IndexOf(hostnamePort, ':'); - if (colonIndex != SIZE_MAX) - { - Memory::Free(hostname); - hostname = String::Substring(hostnamePort, 0, colonIndex); - port = atoi(hostnamePort + colonIndex + 1); - } - // Set the network start configuration gNetworkStart = NETWORK_MODE_CLIENT; - String::Set(gNetworkStartHost, sizeof(gNetworkStartHost), hostname); + String::Set(gNetworkStartHost, sizeof(gNetworkStartHost), hostname.c_str()); gNetworkStartPort = port; - - Memory::Free(hostname); } else { @@ -81,13 +64,28 @@ static exitcode_t HandleUri(const utf8 * uri) } } } - - // Clean up - for (size_t i = 0; i < numArgs; i++) - { - Memory::Free(args[i]); - } - Memory::FreeArray(args, numArgs); - return EXITCODE_CONTINUE; } + +static bool TryParseHostnamePort(const std::string &hostnamePort, std::string * outHostname, sint32 * outPort, sint32 defaultPort) +{ + try + { + // Argument is in hostname:port format, so we need to split + std::string hostname = hostnamePort; + sint32 port = defaultPort; + size_t colonIndex = hostnamePort.find_first_of(':'); + if (colonIndex != std::string::npos) + { + hostname = hostnamePort.substr(0, colonIndex); + port = std::stoi(hostnamePort.substr(colonIndex + 1)); + } + *outPort = port; + *outHostname = hostname; + return true; + } + catch (const std::exception &) + { + return false; + } +} diff --git a/src/openrct2/core/String.cpp b/src/openrct2/core/String.cpp index 15e80d2490..b91061744e 100644 --- a/src/openrct2/core/String.cpp +++ b/src/openrct2/core/String.cpp @@ -354,38 +354,28 @@ namespace String return result; } - size_t Split(utf8 * * * values, const utf8 * buffer, utf8 delimiter) + std::vector Split(const std::string &s, const std::string &delimiter) { - std::vector valuesList; + std::vector results; size_t index = 0; size_t nextIndex; do { - nextIndex = String::IndexOf(buffer, '/', index); - utf8 * value; - if (nextIndex == SIZE_MAX) + nextIndex = s.find_first_of(delimiter, index); + std::string value; + if (nextIndex == std::string::npos) { - value = String::Substring(buffer, index); + value = s.substr(index); } else { - value = String::Substring(buffer, index, nextIndex - index); + value = s.substr(index, nextIndex - index); } - valuesList.push_back(value); + results.push_back(value); index = nextIndex + 1; - } while (nextIndex != SIZE_MAX); - - *values = nullptr; - if (valuesList.size() > 0) - { - utf8 * * valuesArray = Memory::AllocateArray(valuesList.size()); - for (size_t i = 0; i < valuesList.size(); i++) - { - valuesArray[i] = valuesList[i]; - } - *values = valuesArray; } - return valuesList.size(); + while (nextIndex != SIZE_MAX); + return results; } utf8 * SkipBOM(utf8 * buffer) diff --git a/src/openrct2/core/String.hpp b/src/openrct2/core/String.hpp index 1416764dce..4543a3fd97 100644 --- a/src/openrct2/core/String.hpp +++ b/src/openrct2/core/String.hpp @@ -17,6 +17,7 @@ #pragma once #include +#include #include "../common.h" namespace String @@ -79,7 +80,7 @@ namespace String * Splits the given string by a delimiter and returns the values as a new string array. * @returns the number of values. */ - size_t Split(utf8 * * * values, const utf8 * buffer, utf8 delimiter); + std::vector Split(const std::string &s, const std::string &delimiter); utf8 * SkipBOM(utf8 * buffer); const utf8 * SkipBOM(const utf8 * buffer);