Skip to content

Commit

Permalink
libcore|Info: Converting an Info block to a Record
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Nov 5, 2018
1 parent 3f90377 commit 093c0e6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
9 changes: 8 additions & 1 deletion doomsday/sdk/libcore/include/de/data/info.h
Expand Up @@ -20,9 +20,10 @@
#ifndef LIBDENG2_INFO_H
#define LIBDENG2_INFO_H

#include "../String"
#include "../NativePath"
#include "../Record"
#include "../SourceLineTable"
#include "../String"

#include <QStringList>
#include <QHash>
Expand Down Expand Up @@ -246,6 +247,12 @@ class DENG2_PUBLIC Info
*/
void moveContents(BlockElement &destination);

/**
* Converts the contents of the block into a Record.
* @return Record with block elements.
*/
Record asRecord() const;

private:
Info &_info;
String _blockType;
Expand Down
31 changes: 31 additions & 0 deletions doomsday/sdk/libcore/src/data/info.cpp
Expand Up @@ -22,8 +22,10 @@
#include "de/Folder"
#include "de/Log"
#include "de/LogBuffer"
#include "de/RecordValue"
#include "de/ScriptLex"
#include "de/SourceLineTable"
#include <de/TextValue>

#include <QFile>

Expand Down Expand Up @@ -806,6 +808,35 @@ void Info::BlockElement::moveContents(BlockElement &destination)
_contents.clear();
}

Record Info::BlockElement::asRecord() const
{
Record rec;
for (auto i = _contents.begin(); i != _contents.end(); ++i)
{
std::unique_ptr<Variable> var(new Variable(i.key()));
const Element *elem = i.value();
if (elem->isBlock())
{
var->set(RecordValue::takeRecord(elem->as<BlockElement>().asRecord()));
}
else if (elem->isList())
{
std::unique_ptr<ArrayValue> array(new ArrayValue);
for (const auto &value : elem->values())
{
array->add(new TextValue(value.text));
}
var->set(array.release());
}
else
{
var->set(new TextValue(elem->as<KeyElement>().value().text));
}
rec.add(var.release());
}
return rec;
}

//---------------------------------------------------------------------------------------

Info::Info() : d(new Impl(this))
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/data/record.cpp
Expand Up @@ -1155,7 +1155,7 @@ String Record::asInfo() const
os.setCodec("UTF-8");
for (auto i = d->members.constBegin(); i != d->members.constEnd(); ++i)
{
Variable const &var = *i.value();
const Variable &var = *i.value();
String src = i.key();

if (is<RecordValue>(var.value()))
Expand Down

0 comments on commit 093c0e6

Please sign in to comment.