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