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,
32 int main(int argc
, char* argv
[])
38 char *bn
= (char*)basename(argv
[0]);
42 if(strcmp(bn
, "archive") == 0) {
44 fprintf(stderr
, "Usage: %s archive.tnx file1 [....]\n", bn
);
46 } else if (argc
== 2) {
47 fprintf(stderr
, "Refusing to create an empty archive.\n");
51 if (stat(argv
[1], &st
) != -1) {
52 fprintf(stderr
, "File %s already exists. Aborting.\n", argv
[1]);
56 tnxar
= new TennixArchive();
58 fprintf(stderr
, "Creating %s with %d files\n", argv
[1], argc
-2);
59 for (i
=2; i
<argc
; i
++) {
60 fp
= fopen(argv
[i
], "rb");
61 fseek(fp
, 0, SEEK_END
);
63 fseek(fp
, 0, SEEK_SET
);
64 data
= (char*)malloc(len
);
65 assert(fread(data
, len
, 1, fp
) == 1);
67 tnxar
->appendItem((char*)basename(argv
[i
]), data
, len
);
69 tnxar
->buildFile(argv
[1]);
71 } else if(strcmp(bn
, "dump") == 0) {
73 fprintf(stderr
, "Usage: %s archive.tnx\n", bn
);
76 TennixArchive
archive(argv
[1]);
77 std::cerr
<< archive
<< std::endl
;
78 } else if(strcmp(bn
, "extract") == 0) {
79 if (argc
< 2 || argc
> 3) {
80 fprintf(stderr
, "Usage: %s archive.tnx [file]\n", bn
);
83 tnxar
= new TennixArchive(argv
[1]);
85 while (!tnxar
->endOfFile()) {
86 filename
= tnxar
->getItemFilename();
87 data
= tnxar
->getItemBytes();
88 len
= tnxar
->getItemSize();
89 fprintf(stderr
, "Extracting: %s", filename
);
90 fprintf(stderr
, " (%d bytes)", len
);
91 fp
= fopen(filename
, "wb");
93 assert(fwrite(data
, len
, 1, fp
) == 1);
96 fprintf(stderr
, ".OK\n");
100 } else if (argc
== 3) {
102 if (tnxar
->setItemFilename(filename
) != 0) {
103 fprintf(stderr
, "Extracting: %s", filename
);
104 data
= tnxar
->getItemBytes();
105 len
= tnxar
->getItemSize();
106 fprintf(stderr
, " (%d bytes)", len
);
107 fp
= fopen(filename
, "wb");
109 assert(fwrite(data
, len
, 1, fp
) == 1);
112 fprintf(stderr
, ".OK\n");
115 fprintf(stderr
, "File not found in %s: %s\n", argv
[1], filename
);