tigersum/sums.d
King_DuckZ c085388163 Added code to check saved checksums.
The code is very brittle, no error checking is performed at all
so if anything goes wrong the program will misbehave.
Output is still incomplete.
2014-09-25 17:09:46 +02:00

87 lines
2.6 KiB
D

/* tigersum - a command line utility to calculate the tiger hash from input.
Copyright (C) 2014 Michele "King_DuckZ" Santullo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import std.regex;
import std.stdio;
import std.file : isDir;
import std.conv;
import std.string;
import tiger;
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);
}
}
}
bool checkSums (in string parFile, in string parCWD, bool parTiger2, string parFileMode) {
File curr_list = File(parFile, "r");
bool ret_val = true;
const auto reg_bsd = r"^tiger \((.+)\)\s*=\s*([a-fA-F0-9]{48})$";
const auto reg_normal = r"^([a-fA-F0-9]{48})\s+(.+)$";
foreach (line; curr_list.byLine()) {
auto m = match(line, reg_bsd);
int index_hash, index_file;
if (m) {
index_hash = 2;
index_file = 1;
}
else {
m = match(line, reg_normal);
index_hash = 1;
index_file = 2;
}
if (m) {
const string capt_filename = to!string(m.captures[index_file]);
writef("%s: ", capt_filename);
const string curr_path = parCWD ~ capt_filename;
File curr_file = File(curr_path, parFileMode);
const string curr_tiger = tigerToString(getTiger(curr_file, parTiger2));
const string check_tiger = to!string(m.captures[index_hash]).toLower;
if (check_tiger == curr_tiger)
writeln("OK");
else
writeln("FAILED");
}
}
return ret_val;
}
bool checkSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode) {
bool ret_val = true;
foreach (file_name; parFiles) {
const auto curr_path = parCWD ~ file_name;
ret_val &= checkSums(curr_path, parCWD, parTiger2, parFileMode);
}
return ret_val;
}