int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); if (argc == 1) { showWarning(WarnOptions); return EXIT_FAILURE; } int res = EXIT_SUCCESS; int opt, oi = -1; flags = NoneFlag; while (res == EXIT_SUCCESS && (opt = getopt_long(argc, argv, "a:cd:f:hilprs:vw:", long_options, &oi)) != -1) { switch (opt) { case 'a': addOptArg = makeWideCharString(optarg, 0); break; case 'c': flags |= InitFlag; break; case 'd': delOptArg = makeWideCharString(optarg, 0); break; case 'f': fieldsList = makeWideCharString(optarg, 0); break; case 'h': showHelp(); return EXIT_SUCCESS; case 'i': flags |= InfoFlag; break; case 'l': flags |= ListFlag; break; case 'p': flags |= PropFlag; break; case 'r': flags |= RecurFlag; break; case 's': setOptArg = makeWideCharString(optarg, 0); break; case 'v': flags |= VersionFlag; break; case 'w': whrOptArg = makeWideCharString(optarg, 0); break; case MoveFileOption: flags |= MoveFileFlag; break; default: showWarning(WarnOther); res = EXIT_FAILURE; break; } } if (res != EXIT_SUCCESS) return res; res = EXIT_FAILURE; enum WarnMode warn = WarnOptions; if (addOptArg == NULL && delOptArg == NULL && setOptArg == NULL) { int filesCnt = argc - optind; if (flags == InitFlag) // -c option { if (filesCnt == 0 && whrOptArg == NULL && fieldsList == NULL) { res = tagsCreateIndex(); warn = WarnNone; } } else if (flags == InfoFlag) // -i option { if (filesCnt > 0) { if (whrOptArg == NULL && fieldsList == NULL) { res = tagsStatus(&argv[optind], filesCnt); warn = WarnNone; } } else warn = WarnFiles; } else if ((flags & ListFlag) != 0) // -l option { if ((flags & ~(ListFlag | RecurFlag)) == 0 && filesCnt == 0) { res = tagsList(fieldsList, whrOptArg); warn = WarnNone; } } else if ((flags & PropFlag) != 0) // -p option { if ((flags & ~(PropFlag | RecurFlag)) == 0 && filesCnt == 0 && whrOptArg == NULL && fieldsList == NULL) { res = tagsShowProps(); warn = WarnNone; } } else if (flags == VersionFlag) // -v option { if (filesCnt == 0 && whrOptArg == NULL && fieldsList == NULL) { showVersion(); res = EXIT_SUCCESS; warn = WarnNone; } } else if (flags == MoveFileFlag) // --rename-file option { if (filesCnt == 2 && whrOptArg == NULL && fieldsList == NULL) { res = moveFile(&argv[optind]); warn = WarnNone; } } } else // -a -d -s options { if (flags == NoneFlag) { warn = WarnFiles; int filesCnt = argc - optind; if (filesCnt > 0) { res = tagsUpdateFileInfo(&argv[optind], filesCnt, addOptArg, delOptArg, setOptArg, whrOptArg); warn = WarnNone; } } } freeResources(); if (warn != WarnNone) showWarning(warn); return res; }
bool genLang(const QString &lang_in, const QString &lang_out) { QString lang_cpp = lang_out + ".cpp", lang_h = lang_out + ".h"; QFile f(lang_in); if (!f.open(QIODevice::ReadOnly)) { cout << "Could not open lang input file '" << lang_in.toUtf8().constData() << "'!\n"; QCoreApplication::exit(1); return false; } QByteArray checkCodec = f.read(3); if (checkCodec.size() < 3) { cout << "Bad lang input file '" << lang_in.toUtf8().constData() << "'!\n"; QCoreApplication::exit(1); return false; } f.seek(0); QByteArray data; int skip = 0; if ((checkCodec.at(0) == '\xFF' && checkCodec.at(1) == '\xFE') || (checkCodec.at(0) == '\xFE' && checkCodec.at(1) == '\xFF') || (checkCodec.at(1) == 0)) { QTextStream stream(&f); stream.setCodec("UTF-16"); QString string = stream.readAll(); if (stream.status() != QTextStream::Ok) { cout << "Could not read valid UTF-16 file '" << lang_in.toUtf8().constData() << "'!\n"; QCoreApplication::exit(1); return false; } f.close(); data = string.toUtf8(); } else if (checkCodec.at(0) == 0) { QByteArray tmp = "\xFE\xFF" + f.readAll(); // add fake UTF-16 BOM f.close(); QTextStream stream(&tmp); stream.setCodec("UTF-16"); QString string = stream.readAll(); if (stream.status() != QTextStream::Ok) { cout << "Could not read valid UTF-16 file '" << lang_in.toUtf8().constData() << "'!\n"; QCoreApplication::exit(1); return false; } data = string.toUtf8(); } else { data = f.readAll(); if (checkCodec.at(0) == '\xEF' && checkCodec.at(1) == '\xBB' && checkCodec.at(2) == '\xBF') { skip = 3; // skip UTF-8 BOM } } const char *text = data.constData() + skip, *end = text + data.size() - skip; try { while (text < end) { readKeyValue(text, end); } QByteArray cppText, hText; { QTextStream tcpp(&cppText), th(&hText); tcpp.setCodec("ISO 8859-1"); th.setCodec("ISO 8859-1"); th << "\ /*\n\ Created from \'/Resources/lang.txt\' by \'/MetaLang\' project\n\ \n\ WARNING! All changes made in this file will be lost!\n\ \n\ This file is part of Telegram Desktop,\n\ the official desktop version of Telegram messaging app, see https://telegram.org\n\ \n\ Telegram Desktop is free software: you can redistribute it and/or modify\n\ it under the terms of the GNU General Public License as published by\n\ the Free Software Foundation, either version 3 of the License, or\n\ (at your option) any later version.\n\ \n\ It is distributed in the hope that it will be useful,\n\ but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ GNU General Public License for more details.\n\ \n\ In addition, as a special exception, the copyright holders give permission\n\ to link the code of portions of this program with the OpenSSL library.\n\ \n\ Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE\n\ Copyright (c) 2014-2015 John Preston, https://desktop.telegram.org\n\ */\n"; th << "#pragma once\n\n"; for (int i = 0, l = tagsOrder.size(); i < l; ++i) { th << "enum lngtag_" << tagsOrder[i] << " { lt_" << tagsOrder[i] << " = " << i << " };\n"; } th << "static const ushort lngtags_cnt = " << tagsOrder.size() << ";\n"; th << "static const ushort lngtags_max_counted_values = " << MaxCountedValues << ";\n"; th << "\n"; th << "enum LangKey {\n"; for (int i = 0, l = keysOrder.size(); i < l; ++i) { if (keysTags[keysOrder[i]].isEmpty()) { th << "\t" << keysOrder[i] << (i ? "" : " = 0") << ",\n"; } else { th << "\t" << keysOrder[i] << "__tagged" << (i ? "" : " = 0") << ",\n"; QMap<QByteArray, QVector<QString> > &countedTags(keysCounted[keysOrder[i]]); if (!countedTags.isEmpty()) { for (QMap<QByteArray, QVector<QString> >::const_iterator j = countedTags.cbegin(), e = countedTags.cend(); j != e; ++j) { const QVector<QString> &counted(*j); for (int k = 0, s = counted.size(); k < s; ++k) { th << "\t" << keysOrder[i] << "__" + j.key() + QString::number(k).toUtf8() << ",\n"; } } } } } th << "\n\tlngkeys_cnt\n"; th << "};\n\n"; th << "LangString lang(LangKey key);\n\n"; th << "LangString langOriginal(LangKey key);\n\n"; for (int i = 0, l = keysOrder.size(); i < l; ++i) { QVector<QByteArray> &tagsList(keysTags[keysOrder[i]]); if (tagsList.isEmpty()) continue; QMap<QByteArray, QVector<QString> > &countedTags(keysCounted[keysOrder[i]]); th << "inline LangString " << keysOrder[i] << "("; for (int j = 0, s = tagsList.size(); j < s; ++j) { if (countedTags[tagsList[j]].isEmpty()) { th << "lngtag_" << tagsList[j] << ", const QString &" << tagsList[j] << "__val"; } else { th << "lngtag_" << tagsList[j] << ", float64 " << tagsList[j] << "__val"; } if (j + 1 < s) th << ", "; } th << ") {\n"; th << "\treturn lang(" << keysOrder[i] << "__tagged)"; for (int j = 0, s = tagsList.size(); j < s; ++j) { if (countedTags[tagsList[j]].isEmpty()) { th << ".tag(lt_" << tagsList[j] << ", " << tagsList[j] << "__val)"; } else { th << ".tag(lt_" << tagsList[j] << ", langCounted(" << keysOrder[i] << "__" << tagsList[j] << "0, lt_" << tagsList[j] << ", " << tagsList[j] << "__val))"; } } th << ";\n"; th << "}\n"; } tcpp << "\ /*\n\ Created from \'/Resources/lang.txt\' by \'/MetaLang\' project\n\ \n\ WARNING! All changes made in this file will be lost!\n\ \n\ This file is part of Telegram Desktop,\n\ the official desktop version of Telegram messaging app, see https://telegram.org\n\ \n\ Telegram Desktop is free software: you can redistribute it and/or modify\n\ it under the terms of the GNU General Public License as published by\n\ the Free Software Foundation, either version 3 of the License, or\n\ (at your option) any later version.\n\ \n\ It is distributed in the hope that it will be useful,\n\ but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ GNU General Public License for more details.\n\ \n\ In addition, as a special exception, the copyright holders give permission\n\ to link the code of portions of this program with the OpenSSL library.\n\ \n\ Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE\n\ Copyright (c) 2014-2015 John Preston, https://desktop.telegram.org\n\ */\n"; tcpp << "#include \"stdafx.h\"\n#include \"lang.h\"\n\n"; tcpp << "namespace {\n"; tcpp << "\tconst char *_langKeyNames[lngkeys_cnt] = {\n"; for (int i = 0, l = keysOrder.size(); i < l; ++i) { if (keysTags[keysOrder[i]].isEmpty()) { tcpp << "\t\t\"" << keysOrder[i] << "\",\n"; } else { tcpp << "\t\t\"" << keysOrder[i] << "__tagged\",\n"; QMap<QByteArray, QVector<QString> > &countedTags(keysCounted[keysOrder[i]]); if (!countedTags.isEmpty()) { for (QMap<QByteArray, QVector<QString> >::const_iterator j = countedTags.cbegin(), e = countedTags.cend(); j != e; ++j) { const QVector<QString> &counted(*j); for (int k = 0, s = counted.size(); k < s; ++k) { tcpp << "\t\t\"" << keysOrder[i] << "__" + j.key() + QString::number(k).toUtf8() << "\",\n"; } } } } } tcpp << "\t};\n\n"; tcpp << "\tLangString _langValues[lngkeys_cnt], _langValuesOriginal[lngkeys_cnt];\n\n"; tcpp << "\tvoid set(LangKey key, const QString &val) {\n"; tcpp << "\t\t_langValues[key] = val;\n"; tcpp << "\t}\n\n"; tcpp << "\tclass LangInit {\n"; tcpp << "\tpublic:\n"; tcpp << "\t\tLangInit() {\n"; for (int i = 0, l = keysOrder.size(); i < l; ++i) { writeCppKey(tcpp, keysOrder[i] + (keysTags[keysOrder[i]].isEmpty() ? "" : "__tagged"), keys[keysOrder[i]]); QMap<QByteArray, QVector<QString> > &countedTags(keysCounted[keysOrder[i]]); if (!countedTags.isEmpty()) { for (QMap<QByteArray, QVector<QString> >::const_iterator j = countedTags.cbegin(), e = countedTags.cend(); j != e; ++j) { const QVector<QString> &counted(*j); for (int k = 0, s = counted.size(); k < s; ++k) { writeCppKey(tcpp, keysOrder[i] + "__" + j.key() + QString::number(k).toUtf8(), counted[k]); } } } } tcpp << "\t\t}\n"; tcpp << "\t};\n\n"; tcpp << "\tLangInit _langInit;\n\n"; tcpp << "\tinline bool _lngEquals(const QByteArray &key, int from, int len, const char *value, int size) {\n"; tcpp << "\t\tif (size != len || from + len > key.size()) return false;\n"; tcpp << "\t\tfor (const char *v = key.constData() + from, *e = v + len; v != e; ++v, ++value) {\n"; tcpp << "\t\t\tif (*v != *value) return false;\n"; tcpp << "\t\t}\n"; tcpp << "\t\treturn true; \n"; tcpp << "\t}\n"; tcpp << "}\n\n"; tcpp << "#define LNG_EQUALS_PART(key, from, len, value) _lngEquals(key, from, len, value, sizeof(value) - 1)\n"; tcpp << "#define LNG_EQUALS_TAIL(key, from, value) _lngEquals(key, from, key.size() - from, value, sizeof(value) - 1)\n"; tcpp << "#define LNG_EQUALS(key, value) _lngEquals(key, 0, key.size(), value, sizeof(value) - 1)\n\n"; tcpp << "LangString lang(LangKey key) {\n"; tcpp << "\treturn (key < 0 || key > lngkeys_cnt) ? QString() : _langValues[key];\n"; tcpp << "}\n\n"; tcpp << "LangString langOriginal(LangKey key) {\n"; tcpp << "\treturn (key < 0 || key > lngkeys_cnt || _langValuesOriginal[key] == qsl(\"{}\")) ? QString() : (_langValuesOriginal[key].isEmpty() ? _langValues[key] : _langValuesOriginal[key]);\n"; tcpp << "}\n\n"; tcpp << "const char *langKeyName(LangKey key) {\n"; tcpp << "\treturn (key < 0 || key > lngkeys_cnt) ? \"\" : _langKeyNames[key];\n"; tcpp << "}\n\n"; tcpp << "ushort LangLoader::tagIndex(const QByteArray &tag) const {\n"; tcpp << "\tif (tag.isEmpty()) return lngtags_cnt;\n\n"; if (!tags.isEmpty()) { QString tab("\t"); tcpp << "\tconst char *ch = tag.constData(), *e = tag.constData() + tag.size();\n"; QByteArray current; int depth = current.size(); tcpp << "\tswitch (*ch) {\n"; for (LangTags::const_iterator i = tags.cbegin(), j = i + 1, e = tags.cend(); i != e; ++i) { QByteArray tag = i.key(); while (depth > 0 && tag.mid(0, depth) != current) { tcpp << tab.repeated(depth + 1) << "}\n"; current.chop(1); --depth; tcpp << tab.repeated(depth + 1) << "break;\n"; } do { if (tag == current) break; char ich = i.key().at(current.size()); tcpp << tab.repeated(current.size() + 1) << "case '" << ich << "':\n"; if (j == e || ich != ((j.key().size() > depth) ? j.key().at(depth) : 0)) { if (tag == current + ich) { tcpp << tab.repeated(depth + 1) << "\tif (ch + " << (depth + 1) << " == e) return lt_" << tag << ";\n"; } else { tcpp << tab.repeated(depth + 1) << "\tif (LNG_EQUALS_TAIL(tag, " << (depth + 1) << ", \"" << i.key().mid(depth + 1) << "\")) return lt_" << tag << ";\n"; } tcpp << tab.repeated(depth + 1) << "break;\n"; break; } ++depth; current += ich; if (tag == current) { tcpp << tab.repeated(depth + 1) << "if (ch + " << depth << " == e) {\n"; tcpp << tab.repeated(depth + 1) << "\treturn lt_" << tag << ";\n"; tcpp << tab.repeated(depth + 1) << "}\n"; } tcpp << tab.repeated(depth + 1) << "if (ch + " << depth << " < e) switch (*(ch + " << depth << ")) {\n"; } while (true); ++j; } while (QByteArray() != current) { tcpp << tab.repeated(depth + 1) << "}\n"; current.chop(1); --depth; tcpp << tab.repeated(depth + 1) << "break;\n"; } tcpp << "\t}\n\n"; } tcpp << "\treturn lngtags_cnt;\n"; tcpp << "}\n\n"; tcpp << "LangKey LangLoader::keyIndex(const QByteArray &key) const {\n"; tcpp << "\tif (key.size() < 5 || !LNG_EQUALS_PART(key, 0, 4, \"lng_\")) return lngkeys_cnt;\n\n"; if (!keys.isEmpty()) { QString tab("\t"); tcpp << "\tconst char *ch = key.constData(), *e = key.constData() + key.size();\n"; QByteArray current("lng_"); int depth = current.size(); tcpp << "\tswitch (*(ch + " << depth << ")) {\n"; for (LangKeys::const_iterator i = keys.cbegin(), j = i + 1, e = keys.cend(); i != e; ++i) { QByteArray key = i.key(); while (key.mid(0, depth) != current) { tcpp << tab.repeated(depth - 3) << "}\n"; current.chop(1); --depth; tcpp << tab.repeated(depth - 3) << "break;\n"; } do { if (key == current) break; char ich = i.key().at(current.size()); tcpp << tab.repeated(current.size() - 3) << "case '" << ich << "':\n"; if (j == e || ich != ((j.key().size() > depth) ? j.key().at(depth) : 0)) { if (key == current + ich) { tcpp << tab.repeated(depth - 3) << "\tif (ch + " << (depth + 1) << " == e) return " << key << (keysTags[key].isEmpty() ? "" : "__tagged") << ";\n"; } else { tcpp << tab.repeated(depth - 3) << "\tif (LNG_EQUALS_TAIL(key, " << (depth + 1) << ", \"" << i.key().mid(depth + 1) << "\")) return " << key << (keysTags[key].isEmpty() ? "" : "__tagged") << ";\n"; } tcpp << tab.repeated(depth - 3) << "break;\n"; break; } ++depth; current += ich; if (key == current) { tcpp << tab.repeated(depth - 3) << "if (ch + " << depth << " == e) {\n"; tcpp << tab.repeated(depth - 3) << "\treturn " << key << (keysTags[key].isEmpty() ? "" : "__tagged") << ";\n"; tcpp << tab.repeated(depth - 3) << "}\n"; } tcpp << tab.repeated(depth - 3) << "if (ch + " << depth << " < e) switch (*(ch + " << depth << ")) {\n"; } while (true); ++j; } while (QByteArray("lng_") != current) { tcpp << tab.repeated(depth - 3) << "}\n"; current.chop(1); --depth; tcpp << tab.repeated(depth - 3) << "break;\n"; } tcpp << "\t}\n\n"; } tcpp << "\treturn lngkeys_cnt;\n"; tcpp << "}\n\n"; tcpp << "bool LangLoader::tagReplaced(LangKey key, ushort tag) const {\n"; if (!tags.isEmpty()) { tcpp << "\tswitch (key) {\n"; for (int i = 0, l = keysOrder.size(); i < l; ++i) { QVector<QByteArray> &tagsList(keysTags[keysOrder[i]]); if (tagsList.isEmpty()) continue; tcpp << "\tcase " << keysOrder[i] << "__tagged: {\n"; tcpp << "\t\tswitch (tag) {\n"; for (int j = 0, s = tagsList.size(); j < s; ++j) { tcpp << "\t\tcase lt_" << tagsList[j] << ":\n"; } tcpp << "\t\t\treturn true;\n"; tcpp << "\t\t}\n"; tcpp << "\t} break;\n"; } tcpp << "\t}\n\n"; } tcpp << "\treturn false;"; tcpp << "}\n\n"; tcpp << "LangKey LangLoader::subkeyIndex(LangKey key, ushort tag, ushort index) const {\n"; tcpp << "\tif (index >= lngtags_max_counted_values) return lngkeys_cnt;\n\n"; if (!tags.isEmpty()) { tcpp << "\tswitch (key) {\n"; for (int i = 0, l = keysOrder.size(); i < l; ++i) { QVector<QByteArray> &tagsList(keysTags[keysOrder[i]]); if (tagsList.isEmpty()) continue; QMap<QByteArray, QVector<QString> > &countedTags(keysCounted[keysOrder[i]]); tcpp << "\tcase " << keysOrder[i] << "__tagged: {\n"; tcpp << "\t\tswitch (tag) {\n"; for (int j = 0, s = tagsList.size(); j < s; ++j) { if (!countedTags[tagsList[j]].isEmpty()) { tcpp << "\t\tcase lt_" << tagsList[j] << ": return LangKey(" << keysOrder[i] << "__" << tagsList[j] << "0 + index);\n"; } } tcpp << "\t\t}\n"; tcpp << "\t} break;\n"; } tcpp << "\t}\n\n"; } tcpp << "\treturn lngkeys_cnt;"; tcpp << "}\n\n"; tcpp << "bool LangLoader::feedKeyValue(LangKey key, const QString &value) {\n"; tcpp << "\tif (key < lngkeys_cnt) {\n"; tcpp << "\t\t_found[key] = 1;\n"; tcpp << "\t\tif (_langValuesOriginal[key].isEmpty()) {\n"; tcpp << "\t\t\t_langValuesOriginal[key] = _langValues[key].isEmpty() ? qsl(\"{}\") : _langValues[key];\n"; tcpp << "\t\t}\n"; tcpp << "\t\t_langValues[key] = value;\n"; tcpp << "\t\treturn true;\n"; tcpp << "\t}\n"; tcpp << "\treturn false;\n"; tcpp << "}\n\n"; } QFile cpp(lang_cpp), h(lang_h); bool write_cpp = true, write_h = true; if (cpp.open(QIODevice::ReadOnly)) { QByteArray wasCpp = cpp.readAll(); if (wasCpp.size() == cppText.size()) { if (!memcmp(wasCpp.constData(), cppText.constData(), cppText.size())) { write_cpp = false; } } cpp.close(); } if (write_cpp) { cout << "lang.cpp updated, writing " << keysOrder.size() << " rows.\n"; if (!cpp.open(QIODevice::WriteOnly)) throw Exception("Could not open lang.cpp for writing!"); if (cpp.write(cppText) != cppText.size()) throw Exception("Could not open lang.cpp for writing!"); } if (h.open(QIODevice::ReadOnly)) { QByteArray wasH = h.readAll(); if (wasH.size() == hText.size()) { if (!memcmp(wasH.constData(), hText.constData(), hText.size())) { write_h = false; } } h.close(); } if (write_h) { cout << "lang.h updated, writing " << keysOrder.size() << " rows.\n"; if (!h.open(QIODevice::WriteOnly)) throw Exception("Could not open lang.h for writing!"); if (h.write(hText) != hText.size()) throw Exception("Could not open lang.h for writing!"); } } catch (exception &e) { cout << e.what() << "\n"; QCoreApplication::exit(1); return false; } return true; }