Debian Codegraph @

src/bniutils/bni2tga.cpp

raw symbols SCIP
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 "fileio.h"
26 #include "bni.h"
27 #include "common/setup_after.h"
28
29 #define BUFSIZE 1024
30
31 using namespace pvpgn::bni;
32
33 namespace
34 {
35
36 void usage(char const * progname)
37 {
38 std::fprintf(stderr,
39 "usage: %s [<options>] [--] [<BNI file> [<TGA file>]]\n"
40 " -h, --help, --usage show this information and exit\n"
41 " -v, --version print version number and exit\n", progname);
42
43 std::exit(EXIT_FAILURE);
44 }
45
46 }
47
48 extern int main(int argc, char * argv[])
49 {
50 char const * bnifile = NULL;
51 char const * tgafile = NULL;
52 std::FILE * fbni;
53 std::FILE * ftga;
54 int a;
55 int forcefile = 0;
56 char dash[] = "-"; /* unique address used as flag */
57
58 if (argc < 1 || !argv || !argv[0])
59 {
60 std::fprintf(stderr, "bad arguments\n");
61 return EXIT_FAILURE;
62 }
63
64 for (a = 1; a < argc; a++)
65 if (forcefile && !bnifile)
66 bnifile = argv[a];
67 else if (std::strcmp(argv[a], "-") == 0 && !bnifile)
68 bnifile = dash;
69 else if (argv[a][0] != '-' && !bnifile)
70 bnifile = argv[a];
71 else if (forcefile && !tgafile)
72 tgafile = argv[a];
73 else if (std::strcmp(argv[a], "-") == 0 && !tgafile)
74 tgafile = dash;
75 else if (argv[a][0] != '-' && !tgafile)
76 tgafile = argv[a];
77 else if (forcefile || argv[a][0] != '-' || std::strcmp(argv[a], "-") == 0)
78 {
79 std::fprintf(stderr, "%s: extra file argument \"%s\"\n", argv[0], argv[a]);
80 usage(argv[0]);
81 }
82 else if (std::strcmp(argv[a], "--") == 0)
83 forcefile = 1;
84 else if (std::strcmp(argv[a], "-v") == 0 || std::strcmp(argv[a], "--version") == 0)
85 {
86 std::printf("version " PVPGN_VERSION "\n");
87 return EXIT_SUCCESS;
88 }
89 else if (std::strcmp(argv[a], "-h") == 0 || std::strcmp(argv[a], "--help") == 0 || std::strcmp(argv[a], "--usage")
90 == 0)
91 usage(argv[0]);
92 else
93 {
94 std::fprintf(stderr, "%s: unknown option \"%s\"\n", argv[0], argv[a]);
95 usage(argv[0]);
96 }
97
98 if (!bnifile)
99 bnifile = dash;
100 if (!tgafile)
101 tgafile = dash;
102
103 if (bnifile == dash)
104 fbni = stdin;
105 else
106 if (!(fbni = std::fopen(bnifile, "r")))
107 {
108 std::fprintf(stderr, "%s: could not open BNI file \"%s\" for reading (std::fopen: %s)\n", argv[0], bnifile, std::strerror(errno));
109 std::exit(EXIT_FAILURE);
110 }
111 if (tgafile == dash)
112 ftga = stdout;
113 else
114 if (!(ftga = std::fopen(tgafile, "w")))
115 {
116 std::fprintf(stderr, "%s: could not open TGA file \"%s\" for reading (std::fopen: %s)\n", argv[0], tgafile, std::strerror(errno));
117 std::exit(EXIT_FAILURE);
118 }
119
120 {
121 unsigned char buf[BUFSIZE];
122 std::size_t rc;
123 t_bnifile bnih;
124
125 file_rpush(fbni);
126 bnih.unknown1 = file_readd_le();
127 bnih.unknown2 = file_readd_le();
128 bnih.numicons = file_readd_le();
129 bnih.dataoffset = file_readd_le();
130 std::fprintf(stderr, "Info: numicons=%d dataoffset=0x%08x(%d)\n", bnih.numicons, bnih.dataoffset, bnih.dataoffset);
131 if (std::fseek(fbni, bnih.dataoffset, SEEK_SET)<0)
132 {
133 std::fprintf(stderr, "%s: could not seek to offset %u in BNI file \"%s\" (std::fseek: %s)\n", argv[0], bnih.dataoffset, bnifile, std::strerror(errno));
134 return EXIT_FAILURE;
135 }
136 while ((rc = std::fread(buf, 1, sizeof(buf), fbni))>0) {
137 if (std::fwrite(buf, rc, 1, ftga) < 1) {
138 std::fprintf(stderr, "%s: could not write data to TGA file \"%s\" (std::fwrite: %s)\n", argv[0], tgafile, std::strerror(errno));
139 return EXIT_FAILURE;
140 }
141 }
142 file_rpop();
143 }
144
145 if (tgafile != dash && std::fclose(ftga) < 0)
146 std::fprintf(stderr, "%s: could not close TGA file \"%s\" after writing (std::fclose: %s)\n", argv[0], tgafile, std::strerror(errno));
147 if (bnifile != dash && std::fclose(fbni) < 0)
148 std::fprintf(stderr, "%s: could not close BNI file \"%s\" after reading (std::fclose: %s)\n", argv[0], bnifile, std::strerror(errno));
149
150 return EXIT_SUCCESS;
151 }