Ticket #8361: unpackkyra.2.cpp

File unpackkyra.2.cpp, 3.7 KB (added by lordhoto, 20 years ago)

patch for argument parsing

Line 
1#include "unpackkyra.h"
2
3int main(int argc, char **argv) {
4 if (argc < 2) {
5 printf("Use:\n"
6 "%s [OPTIONS] filename\n"
7 "Here are the options, default is listing files to stdout\n"
8 "-o xxx Extract only file 'xxx'\n"
9 "-x Extract all files\n",
10 argv[0]);
11 return false;
12 }
13
14 bool extractAll = false, extractOne = false;
15 uint8 param = 0;
16
17 // looking for the parameters
18 for (int32 pos = 1; pos < argc - 1; ++pos) {
19 if (*argv[pos] == '-') {
20 if (argv[pos][1] == 'o') {
21 extractOne = true;
22 param = pos + 1;
23
24 if (param >= argc) {
25 printf("you have to add a filename to option -o\n"
26 "like: unpackkyra A_E.PAK -o ALGAE.CPS\n");
27 return false;
28 }
29
30 ++pos;
31 } else if (argv[pos][1] == 'x') {
32 extractAll = true;
33 }
34 }
35 }
36
37 PAKFile myfile(argv[agrc - 1]);
38
39 if(extractAll) {
40 myfile.outputAllFiles();
41 } else if(extractOne) {
42 myfile.outputFile(argv[param]);
43 } else {
44 myfile.drawFilelist();
45 }
46
47 return true;
48}
49
50PAKFile::PAKFile(const char* file) {
51 FILE* pakfile = fopen(file, "r");
52
53 if (!pakfile) {
54 error("couldn't open file '%s'", file);
55 }
56
57 _open = true;
58
59 _buffer = new uint8[fileSize(pakfile)];
60 assert(_buffer);
61
62 _filesize = fileSize(pakfile);
63 fread(_buffer, fileSize(pakfile), 1, pakfile);
64
65 fclose(pakfile);
66}
67
68void PAKFile::drawFilelist(void) {
69 const char* currentName = 0;
70
71 uint32 startoffset = *(uint32*)_buffer;
72 uint32 endoffset = 0;
73 uint8* position = _buffer + 4;
74
75 for (;;) {
76 uint32 strlgt = strlen((const char*)position);
77 currentName = (const char*)position;
78
79 if (!(*currentName))
80 break;
81
82 position += strlgt + 1;
83 // scip offset
84 endoffset = *(uint32*)position;
85 if (endoffset > _filesize) {
86 endoffset = _filesize;
87 } else if (endoffset == 0) {
88 endoffset = _filesize;
89 }
90 position += 4;
91
92 printf("Filename: %s size: %d\n", currentName, endoffset - startoffset);
93
94 if (endoffset == _filesize) {
95 break;
96 }
97
98 startoffset = endoffset;
99 }
100}
101
102void PAKFile::outputFile(const char* file) {
103 const char* currentName = 0;
104
105 uint32 startoffset = *(uint32*)_buffer;
106 uint32 endoffset = 0;
107 uint8* position = _buffer + 4;
108
109 for (;;) {
110 uint32 strlgt = strlen((const char*)position);
111 currentName = (const char*)position;
112
113 if (!(*currentName))
114 break;
115
116 position += strlgt + 1;
117 // scip offset
118 endoffset = *(uint32*)position;
119 if (endoffset > _filesize) {
120 endoffset = _filesize;
121 } else if (endoffset == 0) {
122 endoffset = _filesize;
123 }
124 position += 4;
125
126 if (!strcmp(currentName, file)) {
127 FILE* output = fopen(file, "wb+");
128 fwrite(_buffer + startoffset, endoffset - startoffset, 1,output);
129 fclose(output);
130 return;
131 }
132
133 if (endoffset == _filesize) {
134 break;
135 }
136
137 startoffset = endoffset;
138 }
139
140 printf("File '%s' not found in this pakfile", file);
141}
142
143void PAKFile::outputAllFiles(void) {
144 const char* currentName = 0;
145
146 uint32 startoffset = *(uint32*)_buffer;
147 uint32 endoffset = 0;
148 uint8* position = _buffer + 4;
149
150 for (;;) {
151 uint32 strlgt = strlen((const char*)position);
152 currentName = (const char*)position;
153
154 if (!(*currentName))
155 break;
156
157 position += strlgt + 1;
158 // scip offset
159 endoffset = *(uint32*)position;
160 if (endoffset > _filesize) {
161 endoffset = _filesize;
162 } else if (endoffset == 0) {
163 endoffset = _filesize;
164 }
165 position += 4;
166
167 FILE* output = fopen(currentName, "wb+");
168 fwrite(_buffer + startoffset, endoffset - startoffset, 1,output);
169 fclose(output);
170
171 if (endoffset == _filesize) {
172 break;
173 }
174
175 startoffset = endoffset;
176 }
177}