// ParseTable.cpp : 定义控制台应用程序的入口点。
//
#include <fstream>
#include <string.h>
#ifdef WIN32
#include <io.h>
#else
#include <dirent.h>
#endif
#include "StringTool.h"
#include "ExePath.h"
#include "TableFile.h"
using namespace common::tool;
std::string g_NameSpace = ""; // 自定义命名空间
bool g_ThreadSpecific = false; // 是否需要线程本地表格数据
std::string g_CsvPath = GetExePath(); // 原始csv目录
std::string g_CppPath = GetExePath(); // 目标cpp目录
void ParseTable2H(const char* outFilePath, const TableFile& inTableFile)
{
std::ofstream of;
of.open(outFilePath, std::ios::out);
if (of)
{
of << "#ifndef __" << inTableFile.m_strClassName.c_str() << "_h__\n";
of << "#define __" << inTableFile.m_strClassName.c_str() << "_h__\n\n";
of << "#include <map>\n";
of << "#include <vector>\n\n";
if (g_ThreadSpecific)
{
of << "#include <boost/thread/tss.hpp>\n\n";
}
std::vector<std::string> spaces;
StringTool::SplitStr2List(g_NameSpace, ".", spaces);
if (0 < spaces.size())
{
for (size_t i = 0; i < spaces.size(); i++)
{
of << "namespace " << spaces[i] << "{\n";
}
of << "\n";
}
// 生成子结构
std::map<int, ChildStruct>::const_iterator it_child_class = inTableFile.m_mapChildStruct.begin();
while (it_child_class != inTableFile.m_mapChildStruct.end())
{
// 子结构:class XXX {...};
of << "class " << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[it_child_class->first].c_str() << "\n";
of << "{\n";
of << "public:\n";
// 构造:XXX() {...}
of << "\t" << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[it_child_class->first].c_str() << "();\n\n";
// 拷贝构造:XXX(const XXX &other) {...}
of << "\t" << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[it_child_class->first].c_str() <<
"(const " << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[it_child_class->first].c_str() << " &other);\n\n";
// 赋值:XXX& operator = (const XXX &other) {...}
of << "\t" << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[it_child_class->first].c_str() <<
"& operator = (const " << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[it_child_class->first].c_str() << " &other);\n\n";
// 赋值:XXX& operator = (const std::string &other) {...}
of << "\t" << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[it_child_class->first].c_str() << "& operator = (const std::string &other);\n\n";
// 各成员属性定义
const ChildStruct& child_class = it_child_class->second;
for (size_t i = 0; i < child_class.m_arrChildName.size(); i++)
{
if (FT_int == child_class.m_arrFieldType[i]) // 有符号数
{
of << "\tint m_" << child_class.m_arrChildName[i].c_str() << ";\n";
}
else if (FT_uint == child_class.m_arrFieldType[i]) // 无符号数
{
of << "\tunsigned int m_" << child_class.m_arrChildName[i].c_str() << ";\n";
}
else if (FT_float == child_class.m_arrFieldType[i]) // 浮点数
{
of << "\tfloat m_" << child_class.m_arrChildName[i].c_str() << ";\n";
}
else if (FT_string == child_class.m_arrFieldType[i]) // 字符串
{
of << "\tstd::string m_" << child_class.m_arrChildName[i].c_str() << ";\n";
}
else // 其他类型忽略,暂不处理
{
}
}
// 子结构:class XXX {...}结束
of << "};\n\n";
it_child_class++;
}
// 类名:class XXX {...};
of << "class " << inTableFile.m_strClassName.c_str() << "\n";
of << "{\n";
of << "public:\n";
// 构造:XXX() {...}
of << "\t" << inTableFile.m_strClassName.c_str() << "();\n\n";
// 拷贝构造:XXX(const XXX &other) {...}
of << "\t" << inTableFile.m_strClassName.c_str() << "(const " << inTableFile.m_strClassName.c_str() << " &other);\n\n";
// 赋值:XXX& operator = (const XXX &other) {...}
of << "\t" << inTableFile.m_strClassName.c_str() << "& operator = (const " << inTableFile.m_strClassName.c_str() << " &other);\n\n";
// 各成员属性定义
for (size_t i = 0; i < inTableFile.m_arrName.size(); i++)
{
if (inTableFile.m_arrIsList[i]) // 是不定长数组
{
if (FT_int == inTableFile.m_arrFieldType[i]) //数字
{
of << "\tstd::vector<int> " <<
"m_" << inTableFile.m_arrName[i].c_str() << "s;\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_uint == inTableFile.m_arrFieldType[i]) //数字
{
of << "\tstd::vector<unsigned int> " <<
"m_" << inTableFile.m_arrName[i].c_str() << "s;\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_float == inTableFile.m_arrFieldType[i]) // float
{
of << "\tstd::vector<float> " <<
"m_" << inTableFile.m_arrName[i].c_str() << "s;\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_string == inTableFile.m_arrFieldType[i]) // 字符串
{
of << "\tstd::vector<std::string> " <<
"m_" << inTableFile.m_arrName[i].c_str() << "s;\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_struct == inTableFile.m_arrFieldType[i]) // 子结构
{
of << "\tstd::vector<" << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[i].c_str() << "> " <<
"m_" << inTableFile.m_arrName[i].c_str() << "s;\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else //其他类型,忽略,暂不处理
{
}
}
else // 非不定长数组
{
if (FT_int == inTableFile.m_arrFieldType[i]) //数字
{
of << "\tint " <<
"m_" << inTableFile.m_arrName[i].c_str() << ";\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_uint == inTableFile.m_arrFieldType[i]) //数字
{
of << "\tunsigned int " <<
"m_" << inTableFile.m_arrName[i].c_str() << ";\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_float == inTableFile.m_arrFieldType[i]) // float
{
of << "\tfloat " <<
"m_" << inTableFile.m_arrName[i].c_str() << ";\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_string == inTableFile.m_arrFieldType[i]) // 字符串
{
of << "\tstd::string " <<
"m_" << inTableFile.m_arrName[i].c_str() << ";\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else if (FT_struct == inTableFile.m_arrFieldType[i]) // 子结构
{
of << "\t" << inTableFile.m_strClassName.c_str() << inTableFile.m_arrName[i].c_str() <<
" m_" << inTableFile.m_arrName[i].c_str() << ";\t//" << inTableFile.m_arrComments[i].c_str() << "\n";
}
else //其他类型,忽略,暂不处理
{
}
}
}
// 类名:class XXX {...}结束
of << "};\n\n";
// 表格管理器类:class XXXTable {...};
of << "class " << inTableFile.m_strClassName.c_str() << "Table\n";
of << "{\n";
of << "public:\n";
// 构造:XXXTable();
of << "\t" << inTableFile.m_strClassName.c_str() << "Table();\n\n";
// 析构:~XXXTable();
of << "\t~" << inTableFile.m_strClassName.c_str() << "Table();\n\n";
// 获取表格数据
if (3 == inTableFile.m_arrKey.size())
{
//const XXX* Get(k1, k2, k3){...}
//const std::map<k3type>* Get(k1, k2){...}
//const std::map<k2type, std::map<k3type, XXX> >* Get(k1){...}
//const std::map<k1type, std::map<k2type, std::map<k3type, XXX> > >& Get(){...}
of << "\tconst " <<
inTableFile.m_strClassName.c_str() << "* " <<
"Get(" <<
((FT_int == inTableFile.m_arrFieldType[inTableFile.m_arrKey[0]]) ? "int " : "unsigned int ") <<
inTableFile.m_arrName[inTableFile.m_arrKey[0]].c_str() << ", " <<
((FT_int == inTableFile.m_arrFieldType[inTableFile.m_arrKey[1]]) ? "int " : "unsigned int ") <<
inTableFile.m_arrName[inTableFile.m_arrKey[1]].c_str() << ", " <<
((FT_int == inTableFile.m_arrFieldType[inTableFile.m_arrKey[2]]) ? "int " : "unsigned int ") <<
inTableFile.m_arrName[inTabl