From 4b23552ea8e35fab97f04d3de96b62416cb55fb5 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 8 Nov 2019 01:02:53 +0800 Subject: [PATCH] Add function to extract stk version and OS from user agent --- src/utils/string_utils.cpp | 17 +++++++++++++++++ src/utils/string_utils.hpp | 3 +++ 2 files changed, 20 insertions(+) diff --git a/src/utils/string_utils.cpp b/src/utils/string_utils.cpp index 39a650a5c..68ec90486 100644 --- a/src/utils/string_utils.cpp +++ b/src/utils/string_utils.cpp @@ -1377,6 +1377,23 @@ namespace StringUtils assert(versionToInt("1.0-rc1" ) == 10000021); // same as 1-rc1 } // unitTesting // ------------------------------------------------------------------------ + std::pair extractVersionOS( + const std::string& user_agent) + { + std::pair ret; + // '#^(SuperTuxKart/[a-z0-9\\.\\-_]+)( \\(.*\\))?$#' + std::vector out = split(user_agent, '/'); + if (out.size() != 2 || out[1].empty() || out[1].back() != ')') + return ret; + std::vector out2 = split(out[1], '('); + if (out2.size() != 2 || out2[0].empty() || out2[0].back() != ' ' || + out2[1].size() < 2) + return ret; + ret.first = out2[0].substr(0, out2[0].size() - 1); + ret.second = out2[1].substr(0, out2[1].size() - 1); + return ret; + } // extractVersionOS + // ------------------------------------------------------------------------ std::string getUserAgentString() { std::string uagent(std::string("SuperTuxKart/") + STK_VERSION); diff --git a/src/utils/string_utils.hpp b/src/utils/string_utils.hpp index 1ad6e52e5..c89128d54 100644 --- a/src/utils/string_utils.hpp +++ b/src/utils/string_utils.hpp @@ -300,6 +300,9 @@ namespace StringUtils */ std::string getHostNameFromURL(const std::string& url); // ------------------------------------------------------------------------ + std::pair extractVersionOS( + const std::string& user_agent); + // ------------------------------------------------------------------------ /* Get line from istream with taking into account for its line ending. */ inline std::istream& safeGetline(std::istream& is, std::string& t) {