XNSim/XNEngine/main.cpp

106 lines
2.4 KiB
C++
Executable File

/**
* @file main.cpp
* @author jinchao
* @brief 主函数
* @version 1.0
* @date 2025-02-14
*
* @copyright Copyright (c) 2025 COMAC
*
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include "XNEngine.h"
/**
* @brief 主函数
* @param argc 参数个数
* @param argv 参数列表
* @return 返回值
*/
int main(int argc, char *argv[])
{
XNEngine engine;
//检测输入参数个数
if (argc <= 2) {
std::cerr << "0x1000 The input parameters is too less!" << std::endl;
return -1;
}
std::string configPath;
std::string configId;
bool hasConfigPath = false;
bool hasConfigId = false;
//检查是否包含-f和-id参数
for (int i = 1; i < argc;) {
std::string arg = argv[i];
if ("-f" == arg) {
if ((i + 1) < argc) {
configPath = argv[i + 1];
hasConfigPath = true;
i += 2;
} else {
std::cerr << "0x1004 After the -f parameter, the configuration file path is not "
"specified, "
"the engine will exit!"
<< std::endl;
return -1;
}
} else if ("-id" == arg) {
if ((i + 1) < argc) {
configId = argv[i + 1];
hasConfigId = true;
i += 2;
} else {
std::cerr
<< "0x1005 After the -id parameter, the configuration ID is not specified, "
"the engine will exit!"
<< std::endl;
return -1;
}
} else if ("-test" == arg) {
engine.SetTestMode(true);
i++;
} else {
std::cerr << "0x1007 The parameter " << arg << " is not valid, the engine will exit!"
<< std::endl;
return -1;
}
}
//检查是否同时包含-f和-id参数
if (!hasConfigPath || !hasConfigId) {
std::cerr
<< "0x1006 Please specify either -f <config_file> or -id <config_id>, but not both. "
"The engine will exit!"
<< std::endl;
return -1;
}
//检测配置文件格式
if (hasConfigPath) {
size_t index = configPath.find_last_of('.');
if (index != std::string::npos) {
std::string suffix = configPath.substr(index);
if (suffix != ".xml" && suffix != ".sce") {
std::cerr << "0x1001 The configuration file is not a .xml or .sce "
"file, the engine will exit!"
<< std::endl;
return -1;
}
} else {
std::cerr << "0x1002 The configuration file is not a .xml or .sce "
"file, the engine will exit!"
<< std::endl;
return -1;
}
return engine.Run(configPath);
} else if (hasConfigId) {
return engine.Run(configId);
}
// 引擎运行
return engine.Run(configId);
}