]> mj.ucw.cz Git - libucw.git/blob - lib/sort-test.c
Fixed operator priorities in cf_parse_ip().
[libucw.git] / lib / sort-test.c
1 /* Test for sorting routines */
2
3 #include "lib/lib.h"
4 #include "lib/conf2.h"
5 #include "lib/fastbuf.h"
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <getopt.h>
11
12 struct key {
13   char line[4096];
14 };
15
16 #define SORT_KEY struct key
17 #define SORT_PREFIX(x) s_##x
18 #define SORT_PRESORT
19 #define SORT_INPUT_FILE
20 #define SORT_OUTPUT_FILE
21 #define SORT_UNIFY
22
23 static inline int
24 s_compare(struct key *a, struct key *b)
25 {
26   return strcmp(a->line, b->line);
27 }
28
29 static inline int
30 s_fetch_key(struct fastbuf *f, struct key *a)
31 {
32   return !!bgets(f, a->line, sizeof(a->line));
33 }
34
35 static inline void
36 s_copy_data(struct fastbuf *src UNUSED, struct fastbuf *dest, struct key *k)
37 {
38   bputsn(dest, k->line);
39 }
40
41 static inline byte *
42 s_fetch_item(struct fastbuf *src UNUSED, struct key *k, byte *limit UNUSED)
43 {
44   byte *end = (byte *) k->line + strlen(k->line) + 1;
45 #if 0                                   /* Testing splits */
46   uns r = random_max(10000);
47   if (end + r <= limit)
48     return end + r;
49   else
50     return NULL;
51 #else
52   return end;
53 #endif
54 }
55
56 static inline void
57 s_store_item(struct fastbuf *f, struct key *k)
58 {
59   s_copy_data(NULL, f, k);
60 }
61
62 #ifdef SORT_UNIFY
63 static inline void
64 s_merge_data(struct fastbuf *src1 UNUSED, struct fastbuf *src2 UNUSED, struct fastbuf *dest, struct key *k1, struct key *k2 UNUSED)
65 {
66   s_copy_data(NULL, dest, k1);
67 }
68
69 static inline struct key *
70 s_merge_items(struct key *a, struct key *b UNUSED)
71 {
72   return a;
73 }
74 #endif
75
76 #include "lib/sorter.h"
77
78 int
79 main(int argc, char **argv)
80 {
81   log_init(NULL);
82   if (cf_get_opt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0 ||
83       optind != argc - 2)
84   {
85     fputs("This program supports only the following command-line arguments:\n" CF_USAGE, stderr);
86     exit(1);
87   }
88
89   s_sort(argv[optind], argv[optind+1]);
90   return 0;
91 }