| 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>] [--] [<TGA 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 * tgafile = 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 && !tgafile) |
| 62 | tgafile = argv[a]; |
| 63 | else if (std::strcmp(argv[a], "-") == 0 && !tgafile) |
| 64 | tgafile = dash; |
| 65 | else if (argv[a][0] != '-' && !tgafile) |
| 66 | tgafile = 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 (!tgafile) |
| 89 | tgafile = dash; |
| 90 | |
| 91 | if (tgafile == dash) |
| 92 | fp = stdin; |
| 93 | else |
| 94 | if (!(fp = std::fopen(tgafile, "r"))) |
| 95 | { |
| 96 | std::fprintf(stderr, "%s: could not open TGA file \"%s\" for reading (std::fopen: %s)\n", argv[0], tgafile, std::strerror(errno)); |
| 97 | return EXIT_FAILURE; |
| 98 | } |
| 99 | |
| 100 | { |
| 101 | t_tgaimg * tgaimg; |
| 102 | |
| 103 | file_rpush(fp); |
| 104 | if (!(tgaimg = load_tgaheader())) |
| 105 | { |
| 106 | std::fprintf(stderr, "%s: could not load TGA header\n", argv[0]); |
| 107 | if (tgafile != dash && std::fclose(fp) < 0) |
| 108 | std::fprintf(stderr, "%s: could not close file \"%s\" after reading (std::fclose: %s)\n", argv[0], tgafile, std::strerror(errno)); |
| 109 | return EXIT_FAILURE; |
| 110 | } |
| 111 | print_tga_info(tgaimg, stdout); |
| 112 | file_rpop(); |
| 113 | } |
| 114 | |
| 115 | if (tgafile != dash && std::fclose(fp) < 0) |
| 116 | std::fprintf(stderr, "%s: could not close file \"%s\" after reading (std::fclose: %s)\n", argv[0], tgafile, std::strerror(errno)); |
| 117 | return EXIT_SUCCESS; |
| 118 | } |