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,
34 extract_file(TennixArchive
*tnxar
, bool verbose
)
36 char *data
= tnxar
->getItemBytes();
39 printf("%s\n", tnxar
->getItemFilename());
42 FILE *fp
= fopen(tnxar
->getItemFilename(), "wb");
46 fwrite(data
, tnxar
->getItemSize(), 1, fp
);
57 ArchiveTool(int argc
, char **argv
);
64 int create(bool verbose
);
65 int test(bool verbose
);
66 int extract(bool verbose
);
72 ArchiveTool::ArchiveTool(int argc
, char **argv
)
81 bool do_create
= false;
83 bool do_extract
= false;
91 if (*opts
== '-') opts
++;
119 if (do_create
&& !do_test
&& !do_extract
&& argc
> 3) {
120 return create(verbose
);
121 } else if (do_test
&& !do_create
&& !do_extract
&& argc
> 2) {
122 return test(verbose
);
123 } else if (do_extract
&& !do_create
&& !do_test
&& argc
> 2) {
124 return extract(verbose
);
131 ArchiveTool::create(bool verbose
)
134 fprintf(stderr
, "Refusing to create an empty archive.\n");
138 char *archive
= argv
[2];
139 if (strcmp(".tnx", archive
+ strlen(archive
) - 4) != 0) {
140 fprintf(stderr
, "Wrong file extension: %s\n", archive
);
145 for (int i
=3; i
<argc
; i
++) {
146 FILE *fp
= fopen(argv
[i
], "rb");
147 fseek(fp
, 0, SEEK_END
);
148 size_t len
= ftell(fp
);
149 fseek(fp
, 0, SEEK_SET
);
150 char *data
= (char*)malloc(len
);
151 tnx_assert(fread(data
, len
, 1, fp
) == 1);
153 char *filename
= strdup(basename(argv
[i
]));
155 printf("%s\n", filename
);
157 tnxar
.appendItem(filename
, data
, len
);
159 tnxar
.buildFile(archive
);
164 ArchiveTool::test(bool verbose
)
166 TennixArchive
tnxar(argv
[2]);
168 while (!tnxar
.endOfFile()) {
170 printf("%ld %s\n", tnxar
.getItemSize(), tnxar
.getItemFilename());
172 printf("%s\n", tnxar
.getItemFilename());
181 ArchiveTool::extract(bool verbose
)
183 TennixArchive
tnxar(argv
[2]);
186 while (!tnxar
.endOfFile()) {
187 if (extract_file(&tnxar
, verbose
) != 0) {
188 printf("Error extracting file\n");
196 for (int i
=3; i
<argc
; i
++) {
197 if (tnxar
.setItemFilename(argv
[i
])) {
198 if (extract_file(&tnxar
, verbose
) != 0) {
199 printf("Error extracting file: %s\n", argv
[i
]);
203 fprintf(stderr
, "File not in archive: %s\n", argv
[i
]);
214 fprintf(stderr
, "Usage: %s [-ctxvf] <archive.tnx> file1 ...\n", argv
[0]);
215 fprintf(stderr
, " -c ... Create archive\n");
216 fprintf(stderr
, " -t ... Test archive (list contents)\n");
217 fprintf(stderr
, " -x ... Extract archive\n");
218 fprintf(stderr
, " -v ... Verbose output\n");
219 fprintf(stderr
, " -f ... Filename of archive (optional)\n");
224 main(int argc
, char **argv
)
226 ArchiveTool
tool(argc
, argv
);