-
Notifications
You must be signed in to change notification settings - Fork 2
/
str_list.c
148 lines (130 loc) · 3.19 KB
/
str_list.c
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
/*
tdiff - tree diffs
str_list.c - Lists of strings
Copyright (C) 1999, 2014, 2019 Philippe Troin <phil+github-commits@fifi.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "str_list.h"
#include <string.h>
#include "tdiff.h"
#include "utils.h"
void
str_list_new_size(str_list_t **l, size_t sz)
{
str_list_t* rv;
/**/
*l = NULL;
rv = xmalloc(sizeof(str_list_t));
rv->size = 0;
rv->avail = sz;
if (rv->avail > 0)
rv->strings = xmalloc(sizeof(const char*)*rv->avail);
else
rv->strings = 0;
*l =rv;
}
static inline void
str_list_grow(str_list_t *l)
{
if (l->size == l->avail)
{
l->avail = l->avail ? l->avail*2 : 1;
l->strings = xrealloc(l->strings, l->avail*sizeof(const char*));
}
}
void
str_list_push(str_list_t *l, const char* s)
{
str_list_grow(l);
l->strings[l->size++] = xstrdup(s);
}
void
str_list_push_length(str_list_t *l, const char* s, size_t sz)
{
char *buf;
/**/
str_list_grow(l);
buf = xmalloc(sz);
memcpy(buf, s, sz);
l->strings[l->size++] = buf;
}
static
int strpcmp(const char** s1, const char** s2)
{
return (strcmp(*s1, *s2));
}
static inline void
str_list_sort(const str_list_t *l)
{
qsort(l->strings, l->size, sizeof(char*),
(int(*)(const void*, const void*))strpcmp);
}
void
str_list_destroy(str_list_t *d)
{
size_t i;
/**/
for (i=0; i<d->size; ++i)
free((char*)d->strings[i]);
free(d->strings);
free(d);
}
int
str_list_compare(const char *p1, const char* p2,
const str_list_t *ct1, const str_list_t *ct2,
int (*reportMissing)(int, const char*, const char*, struct str_list_client_data_s*),
int (*compareEntries)(const char* p1, const char* p2,
const char* e1, const char* e2,
struct str_list_client_data_s*),
struct str_list_client_data_s *clientData)
{
size_t i1, i2;
int cmpres;
int rv = XIT_OK;
int nrv;
/**/
str_list_sort(ct1);
str_list_sort(ct2);
for (i1 = i2 = 0; i1 < ct1->size || i2 < ct2->size; )
{
if (i1 == ct1->size)
goto missing_2;
else if (i2 == ct2->size)
goto missing_1;
else if (!(cmpres = strcmp(ct1->strings[i1], ct2->strings[i2])))
{
if (compareEntries != NULL)
{
nrv = compareEntries(p1, p2, ct1->strings[i1], ct2->strings[i2], clientData);
BUMP_EXIT_CODE(rv, nrv);
}
i1++;
i2++;
}
else if (cmpres<0)
{
missing_1:
nrv = reportMissing(1, p1, ct1->strings[i1], clientData);
BUMP_EXIT_CODE(rv, nrv);
i1++;
}
else /* cmpres>0 */
{
missing_2:
nrv = reportMissing(2, p2, ct2->strings[i2], clientData);
BUMP_EXIT_CODE(rv, nrv);
i2++;
}
}
return rv;
}