#include "GenCode.h" #include #include #include #include GenCode::GenCode(QObject *parent) : QObject(parent) { } GenCode::~GenCode() { } int GenCode::analyzePrjFile(QString prjPath) { QFile file(prjPath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return -1; } QXmlStreamReader xmlReader(&file); while (xmlReader.readNextStartElement()) { if (xmlReader.name() == QLatin1String("Project")) { while (xmlReader.readNextStartElement()) { if (xmlReader.name() == QLatin1String("Name")) { modelName = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("CodePath")) { codePath = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("ClassName")) { className = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("Author")) { author = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("Version")) { version = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("Description")) { description = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("CreateTime")) { createTime = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("ChangeTime")) { changeTime = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("Node")) { node = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("Priority")) { priority = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("MathPath")) { mathPath = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("MathHeaderPath")) { mathHeaderPath = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("MathEntryPoint")) { mathEntryPoint = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("MathEntryParam")) { mathEntryParam = xmlReader.readElementText(); } else if (xmlReader.name() == QLatin1String("CommandList")) { while (xmlReader.readNextStartElement()) { if (xmlReader.name() == QLatin1String("Command")) { CommandInfo temp; temp.name = xmlReader.attributes().value(QLatin1String("Name")).toString(); temp.description = xmlReader.attributes() .value(QLatin1String("Description")) .toString(); temp.callbackFunction = xmlReader.attributes() .value(QLatin1String("CallbackFunction")) .toString(); commandList.push_back(temp); } else { xmlReader.skipCurrentElement(); } } } else { xmlReader.skipCurrentElement(); } } } else { xmlReader.skipCurrentElement(); } } file.close(); int ret = 0; //error check if (modelName.isEmpty()) { ret = ret | 1; } if (codePath.isEmpty()) { ret = ret | 2; } if (className.isEmpty()) { ret = ret | 4; } if (node.isEmpty()) { ret = ret | 8; } if (priority.isEmpty()) { ret = ret | 16; } //warning check if (author.isEmpty() || version.isEmpty() || createTime.isEmpty() || changeTime.isEmpty() || description.isEmpty()) { ret = ret | 32; } if (mathPath.isEmpty() || mathHeaderPath.isEmpty() || mathEntryPoint.isEmpty() || mathEntryParam.isEmpty()) { ret = ret | 64; } if (commandList.size() == 0) { ret = ret | 128; } return ret; } bool GenCode::genCMakeFile() { QString templateFilePath = QDir::currentPath() + "/template/CMakeLists.txt.tmp"; QString outputFilePath = codePath + "/" + "CMakeLists.txt"; QFile templateFile(templateFilePath); if (!templateFile.open(QIODevice::ReadOnly | QIODevice::Text)) { return false; } QString content = templateFile.readAll(); templateFile.close(); content.replace("Template", className); content.replace("TEMPLATE", className.toUpper()); QFile outputFile(outputFilePath); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } outputFile.write(content.toUtf8()); outputFile.close(); return true; } bool GenCode::genHeaderFile() { QString templateFilePath = QDir::currentPath() + "/template/Template.h.tmp"; QString outputFilePath = codePath + "/" + className + ".h"; QFile templateFile(templateFilePath); if (!templateFile.open(QIODevice::ReadOnly | QIODevice::Text)) { return false; } QString content = templateFile.readAll(); templateFile.close(); content.replace("Template", className); content.replace("TEMPLATE", className.toUpper()); QFile outputFile(outputFilePath); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } outputFile.write(content.toUtf8()); outputFile.close(); return true; } bool GenCode::genSourceFile() { QString templateFilePath = QDir::currentPath() + "/template/Template.cpp.tmp"; QString outputFilePath = codePath + "/" + className + ".cpp"; QFile templateFile(templateFilePath); if (!templateFile.open(QIODevice::ReadOnly | QIODevice::Text)) { return false; } QString content = templateFile.readAll(); templateFile.close(); content.replace("Template", className); if (mathEntryPoint.isEmpty()) { content.replace("EntryPoint", "your_entry_point"); } else { content.replace("EntryPoint", mathEntryPoint); } QFile outputFile(outputFilePath); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } outputFile.write(content.toUtf8()); outputFile.close(); return true; } bool GenCode::genPrivateHeaderFile() { QString templateFilePath = QDir::currentPath() + "/template/Template_p.h.tmp"; QString outputFilePath = codePath + "/" + className + "_p.h"; QFile templateFile(templateFilePath); if (!templateFile.open(QIODevice::ReadOnly | QIODevice::Text)) { return false; } QString content = templateFile.readAll(); templateFile.close(); content.replace("Template", className); if (mathHeaderPath.isEmpty()) { content.replace("MathHeaderPath", "your_math_header_path"); } else { content.replace("MathHeaderPath", mathHeaderPath); } if (mathEntryParam.isEmpty()) { content.replace("MathEntryParam", "your_math_entry_param"); } else { content.replace("MathEntryParam", mathEntryParam); } QFile outputFile(outputFilePath); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } outputFile.write(content.toUtf8()); outputFile.close(); return true; } bool GenCode::genConfigFile() { QString outputFilePath = codePath + "/" + className + ".mcfg"; QFile outputFile(outputFilePath); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } QXmlStreamWriter xmlWriter(&outputFile); xmlWriter.setAutoFormatting(true); xmlWriter.writeStartDocument(); xmlWriter.writeStartElement("Model"); xmlWriter.writeTextElement("Name", className); xmlWriter.writeTextElement("Description", description); xmlWriter.writeTextElement("Author", author); xmlWriter.writeTextElement("Version", version); xmlWriter.writeTextElement("CreateTime", createTime); xmlWriter.writeTextElement("ChangeTime", changeTime); xmlWriter.writeTextElement("Node", node); xmlWriter.writeTextElement("Priority", priority); xmlWriter.writeTextElement("MathLib", mathPath); xmlWriter.writeStartElement("CommandList"); for (const auto &command : commandList) { xmlWriter.writeStartElement("Command"); xmlWriter.writeAttribute("Name", command.name); xmlWriter.writeAttribute("Description", command.description); xmlWriter.writeAttribute("Call", command.callbackFunction); xmlWriter.writeEndElement(); } xmlWriter.writeEndElement(); //CommandList xmlWriter.writeEndElement(); //Model xmlWriter.writeEndDocument(); outputFile.close(); return true; } bool GenCode::genGlobalFile() { QString templateFilePath = QDir::currentPath() + "/template/Template_global.h.tmp"; QString outputFilePath = codePath + "/" + className + "_global.h"; QFile templateFile(templateFilePath); if (!templateFile.open(QIODevice::ReadOnly | QIODevice::Text)) { return false; } QString content = templateFile.readAll(); templateFile.close(); content.replace("TEMPLATE", className.toUpper()); QFile outputFile(outputFilePath); if (!outputFile.open(QIODevice::WriteOnly | QIODevice::Text)) { return false; } outputFile.write(content.toUtf8()); outputFile.close(); return true; }