Add function to extract stk version and OS from user agent

This commit is contained in:
Benau 2019-11-08 01:02:53 +08:00
parent 9d75b706d5
commit 4b23552ea8
2 changed files with 20 additions and 0 deletions

View File

@ -1377,6 +1377,23 @@ namespace StringUtils
assert(versionToInt("1.0-rc1" ) == 10000021); // same as 1-rc1
} // unitTesting
// ------------------------------------------------------------------------
std::pair<std::string, std::string> extractVersionOS(
const std::string& user_agent)
{
std::pair<std::string, std::string> ret;
// '#^(SuperTuxKart/[a-z0-9\\.\\-_]+)( \\(.*\\))?$#'
std::vector<std::string> out = split(user_agent, '/');
if (out.size() != 2 || out[1].empty() || out[1].back() != ')')
return ret;
std::vector<std::string> 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);

View File

@ -300,6 +300,9 @@ namespace StringUtils
*/
std::string getHostNameFromURL(const std::string& url);
// ------------------------------------------------------------------------
std::pair<std::string, std::string> 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)
{