-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
173 lines (129 loc) · 4.4 KB
/
Copy pathsearch.cpp
File metadata and controls
173 lines (129 loc) · 4.4 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
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
#include "search.hpp"
#include <iostream>
#include "booktable.hpp"
#include "authortable.hpp"
#include "publishertable.hpp"
#include "serietable.hpp"
#include "tagtable.hpp"
#include "data.hpp"
#include "utils.hpp"
#include "config.hpp"
#include "configparser.hpp"
#include "renderer.hpp"
#include <fastcgi/fcgio.h>
Search::Search() :
_tables{new BookTable(), new AuthorTable(), new PublisherTable(), new SerieTable(), new TagTable()}
{
}
Search::~Search() {
for(Table* table: _tables)
delete table;
}
std::vector< std::set< Data* > > Search::search(const std::string& entry) {
std::vector<std::set<Data*>> results;
if(!entry.empty()) {
std::vector<std::string> tokens = tokenize(entry);
if(!tokens.empty()) {
SearchQuery query = group(tokens);
for(Table* table: _tables)
results.push_back(table->search(query));
}
}
return results;
}
std::vector<std::string> Search::tokenize(std::string const& entry) {
std::vector<std::string> tokens;
SearchState state = None, oldState = None;
int start = 0;
int length = 0;
for(std::size_t i = 0; i < entry.size(); ++i, ++length) {
char c = entry[i];
if(c == DoubleQuote and state != BackSlash) {
if(state == DoubleQuote)
state = None;
else
state = DoubleQuote;
if(length > 0)
tokens.push_back(entry.substr(start, length));
start = i+1;
length = -1;
} else {
if(state == BackSlash)
state = oldState;
if(c == None and state == None) {
if(length > 0)
tokens.push_back(entry.substr(start, length));
start = i+1;
length = -1;
} else if(c == BackSlash) {
oldState = state;
state = BackSlash;
}
}
}
if(length > 0)
tokens.push_back(entry.substr(start, length));
/* Post-Processing */
for(auto it = tokens.begin(); it != tokens.end(); ++it) {
if((*it) == "+" or (*it) == "-") {
std::string token = (*it);
tokens.erase(it);
if(it != tokens.end())
(*it) = token + (*it);
--it;
} else
Utils::replace((*it), "\\\"", "\"");
}
return tokens;
}
SearchQuery Search::group(std::vector<std::string> const& tokens) {
SearchQuery query;
for(std::string const& token : tokens) {
switch(token[0]) {
case '+':
query.ands.insert(token.substr(1, token.size()));
break;
case '-':
query.nots.insert(token.substr(1, token.size()));
break;
default:
query.ors.insert(token);
break;
}
}
return query;
}
int main(int argc, char **argv) {
ConfigParser().read("oook.conf");
std::streambuf* cin_streambuf = std::cin.rdbuf();
std::streambuf* cout_streambuf = std::cout.rdbuf();
std::streambuf* cerr_streambuf = std::cerr.rdbuf();
FCGX_Init();
FCGX_Request request;
FCGX_InitRequest(&request,0,0);
Search search;
while(FCGX_Accept_r(&request) == 0)
{
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
std::cin.rdbuf(&cin_fcgi_streambuf);
std::cout.rdbuf(&cout_fcgi_streambuf);
std::cerr.rdbuf(&cerr_fcgi_streambuf);
std::cout << "Content-type: text/html" << std::endl << std::endl;
std::map<std::string, std::string> params = Utils::paramsURI(
FCGX_GetParam("REQUEST_URI", request.envp)
);
std::vector<std::set<Data*>> results;
try {
results = search.search(params.at("q"));
} catch(std::exception const& e) {
std::cerr << e.what() << std::endl;
}
std::cout << Renderer::instance().renderSection(results, "../") << std::endl;
}
std::cin.rdbuf(cin_streambuf);
std::cout.rdbuf(cout_streambuf);
std::cerr.rdbuf(cerr_streambuf);
return 0;
}