Debian Codegraph @

lib/sorter.h

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 2004, 2005 Antti-Juhani Kaijanaho
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef SORTER_H
20 #define SORTER_H
21
22 #include <stdbool.h>
23 #include "para_bundle.h"
24
25 struct key {
26 int field_inx;
27 enum { FT_STRING, FT_VERSION } type;
28 bool reverse : 1;
29 };
30
31 struct keys {
32 size_t max;
33 size_t nks;
34 struct key * keys;
35 };
36
37 typedef struct keys keys_t;
38
39 static inline
40 void keys_init(keys_t * ks)
41 {
42 ks->max = 0;
43 ks->nks = 0;
44 ks->keys = 0;
45 }
46
47 static inline
48 void keys_append(keys_t * ks, struct key key)
49 {
50 assert(ks != 0);
51 if (ks->nks == ks->max) {
52 size_t max = ks->max == 0 ? 4 : 2 * ks->max;
53 struct key * keys = realloc(ks->keys, max * sizeof *keys);
54 if (keys == 0) fatal_enomem(0);
55 ks->max = max;
56 ks->keys = keys;
57 }
58 assert(ks->nks < ks->max);
59 ks->keys[ks->nks++] = key;
60 }
61
62 static inline
63 void keys_fini(keys_t * ks)
64 {
65 assert(ks != 0);
66 ks->max = 0;
67 ks->nks = 0;
68 free(ks->keys);
69 ks->keys = 0;
70 }
71
72 int para_compare(keys_t *, const para_t *, const para_t *);
73
74 /*
75 Note that this function is NOT reentrant!
76 */
77 void sort_bundle(keys_t *, struct para_bundle *);
78
79
80 #endif /* SORTER_H */