diff options
Diffstat (limited to 'tests/binpack.c')
-rw-r--r-- | tests/binpack.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/tests/binpack.c b/tests/binpack.c new file mode 100644 index 0000000..b54ed80 --- /dev/null +++ b/tests/binpack.c | |||
@@ -0,0 +1,86 @@ | |||
1 | /* This file is part of vmod-binlog | ||
2 | Copyright (C) 2013 Sergey Poznyakoff | ||
3 | |||
4 | Vmod-binlog 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 3, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | Vmod-binlog 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 | ||
15 | along with vmod-binlog. If not, see <http://www.gnu.org/licenses/>. | ||
16 | */ | ||
17 | #include <unistd.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <stdio.h> | ||
20 | #include <stdarg.h> | ||
21 | #include "pack.h" | ||
22 | #include "err.h" | ||
23 | |||
24 | int | ||
25 | main(int argc, char **argv) | ||
26 | { | ||
27 | enum { mode_packin, mode_packout } mode = mode_packin; | ||
28 | struct packinst *pi; | ||
29 | struct packenv *env; | ||
30 | char *end; | ||
31 | int c; | ||
32 | |||
33 | setprogname(argv[0]); | ||
34 | |||
35 | while ((c = getopt(argc, argv, "d")) != EOF) { | ||
36 | switch (c) { | ||
37 | case 'd': | ||
38 | mode = mode_packout; | ||
39 | break; | ||
40 | default: | ||
41 | exit(1); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | argc -= optind; | ||
46 | argv += optind; | ||
47 | |||
48 | if (argc == 0) { | ||
49 | error("not enough arguments"); | ||
50 | exit(1); | ||
51 | } | ||
52 | |||
53 | pi = packcomp(argv[0], &end); | ||
54 | if (!pi) { | ||
55 | error("out of memory"); | ||
56 | abort(); | ||
57 | } | ||
58 | if (*end) { | ||
59 | error("compile error near %s", end); | ||
60 | exit(1); | ||
61 | } | ||
62 | env = packenv_create(packsize(pi)); | ||
63 | if (!env) { | ||
64 | error("out of memory"); | ||
65 | abort(); | ||
66 | } | ||
67 | |||
68 | switch (mode) { | ||
69 | case mode_packin: | ||
70 | env->argv = argv + 1; | ||
71 | env->argc = argc - 1; | ||
72 | packin(pi, env); | ||
73 | fwrite(env->buf_base, env->buf_size, 1, stdout); | ||
74 | break; | ||
75 | |||
76 | case mode_packout: | ||
77 | env->fp = stdout; | ||
78 | fread(env->buf_base, env->buf_size, 1, stdin); | ||
79 | packout(pi, env); | ||
80 | fputc('\n', stdout); | ||
81 | break; | ||
82 | } | ||
83 | |||
84 | packenv_free(env); | ||
85 | packfree(pi); | ||
86 | } | ||