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/attribute.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/attribute.cpp (limited to 'src/attribute.cpp') diff --git a/src/attribute.cpp b/src/attribute.cpp new file mode 100644 index 0000000..4458713 --- /dev/null +++ b/src/attribute.cpp @@ -0,0 +1,119 @@ +#include "attribute.hpp" + +#include "utils.hpp" +#include +#include + + +XiftAttributes::XiftAttributes(): stack(0) +{} + +XiftAttributes::~XiftAttributes() +{ + while (stack) { + Stack *old = stack; + stack = stack->next; + delete old; + } +} + + +XiftAttributes::Attr &XiftAttributes::Attribute(const char *name) +{ + Stack *current = stack; + while (current) { + if (!strcmp(current->item.name, name)) { + return current->item; + } + current = current->next; + } + Attr &res = New(); + res.nlen = strlen(name); + res.name = xift_str_create_copy(name, name + res.nlen); + res.nsize = res.nlen + 1; + return res; +} + +void XiftAttributes::Remove(const char *name) +{ + Stack **current = &stack; + while (*current) { + if (!strcmp((*current)->item.name, name)) { + Stack *old = *current; + *current = (*current)->next; + delete old; + return; + } + current = &(*current)->next; + } +} + + +XiftAttributes::Attr::Attr(): + name(0), nlen(0), nsize(0), value(0), vlen(0), vsize(0) +{} + +XiftAttributes::Attr::~Attr() +{ + if (name) free(name); + if (value) free(value); +} + + +bool XiftAttributes::Attr::MatchesForm(const XiftAttributes::Attr &form) const +{ + return !strcmp(name, form.name); +} + + +XiftAttributes::Attr *XiftAttributes::Top() +{ + if (stack) { + return &stack->item; + } else { + return 0; + } +} + +void XiftAttributes::Pop() +{ + if (stack) { + Stack *old = stack; + stack = stack->next; + delete old; + } +} + +XiftAttributes::Attr &XiftAttributes::New() +{ + Stack *old = stack; + stack = new Stack; + stack->next = old; + return stack->item; +} + +bool XiftAttributes::ContainsMatchedForm( + const XiftAttributes::Attr &attribute +) const +{ + Stack *current = stack; + while (current) { + if (attribute.MatchesForm(current->item)) { + return true; + } + current = current->next; + } + return false; +} + +bool XiftAttributes::MatchesForm(const XiftAttributes &form) const +{ + Stack *current = stack; + while (current) { + if (!form.ContainsMatchedForm(current->item)) { + return false; + } + current = current->next; + } + return true; +} -- cgit v1.2.3