1 | #include <stdio.h> |
---|
2 | #include <stdlib.h> |
---|
3 | #include <string.h> |
---|
4 | #include <arbdb.h> |
---|
5 | |
---|
6 | static void to_stderr(const char *msg) { |
---|
7 | fprintf(stderr, "arb_2_ascii: %s\n", msg); |
---|
8 | } |
---|
9 | |
---|
10 | int main(int argc, char **argv) { |
---|
11 | GB_ERROR error = 0; |
---|
12 | |
---|
13 | fprintf(stderr, "arb_2_ascii - ARB database binary to ascii converter\n"); |
---|
14 | |
---|
15 | if (argc<2 || strcmp(argv[1], "--help") == 0) { |
---|
16 | fprintf(stderr, |
---|
17 | "\n" |
---|
18 | "Usage: arb_2_ascii <source.arb> [<dest.arb>|-]\n" |
---|
19 | "Purpose: Converts a database to ascii format\n" |
---|
20 | "\n" |
---|
21 | "if <dest.arb> is set, try to fix problems of <source.arb> and write to <dest.arb>\n" |
---|
22 | "if the second parameter is '-' print to console.\n" |
---|
23 | "else replace <source.arb> by ascii version (backup first)\n" |
---|
24 | "\n" |
---|
25 | ); |
---|
26 | |
---|
27 | if (strcmp(argv[1], "--help") != 0) { error = "Missing arguments"; } |
---|
28 | } |
---|
29 | else { |
---|
30 | char *in = argv[1]; |
---|
31 | char *out = NULL; |
---|
32 | |
---|
33 | const char *readflags = "rw"; |
---|
34 | const char *saveflags = "a"; |
---|
35 | |
---|
36 | if (argc == 2) { |
---|
37 | out = in; |
---|
38 | } |
---|
39 | else { |
---|
40 | readflags = "rwR"; /* try to recover corrupt data */ |
---|
41 | out = argv[2]; |
---|
42 | |
---|
43 | if (!out || strcmp(out, "-") == 0) { |
---|
44 | saveflags = "aS"; |
---|
45 | GB_install_information(to_stderr); |
---|
46 | GB_install_warning(to_stderr); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | GBDATA *gb_main = GB_open(in,readflags); |
---|
51 | if (!gb_main) { |
---|
52 | error = GB_await_error(); |
---|
53 | } |
---|
54 | else { |
---|
55 | error = GB_save(gb_main,out, saveflags); |
---|
56 | } |
---|
57 | |
---|
58 | GB_close(gb_main); |
---|
59 | } |
---|
60 | |
---|
61 | if (error) { |
---|
62 | fprintf(stderr, "arb_2_ascii: Error: %s\n", error); |
---|
63 | return EXIT_FAILURE; |
---|
64 | } |
---|
65 | return EXIT_SUCCESS; |
---|
66 | } |
---|