annotate tools/packed.c @ 2480:c7a0913e1032

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