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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_LOOLPROTOCOL_HPP
|
|
|
|
#define INCLUDED_LOOLPROTOCOL_HPP
|
|
|
|
|
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
|
|
|
#include <cstring>
|
2015-04-27 13:09:27 -05:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2016-04-20 08:44:12 -05:00
|
|
|
#include <Poco/StringTokenizer.h>
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
|
|
|
|
|
|
|
|
namespace LOOLProtocol
|
|
|
|
{
|
2016-01-06 11:00:44 -06:00
|
|
|
// Protocol Version Number.
|
|
|
|
// See protocol.txt.
|
|
|
|
constexpr unsigned ProtocolMajorVersionNumber = 0;
|
|
|
|
constexpr unsigned ProtocolMinorVersionNumber = 1;
|
|
|
|
|
|
|
|
inline
|
|
|
|
std::string GetProtocolVersion()
|
|
|
|
{
|
|
|
|
return std::to_string(ProtocolMajorVersionNumber) + '.'
|
|
|
|
+ std::to_string(ProtocolMinorVersionNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse a string into a version tuple.
|
|
|
|
// Negative numbers for error.
|
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-01-12 05:07:13 -06:00
|
|
|
bool stringToInteger(const std::string& input, int& value);
|
2016-05-15 16:44:40 -05:00
|
|
|
bool parseNameIntegerPair(const std::string& token, std::string& name, int& value);
|
2016-05-22 10:26:30 -05:00
|
|
|
bool parseNameValuePair(const std::string& token, std::string& name, std::string& value);
|
2016-03-27 13:30:03 -05:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
bool getTokenInteger(const std::string& token, const std::string& name, int& value);
|
|
|
|
bool getTokenString(const std::string& token, const std::string& name, std::string& value);
|
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);
|
2015-03-26 09:49:07 -05:00
|
|
|
|
2016-04-20 08:44:12 -05:00
|
|
|
bool getTokenInteger(const Poco::StringTokenizer& tokens, const std::string& name, int& value);
|
|
|
|
bool getTokenString(const Poco::StringTokenizer& tokens, const std::string& name, std::string& value);
|
|
|
|
bool getTokenKeyword(const Poco::StringTokenizer& tokens, const std::string& name, const std::map<std::string, int>& map, int& value);
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
// Functions that parse messages. All return false if parsing fails
|
|
|
|
bool parseStatus(const std::string& message, LibreOfficeKitDocumentType& type, int& nParts, int& currentPart, int& width, int& height);
|
2015-04-14 09:50:38 -05:00
|
|
|
|
2016-03-27 13:30:03 -05:00
|
|
|
inline
|
2016-04-11 02:11:49 -05:00
|
|
|
std::string getDelimitedInitialSubstring(const char *message, const int length, const char delimiter)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
|
|
|
if (message == nullptr || length <= 0)
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:11:49 -05:00
|
|
|
const char *foundDelimiter = static_cast<const char *>(std::memchr(message, delimiter, length));
|
|
|
|
const auto size = (foundDelimiter == nullptr ? length : foundDelimiter - message);
|
2016-03-27 13:30:03 -05:00
|
|
|
return std::string(message, size);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:11:49 -05:00
|
|
|
/// Returns the first token of a message.
|
|
|
|
inline
|
|
|
|
std::string getFirstToken(const char *message, const int length)
|
|
|
|
{
|
|
|
|
return getDelimitedInitialSubstring(message, length, ' ');
|
|
|
|
}
|
|
|
|
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string getFirstToken(const T& message)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
|
|
|
return getFirstToken(message.data(), message.size());
|
|
|
|
}
|
|
|
|
|
2016-04-09 22:16:09 -05:00
|
|
|
/// Returns true if the token is a user-interaction token.
|
|
|
|
/// Currently this excludes commands sent automatically.
|
|
|
|
/// Notice that this doesn't guarantee editing activity,
|
|
|
|
/// rather just user interaction with the UI.
|
2016-04-06 03:36:33 -05:00
|
|
|
inline
|
|
|
|
bool tokenIndicatesUserInteraction(const std::string& token)
|
|
|
|
{
|
2016-04-09 22:16:09 -05:00
|
|
|
// Exclude tokens that include these keywords,
|
|
|
|
// such as canceltiles statusindicator.
|
|
|
|
return (token.find("tile") == std::string::npos &&
|
|
|
|
token.find("status") == std::string::npos &&
|
|
|
|
token.find("state") == std::string::npos);
|
2016-04-06 03:36:33 -05:00
|
|
|
}
|
|
|
|
|
2016-03-27 13:30:03 -05:00
|
|
|
/// Returns the first line of a message.
|
|
|
|
inline
|
|
|
|
std::string getFirstLine(const char *message, const int length)
|
|
|
|
{
|
2016-04-11 02:11:49 -05:00
|
|
|
return getDelimitedInitialSubstring(message, length, '\n');
|
2016-03-27 13:30:03 -05:00
|
|
|
}
|
|
|
|
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string getFirstLine(const T& message)
|
2016-03-27 13:30:03 -05:00
|
|
|
{
|
|
|
|
return getFirstLine(message.data(), message.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns an abbereviation of the message (the first line, indicating truncation).
|
|
|
|
std::string getAbbreviatedMessage(const char *message, const int length);
|
2016-04-10 12:13:05 -05:00
|
|
|
|
|
|
|
inline
|
|
|
|
std::string getAbbreviatedMessage(const std::string& message)
|
|
|
|
{
|
|
|
|
return getAbbreviatedMessage(message.data(), message.size());
|
|
|
|
}
|
2016-05-01 19:31:40 -05:00
|
|
|
|
2016-05-08 23:35:45 -05:00
|
|
|
template <typename T>
|
|
|
|
std::string getAbbreviatedMessage(const T& message)
|
2016-05-01 19:31:40 -05:00
|
|
|
{
|
|
|
|
return getAbbreviatedMessage(message.data(), message.size());
|
|
|
|
}
|
2015-03-26 09:49:07 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|