-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsearch.cpp
More file actions
46 lines (37 loc) · 1.21 KB
/
search.cpp
File metadata and controls
46 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "search.h"
#include <iostream>
#include <fstream>
#include <string>
#include <mutex>
#include <regex>
using namespace std;
mutex coutMutex;
const string BOLD_YELLOW = "\033[1;33m";
const string RESET = "\033[0m";
void searchInFile(const string& filename, const regex& pattern) {
ifstream file(filename);
if (!file.is_open()) return;
string line;
int lineNumber = 1;
while (getline(file, line)) {
sregex_iterator iter(line.begin(), line.end(), pattern);
sregex_iterator end;
if(iter != end) {
string highlightedLine = "";
size_t start = 0;
while (iter != end) {
smatch match = *iter;
highlightedLine += line.substr(start, match.position() - start);
highlightedLine += BOLD_YELLOW + match.str() + RESET;
start = match.position() + match.length();
++iter;
}
highlightedLine += line.substr(start);
{
lock_guard<mutex> lock(coutMutex);
cout << filename << " : Line " << lineNumber << " -> " << highlightedLine << endl;
}
}
lineNumber++;
}
}