raxbg

guard.cpp

Feb 17th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <fstream>
  4. #include <dirent.h>
  5. #include "llist.h"
  6.  
  7. using namespace std;
  8.  
  9. bool ParseParameters(int &argc,char *argv[],char *dir, string &needle, bool &recursive);
  10. void ListDir(char *dir, string &needle, llist *lastObj, bool recursive, int indent);
  11. void ReadFile(char *filename, string &needle, llist *lastObj);
  12. void PrintList(llist *startPoint);
  13.  
  14. int main(int argc,char *argv[]){
  15.     string needle;
  16.     char dir[256];
  17.     bool recursive = false;
  18.     llist *startPoint, *lastObject;
  19.     startPoint = new llist;
  20.     startPoint->next = 0;
  21.     lastObject = startPoint;
  22.    
  23.     if(ParseParameters(argc,argv,dir,needle,recursive)){
  24.         cout << "listing dir: " << dir <<endl;
  25.         if (recursive) cout << "Using recursive..." << endl;
  26.         ListDir(dir, needle, lastObject, recursive, 2);
  27.         PrintList(startPoint);
  28.         return 0;
  29.     }
  30.     else{
  31.         cout << "Failed to parse parameters";
  32.         return 0;
  33.     }
  34. }
  35.  
  36. bool ParseParameters(int &argc,char *argv[],char *dir, string &needle, bool &recursive){
  37.     int i=0;
  38.     int err=2;
  39.     while(i+1 != argc && i != argc){
  40.         if(i == 0) i++;
  41.         if(strcmp(argv[i], "-d") == 0){
  42.             strcpy(dir, argv[i+1]);
  43.             i+=2;
  44.             err--;
  45.         }
  46.         else if(strcmp(argv[i], "-s") == 0){
  47.             needle = argv[i+1];
  48.             i+=2;
  49.             err--;
  50.         }else if(strcmp(argv[i], "-R") == 0){
  51.             recursive = true;
  52.             i++;
  53.         }
  54.         else{
  55.             cout << "unknown switch " << argv[i] << endl;
  56.             return false;
  57.         }
  58.     }
  59.     if(i!=0 && err == 0) return true;
  60.     else {
  61.         cout << "too few arguments" << endl;
  62.         return false;
  63.     }
  64. }
  65.  
  66. void ListDir(char *dir, string &needle, llist *lastObj, bool recursive, int indent){
  67.     DIR *dp, *tdp;
  68.     struct dirent *ep;
  69.     char tmpObj[256];
  70.     if (dir[strlen(dir)-1] != '/') strcat(dir,"/");
  71.     dp = opendir(dir);
  72.     if (dp != NULL){
  73.         while (ep = readdir(dp)){
  74.             strcpy(tmpObj,dir);
  75.             strcat(tmpObj,ep->d_name);
  76.            
  77.             if(strcmp(ep->d_name,".") != 0 && strcmp(ep->d_name,"..") != 0){
  78.                 tdp = opendir(tmpObj);
  79.                 if (tdp != NULL){
  80.                     closedir(tdp);
  81.                     tdp = NULL;
  82.                     for(int i=0; i<indent; i++) cout << '-';
  83.                     cout << "(dir)" << ep->d_name << endl;
  84.                     if (recursive) ListDir(tmpObj, needle, lastObj, recursive, indent+2);
  85.                 }
  86.                 else{
  87.                     for(int i=0; i<indent; i++) cout << '-';
  88.                     cout << ep->d_name << endl;
  89.                     ReadFile(tmpObj, needle, lastObj);
  90.                 }
  91.                 tmpObj[0] = '\0';
  92.             }
  93.         }
  94.         closedir(dp);
  95.     }
  96. }
  97.  
  98. void ReadFile(char *filename, string &needle, llist *lastObj){
  99.     string line;
  100.     int lineNum=0,pos,matches=0;
  101.     ifstream file(filename);
  102.    
  103.     while (file.good()){
  104.         getline(file, line);
  105.         lineNum++;
  106.         pos = line.find(needle);
  107.         if (pos != string::npos){
  108.             if(matches == 0) strcpy(lastObj->file,filename);
  109.             lastObj->pos[matches][0] = lineNum;
  110.             lastObj->pos[matches][1] = pos+1;
  111.             //cout << "Match at " << filename << ' ' << lastObj->pos[matches][0] << ':' << lastObj->pos[matches][1] <<endl;
  112.             lastObj->matches = ++matches;
  113.         }
  114.     }
  115.     file.close();
  116.     if (matches != 0){
  117.         lastObj->next = new llist;
  118.         lastObj = lastObj->next;
  119.         lastObj->next = 0;
  120.     }
  121. }
  122.  
  123. void PrintList(llist *node){
  124.     while (node->next != 0){
  125.         if (node->matches != 0){//if this is the last element it is most likely to be empty
  126.             cout << "In file: " << node->file << endl;
  127.             for (int i=0;i <= node->matches;i++){
  128.                 cout << node->pos[i][0] << ':' << node->pos[i][1] << endl;
  129.             }
  130.         }
  131.         node = node->next;
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment