forked from alexpevzner/sane-airscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairscan-array.c
237 lines (196 loc) · 4.33 KB
/
airscan-array.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* SANE_Word/SANE_String arrays
*/
#include "airscan.h"
#include <stdlib.h>
#include <string.h>
/* Initial capacity of arrays
*/
#define ARRAY_INITIAL_CAPACITY 4
/* Create array of SANE_Word
*/
SANE_Word*
sane_word_array_new (void)
{
return g_new0(SANE_Word, ARRAY_INITIAL_CAPACITY);
}
/* Free array of SANE_Word
*/
void
sane_word_array_free (SANE_Word *a)
{
g_free(a);
}
/* Reset array of SANE_Word
*/
void
sane_word_array_reset (SANE_Word **a)
{
(*a)[0] = 0;
}
/* Get length of the SANE_Word array
*/
size_t
sane_word_array_len (const SANE_Word *a)
{
return (size_t) a[0];
}
/* Append word to array. Returns new array (old becomes invalid)
*/
SANE_Word*
sane_word_array_append (SANE_Word *a, SANE_Word w)
{
size_t sz = sane_word_array_len(a) + 1;
/* If sz reached the power-of-2, reallocate the array, doubling its size */
if (sz >= ARRAY_INITIAL_CAPACITY && (sz & (sz - 1)) == 0) {
a = g_renew(SANE_Word, a, sz + sz);
}
a[sz] = w;
a[0] = sz;
return a;
}
/* Compare function for sane_word_array_sort
*/
static int
sane_word_array_sort_cmp(const void *p1, const void *p2)
{
return *(SANE_Word*) p1 - *(SANE_Word*) p2;
}
/* Sort array of SANE_Word in increasing order
*/
void
sane_word_array_sort(SANE_Word *a)
{
SANE_Word len = a[0];
if (len) {
qsort(a + 1, len, sizeof(SANE_Word), sane_word_array_sort_cmp);
}
}
/* Intersect two sorted arrays.
*/
SANE_Word*
sane_word_array_intersect_sorted (const SANE_Word *a1, const SANE_Word *a2)
{
const SANE_Word *end1 = a1 + sane_word_array_len(a1) + 1;
const SANE_Word *end2 = a2 + sane_word_array_len(a2) + 1;
SANE_Word *out = sane_word_array_new();
a1 ++;
a2 ++;
while (a1 < end1 && a2 < end2) {
if (*a1 < *a2) {
a1 ++;
} else if (*a1 > *a2) {
a2 ++;
} else {
out = sane_word_array_append(out, *a1);
a1 ++;
a2 ++;
}
}
return out;
}
/* Create initialize array of SANE_String
*/
SANE_String*
sane_string_array_new (void)
{
return g_new0(SANE_String, ARRAY_INITIAL_CAPACITY);
}
/* Free array of SANE_String
*/
void
sane_string_array_free (SANE_String *a)
{
g_free(a);
}
/* Reset array of SANE_String
*/
void
sane_string_array_reset (SANE_String *a)
{
a[0] = NULL;
}
/* Get length of the SANE_String array
*/
size_t
sane_string_array_len (const SANE_String *a)
{
size_t sz;
for (sz = 0; a[sz] != NULL; sz ++)
;
return sz;
}
/* Append string to array Returns new array (old becomes invalid)
*/
SANE_String*
sane_string_array_append(SANE_String *a, SANE_String s)
{
size_t sz = sane_string_array_len(a) + 1;
/* If sz reached the power-of-2, reallocate the array, doubling its size */
if (sz >= ARRAY_INITIAL_CAPACITY && (sz & (sz - 1)) == 0) {
a = g_renew(SANE_String, a, sz + sz);
}
/* Append string */
a[sz - 1] = s;
a[sz] = NULL;
return a;
}
/* Compute max string length in array of strings
*/
size_t
sane_string_array_max_strlen(const SANE_String *a)
{
size_t max_len = 0;
for (; *a != NULL; a ++) {
size_t len = strlen(*a);
if (len > max_len) {
max_len = len;
}
}
return max_len;
}
/* Create array of SANE_Device
*/
const SANE_Device**
sane_device_array_new (void)
{
return g_new0(const SANE_Device*, ARRAY_INITIAL_CAPACITY);
}
/* Free array of SANE_Device
*/
void
sane_device_array_free (const SANE_Device **a)
{
g_free(a);
}
/* Get length of the SANE_Device array
*/
size_t
sane_device_array_len (const SANE_Device * const *a)
{
size_t sz;
for (sz = 0; a[sz] != NULL; sz ++)
;
return sz;
}
/* Append device to array. Returns new array (old becomes invalid)
*/
const SANE_Device**
sane_device_array_append(const SANE_Device **a, SANE_Device *d)
{
size_t sz = sane_device_array_len(a) + 1;
/* If sz reached the power-of-2, reallocate the array, doubling its size */
if (sz >= ARRAY_INITIAL_CAPACITY && (sz & (sz - 1)) == 0) {
a = g_renew(const SANE_Device*, a, sz + sz);
}
/* Append a device */
a[sz - 1] = d;
a[sz] = NULL;
return a;
}
/* vim:ts=8:sw=4:et
*/