-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiVaultHandler.cpp
280 lines (224 loc) · 9.19 KB
/
MultiVaultHandler.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <regex>
#include <Windows.h>
#include "MultiVaultHandler.h"
#include <nfd.h>
#include <stdio.h>
#include <stdlib.h>
const std::string vault_file_name = "vault_paths.txt";
MultiVaultHandler::MultiVaultHandler() {
// read file of existing vault paths
std::ifstream vault_file(vault_file_name);
if (!vault_file || vault_file.bad() || vault_file.fail()) {
std::cout << "Failed to read existing paths" << std::endl;
} else if (vault_file.good()) {
for (std::string line; getline(vault_file, line);) {
std::cout << line << std::endl;
try {
this->obsidian_vaults_path.emplace_back(fs::path(line.c_str()));
}
catch (std::filesystem::filesystem_error const &ex) {
std::cout << "caught an error with file " << line << ex.what() << std::endl;
}
}
}
for (const auto &path: obsidian_vaults_path) {
std::cout << path << std::endl;
}
// create the hashmap
this->createHashmapWithHashtags();
}
MultiVaultHandler::~MultiVaultHandler() {
std::cout << "saving paths" << std::endl;
for (const auto &path: obsidian_vaults_path) {
std::cout << "path " << path << std::endl;
}
std::fstream vault_paths;
vault_paths.open(vault_file_name, std::ios::out);
if (!vault_paths) {
std::cout << "Error saving vault paths";
} else {
for (const auto &path: obsidian_vaults_path) {
vault_paths << path.string() << std::endl;
}
vault_paths.close();
}
// Free memory
obsidian_vaults_path.clear();
markdown_files.clear();
results_hash_map.clear();
}
bool MultiVaultHandler::createHashmapWithHashtags() {
auto iterator = this->obsidian_vaults_path.begin();
while (iterator != this->obsidian_vaults_path.end()) {
if (!std::filesystem::is_directory(*iterator)) {
std::cout << "deleting vault path is not valid " << *iterator << std::endl;
iterator = this->obsidian_vaults_path.erase(
iterator); // delete this entry NOTE: this seems to be a slow operation since vector is shifted everytime
continue;
}
this->findMarkdownFiles(*iterator, 0);
iterator++;
}
for (const auto &file: this->markdown_files) {
std::vector<obsidian_result > results_vector = this->searchForTextInMarkdown(file);
for (const auto &result: results_vector) {
results_hash_map[result.hashtag].emplace_back(result);
}
}
for (const auto &pair: this->results_hash_map) {
const std::string &key = pair.first;
const std::vector<obsidian_result > &values = pair.second;
std::cout << "Key: " << key << std::endl;
for (const auto &item: values) {
std::cout << " Path: " << item.path << ", Line Number: " << item.line_number << "Hashtag: "
<< item.hashtag << std::endl;
}
}
return true;
}
void MultiVaultHandler::findMarkdownFiles(const fs::path &directory_path, uint16_t depth) {
if (depth > MAX_DEPTH) {
std::cout << "max depth reached " << std::endl;
return;
}
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
this->findMarkdownFiles(entry, depth++);
} else if (entry.path().extension() == ".md") {
// If the entry is a Markdown file, add its path to the vector
this->markdown_files.push_back(entry.path());
}
}
} catch (const fs::filesystem_error &ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
std::vector<obsidian_result > MultiVaultHandler::searchForTextInMarkdown(const fs::path &markdown_file) {
auto return_vector = std::vector<obsidian_result >{};
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("#[\\w]+");
std::regex pattern("\\W(\\#[a-zA-Z]+\\b)");
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;
for (size_t i = 0; i < match.size(); ++i) {
obsidian_result new_obrs = obsidian_result{};
new_obrs.line_number = line_number;
new_obrs.path = fs::path(markdown_file).string();
new_obrs.hashtag = match[i];
new_obrs.line = line;
return_vector.push_back(new_obrs);
}
}
}
file.close(); // very important to close the file again
} else {
std::cerr << "Unable to open file: " << markdown_file << std::endl;
}
} catch (const std::exception &ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
return return_vector;
}
bool MultiVaultHandler::addFolderPath() {
char *folder_path = nullptr;
nfdresult_t result = NFD_PickFolder(nullptr, &folder_path);
if (result == NFD_OKAY) {
puts("Success!");
puts(folder_path);
fs::path new_path = folder_path;
//
obsidian_vaults_path.push_back(new_path);
// remove duplicates
std::sort(obsidian_vaults_path.begin(), obsidian_vaults_path.end());
obsidian_vaults_path.erase(std::unique(obsidian_vaults_path.begin(), obsidian_vaults_path.end()),
obsidian_vaults_path.end());
free(folder_path);
} else if (result == NFD_CANCEL) {
puts("User pressed cancel.");
} else {
printf("Error: %s\n", NFD_GetError());
}
this->createHashmapWithHashtags();
return false;
}
std::unordered_map<std::string, std::vector<obsidian_result > > MultiVaultHandler::getResults() {
return this->results_hash_map;
}
std::vector<std::filesystem::path> MultiVaultHandler::getVaultPaths() {
return this->obsidian_vaults_path;
}
std::vector<obsidian_result> MultiVaultHandler::searchForSearchTerm(const std::string &searchTerm){
auto return_vector = std::vector<obsidian_result>{};
std::cout << "searching for " << searchTerm << std::endl;
for(auto const& current_file : this->markdown_files){
try {
std::ifstream file(current_file);
if (file.is_open()) {
std::string line;
int line_number = 0;
while (std::getline(file, line)) {
line_number++;
if (line.find(searchTerm) != std::string::npos) {
std::cout << "Word found on line " << line_number << ": " << line << std::endl;
auto new_obrs = obsidian_result{};
new_obrs.line_number = line_number;
new_obrs.path = fs::path(current_file).string();
new_obrs.hashtag = searchTerm;
new_obrs.line = line;
return_vector.push_back(new_obrs);
}
}
file.close(); // very important to close the file again
} else {
std::cerr << "Unable to open file: " << current_file << std::endl;
}
} catch (const std::exception &ex) {
std::cerr << "Error: " << ex.what() << std::endl;
}
}
return return_vector;
}
std::vector<obsidian_result> MultiVaultHandler::searchForHashtags(const std::string &search) {
if (search[0] == '#') {
//std::string search_string_w_hashtag = "#" + search;
auto searchResult = this->results_hash_map.find(search);
std::vector<obsidian_result> return_results = {};
if (searchResult == results_hash_map.end()) {
std::cout << "could not find the given key in the files " << std::endl;
return return_results;
}
for (const auto& vector_iter: searchResult->second) {
obsidian_result res = obsidian_result{vector_iter.path, vector_iter.line_number, vector_iter.hashtag,
vector_iter.line};
std::cout << "Found in file " << vector_iter.path << std::endl;
return_results.push_back(res);
}
return return_results;
}
else{
// Note this is not cached
// is this bad code?
std::cout << "searching for non hashtag search term" << std::endl;
return this->searchForSearchTerm(search);
}
}
void MultiVaultHandler::deletePath(fs::path inputPath) {
auto iterator = std::find(this->obsidian_vaults_path.begin(), this->obsidian_vaults_path.end(), inputPath);
obsidian_vaults_path.erase(iterator);
results_hash_map = {};
}