summaryrefslogtreecommitdiffstats
path: root/common/sys/vector.c
blob: d2c7a565829bd3c6c6fe390457b3b14317299ab1 (plain) (blame)
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
/* $Id$ */
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "cmsys.h"

#define chartoupper(c)  ((c >= 'a' && c <= 'z') ? c+'A'-'a' : c)

void
Vector_init(struct Vector *self, const int size)
{
    assert(size != 0);
    self->size = size;
    self->length = 0;
    self->capacity = 0;
    self->base = NULL;
    self->constant = false;
}

void
Vector_init_const(struct Vector *self, char * base, const int length, const int size)
{
    assert(size != 0);
    self->size = size;
    self->length = length;
    self->capacity = 0;
    self->base = base;
    self->constant = true;
}

void
Vector_delete(struct Vector *self)
{
    self->length = 0;
    self->capacity = 0;
    if (self->base && !self->constant)
    free(self->base);
    self->base = NULL;
    self->constant = false;
}

void
Vector_clear(struct Vector *self, const int size)
{
    Vector_delete(self);
    Vector_init(self, size);
}

int
Vector_length(struct Vector *self)
{
    return self->length;
}

void
Vector_resize(struct Vector *self, const int length)
{
    int capacity = length * self->size;

    assert(!self->constant);

#define MIN_CAPACITY 4096
    if (capacity == 0) {
    if (self->base)
        free(self->base);
    self->base = NULL;
    self->capacity = 0;
    } else {
    int old_capacity = self->capacity;
    assert(capacity > 0);
    if (self->capacity == 0)
        self->capacity = MIN_CAPACITY;
    //if (self->capacity > capacity && self->capacity > MIN_CAPACITY)
    //    self->capacity /= 2;
    while (self->capacity < capacity)
        self->capacity *= 2;

    if (old_capacity != self->capacity || self->base == NULL) {
        char *tmp = (char *)realloc(self->base, self->capacity);
        assert(tmp);
        self->base = tmp;
    }
    }
}

void
Vector_add(struct Vector *self, const char *name)
{
    assert(!self->constant);
    Vector_resize(self, self->length+1);
    strlcpy(self->base + self->size * self->length, name, self->size);
    self->length++;
}

const char*
Vector_get(struct Vector *self, const int idx)
{
    assert(0 <= idx && idx < self->length);
    return self->base + self->size * idx;
}

int
Vector_MaxLen(const struct Vector *list, const int offset, const int count)
{
    int i;
    int maxlen = 0;

    for(i=offset; i<list->length; i++) {
    int len = strlen(list->base + list->size * i);
    if (len > maxlen)
        maxlen = len;
    }
    assert(maxlen <= list->size);
    return maxlen;
}

int
Vector_match(const struct Vector *src, struct Vector *dst, const int key, const int pos)
{
    int uckey, lckey;
    int i;

    Vector_clear(dst, src->size);

    uckey = chartoupper(key);
    if (key >= 'A' && key <= 'Z')
    lckey = key | 0x20;
    else
    lckey = key;

    for (i=0; i<src->length; i++) {
    int ch = src->base[src->size * i + pos];
    if (ch == lckey || ch == uckey)
        Vector_add(dst, src->base + src->size * i);
    }

    return dst->length;
}

void
Vector_sublist(struct Vector *src, struct Vector *dst, const char *tag)
{
    int i;
    int len;
    Vector_clear(dst, src->size);

    len = strlen(tag);
    for (i=0; i<src->length; i++)
    if (len==0 || strncasecmp(src->base + src->size * i, tag, len)==0)
        Vector_add(dst, src->base + src->size * i);
}

int
Vector_remove(struct Vector *self, const char *name)
{
    int i;
    assert(!self->constant);
    for (i=0; i<self->length; i++)
    if (strcasecmp(self->base + self->size * i, name) == 0) {
        strlcpy(self->base + self->size * i,
            self->base + self->size * (self->length-1), self->size);

        self->length--;
        Vector_resize(self, self->length);
        return 1;
    }
    return 0;
}

int
Vector_search(const struct Vector *self, const char *name)
{
    int i;
    for (i=0; i<self->length; i++)
    if (strcasecmp(self->base + self->size * i, name) == 0)
        return 1;
    return 0;
}