kate-deft/src/plumb/plumbmsg.h

83 lines
2.7 KiB
C++

/*
* SPDX-License-Identifier: LGPL-2.0-or-later
*
* PlumbMsg — a plan9port plumbing message and its wire codec.
*
* A plumb message is a context-carrying request routed by the plumber daemon
* through named ports. The wire format (plan9port libplumb/mesg.c) is six
* newline-terminated header lines followed by the raw data bytes:
*
* src\n source application ("kate")
* dst\n destination port ("" lets the rules decide)
* wdir\n working directory (resolves relative paths)
* type\n data type ("text")
* attr\n space-separated name=value attributes
* ndata\n decimal byte count of the data that follows
* <ndata bytes> the data (NOT newline-terminated)
*
* Attribute values containing space, tab, '=' or ' are single-quoted, with a
* literal ' escaped by doubling it ('') — exactly as libplumb quotes them.
*
* This header is pure (QString/QByteArray only) so pack/unpack can be unit
* tested against the bytes captured from a live plumber on the edit port.
*/
#ifndef KATECUSTOM_PLUMBMSG_H
#define KATECUSTOM_PLUMBMSG_H
#include <QByteArray>
#include <QList>
#include <QString>
namespace katecustom
{
/*! One plumb attribute (name=value). */
struct PlumbAttr {
QString name;
QString value;
bool operator==(const PlumbAttr &o) const
{
return name == o.name && value == o.value;
}
};
/*! A decoded plumb message. */
struct PlumbMsg {
QString src;
QString dst;
QString wdir;
QString type;
QList<PlumbAttr> attr;
QByteArray data;
/*! Value of attribute \a name, or empty QString if absent. */
QString lookup(const QString &name) const;
/*! Set (or replace) attribute \a name to \a value. */
void setAttr(const QString &name, const QString &value);
/*!
* Encode to the libplumb wire format. The attribute list is serialised with
* libplumb-compatible quoting. ndata is the byte length of \c data.
*/
QByteArray pack() const;
/*!
* Decode \a buf (a complete message) into \a out. Returns true on success.
* Mirrors libplumb plumbunpackpartial: parses six header lines, then takes
* exactly the trailing bytes as data (libplumb clamps ndata to what was
* actually received, so a short ndata header is tolerated the same way).
*/
static bool unpack(const QByteArray &buf, PlumbMsg *out);
};
/*! Serialise an attribute list with libplumb quoting rules (exposed for tests). */
QByteArray packPlumbAttr(const QList<PlumbAttr> &attr);
/*! Parse a packed attribute line back into a list (exposed for tests). */
QList<PlumbAttr> unpackPlumbAttr(const QByteArray &line);
} // namespace katecustom
#endif