扫描目录自动生成VS2005/2008工程文件

新浪微博 QQ空间 腾讯微博

对于喜欢使用Visual Studio看代码和写代码的同学而言,没有VS工程,手动建工程通常是一件非常痛苦的事情。之前一直使用一个叫做MPC的工具生成各个版本的VS工程文件,该工具依赖perl语言,而且只能遍历一级目录,不能递归到所有的子目录(也可能工具提供,但是是我没有深入研究工具的配置),而且工具集成到右键菜单,不是经常使用的功能集成到右键菜单不是很好。重新安装了操作系统后,再也不想安装复杂的MPC了,在网上找新的替代工具,找到了如下网址提供的DIY式的工具:链接

感觉工具中写死工程名字不是很好,稍作了一点修改。使用目录名作为工程名,同时省去了第三个参数。这样只需要将目录拖拽到工具上面就可以在目录内部生成一个与目录名同名的工程文件。该工程文件为VS2005和VS2008兼容的vcxproj格式的文件。对于高版本打开该工具生成的工程时,打开时,会提示升级。以下代码在VS2013编译通过。

vcmaketool

根据下面的代码生成一个vcmaketool.exe的工具,然后将需要生成VS工程的目录拖拽到其上即可。

VS2013工程下载:链接

#include "stdafx.h"
#include <string>
#include <io.h>
#include <windows.h>

FILE *g_pFile = NULL;
const char *g_packsDir = NULL;
int g_packsDirLen = 0;

void Build(const char *packsDir, const char *subdir)
{
WIN32_FIND_DATAA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char DirSpec[MAX_PATH]; // directory specification
DWORD dwError;
strcpy_s(DirSpec, packsDir);

int rlen = strlen(DirSpec);
if (DirSpec[rlen - 1] == '\\')
{
DirSpec[rlen - 1] = '
0';
}
if (subdir&&subdir[0] != 0)
{
strcat_s(DirSpec, "\\");
strcat_s(DirSpec, subdir);
}
int saveLen = strlen(DirSpec);

strcat_s(DirSpec, "\\*");

hFind = FindFirstFileA(DirSpec, &FindFileData);
DirSpec[saveLen] = '
\0';
if (hFind != INVALID_HANDLE_VALUE)
{
do
{
if (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
if (FindFileData.cFileName[0] != '
.'
&& strstr(FindFileData.cFileName, "objchk") == NULL
&& strstr(FindFileData.cFileName, "objfre") == NULL
)
{
fprintf(g_pFile, " <Filter "
"\nName=\"%s\""
"\nFilter=\"cpp;c;h;cc;cxx;def;odl;idl;hpj;bat;asm;asmx\""
"\n>\n", FindFileData.cFileName
);
Build(DirSpec, FindFileData.cFileName);
fprintf(g_pFile, "%s\n", "\n </Filter>\n");
}
}
else
{
int len = strlen(FindFileData.cFileName);
if (len < 4 || _strcmpi(FindFileData.cFileName + len - 4, ".obj") != 0)
{
fprintf(g_pFile,
"\n<File"
"\nRelativePath=\".%s\\%s\""
"\n></File>\n",
DirSpec + g_packsDirLen, FindFileData.cFileName);
}

}

} while (FindNextFileA(hFind, &FindFileData) != 0);
dwError = GetLastError();
FindClose(hFind);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
if (argc != 2)
{
printf("usage:\nvcmaketool dir"
"\n\nexample:"
"\nvcmaketool c:\\abc\n");
getchar();
return 0;
}

std::string path = argv[1];

int pos = 0;
while (((pos = path.rfind("\\")) == (path.size() - 1)) && !path.empty())
{
path.erase(pos);
pos = path.rfind("
\\");
}

if (pos == 0 || path.empty())
{
printf("
input path is invalid!\n");
return -1;
}

std::string projname = path.substr(pos + 1, path.size());
std::string projfilename = path + "
\\" + projname + ".vcproj";

if (_access(projfilename.c_str(), 0) == 0)
{
printf("
the project file(%s) is already exist.\n", projfilename.c_str());
getchar();
return -1;
}

int ret = fopen_s(&g_pFile, projfilename.c_str(), "
w");
if (0 != ret)
{
printf("
create the project file failed!\n");
getchar();
return -1;
}

const char* proj = projname.c_str();

#define MAX_OUT_LEN (1024 * 1024)

char* szOut = new char[MAX_OUT_LEN];

sprintf_s(szOut, MAX_OUT_LEN - 1,
"
<?xml version=\"1.0\" encoding=\"gb2312\"?>"
"\n<VisualStudioProject"
"\n ProjectType=\"Visual C++\""
"\n Version=\"9.00\""
"\n Name=\"%s\""
"\n ProjectGUID=\"{8B8C6959-68F6-4182-8EA9-87C1E30EBE9E}\""
"\n Keyword=\"MakeFileProj\""
"\n TargetFrameworkVersion=\"196613\""
"\n >"
"\n <Platforms>"
"\n <Platform"
"\n Name=\"Win32\""
"\n />"
"\n </Platforms>"
"\n <ToolFiles>"
"\n </ToolFiles>"
"\n <Configurations>"
"\n <Configuration"
"\n Name=\"Debug|Win32\""
"\n OutputDirectory=\"$(ConfigurationName)\""
"\n IntermediateDirectory=\"$(ConfigurationName)\""
"\n ConfigurationType=\"0\""
"\n >"
"\n <Tool"
"\n Name=\"%s\""
"\n BuildCommandLine=\"\""
"\n ReBuildCommandLine=\"\""
"\n CleanCommandLine=\"\""
"\n Output=\"%sd.exe\""
"\n PreprocessorDefinitions=\"WIN32;_DEBUG\""
"\n IncludeSearchPath=\"\""
"\n ForcedIncludes=\"\""
"\n AssemblySearchPath=\"\""
"\n ForcedUsingAssemblies=\"\""
"\n CompileAsManaged=\"\""
"\n />"
"\n </Configuration>"
"\n <Configuration"
"\n Name=\"Release|Win32\""
"\n OutputDirectory=\"$(ConfigurationName)\""
"\n IntermediateDirectory=\"$(ConfigurationName)\""
"\n ConfigurationType=\"0\""
"\n >"
"\n <Tool"
"\n Name=\"%s\""
"\n BuildCommandLine=\"\""
"\n ReBuildCommandLine=\"\""
"\n CleanCommandLine=\"\""
"\n Output=\"%s.exe\""
"\n PreprocessorDefinitions=\"WIN32;NDEBUG\""
"\n IncludeSearchPath=\"\""
"\n ForcedIncludes=\"\""
"\n AssemblySearchPath=\"\""
"\n ForcedUsingAssemblies=\"\""
"\n CompileAsManaged=\"\""
"\n />"
"\n </Configuration>"
"\n </Configurations>"
"\n <References>"
"\n </References>"
"\n <Files>"
"\n ", proj, proj, proj, proj, proj);
fprintf(g_pFile, szOut);
delete [] szOut;

g_packsDir = argv[1];
g_packsDirLen = strlen(g_packsDir);
Build(g_packsDir, "");
fprintf(g_pFile, "%s",
"\n </Files>"
"\n <Globals>"
"\n </Globals>"
"\n</VisualStudioProject>"
);
fclose(g_pFile);
printf("created VS Project successfully!\n");
getchar();

return 0;
}

新浪微博 QQ空间 腾讯微博

| 1 分2 分3 分4 分5 分 (5.00- 9票)
Loading ... Loading ... | 这篇文章归档在:C/C++, 软件技术
| 标签: , , . | 永久链接:链接 | 评论(1) |

1条评论

  1. 匿名
    评论于 十月 24, 2014 at 17:05:29 CST | 评论链接

    在选取文件类型时,仅仅只是排除了obj类型的文件。感觉最好是限定哪些文件加入到工程,而不是排除obj文件。
    if (len < 4 || _strcmpi(FindFileData.cFileName + len – 4, ".obj") != 0)

评论

邮箱地址不会被泄露, 标记为 * 的项目必填。

8 - 2 = *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <img alt="" src="" class=""> <pre class=""> <q cite=""> <s> <strike> <strong>

返回顶部