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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
|
|
|
#include "LOOLProtocol.hpp"
|
|
|
|
|
|
|
|
using Poco::StringTokenizer;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
StringTokenizer firstTokens(version, ".", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
if (firstTokens.count() > 0)
|
|
|
|
{
|
|
|
|
major = std::stoi(firstTokens[0]);
|
|
|
|
|
|
|
|
StringTokenizer secondTokens(firstTokens[1], "-", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
minor = std::stoi(secondTokens[0]);
|
|
|
|
|
|
|
|
if (secondTokens.count() > 1)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
bool getTokenInteger(const std::string& token, const std::string& name, int& value)
|
|
|
|
{
|
|
|
|
size_t nextIdx;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (token.size() < name.size() + 2 ||
|
|
|
|
token.substr(0, name.size()) != name ||
|
|
|
|
token[name.size()] != '=' ||
|
|
|
|
(value = std::stoi(token.substr(name.size() + 1), &nextIdx), false) ||
|
|
|
|
nextIdx != token.size() - name.size() - 1)
|
|
|
|
{
|
|
|
|
throw std::invalid_argument("bah");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::invalid_argument&)
|
|
|
|
{
|
2015-04-27 13:09:27 -05:00
|
|
|
return false;
|
2015-03-26 09:49:07 -05:00
|
|
|
}
|
2015-04-27 13:09:27 -05:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getTokenString(const std::string& token, const std::string& name, std::string& value)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (token.size() < name.size() + 2 ||
|
|
|
|
token.substr(0, name.size()) != name ||
|
|
|
|
token[name.size()] != '=')
|
|
|
|
{
|
|
|
|
throw std::invalid_argument("bah");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::invalid_argument&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
value = token.substr(name.size() + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-27 13:09:27 -05:00
|
|
|
bool getTokenKeyword(const std::string& token, const std::string& name, const std::map<std::string, int>& map, int& value)
|
|
|
|
{
|
|
|
|
if (token.size() < name.size() + 2 ||
|
|
|
|
token.substr(0, name.size()) != name ||
|
|
|
|
token[name.size()] != '=')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
std::string t = token.substr(name.size()+1);
|
|
|
|
if (t[0] == '\'' && t[t.size()-1] == '\'')
|
|
|
|
t = t.substr(1, t.size()-2);
|
|
|
|
|
|
|
|
auto p = map.find(t);
|
|
|
|
if (p == map.cend())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
value = p->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
bool parseStatus(const std::string& message, LibreOfficeKitDocumentType& type, int& nParts, int& currentPart, int& width, int& height)
|
|
|
|
{
|
|
|
|
StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
assert(tokens.count() == 6);
|
|
|
|
assert(tokens[0] == "status:");
|
|
|
|
|
|
|
|
std::string typeString;
|
|
|
|
if (!getTokenString(tokens[1], "type", typeString))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (typeString == "text")
|
|
|
|
type = LOK_DOCTYPE_TEXT;
|
|
|
|
else if (typeString == "spreadsheet")
|
|
|
|
type = LOK_DOCTYPE_SPREADSHEET;
|
|
|
|
else if (typeString == "presentation")
|
|
|
|
type = LOK_DOCTYPE_PRESENTATION;
|
|
|
|
else if (typeString == "drawing")
|
|
|
|
type = LOK_DOCTYPE_PRESENTATION;
|
|
|
|
else if (typeString == "other")
|
|
|
|
type = LOK_DOCTYPE_OTHER;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!getTokenInteger(tokens[2], "parts", nParts) ||
|
|
|
|
!getTokenInteger(tokens[3], "current", currentPart) ||
|
|
|
|
!getTokenInteger(tokens[4], "width", width) ||
|
|
|
|
!getTokenInteger(tokens[5], "height", height))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-04-14 09:50:38 -05:00
|
|
|
|
|
|
|
std::string getFirstLine(const char *message, int length)
|
|
|
|
{
|
2016-01-23 16:33:19 -06:00
|
|
|
if (message == nullptr || length <= 0)
|
|
|
|
return "";
|
|
|
|
|
2015-04-14 09:50:38 -05:00
|
|
|
const char *endOfLine = static_cast<const char *>(std::memchr(message, '\n', length));
|
|
|
|
if (endOfLine == nullptr)
|
|
|
|
return std::string(message, length);
|
|
|
|
else
|
|
|
|
return std::string(message, endOfLine-message);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string getAbbreviatedMessage(const char *message, int length)
|
|
|
|
{
|
2016-01-23 16:33:19 -06:00
|
|
|
if (message == nullptr || length <= 0)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
const auto firstLine = getFirstLine(message, length);
|
|
|
|
std::string result = "'" + firstLine + "'";
|
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length))
|
2015-04-14 09:50:38 -05:00
|
|
|
result += "...";
|
|
|
|
return result;
|
|
|
|
}
|
2015-03-26 09:49:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|