File: tlist.c

package info (click to toggle)
kbtin 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,856 kB
  • sloc: ansic: 18,627; perl: 179; sh: 88; makefile: 17
file content (199 lines) | stat: -rw-r--r-- 4,556 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "tintin.h"
#include "protos/glob.h"
#include "protos/print.h"
#include "protos/utils.h"


/******************************************************************/
/* compare priorities of a and b in a semi-lexicographical order: */
/* strings generally sort in ASCIIbetical order, however numbers  */
/* sort according to their numerical values.                      */
/******************************************************************/
int prioritycmp(const char *a, const char *b)
{
    int res;

not_numeric:
    while (*a && *a==*b && !isadigit(*a))
    {
        a++;
        b++;
    }
    if (!isadigit(*a) || !isadigit(*b))
        return (*a<*b)? -1 : (*a>*b)? 1 : 0;
    while (*a=='0')
        a++;
    while (*b=='0')
        b++;
    res=0;
    while (isadigit(*a))
    {
        if (!isadigit(*b))
            return 1;
        if (*a!=*b && !res)
            res=(*a<*b)? -1 : 1;
        a++;
        b++;
    }
    if (isadigit(*b))
        return -1;
    if (res)
        return res;
    goto not_numeric;
}

/*****************************************************/
/* strcmp() that sorts '\0' later than anything else */
/*****************************************************/
static int strlongercmp(const char *a, const char *b)
{
next:
    if (!*a)
        return *b? 1 : 0;
    if (*a==*b)
    {
        a++;
        b++;
        goto next;
    }
    if (!*b || ((unsigned char)*a) < ((unsigned char)*b))
        return -1;
    return 1;
}

static int tripcmp(const ptrip a, const ptrip b)
{
    if (a->pr)
    {
        assert(b->pr);
        int r=prioritycmp(a->pr, b->pr);
        if (r)
            return r;
    }
    return strlongercmp(a->left, b->left);
}

/**/ KBTREE_CODE(trip, ptrip, tripcmp)

kbtree_t(trip)* init_tlist(void)
{
    return kb_init(trip, KB_DEFAULT_SIZE);
}

void kill_tlist(kbtree_t(trip) *l)
{
    TRIP_ITER(l, i)
        free(i->left);
        free(i->right);
        free(i->pr);
        free(i);
    ENDITER

    kb_destroy(trip, l);
}

void show_trip(const ptrip t, struct session *ses)
{
    if (t->pr)
        tintin_printf(ses, "~7~{%s~7~}={%s~7~} @ {%s}", t->left, t->right, t->pr);
    else
        tintin_printf(ses, "~7~{%s~7~}={%s~7~}", t->left, t->right);
}

bool show_tlist(kbtree_t(trip) *l, const char *pat, const char *msg, bool no_pr, struct session *ses)
{
    if (no_pr && pat && is_literal(pat))
    {
        struct trip srch = {(char*)pat, 0, 0};
        const ptrip *t = kb_get(trip, l, &srch);
        if (!t)
            return false;
        if (msg)
            tintin_printf(ses, msg);
        show_trip(*t, ses);
        return true;
    }

    bool had_any = false;

    TRIP_ITER(l, t)
        if (pat && !match(pat, t->left))
            continue;
        if (!had_any)
        {
            had_any = true;
            if (msg)
                tintin_printf(ses, msg);
        }
        show_trip(t, ses);
    ENDITER

    return had_any;
}

bool delete_tlist(kbtree_t(trip) *l, const char *pat, const char *msg, bool (*checkright)(char **right), bool no_pr, struct session *ses)
{
    if (no_pr && pat && is_literal(pat))
    {
        struct trip srch = {(char*)pat, 0, 0};
        ptrip *d = kb_get(trip, l, &srch);
        if (!d)
            return false;
        ptrip t = *d;
        if (checkright && checkright(&t->right))
            return false;
        kb_del(trip, l, &srch);
        if (msg)
            tintin_printf(ses, msg, t->left);
        free(t->left);
        free(t->right);
        free(t->pr);
        free(t);
        return true;
    }

    ptrip *todel = malloc(kb_size(l) * sizeof(ptrip));
    ptrip *last = todel;

    TRIP_ITER(l, t)
        if (pat && !match(pat, t->left))
            continue;
        if (checkright && checkright(&t->right))
            continue;
        *last++ = t;
    ENDITER

    for (ptrip *del = todel; del != last; del++)
    {
        if (msg)
            tintin_printf(ses, msg, (*del)->left);
        kb_del(trip, l, *del);
        free((*del)->left);
        free((*del)->right);
        free((*del)->pr);
        free(*del);
    }

    free(todel);
    return last != todel;
}

kbtree_t(trip) *copy_tlist(kbtree_t(trip) *a)
{
    kbtree_t(trip) *b = kb_init(trip, KB_DEFAULT_SIZE);

    TRIP_ITER(a, old)
        ptrip new = MALLOC(sizeof(struct trip));
        new->left = mystrdup(old->left);
        new->right = mystrdup(old->right);
        new->pr = mystrdup(old->pr);
        kb_put(trip, b, new);
    ENDITER

    return b;
}

int count_tlist(kbtree_t(trip) *s)
{
    return kb_size(s);
}