Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <fstream>
- #include <dirent.h>
- #include "llist.h"
- using namespace std;
- bool ParseParameters(int &argc,char *argv[],char *dir, string &needle, bool &recursive);
- void ListDir(char *dir, string &needle, llist *lastObj, bool recursive, int indent);
- void ReadFile(char *filename, string &needle, llist *lastObj);
- void PrintList(llist *startPoint);
- int main(int argc,char *argv[]){
- string needle;
- char dir[256];
- bool recursive = false;
- llist *startPoint, *lastObject;
- startPoint = new llist;
- startPoint->next = 0;
- lastObject = startPoint;
- if(ParseParameters(argc,argv,dir,needle,recursive)){
- cout << "listing dir: " << dir <<endl;
- if (recursive) cout << "Using recursive..." << endl;
- ListDir(dir, needle, lastObject, recursive, 2);
- PrintList(startPoint);
- return 0;
- }
- else{
- cout << "Failed to parse parameters";
- return 0;
- }
- }
- bool ParseParameters(int &argc,char *argv[],char *dir, string &needle, bool &recursive){
- int i=0;
- int err=2;
- while(i+1 != argc && i != argc){
- if(i == 0) i++;
- if(strcmp(argv[i], "-d") == 0){
- strcpy(dir, argv[i+1]);
- i+=2;
- err--;
- }
- else if(strcmp(argv[i], "-s") == 0){
- needle = argv[i+1];
- i+=2;
- err--;
- }else if(strcmp(argv[i], "-R") == 0){
- recursive = true;
- i++;
- }
- else{
- cout << "unknown switch " << argv[i] << endl;
- return false;
- }
- }
- if(i!=0 && err == 0) return true;
- else {
- cout << "too few arguments" << endl;
- return false;
- }
- }
- void ListDir(char *dir, string &needle, llist *lastObj, bool recursive, int indent){
- DIR *dp, *tdp;
- struct dirent *ep;
- char tmpObj[256];
- if (dir[strlen(dir)-1] != '/') strcat(dir,"/");
- dp = opendir(dir);
- if (dp != NULL){
- while (ep = readdir(dp)){
- strcpy(tmpObj,dir);
- strcat(tmpObj,ep->d_name);
- if(strcmp(ep->d_name,".") != 0 && strcmp(ep->d_name,"..") != 0){
- tdp = opendir(tmpObj);
- if (tdp != NULL){
- closedir(tdp);
- tdp = NULL;
- for(int i=0; i<indent; i++) cout << '-';
- cout << "(dir)" << ep->d_name << endl;
- if (recursive) ListDir(tmpObj, needle, lastObj, recursive, indent+2);
- }
- else{
- for(int i=0; i<indent; i++) cout << '-';
- cout << ep->d_name << endl;
- ReadFile(tmpObj, needle, lastObj);
- }
- tmpObj[0] = '\0';
- }
- }
- closedir(dp);
- }
- }
- void ReadFile(char *filename, string &needle, llist *lastObj){
- string line;
- int lineNum=0,pos,matches=0;
- ifstream file(filename);
- while (file.good()){
- getline(file, line);
- lineNum++;
- pos = line.find(needle);
- if (pos != string::npos){
- if(matches == 0) strcpy(lastObj->file,filename);
- lastObj->pos[matches][0] = lineNum;
- lastObj->pos[matches][1] = pos+1;
- //cout << "Match at " << filename << ' ' << lastObj->pos[matches][0] << ':' << lastObj->pos[matches][1] <<endl;
- lastObj->matches = ++matches;
- }
- }
- file.close();
- if (matches != 0){
- lastObj->next = new llist;
- lastObj = lastObj->next;
- lastObj->next = 0;
- }
- }
- void PrintList(llist *node){
- while (node->next != 0){
- if (node->matches != 0){//if this is the last element it is most likely to be empty
- cout << "In file: " << node->file << endl;
- for (int i=0;i <= node->matches;i++){
- cout << node->pos[i][0] << ':' << node->pos[i][1] << endl;
- }
- }
- node = node->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment