Skip to content

Commit

Permalink
fdt-v2: moved root node renaming to separate function
Browse files Browse the repository at this point in the history
Signed-off-by: Bartłomiej Burdukiewicz <[email protected]>
  • Loading branch information
dev-0x7C6 committed Jul 2, 2024
1 parent 69305d3 commit 3f5bdf6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
16 changes: 10 additions & 6 deletions src/fdt/fdt-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ auto foreach_token_type(std::variant<Ts...>, const u32 token_id, fdt::parser::co
return (conditional_parse(Ts{}) || ...);
}

auto fdt::parser::parse(std::string_view view, std::string_view root_name) -> std::expected<fdt::parser::tokens, fdt::parser::error> {
auto fdt::parser::parse(std::string_view view) -> std::expected<fdt::parser::tokens, fdt::parser::error> {
using error = fdt::parser::error;

if (view.size() < sizeof(decode::header))
Expand Down Expand Up @@ -117,21 +117,25 @@ auto fdt::parser::parse(std::string_view view, std::string_view root_name) -> st
if (std::holds_alternative<token_types::property>(tokens.back())) {
auto &prop = std::get<token_types::property>(tokens.back());

if (auto dtb = fdt::parser::parse(prop.data, prop.name); dtb.has_value()) {
if (auto dtb = fdt::parser::parse(prop.data); dtb.has_value()) {
auto &embedded_tokens = dtb.value();
rename_root(embedded_tokens, prop.name);
std::move(std::begin(embedded_tokens), std::end(embedded_tokens), std::back_inserter(tokens));
}
}
}

// change property name for first node
return tokens;
}

auto fdt::parser::rename_root(fdt::parser::tokens &tokens, std::string_view name) -> bool {
for (auto &&token : tokens)
if (std::holds_alternative<token_types::node_begin>(token)) {
std::get<token_types::node_begin>(token).name = root_name;
break;
std::get<token_types::node_begin>(token).name = name;
return true;
}

return tokens;
return false;
}

template <class... Ts>
Expand Down
4 changes: 3 additions & 1 deletion src/fdt/fdt-parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ enum class error {
unsupported_version,
};

auto parse(std::string_view view, std::string_view root_name) -> std::expected<tokens, error>;
auto parse(std::string_view data) -> std::expected<tokens, error>;
auto validate(const tokens &) -> bool;
auto rename_root(tokens &, std::string_view name) -> bool;

} // namespace fdt::parser
4 changes: 3 additions & 1 deletion src/fdt/fdt-view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ bool fdt::viewer::load(QByteArray &&data, string &&name, string &&id) {
using namespace fdt::parser;
using namespace fdt::qt_wrappers;

const auto tokens = parse({data.data(), data.size()}, name.toStdString());
auto tokens = parse({data.data(), data.size()});

if (!tokens)
return false;

if (!validate(tokens.value()))
return false;

fdt::parser::rename_root(tokens.value(), name.toStdString());

tree_generator generator(m_tree[id], m_target, std::move(name), std::move(id));

for (auto &&token : tokens.value())
Expand Down

0 comments on commit 3f5bdf6

Please sign in to comment.