summaryrefslogtreecommitdiffhomepage
path: root/ArchiveEx/dat.cpp
blob: ae1304d72417c1fe659afd288e5e670267c0c8b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <unistd.h>

#include <exception>
#include <iostream>
#include <ostream>
#include <string>
#include <vector>

#include <Archive.h>


enum class Action
{
	NOTHING,
	LIST,
	UPDATE,
	EXTRACT,
};


struct Options
{
	bool help {false};
	Action action {Action::NOTHING};
	bool create {false};
	const char* archive {nullptr};
	std::vector<std::string> files {};
};


class ArgsError : std::exception
{
public:
	ArgsError(int opt, const char* msg) : option {opt}, message {msg} {}
	int option;
	const char* message;
};


static Options ParseArgs(int argc, char* argv[]);
static void PrintUsage(std::ostream& out=std::cerr);
static void PrintHelp(std::ostream& out=std::cout);


static const char* program {"dat"};


int
main(int argc, char* argv[])
try {
	program = argv[0];
	const auto opts = ParseArgs(argc, argv);
	if (opts.help) {
		PrintUsage(std::cout);
		PrintHelp();
		return 0;
	}
	ArchiveEx::Archive archive(opts.archive, opts.create);
	switch (opts.action) {
	case Action::NOTHING:
		break;
	case Action::LIST:
		archive.PrintNamesOfEntries();
		break;
	case Action::UPDATE:
		for (const auto& file : opts.files)
			archive.Insert(file.c_str());  // Support recursive directories tar-like
		break;
	case Action::EXTRACT:
		break;  // Not implemented
	}
}
catch (const ArgsError& err) {
	std::cerr << err.message;
	if (err.option > 0)
		std::cerr << ": " << static_cast<char>(err.option);
	std::cerr << std::endl;
	PrintUsage();
	return 1;
}
catch (const char* err) {
	std::cerr << err << std::endl;
	return 1;
}


Options
ParseArgs(int argc, char* argv[])
{
	int opt;
	Options opts;
	while (-1 != (opt = getopt(argc, argv, ":hluxc"))) {
		switch (opt) {
		case 'h':
			opts.help = true;
			return opts;  // No need to waste time as program won't do anything else anyway.
		case 'l':
			opts.action = Action::LIST;
			break;
		case 'u':
			opts.action = Action::UPDATE;
			break;
		case 'x':
			opts.action = Action::EXTRACT;
			break;
		case 'c':
			opts.create = true;
			break;
		case ':':
			throw ArgsError(optopt, "missing argument for option");
		default:
			throw ArgsError(optopt, "invalid option");
		}
	}
	if (optind >= argc)
		throw ArgsError(0, "missing archive name");
	opts.archive = argv[optind++];
	for (; optind < argc; ++optind)
		opts.files.emplace_back(argv[optind]);
	return opts;
}


void
PrintUsage(std::ostream& out)
{
	out << "Usage: " << program << " [-hluxc] archive [file [files...]]" << std::endl;
}


void
PrintHelp(std::ostream& out)
{
	out << "Options:" << std::endl;
	out << "  -h\tPrints this help message and exits." << std::endl;
	out << "  -l\tLists files in the archive." << std::endl;
	out << "  -u\tInserts files from file system into archive." << std::endl;
	out << "  -x\tExtracts files from the archive." << std::endl;
	out << "  -c\tCreate the archive if it does not exist." << std::endl;
}