2014-05-30 08:53:19 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2014-05-30 08:53:19 +00:00
|
|
|
*/
|
2017-04-13 08:27:10 +00:00
|
|
|
#include "lang/lang_file_parser.h"
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-06 14:38:10 +00:00
|
|
|
#include "base/parse_helper.h"
|
2020-09-30 09:11:44 +00:00
|
|
|
#include "ui/integration.h"
|
|
|
|
|
|
|
|
#include <QtCore/QTextStream>
|
|
|
|
#include <QtCore/QFile>
|
|
|
|
#include <QtCore/QFileInfo>
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 08:45:58 +00:00
|
|
|
namespace Lang {
|
2017-04-13 17:59:05 +00:00
|
|
|
namespace {
|
2017-04-13 08:45:58 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
constexpr auto kLangFileLimit = 1024 * 1024;
|
2017-04-13 08:45:58 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
} // namespace
|
2017-04-13 08:45:58 +00:00
|
|
|
|
2019-06-19 15:42:16 +00:00
|
|
|
FileParser::FileParser(const QString &file, const std::set<ushort> &request)
|
2017-04-13 17:59:05 +00:00
|
|
|
: _content(base::parse::stripComments(ReadFile(file, file)))
|
|
|
|
, _request(request) {
|
|
|
|
parse();
|
|
|
|
}
|
2017-04-13 08:45:58 +00:00
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
FileParser::FileParser(const QByteArray &content, Fn<void(QLatin1String key, const QByteArray &value)> callback)
|
2017-04-13 17:59:05 +00:00
|
|
|
: _content(base::parse::stripComments(content))
|
|
|
|
, _callback(std::move(callback)) {
|
|
|
|
parse();
|
|
|
|
}
|
2017-04-13 08:45:58 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
void FileParser::parse() {
|
|
|
|
if (_content.isEmpty()) {
|
2020-09-30 09:11:44 +00:00
|
|
|
error(u"Got empty lang file content"_q);
|
2017-04-13 17:59:05 +00:00
|
|
|
return;
|
2017-04-13 08:45:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
auto text = _content.constData(), end = text + _content.size();
|
|
|
|
while (text != end) {
|
|
|
|
if (!readKeyValue(text, end)) {
|
|
|
|
break;
|
2017-04-13 08:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &FileParser::errors() const {
|
|
|
|
if (_errors.isEmpty() && !_errorsList.isEmpty()) {
|
|
|
|
_errors = _errorsList.join('\n');
|
|
|
|
}
|
|
|
|
return _errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString &FileParser::warnings() const {
|
|
|
|
if (_warnings.isEmpty() && !_warningsList.isEmpty()) {
|
|
|
|
_warnings = _warningsList.join('\n');
|
|
|
|
}
|
|
|
|
return _warnings;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileParser::readKeyValue(const char *&from, const char *end) {
|
2016-10-28 12:44:28 +00:00
|
|
|
using base::parse::skipWhitespaces;
|
|
|
|
if (!skipWhitespaces(from, end)) return false;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
if (*from != '"') {
|
|
|
|
return error("Expected quote before key name!");
|
|
|
|
}
|
2014-12-18 18:40:49 +00:00
|
|
|
++from;
|
|
|
|
const char *nameStart = from;
|
2017-06-02 11:46:02 +00:00
|
|
|
while (from < end && ((*from >= 'a' && *from <= 'z') || (*from >= 'A' && *from <= 'Z') || *from == '_' || (*from >= '0' && *from <= '9') || *from == '#')) {
|
2014-12-18 18:40:49 +00:00
|
|
|
++from;
|
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
auto key = QLatin1String(nameStart, from - nameStart);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
if (from == end || *from != '"') {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Expected quote after key name '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
2014-12-18 18:40:49 +00:00
|
|
|
++from;
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
if (!skipWhitespaces(from, end)) {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
if (*from != '=') {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"'=' expected in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
if (!skipWhitespaces(++from, end)) {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
if (*from != '"') {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Expected string after '=' in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
auto skipping = false;
|
2019-06-19 15:42:16 +00:00
|
|
|
auto keyIndex = kKeysCount;
|
2017-04-13 17:59:05 +00:00
|
|
|
if (!_callback) {
|
|
|
|
keyIndex = GetKeyIndex(key);
|
|
|
|
skipping = (_request.find(keyIndex) == _request.end());
|
2014-12-19 21:20:30 +00:00
|
|
|
}
|
2014-12-18 18:40:49 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
auto value = QByteArray();
|
|
|
|
auto appendValue = [&value, skipping](auto&&... args) {
|
|
|
|
if (!skipping) {
|
|
|
|
value.append(std::forward<decltype(args)>(args)...);
|
|
|
|
}
|
|
|
|
};
|
2014-12-18 18:40:49 +00:00
|
|
|
const char *start = ++from;
|
|
|
|
while (from < end && *from != '"') {
|
|
|
|
if (*from == '\n') {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Unexpected end of string in key '%1'!"_q.arg(key));
|
2014-12-18 18:40:49 +00:00
|
|
|
}
|
|
|
|
if (*from == '\\') {
|
2017-04-13 17:59:05 +00:00
|
|
|
if (from + 1 >= end) {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
if (*(from + 1) == '"' || *(from + 1) == '\\') {
|
|
|
|
if (from > start) appendValue(start, from - start);
|
2014-12-18 18:40:49 +00:00
|
|
|
start = ++from;
|
|
|
|
} else if (*(from + 1) == 'n') {
|
2017-04-13 17:59:05 +00:00
|
|
|
if (from > start) appendValue(start, from - start);
|
|
|
|
appendValue('\n');
|
2014-12-18 18:40:49 +00:00
|
|
|
start = (++from) + 1;
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2014-12-18 18:40:49 +00:00
|
|
|
++from;
|
|
|
|
}
|
2017-04-13 17:59:05 +00:00
|
|
|
if (from >= end) {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
if (from > start) {
|
|
|
|
appendValue(start, from - start);
|
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
if (!skipWhitespaces(++from, end)) {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"Unexpected end of file in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
|
|
|
if (*from != ';') {
|
2020-09-30 09:11:44 +00:00
|
|
|
return error(u"';' expected after \"value\" in key '%1'!"_q.arg(key));
|
2017-04-13 17:59:05 +00:00
|
|
|
}
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2016-10-28 12:44:28 +00:00
|
|
|
skipWhitespaces(++from, end);
|
2014-05-30 08:53:19 +00:00
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
if (_callback) {
|
|
|
|
_callback(key, value);
|
|
|
|
} else if (!skipping) {
|
|
|
|
_result.insert(keyIndex, QString::fromUtf8(value));
|
2014-12-19 21:20:30 +00:00
|
|
|
}
|
2014-12-18 18:40:49 +00:00
|
|
|
|
|
|
|
return true;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 17:59:05 +00:00
|
|
|
QByteArray FileParser::ReadFile(const QString &absolutePath, const QString &relativePath) {
|
|
|
|
QFile file(QFileInfo(relativePath).exists() ? relativePath : absolutePath);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2020-09-30 09:11:44 +00:00
|
|
|
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Could not open file at '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
2017-04-13 17:59:05 +00:00
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
if (file.size() > kLangFileLimit) {
|
2020-09-30 09:11:44 +00:00
|
|
|
Ui::Integration::Instance().writeLogEntry(u"Lang Error: File is too big: %1"_q.arg(file.size()));
|
2017-04-13 17:59:05 +00:00
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr auto kCodecMagicSize = 3;
|
|
|
|
auto codecMagic = file.read(kCodecMagicSize);
|
|
|
|
if (codecMagic.size() < kCodecMagicSize) {
|
2020-09-30 09:11:44 +00:00
|
|
|
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Found bad file at '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
2017-04-13 17:59:05 +00:00
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
file.seek(0);
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
int skip = 0;
|
2017-06-06 10:40:15 +00:00
|
|
|
auto readUtf16Stream = [relativePath, absolutePath](auto &&stream) {
|
2017-04-13 17:59:05 +00:00
|
|
|
stream.setCodec("UTF-16");
|
|
|
|
auto string = stream.readAll();
|
|
|
|
if (stream.status() != QTextStream::Ok) {
|
2020-09-30 09:11:44 +00:00
|
|
|
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Could not read UTF-16 data from '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
2017-04-13 17:59:05 +00:00
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
if (string.isEmpty()) {
|
2020-09-30 09:11:44 +00:00
|
|
|
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Empty UTF-16 content in '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
2017-04-13 17:59:05 +00:00
|
|
|
return QByteArray();
|
|
|
|
}
|
|
|
|
return string.toUtf8();
|
|
|
|
};
|
|
|
|
if ((codecMagic.at(0) == '\xFF' && codecMagic.at(1) == '\xFE') || (codecMagic.at(0) == '\xFE' && codecMagic.at(1) == '\xFF') || (codecMagic.at(1) == 0)) {
|
|
|
|
return readUtf16Stream(QTextStream(&file));
|
|
|
|
} else if (codecMagic.at(0) == 0) {
|
|
|
|
auto utf16WithBOM = "\xFE\xFF" + file.readAll();
|
|
|
|
return readUtf16Stream(QTextStream(utf16WithBOM));
|
|
|
|
}
|
|
|
|
data = file.readAll();
|
|
|
|
if (codecMagic.at(0) == '\xEF' && codecMagic.at(1) == '\xBB' && codecMagic.at(2) == '\xBF') {
|
|
|
|
data = data.mid(3); // skip UTF-8 BOM
|
|
|
|
}
|
|
|
|
if (data.isEmpty()) {
|
2020-09-30 09:11:44 +00:00
|
|
|
Ui::Integration::Instance().writeLogEntry(u"Lang Error: Empty UTF-8 content in '%1' ('%2')"_q.arg(relativePath).arg(absolutePath));
|
2017-04-13 17:59:05 +00:00
|
|
|
return QByteArray();
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-04-13 17:59:05 +00:00
|
|
|
return data;
|
2014-05-30 08:53:19 +00:00
|
|
|
}
|
2017-04-13 08:45:58 +00:00
|
|
|
|
|
|
|
} // namespace Lang
|