/[LeafOK_CVS]/pvpgn-1.7.4/src/bniutils/bni2tga.c
ViewVC logotype

Annotation of /pvpgn-1.7.4/src/bniutils/bni2tga.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
CVS Tags: pvpgn_1-7-4-0_MIL
Branch point for: GNU, MAIN
Content type: text/x-csrc
Initial revision

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

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1