| 1 | // =============================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : arb_2_ascii.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // =============================================================== // |
|---|
| 10 | |
|---|
| 11 | #include <arbdb.h> |
|---|
| 12 | #include <arb_handlers.h> |
|---|
| 13 | |
|---|
| 14 | int ARB_main(int argc, char *argv[]) { |
|---|
| 15 | GB_ERROR error = NULp; |
|---|
| 16 | |
|---|
| 17 | ARB_redirect_handlers_to(stderr, stderr); |
|---|
| 18 | |
|---|
| 19 | fprintf(stderr, "arb_2_ascii - ARB database binary to ascii converter\n"); |
|---|
| 20 | |
|---|
| 21 | bool recovery = false; // try to recover corrupt data ? |
|---|
| 22 | char compressionFlag[] = "\0x"; // extra space for compression flag |
|---|
| 23 | bool help_requested = false; |
|---|
| 24 | |
|---|
| 25 | while (argv[1]) { |
|---|
| 26 | if (strcmp(argv[1], "-r") == 0) { |
|---|
| 27 | recovery = true; |
|---|
| 28 | argv++; argc--; |
|---|
| 29 | } |
|---|
| 30 | else if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { |
|---|
| 31 | help_requested = true; |
|---|
| 32 | argv++; argc--; |
|---|
| 33 | } |
|---|
| 34 | else if (strncmp(argv[1], "-C", 2) == 0) { |
|---|
| 35 | compressionFlag[0] = argv[1][2]; |
|---|
| 36 | compressionFlag[1] = 0; |
|---|
| 37 | argv++; argc--; |
|---|
| 38 | } |
|---|
| 39 | else break; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | if (!argv[1]) help_requested = true; |
|---|
| 43 | |
|---|
| 44 | if (help_requested) { |
|---|
| 45 | fprintf(stderr, |
|---|
| 46 | "\n" |
|---|
| 47 | "Usage: arb_2_ascii [-r] [-C<type>] <source.arb> [<dest.arb>|-]\n" |
|---|
| 48 | "Purpose: Converts a database to ascii format\n" |
|---|
| 49 | "\n" |
|---|
| 50 | "if '-r' (=recovery) is specified, try to fix problems while reading <source.arb>.\n" |
|---|
| 51 | "\n" |
|---|
| 52 | "if '-C' (=Compress) is specified, use extra compression\n" |
|---|
| 53 | " known <type>s: %s\n" |
|---|
| 54 | "\n" |
|---|
| 55 | "if <dest.arb> is set, write to <dest.arb>\n" |
|---|
| 56 | "if the second parameter is '-' write to console.\n" |
|---|
| 57 | "else replace <source.arb> by ascii version (backup first)\n" |
|---|
| 58 | "\n", |
|---|
| 59 | GB_get_supported_compression_flags(true)); |
|---|
| 60 | } |
|---|
| 61 | else { |
|---|
| 62 | const char *readflags = recovery ? "rwR" : "rw"; |
|---|
| 63 | char saveflags[] = "a\0xx"; // extra optional space for: compressionFlag, 'S' |
|---|
| 64 | |
|---|
| 65 | strcat(saveflags, compressionFlag); |
|---|
| 66 | if (!strchr(GB_get_supported_compression_flags(false), compressionFlag[0])) { |
|---|
| 67 | error = GBS_global_string("Unknown compression flag '%c'", compressionFlag[0]); |
|---|
| 68 | } |
|---|
| 69 | else { |
|---|
| 70 | const char *in = argv[1]; |
|---|
| 71 | const char *out = NULp; |
|---|
| 72 | |
|---|
| 73 | if (argc == 2) { |
|---|
| 74 | out = in; |
|---|
| 75 | } |
|---|
| 76 | else { |
|---|
| 77 | out = argv[2]; |
|---|
| 78 | |
|---|
| 79 | if (!out || strcmp(out, "-") == 0) { |
|---|
| 80 | strcat(saveflags, "S"); |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | if (!in) { |
|---|
| 85 | error = "Missing parameters"; |
|---|
| 86 | } |
|---|
| 87 | else { |
|---|
| 88 | GB_shell shell; |
|---|
| 89 | GBDATA *gb_main = GB_open(in, readflags); |
|---|
| 90 | if (!gb_main) { |
|---|
| 91 | error = GB_await_error(); |
|---|
| 92 | } |
|---|
| 93 | else { |
|---|
| 94 | error = GB_save(gb_main, out, saveflags); |
|---|
| 95 | GB_close(gb_main); |
|---|
| 96 | } |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if (error) { |
|---|
| 102 | fprintf(stderr, "arb_2_ascii: Error: %s\n", error); |
|---|
| 103 | return EXIT_FAILURE; |
|---|
| 104 | } |
|---|
| 105 | return EXIT_SUCCESS; |
|---|
| 106 | } |
|---|