int main(int argc, char *argv[]) { if (argc != 4) { printf("Usage: %s <source file or directory> <destination file> <marker>\nExample: %s specs ../TLValues.hpp TLValues\n", argv[0], argv[0]); return 0; } QCoreApplication app(argc, argv); static const QStringList arguments = app.arguments(); static const int indentation = 8; QString indentationStr(indentation, QChar(' ')); QString code; QDir argDir(arguments.at(1)); if (argDir.exists()) { const QStringList filesList = argDir.entryList(QStringList() << "*.txt", QDir::Files|QDir::Readable, QDir::Name); for (int i = 0; i < filesList.count(); ++i) { bool closeEnum = i == filesList.count() - 1; code.append(indentationStr + QString("// From %1:\n").arg(filesList.at(i))); code.append(generateCode(argDir.absolutePath() + "/" + filesList.at(i), indentation, closeEnum)); if (!closeEnum) { code.append(QChar('\n')); } } } else { code.append(generateCode(arguments.at(1), indentation, /* closeEnum */ true)); } if (replacingHelper(arguments.at(2), indentation, arguments.at(3), code)) { printf("File successfully updated.\n"); } return 0; }
StatusCode generate(SchemaFormat format, const QString &specFileName) { QFile specsFile(specFileName); specsFile.open(QIODevice::ReadOnly); const QByteArray data = specsFile.readAll(); if (data.isEmpty()) { printf("Unable to read the file.\n"); return InvalidArgument; } specsFile.close(); GeneratorNG generator; bool success = true; switch (format) { case JsonFormat: success = generator.loadDataFromJson(data); break; case TextFormat: success = generator.loadDataFromText(data); break; } if (!success) { printf("Unable to parse the scheme.\n"); return SchemaReadError; } generator.generate(); replacingHelper(QLatin1String("../TLValues.hpp"), 8, QLatin1String("TLValues"), generator.codeOfTLValues); replacingHelper(QLatin1String("../TLTypes.hpp"), 0, QLatin1String("TLTypes"), generator.codeOfTLTypes); replacingHelper(QLatin1String("../CTelegramStream.hpp"), 4, QLatin1String("read operators"), generator.codeStreamReadDeclarations); replacingHelper(QLatin1String("../CTelegramStream.cpp"), 0, QLatin1String("read operators implementation"), generator.codeStreamReadDefinitions); replacingHelper(QLatin1String("../CTelegramStream.cpp"), 0, QLatin1String("vector read templates instancing"), generator.codeStreamReadTemplateInstancing); replacingHelper(QLatin1String("../CTelegramStream.hpp"), 4, QLatin1String("write operators"), generator.codeStreamWriteDeclarations); replacingHelper(QLatin1String("../CTelegramStream.cpp"), 0, QLatin1String("write operators implementation"), generator.codeStreamWriteDefinitions); replacingHelper(QLatin1String("../CTelegramStream.cpp"), 0, QLatin1String("vector write templates instancing"), generator.codeStreamWriteTemplateInstancing); replacingHelper(QLatin1String("../CTelegramConnection.hpp"), 4, QLatin1String("Telegram API methods declaration"), generator.codeConnectionDeclarations); replacingHelper(QLatin1String("../CTelegramConnection.cpp"), 0, QLatin1String("Telegram API methods implementation"), generator.codeConnectionDefinitions); replacingHelper(QLatin1String("../TLTypesDebug.hpp"), 0, QLatin1String("TLTypes debug operators"), generator.codeDebugWriteDeclarations); replacingHelper(QLatin1String("../TLTypesDebug.cpp"), 0, QLatin1String("TLTypes debug operators"), generator.codeDebugWriteDefinitions); printf("Spec file successfully used for generation.\n"); return NoError; }