Code clean-ups and makefile optimizations
[tennix.git] / archivetool.cc
blobad61006c7d3b072dea277484e5887c30aded5c2d
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 <libgen.h>
28 #include <sys/stat.h>
30 #include "archive.hh"
32 int main(int argc, char* argv[])
34 TennixArchive* tnxar;
35 char* data;
36 FILE *fp;
37 const char* filename;
38 char *bn = (char*)basename(argv[0]);
39 int len, i;
40 struct stat st;
42 if(strcmp(bn, "archive") == 0) {
43 if (argc < 2) {
44 fprintf(stderr, "Usage: %s archive.tnx file1 [....]\n", bn);
45 exit(EXIT_FAILURE);
46 } else if (argc == 2) {
47 fprintf(stderr, "Refusing to create an empty archive.\n");
48 exit(EXIT_FAILURE);
51 if (stat(argv[1], &st) != -1) {
52 fprintf(stderr, "File %s already exists. Aborting.\n", argv[1]);
53 exit(EXIT_FAILURE);
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);
62 len = ftell(fp);
63 fseek(fp, 0, SEEK_SET);
64 data = (char*)malloc(len);
65 assert(fread(data, len, 1, fp) == 1);
66 fclose(fp);
67 tnxar->appendItem((char*)basename(argv[i]), data, len);
69 tnxar->buildFile(argv[1]);
70 delete tnxar;
71 } else if(strcmp(bn, "dump") == 0) {
72 if (argc < 2) {
73 fprintf(stderr, "Usage: %s archive.tnx\n", bn);
74 exit(EXIT_FAILURE);
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);
81 exit(EXIT_FAILURE);
83 tnxar = new TennixArchive(argv[1]);
84 if (argc == 2) {
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");
92 fputc('.', stderr);
93 assert(fwrite(data, len, 1, fp) == 1);
94 fputc('.', stderr);
95 fclose(fp);
96 fprintf(stderr, ".OK\n");
97 free(data);
98 tnxar->next();
100 } else if (argc == 3) {
101 filename = argv[2];
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");
108 fputc('.', stderr);
109 assert(fwrite(data, len, 1, fp) == 1);
110 fputc('.', stderr);
111 fclose(fp);
112 fprintf(stderr, ".OK\n");
113 free(data);
114 } else {
115 fprintf(stderr, "File not found in %s: %s\n", argv[1], filename);
116 delete tnxar;
117 exit(EXIT_FAILURE);
120 delete tnxar;
123 return EXIT_SUCCESS;