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