当前访客身份:游客 [ 登录 | 加入开源中国 ]

代码分享

当前位置:
代码分享 » C/C++  » 编程基础
mickelfeng

VC2008/2005工程自动生成器

mickelfeng 发布于 2013年07月14日 22时, 2评/704阅
分享到: 
收藏 +0
2
扫描指定目录并递归其子目录的所有文件,生成一个xml的VCMakeTool类型的工程文件.

使用方法:

usage:
buildvcproj dir destfilename
example:
buildvcproj c://abc abc.xml

源代码如下, 非常简单,贴出来,是希望有同样需要的朋友不用重复劳动.

出现:不能将参数1从 "_TCHAR*" 转换为"const char *"
vs2008下设置项目属性
配置属性->常规->字符集,不选择“使用Unicode字符集”就可以了

因为C++支持两种字符串,即常规的ANSI编码(使用""包裹)和Unicode编码(使用L""包裹),这样对应的就有了两套字符串字符串处理函数,比如:strlen和wcslen,分别用于处理两种字符串
微软将这两套字符集及其操作进行了统一,通过条件编译(通过_UNICODE和UNICODE宏)控制实际使用的字符集,这样就有了_T("")这样的字符串,对应的就有了_tcslen这样的函数
为了存储这样的通用字符,就有了TCHAR:
当没有定义_UNICODE宏时,TCHAR = char,_tcslen = strlen
当定义了_UNICODE宏时,TCHAR = wchar_t , _tcslen = wcslen[1]
当我们定义了UNICODE宏,就相当于告诉了编译器:我准备采用UNICODE版本。这个时候,TCHAR就会摇身一变,变成了wchar_t。而未定义UNICODE宏时,TCHAR摇身一变,变成了unsignedchar。这样就可以很好的切换宽窄字符集。
tchar可用于双字节字符串,使程序可以用于中日韩等国 语言文字处理、显示。使编程方法简化。
标签: <无>

代码片段(1) [全屏查看所有代码]

1. [代码][C/C++]代码     跳至 [1] [全屏预览]

#include "stdafx.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(DirSpec,packsDir);
    
    int rlen = strlen(DirSpec);
    if(DirSpec[rlen -1]=='\\')
    {
        DirSpec[rlen-1]='0';
    }
    if(subdir&&subdir[0]!=0)
    {
        strcat(DirSpec,"\\");
        strcat(DirSpec,subdir);
    }
    int saveLen = strlen(DirSpec);
    
    strcat(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<3)
    {
        printf("usage:\nbuildvcproj dir destfilename\n"
            "example:\n"
            "buildvcproj c:\\abc abc.xml\n");
        return 0;
    }
    g_pFile=fopen(argv[2],"w");
    if(g_pFile)
    {
        fprintf(g_pFile,"%s",
                            "<?xml version=\"1.0\" encoding=\"gb2312\"?>"
                            "\n<VisualStudioProject"
                            "\n ProjectType=\"Visual C++\""
                            "\n Version=\"9.00\""
                            "\n Name=\"win2ksrc\""
                            "\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=\"VCNMakeTool\""
                            "\n             BuildCommandLine=\"\""
                            "\n             ReBuildCommandLine=\"\""
                            "\n             CleanCommandLine=\"\""
                            "\n             Output=\"win2ksrc.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=\"VCNMakeTool\""
                            "\n             BuildCommandLine=\"\""
                            "\n             ReBuildCommandLine=\"\""
                            "\n             CleanCommandLine=\"\""
                            "\n             Output=\"win2ksrc.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 "
                            );
        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);
    }
    else
    {
        printf("Can not open file %s\n",argv[2]);
    }
    return 0;
}


开源中国-程序员在线工具:Git代码托管 API文档大全(120+) JS在线编辑演示 二维码 更多»

发表评论 回到顶部 网友评论(2)

  • 1楼:autocoder 发表于 2013-07-15 10:51 回复此评论
    没看懂.作用是什么?
  • 2楼:mickelfeng 发表于 2013-07-15 12:00 回复此评论
    自动创建工程文件
开源从代码分享开始 分享代码
mickelfeng的其它代码 全部(129)...