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