-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiVaultSearch.cpp
91 lines (74 loc) · 2.95 KB
/
MultiVaultSearch.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <fstream>
#include <filesystem>
#include <vector>
#include <string>
#include <regex>
#include <Windows.h>
namespace fs = std::filesystem;
std::string directory_path_to_test = "";
std::regex regex_pattern_to_test("#[\\w]+"); // everything beginning with a hashtag
void findMarkdownFiles(const fs::path &directory_path, std::vector<fs::path> &markdown_files) {
try {
// Iterate over the directory
for (const auto &entry: fs::directory_iterator(directory_path)) {
if (fs::is_directory(entry)) {
// If the entry is a subdirectory, recursively search it
findMarkdownFiles(entry, markdown_files);
} else if (entry.path().extension() == ".md") {
// If the entry is a Markdown file, add its path to the vector
markdown_files.push_back(entry.path());
}
}
} catch (const fs::filesystem_error &ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
// Function to search for lines containing "#Sometext" in a Markdown file
void searchForTextInMarkdown(const fs::path &markdown_file) {
try {
std::ifstream file(markdown_file);
if (file.is_open()) {
std::string line;
int line_number = 0;
while (std::getline(file, line)) {
line_number++;
std::regex pattern(regex_pattern_to_test);
std::smatch match;
// Check if the line contains the search text
if (std::regex_search(line, match, pattern)) {
std::cout << "Found in file: " << markdown_file << " (line " << line_number << "):\n" << line
<< std::endl;
}
}
file.close();
} else {
std::cerr << "Unable to open file: " << markdown_file << std::endl;
}
} catch (const std::exception &ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
int not_main() {
// WINDOWS UTF-8 Charset workaround :(
// see: https://stackoverflow.com/questions/45575863/how-to-print-utf-8-strings-to-stdcout-on-windows
SetConsoleOutputCP(CP_UTF8);
setvbuf(stdout, nullptr, _IOFBF, 1000);
std::vector<fs::path> markdown_files;
try {
// Check if the provided path exists and is a directory
if (fs::is_directory(directory_path_to_test)) {
// Find Markdown files recursively within the directory
findMarkdownFiles(directory_path_to_test, markdown_files);
// Search for the specified text in each Markdown file
for (const auto &markdown_file: markdown_files) {
searchForTextInMarkdown(markdown_file);
}
} else {
std::cerr << "Provided path is not a directory." << std::endl;
}
} catch (const fs::filesystem_error &ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
return 0;
}