2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-26 09:49:07 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2017-12-20 07:06:26 -06:00
|
|
|
#include <config.h>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
|
2017-03-08 10:38:22 -06:00
|
|
|
#include "Protocol.hpp"
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
#include <cassert>
|
2015-04-14 09:50:38 -05:00
|
|
|
#include <cstring>
|
2015-04-27 13:09:27 -05:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2015-03-26 09:49:07 -05:00
|
|
|
|
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
|
|
|
|
|
|
|
|
namespace LOOLProtocol
|
|
|
|
{
|
2016-02-23 02:22:18 -06:00
|
|
|
std::tuple<int, int, std::string> ParseVersion(const std::string& version)
|
2016-01-06 11:00:44 -06:00
|
|
|
{
|
2016-02-23 02:22:18 -06:00
|
|
|
int major = -1;
|
|
|
|
int minor = -1;
|
2016-01-06 11:00:44 -06:00
|
|
|
std::string patch;
|
|
|
|
|
2019-11-08 07:50:33 -06:00
|
|
|
std::vector<std::string> firstTokens(tokenize(version, '.'));
|
|
|
|
if (firstTokens.size() > 0)
|
2016-01-06 11:00:44 -06:00
|
|
|
{
|
|
|
|
major = std::stoi(firstTokens[0]);
|
|
|
|
|
2019-11-08 07:50:33 -06:00
|
|
|
std::vector<std::string> secondTokens(tokenize(firstTokens[1], '-'));
|
2016-01-06 11:00:44 -06:00
|
|
|
minor = std::stoi(secondTokens[0]);
|
|
|
|
|
2019-11-08 07:50:33 -06:00
|
|
|
if (secondTokens.size() > 1)
|
2016-01-06 11:00:44 -06:00
|
|
|
patch = secondTokens[1];
|
|
|
|
}
|
|
|
|
return std::make_tuple(major, minor, patch);
|
|
|
|
}
|
|
|
|
|
2016-01-12 05:07:13 -06:00
|
|
|
bool stringToInteger(const std::string& input, int& value)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
value = std::stoi(input);
|
|
|
|
}
|
|
|
|
catch (std::invalid_argument&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-14 11:28:07 -05:00
|
|
|
bool stringToUInt32(const std::string& input, uint32_t& value)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
value = std::stoul(input);
|
|
|
|
}
|
|
|
|
catch (std::invalid_argument&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-04 06:36:13 -06:00
|
|
|
bool stringToUInt64(const std::string& input, uint64_t& value)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
value = std::stoull(input);
|
|
|
|
}
|
|
|
|
catch (std::invalid_argument&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
bool getTokenInteger(const std::string& token, const std::string& name, int& value)
|
|
|
|
{
|
2017-01-30 19:43:27 -06:00
|
|
|
if (token.size() > (name.size() + 1) &&
|
|
|
|
token.compare(0, name.size(), name) == 0 &&
|
|
|
|
token[name.size()] == '=')
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
2017-01-30 19:43:27 -06:00
|
|
|
const char* str = token.data() + name.size() + 1;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
value = strtol(str, &endptr, 10);
|
|
|
|
return (endptr > str);
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
2015-04-27 13:09:27 -05:00
|
|
|
|
2017-01-30 19:43:27 -06:00
|
|
|
return false;
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
|
|
|
|
2017-01-04 06:36:13 -06:00
|
|
|
bool getTokenUInt64(const std::string& token, const std::string& name, uint64_t& value)
|
|
|
|
{
|
2017-01-30 19:43:27 -06:00
|
|
|
if (token.size() > (name.size() + 1) &&
|
|
|
|
token.compare(0, name.size(), name) == 0 &&
|
|
|
|
token[name.size()] == '=')
|
2017-01-04 06:36:13 -06:00
|
|
|
{
|
2017-01-30 19:43:27 -06:00
|
|
|
const char* str = token.data() + name.size() + 1;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
value = strtoull(str, &endptr, 10);
|
|
|
|
return (endptr > str);
|
2017-01-04 06:36:13 -06:00
|
|
|
}
|
|
|
|
|
2017-01-30 19:43:27 -06:00
|
|
|
return false;
|
2017-01-04 06:36:13 -06:00
|
|
|
}
|
|
|
|
|
2017-06-14 11:28:07 -05:00
|
|
|
bool getTokenUInt32(const std::string& token, const std::string& name, uint32_t& value)
|
|
|
|
{
|
|
|
|
if (token.size() > (name.size() + 1) &&
|
|
|
|
token.compare(0, name.size(), name) == 0 &&
|
|
|
|
token[name.size()] == '=')
|
|
|
|
{
|
|
|
|
const char* str = token.data() + name.size() + 1;
|
|
|
|
char* endptr = nullptr;
|
|
|
|
value = strtoul(str, &endptr, 10);
|
|
|
|
return (endptr > str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
bool getTokenString(const std::string& token, const std::string& name, std::string& value)
|
|
|
|
{
|
2018-02-07 12:30:45 -06:00
|
|
|
if (token.size() >= (name.size() + 1) &&
|
2017-01-30 19:43:27 -06:00
|
|
|
token.compare(0, name.size(), name) == 0 &&
|
|
|
|
token[name.size()] == '=')
|
2015-03-26 09:49:07 -05:00
|
|
|
{
|
2017-01-30 19:43:27 -06:00
|
|
|
value = token.substr(name.size() + 1);
|
|
|
|
return true;
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
2016-05-15 16:44:40 -05:00
|
|
|
|
2017-01-30 19:43:27 -06:00
|
|
|
return false;
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
|
|
|
|
2017-01-30 19:43:27 -06:00
|
|
|
bool getTokenKeyword(const std::string& token, const std::string& name,
|
|
|
|
const std::map<std::string, int>& map, int& value)
|
2015-04-27 13:09:27 -05:00
|
|
|
{
|
2017-01-30 19:43:27 -06:00
|
|
|
std::string t;
|
|
|
|
if (getTokenString(token, name, t))
|
|
|
|
{
|
|
|
|
if (t[0] == '\'' && t[t.size() - 1] == '\'')
|
|
|
|
{
|
|
|
|
t = t.substr(1, t.size() - 2);
|
|
|
|
}
|
2015-04-27 13:09:27 -05:00
|
|
|
|
2017-01-30 19:43:27 -06:00
|
|
|
const auto p = map.find(t);
|
|
|
|
if (p != map.cend())
|
|
|
|
{
|
|
|
|
value = p->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 13:09:27 -05:00
|
|
|
|
2017-01-30 19:43:27 -06:00
|
|
|
return false;
|
2015-04-27 13:09:27 -05:00
|
|
|
}
|
|
|
|
|
2019-11-08 07:50:33 -06:00
|
|
|
bool getTokenInteger(const std::vector<std::string>& tokens, const std::string& name, int& value)
|
2016-04-20 08:44:12 -05:00
|
|
|
{
|
2019-11-08 07:50:33 -06:00
|
|
|
for (size_t i = 0; i < tokens.size(); i++)
|
2016-04-20 08:44:12 -05:00
|
|
|
{
|
|
|
|
if (getTokenInteger(tokens[i], name, value))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-08 07:50:33 -06:00
|
|
|
bool getTokenKeyword(const std::vector<std::string>& tokens, const std::string& name, const std::map<std::string, int>& map, int& value)
|
2016-04-20 08:44:12 -05:00
|
|
|
{
|
2019-11-08 07:50:33 -06:00
|
|
|
for (size_t i = 0; i < tokens.size(); i++)
|
2016-04-20 08:44:12 -05:00
|
|
|
{
|
|
|
|
if (getTokenKeyword(tokens[i], name, map, value))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-26 11:31:19 -05:00
|
|
|
bool getTokenStringFromMessage(const std::string& message, const std::string& name, std::string& value)
|
|
|
|
{
|
2016-11-20 15:16:13 -06:00
|
|
|
if (message.size() > name.size() + 1)
|
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
size_t pos = message.find(name);
|
2016-11-20 15:16:13 -06:00
|
|
|
while (pos != std::string::npos)
|
|
|
|
{
|
2017-06-20 15:49:02 -05:00
|
|
|
bool spaceBefore = pos == 0 || message[pos-1] == ' ';
|
2018-02-07 03:17:19 -06:00
|
|
|
const size_t beg = pos + name.size();
|
2017-06-20 15:49:02 -05:00
|
|
|
if (spaceBefore && message[beg] == '=')
|
2016-11-20 15:16:13 -06:00
|
|
|
{
|
2018-02-07 03:17:19 -06:00
|
|
|
const size_t end = message.find_first_of(" \n", beg);
|
2016-11-20 15:16:13 -06:00
|
|
|
value = message.substr(beg + 1, end - beg - 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = message.find(name, pos + name.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-09-26 11:31:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool getTokenKeywordFromMessage(const std::string& message, const std::string& name, const std::map<std::string, int>& map, int& value)
|
|
|
|
{
|
2019-11-08 07:50:33 -06:00
|
|
|
return getTokenKeyword(tokenize(message), name, map, value);
|
2016-09-26 11:31:19 -05:00
|
|
|
}
|
2015-03-26 09:49:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|