4 * Tennix Archive File Format
5 * Copyright (C) 2009-2010 Thomas Perl <thp@thpinfo.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 int main(int argc
, char* argv
[])
37 char *bn
= (char*)basename(argv
[0]);
41 if(strcmp(bn
, "archive") == 0) {
43 fprintf(stderr
, "Usage: %s archive.tnx file1 [....]\n", bn
);
45 } else if (argc
== 2) {
46 fprintf(stderr
, "Refusing to create an empty archive.\n");
50 if (stat(argv
[1], &st
) != -1) {
51 fprintf(stderr
, "File %s already exists. Aborting.\n", argv
[1]);
55 tnxar
= new TennixArchive();
57 fprintf(stderr
, "Creating %s with %d files\n", argv
[1], argc
-2);
58 for (i
=2; i
<argc
; i
++) {
59 fp
= fopen(argv
[i
], "rb");
60 fseek(fp
, 0, SEEK_END
);
62 fseek(fp
, 0, SEEK_SET
);
63 data
= (char*)malloc(len
);
64 assert(fread(data
, len
, 1, fp
) == 1);
66 tnxar
->appendItem((char*)basename(argv
[i
]), data
, len
);
68 tnxar
->buildFile(argv
[1]);
70 } else if(strcmp(bn
, "dump") == 0) {
72 fprintf(stderr
, "Usage: %s archive.tnx\n", bn
);
75 TennixArchive
archive(argv
[1]);
76 std::cerr
<< archive
<< std::endl
;
77 } else if(strcmp(bn
, "extract") == 0) {
78 if (argc
< 2 || argc
> 3) {
79 fprintf(stderr
, "Usage: %s archive.tnx [file]\n", bn
);
82 tnxar
= new TennixArchive(argv
[1]);
84 while (!tnxar
->endOfFile()) {
85 filename
= tnxar
->getItemFilename();
86 data
= tnxar
->getItemBytes();
87 len
= tnxar
->getItemSize();
88 fprintf(stderr
, "Extracting: %s", filename
);
89 fprintf(stderr
, " (%d bytes)", len
);
90 fp
= fopen(filename
, "wb");
92 assert(fwrite(data
, len
, 1, fp
) == 1);
95 fprintf(stderr
, ".OK\n");
99 } else if (argc
== 3) {
101 if (tnxar
->setItemFilename(filename
) != 0) {
102 fprintf(stderr
, "Extracting: %s", filename
);
103 data
= tnxar
->getItemBytes();
104 len
= tnxar
->getItemSize();
105 fprintf(stderr
, " (%d bytes)", len
);
106 fp
= fopen(filename
, "wb");
108 assert(fwrite(data
, len
, 1, fp
) == 1);
111 fprintf(stderr
, ".OK\n");
114 fprintf(stderr
, "File not found in %s: %s\n", argv
[1], filename
);