From 64d67739383a0b58adcfa3dd9b070bb0ed876e5f Mon Sep 17 00:00:00 2001 From: "Fred T. Hamster" Date: Wed, 15 Feb 2012 21:37:08 -0500 Subject: [PATCH 1/1] added quiet mode to checker. --- nucleus/applications/utilities/checker.cpp | 89 ++++++++++++---------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/nucleus/applications/utilities/checker.cpp b/nucleus/applications/utilities/checker.cpp index c56cec3f..94f0390c 100644 --- a/nucleus/applications/utilities/checker.cpp +++ b/nucleus/applications/utilities/checker.cpp @@ -16,8 +16,10 @@ * Please send any updates to: fred@gruntose.com * \*****************************************************************************/ +#include #include #include +#include #include #include #include @@ -26,21 +28,24 @@ #include #include +using namespace application; using namespace basis; +using namespace loggers; using namespace structures; using namespace timely; const int buffer_size = 4096; -//HOOPLE_STARTUP_CODE; - -//#define DEBUG_CHECKER +#define DEBUG_CHECKER // uncomment for noisy version. -void print_instructions_and_exit(char *program_name) +#undef LOG +#define LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT) + +int print_instructions(bool good, const astring &program_name) { printf("\n\ -Usage:\n\t%s [-t] filename [filename]\n\n\ +Usage:\n\t%s [-q] [-t|-b] filename [filename]\n\n\ This program generates a checksum for each file that is entered on the\n\ command line. The checksum is (hopefully) an architecture independent\n\ number that is a very compressed representation of the file gestalt.\n\ @@ -49,11 +54,12 @@ This is a useful test of whether a file copy or a program download is\n\ successful in making an identical version of the file. In particular, if the\n\ file is made slightly bigger or smaller, or if an item in the file is changed,\n\ then the checksums of the two versions should be different numbers.\n\n\ +The -q flag specifies a quieter print-out, without any headers.\n\ The -b flag is used if the files are to be compared as binary files, and this\n\ is also the default. The -t flag is used if the files are to be compared as\n\ text files.\n", - program_name); - exit(1); + program_name.s()); + return !good; // zero is successful exit. } #define HIGHEST_CHECK 32714 @@ -61,12 +67,12 @@ text files.\n", // do_checksum: takes the specified file name and generates a checksum for it. // if the file is inaccessible or, at any point, reading it returns an // error message, then a negative value is returned. -int do_checksum(char *file_name, int open_as_a_text_file) +int do_checksum(const astring &file_name, int open_as_a_text_file) { char file_open_mode[10]; if (open_as_a_text_file) strcpy(file_open_mode, "rt"); else strcpy(file_open_mode, "rb"); - FILE *opened_file = fopen(file_name, file_open_mode); + FILE *opened_file = fopen(file_name.s(), file_open_mode); #ifdef DEBUG_CHECKER LOG(astring("opened ") + file_name); #endif @@ -100,12 +106,12 @@ int do_checksum(char *file_name, int open_as_a_text_file) // do_fletcher_checksum: takes the specified file name and generates a fletcher // checksum for it. if the file is inaccessible or, at any point, // reading it returns an error message, then a negative value is returned. -int do_fletcher_checksum(char *file_name, int open_as_a_text_file) +int do_fletcher_checksum(const astring &file_name, int open_as_a_text_file) { char file_open_mode[10]; if (open_as_a_text_file) strcpy(file_open_mode, "rt"); else strcpy(file_open_mode, "rb"); - FILE *opened_file = fopen(file_name, file_open_mode); + FILE *opened_file = fopen(file_name.s(), file_open_mode); #ifdef DEBUG_CHECKER LOG(astring("opened ") + file_name); #endif @@ -138,40 +144,45 @@ int do_fletcher_checksum(char *file_name, int open_as_a_text_file) int main(int argc, char *argv[]) { - char name[200]; - // if the file is to be read as a text file, then this is true. - int open_file_as_text = false; + bool open_file_as_text = false; + // if we are to show our normal header info, this will be true. + bool show_header = true; - if (argc <= 1) print_instructions_and_exit(argv[0]); - else { - int current_parameter = 0; - if (argv[1][0] == '-') { - if (argv[1][1] == 't') { - current_parameter++; - open_file_as_text = true; - } else if (argv[1][1] == 'b') { - current_parameter++; - open_file_as_text = false; - } else print_instructions_and_exit(argv[0]); - } - bool printed_header = false; - while (++current_parameter < argc) { - if (!printed_header) { - printed_header = true; + if (argc <= 1) return print_instructions(false, argv[0]); + + command_line cmds(argc, argv); + int index = 0; + if (cmds.find('b', index)) open_file_as_text = false; + index = 0; + if (cmds.find('t', index)) open_file_as_text = true; + index = 0; + if (cmds.find('q', index)) show_header = false; + index = 0; + if (cmds.find('?', index)) return print_instructions(true, argv[0]); + index = 0; + if (cmds.find("help", index)) return print_instructions(true, argv[0]); + bool printed_header = false; + + for (int entry = 0; entry < cmds.entries(); entry++) { + command_parameter c = cmds.get(entry); + if (c.type() != command_parameter::VALUE) continue; + if (!printed_header) { + printed_header = true; + if (show_header) { printf("%s\n", (astring("[ checker running at ") + time_stamp::notarize(true) + "]").s()); printf("bizarro fletcher filename\n"); printf("======= ======== ========\n"); } - strcpy(name, argv[current_parameter]); - int checksum_of_file = do_checksum(name, open_file_as_text); - int fletcher_chksum = do_fletcher_checksum(name, open_file_as_text); - if (checksum_of_file >= 0) { - printf("%s", a_sprintf(" %05d 0x%04x %s\n", checksum_of_file, - fletcher_chksum, name).s()); - } else { - printf("%s", a_sprintf("%s is inaccessible.\n", name).s()); - } + } + astring name = c.text(); + int checksum_of_file = do_checksum(name, open_file_as_text); + int fletcher_chksum = do_fletcher_checksum(name, open_file_as_text); + if (checksum_of_file >= 0) { + printf("%s", a_sprintf(" %05d 0x%04x %s\n", checksum_of_file, + fletcher_chksum, name.s()).s()); + } else { + printf("%s", a_sprintf("%s is inaccessible.\n", name.s()).s()); } } return 0; -- 2.34.1