///////////////////////// // // This program reads in a list of hash values and stores them in a // vector. It then reads in a second list of hashes and compares each // to the first list. If any match, a message is output that says so. #include #include #include #include #include using namespace std; void print_hash_vector(vector ); vector load_hashes(); void print_case_vectors(vector , vector ); void load_case_file( vector &, vector &); void print_instructions(); int compare_hashes_with_case (vector , vector , vector ); int main (){ vector case_names, case_hashes, known_hashes; int matches = 0; char input; bool done = 0; // Loop until someone hits q and done is set true while (!done){ print_instructions(); cin >> input; switch (input){ case 'H': case 'h': known_hashes = load_hashes(); break; case'C': case 'c': load_case_file(case_names, case_hashes); break; case 'M': case 'm': matches = compare_hashes_with_case (case_names, case_hashes, known_hashes ); cout << "Number of matches: " << matches << endl; break; case'D': case 'd': cout << "Hash Set: " << endl; print_hash_vector(known_hashes); cout << endl << endl << endl << endl; cout <<"Case file: " << endl; print_case_vectors(case_names, case_hashes); cout << endl; break; case 'Q': case 'q': done = 1; break; default: cout << "Invalid command character" << endl; } } return 0; } ////////////////////////////////////// // This function takes an ifstream and // loads value from it into a vector of strings vector load_hashes() { vector thevector; string current_value, headers; string hashfilename; ifstream inputfile; // Get the filename to be used cout << "Please enter the name of the hash file: "; cin >> hashfilename; // Try and open that file. Die if it fails. inputfile.open(hashfilename.c_str()); if (!inputfile){ cerr << "File: " << hashfilename << " not found. Aborting." << endl; exit(1); } // now get rid of the 3 headerlines for (int i=0; i < 3; i++) getline(inputfile, headers); // Read the remaining values and enter them into the vector while (inputfile >> current_value){ thevector.push_back(current_value); } return thevector; } ///////////////////////////////////// // this fuction prints a vector of strings, // one entry to a line void print_hash_vector(vector hashes) { for (int i = 0; i < hashes.size(); i++){ cout << hashes[i] << endl; } } ///////////////////////////////////// // Take a couple of vectors as reference parameters, // ask for a file name, read it, and load the contents // of the file into the two vectors void load_case_file(vector & names,vector & hashes){ string casefilename; ifstream inputfile; string headers, current_file_string, current_hash_string; // Get the filename to be used cout << "Please enter the name of the file : "; cin >> casefilename; // Try and open that file. Die if it fails. inputfile.open(casefilename.c_str()); if (!inputfile){ cerr << "File: " << casefilename << " not found. Aborting." << endl; exit(1); } // now get rid of the 3 headerlines for (int i=0; i < 3; i++) getline(inputfile, headers); // Read the remaining values and enter them into the vector while (inputfile >> current_file_string >> current_hash_string){ names.push_back(current_file_string); hashes.push_back(current_hash_string); } } ///////////////////////////////////// // Print the vectors associated with the case // void print_case_vectors(vector files, vector hashes){ for (int i = 0; i < files.size(); i++){ cout << files[i] << " " << hashes[i] << endl; } } ///////////////////////////////////// // Loop over the vector of case hashes. For each hash, // loop over the known hashes. If they match, print the file // name and matching hash. int compare_hashes_with_case (vector file_names, vector file_hashes, vector known_hashes){ int matches = 0; for (int i = 0; i < file_names.size();i++){ for (int j = 0; j < known_hashes.size();j++){ if (file_hashes[i] == known_hashes[j]){ cout << "File: " << file_names[i] << " matches known hashes with hash: " << known_hashes[j] << endl; matches++; } } } return matches; } void print_instructions(){ cout << "This program reads a file containing a set of hashes, " << endl; cout << "then reads a second file consisting of filenames and hashes. " << endl; cout << "It will then identify each file from the second file that match " << endl; cout << "a hash from the first file." << endl << endl; cout << "Menu choices:" << endl; cout << left; cout << setw(5) << "H" << setw(45) << "Load the hash set" << endl; cout << setw(5) << "C" << setw(45) << "Load the case file" << endl; cout << setw(5) << "M" << setw(45) << "Compare the hash set and case file" << endl; cout << setw(5) << "D" << setw(45) << "Print hash set and case file" << endl; cout << setw(5) << "" << setw(45) << "(for debugging)" << endl; cout << setw(5) << "Q" << setw(45) << "Quit the program" << endl; cout << endl << endl << "Your choice: "; }