Streamline the archive loading mechanism
[tennix.git] / archivetool.cc
blob870889e15cb6c39c84cd1e70580268eead64bb1c
2 /**
4 * Tennix Archive File Format
5 * Copyright (C) 2009-2010 Thomas Perl <thp@thpinfo.com>
6 *
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,
20 * MA 02110-1301, USA.
22 **/
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
29 #include "archive.hh"
31 int main(int argc, char* argv[])
33 TennixArchive* tnxar;
34 char* data;
35 FILE *fp;
36 const char* filename;
37 char *bn = (char*)basename(argv[0]);
38 int len, i;
39 struct stat st;
41 if(strcmp(bn, "archive") == 0) {
42 if (argc < 2) {
43 fprintf(stderr, "Usage: %s archive.tnx file1 [....]\n", bn);
44 exit(EXIT_FAILURE);
45 } else if (argc == 2) {
46 fprintf(stderr, "Refusing to create an empty archive.\n");
47 exit(EXIT_FAILURE);
50 if (stat(argv[1], &st) != -1) {
51 fprintf(stderr, "File %s already exists. Aborting.\n", argv[1]);
52 exit(EXIT_FAILURE);
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);
61 len = ftell(fp);
62 fseek(fp, 0, SEEK_SET);
63 data = (char*)malloc(len);
64 assert(fread(data, len, 1, fp) == 1);
65 fclose(fp);
66 tnxar->appendItem((char*)basename(argv[i]), data, len);
68 tnxar->buildFile(argv[1]);
69 delete tnxar;
70 } else if(strcmp(bn, "dump") == 0) {
71 if (argc < 2) {
72 fprintf(stderr, "Usage: %s archive.tnx\n", bn);
73 exit(EXIT_FAILURE);
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);
80 exit(EXIT_FAILURE);
82 tnxar = new TennixArchive(argv[1]);
83 if (argc == 2) {
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");
91 fputc('.', stderr);
92 assert(fwrite(data, len, 1, fp) == 1);
93 fputc('.', stderr);
94 fclose(fp);
95 fprintf(stderr, ".OK\n");
96 free(data);
97 tnxar->next();
99 } else if (argc == 3) {
100 filename = argv[2];
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");
107 fputc('.', stderr);
108 assert(fwrite(data, len, 1, fp) == 1);
109 fputc('.', stderr);
110 fclose(fp);
111 fprintf(stderr, ".OK\n");
112 free(data);
113 } else {
114 fprintf(stderr, "File not found in %s: %s\n", argv[1], filename);
115 delete tnxar;
116 exit(EXIT_FAILURE);
119 delete tnxar;
122 return EXIT_SUCCESS;