XNSim/XNServiceGenServer/XNServiceGenServer.cpp

157 lines
4.5 KiB
C++
Raw Normal View History

#include "XNServiceGenServer.h"
#include "XNServiceGen.h"
#include "XNCodeZip.h"
#include "XNServiceCompile.h"
#include <cstring>
int XNServiceCodeGen(const char *className, int classNameLen, const char *version, int versionLen,
char *errorMsg, int errorMsgLen)
{
std::string classNameStr(className, classNameLen);
std::string versionStr(version, versionLen);
if (classNameStr.empty() || versionStr.empty()) {
if (errorMsg != nullptr) {
strncpy(errorMsg, "className or version is empty", errorMsgLen);
}
return -1;
}
std::string errorMsgStr;
XNServiceGen serviceGen;
int ret = serviceGen.Initialize(classNameStr, versionStr, errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
ret = serviceGen.GenerateCode(errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return 0;
}
int XNServiceCodeZip(const char *className, int classNameLen, const char *version, int versionLen,
char *dstPath, int dstPathLen, char *errorMsg, int errorMsgLen)
{
std::string classNameStr(className, classNameLen);
std::string versionStr(version, versionLen);
if (classNameStr.empty() || versionStr.empty()) {
if (errorMsg != nullptr) {
strncpy(errorMsg, "className or version is empty", errorMsgLen);
}
return -1;
}
XNServiceGen serviceGen;
std::string errorMsgStr;
int ret = serviceGen.Initialize(classNameStr, versionStr, errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
std::string srcPath = serviceGen.GetCodePath();
std::string dstPathStr = srcPath;
size_t lastSlash = dstPathStr.find_last_of("/\\");
if (lastSlash != std::string::npos) {
dstPathStr = dstPathStr.substr(0, lastSlash);
std::string dirName = srcPath.substr(lastSlash + 1);
dstPathStr += "/" + dirName + ".zip";
} else {
dstPathStr += ".zip";
}
ret = XNCodeZip::Zip(srcPath, dstPathStr, errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
// 将生成的zip文件路径复制到输出参数
if (dstPath != nullptr) {
strncpy(dstPath, dstPathStr.c_str(), dstPathLen);
}
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return 0;
}
int XNServiceCodeUnzip(const char *className, int classNameLen, const char *version, int versionLen,
const char *srcPath, int srcPathLen, char *errorMsg, int errorMsgLen)
{
std::string classNameStr(className, classNameLen);
std::string versionStr(version, versionLen);
if (classNameStr.empty() || versionStr.empty()) {
if (errorMsg != nullptr) {
strncpy(errorMsg, "className or version is empty", errorMsgLen);
}
return -1;
}
XNServiceGen serviceGen;
std::string errorMsgStr;
int ret = serviceGen.Initialize(classNameStr, versionStr, errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
std::string srcPathStr(srcPath, srcPathLen);
std::string dstPathStr = serviceGen.GetCodePath();
ret = XNCodeZip::Unzip(srcPathStr, dstPathStr, errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return 0;
}
int XNServiceCodeCompile(const char *className, int classNameLen, const char *version,
int versionLen, char *errorMsg, int errorMsgLen)
{
std::string classNameStr(className, classNameLen);
std::string versionStr(version, versionLen);
if (classNameStr.empty() || versionStr.empty()) {
if (errorMsg != nullptr) {
strncpy(errorMsg, "className or version is empty", errorMsgLen);
}
return -1;
}
XNServiceGen serviceGen;
std::string errorMsgStr;
int ret = serviceGen.Initialize(classNameStr, versionStr, errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
std::string srcPath = serviceGen.GetCodePath();
ret = XNServiceCompile::Compile(srcPath, errorMsgStr);
if (ret != 0) {
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return ret;
}
if (errorMsg != nullptr) {
strncpy(errorMsg, errorMsgStr.c_str(), errorMsgLen);
}
return 0;
}