From 0be032c6998e712dc2c9f2ed97c3491d89eb05af Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Fri, 31 Jan 2020 17:16:27 +0300 Subject: [xift] Almost done. --- src/tag.cpp | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/tag.cpp (limited to 'src/tag.cpp') diff --git a/src/tag.cpp b/src/tag.cpp new file mode 100644 index 0000000..2f4bbcb --- /dev/null +++ b/src/tag.cpp @@ -0,0 +1,120 @@ +#include "tag.hpp" + +#include "utils.hpp" +#include +#include + + +XiftTags::Item::Item(): name(0), len(0), size(0) +{} + +XiftTags::Item::~Item() +{ + if (name) free(name); +} + + +bool XiftTags::Item::MatchesForm(const XiftTags::Item &form) const +{ + return !strcmp(name, form.name) && XiftAttributes::MatchesForm(form); +} + + +XiftTags::XiftTags(): stack(0) +{} + +XiftTags::~XiftTags() +{ + while (stack) { + Stack *old = stack; + stack = stack->next; + delete old->item; + delete old; + } +} + + +XiftTags::Item &XiftTags::Tag(const char *name) +{ + Stack *current = stack; + while (current) { + if (!strcmp(current->item->name, name)) { + return *current->item; + } + current = current->next; + } + Item &res = New(); + res.len = strlen(name); + res.name = xift_str_create_copy(name, name + res.len); + res.size = res.len + 1; + return res; +} + +void XiftTags::Remove(const char *name) +{ + Stack **current = &stack; + while (*current) { + if (!strcmp((*current)->item->name, name)) { + Stack *old = *current; + *current = (*current)->next; + delete old->item; + delete old; + return; + } + current = &(*current)->next; + } +} + + +XiftTags::Item *XiftTags::Top() +{ + if (stack) { + return stack->item; + } else { + return 0; + } +} + +void XiftTags::Pop() +{ + if (stack) { + Stack *old = stack; + stack = stack->next; + delete old->item; + delete old; + } +} + +XiftTags::Item *XiftTags::PopToBeDeleted() +{ + XiftTags::Item *res = 0; + if (stack) { + Stack *old = stack; + res = stack->item; + stack = stack->next; + delete old; + } + return res; +} + +XiftTags::Item &XiftTags::New() +{ + Stack *old = stack; + stack = new Stack; + stack->item = new XiftTags::Item; + stack->next = old; + return *stack->item; +} + + +bool XiftTags::ContainsMatchedForm(const XiftTags::Item & tag) const +{ + Stack *current = stack; + while (current) { + if (tag.MatchesForm(*current->item)) { + return true; + } + current = current->next; + } + return false; +} -- cgit v1.2.3