| 1 | /* |
| 2 | Copyright (C) 2000 Marco Ziech ([email protected]) |
| 3 | Copyright (C) 2000 Ross Combs ([email protected]) |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include "common/setup_before.h" |
| 20 | #include <cstdlib> |
| 21 | #include <cstring> |
| 22 | #include <cerrno> |
| 23 | |
| 24 | #include "common/version.h" |
| 25 | #include "tga.h" |
| 26 | #include "fileio.h" |
| 27 | #include "common/setup_after.h" |
| 28 | |
| 29 | using namespace pvpgn::bni; |
| 30 | |
| 31 | namespace |
| 32 | { |
| 33 | |
| 34 | void usage(char const * progname) |
| 35 | { |
| 36 | std::fprintf(stderr, |
| 37 | "usage: %s [<options>] [--] [<BNI file>]\n" |
| 38 | " -h, --help, --usage show this information and exit\n" |
| 39 | " -v, --version print version number and exit\n", progname); |
| 40 | |
| 41 | std::exit(EXIT_FAILURE); |
| 42 | } |
| 43 | |
| 44 | } |
| 45 | |
| 46 | extern int main(int argc, char * argv[]) |
| 47 | { |
| 48 | char const * bnifile = NULL; |
| 49 | std::FILE * fp; |
| 50 | int a; |
| 51 | int forcefile = 0; |
| 52 | char dash[] = "-"; /* unique address used as flag */ |
| 53 | |
| 54 | if (argc < 1 || !argv || !argv[0]) |
| 55 | { |
| 56 | std::fprintf(stderr, "bad arguments\n"); |
| 57 | return EXIT_FAILURE; |
| 58 | } |
| 59 | |
| 60 | for (a = 1; a < argc; a++) |
| 61 | if (forcefile && !bnifile) |
| 62 | bnifile = argv[a]; |
| 63 | else if (std::strcmp(argv[a], "-") == 0 && !bnifile) |
| 64 | bnifile = dash; |
| 65 | else if (argv[a][0] != '-' && !bnifile) |
| 66 | bnifile = argv[a]; |
| 67 | else if (forcefile || argv[a][0] != '-' || std::strcmp(argv[a], "-") == 0) |
| 68 | { |
| 69 | std::fprintf(stderr, "%s: extra file argument \"%s\"\n", argv[0], argv[a]); |
| 70 | usage(argv[0]); |
| 71 | } |
| 72 | else if (std::strcmp(argv[a], "--") == 0) |
| 73 | forcefile = 1; |
| 74 | else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0) |
| 75 | { |
| 76 | std::printf("version " PVPGN_VERSION "\n"); |
| 77 | return EXIT_SUCCESS; |
| 78 | } |
| 79 | else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage") |
| 80 | == 0) |
| 81 | usage(argv[0]); |
| 82 | else |
| 83 | { |
| 84 | std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]); |
| 85 | usage(argv[0]); |
| 86 | } |
| 87 | |
| 88 | if (!bnifile) |
| 89 | bnifile = dash; |
| 90 | |
| 91 | if (bnifile == dash) |
| 92 | fp = stdin; |
| 93 | else |
| 94 | if (!(fp = std::fopen(bnifile, "r"))) |
| 95 | { |
| 96 | std::fprintf(stderr, "%s: could not open BNI file \"%s\" for reading (std::fopen: %s)\n", argv[0], bnifile, std::strerror(errno)); |
| 97 | std::exit(EXIT_FAILURE); |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | t_tgaimg * tgaimg; |
| 102 | int i; |
| 103 | int bniid, unknown, icons, datastart; |
| 104 | int expected_width, expected_height; |
| 105 | |
| 106 | file_rpush(fp); |
| 107 | bniid = file_readd_le(); |
| 108 | unknown = file_readd_le(); |
| 109 | icons = file_readd_le(); |
| 110 | datastart = file_readd_le(); |
| 111 | std::fprintf(stderr, "BNIHeader: id=0x%08x unknown=0x%08x icons=0x%08x datastart=0x%08x\n", bniid, unknown, icons, datastart); |
| 112 | expected_width = 0; |
| 113 | expected_height = 0; |
| 114 | for (i = 0; i < icons; i++) { |
| 115 | int id, x, y, flags, tag; |
| 116 | id = file_readd_le(); |
| 117 | x = file_readd_le(); |
| 118 | y = file_readd_le(); |
| 119 | if (id == 0) { |
| 120 | tag = file_readd_le(); |
| 121 | } |
| 122 | else { |
| 123 | tag = 0; |
| 124 | } |
| 125 | flags = file_readd_le(); |
| 126 | std::fprintf(stderr, "Icon[%d]: id=0x%08x x=%d y=%d tag=0x%08x(\"%c%c%c%c\") flags=0x%08x\n", i, id, x, y, tag, |
| 127 | ((unsigned char)((tag >> 24) & 0xff)), |
| 128 | ((unsigned char)((tag >> 16) & 0xff)), |
| 129 | ((unsigned char)((tag >> 8) & 0xff)), |
| 130 | ((unsigned char)((tag)& 0xff)), flags); |
| 131 | if (x > expected_width) expected_width = x; |
| 132 | expected_height += y; |
| 133 | } |
| 134 | if (std::ftell(fp) != datastart) { |
| 135 | std::fprintf(stderr, "Warning: garbage after header (pos=0x%lx-datastart=0x%lx) = %ld bytes of garbage! \n", (unsigned long)std::ftell(fp), (unsigned long)datastart, (long)(std::ftell(fp) - datastart)); |
| 136 | } |
| 137 | tgaimg = load_tgaheader(); |
| 138 | print_tga_info(tgaimg, stdout); |
| 139 | std::fprintf(stderr, "\n"); |
| 140 | std::fprintf(stderr, "Check: Expected %dx%d TGA, got %ux%u. %s\n", expected_width, expected_height, tgaimg->width, tgaimg->height, ((tgaimg->width == expected_width) && (tgaimg->height == expected_height)) ? "OK." : "FAIL."); |
| 141 | std::fprintf(stderr, "Check: Expected 24bit color depth TGA, got %dbit. %s\n", tgaimg->bpp, (tgaimg->bpp == 24) ? "OK." : "FAIL."); |
| 142 | std::fprintf(stderr, "Check: Expected ImageType 10, got %d. %s\n", tgaimg->imgtype, (tgaimg->imgtype == 10) ? "OK." : "FAIL."); |
| 143 | file_rpop(); |
| 144 | } |
| 145 | |
| 146 | if (bnifile != dash && std::fclose(fp) < 0) |
| 147 | std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno)); |
| 148 | return EXIT_SUCCESS; |
| 149 | } |