Mercurial > hg > dmlib
annotate tools/packed.c @ 2489:c64806412be0
Cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 28 Apr 2020 18:11:59 +0300 |
parents | c7a0913e1032 |
children | 9807ae37ad69 |
rev | line source |
---|---|
0 | 1 /* |
2 * PACKed - PACKfile EDitor | |
3 * Programmed and designed by Matti 'ccr' Hamalainen | |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
4 * (C) Copyright 2011, 2018 Tecnic Software productions (TNSP) |
0 | 5 */ |
1432
a9516570cc26
Improve build, so that we can build the tools and tests with minimal
Matti Hamalainen <ccr@tnsp.org>
parents:
1407
diff
changeset
|
6 #include "dmtool.h" |
0 | 7 #include "dmlib.h" |
8 #include "dmargs.h" | |
9 #include "dmpack.h" | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
10 #include "dmfile.h" |
115
23e03defa759
Prettify pack content listing output.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
11 #include "dmres.h" |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
12 #include <zlib.h> |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
13 |
0 | 14 |
15 #define SET_MAX_FILES (4096) | |
1977
33c29e7d1861
Increase the zlib compression buffer size to 1MB to improve the efficiency.
Matti Hamalainen <ccr@tnsp.org>
parents:
1742
diff
changeset
|
16 #define SET_TMPBUF_SIZE (1024 * 1024) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
17 |
0 | 18 enum |
19 { | |
20 CMD_NONE = 0, | |
21 CMD_CREATE, | |
22 CMD_ADD, | |
23 CMD_LIST, | |
24 CMD_EXTRACT | |
25 } DCOMMAND; | |
26 | |
27 enum | |
28 { | |
29 PACK_EXTRACTED = 0x0001, | |
30 }; | |
31 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
32 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
33 size_t nsrcFilenames = 0, nexcFilenames = 0; |
0 | 34 char * srcFilenames[SET_MAX_FILES]; |
752
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
35 char * excFilenames[SET_MAX_FILES]; |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
36 |
0 | 37 char * optPackFilename = NULL; |
1027
d29058e93799
Add option for setting the zlib compression level.
Matti Hamalainen <ccr@tnsp.org>
parents:
1026
diff
changeset
|
38 int optCompressLevel = Z_BEST_COMPRESSION; |
0 | 39 int optCommand = CMD_NONE; |
40 | |
41 | |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
42 static const DMOptArg cmdList[] = |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
43 { |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
44 { CMD_CREATE , 'c', "create", "Create and add files to PACK", OPT_NONE }, |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
45 { CMD_ADD , 'a', "add", "Add files to PACK", OPT_NONE }, |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
46 { CMD_LIST , 'l', "list", "List files in PACK", OPT_NONE }, |
2440
5b8cb850f5ab
Rename extract short from 'e' to 'x'.
Matti Hamalainen <ccr@tnsp.org>
parents:
2439
diff
changeset
|
47 { CMD_EXTRACT , 'x', "extract", "Extract files from PACK", OPT_NONE }, |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
48 }; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
49 |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
50 static const int cmdListN = sizeof(cmdList) / sizeof(cmdList[0]); |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
51 |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
52 |
860
daebbf28953d
The argument handling API in dmargs* was synced with th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents:
752
diff
changeset
|
53 static const DMOptArg optList[] = |
0 | 54 { |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
55 { 0, '?', "help" , "Show this help", OPT_NONE }, |
2410
bc05bcfc4598
Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents:
2408
diff
changeset
|
56 { 1, 0, "license" , "Print out this program's license agreement", OPT_NONE }, |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
57 { 2, 'v', "verbose" , "Be more verbose", OPT_NONE }, |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
58 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
59 { 4, 'C', "level" , "Set zlib compression level 0-9", OPT_ARGREQ }, |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
60 { 6, 'x', "exclude" , "Exclude name/glob pattern", OPT_ARGREQ }, |
0 | 61 }; |
62 | |
63 static const int optListN = sizeof(optList) / sizeof(optList[0]); | |
64 | |
65 | |
66 void argShowHelp() | |
67 { | |
2438 | 68 FILE *fh = stdout; |
69 | |
70 dmPrintBanner(fh, dmProgName, "<command> [options] <packfile> [<filename(s)>]"); | |
71 | |
72 fprintf(fh, "\n"); | |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
73 |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
74 for (int i = 0; i < cmdListN; i++) |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
75 { |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
76 const DMOptArg *cmd = &cmdList[i]; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
77 char tmpStr[128]; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
78 snprintf(tmpStr, sizeof(tmpStr), "%c / %s", |
1742
ddec147d1f90
Bring in changes from the th-libs version of commandline argument handling.
Matti Hamalainen <ccr@tnsp.org>
parents:
1642
diff
changeset
|
79 cmd->o_short, cmd->o_long); |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
80 |
2438 | 81 fprintf(fh, " %-15s %s\n", tmpStr, cmd->desc); |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
82 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
83 |
2438 | 84 fprintf(fh, "\n"); |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
85 |
2438 | 86 dmArgsPrintHelp(fh, optList, optListN, 0, 80 - 2); |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
87 |
2438 | 88 fprintf(fh, |
1406 | 89 "\n" |
90 "Examples:\n" | |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
91 "$ %s l test.pak -- list files in test.pak\n" |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
92 "$ %s c test.pak foobar.jpg -- create test.pak and add foobar.jpg in it\n" |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
93 "$ %s e test.pak foobar.jpg -- extract foobar.jpg from test.pak\n", |
1406 | 94 dmProgName, dmProgName, dmProgName); |
0 | 95 } |
96 | |
97 | |
98 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) | |
99 { | |
100 switch (optN) | |
101 { | |
211 | 102 case 0: |
103 argShowHelp(); | |
104 exit(0); | |
105 break; | |
0 | 106 |
2410
bc05bcfc4598
Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents:
2408
diff
changeset
|
107 case 1: |
bc05bcfc4598
Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents:
2408
diff
changeset
|
108 dmPrintLicense(stdout); |
bc05bcfc4598
Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents:
2408
diff
changeset
|
109 exit(0); |
bc05bcfc4598
Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents:
2408
diff
changeset
|
110 break; |
bc05bcfc4598
Add a C file with the generic BSD license text and a function for
Matti Hamalainen <ccr@tnsp.org>
parents:
2408
diff
changeset
|
111 |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
112 case 2: |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
113 dmVerbosity++; |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
114 break; |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
115 |
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
116 case 4: |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
117 optCompressLevel = atoi(optArg); |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
118 if (optCompressLevel < 0 || optCompressLevel > 9) |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
119 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
120 dmErrorMsg("Invalid compression level argument '%s', must be 0 .. 9.\n", optArg); |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
121 return FALSE; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
122 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
123 break; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
124 |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
125 case 6: |
752
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
126 if (nexcFilenames < SET_MAX_FILES) |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
127 { |
1267
ad13510c8e41
Fix handling of -x option in packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
1238
diff
changeset
|
128 excFilenames[nexcFilenames++] = optArg; |
752
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
129 } |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
130 else |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
131 { |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
952
diff
changeset
|
132 dmErrorMsg("Maximum number of exclusion patterns (%d) exceeded!\n", |
752
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
133 SET_MAX_FILES); |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
134 return FALSE; |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
135 } |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
136 break; |
0 | 137 |
211 | 138 default: |
2183
e3f0eaf23f4f
Change the error message for unimplemented option argument.
Matti Hamalainen <ccr@tnsp.org>
parents:
2105
diff
changeset
|
139 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg); |
211 | 140 return FALSE; |
0 | 141 } |
142 | |
143 return TRUE; | |
144 } | |
145 | |
146 | |
2443 | 147 BOOL argHandleNonOpt(char *arg) |
0 | 148 { |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
149 if (optCommand == CMD_NONE) |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
150 { |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
151 for (int n = 0; n < cmdListN; n++) |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
152 { |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
153 const DMOptArg *cmd = &cmdList[n]; |
2443 | 154 if ((arg[0] == cmd->o_short && arg[1] == 0) || |
155 (arg[0] == cmd->o_long[0] && arg[1] == 0) || | |
156 strcmp(arg, cmd->o_long) == 0) | |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
157 { |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
158 optCommand = cmd->id; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
159 break; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
160 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
161 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
162 if (optCommand == CMD_NONE) |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
163 { |
2443 | 164 dmErrorMsg("Invalid command '%s'.\n", arg); |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
165 return FALSE; |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
166 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
167 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
168 else |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
169 if (optPackFilename == NULL) |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
170 { |
2443 | 171 optPackFilename = arg; |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
172 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
173 else |
0 | 174 if (nsrcFilenames < SET_MAX_FILES) |
175 { | |
2443 | 176 srcFilenames[nsrcFilenames++] = arg; |
0 | 177 } |
178 else | |
179 { | |
958
985225a93aeb
Add error code parameter to dmError() and dmErrorVA().
Matti Hamalainen <ccr@tnsp.org>
parents:
952
diff
changeset
|
180 dmErrorMsg("Maximum number of input files (%d) exceeded!\n", |
0 | 181 SET_MAX_FILES); |
182 return FALSE; | |
183 } | |
184 return TRUE; | |
185 } | |
186 | |
187 | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
188 DMPackEntry *dmPackEntryCopy(const DMPackEntry *src) |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
189 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
190 DMPackEntry *entry = dmPackEntryNew(); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
191 if (entry == NULL) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
192 return NULL; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
193 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
194 strncpy(entry->filename, src->filename, DMRES_NAME_LEN); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
195 entry->filename[DMRES_NAME_LEN] = 0; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
196 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
197 entry->size = src->size; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
198 entry->offset = src->offset; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
199 entry->csize = src->csize; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
200 entry->flags = src->flags; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
201 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
202 return entry; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
203 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
204 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
205 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
206 int dmPackWrite(DMPackFile * pack) |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
207 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
208 DMPackEntry *entry; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
209 DMPackFileHeader hdr; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
210 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
211 if (pack == NULL) |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
212 return DMERR_OK; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
213 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
214 if (pack->file == NULL) |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
215 return DMERR_FOPEN; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
216 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
217 // Compute directory offset and number of entries |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
218 memcpy(&hdr.ident, DPACK_IDENT, sizeof(hdr.ident)); |
2321 | 219 hdr.version = DPACK_VERSION; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
220 hdr.dirEntries = 0; |
2321 | 221 hdr.dirOffset = |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
222 sizeof(hdr.ident) + sizeof(hdr.version) + |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
223 sizeof(hdr.dirEntries) + sizeof(hdr.dirOffset); |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
224 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
225 for (entry = pack->entries; entry != NULL; entry = entry->next) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
226 { |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
227 hdr.dirEntries++; |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
228 hdr.dirOffset += entry->csize; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
229 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
230 |
2391
d1992206765a
Fix some format specifiers, was erraneously using DM_PRI*_SIZE_T instead of DM_PRI*64.
Matti Hamalainen <ccr@tnsp.org>
parents:
2384
diff
changeset
|
231 dmMsg(1, "%d entries in PACK, dir at offset 0x%08" DM_PRIx64 ".\n", |
1128
0194c8a26aa8
Add informative message to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
1120
diff
changeset
|
232 hdr.dirEntries, hdr.dirOffset); |
0194c8a26aa8
Add informative message to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
1120
diff
changeset
|
233 |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
234 // Write PACK header |
1108 | 235 if (fseeko(pack->file, 0, SEEK_SET) != 0) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
236 return DMERR_FSEEK; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
237 |
2321 | 238 if (!dm_fwrite_str(pack->file, (Uint8 *) &hdr.ident, sizeof(hdr.ident)) || |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
239 !dm_fwrite_le16(pack->file, hdr.version) || |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
240 !dm_fwrite_le32(pack->file, hdr.dirEntries) || |
1037
d674ddc0fc82
Change PACK file format to use 64 bit fileoffsets. Also switch some
Matti Hamalainen <ccr@tnsp.org>
parents:
1036
diff
changeset
|
241 !dm_fwrite_le64(pack->file, hdr.dirOffset)) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
242 return DMERR_FWRITE; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
243 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
244 // Write the directory |
1108 | 245 if (fseeko(pack->file, hdr.dirOffset, SEEK_SET) != 0) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
246 return DMERR_FSEEK; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
247 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
248 for (entry = pack->entries; entry != NULL; entry = entry->next) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
249 { |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
250 // Write one entry |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
251 if (!dm_fwrite_str(pack->file, entry->filename, DMRES_NAME_LEN) || |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
252 !dm_fwrite_le64(pack->file, entry->offset) || |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
253 !dm_fwrite_le32(pack->file, entry->csize) || |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
254 !dm_fwrite_le32(pack->file, entry->size) || |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
255 !dm_fwrite_le32(pack->file, entry->flags)) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
256 return DMERR_FWRITE; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
257 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
258 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
259 return DMERR_OK; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
260 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
261 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
262 |
2489 | 263 int dmPackCreate(const char *filename, DMPackFile ** ppack) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
264 { |
2489 | 265 DMPackFile *pack; |
266 | |
267 if ((pack = *ppack = (DMPackFile *) dmMalloc0(sizeof(DMPackFile))) == NULL) | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
268 return DMERR_MALLOC; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
269 |
2489 | 270 if ((pack->file = fopen(filename, "wb")) == NULL) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
271 { |
2489 | 272 dmFree(pack); |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
273 return DMERR_FOPEN; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
274 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
275 |
2489 | 276 pack->filename = dm_strdup(filename); |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
277 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
278 return DMERR_OK; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
279 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
280 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
281 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
282 int dmPackAddFile(DMPackFile * pack, const char *filename, |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
283 const int level, DMPackEntry ** ppEntry) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
284 { |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
285 Uint64 startOffs, outSize; |
994 | 286 Uint8 *inBuffer = NULL, *outBuffer = NULL; |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
287 DMPackEntry rentry, *pentry; |
1031
f7863608d84c
Cleanup, remove debug code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1029
diff
changeset
|
288 FILE *inFile = NULL; |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
289 int ret = DMERR_OK; |
994 | 290 BOOL zinit = FALSE; |
1031
f7863608d84c
Cleanup, remove debug code.
Matti Hamalainen <ccr@tnsp.org>
parents:
1029
diff
changeset
|
291 z_stream zstr; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
292 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
293 *ppEntry = NULL; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
294 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
295 if (pack == NULL) |
994 | 296 return DMERR_NULLPTR; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
297 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
298 if (pack->file == NULL) |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
299 return DMERR_FOPEN; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
300 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
301 // Compute starting offset |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
302 outSize = 0; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
303 startOffs = sizeof(DMPackFileHeader); |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
304 for (pentry = pack->entries; pentry != NULL; pentry = pentry->next) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
305 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
306 startOffs += pentry->csize; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
307 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
308 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
309 // Seek to the position |
1108 | 310 if (fseeko(pack->file, startOffs, SEEK_SET) != 0) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
311 return DMERR_INVALID; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
312 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
313 // Read file data |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
314 if ((inFile = fopen(filename, "rb")) == NULL) |
994 | 315 return DMERR_FOPEN; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
316 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
317 // Allocate temporary buffer |
996 | 318 if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL || |
319 (outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL) | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
320 { |
994 | 321 ret = DMERR_MALLOC; |
322 goto out; | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
323 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
324 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
325 // Read (and possibly compress) the data |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
326 if (level > 0) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
327 { |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
328 int zret; |
2439 | 329 |
330 // Initialize compression streams | |
2414
69a5af2eb1ea
Remove useless dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents:
2410
diff
changeset
|
331 memset(&zstr, 0, sizeof(zstr)); |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
332 if (deflateInit(&zstr, level) != Z_OK) |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
333 { |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
334 ret = DMERR_COMPRESSION; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
335 goto out; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
336 } |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
337 zinit = TRUE; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
338 |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
339 zret = Z_OK; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
340 while (!feof(inFile) && zret == Z_OK) |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
341 { |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
342 zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile); |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
343 |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
344 zstr.next_in = inBuffer; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
345 zstr.next_out = outBuffer; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
346 zstr.avail_out = SET_TMPBUF_SIZE; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
347 zstr.total_out = 0; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
348 zret = deflate(&zstr, Z_FULL_FLUSH); |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
349 |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
350 if (zret == Z_OK && zstr.total_out > 0) |
994 | 351 { |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
352 outSize += zstr.total_out; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
353 if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file) != zstr.total_out) |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
354 { |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
355 ret = DMERR_FWRITE; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
356 goto out; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
357 } |
994 | 358 } |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
359 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
360 } |
1032
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
361 else |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
362 { |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
363 while (!feof(inFile)) |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
364 { |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
365 size_t read = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile); |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
366 if (read > 0) |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
367 { |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
368 outSize += read; |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
369 if (fwrite(inBuffer, sizeof(Uint8), read, pack->file) != read) |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
370 { |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
371 ret = DMERR_FWRITE; |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
372 goto out; |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
373 } |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
374 } |
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
375 } |
1039
54970cd5acf0
Remember to set the "compressed" size field for raw nodes too, as we check
Matti Hamalainen <ccr@tnsp.org>
parents:
1037
diff
changeset
|
376 zstr.total_in = outSize; |
1032
f8d75a51de2c
Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents:
1031
diff
changeset
|
377 } |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
378 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
379 // Create directory entry |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
380 strncpy(rentry.filename, filename, DMRES_NAME_LEN); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
381 rentry.filename[DMRES_NAME_LEN] = 0; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
382 rentry.offset = startOffs; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
383 rentry.size = zstr.total_in; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
384 rentry.csize = outSize; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
385 rentry.flags = level > 0 ? DMF_COMPRESSED : 0; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
386 |
994 | 387 // Add directory entry |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
388 if ((*ppEntry = dmPackEntryCopy(&rentry)) == NULL) |
994 | 389 { |
390 ret = DMERR_MALLOC; | |
391 goto out; | |
392 } | |
393 | |
394 dmPackEntryInsert(&pack->entries, *ppEntry); | |
395 | |
396 out: | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
397 // Cleanup |
994 | 398 if (zinit) |
399 deflateEnd(&zstr); | |
400 | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
401 dmFree(inBuffer); |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
402 dmFree(outBuffer); |
994 | 403 if (inFile != NULL) |
404 fclose(inFile); | |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
405 |
994 | 406 return ret; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
407 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
408 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
409 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
410 int dmPackExtractFile(DMPackFile *pack, DMPackEntry * entry, const BOOL decompress) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
411 { |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
412 z_stream zstr; |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
413 FILE *outFile = NULL; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
414 Uint8 *inBuffer = NULL, *outBuffer = NULL; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
415 size_t remaining; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
416 int zret, ret = DMERR_OK; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
417 BOOL zinit = FALSE; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
418 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
419 if (pack == NULL) |
1018 | 420 return DMERR_NULLPTR; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
421 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
422 if (pack->file == NULL) |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
423 return DMERR_FOPEN; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
424 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
425 // Seek to the position |
1108 | 426 if (fseeko(pack->file, entry->offset, SEEK_SET) != 0) |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
427 return DMERR_FSEEK; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
428 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
429 // Open destination file |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
430 if ((outFile = fopen(entry->filename, "wb")) == NULL) |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
431 return DMERR_FOPEN; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
432 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
433 // Allocate temporary buffer |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
434 if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL || |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
435 (outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
436 { |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
437 ret = DMERR_MALLOC; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
438 goto out; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
439 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
440 |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
441 // Read and uncompress the data, if needed |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
442 if (decompress && (entry->flags & DMF_COMPRESSED)) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
443 { |
2439 | 444 // Initialize compression streams |
2414
69a5af2eb1ea
Remove useless dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents:
2410
diff
changeset
|
445 memset(&zstr, 0, sizeof(zstr)); |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
446 if (inflateInit(&zstr) != Z_OK) |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
447 { |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
448 ret = DMERR_COMPRESSION; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
449 goto out; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
450 } |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
451 zinit = TRUE; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
452 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
453 |
2479
c1cae47cd410
Rename DMPackEntry::length to csize (compressed size).
Matti Hamalainen <ccr@tnsp.org>
parents:
2443
diff
changeset
|
454 remaining = entry->csize; |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
455 zret = Z_OK; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
456 while (remaining > 0 && zret == Z_OK) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
457 { |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
458 size_t needed = remaining > SET_TMPBUF_SIZE ? SET_TMPBUF_SIZE : remaining; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
459 zstr.avail_in = fread(inBuffer, sizeof(Uint8), needed, pack->file); |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
460 if (zstr.avail_in < needed) |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
461 { |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
462 ret = DMERR_FREAD; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
463 goto out; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
464 } |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
465 |
1018 | 466 remaining -= zstr.avail_in; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
467 zstr.next_in = inBuffer; |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
468 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
469 if (zinit) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
470 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
471 while (zstr.avail_in > 0 && zret == Z_OK) |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
472 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
473 zstr.next_out = outBuffer; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
474 zstr.avail_out = SET_TMPBUF_SIZE; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
475 zstr.total_out = 0; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
476 zret = inflate(&zstr, Z_FULL_FLUSH); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
477 if (zstr.total_out > 0 && |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
478 fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
479 { |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
480 ret = DMERR_FWRITE; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
481 goto out; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
482 } |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
483 } |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
484 } |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
485 else |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
486 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
487 if (fwrite(inBuffer, sizeof(Uint8), zstr.avail_in, outFile) != zstr.avail_in) |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
488 { |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
489 ret = DMERR_FWRITE; |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
490 goto out; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
491 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
492 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
493 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
494 |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
495 out: |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
496 // Cleanup |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
497 if (zinit) |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
498 inflateEnd(&zstr); |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
499 |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
500 dmFree(inBuffer); |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
501 dmFree(outBuffer); |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
502 |
1026
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
503 if (outFile != NULL) |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
504 fclose(outFile); |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
505 |
16e63811d0c2
Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents:
1025
diff
changeset
|
506 return ret; |
952
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
507 } |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
508 |
ffdae9cd81de
Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents:
871
diff
changeset
|
509 |
0 | 510 /* Compare a string to a pattern. Case-SENSITIVE version. |
511 * The matching pattern can consist of any normal characters plus | |
512 * wildcards ? and *. "?" matches any character and "*" matches | |
513 * any number of characters. | |
514 */ | |
1407
4bca92dd7280
Rename dm_strmatch() to dmStrMatch().
Matti Hamalainen <ccr@tnsp.org>
parents:
1406
diff
changeset
|
515 BOOL dmStrMatch(const char *str, const char *pattern) |
0 | 516 { |
1019 | 517 BOOL matched = TRUE, any = FALSE, end = FALSE; |
518 const char *tmp = NULL; | |
0 | 519 |
520 // Check given pattern and string | |
521 if (str == NULL || pattern == NULL) | |
522 return FALSE; | |
523 | |
524 // Start comparision | |
525 do { | |
1019 | 526 matched = FALSE; |
0 | 527 switch (*pattern) |
528 { | |
529 case '?': | |
530 // Any single character matches | |
531 if (*str) | |
532 { | |
1019 | 533 matched = TRUE; |
0 | 534 pattern++; |
535 str++; | |
536 } | |
537 break; | |
538 | |
539 case '*': | |
1019 | 540 matched = TRUE; |
0 | 541 pattern++; |
542 if (!*pattern) | |
1019 | 543 end = TRUE; |
544 any = TRUE; | |
545 tmp = pattern; | |
0 | 546 break; |
547 | |
548 case 0: | |
1019 | 549 if (any) |
0 | 550 { |
551 if (*str) | |
552 str++; | |
553 else | |
1019 | 554 end = TRUE; |
0 | 555 } |
556 else | |
557 { | |
558 if (*str) | |
559 { | |
1019 | 560 if (tmp) |
0 | 561 { |
1019 | 562 any = TRUE; |
563 pattern = tmp; | |
0 | 564 } |
565 else | |
1019 | 566 matched = FALSE; |
0 | 567 } |
568 else | |
1019 | 569 end = TRUE; |
0 | 570 } |
571 break; | |
572 default: | |
1019 | 573 if (any) |
0 | 574 { |
575 if (*pattern == *str) | |
576 { | |
1019 | 577 any = FALSE; |
578 matched = TRUE; | |
0 | 579 } |
580 else | |
581 { | |
582 if (*str) | |
583 { | |
1019 | 584 matched = TRUE; |
0 | 585 str++; |
586 } | |
587 } | |
588 } | |
589 else | |
590 { | |
591 if (*pattern == *str) | |
592 { | |
1019 | 593 matched = TRUE; |
0 | 594 if (*pattern) |
595 pattern++; | |
596 if (*str) | |
597 str++; | |
598 } | |
599 else | |
600 { | |
1019 | 601 if (tmp) |
0 | 602 { |
1019 | 603 matched = TRUE; |
604 any = TRUE; | |
605 pattern = tmp; | |
0 | 606 } |
607 } | |
608 } | |
609 | |
610 if (!*str && !*pattern) | |
1019 | 611 end = TRUE; |
0 | 612 break; |
613 | |
614 } // switch | |
615 | |
1019 | 616 } while (matched && !end); |
0 | 617 |
1019 | 618 return matched; |
0 | 619 } |
620 | |
621 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
622 BOOL dmMatchList(char **list, const size_t nlist, const char *str) |
752
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
623 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
624 for (size_t i = 0; i < nlist; i++) |
752
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
625 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
626 if (dmStrMatch(str, list[i])) |
752
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
627 return TRUE; |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
628 } |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
629 return FALSE; |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
630 } |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
631 |
2b9dd22f01e9
Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents:
721
diff
changeset
|
632 |
0 | 633 int main(int argc, char *argv[]) |
634 { | |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
635 int res = 0; |
0 | 636 DMPackFile *pack = NULL; |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
637 DMPackEntry *entry = NULL; |
0 | 638 |
639 // Parse arguments | |
2408
60e119262c67
Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents:
2402
diff
changeset
|
640 dmInitProg("packed", "TNSP Pack File Editor", "0.7", NULL, NULL); |
1011
aa745d31612e
Set default verbosity of 'packed' to none.
Matti Hamalainen <ccr@tnsp.org>
parents:
1008
diff
changeset
|
641 dmVerbosity = 0; |
1238 | 642 |
0 | 643 if (!dmArgsProcess(argc, argv, optList, optListN, |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
644 argHandleOpt, argHandleNonOpt, OPTH_BAILOUT)) |
0 | 645 exit(1); |
646 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
647 if (optCommand == CMD_NONE || argc < 2) |
0 | 648 { |
2441
275374725598
Show help when no arguments.
Matti Hamalainen <ccr@tnsp.org>
parents:
2440
diff
changeset
|
649 argShowHelp(); |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
650 goto out; |
0 | 651 } |
1238 | 652 |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
653 if (optPackFilename == NULL) |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
654 { |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
655 dmErrorMsg("No PACK file specified.\n"); |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
656 goto out; |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
657 } |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
658 |
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
659 dmMsg(1, "Processing PACK '%s' ...\n", optPackFilename); |
0 | 660 |
661 // Execute command | |
662 switch (optCommand) | |
663 { | |
664 case CMD_CREATE: | |
665 case CMD_ADD: | |
666 switch (optCommand) | |
667 { | |
668 case CMD_CREATE: | |
669 dmMsg(1, "Creating new PACK\n"); | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
670 res = dmPackCreate(optPackFilename, &pack); |
0 | 671 break; |
672 | |
673 case CMD_ADD: | |
674 dmMsg(1, "Opening existing PACK\n"); | |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
675 res = dmPackOpen(optPackFilename, &pack, FALSE); |
0 | 676 break; |
677 } | |
678 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
679 if (res != DMERR_OK) |
0 | 680 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
681 dmErrorMsg("Could not open PACK file: %s\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
682 dmErrorStr(res)); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
683 goto out; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
684 } |
0 | 685 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
686 // Add files into PACK |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
687 dmMsg(1, "Adding files...\n"); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
688 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
689 for (size_t i = 0; i < nsrcFilenames; i++) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
690 if (!dmMatchList(excFilenames, nexcFilenames, srcFilenames[i])) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
691 { |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
692 res = dmPackAddFile(pack, srcFilenames[i], optCompressLevel, &entry); |
721
bb14d7907eb2
Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
652
diff
changeset
|
693 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
694 if (res != DMERR_OK) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
695 { |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
696 dmPrint(1, "%-32s [ERROR:%d]\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
697 srcFilenames[i], res); |
0 | 698 } |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
699 else |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
700 { |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
701 dmPrint(1, "%-32s ['%s', s=%d, c=%d, o=%" DM_PRId64 ", f=0x%04x]\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
702 srcFilenames[i], entry->filename, |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
703 entry->size, entry->csize, entry->offset, |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
704 entry->flags); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
705 } |
0 | 706 } |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
707 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
708 dmMsg(2, "w=%d\n", dmPackWrite(pack)); |
0 | 709 break; |
710 | |
711 case CMD_LIST: | |
712 // List files in PACK | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
713 if ((res = dmPackOpen(optPackFilename, &pack, TRUE)) != DMERR_OK) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
714 { |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
715 dmErrorMsg("Could not open PACK file: %s\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
716 dmErrorStr(res)); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
717 goto out; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
718 } |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
719 else |
0 | 720 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
721 size_t ntotal = 0, nshown = 0, |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
722 totalSize = 0, totalCSize = 0; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
723 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
724 // Print the list header |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
725 int hlen = printf( |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
726 "%-32s | %10s | %10s | %10s | %s\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
727 "Name", "Size", "CSize", "Offset", "Flags"); |
1642
8fb4e8c825d6
Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents:
1432
diff
changeset
|
728 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
729 for (int n = 0; n < hlen; n++) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
730 fputc('-', stdout); |
0 | 731 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
732 fputs("\n", stdout); |
115
23e03defa759
Prettify pack content listing output.
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
733 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
734 // Print all the matching entries |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
735 for (entry = pack->entries; entry != NULL; entry = entry->next) |
0 | 736 { |
737 BOOL match; | |
1238 | 738 |
0 | 739 if (nsrcFilenames > 0) |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
740 // Check for matches in specified filenames/patterns |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
741 match = dmMatchList(srcFilenames, nsrcFilenames, entry->filename); |
0 | 742 else |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
743 // No filenames specified, everything shown |
0 | 744 match = TRUE; |
745 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
746 if (match && !dmMatchList(excFilenames, nexcFilenames, entry->filename)) |
0 | 747 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
748 printf("%-32s | %10d | %10d | %010" DM_PRIx64 " | %04x\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
749 entry->filename, entry->size, entry->csize, |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
750 entry->offset, entry->flags); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
751 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
752 nshown++; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
753 totalSize += entry->size; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
754 totalCSize += entry->csize; |
0 | 755 } |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
756 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
757 ntotal++; |
0 | 758 } |
759 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
760 for (int n = 0; n < hlen; n++) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
761 fputc('-', stdout); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
762 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
763 printf( |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
764 "\n" |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
765 "%-32s | %10" DM_PRIu_SIZE_T " | %10" DM_PRIu_SIZE_T " |\n" |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
766 "Listed %" DM_PRIu_SIZE_T " of %" DM_PRIu_SIZE_T " files total.\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
767 "Totals", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
768 totalSize, totalCSize, |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
769 nshown, ntotal); |
0 | 770 } |
771 break; | |
772 | |
773 case CMD_EXTRACT: | |
774 // Extract files from PACK | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
775 if ((res = dmPackOpen(optPackFilename, &pack, TRUE)) != DMERR_OK) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
776 { |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
777 dmErrorMsg("Could not open PACK file: %s\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
778 dmErrorStr(res)); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
779 goto out; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
780 } |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
781 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
782 for (entry = pack->entries; entry != NULL; entry = entry->next) |
0 | 783 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
784 BOOL match; |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
785 |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
786 // Check for matches |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
787 if (nsrcFilenames > 0) |
0 | 788 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
789 match = !dmMatchList(excFilenames, nexcFilenames, entry->filename) && |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
790 dmMatchList(srcFilenames, nsrcFilenames, entry->filename); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
791 } |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
792 else |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
793 { |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
794 match = !dmMatchList(excFilenames, nexcFilenames, entry->filename); |
0 | 795 } |
796 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
797 if (match && (entry->privFlags & PACK_EXTRACTED) == 0) |
0 | 798 { |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
799 // Print one entry |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
800 dmPrint(0, "Extracting: %-32s [siz=%d, cmp=%d, offs=0x%08" DM_PRIx64 ", flags=0x%04x]\n", |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
801 entry->filename, entry->size, entry->csize, |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
802 entry->offset, entry->flags); |
0 | 803 |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
804 if ((res = dmPackExtractFile(pack, entry, optCompressLevel > 0)) == DMERR_OK) |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
805 entry->privFlags |= PACK_EXTRACTED; |
0 | 806 } |
807 } | |
808 break; | |
809 } | |
810 | |
2480
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
811 out: |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
812 dmPackClose(pack); |
c7a0913e1032
Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents:
2479
diff
changeset
|
813 |
0 | 814 return 0; |
815 } |