Implement the code to calculate the checksum

This commit is contained in:
King_DuckZ 2014-09-25 16:30:42 +02:00
parent 4eb37e4773
commit d1c01a340b

88
main.d
View file

@ -23,31 +23,84 @@ import std.getopt;
mixin(import("tigersumConfig.d"));
void main (string[] parArgs) {
void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) {
foreach (file_name; parFiles) {
const auto curr_path = parCWD ~ file_name;
File curr_file = File(curr_path, parFileMode);
if (!isDir(curr_path)) {
const string hash = tigerToString(getTiger(curr_file, parTiger2));
if (parBsdStyle) {
writefln("tiger (%s) = %s", file_name, hash);
}
else {
writefln("%s %s", hash, file_name);
}
}
else {
writefln("tigersum: %s: is a directory", file_name);
}
}
}
int main (string[] parArgs) {
if (parArgs.length == 1)
return 0;
const auto cwd = getcwd();
const auto working_dir = cwd ~ (cwd[$-1] == '/' ? "" : "/");
bool show_version, show_help, show_license;
bool check_mode;
string file_mode = "r";
bool bsd_style;
bool switch_quiet, switch_status, switch_warn;
ushort tiger_version = TIGER_VERSION_DEF;
getopt(parArgs,
void fileModeHandler (string parOption) {
if (parOption == "text|t") {
file_mode = "r";
}
else if (parOption == "binary|b") {
file_mode = "rb";
}
}
string[] args = parArgs[];
getopt(args,
std.getopt.config.passThrough,
std.getopt.config.bundling,
"version|v", &show_version,
"help|h", &show_help,
"check|c", &check_mode,
"tiger|t", &tiger_version,
"licence|license", &show_license
"tiger|i", &tiger_version,
"licence|license", &show_license,
"binary|b", &fileModeHandler,
"text|t", &fileModeHandler,
"tag", &bsd_style
);
if (check_mode) {
}
else if (show_help) {
writefln("Usage: %s [OPTION]... [FILE]...", PROGRAM_NAME);
writeln(" -c, --check Read tiger sums from FILE and check them.");
writeln(" -h, --help Show this help.");
writeln(" -v, --version Show version info and quit.");
writefln(" -t n, --tiger=n Set the tiger version that should be used. Valid values are 1 or 2, %d is default.", TIGER_VERSION_DEF);
writefln(" --license Show details about the license under which %s is distributed.", PROGRAM_NAME);
writeln(" -b, --binary read in binary mode");
writeln(" -c, --check read tiger sums from FILEs and check them");
writeln(" --tag create a BSD-style checksum");
writeln(" -t, --text read in text mode (default)");
writefln(" -i n, --tiger=n set the tiger version that should be used; valid values are 1 or 2, %d is default", TIGER_VERSION_DEF);
writeln();
writeln();
writeln(" -h, --help display this help and exit");
writeln(" -v, --version display version informations and exit");
writefln(" --license display details about the license under which %s is distributed", PROGRAM_NAME);
writeln();
writeln("The sums are computed using the reference implementation algorithm. When checking, the input");
writeln("should be a former output of this program. The default mode is to print");
writeln("a line with checksum, a character indicating type(`*' for binary, ` ' for");
writeln("text), and name for each FILE.");
writeln();
writeln("Report bugs on <https://bitbucket.org/King_DuckZ/tigersum/issues?status=new&status=open>");
writeln("tigersum home page: <https://bitbucket.org/King_DuckZ/tigersum>");
}
else if (show_version) {
writefln("%s %d.%d Copyright © %d Michele \"King_DuckZ\" Santullo", PROGRAM_NAME, TIGERSUM_VERSION_MAJOR, TIGERSUM_VERSION_MINOR, COPYRIGHT_YEAR);
@ -76,17 +129,12 @@ void main (string[] parArgs) {
writeln("along with this program. If not, see <http://www.gnu.org/licenses/>.");
}
else {
//regular mode
if (tiger_version != 1 && tiger_version != 2) {
writefln("Invalid tiger version requested: %d - use %s --help for help on program's usage.", tiger_version, PROGRAM_NAME);
return 2;
}
calculateSums(args[1 .. $], working_dir, tiger_version == 2, file_mode, bsd_style);
}
//foreach (arg; parArgs[1 .. $]) {
// const auto curr_path = working_dir ~ arg;
// File curr_file = File(curr_path, "rb");
// if (!isDir(curr_path)) {
// writefln("%s %s", tigerToString(getTiger(curr_file, true)), arg);
// }
// else {
// writefln("tigersum: %s: is a directory", arg);
// }
//}
return 0;
}