Created
June 6, 2021 04:26
-
-
Save fffaraz/2565fb35649b3b800d2154efa1621e07 to your computer and use it in GitHub Desktop.
telegram_emojis.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include <QFile> | |
#include <QDataStream> | |
#include <QDebug> | |
#include <iostream> | |
namespace Ui { | |
namespace Emoji { | |
constexpr auto kPostfix = 0xFE0FU; | |
class One {}; | |
} | |
} | |
using EmojiPtr = const Ui::Emoji::One*; | |
Ui::Emoji::One globalOne; | |
struct LangPackEmoji { | |
EmojiPtr emoji = nullptr; | |
QString text; | |
}; | |
struct LangPackData { | |
int version = 0; | |
int maxKeyLength = 0; | |
std::map<QString, std::vector<LangPackEmoji>> emoji; | |
}; | |
bool MustAddPostfix(const QString &text) { | |
if (text.size() != 1) { | |
return false; | |
} | |
const auto code = text[0].unicode(); | |
return (code == 0x2122U) || (code == 0xA9U) || (code == 0xAEU); | |
} | |
inline EmojiPtr Find(const QString &text, int *outLength = nullptr) { | |
//qDebug() << "Find" << text; | |
return &globalOne; | |
} | |
EmojiPtr FindExact(const QString &text) { | |
auto length = 0; | |
const auto result = Find(text, &length); | |
return (length < text.size()) ? nullptr : result; | |
} | |
LangPackData ReadLocalCache() { | |
QFile file("C:/Users/Faraz/AppData/Roaming/Telegram Desktop/tdata/emoji/keywords/fa"); | |
if (!file.open(QIODevice::ReadOnly)) { | |
qDebug() << "ReadLocalCache 1"; | |
return {}; | |
} | |
auto result = LangPackData(); | |
QDataStream stream(&file); | |
stream.setVersion(QDataStream::Qt_5_1); | |
auto version = qint32(); | |
auto count = qint32(); | |
stream | |
>> version | |
>> count; | |
if (version < 0 || count < 0 || stream.status() != QDataStream::Ok) { | |
qDebug() << "ReadLocalCache 2"; | |
return {}; | |
} | |
for (auto i = 0; i != count; ++i) { | |
auto key = QString(); | |
auto size = qint32(); | |
stream | |
>> key | |
>> size; | |
if (size < 0 || stream.status() != QDataStream::Ok) { | |
qDebug() << "ReadLocalCache 3"; | |
return {}; | |
} | |
auto &list = result.emoji[key]; | |
for (auto j = 0; j != size; ++j) { | |
auto text = QString(); | |
stream >> text; | |
if (stream.status() != QDataStream::Ok) { | |
qDebug() << "ReadLocalCache 4"; | |
return {}; | |
} | |
const auto emoji = MustAddPostfix(text) | |
? (text + QChar(Ui::Emoji::kPostfix)) | |
: text; | |
const auto entry = LangPackEmoji{ FindExact(emoji), text }; | |
if (!entry.emoji) { | |
//qDebug() << "ReadLocalCache 5"; | |
//return {}; | |
} | |
list.push_back(entry); | |
} | |
result.maxKeyLength = std::max(result.maxKeyLength, key.size()); | |
} | |
result.version = version; | |
qDebug() << "ReadLocalCache 6"; | |
return result; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
QCoreApplication a(argc, argv); | |
auto x = ReadLocalCache(); | |
qDebug() << "version" << x.version; | |
qDebug() << "maxKeyLength" << x.maxKeyLength; | |
qDebug() << "emoji" << x.emoji.size(); | |
for (const auto& [key, value] : x.emoji) { | |
std::cout << key.toUtf8().toBase64().toStdString() << std::endl; | |
} | |
return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment