Hot fixes for QDir

master
const an teen 2021-12-04 11:29:01 -05:00
parent 96f6ea1022
commit 0f2aad299b
6 changed files with 55 additions and 41 deletions

View File

@ -120,12 +120,12 @@ void ApplicationData::readConfig()
//// Парсинг GLOBAL
QString globalSection {conffile};
size_t globalBegin = conffile.indexOf("[GLOBAL]");
if (globalBegin == std::string::npos) {
int globalBegin = conffile.indexOf("[GLOBAL]");
if (globalBegin == -1) {
throw std::runtime_error("ApplicationData::readConfig(): Wrong config. [GLOBAL] section not exist!");
}
size_t globalEnd = conffile.indexOf('[', globalBegin+1);
if (globalEnd != std::string::npos) {
int globalEnd = conffile.indexOf('[', globalBegin+1);
if (globalEnd != -1) {
// Удаление последующей [секции]
globalSection.remove(globalEnd, conffile.size()-globalEnd);
}
@ -192,9 +192,9 @@ void ApplicationData::readConfig()
// Цикл до тех пор, пока остались заголовки секций
while (conffile.contains(QRegularExpression("\\[[^\\n]*\\]"))) {
QString currentSection {conffile};
size_t begin = conffile.indexOf(QRegularExpression("\\[[^\\n]*\\]"));
size_t end = conffile.indexOf('[', begin+1);
if (end != std::string::npos) {
int begin = conffile.indexOf(QRegularExpression("\\[[^\\n]*\\]"));
int end = conffile.indexOf('[', begin+1);
if (end != -1) {
currentSection.remove(end, currentSection.size() - end);
}
currentSection.remove(0, begin);

View File

@ -19,14 +19,14 @@ QString getValue(const QString &string, const QString &key, Type type)
if (key.isEmpty())
return QString();
size_t keyStart = string.indexOf(QRegularExpression(key + "\\s*="));
if (keyStart == std::string::npos) {
int keyStart = string.indexOf(QRegularExpression(key + "\\s*="));
if (keyStart == -1) {
return QString();
}
size_t keyEnd = string.indexOf('=', keyStart) + 1;
size_t keySize = keyEnd - keyStart;
int keyEnd = string.indexOf('=', keyStart) + 1;
int keySize = keyEnd - keyStart;
size_t valueStart = keyStart + keySize;
int valueStart = keyStart + keySize;
QString separator {"\n"};
if (type == eForTriggers) {
@ -34,8 +34,8 @@ QString getValue(const QString &string, const QString &key, Type type)
} else if (type == eForWeb) {
separator = " ";
}
size_t valueEnd = string.indexOf(separator, valueStart);
if (valueEnd == std::string::npos) {
int valueEnd = string.indexOf(separator, valueStart);
if (valueEnd == -1) {
valueEnd = string.size();
}
std::string result = string.toStdString().substr(valueStart, valueEnd - valueStart);

View File

@ -30,6 +30,12 @@ HttpServer::HttpServer(const QString &address, quint16 port, const QString& logF
connect (m_TcpServer, &QTcpServer::newConnection, this, &HttpServer::acceptor);
}
HttpServer::~HttpServer()
{
m_TcpServer->close();
m_TcpServer->deleteLater();
}
void HttpServer::consoleLog(const QString &message)
{
qInfo().noquote() << "[WEBINTERFACE]" << message;
@ -91,7 +97,7 @@ void HttpServer::reader()
void HttpServer::ircBotFirstInfo(QString server, QStringList channels)
{
for (const auto c: channels) {
for (const auto &c: channels) {
m_backendInfo[server][c] = QStringList();
}
}
@ -183,12 +189,13 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
if (channel.isEmpty()){
// First channel if not passed directly
QDirIterator it(fsPath.path());
it.next(); it.next(); // . and .. directories
if (it.hasNext()) {
while (it.hasNext()) {
channel = it.next();
if (channel.endsWith(".") or channel.endsWith("..")) continue; // QDir::NoDotAndNoDotDot not works!
while(channel.contains('/')) {
channel.remove(QRegularExpression("^.*/"));
}
break;
}
}
if (not fsPath.cd(channel)) {
@ -218,7 +225,6 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
QString day;
if (not year.isEmpty() and fsPath.cd(year)) {
urlPath.remove(QRegularExpression("^.*/"+year));
month = getWordFromPath(urlPath);
month.remove(QRegularExpression("\\?.*$"));
if (not month.isEmpty() and fsPath.cd(month)) {
@ -297,7 +303,7 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
}
}
uint currentOnline = m_backendInfo[originalServerName][originalChannelName].size();
int currentOnline = m_backendInfo[originalServerName][originalChannelName].size();
if (currentOnline > 0) currentOnline -= 1;
replaceTag(page, "ONLINE", QString::number(currentOnline));
@ -336,14 +342,15 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
}
middlePath += " ("+searchRequest+")";
QDirIterator it(fsPath.path());
it.next(); it.next(); // . and .. directories
QStringList paths;
QDirIterator it(fsPath.path());
while (it.hasNext()) {
QString currentPath = it.next();
if (currentPath.endsWith(".") or currentPath.endsWith("..")) continue;
QString logFolder = m_logFolder;
#ifdef WIN32
logFolder.replace('\\', '/');
#endif
QString server {currentPath}; // Folder wich is not server folder is ignored
server.remove(QRegularExpression("^"+logFolder));
@ -407,13 +414,13 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
}
}
else if (month.isEmpty() and not year.isEmpty()){
for (const auto p: paths) {
for (const auto &p: paths) {
QStringList slavePaths;
QDirIterator it(p);
it.next(); it.next(); // . and .. directories
QDirIterator it(p);
while (it.hasNext()) {
QString fileName = it.next();
if (fileName.endsWith(".") or fileName.endsWith("..")) continue;
if (not QRegularExpression("\\.txt$").match(fileName).hasMatch()) continue;
slavePaths.push_back(fileName);
}
@ -454,21 +461,20 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
QStringList fileNameS;
for (const auto& p: yearPaths) {
QStringList slavePaths;
QDirIterator it(p);
it.next(); it.next(); // . and .. directories
QDirIterator it(p);
while (it.hasNext()) {
QString folderName = it.next();
if (folderName.endsWith(".") or folderName.endsWith("..")) continue;
if (not QRegularExpression("/[0-9]{2}$").match(folderName).hasMatch()) continue;
slavePaths.push_back(folderName);
}
for (const auto &path: slavePaths) {
QDirIterator itMonth(path);
itMonth.next(); itMonth.next(); // . and .. directories
while (itMonth.hasNext()) {
QString fileName = itMonth.next();
if (fileName.endsWith(".") or fileName.endsWith("..")) continue;
if (not QRegularExpression("^.*[0-9]{2}\\.txt$").match(fileName).hasMatch()) continue;
fileNameS.push_back(fileName);
}
@ -523,9 +529,9 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
if (year.isEmpty()) { // /
QStringList folderNameS;
QDirIterator it(fsPath.path());
it.next(); it.next(); // . and .. directories
while (it.hasNext()) {
QString folderName = it.next();
if (folderName.endsWith(".") or folderName.endsWith("..")) continue;
while(folderName.contains('/')) {
folderName.remove(QRegularExpression("^.*/"));
}
@ -544,9 +550,9 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
else if (not year.isEmpty() and month.isEmpty()) { // /YYYY
QStringList folderNameS;
QDirIterator it(fsPath.path());
it.next(); it.next(); // . and .. directories
while (it.hasNext()) {
QString folderName = it.next();
if (folderName.endsWith(".") or folderName.endsWith("..")) continue;
while(folderName.contains('/')) {
folderName.remove(QRegularExpression("^.*/"));
}
@ -565,9 +571,9 @@ void HttpServer::writeMainPage(QTcpSocket *socket, QString &urlPath)
else if (not month.isEmpty() and day.isEmpty()) { // /YYYY/MM
QStringList fileNameS;
QDirIterator it(fsPath.path());
it.next(); it.next(); // . and .. directories
while (it.hasNext()) {
QString fileName = it.next();
if (fileName.endsWith(".") or fileName.endsWith("..")) continue; // QDir::NoDotAndNoDotDot not works!
while(fileName.contains('/')) {
fileName.remove(QRegularExpression("^.*/"));
}

View File

@ -10,6 +10,7 @@ class HttpServer : public QObject
Q_OBJECT
public:
explicit HttpServer(const QString& address, quint16 port, const QString& logFolder, const QString& mainChannel, QObject *parent = nullptr);
~HttpServer();
private:
QString getRequestPath(const QString &req);

View File

@ -21,12 +21,13 @@ IrcClient::IrcClient(const ConnectionData& config, QObject *parent) :
throw std::runtime_error("Empty ConnectionData::address! You trying wrong way to use it, man!");
}
QString path {global::toLowerAndNoSpaces(m_connectionData.displayName)};
QDir dir(m_connectionData.logFolderPath);
if (not dir.exists()) {
dir.cdUp();
QString path {global::toLowerAndNoSpaces(m_connectionData.displayName)};
dir.mkdir(path);
}
dir.cd(path);
for (auto chan: m_connectionData.channels) {
chan.remove('#');
dir.mkdir(chan);
@ -38,6 +39,11 @@ IrcClient::IrcClient(const ConnectionData& config, QObject *parent) :
connect (&m_timerToJoin, &QTimer::timeout, this, &IrcClient::onLogin);
}
IrcClient::~IrcClient()
{
m_socket->disconnectFromHost();
}
QStringList IrcClient::getOnlineUsers(const QString &channel)
{
if (m_online.find(channel) == m_online.end()) {
@ -98,11 +104,11 @@ IrcClient::IrcCode IrcClient::getServerCode(const QString &message)
IrcCode code = IrcCode::Failed;
QString result {message};
size_t beginPosition = result.indexOf(' ');
if (beginPosition == std::string::npos) return code;
int beginPosition = result.indexOf(' ');
if (beginPosition == -1) return code;
result.remove(0, beginPosition+1);
size_t endPosition = result.indexOf(' ');
if (endPosition == std::string::npos) return code;
int endPosition = result.indexOf(' ');
if (endPosition == -1) return code;
result.remove(endPosition, result.size()-endPosition);
bool convert {false};
int resultInt {result.toInt(&convert)};
@ -122,12 +128,12 @@ IrcClient::IrcCode IrcClient::getServerCode(const QString &message)
QString IrcClient::getChannelName(const QString &message)
{
size_t begin = message.indexOf('#');
if (begin == std::string::npos) return QString();
int begin = message.indexOf('#');
if (begin == -1) return QString();
QString result {message};
result.remove(0,begin);
size_t end = result.indexOf(' ');
if (end == std::string::npos) {
int end = result.indexOf(' ');
if (end == -1) {
end = result.size();
}
else {

View File

@ -15,6 +15,7 @@ class IrcClient : public QObject
Q_OBJECT
public:
IrcClient(const ConnectionData& config, QObject *parent = nullptr);
~IrcClient();
QStringList getOnlineUsers(const QString& channel);
void connectToServer();