Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using std::ios;
- using std::ifstream;
- using std::ofstream;
- #include <string>
- using std::string;
- #include <unordered_map>
- using std::unordered_map;
- #include <vector>
- using std::vector;
- void loadHosts(unordered_map<string, size_t>& target,
- const char* sourceFile);
- void outputHosts(const unordered_map<string, size_t>& source,
- const char* outputDirectory);
- int main(
- #ifndef _DEBUG
- const int argc, const char* argv[]
- #endif
- )
- {
- #ifdef _DEBUG
- const char* argv[] = { __FILE__, "C:/hosts1.txt" , "C:/hosts2.txt",
- "C:/hosts3.txt", "C:/"};
- int argc = 5;
- #endif
- if (argc < 2)
- {
- std::cout << "Usage: "
- << argv[0]
- << " <hosts_file_1.txt> ... <hosts_file_n.txt> <output_folder>\n\n";
- return 1;
- }
- vector<const char*> inputFilenames;
- for (size_t argIdx = 1; argIdx < argc - 1; ++argIdx)
- {
- inputFilenames.push_back(argv[argIdx]);
- }
- unordered_map<string, size_t> counter;
- for (const char* sourceFilepath : inputFilenames)
- {
- loadHosts(counter, sourceFilepath);
- }
- #ifdef _DEBUG
- ofstream log("log.txt", ios::trunc);
- for (const auto& KVP : counter)
- {
- if(KVP.second > 1)
- {
- log << KVP.first << " " << KVP.second << '\n';
- }
- }
- log.close();
- #endif
- outputHosts(counter, argv[argc - 1]);
- return 0;
- }
- void loadHosts(unordered_map<string, size_t>& target,
- const char* sourceFilepath)
- {
- ifstream inFile(sourceFilepath);
- if (!inFile.good())
- {
- std::cout << "\nCould not open "
- << sourceFilepath
- << " for reading!\n";
- return;
- }
- while (!inFile.eof())
- {
- string line;
- inFile >> line;
- if (line == "0.0.0.0" || line == "0")
- {
- inFile >> line;
- ++target[line];
- }
- else
- {
- inFile.ignore(UINT64_MAX, '\n');
- }
- }
- if (inFile.bad())
- {
- std::cout << "\nError processing " << sourceFilepath << '\n';
- }
- else
- {
- std::cout << '\n' << sourceFilepath << " loaded successfully!\n";
- }
- }
- void outputHosts(const unordered_map<string, size_t>& source,
- const char* outputDirectory)
- {
- string outputFile = outputDirectory;
- outputFile += "hosts";
- ofstream outFile(outputFile, ios::trunc);
- if (!outFile.good())
- {
- std::cout << "\nCould not open new file for writing hosts!\n";
- return;
- }
- for (const auto& KVP : source)
- {
- outFile << "0.0.0.0 "
- << KVP.first
- << '\n';
- }
- if (outFile.bad())
- {
- std::cout << "\nError processing output hosts file!\n";
- }
- else
- {
- std::cout << "\nSuccess! Output file: "
- << outputFile << "\n\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement