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