Add support for encoding collections

This commit is contained in:
Anton Thomasson 2020-06-13 20:12:29 +02:00
parent ac2b76a0f0
commit c58ed1e67d
2 changed files with 29 additions and 3 deletions

View file

@ -187,7 +187,7 @@ QJsonValue IppMsg::collect_attributes(QJsonArray& attrs)
tmpobj = attrs.takeAt(0).toObject(); tmpobj = attrs.takeAt(0).toObject();
if(tmpobj["tag"] == BeginCollection) if(tmpobj["tag"] == BeginCollection)
{ {
resObj[key] = collect_attributes(attrs); resObj[key] = QJsonObject {{"tag", BeginCollection}, {"value", collect_attributes(attrs)}};
} }
else else
{ // This should be general data attributes { // This should be general data attributes
@ -333,11 +333,17 @@ QByteArray IppMsg::encode(Operation op)
return QByteArray((char*)(ipp.raw()), ipp.size()); return QByteArray((char*)(ipp.raw()), ipp.size());
} }
void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValueRef value) void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValue value, bool inCollection)
{ {
if(inCollection)
{
msg << (quint8)MemberName << (quint16)0 << (quint16)name.length() << name.toStdString();
name = "";
}
msg << tag << quint16(name.length()) << name.toStdString(); msg << tag << quint16(name.length()) << name.toStdString();
switch (tag) { switch (tag) {
case OpAttrs: case OpAttrs:
case JobAttrs: case JobAttrs:
@ -377,6 +383,26 @@ void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValueRe
msg << (quint16)8 << low << high; msg << (quint16)8 << low << high;
break; break;
} }
case BeginCollection:
{
msg << (quint16)0; // length of value
if(value.isObject())
{
QJsonObject collection = value.toObject();
foreach(QString key, collection.keys())
{
encode_attr(msg, collection[key].toObject()["tag"].toInt(), key,
collection[key].toObject()["value"], true);
}
}
else
{
// TODO add support for 1-setOf in collections
Q_ASSERT("FIXME-array");
}
msg << (quint8)EndCollection << (quint16)0 << (quint16)0;
break;
}
case OctetStringUnknown: case OctetStringUnknown:
case TextWithLanguage: case TextWithLanguage:
case NameWithLanguage: case NameWithLanguage:

View file

@ -85,7 +85,7 @@ private:
QJsonArray get_unnamed_attributes(Bytestream& data); QJsonArray get_unnamed_attributes(Bytestream& data);
QJsonValue collect_attributes(QJsonArray& attrs); QJsonValue collect_attributes(QJsonArray& attrs);
QString consume_attribute(QJsonObject& attrs, Bytestream& data); QString consume_attribute(QJsonObject& attrs, Bytestream& data);
void encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValueRef value); void encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValue value, bool subCollection=false);
quint8 _majVsn; quint8 _majVsn;
quint8 _minVsn; quint8 _minVsn;