-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (149 loc) · 5.09 KB
/
main.cpp
File metadata and controls
167 lines (149 loc) · 5.09 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
#include <QCoreApplication>
#include <memory>
#include <iostream>
#include <winnls.h>
#include "filemanager.h"
#include <QThread>
#include "consolelogger.h"
#include <windows.h>
QString toQStringCP866(const std::string& str)
{
#ifdef Q_OS_WINDOWS
int wideSize = MultiByteToWideChar(866, 0, str.c_str(), -1, NULL, 0);
if (wideSize > 0) {
std::wstring wstr(wideSize, L'\0');
MultiByteToWideChar(866, 0, str.c_str(), -1, &wstr[0], wideSize);
wstr.resize(wideSize - 1);
return QString::fromStdWString(wstr);
}
return QString::fromLocal8Bit(str.c_str());
#endif
}
void setupConsole()
{
#ifdef Q_OS_WINDOWS
SetConsoleOutputCP(CP_UTF8);
setbuf(stdout, NULL);
setbuf(stdin, NULL);
#endif
}
QStringList parseArgs(const QString& line)
{
QStringList result;
QString current;
bool inQuotes = false;
for (QChar ch : line) {
if (ch == '"') {
inQuotes = !inQuotes;
} else if (ch == ' ' && !inQuotes) {
if (!current.isEmpty()) result.append(current);
current.clear();
} else {
current.append(ch);
}
}
if (!current.isEmpty()) result.append(current);
return result;
}
QStringList extractPaths(const QString& arg)
{
QStringList paths;
QString current;
bool inQuotes = false;
for (QChar ch : arg) {
if (ch == '"') {
inQuotes = !inQuotes;
} else if (ch == ',' && !inQuotes) {
if (!current.isEmpty()) paths.append(current);
current.clear();
} else {
current.append(ch);
}
}
if (!current.isEmpty()) paths.append(current);
for (QString& path : paths) {
path = path.trimmed();
if (path.startsWith('"') && path.endsWith('"')) {
path = path.mid(1, path.length() - 2);
}
}
return paths;
}
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
setupConsole();
auto logger = std::make_shared<ConsoleLogger>();
FileManager& manager = FileManager::getInstance();
manager.setLogger(logger);
logger->printHelp();
QThread inputThread;
bool running = true;
QObject::connect(&inputThread, &QThread::started, [&]() {
while (running) {
std::cout << "\n> Введите команду: " << std::flush;
std::string input;
std::getline(std::cin, input);
if (!running) break;
if (input.empty()) continue;
QString line = toQStringCP866(input);
QStringList parts = parseArgs(line);
if (parts.isEmpty()) continue;
QString cmd = parts[0].toLower();
if (cmd == "exit" || cmd == "quit") {
std::cout << "Завершение программы..." << std::endl;
running = false;
QMetaObject::invokeMethod(&app, &QCoreApplication::quit, Qt::QueuedConnection);
break;
}
else if (cmd == "help") {
logger->printHelp();
}
else if (cmd == "add" && parts.size() > 1) {
QString argsPart = parts.mid(1).join(' ');
QStringList files = extractPaths(argsPart);
QMetaObject::invokeMethod(&manager, [&manager, files]() {
manager.addFiles(files);
}, Qt::QueuedConnection);
}
else if (cmd == "remove" && parts.size() > 1) {
QString path = parts.mid(1).join(' ');
if (path.startsWith('"') && path.endsWith('"')) {
path = path.mid(1, path.length() - 2);
}
QMetaObject::invokeMethod(&manager, [&manager, path]() {
manager.removeFile(path);
}, Qt::QueuedConnection);
}
else if (cmd == "list") {
QMetaObject::invokeMethod(&manager, [&manager]() {
manager.listFiles();
}, Qt::QueuedConnection);
}
else if (cmd == "start") {
int interval = 100;
if (parts.size() > 1) {
interval = parts[1].toInt();
if (interval <= 0) interval = 100;
}
QMetaObject::invokeMethod(&manager, [&manager, interval]() {
manager.startMonitoring(interval);
}, Qt::QueuedConnection);
}
else if (cmd == "stop") {
QMetaObject::invokeMethod(&manager, [&manager]() {
manager.stopMonitoring();
}, Qt::QueuedConnection);
}
else {
std::cout << "Неизвестная команда. Введите 'help' для справки." << std::endl;
}
}
});
inputThread.start();
int result = app.exec();
running = false;
inputThread.quit();
inputThread.wait();
return result;
}