| 1 |
#ifndef FILEGREP_H |
|---|
| 2 |
#define FILEGREP_H |
|---|
| 3 |
|
|---|
| 4 |
#include <vector> |
|---|
| 5 |
#include <string> |
|---|
| 6 |
#include "pme-1.0.4/pme.h" |
|---|
| 7 |
#include "MainFrm.h" |
|---|
| 8 |
|
|---|
| 9 |
using namespace std; |
|---|
| 10 |
|
|---|
| 11 |
struct Listing |
|---|
| 12 |
{ |
|---|
| 13 |
string file; |
|---|
| 14 |
string match; |
|---|
| 15 |
int lineNumber; |
|---|
| 16 |
|
|---|
| 17 |
}; |
|---|
| 18 |
|
|---|
| 19 |
typedef vector < vector< string > > str_vec_2d; |
|---|
| 20 |
|
|---|
| 21 |
class FileGrep |
|---|
| 22 |
{ |
|---|
| 23 |
public: |
|---|
| 24 |
|
|---|
| 25 |
int nestingLevel; |
|---|
| 26 |
int nestingLimit; |
|---|
| 27 |
|
|---|
| 28 |
str_vec_2d undoFileContents; |
|---|
| 29 |
str_vec_2d undoFileNames; |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
FileGrep(int limit = 10 ) : nestingLimit(limit), nestingLevel(0) {} // this is for how many undo operations you want to support |
|---|
| 34 |
|
|---|
| 35 |
vector<Listing> GrepFiles(const vector<string> &files, const string& regex, const string& options = "" ) |
|---|
| 36 |
{ |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
PME reg(regex, options); |
|---|
| 40 |
|
|---|
| 41 |
vector<Listing> listings; |
|---|
| 42 |
Listing listing; |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
for ( int i = 0 ; i < files.size(); i++ ) |
|---|
| 47 |
{ |
|---|
| 48 |
|
|---|
| 49 |
int lineCount = 1; |
|---|
| 50 |
string line; |
|---|
| 51 |
ifstream in(files[i].c_str() ); |
|---|
| 52 |
while ( !in.eof () ) |
|---|
| 53 |
{ |
|---|
| 54 |
std::getline(in,line); |
|---|
| 55 |
//AfxMessageBox(string("line [ " + ITOA(lineCount) + "] " + line).c_str()); |
|---|
| 56 |
if ( reg.match(line) > 0 ) |
|---|
| 57 |
{ |
|---|
| 58 |
listing.match = line; |
|---|
| 59 |
listing.file = files[i]; |
|---|
| 60 |
listing.lineNumber = lineCount; |
|---|
| 61 |
listings.push_back(listing); |
|---|
| 62 |
} |
|---|
| 63 |
lineCount++; |
|---|
| 64 |
|
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
return listings; |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
vector<Listing> GrepReplaceFiles(const vector<string> &files, const string& regex, const string& options , const string& replaceRegex) |
|---|
| 76 |
{ |
|---|
| 77 |
|
|---|
| 78 |
int i = 0; |
|---|
| 79 |
|
|---|
| 80 |
vector<string> fileContents; |
|---|
| 81 |
vector<string> backupContents; |
|---|
| 82 |
vector<string> fileNames; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
vector<Listing> listings; |
|---|
| 86 |
Listing listing; |
|---|
| 87 |
|
|---|
| 88 |
for ( i = 0 ; i < files.size(); i ++ ) |
|---|
| 89 |
{ |
|---|
| 90 |
//AfxMessageBox(ReadFile(files[i]).c_str() ); |
|---|
| 91 |
backupContents.push_back(ReadFile(files[i])); |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
PME reg(regex, options); |
|---|
| 95 |
//--- SAVE ALL FILES --- // |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
for ( i = 0 ; i < files.size(); i ++ ) |
|---|
| 100 |
{ |
|---|
| 101 |
int lineCount = 1; |
|---|
| 102 |
string line = ""; |
|---|
| 103 |
string data = ""; |
|---|
| 104 |
|
|---|
| 105 |
ifstream in(files[i].c_str() ); |
|---|
| 106 |
while ( !in.eof () ) |
|---|
| 107 |
{ |
|---|
| 108 |
std::getline(in,line); |
|---|
| 109 |
//AfxMessageBox(line.c_str()); |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
if ( reg.match(line) > 0 ) |
|---|
| 113 |
{ |
|---|
| 114 |
|
|---|
| 115 |
listing.file = files[i]; |
|---|
| 116 |
listing.lineNumber = lineCount; |
|---|
| 117 |
|
|---|
| 118 |
//AfxMessageBox(string("line before : " + line).c_str()); |
|---|
| 119 |
line = reg.sub( line, replaceRegex); |
|---|
| 120 |
//AfxMessageBox(string("line after : " + line).c_str()); |
|---|
| 121 |
listing.match = line; |
|---|
| 122 |
|
|---|
| 123 |
listings.push_back(listing); |
|---|
| 124 |
|
|---|
| 125 |
data += line + "\n"; |
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
} |
|---|
| 129 |
else { |
|---|
| 130 |
data += line + "\n"; |
|---|
| 131 |
} |
|---|
| 132 |
lineCount++; |
|---|
| 133 |
|
|---|
| 134 |
} |
|---|
| 135 |
in.close(); |
|---|
| 136 |
fileNames.push_back(files[i]); |
|---|
| 137 |
fileContents.push_back(data); |
|---|
| 138 |
|
|---|
| 139 |
//AfxMessageBox(data.c_str()); |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
for ( i = 0 ; i < fileContents.size(); i ++ ) |
|---|
| 143 |
{ |
|---|
| 144 |
string fileName = files[i]; |
|---|
| 145 |
ofstream out(fileName.c_str()); |
|---|
| 146 |
out << fileContents[i]; |
|---|
| 147 |
//AfxMessageBox(fileContents[i].c_str()) |
|---|
| 148 |
; |
|---|
| 149 |
out.close(); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
undoFileContents.push_back(backupContents); |
|---|
| 153 |
undoFileNames.push_back(fileNames); |
|---|
| 154 |
nestingLevel++; |
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
return listings; |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
bool Undo() |
|---|
| 162 |
{ |
|---|
| 163 |
|
|---|
| 164 |
int level = --nestingLevel; |
|---|
| 165 |
if ( level >= 0) |
|---|
| 166 |
{ |
|---|
| 167 |
|
|---|
| 168 |
for ( int i = 0 ; i < undoFileNames[level].size();i++) |
|---|
| 169 |
{ |
|---|
| 170 |
//AfxMessageBox(undoFileContents[level][i].c_str() ); |
|---|
| 171 |
//AfxMessageBox(undoFileNames[level][i].c_str() ); |
|---|
| 172 |
//log(string("undo file name" + undoFileNames[level][i] + " has contents " + undoFileNames[level][i]),__FILE__,__LINE__ ); |
|---|
| 173 |
|
|---|
| 174 |
ofstream out(undoFileNames[level][i].c_str() ); |
|---|
| 175 |
out << undoFileContents[level][i]; |
|---|
| 176 |
out.close(); |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
undoFileNames.pop_back(); |
|---|
| 180 |
undoFileContents.pop_back(); |
|---|
| 181 |
return true; |
|---|
| 182 |
|
|---|
| 183 |
} |
|---|
| 184 |
else { |
|---|
| 185 |
// resset the undo's |
|---|
| 186 |
nestingLevel = 0; |
|---|
| 187 |
undoFileContents.clear(); |
|---|
| 188 |
undoFileNames.clear(); |
|---|
| 189 |
return false; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
} |
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
}; |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
#endif |
|---|