Skip to content

XML: Move version string into attribute #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
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
18 changes: 13 additions & 5 deletions src/base/xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ std::wstring XmlReadStr(const XmlNode& node, const std::wstring_view name) {
return node.child_value(name.data());
}

std::wstring XmlReadAttr(const XmlNode& node, const std::wstring_view name) {
return node.attribute(name.data()).value();
}

////////////////////////////////////////////////////////////////////////////////

void XmlWriteInt(XmlNode& node, const std::wstring_view name,
Expand All @@ -77,6 +81,11 @@ void XmlWriteStr(XmlNode& node, const std::wstring_view name,
.append_child(node_type).set_value(value.data());
}

void XmlWriteAttr(XmlNode& node, const std::wstring_view name,
const std::wstring_view value) {
XmlAttr(node, name).set_value(value.data());
}

////////////////////////////////////////////////////////////////////////////////

void XmlWriteChildNodes(XmlNode& parent_node,
Expand Down Expand Up @@ -105,11 +114,10 @@ bool XmlSaveDocumentToFile(const XmlDocument& document,
return document.save_file(path.data(), indent.data(), flags, encoding);
}

std::wstring XmlReadMetaVersion(const XmlDocument& document) {
return XmlReadStr(document.child(L"meta"), L"version");
std::wstring XmlReadVersionAttr(const XmlNode& node) {
return XmlReadAttr(node, L"version");
}

void XmlWriteMetaVersion(XmlDocument& document,
const std::wstring_view version) {
XmlWriteStr(XmlChild(document, L"meta"), L"version", version);
void XmlWriteVersionAttr(XmlNode& node, const std::wstring_view version) {
XmlWriteAttr(node, L"version", version);
}
4 changes: 2 additions & 2 deletions src/base/xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ bool XmlSaveDocumentToFile(
const unsigned int flags = pugi::format_default,
const pugi::xml_encoding encoding = pugi::xml_encoding::encoding_utf8);

std::wstring XmlReadMetaVersion(const XmlDocument& document);
void XmlWriteMetaVersion(XmlDocument& document, const std::wstring_view version);
std::wstring XmlReadVersionAttr(const XmlNode& node);
void XmlWriteVersionAttr(XmlNode& parent_node, const std::wstring_view version);
10 changes: 5 additions & 5 deletions src/media/anime_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ bool Database::LoadDatabase() {
if (!parse_result)
return false;

const auto meta_version = XmlReadMetaVersion(document);

auto database_node = document.child(L"database");
ReadDatabaseNode(database_node);

HandleCompatibility(meta_version);
const auto xml_version = XmlReadVersionAttr(database_node);
HandleCompatibility(xml_version);

return true;
}
Expand Down Expand Up @@ -117,8 +116,9 @@ void Database::ReadDatabaseNode(XmlNode& database_node) {
bool Database::SaveDatabase() const {
XmlDocument document;

XmlWriteMetaVersion(document, StrToWstr(taiga::version().to_string()));
WriteDatabaseNode(XmlChild(document, L"database"));
auto database_node = XmlChild(document, L"database");
XmlWriteVersionAttr(database_node, StrToWstr(taiga::version().to_string()));
WriteDatabaseNode(database_node);

const auto path = taiga::GetPath(taiga::Path::DatabaseAnime);
return XmlSaveDocumentToFile(document, path);
Expand Down
19 changes: 10 additions & 9 deletions src/media/library/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ bool History::Load() {
if (!parse_result)
return false;

// Meta
const auto meta_version = XmlReadMetaVersion(document);
const semaver::Version version(WstrToStr(meta_version));
const auto history_node = document.child(L"history");

// Version handling
const auto xml_version = XmlReadVersionAttr(history_node);
const semaver::Version version(WstrToStr(xml_version));

// Items
auto node_items = document.child(L"history").child(L"items");
auto node_items = history_node.child(L"items");
for (auto item : node_items.children(L"item")) {
HistoryItem history_item;
history_item.anime_id = item.attribute(L"anime_id").as_int(anime::ID_NOTINLIST);
Expand All @@ -73,7 +75,7 @@ bool History::Load() {
}
// Queue events
ReadQueue(document);
HandleCompatibility(meta_version);
HandleCompatibility(xml_version);

return true;
}
Expand Down Expand Up @@ -123,12 +125,11 @@ void History::ReadQueue(const XmlDocument& document) {

bool History::Save() {
XmlDocument document;

// Write meta
XmlWriteMetaVersion(document, StrToWstr(taiga::version().to_string()));

auto node_history = document.append_child(L"history");

// Write version attribute
XmlWriteVersionAttr(node_history, StrToWstr(taiga::version().to_string()));

// Write items
auto node_items = node_history.append_child(L"items");
for (const auto& history_item : items) {
Expand Down
9 changes: 4 additions & 5 deletions src/media/library/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ bool Database::LoadList() {
return false;
}

const auto meta_version = XmlReadMetaVersion(document);

auto node_database = document.child(L"database");
ReadDatabaseNode(node_database);

Expand All @@ -74,7 +72,8 @@ bool Database::LoadList() {
anime_item.SetMyLastUpdated(XmlReadStr(node, L"last_updated"));
}

HandleListCompatibility(meta_version);
const auto xml_version = XmlReadVersionAttr(node_library);
HandleListCompatibility(xml_version);

return true;
}
Expand All @@ -85,14 +84,14 @@ bool Database::SaveList(bool include_database) const {

XmlDocument document;

XmlWriteMetaVersion(document, StrToWstr(taiga::version().to_string()));

if (include_database) {
WriteDatabaseNode(XmlChild(document, L"database"));
}

auto node_library = document.append_child(L"library");

XmlWriteVersionAttr(node_library, StrToWstr(taiga::version().to_string()));

for (const auto& [id, item] : items) {
if (item.IsInList()) {
auto node = node_library.append_child(L"anime");
Expand Down