2016-04-16 17:51:25 +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.
|
2016-04-16 17:51:25 +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
|
2016-04-16 17:51:25 +00:00
|
|
|
*/
|
|
|
|
#include "codegen/common/clean_file.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
|
|
|
#include "codegen/common/logging.h"
|
|
|
|
|
|
|
|
namespace codegen {
|
|
|
|
namespace common {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool readFile(const QString &filepath, QByteArray *outResult) {
|
|
|
|
QFile f(filepath);
|
|
|
|
if (!f.exists()) {
|
|
|
|
logError(kErrorFileNotFound, filepath) << ": error: file does not exist.";
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-19 10:59:59 +00:00
|
|
|
auto limit = CleanFile::MaxSize;
|
|
|
|
if (f.size() > limit) {
|
|
|
|
logError(kErrorFileTooLarge, filepath) << "' is too large, size=" << f.size() << " > maxsize=" << limit;
|
2016-04-16 17:51:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
logError(kErrorFileNotOpened, filepath) << "' for read.";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*outResult = f.readAll();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2016-04-18 22:00:54 +00:00
|
|
|
CleanFile::CleanFile(const QString &filepath)
|
|
|
|
: filepath_(filepath)
|
|
|
|
, read_(true) {
|
|
|
|
}
|
|
|
|
|
|
|
|
CleanFile::CleanFile(const QByteArray &content, const QString &filepath)
|
|
|
|
: filepath_(filepath)
|
|
|
|
, content_(content)
|
|
|
|
, read_(false) {
|
2016-04-16 17:51:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CleanFile::read() {
|
2016-04-18 22:00:54 +00:00
|
|
|
if (read_) {
|
|
|
|
if (!readFile(filepath_, &content_)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-16 17:51:25 +00:00
|
|
|
}
|
|
|
|
filepath_ = QFileInfo(filepath_).absoluteFilePath();
|
|
|
|
|
|
|
|
enum class InsideComment {
|
|
|
|
None,
|
|
|
|
SingleLine,
|
|
|
|
MultiLine,
|
|
|
|
};
|
|
|
|
auto insideComment = InsideComment::None;
|
|
|
|
bool insideString = false;
|
|
|
|
|
2016-04-18 22:00:54 +00:00
|
|
|
const char *begin = content_.cbegin(), *end = content_.cend(), *offset = begin;
|
2016-04-16 17:51:25 +00:00
|
|
|
auto feedContent = [this, &offset, end](const char *ch) {
|
|
|
|
if (ch > offset) {
|
2016-04-18 22:00:54 +00:00
|
|
|
if (result_.isEmpty()) result_.reserve(end - offset - 2);
|
|
|
|
result_.append(offset, ch - offset);
|
2016-04-16 17:51:25 +00:00
|
|
|
offset = ch;
|
|
|
|
}
|
|
|
|
};
|
2017-02-03 20:07:26 +00:00
|
|
|
|
|
|
|
auto lineNumber = 0;
|
|
|
|
auto feedComment = [this, &offset, end, &lineNumber](const char *ch, bool save = false) {
|
2016-04-16 17:51:25 +00:00
|
|
|
if (ch > offset) {
|
2017-02-03 20:07:26 +00:00
|
|
|
if (save) {
|
|
|
|
singleLineComments_.resize(lineNumber + 1);
|
|
|
|
singleLineComments_[lineNumber] = QByteArray(offset, ch - offset);
|
|
|
|
}
|
|
|
|
if (result_.isEmpty()) {
|
|
|
|
result_.reserve(end - offset - 2);
|
|
|
|
}
|
2016-04-18 22:00:54 +00:00
|
|
|
result_.append(' ');
|
2016-04-16 17:51:25 +00:00
|
|
|
offset = ch;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (const char *ch = offset; ch != end;) {
|
|
|
|
char currentChar = *ch;
|
|
|
|
char nextChar = (ch + 1 == end) ? 0 : *(ch + 1);
|
|
|
|
|
|
|
|
if (insideComment == InsideComment::None && currentChar == '"') {
|
|
|
|
bool escaped = ((ch > begin) && *(ch - 1) == '\\') && ((ch - 1 < begin) || *(ch - 2) != '\\');
|
|
|
|
if (!escaped) {
|
|
|
|
insideString = !insideString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (insideString) {
|
2017-02-03 20:07:26 +00:00
|
|
|
if (currentChar == '\n') {
|
|
|
|
++lineNumber;
|
|
|
|
}
|
2016-04-16 17:51:25 +00:00
|
|
|
++ch;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (insideComment == InsideComment::None && currentChar == '/' && nextChar == '/') {
|
|
|
|
feedContent(ch);
|
|
|
|
insideComment = InsideComment::SingleLine;
|
|
|
|
ch += 2;
|
|
|
|
} else if (insideComment == InsideComment::SingleLine && currentChar == '\r' && nextChar == '\n') {
|
2017-02-03 20:07:26 +00:00
|
|
|
feedComment(ch, true);
|
2016-04-16 17:51:25 +00:00
|
|
|
ch += 2;
|
2017-02-03 20:07:26 +00:00
|
|
|
++lineNumber;
|
2016-04-16 17:51:25 +00:00
|
|
|
insideComment = InsideComment::None;
|
|
|
|
} else if (insideComment == InsideComment::SingleLine && currentChar == '\n') {
|
2017-02-03 20:07:26 +00:00
|
|
|
feedComment(ch, true);
|
2016-04-16 17:51:25 +00:00
|
|
|
++ch;
|
2017-02-03 20:07:26 +00:00
|
|
|
++lineNumber;
|
2016-04-16 17:51:25 +00:00
|
|
|
insideComment = InsideComment::None;
|
|
|
|
} else if (insideComment == InsideComment::None && currentChar == '/' && nextChar == '*') {
|
|
|
|
feedContent(ch);
|
|
|
|
ch += 2;
|
|
|
|
insideComment = InsideComment::MultiLine;
|
|
|
|
} else if (insideComment == InsideComment::MultiLine && currentChar == '*' && nextChar == '/') {
|
|
|
|
ch += 2;
|
|
|
|
feedComment(ch);
|
|
|
|
insideComment = InsideComment::None;
|
|
|
|
} else if (insideComment == InsideComment::MultiLine && currentChar == '\r' && nextChar == '\n') {
|
|
|
|
feedComment(ch);
|
|
|
|
ch += 2;
|
2017-02-03 20:07:26 +00:00
|
|
|
++lineNumber;
|
2016-04-16 17:51:25 +00:00
|
|
|
feedContent(ch);
|
|
|
|
} else if (insideComment == InsideComment::MultiLine && currentChar == '\n') {
|
|
|
|
feedComment(ch);
|
|
|
|
++ch;
|
2017-02-03 20:07:26 +00:00
|
|
|
++lineNumber;
|
2016-04-16 17:51:25 +00:00
|
|
|
feedContent(ch);
|
|
|
|
} else {
|
2017-02-03 20:07:26 +00:00
|
|
|
if (currentChar == '\n') {
|
|
|
|
++lineNumber;
|
|
|
|
}
|
2016-04-16 17:51:25 +00:00
|
|
|
++ch;
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 20:07:26 +00:00
|
|
|
singleLineComments_.resize(lineNumber + 1);
|
2016-04-16 17:51:25 +00:00
|
|
|
|
|
|
|
if (insideComment == InsideComment::MultiLine) {
|
|
|
|
common::logError(kErrorUnexpectedEndOfFile, filepath_);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (insideComment == InsideComment::None && end > offset) {
|
2016-04-18 22:00:54 +00:00
|
|
|
if (result_.isEmpty()) {
|
|
|
|
result_ = content_;
|
2016-04-16 17:51:25 +00:00
|
|
|
} else {
|
2016-04-18 22:00:54 +00:00
|
|
|
result_.append(offset, end - offset);
|
2016-04-16 17:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-03 20:07:26 +00:00
|
|
|
QVector<QByteArray> CleanFile::singleLineComments() const {
|
|
|
|
return singleLineComments_;
|
|
|
|
}
|
|
|
|
|
2016-04-16 17:51:25 +00:00
|
|
|
LogStream CleanFile::logError(int code, int line) const {
|
|
|
|
return common::logError(code, filepath_, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace common
|
|
|
|
} // namespace codegen
|