summaryrefslogtreecommitdiff
path: root/src/attribute.cpp
blob: d6f928acd90407322c96e04926049c0e58d337d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "attribute.hpp"

#include "utils.hpp"
#include <stdlib.h>
#include <string.h>


XiftAttribute::XiftAttribute():
    name(0), nlen(0), nsize(0), value(0), vlen(0), vsize(0)
{}

XiftAttribute::~XiftAttribute()
{
    if (name) free(name);
    if (value) free(value);
}


bool XiftAttribute::MatchesForm(const XiftAttribute &form) const
{
    return !strcmp(name, form.name);
}


XiftAttributes::XiftAttributes(): stack(0)
{}

XiftAttributes::~XiftAttributes()
{
    while (stack) {
        Stack *old = stack;
        stack      = stack->next;
        delete old;
    }
}


XiftAttribute &XiftAttributes::Attribute(const char *name)
{
    Stack *current = stack;
    while (current) {
        if (!strcmp(current->item.name, name)) {
            return current->item;
        }
        current = current->next;
    }
    XiftAttribute &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;
    }
}


XiftAttribute *XiftAttributes::Top()
{
    if (stack) {
        return &stack->item;
    } else {
        return 0;
    }
}

void XiftAttributes::Pop()
{
    if (stack) {
        Stack *old = stack;
        stack      = stack->next;
        delete old;
    }
}

XiftAttribute &XiftAttributes::New()
{
    Stack *old  = stack;
    stack       = new Stack;
    stack->next = old;
    return stack->item;
}

bool XiftAttributes::ContainsMatchedForm(const XiftAttribute &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;
}