must_free_canvas isn't used when AUTOMAP_DIRECT_RENDER is defined (whoops)
[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         fread(buf, 3, 1, hogfile);
48         printf("Extracting from: %s\n", argv[1]);
49         free(buf);
50         while(ftell(hogfile)<statbuf.st_size) {
51                 fread(filename, 13, 1, hogfile);
52                 fread(&len, 1, 4, hogfile);
53 #ifdef WORDS_BIGENDIAN
54                 len = SWAPINT(len);
55 #endif
56                 if (argc > 2 && strcmp(argv[2], filename))
57                         fseek(hogfile, len, SEEK_CUR);
58                 else {
59                         printf("Filename: %s \tLength: %i\n", filename, len);
60                         if (v)
61                                 fseek(hogfile, len, SEEK_CUR);
62                         else {
63                                 buf = (char *)malloc(len);
64                                 if (buf == NULL) {
65                                         printf("Unable to allocate memory\n");
66                                 } else {
67                                         fread(buf, len, 1, hogfile);
68                                         writefile = fopen(filename, "wb");
69                                         fwrite(buf, len, 1, writefile);
70                                         fclose(writefile);
71                                         free(buf);
72                                 }
73                         }
74                 }
75         }
76         fclose(hogfile);
77
78         return 0;
79 }