]> icculus.org git repositories - btb/d2x.git/blob - utilities/hogextract.c
use the orientation parameter of g3_draw_bitmap
[btb/d2x.git] / utilities / hogextract.c
1 /*
2  * Written 1999 Jan 29 by Josh Cogliati
3  * Modified by Bradley Bell, 2002, 2003
4  * This program is licensed under the terms of the GPL, version 2 or later
5  */
6
7 #ifdef HAVE_CONFIG_H
8 #include <conf.h>
9 #endif
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17
18 #define SWAPINT(x)   (((x)<<24) | (((uint)(x)) >> 24) | (((x) &0x0000ff00) << 8) | (((x) & 0x00ff0000) >> 8))
19
20 int
21 main(int argc, char *argv[])
22 {
23         FILE *hogfile, *writefile;
24         int len;
25         char filename[13];
26         char *buf;
27         struct stat statbuf;
28         int v = 0;
29
30         if (argc > 1 && !strcmp(argv[1], "v")) {
31                 v = 1;
32                 argc--;
33                 argv++;
34         }
35
36         if (argc < 2) {
37                 printf("Usage: hogextract [v] hogfile [filename]\n"
38                        "extracts all the files in hogfile into the current directory\n"
39                            "Options:\n"
40                            "  v    View files, don't extract\n");
41                 exit(0);
42         }
43         hogfile = fopen(argv[1], "rb");
44         stat(argv[1], &statbuf);
45         printf("%i\n", (int)statbuf.st_size);
46         buf = (char *)malloc(3);
47         if ( fread(buf, 3, 1, hogfile) != 1 ) {
48                 printf("Error reading %s\n", argv[1]);
49                 fclose(hogfile);
50                 exit(EXIT_FAILURE);
51         }
52         printf("Extracting from: %s\n", argv[1]);
53         free(buf);
54         while(ftell(hogfile)<statbuf.st_size) {
55                 if ( fread(filename, 13, 1, hogfile) != 1 ||
56                          fread(&len, 1, 4, hogfile) != 4 ) {
57                         printf("Error reading %s\n", argv[1]);
58                         fclose(hogfile);
59                         exit(EXIT_FAILURE);
60                 }
61 #ifdef WORDS_BIGENDIAN
62                 len = SWAPINT(len);
63 #endif
64                 if (argc > 2 && strcmp(argv[2], filename))
65                         fseek(hogfile, len, SEEK_CUR);
66                 else {
67                         printf("Filename: %s \tLength: %i\n", filename, len);
68                         if (v)
69                                 fseek(hogfile, len, SEEK_CUR);
70                         else {
71                                 buf = (char *)malloc(len);
72                                 if (buf == NULL) {
73                                         printf("Unable to allocate memory\n");
74                                 } else {
75                                         if ( fread(buf, len, 1, hogfile) != 1 ) {
76                                                 printf("Error reading %s\n", argv[1]);
77                                                 fclose(hogfile);
78                                                 exit(EXIT_FAILURE);
79                                         }
80                                         writefile = fopen(filename, "wb");
81                                         fwrite(buf, len, 1, writefile);
82                                         fclose(writefile);
83                                         free(buf);
84                                 }
85                         }
86                 }
87         }
88         fclose(hogfile);
89
90         return 0;
91 }