43#define LOG(to_print) program_wide_logger::get().log(to_print, ALWAYS_PRINT)
48Usage:\n\t%s [-q] [-t|-b] filename [filename]\n\n\
49This program generates a checksum for each file that is entered on the\n\
50command line. The checksum is (hopefully) an architecture independent\n\
51number that is a very compressed representation of the file gestalt.\n\
52If one compares two copies of a file, then the checksums should be identical.\n\
53This is a useful test of whether a file copy or a program download is\n\
54successful in making an identical version of the file. In particular, if the\n\
55file is made slightly bigger or smaller, or if an item in the file is changed,\n\
56then the checksums of the two versions should be different numbers.\n\n\
57The -q flag specifies a quieter print-out, without any headers.\n\
58The -b flag is used if the files are to be compared as binary files, and this\n\
59is also the default. The -t flag is used if the files are to be compared as\n\
65#define HIGHEST_CHECK 32714
72 char file_open_mode[10];
73 if (open_as_a_text_file) strcpy(file_open_mode,
"rt");
74 else strcpy(file_open_mode,
"rb");
75 FILE *opened_file = fopen(file_name.
s(), file_open_mode);
79 if (!opened_file)
return common::NOT_FOUND;
80 int characters_read = 0;
81 int current_checksum_value = 0;
83 while (!feof(opened_file)) {
84 characters_read = int(fread(buffer_chunk,
sizeof(
char),
buffer_size,
90 if (characters_read <= 0) {
91 if (characters_read < 0) current_checksum_value = -1;
92 else if (current_checksum_value == 0) current_checksum_value = -1;
95 current_checksum_value = (current_checksum_value
99 LOG(
a_sprintf(
"current checksum=%d", current_checksum_value));
103 return int(current_checksum_value);
111 char file_open_mode[10];
112 if (open_as_a_text_file) strcpy(file_open_mode,
"rt");
113 else strcpy(file_open_mode,
"rb");
114 FILE *opened_file = fopen(file_name.
s(), file_open_mode);
118 if (!opened_file)
return common::NOT_FOUND;
119 int characters_read = 0;
120 int current_checksum_value = 0;
122 while (!feof(opened_file)) {
123 characters_read = int(fread(buffer_chunk,
sizeof(
char),
buffer_size,
129 if (characters_read <= 0) {
130 if (characters_read < 0) current_checksum_value = -1;
131 else if (current_checksum_value == 0) current_checksum_value = -1;
135 ((
uint16)current_checksum_value, (
abyte *)buffer_chunk,
138 LOG(
a_sprintf(
"current checksum=%d", current_checksum_value));
142 return current_checksum_value;
145int main(
int argc,
char *argv[])
148 bool open_file_as_text =
false;
150 bool show_header =
true;
156 if (cmds.
find(
'b', index)) open_file_as_text =
false;
158 if (cmds.
find(
't', index)) open_file_as_text =
true;
160 if (cmds.
find(
'q', index)) show_header =
false;
165 bool printed_header =
false;
167 for (
int entry = 0; entry < cmds.
entries(); entry++) {
170 if (!printed_header) {
171 printed_header =
true;
174 printf(
"bizarro fletcher filename\n");
175 printf(
"======= ======== ========\n");
179 int checksum_of_file =
do_checksum(name, open_file_as_text);
181 if (checksum_of_file >= 0) {
182 printf(
"%s",
a_sprintf(
" %05d 0x%04x %s\n", checksum_of_file,
183 fletcher_chksum, name.s()).
s());
185 printf(
"%s",
a_sprintf(
"%s is inaccessible.\n", name.s()).
s());
int main(int argc, char *argv[])
int do_checksum(const astring &file_name, int open_as_a_text_file)
int do_fletcher_checksum(const astring &file_name, int open_as_a_text_file)
int print_instructions(bool good, const astring &program_name)
int entries() const
Returns the number of fields found on the command line.
bool find(char option_character, int &index, bool case_sense=true) const
Returns true if the "option_character" is found in the parameters.
const command_parameter & get(int field) const
Returns the parameter at the "field" specified.
const basis::astring & text() const
observes the string contents.
parameter_types type() const
observes the type of the parameter.
a_sprintf is a specialization of astring that provides printf style support.
Provides a dynamically resizable ASCII character string.
const char * s() const
synonym for observe. the 's' stands for "string", if that helps.
static basis::un_short rolling_fletcher_checksum(basis::un_short previous, const basis::abyte *data, int len)
Fletcher checksums applied to streaming data.
static unsigned int bizarre_checksum(const basis::abyte *data, int length)
A different type of checksum with somewhat unknown properties.
static basis::astring notarize(bool add_space=true)
a useful method for getting a textual version of the time "right now".
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
unsigned char abyte
A fairly important unit which is seldom defined...
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.