Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using std::ios;
  4. using std::ifstream;
  5. using std::ofstream;
  6. #include <string>
  7. using std::string;
  8. #include <unordered_map>
  9. using std::unordered_map;
  10. #include <vector>
  11. using std::vector;
  12.  
  13. void loadHosts(unordered_map<string, size_t>& target,
  14. const char* sourceFile);
  15. void outputHosts(const unordered_map<string, size_t>& source,
  16. const char* outputDirectory);
  17.  
  18. int main(
  19. #ifndef _DEBUG
  20. const int argc, const char* argv[]
  21. #endif
  22. )
  23. {
  24. #ifdef _DEBUG
  25. const char* argv[] = { __FILE__, "C:/hosts1.txt" , "C:/hosts2.txt",
  26. "C:/hosts3.txt", "C:/"};
  27. int argc = 5;
  28. #endif
  29.  
  30. if (argc < 2)
  31. {
  32. std::cout << "Usage: "
  33. << argv[0]
  34. << " <hosts_file_1.txt> ... <hosts_file_n.txt> <output_folder>\n\n";
  35. return 1;
  36. }
  37.  
  38. vector<const char*> inputFilenames;
  39. for (size_t argIdx = 1; argIdx < argc - 1; ++argIdx)
  40. {
  41. inputFilenames.push_back(argv[argIdx]);
  42. }
  43.  
  44. unordered_map<string, size_t> counter;
  45. for (const char* sourceFilepath : inputFilenames)
  46. {
  47. loadHosts(counter, sourceFilepath);
  48. }
  49.  
  50. #ifdef _DEBUG
  51. ofstream log("log.txt", ios::trunc);
  52. for (const auto& KVP : counter)
  53. {
  54. if(KVP.second > 1)
  55. {
  56. log << KVP.first << " " << KVP.second << '\n';
  57. }
  58. }
  59. log.close();
  60. #endif
  61.  
  62. outputHosts(counter, argv[argc - 1]);
  63.  
  64. return 0;
  65. }
  66.  
  67. void loadHosts(unordered_map<string, size_t>& target,
  68. const char* sourceFilepath)
  69. {
  70. ifstream inFile(sourceFilepath);
  71. if (!inFile.good())
  72. {
  73. std::cout << "\nCould not open "
  74. << sourceFilepath
  75. << " for reading!\n";
  76. return;
  77. }
  78.  
  79. while (!inFile.eof())
  80. {
  81. string line;
  82. inFile >> line;
  83.  
  84. if (line == "0.0.0.0" || line == "0")
  85. {
  86. inFile >> line;
  87. ++target[line];
  88. }
  89. else
  90. {
  91. inFile.ignore(UINT64_MAX, '\n');
  92. }
  93. }
  94.  
  95. if (inFile.bad())
  96. {
  97. std::cout << "\nError processing " << sourceFilepath << '\n';
  98. }
  99. else
  100. {
  101. std::cout << '\n' << sourceFilepath << " loaded successfully!\n";
  102. }
  103. }
  104.  
  105. void outputHosts(const unordered_map<string, size_t>& source,
  106. const char* outputDirectory)
  107. {
  108. string outputFile = outputDirectory;
  109. outputFile += "hosts";
  110. ofstream outFile(outputFile, ios::trunc);
  111. if (!outFile.good())
  112. {
  113. std::cout << "\nCould not open new file for writing hosts!\n";
  114. return;
  115. }
  116.  
  117. for (const auto& KVP : source)
  118. {
  119. outFile << "0.0.0.0 "
  120. << KVP.first
  121. << '\n';
  122. }
  123.  
  124. if (outFile.bad())
  125. {
  126. std::cout << "\nError processing output hosts file!\n";
  127. }
  128. else
  129. {
  130. std::cout << "\nSuccess! Output file: "
  131. << outputFile << "\n\n";
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement