Skip to content

Commit 632f09c

Browse files
committed
config
1 parent 7768c75 commit 632f09c

11 files changed

+364
-51
lines changed

CMakeLists.txt

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# include(FetchContent)
21
cmake_minimum_required(VERSION 3.20)
32

43
project(cpp_search_qt_creator LANGUAGES CXX)
@@ -12,20 +11,15 @@ set(Boost_USE_STATIC_RUNTIME OFF)
1211
find_package(Boost REQUIRED)
1312
find_package(OpenSSL)
1413

15-
# FetchContent_Declare(
16-
# libpqxx
17-
# GIT_REPOSITORY https://github.com/jtv/libpqxx.git
18-
# GIT_TAG a6b1d60e74c1427c8ac2324b85cd4a0dc2068332
19-
# )
20-
# set(PQXX_LIBRARIES pqxx_static)
21-
# FetchContent_MakeAvailable(libpqxx)
22-
2314
if(Boost_FOUND)
2415
include_directories(${Boost_INCLUDE_DIRS})
2516
add_executable(cpp_search_qt_creator main.cpp Crowler.h Crowler.cpp root_certificates.hpp
2617
DbManager.h DbManager.cpp
2718
Searcher.h Searcher.cpp
28-
SafeQueue.h SafeQueue.cpp)
19+
SafeQueue.h SafeQueue.cpp
20+
IniParser.h IniParser.cpp
21+
exceptions.h
22+
config.ini)
2923
add_subdirectory(./libpqxx-7.9.0 libpqxx-build)
3024
target_compile_features(cpp_search_qt_creator PRIVATE cxx_std_17)
3125
target_link_libraries(cpp_search_qt_creator ${Boost_LIBRARIES} OpenSSL::SSL OpenSSL::Crypto pqxx)# "${PQXX_LIBRARIES}")

Crowler.cpp

+24-15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <iostream>
1515
#include <string>
1616
#include <unordered_map>
17+
#include "DbManager.h"
18+
#include "IniParser.h"
1719

1820
namespace beast = boost::beast; // from <boost/beast.hpp>
1921
namespace http = beast::http; // from <boost/beast/http.hpp>
@@ -22,7 +24,24 @@ namespace ssl = net::ssl; // from <boost/asio/ssl.hpp>
2224
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
2325

2426

27+
Crowler::Crowler()
28+
{
29+
const int cores_count = std::thread::hardware_concurrency(); // количество аппаратных ядер
30+
for (size_t i = 0; i != cores_count; ++i) {
31+
// наполнение вектора, хранящего потоки, задачами на обработку
32+
std::thread t(&Crowler::work, this);
33+
threadsPool_.push_back(std::move(t));
34+
}
35+
}
2536

37+
Crowler::~Crowler()
38+
{
39+
for (auto& thread : threadsPool_) {
40+
// ожидание окончания работы потоков
41+
thread.join();
42+
}
43+
threadsPool_.clear();
44+
}
2645

2746
void Crowler::processUrl(std::string url, short depth)
2847
{
@@ -152,23 +171,13 @@ void Crowler::savePresencesToDb(std::vector<std::string> words, std::string url)
152171

153172
}
154173

155-
Crowler::Crowler()
156-
{
157-
const int cores_count = std::thread::hardware_concurrency(); // количество аппаратных ядер
158-
for (size_t i = 0; i != cores_count; ++i) {
159-
// наполнение вектора, хранящего потоки, задачами на обработку
160-
std::thread t(&Crowler::work, this);
161-
threadsPool_.push_back(std::move(t));
162-
}
163-
}
164174

165-
Crowler::~Crowler()
175+
void Crowler::processStartPage()
166176
{
167-
for (auto& thread : threadsPool_) {
168-
// ожидание окончания работы потоков
169-
thread.join();
170-
}
171-
threadsPool_.clear();
177+
IniParser parser("/Users/tkvitko/c/netology/cpp_diploma/cpp_search_qt_creator/config.ini"); // не хочет работать с "./config.ini"
178+
std::string url = parser.get_value<std::string>("Crowler.startPage");
179+
unsigned short depth = parser.get_value<unsigned short>("Crowler.recursionDepth");
180+
addToCrowlingQueue(url, depth);
172181
}
173182

174183
void Crowler::addToCrowlingQueue(std::string url, unsigned short depth)

Crowler.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
#include <string>
55
#include <regex>
6-
#include <queue>
7-
#include <mutex>
86
#include <vector>
97
#include <thread>
10-
#include "DbManager.h"
11-
#include "SafeQueue.h""
8+
#include "SafeQueue.h"
129

1310
class Crowler
1411
{
@@ -31,12 +28,16 @@ class Crowler
3128
void savePresencesToDb(std::vector<std::string> words, std::string url);
3229
// обход ресурса
3330
void processUrl(std::string url, short depth);
31+
// добавление задачи в очередь на обход
32+
void addToCrowlingQueue(std::string url, unsigned short depth);
33+
// методя для взятия очередной задачи на процессинг ресурса из очереди задач и процессинга
3434
void work();
3535

3636
public:
3737
Crowler();
3838
~Crowler();
39-
void addToCrowlingQueue(std::string url, unsigned short depth);
39+
// метод запуска процессинга стартового ресурса (из конфига)
40+
void processStartPage();
4041
};
4142

4243
#endif // CROWLER_H

DbManager.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
#include "DbManager.h"
2+
#include "IniParser.h"
23
#include <algorithm>
34

45

56

67
DbManager::DbManager()
78
{
9+
10+
IniParser parser("/Users/tkvitko/c/netology/cpp_diploma/cpp_search_qt_creator/config.ini"); // не хочет работать с "./config.ini"
11+
std::string host = parser.get_value<std::string>("Db.host");
12+
std::string port = parser.get_value<std::string>("Db.port");
13+
std::string dbname = parser.get_value<std::string>("Db.name");
14+
std::string user = parser.get_value<std::string>("Db.userName");
15+
std::string password = parser.get_value<std::string>("Db.password");
16+
817
try {
9-
conn = new pqxx::connection("host=localhost "
10-
"port=5432 "
11-
"dbname=searcher "
12-
"user=searcher "
13-
"password=searcher");
18+
conn = new pqxx::connection("host=" + host +
19+
" port=" + port +
20+
" dbname=" + dbname +
21+
" user=" + user +
22+
" password=" + password);
1423
createTables();
1524

1625
} catch (pqxx::sql_error e) {

DbManager.h

-6
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ class DbManager {
2828
// параметры подключения к базе данных
2929
pqxx::connection* conn = nullptr;
3030

31-
std::string host;
32-
int port;
33-
std::string dbName;
34-
std::string userName;
35-
std::string password;
36-
3731
// метод для создания таблиц базы данных, если их ещё нет
3832
void createTables();
3933
// метод для перевода вектора строк в форматированную строку

IniParser.cpp

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "IniParser.h"
2+
#include "exceptions.h"
3+
#include <iostream>
4+
#include <fstream>
5+
#include <algorithm>
6+
7+
8+
IniParser::IniParser(std::string filename){
9+
// конструктор, принимающий имя файла
10+
11+
filename_ = filename;
12+
std::ifstream source_file = std::ifstream(this->filename_);
13+
std::string current_section;
14+
unsigned int line_number = 0;
15+
16+
if (source_file) {
17+
while (source_file) {
18+
std::string line;
19+
std::getline(source_file, line);
20+
line_number++;
21+
22+
// заполнение data
23+
if (this->get_line_type(line) == LineType::comment) {
24+
// если это комментарий, пропускаем строку
25+
26+
} else if (this->get_line_type(line) == LineType::section) {
27+
// если это описание секции:
28+
this->process_section_line(line, current_section);
29+
30+
} else if (this->get_line_type(line) == LineType::field) {
31+
// если это описание значения:
32+
this->process_field_line(line, current_section, line_number);
33+
34+
} else if (this->get_line_type(line) == LineType::unknown) {
35+
std::cout << "Warning: bad syntax in line " << line_number << std::endl;
36+
}
37+
}
38+
} else {
39+
throw NoSuchFile();
40+
}
41+
};
42+
43+
// деструктр
44+
IniParser::~IniParser() {};
45+
46+
std::string IniParser::_get_value(const std::string& value_path) {
47+
auto section_field = this->split_line(value_path, '.');
48+
auto section = section_field[0];
49+
auto field = section_field[1];
50+
if (this->data.find(section) != this->data.end()) {
51+
if (this->data[section].find(field) != this->data[section].end()) {
52+
std::string value = this->data[section][field];
53+
return value;
54+
} else {
55+
std::cout << "No field " << field << " in section " << section << std::endl;
56+
std::cout << "You probably meant: " << std::endl;
57+
for (auto el : data[section]) {
58+
std::cout << el.first << std::endl;
59+
};
60+
throw NoSuchFieldInSection();
61+
}
62+
} else {
63+
throw NoSuchSection();
64+
}
65+
}
66+
void IniParser::process_section_line(std::string& line, std::string& current_section) {
67+
/*
68+
метод процессинга строки с секцией:
69+
- line - строка,
70+
- current_section - текущая секция в цикле обработки файла.
71+
*/
72+
73+
line.erase(std::remove(line.begin(), line.end(), '['), line.end());
74+
line.erase(std::remove(line.begin(), line.end(), ']'), line.end());
75+
line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
76+
current_section = line;
77+
78+
if (this->data.find(current_section) == this->data.end()) {
79+
// если такой секции ещё нет
80+
std::map<std::string, std::string> empty;
81+
this->data.insert({line, empty});
82+
}
83+
}
84+
85+
void IniParser::process_field_line(std::string& line, const std::string& current_section, const int& line_number) {
86+
/*
87+
метод процессинга строки с полем (парой ключ-значение):
88+
- line - строка,
89+
- current_section - текущая секция в цикле обработки файла,
90+
- line_number - номер строки в файле.
91+
*/
92+
93+
auto key_value = this->split_line(line, '=');
94+
95+
// подготовка ключа (удаление всех символов пробела)
96+
auto key = key_value[0];
97+
key.erase(std::remove(key.begin(), key.end(), ' '), key.end());
98+
// подготовка значения (удаление символов пробела, идущих до первого символа)
99+
if (key_value.size() > 1) {
100+
auto value = key_value[1];
101+
auto value_start = value.find_first_not_of(' ');
102+
value = value.substr(value_start != std::string::npos ? value_start : 0);
103+
// удаление комментария из строки значения
104+
auto comment_start_pos = value.find(";");
105+
value = value.substr(0, comment_start_pos);
106+
this->data[current_section][key] = value;
107+
} else {
108+
// если значения нет, ничего не добавляем
109+
}
110+
111+
}
112+
113+
// метод для получения значения конкретного поля файла
114+
template <typename VALUE_TYPE>
115+
VALUE_TYPE IniParser::get_value(std::string value_path) {
116+
static_assert(sizeof(VALUE_TYPE) == -1, "not implemented type for get_value");
117+
};
118+
119+
template <>
120+
std::string IniParser::get_value(std::string value_path) {
121+
std::string value = this->_get_value(value_path);
122+
return value;
123+
};
124+
125+
template <>
126+
int IniParser::get_value(std::string value_path) {
127+
std::string value = this->_get_value(value_path);
128+
return std::stoi(value);
129+
};
130+
131+
template <>
132+
unsigned short IniParser::get_value(std::string value_path) {
133+
std::string value = this->_get_value(value_path);
134+
return std::stoul(value);
135+
};
136+
137+
template <>
138+
float IniParser::get_value(std::string value_path) {
139+
std::string value = this->_get_value(value_path);
140+
return std::stof(value);
141+
};

0 commit comments

Comments
 (0)