Debian Codegraph @

lib/sorter.c

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 2004, 2005, 2008 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 #include <stdlib.h>
20
21 #include "msg.h"
22 #include "sorter.h"
23 #include "version.h"
24
25 //void keys_fini(keys_t *);
26
27
28
29 static keys_t *keys;
30
31 int para_compare(keys_t *keys, const para_t *a, const para_t *b)
32 {
33 int r = 0;
34 for (size_t i = 0; i < keys->nks; i++) {
35 char * af = get_field_as(a, keys->keys[i].field_inx);
36 char * bf = get_field_as(b, keys->keys[i].field_inx);
37 if (af == 0 || bf == 0) fatal_enomem(0);
38 switch (keys->keys[i].type) {
39 case FT_STRING:
40 r = strcmp(af, bf);
41 break;
42 case FT_VERSION:
43 ;
44 struct versionrevision ar, br;
45 bool aok = parse_version(&ar, af, strlen(af));
46 bool bok = parse_version(&br, bf, strlen(bf));
47 if (!aok || !bok) {
48 message(L_IMPORTANT, 0,
49 _("Parse error in field."));
50 free(af);
51 free(bf);
52 return aok ? (bok ? 0 : 1) : (bok ? -1 : 0);
53 }
54 r = versioncompare(&ar, &br);
55 break;
56 }
57 debug("cmp: a = %s, b = %s, verdict is %d", af, bf, r);
58 free(af);
59 free(bf);
60 if (keys->keys[i].reverse) r = -r;
61 if (r != 0) break;
62 }
63
64 return r;
65 }
66
67 static
68 int compare(const void * av, const void * bv)
69 {
70 debug("cmp: av = %p, bv = %p", av, bv);
71 para_t ** a = (para_t **)av;
72 para_t ** b = (para_t **)bv;
73
74 return para_compare(keys, *a, *b);
75 }
76
77 void sort_bundle(keys_t * ks, struct para_bundle * pb)
78 {
79 size_t num_paras = bundle_size(pb);
80 para_t ** paras = bundle_vec(pb);
81
82 keys = ks;
83
84 debug("sort_bundle: num_paras = %zd", num_paras);
85 debug("sort_bundle: sizeof *paras = %zd", sizeof *paras);
86 debug("sort_bundle: paras = %p, paras+num_paras*sizeof *paras = %p",
87 paras, paras+num_paras*sizeof *paras);
88
89 qsort(paras, num_paras, sizeof *paras, compare);
90 }