用途
对二次开发过程中用到的功能进行梳理过程中,将单个完整的功能单独拿出来整理成一个独立完整的 dll,便于后期整合应用,也方便分享。在这一过程中每完成一个功能就要写一个 dat 文件,比较麻烦。于是就用 C++写了一个可以批量生成 dat 文件的小程序。可以根据当前目录下存在的 dll 来生成对应 dat 文件数据行
代码
递归遍历当前目录下所有文件及其子目录下文件,遇到*.dll 则生成相应的数据行。
void GenDatFile(const wchar_t *path, ofstream *file)
{
//文件句柄
intptr_t hFile = 0;
//文件信息
struct _wfinddata64i32_t fileinfo; //包含中文目录,使用宽字符
wstring p;
if ((hFile = _wfindfirst(p.assign(path).append(L"\\*").c_str(), &fileinfo)) != -1)
{
do
{
if (fileinfo.attrib & _A_SUBDIR)
{
if (wcscmp(fileinfo.name, L".") != 0 && wcscmp(fileinfo.name, L"..") != 0)
{
GenDatFile(p.assign(path).append(L"\\").append(fileinfo.name).c_str(), file);
}
}
else
{
wstring filename(fileinfo.name);
string fname;
gconvert::uni2utf8(filename, fname);
string fpath;
gconvert::uni2utf8(wstring(path), fpath);
if (strcmp(fname.substr(fname.length() - 4).c_str(), ".dll") == 0)
{
*file << "NAME " << fname.substr(0, fname.length() - 4) << "\n"
<< "EXEC_FILE " << fpath << "\\" << fname << "\n"
<< "TEXT_DIR " << fpath << "\\text\n"
<< "STARTUP dll\n"
<< "ALLOW_STOP TRUE\n"
<< "END\n\n";
}
}
} while (_wfindnext(hFile, &fileinfo) == 0);
}
_findclose(hFile);
}
文件编码
如果目录中存在中文,则直接生成的文件编码为 GB2312,Creo 不支持改编码,需要转为 UTF-8 with BOM。
因此,在写入文件的时候要注意将数据编码转为 unicode,并写入 BOM 文件头
char c1 = 0xEF; // 仿utf-8 BOM头 三字节
char c2 = 0xBB;
char c3 = 0xBF;
file << c1 << c2 << c3;
源代码
为保证编译风格统一,该程序也采用 makefile 形式编译,生成的 exe 文件放到上一层目录,该目录下包含各个在开发的功能模块,因此可以一键生成包含所有 dll 的 creotk.dat
0.0Install