100 lines
2.2 KiB
C++
Raw Permalink Normal View History

#include "GenIDL.h"
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <filesystem>
namespace fs = std::filesystem;
std::string GenIDL::idlFilePath = "";
bool GenIDL::createConfigDirectory(const std::string &configName)
{
std::string dirPath = GetXNCoreEnv() + "/IDL/" + configName;
try {
fs::create_directories(dirPath);
GenIDL::idlFilePath = dirPath + "/" + configName + ".idl";
return true;
} catch (const fs::filesystem_error &) {
GenIDL::idlFilePath = "";
return false;
}
}
std::string GenIDL::generateStructDefinition(const StructInterfaceData &structData)
{
std::stringstream ss;
// 生成结构体名称
std::string structName = structData.modelStructName;
// 生成结构体定义
ss << "\t\t\tstruct " << structName << "\n\t\t\t{\n";
// 生成成员变量
for (const auto &interface : structData.interfaceData) {
ss << "\t\t\t\t@optional ";
ss << interface.interfaceType << " ";
// 生成变量名
ss << interface.interfaceName;
// 如果是数组,添加数组维度
if (interface.interfaceIsArray) {
if (interface.interfaceArraySize_2 > 1) {
ss << "[" << interface.interfaceArraySize_1 << "]["
<< interface.interfaceArraySize_2 << "]";
} else {
ss << "[" << interface.interfaceArraySize_1 << "]";
}
}
ss << ";\n";
}
ss << "\t\t\t};\n";
return ss.str();
}
std::string GenIDL::generateIDLContent(const AllInterfaceData &interfaceData)
{
std::stringstream ss;
// 生成模块定义
ss << "module " << interfaceData.systemName << "\n{\n";
ss << "\tmodule " << interfaceData.planeName << "\n\t{\n";
// 遍历ATA数据
for (const auto &ataData : interfaceData.ataInterfaceData) {
ss << "\t\tmodule " << ataData.ataName << "\n\t\t{\n";
// 遍历结构体数据
for (const auto &structData : ataData.structInterfaceData) {
ss << generateStructDefinition(structData);
}
ss << "\t\t};\n";
}
ss << "\t};\n};\n";
return ss.str();
}
bool GenIDL::generateIDL(const AllInterfaceData &interfaceData)
{
if (GenIDL::idlFilePath.empty()) {
return false;
}
std::ofstream idlFile(GenIDL::idlFilePath);
if (!idlFile.is_open()) {
return false;
}
idlFile << generateIDLContent(interfaceData);
idlFile.close();
return true;
}