annotate tools/packed.c @ 2489:c64806412be0

Cleanup.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 28 Apr 2020 18:11:59 +0300
parents c7a0913e1032
children 9807ae37ad69
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
2489
c64806412be0 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
263 int dmPackCreate(const char *filename, DMPackFile ** ppack)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
264 {
2489
c64806412be0 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
265 DMPackFile *pack;
c64806412be0 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
266
c64806412be0 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
267 if ((pack = *ppack = (DMPackFile *) dmMalloc0(sizeof(DMPackFile))) == NULL)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
268 return DMERR_MALLOC;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
269
2489
c64806412be0 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
270 if ((pack->file = fopen(filename, "wb")) == NULL)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
271 {
2489
c64806412be0 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
272 dmFree(pack);
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
273 return DMERR_FOPEN;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
274 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
275
2489
c64806412be0 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 2480
diff changeset
276 pack->filename = dm_strdup(filename);
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
277
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
278 return DMERR_OK;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
279 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
280
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
281
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
282 int dmPackAddFile(DMPackFile * pack, const char *filename,
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
283 const int level, DMPackEntry ** ppEntry)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
284 {
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
285 Uint64 startOffs, outSize;
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
286 Uint8 *inBuffer = NULL, *outBuffer = NULL;
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
287 DMPackEntry rentry, *pentry;
1031
f7863608d84c Cleanup, remove debug code.
Matti Hamalainen <ccr@tnsp.org>
parents: 1029
diff changeset
288 FILE *inFile = NULL;
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
289 int ret = DMERR_OK;
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
290 BOOL zinit = FALSE;
1031
f7863608d84c Cleanup, remove debug code.
Matti Hamalainen <ccr@tnsp.org>
parents: 1029
diff changeset
291 z_stream zstr;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
292
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
293 *ppEntry = NULL;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
294
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
295 if (pack == NULL)
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
296 return DMERR_NULLPTR;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
297
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
298 if (pack->file == NULL)
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
299 return DMERR_FOPEN;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
300
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
301 // Compute starting offset
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
302 outSize = 0;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
303 startOffs = sizeof(DMPackFileHeader);
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
304 for (pentry = pack->entries; pentry != NULL; pentry = pentry->next)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
305 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
306 startOffs += pentry->csize;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
307 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
308
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
309 // Seek to the position
1108
5a8d29b88431 More fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 1041
diff changeset
310 if (fseeko(pack->file, startOffs, SEEK_SET) != 0)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
311 return DMERR_INVALID;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
312
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
313 // Read file data
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
314 if ((inFile = fopen(filename, "rb")) == NULL)
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
315 return DMERR_FOPEN;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
316
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
317 // Allocate temporary buffer
996
acda8cfc9119 Also here.
Matti Hamalainen <ccr@tnsp.org>
parents: 994
diff changeset
318 if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL ||
acda8cfc9119 Also here.
Matti Hamalainen <ccr@tnsp.org>
parents: 994
diff changeset
319 (outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
320 {
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
321 ret = DMERR_MALLOC;
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
322 goto out;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
323 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
324
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
325 // Read (and possibly compress) the data
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
326 if (level > 0)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
327 {
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
328 int zret;
2439
f81b42dd600d Adjust few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 2438
diff changeset
329
f81b42dd600d Adjust few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 2438
diff changeset
330 // Initialize compression streams
2414
69a5af2eb1ea Remove useless dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
331 memset(&zstr, 0, sizeof(zstr));
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
332 if (deflateInit(&zstr, level) != Z_OK)
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
333 {
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
334 ret = DMERR_COMPRESSION;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
335 goto out;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
336 }
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
337 zinit = TRUE;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
338
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
339 zret = Z_OK;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
340 while (!feof(inFile) && zret == Z_OK)
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
341 {
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
342 zstr.avail_in = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile);
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
343
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
344 zstr.next_in = inBuffer;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
345 zstr.next_out = outBuffer;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
346 zstr.avail_out = SET_TMPBUF_SIZE;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
347 zstr.total_out = 0;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
348 zret = deflate(&zstr, Z_FULL_FLUSH);
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
349
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
350 if (zret == Z_OK && zstr.total_out > 0)
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
351 {
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
352 outSize += zstr.total_out;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
353 if (fwrite(outBuffer, sizeof(Uint8), zstr.total_out, pack->file) != zstr.total_out)
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
354 {
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
355 ret = DMERR_FWRITE;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
356 goto out;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
357 }
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
358 }
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
359 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
360 }
1032
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
361 else
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
362 {
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
363 while (!feof(inFile))
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
364 {
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
365 size_t read = fread(inBuffer, sizeof(Uint8), SET_TMPBUF_SIZE, inFile);
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
366 if (read > 0)
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
367 {
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
368 outSize += read;
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
369 if (fwrite(inBuffer, sizeof(Uint8), read, pack->file) != read)
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
370 {
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
371 ret = DMERR_FWRITE;
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
372 goto out;
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
373 }
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
374 }
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
375 }
1039
54970cd5acf0 Remember to set the "compressed" size field for raw nodes too, as we check
Matti Hamalainen <ccr@tnsp.org>
parents: 1037
diff changeset
376 zstr.total_in = outSize;
1032
f8d75a51de2c Implement raw uncompressed data copy.
Matti Hamalainen <ccr@tnsp.org>
parents: 1031
diff changeset
377 }
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
378
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
379 // Create directory entry
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
380 strncpy(rentry.filename, filename, DMRES_NAME_LEN);
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
381 rentry.filename[DMRES_NAME_LEN] = 0;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
382 rentry.offset = startOffs;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
383 rentry.size = zstr.total_in;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
384 rentry.csize = outSize;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
385 rentry.flags = level > 0 ? DMF_COMPRESSED : 0;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
386
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
387 // Add directory entry
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
388 if ((*ppEntry = dmPackEntryCopy(&rentry)) == NULL)
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
389 {
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
390 ret = DMERR_MALLOC;
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
391 goto out;
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
392 }
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
393
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
394 dmPackEntryInsert(&pack->entries, *ppEntry);
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 out:
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
397 // Cleanup
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
398 if (zinit)
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
399 deflateEnd(&zstr);
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
400
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
401 dmFree(inBuffer);
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
402 dmFree(outBuffer);
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
403 if (inFile != NULL)
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
404 fclose(inFile);
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
405
994
e8de4fbc03b6 Clean up packed a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 958
diff changeset
406 return ret;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
407 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
408
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
409
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
410 int dmPackExtractFile(DMPackFile *pack, DMPackEntry * entry, const BOOL decompress)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
411 {
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
412 z_stream zstr;
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
413 FILE *outFile = NULL;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
414 Uint8 *inBuffer = NULL, *outBuffer = NULL;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
415 size_t remaining;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
416 int zret, ret = DMERR_OK;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
417 BOOL zinit = FALSE;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
418
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
419 if (pack == NULL)
1018
782a2a3a3707 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 1011
diff changeset
420 return DMERR_NULLPTR;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
421
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
422 if (pack->file == NULL)
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
423 return DMERR_FOPEN;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
424
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
425 // Seek to the position
1108
5a8d29b88431 More fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 1041
diff changeset
426 if (fseeko(pack->file, entry->offset, SEEK_SET) != 0)
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
427 return DMERR_FSEEK;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
428
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
429 // Open destination file
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
430 if ((outFile = fopen(entry->filename, "wb")) == NULL)
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
431 return DMERR_FOPEN;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
432
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
433 // Allocate temporary buffer
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
434 if ((inBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL ||
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
435 (outBuffer = (Uint8 *) dmMalloc(SET_TMPBUF_SIZE)) == NULL)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
436 {
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
437 ret = DMERR_MALLOC;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
438 goto out;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
439 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
440
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
441 // Read and uncompress the data, if needed
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
442 if (decompress && (entry->flags & DMF_COMPRESSED))
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
443 {
2439
f81b42dd600d Adjust few comments.
Matti Hamalainen <ccr@tnsp.org>
parents: 2438
diff changeset
444 // Initialize compression streams
2414
69a5af2eb1ea Remove useless dmMemset().
Matti Hamalainen <ccr@tnsp.org>
parents: 2410
diff changeset
445 memset(&zstr, 0, sizeof(zstr));
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
446 if (inflateInit(&zstr) != Z_OK)
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
447 {
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
448 ret = DMERR_COMPRESSION;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
449 goto out;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
450 }
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
451 zinit = TRUE;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
452 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
453
2479
c1cae47cd410 Rename DMPackEntry::length to csize (compressed size).
Matti Hamalainen <ccr@tnsp.org>
parents: 2443
diff changeset
454 remaining = entry->csize;
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
455 zret = Z_OK;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
456 while (remaining > 0 && zret == Z_OK)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
457 {
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
458 size_t needed = remaining > SET_TMPBUF_SIZE ? SET_TMPBUF_SIZE : remaining;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
459 zstr.avail_in = fread(inBuffer, sizeof(Uint8), needed, pack->file);
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
460 if (zstr.avail_in < needed)
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
461 {
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
462 ret = DMERR_FREAD;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
463 goto out;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
464 }
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
465
1018
782a2a3a3707 Cosmetic.
Matti Hamalainen <ccr@tnsp.org>
parents: 1011
diff changeset
466 remaining -= zstr.avail_in;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
467 zstr.next_in = inBuffer;
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
468
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
469 if (zinit)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
470 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
471 while (zstr.avail_in > 0 && zret == Z_OK)
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
472 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
473 zstr.next_out = outBuffer;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
474 zstr.avail_out = SET_TMPBUF_SIZE;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
475 zstr.total_out = 0;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
476 zret = inflate(&zstr, Z_FULL_FLUSH);
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
477 if (zstr.total_out > 0 &&
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
478 fwrite(outBuffer, sizeof(Uint8), zstr.total_out, outFile) != zstr.total_out)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
479 {
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
480 ret = DMERR_FWRITE;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
481 goto out;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
482 }
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
483 }
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
484 }
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
485 else
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
486 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
487 if (fwrite(inBuffer, sizeof(Uint8), zstr.avail_in, outFile) != zstr.avail_in)
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
488 {
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
489 ret = DMERR_FWRITE;
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
490 goto out;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
491 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
492 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
493 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
494
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
495 out:
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
496 // Cleanup
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
497 if (zinit)
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
498 inflateEnd(&zstr);
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
499
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
500 dmFree(inBuffer);
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
501 dmFree(outBuffer);
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
502
1026
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
503 if (outFile != NULL)
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
504 fclose(outFile);
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
505
16e63811d0c2 Clean up the pack file node adding/extracting.
Matti Hamalainen <ccr@tnsp.org>
parents: 1025
diff changeset
506 return ret;
952
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
507 }
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
508
ffdae9cd81de Remove the dmpackutil module, move the code to packed utility
Matti Hamalainen <ccr@tnsp.org>
parents: 871
diff changeset
509
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 /* Compare a string to a pattern. Case-SENSITIVE version.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 * The matching pattern can consist of any normal characters plus
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 * wildcards ? and *. "?" matches any character and "*" matches
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 * any number of characters.
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514 */
1407
4bca92dd7280 Rename dm_strmatch() to dmStrMatch().
Matti Hamalainen <ccr@tnsp.org>
parents: 1406
diff changeset
515 BOOL dmStrMatch(const char *str, const char *pattern)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
517 BOOL matched = TRUE, any = FALSE, end = FALSE;
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
518 const char *tmp = NULL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 // Check given pattern and string
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521 if (str == NULL || pattern == NULL)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 return FALSE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 // Start comparision
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 do {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
526 matched = FALSE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 switch (*pattern)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 case '?':
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 // Any single character matches
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 if (*str)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
533 matched = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 pattern++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 str++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 break;
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 case '*':
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
540 matched = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 pattern++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 if (!*pattern)
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
543 end = TRUE;
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
544 any = TRUE;
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
545 tmp = pattern;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 case 0:
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
549 if (any)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 if (*str)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 str++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 else
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
554 end = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 else
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 if (*str)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
560 if (tmp)
0
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 any = TRUE;
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
563 pattern = tmp;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565 else
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
566 matched = FALSE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 else
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
569 end = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 default:
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
573 if (any)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 if (*pattern == *str)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576 {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
577 any = FALSE;
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
578 matched = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580 else
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 if (*str)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
584 matched = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 str++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 }
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 else
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 if (*pattern == *str)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
593 matched = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 if (*pattern)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 pattern++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 if (*str)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597 str++;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 else
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 {
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
601 if (tmp)
0
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 matched = TRUE;
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
604 any = TRUE;
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
605 pattern = tmp;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 }
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 if (!*str && !*pattern)
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
611 end = TRUE;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 } // switch
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615
1019
8cf8a8035438 Rename some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 1018
diff changeset
616 } while (matched && !end);
0
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 return matched;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
622 BOOL dmMatchList(char **list, const size_t nlist, const char *str)
752
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
623 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
624 for (size_t i = 0; i < nlist; i++)
752
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
625 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
626 if (dmStrMatch(str, list[i]))
752
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
627 return TRUE;
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
628 }
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
629 return FALSE;
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
630 }
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
631
2b9dd22f01e9 Improve packed by adding file exclusion option.
Matti Hamalainen <ccr@tnsp.org>
parents: 721
diff changeset
632
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 int main(int argc, char *argv[])
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 {
1642
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
635 int res = 0;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 DMPackFile *pack = NULL;
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
637 DMPackEntry *entry = NULL;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 // Parse arguments
2408
60e119262c67 Option index re-ordering cleanup work.
Matti Hamalainen <ccr@tnsp.org>
parents: 2402
diff changeset
640 dmInitProg("packed", "TNSP Pack File Editor", "0.7", NULL, NULL);
1011
aa745d31612e Set default verbosity of 'packed' to none.
Matti Hamalainen <ccr@tnsp.org>
parents: 1008
diff changeset
641 dmVerbosity = 0;
1238
e8c99da451cd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
642
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 if (!dmArgsProcess(argc, argv, optList, optListN,
1642
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
644 argHandleOpt, argHandleNonOpt, OPTH_BAILOUT))
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 exit(1);
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
647 if (optCommand == CMD_NONE || argc < 2)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648 {
2441
275374725598 Show help when no arguments.
Matti Hamalainen <ccr@tnsp.org>
parents: 2440
diff changeset
649 argShowHelp();
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
650 goto out;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651 }
1238
e8c99da451cd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
652
1642
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
653 if (optPackFilename == NULL)
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
654 {
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
655 dmErrorMsg("No PACK file specified.\n");
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
656 goto out;
1642
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
657 }
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
658
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
659 dmMsg(1, "Processing PACK '%s' ...\n", optPackFilename);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 // Execute command
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662 switch (optCommand)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664 case CMD_CREATE:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 case CMD_ADD:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666 switch (optCommand)
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 case CMD_CREATE:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
669 dmMsg(1, "Creating new PACK\n");
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
670 res = dmPackCreate(optPackFilename, &pack);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 case CMD_ADD:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674 dmMsg(1, "Opening existing PACK\n");
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
675 res = dmPackOpen(optPackFilename, &pack, FALSE);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
677 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
679 if (res != DMERR_OK)
0
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 dmErrorMsg("Could not open PACK file: %s\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
682 dmErrorStr(res));
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
683 goto out;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
684 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
686 // Add files into PACK
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
687 dmMsg(1, "Adding files...\n");
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
688
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
689 for (size_t i = 0; i < nsrcFilenames; i++)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
690 if (!dmMatchList(excFilenames, nexcFilenames, srcFilenames[i]))
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
691 {
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
692 res = dmPackAddFile(pack, srcFilenames[i], optCompressLevel, &entry);
721
bb14d7907eb2 Rename many pack & resource handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 652
diff changeset
693
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
694 if (res != DMERR_OK)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
695 {
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
696 dmPrint(1, "%-32s [ERROR:%d]\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
697 srcFilenames[i], res);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 }
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
699 else
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
700 {
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
701 dmPrint(1, "%-32s ['%s', s=%d, c=%d, o=%" DM_PRId64 ", f=0x%04x]\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
702 srcFilenames[i], entry->filename,
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
703 entry->size, entry->csize, entry->offset,
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
704 entry->flags);
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
705 }
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 }
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
707
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
708 dmMsg(2, "w=%d\n", dmPackWrite(pack));
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709 break;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711 case CMD_LIST:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 // List files in PACK
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
713 if ((res = dmPackOpen(optPackFilename, &pack, TRUE)) != DMERR_OK)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
714 {
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
715 dmErrorMsg("Could not open PACK file: %s\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
716 dmErrorStr(res));
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
717 goto out;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
718 }
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
719 else
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
721 size_t ntotal = 0, nshown = 0,
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
722 totalSize = 0, totalCSize = 0;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
723
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
724 // Print the list header
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
725 int hlen = printf(
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
726 "%-32s | %10s | %10s | %10s | %s\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
727 "Name", "Size", "CSize", "Offset", "Flags");
1642
8fb4e8c825d6 Refactor "packed" utility. This breaks commandline backwards compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 1432
diff changeset
728
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
729 for (int n = 0; n < hlen; n++)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
730 fputc('-', stdout);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
732 fputs("\n", stdout);
115
23e03defa759 Prettify pack content listing output.
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
733
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
734 // Print all the matching entries
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
735 for (entry = pack->entries; entry != NULL; entry = entry->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736 {
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 BOOL match;
1238
e8c99da451cd Cosmetics.
Matti Hamalainen <ccr@tnsp.org>
parents: 1167
diff changeset
738
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 if (nsrcFilenames > 0)
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
740 // Check for matches in specified filenames/patterns
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
741 match = dmMatchList(srcFilenames, nsrcFilenames, entry->filename);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742 else
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
743 // No filenames specified, everything shown
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
744 match = TRUE;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
746 if (match && !dmMatchList(excFilenames, nexcFilenames, entry->filename))
0
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 printf("%-32s | %10d | %10d | %010" DM_PRIx64 " | %04x\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
749 entry->filename, entry->size, entry->csize,
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
750 entry->offset, entry->flags);
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
751
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
752 nshown++;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
753 totalSize += entry->size;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
754 totalCSize += entry->csize;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755 }
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
756
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
757 ntotal++;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
760 for (int n = 0; n < hlen; n++)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
761 fputc('-', stdout);
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
762
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
763 printf(
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
764 "\n"
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
765 "%-32s | %10" DM_PRIu_SIZE_T " | %10" DM_PRIu_SIZE_T " |\n"
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
766 "Listed %" DM_PRIu_SIZE_T " of %" DM_PRIu_SIZE_T " files total.\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
767 "Totals",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
768 totalSize, totalCSize,
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
769 nshown, ntotal);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
770 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 break;
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 case CMD_EXTRACT:
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
774 // Extract files from PACK
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
775 if ((res = dmPackOpen(optPackFilename, &pack, TRUE)) != DMERR_OK)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
776 {
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
777 dmErrorMsg("Could not open PACK file: %s\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
778 dmErrorStr(res));
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
779 goto out;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
780 }
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
781
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
782 for (entry = pack->entries; entry != NULL; entry = entry->next)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
783 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
784 BOOL match;
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
785
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
786 // Check for matches
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
787 if (nsrcFilenames > 0)
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
788 {
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
789 match = !dmMatchList(excFilenames, nexcFilenames, entry->filename) &&
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
790 dmMatchList(srcFilenames, nsrcFilenames, entry->filename);
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
791 }
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
792 else
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
793 {
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
794 match = !dmMatchList(excFilenames, nexcFilenames, entry->filename);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
795 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
796
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
797 if (match && (entry->privFlags & PACK_EXTRACTED) == 0)
0
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 // Print one entry
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
800 dmPrint(0, "Extracting: %-32s [siz=%d, cmp=%d, offs=0x%08" DM_PRIx64 ", flags=0x%04x]\n",
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
801 entry->filename, entry->size, entry->csize,
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
802 entry->offset, entry->flags);
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
803
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
804 if ((res = dmPackExtractFile(pack, entry, optCompressLevel > 0)) == DMERR_OK)
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
805 entry->privFlags |= PACK_EXTRACTED;
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
807 }
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
808 break;
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
2480
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
811 out:
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
812 dmPackClose(pack);
c7a0913e1032 Various cleanups, integrate some changes/improvements from opendat project to packed.
Matti Hamalainen <ccr@tnsp.org>
parents: 2479
diff changeset
813
0
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
814 return 0;
32250b436bca Initial re-import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
815 }