Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <boost/filesystem.hpp>
  3. #include <boost/range/iterator_range.hpp>
  4. #include <string>
  5. #include <vector>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <boost/algorithm/string/predicate.hpp>
  9. using namespace boost::filesystem;
  10. using namespace std;
  11.  
  12.  
  13. //determines whether a file is a .txt file or not
  14. bool isTxtFile(string a){
  15.   string tmp = "";
  16.   int length = a.length();
  17.   if(length < 4){
  18.     return false;
  19.   }else {
  20.     for (int j = length-4; j < length; j++){
  21.       tmp += a[j];
  22.     }
  23.     return (tmp == ".txt");
  24.   }
  25. }
  26. //returns a string vector containing all .txt files in current directory
  27. vector<string> filesToSearch(){
  28.   vector<string> file_names;
  29.   path p(".");
  30.   for (directory_iterator itr(p); itr!=directory_iterator(); ++itr)
  31.      {
  32.        if (isTxtFile(itr->path().filename().string())){
  33.        file_names.push_back(itr->path().filename().string());
  34.       }
  35.     }
  36.   return file_names;
  37. }
  38.  
  39. void searchFiles(vector<string> searchFiles, string keyword){
  40.   int line_count = 0;
  41.   int word_count = 0;
  42.   int file_count = searchFiles.size();
  43.   bool word_found = false;
  44.  
  45.   for (int i = 0; i < file_count; i++){
  46.     string line_string;
  47.     ifstream f(searchFiles[i]);
  48.     if (f.is_open()){
  49.       while (!f.eof()){
  50.         getline(f, line_string), line_count++;
  51.         istringstream isis(line_string);
  52.         string tmp_word;
  53.         while (isis >> tmp_word){
  54.           if (boost::iequals(tmp_word, keyword)){
  55.             if (!word_found){
  56.               cout << keyword << ":" << endl;
  57.               word_found = true;
  58.             }
  59.             word_count++;
  60.           }isis.clear();
  61.         }
  62.         if (word_count == 1){
  63.         cout << "\tFound in \"" << searchFiles[i] << "\" at line " << line_count << endl;
  64.         word_count = 0;
  65.       }else if(word_count > 1){
  66.         cout << "\tFound in \"" << searchFiles[i] << "\" at line " << line_count
  67.              << " (" << word_count << " times)" << endl;
  68.         word_count = 0;
  69.         }
  70.       }line_count = 0, word_count = 0, f.close();
  71.     }else{ cout << "Could not open file." << endl;}
  72.  
  73.   }
  74.   if (!word_found){
  75.     cout << keyword << ":" << endl;
  76.     cout << "\t not found" << endl;
  77.   }
  78. }
  79.  
  80. void makeIndex(vector<string> files);
  81.  
  82.  
  83. int main(int argc, char** argv){
  84.  
  85.   if (argc < 2){
  86.     cout << "No keywords provided." << endl;
  87.     return -1;
  88.   }
  89.  
  90.   vector<string> txtFiles = filesToSearch();
  91.   if (txtFiles.size() < 1) cout << "No txt files to search." << endl, exit(0);
  92.  
  93.   if (strcmp(argv[1],"--make-index") == 0){
  94.     cout << "ter" << endl;
  95.   }else{
  96.     for (int i = 0; i < argc-1; i++){
  97.       searchFiles(txtFiles, argv[i+1]);
  98.     }
  99.   }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.   return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement