Skip to content
This repository was archived by the owner on Jan 2, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Libraries/libcar/Headers/car/AttributeList.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class AttributeList {
size_t count,
uint32_t const *identifiers) const;

/*
* Write an attribute list into an a vector of bytes using the identifier
* order in keyfmt->identifiers.
*/
std::vector<uint8_t> write(
car_key_format const *keyfmt) const;

public:
/*
* Print debugging information about the list.
Expand All @@ -90,6 +97,14 @@ class AttributeList {
uint32_t const *identifiers,
uint16_t const *values);

/*
* Load an attribute list from packed struct car_key_format members and
* a car_rendition_key buffer.
*/
static AttributeList Load(
car_key_format const *keyfmt,
car_rendition_key const *values);

/*
* Load an attribute list from a buffer of identifier value pairs.
*/
Expand Down
27 changes: 27 additions & 0 deletions Libraries/libcar/Sources/AttributeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ Load(size_t count, uint32_t const *identifiers, uint16_t const *values)
return AttributeList(attributes);
}

AttributeList AttributeList::
Load(car_key_format const *keyfmt, car_rendition_key const *values)
{
std::unordered_map<enum car_attribute_identifier, uint16_t> attributes;
for (size_t i = 0; i < keyfmt->num_identifiers; ++i) {
attributes.insert({ (enum car_attribute_identifier)keyfmt->identifier_list[i], values[i] });
}
return AttributeList(attributes);
}

AttributeList AttributeList::
Load(size_t count, struct car_attribute_pair const *pairs)
{
Expand Down Expand Up @@ -95,3 +105,20 @@ write(size_t count, uint32_t const *identifiers) const
return output;
}

std::vector<uint8_t> AttributeList::
write(car_key_format const *keyfmt) const
{
std::vector<uint8_t> output = std::vector<uint8_t>(sizeof(uint16_t) * keyfmt->num_identifiers);
uint16_t *values = reinterpret_cast<uint16_t *>(output.data());
std::unordered_map<enum car_attribute_identifier, uint16_t> attributes;
for (size_t i = 0; i < keyfmt->num_identifiers; ++i) {
enum car_attribute_identifier identifier = (enum car_attribute_identifier) keyfmt->identifier_list[i];
auto result = _values.find(identifier);
if (result != _values.end()) {
values[i] = result->second;
} else {
values[i] = 0;
}
}
return output;
}
4 changes: 2 additions & 2 deletions Libraries/libcar/Sources/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ renditionIterate(std::function<void(Rendition const &)> const &iterator) const
KeyValuePair kv = (KeyValuePair)it.second;
car_rendition_key *rendition_key = (car_rendition_key *)kv.key;
struct car_rendition_value *rendition_value = (struct car_rendition_value *)kv.value;
AttributeList attributes = AttributeList::Load(keyfmt->num_identifiers, keyfmt->identifier_list, rendition_key);
AttributeList attributes = AttributeList::Load(keyfmt, rendition_key);
Rendition rendition = Rendition::Load(attributes, rendition_value);
iterator(rendition);
}
Expand Down Expand Up @@ -264,7 +264,7 @@ lookupRenditions(Facet const &facet) const
KeyValuePair value = (KeyValuePair)it->second;
car_rendition_key *rendition_key = (car_rendition_key *)value.key;
struct car_rendition_value *rendition_value = (struct car_rendition_value *)value.value;
AttributeList attributes = AttributeList::Load(keyfmt->num_identifiers, keyfmt->identifier_list, rendition_key);
AttributeList attributes = AttributeList::Load(keyfmt, rendition_key);
Rendition rendition = Rendition::Load(attributes, rendition_value);
result.push_back(rendition);
}
Expand Down
2 changes: 1 addition & 1 deletion Libraries/libcar/Sources/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ write() const
bom_tree_reserve(renditions_tree_context, rendition_count);
if (renditions_tree_context != NULL) {
for (auto const &item : _renditions) {
auto attributes_value = item.second.attributes().write(keyfmt->num_identifiers, keyfmt->identifier_list);
auto attributes_value = item.second.attributes().write(keyfmt);
auto rendition_value = item.second.write();
bom_tree_add(
renditions_tree_context,
Expand Down