Rework list/1-setOf handling

This commit is contained in:
Anton Thomasson 2020-06-12 18:56:06 +02:00
parent ca2b9086d8
commit 761f0a3869
2 changed files with 50 additions and 27 deletions

View file

@ -34,8 +34,6 @@ IppMsg::IppMsg(QNetworkReply* resp)
QJsonObject attrs; QJsonObject attrs;
IppMsg::IppTag currentAttrType = IppTag::EndAttrs; IppMsg::IppTag currentAttrType = IppTag::EndAttrs;
QString last_name;
while(!bts.atEnd()) while(!bts.atEnd())
{ {
if(bts.peekU8() <= IppTag::UnsupportedAttrs) { if(bts.peekU8() <= IppTag::UnsupportedAttrs) {
@ -62,24 +60,15 @@ IppMsg::IppMsg(QNetworkReply* resp)
} }
else { else {
last_name = consume_attribute(attrs, bts, last_name); consume_attribute(attrs, bts);
} }
} }
} }
QString IppMsg::consume_attribute(QJsonObject& attrs, Bytestream& data, QString lastName) QJsonValue IppMsg::consume_value(quint8 tag, Bytestream& data)
{ {
quint8 tag;
quint16 tmp_len;
QString name;
QJsonValue value; QJsonValue value;
std::string tmp_str = ""; quint16 tmp_len;
bool noList = false;
data >> tag >> tmp_len;
data/tmp_len >> tmp_str;
name = tmp_str!="" ? tmp_str.c_str() : lastName;
switch (tag) { switch (tag) {
case OpAttrs: case OpAttrs:
@ -98,7 +87,6 @@ QString IppMsg::consume_attribute(QJsonObject& attrs, Bytestream& data, QString
quint8 tmp_bool; quint8 tmp_bool;
data >> tmp_len >> tmp_bool; data >> tmp_len >> tmp_bool;
value = (bool)tmp_bool; value = (bool)tmp_bool;
noList = true;
break; break;
case DateTime: case DateTime:
{ {
@ -133,7 +121,6 @@ QString IppMsg::consume_attribute(QJsonObject& attrs, Bytestream& data, QString
tmp_range.insert("low", low); tmp_range.insert("low", low);
tmp_range.insert("high", high); tmp_range.insert("high", high);
value = tmp_range; value = tmp_range;
noList = true;
break; break;
} }
case OctetStringUnknown: case OctetStringUnknown:
@ -148,31 +135,65 @@ QString IppMsg::consume_attribute(QJsonObject& attrs, Bytestream& data, QString
case NaturalLanguage: case NaturalLanguage:
case MimeMediaType: case MimeMediaType:
default: default:
{
std::string tmp_str = "";
data >> tmp_len; data >> tmp_len;
data/tmp_len >> tmp_str; data/tmp_len >> tmp_str;
value = tmp_str.c_str(); value = tmp_str.c_str();
break; break;
}
}; };
return value;
}
QJsonArray IppMsg::get_unnamed_attributes(Bytestream& data)
if(attrs.contains(name)) {
quint8 tag;
QJsonArray attrs;
while(data.remaining())
{ {
QJsonObject tmp = attrs[name].toObject(); data >> tag;
QJsonArray tmpa; if(data >>= (quint16)0)
if(tmp["value"].isArray())
{ {
tmpa = tmp["value"].toArray(); attrs.append(consume_value(tag, data));
} }
else else
{ {
tmpa = QJsonArray {tmp["value"]}; data -= 1;
break;
} }
tmpa.append(value); }
tmp["value"] = tmpa; return attrs;
attrs.insert(name, tmp); }
QString IppMsg::consume_attribute(QJsonObject& attrs, Bytestream& data)
{
quint8 tag;
quint16 tmp_len;
QString name;
QJsonValue value;
std::string tmp_str = "";
data >> tag >> tmp_len;
data/tmp_len >> tmp_str;
QString name0 = tmp_str.c_str();
name = tmp_str.c_str();
value = consume_value(tag, data);
QJsonArray unnamed = get_unnamed_attributes(data);
qDebug() << name0 << tag << tmp_len << value << unnamed;
if(!unnamed.empty())
{
unnamed.prepend(value);
attrs.insert(name, QJsonObject {{"tag", tag}, {"value", unnamed}});
} }
else else
{ {
bool noList = value.isObject() || value.isBool();
if((name.endsWith("-supported") || name == "printer-icons") && !noList) if((name.endsWith("-supported") || name == "printer-icons") && !noList)
{ {
value = QJsonArray {value}; value = QJsonArray {value};

View file

@ -78,7 +78,9 @@ public:
protected: protected:
private: private:
QString consume_attribute(QJsonObject& attrs, Bytestream& data, QString lastName); QJsonValue consume_value(quint8 tag, Bytestream& data);
QJsonArray get_unnamed_attributes(Bytestream& data);
QString consume_attribute(QJsonObject& attrs, Bytestream& data);
Bytestream encode_attr(quint8 tag, QString name, QJsonValueRef value); Bytestream encode_attr(quint8 tag, QString name, QJsonValueRef value);
QJsonObject _opAttrs; QJsonObject _opAttrs;