-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtagdb.cpp
More file actions
104 lines (94 loc) · 2.79 KB
/
tagdb.cpp
File metadata and controls
104 lines (94 loc) · 2.79 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
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
//
// Created by edmond on 01.11.24.
//
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using std::cout, std::endl;
void help() {
std::ifstream ifs("help.txt");
std::string line;
while(std::getline(ifs, line)) {
cout << line << endl;
}
}
void append(const std::vector<std::string>& content_to_append) {
std::string filename("tagdb.txt");
std::ofstream ofs(filename, std::ios::app);
if(!ofs.is_open()) {
std::cerr << "File could not open!" << std::endl;
return;
}
for(const auto& line : content_to_append) {
ofs << line << endl;
}
}
void search(const std::vector<std::vector<std::string>>& queries) {
std::string filename("tagdb.txt");
std::ifstream ifs(filename);
if(!ifs.is_open()) {
std::cerr << "File could not open!" << std::endl;
exit(-1);
}
std::string line;
while(std::getline(ifs, line)) {
std::stringstream ss(line);
std::string line_tags;
std::getline(ss, line_tags, ' ');
std::vector<std::string> tags;
std::stringstream ss_tags(line_tags);
std::string tag;
while(std::getline(ss_tags, tag, ',' )) {
tags.push_back(tag);
}
bool match = false;
for(const auto& querieTokens : queries) {
bool all_tokens_found = true;
for(const auto& token : querieTokens) {
if(std::find(tags.begin(), tags.end(), token) == tags.end()) {
all_tokens_found = false;
break;
}
}
if(all_tokens_found) {
match = true;
break;
}
}
if(match) {
std::string content;
getline(ss, content);
cout << content << endl;
}
}
}
int main(int argc, char **argv) {
if(argc == 1 || std::string(argv[1]) == "--help" || std::string(argv[1]) == "-h") {
help();
} else if (std::string(argv[1]) == "-a") {
std::vector<std::string> content_to_append;
std::stringstream ss;
for(size_t i = 2; i < argc; ++i) {
ss << argv[i] << " ";
}
content_to_append.push_back(ss.str());
append(content_to_append);
} else if (std::string(argv[1]) == "-q") {
std::vector<std::vector<std::string>> searchQueries;
for(int i = 2; i < argc; i+=2) {
std::string line(argv[i]);
std::stringstream ss(line);
std::string token;
std::vector<std::string> tokens;
while(std::getline(ss, token, ',')) {
tokens.push_back(token);
}
searchQueries.push_back(tokens);
}
search(searchQueries);
}
return 0;
}