vs下,GBK格式的.h .cpp 编译时,自动转换为UTF-8

来源:互联网 发布:linux $$ 编辑:程序博客网 时间:2024/06/09 15:15

转自

 http://suool.net/



在VS下面开发Cocos程序的时候,他的默认编码是GBK的,但是在迁移或者是编译调试的时候要求UTF的编码更为方便。因此便有了将C++文件的编码格式转换为UTF-8的需求问题。

这个问题,当然可以在建立文件保存的时候选择高级保存选择,然后选择保存的格式。

 
但是,显然,在项目文件很多的时候,这个不是一个聪明的选择。所以,就要想办法如何批量的转化处理。
在Linux下面有专门的命令可以实现这个功能。
在Windows下面要如何做呢?
当然,借助于我们万能的C++一样可以很方便的解决它,经过一番查找资料,现在共享一下我的解决方法,首先,要在VS里面建立一个VC的控制台程序项目。
然后新建一个convert源文件。代码如下:
⦁    //  定义控制台应用程序的入口点。 
⦁    // 
⦁     
⦁    #include "stdafx.h" 
⦁    #include <afxwin.h> 
⦁    #include <string> 
⦁    #include <iostream> 
⦁     
⦁    #ifdef _DEBUG 
⦁    #define new DEBUG_NEW 
⦁    #endif 
⦁     
⦁    #define _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES 1 
⦁     
⦁    // 唯一的应用程序对象 
⦁     
⦁    CWinApp theApp; 
⦁     
⦁    using namespace std; 
⦁     
⦁    void recursiveFile(CString strFileType); 
⦁    void convertGBToUTF8(CString strWritePath, const char* gb2312); 
⦁     
⦁    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) 
⦁    { 
⦁        int nRetCode = 0; 
⦁     
⦁        // 初始化 MFC 并在失败时显示错误 
⦁        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) 
⦁        { 
⦁            // TODO: 更改错误代码以符合您的需要 
⦁            _tprintf(_T("错误: MFC 初始化失败\n")); 
⦁            nRetCode = 1; 
⦁        } 
⦁        else 
⦁        { 
⦁            /*for(int i = 0; i < argc; i++)
⦁            {
⦁            MessageBox(NULL, argv[i], L"Arglist contents", MB_OK);
⦁            }*/ 
⦁            //声明一个CFileFind类变量,以用来搜索 
⦁     
⦁            //接受一个参数作为源代码文件的根目录 
⦁            TCHAR *lpszDirName = argv[1]; 
⦁            CString strFileType; 
⦁            strFileType.Format(_T("%s\\*.*"), lpszDirName); 
⦁            //递归此目录下的.h文件和.cpp文件,如果发现不是utf8编码则转换为utf8编码 
⦁            recursiveFile(strFileType); 
⦁     
⦁        } 
⦁     
⦁        return nRetCode; 
⦁    } 
⦁     
⦁    void recursiveFile(CString strFileType) 
⦁    { 
⦁        CFileFind finder; 
⦁        BOOL isFinded = finder.FindFile(strFileType);//查找第一个文件 
⦁        while (isFinded) 
⦁        { 
⦁            isFinded = finder.FindNextFile(); //递归搜索其他的文件 
⦁            if (!finder.IsDots()) //如果不是"."目录 
⦁            { 
⦁                CString strFoundFile = finder.GetFilePath(); 
⦁                if (finder.IsDirectory()) //如果是目录,则递归地调用 
⦁                { 
⦁                    CString strNextFileType; 
⦁                    strNextFileType.Format(_T("%s\\*.*"), strFoundFile); 
⦁                    recursiveFile(strNextFileType); 
⦁                } 
⦁                else 
⦁                { 
⦁                    //如果是头文件或cpp文件 
⦁                    if (strFoundFile.Right(4) == _T(".cpp") || strFoundFile.Right(2) == _T(".h")) { 
⦁                        CFile fileReader(strFoundFile, CFile::modeRead); 
⦁                        byte head[3]; 
⦁                        fileReader.Read(head, 3); 
⦁                        //判断是否带有BOM文件头 
⦁                        if (head[0] == 0xef && head[1] == 0xbb && head[2] == 0xbf) 
⦁                        { 
⦁                            fileReader.Close(); 
⦁                            continue; 
⦁                        } 
⦁                        fileReader.SeekToBegin(); 
⦁     
⦁                        int bufLength = 256; 
⦁                        char *buf = new char[bufLength]; 
⦁                        ZeroMemory(buf, bufLength); 
⦁                        int nReadLength; 
⦁                        std::string strContent; 
⦁                        while ((nReadLength = fileReader.Read(buf, bufLength))) 
⦁                        { 
⦁                            strContent.append(buf, nReadLength); 
⦁                            ZeroMemory(buf, nReadLength); 
⦁                        } 
⦁                        delete buf; 
⦁                        fileReader.Close(); 
⦁                        convertGBToUTF8(strFoundFile, strContent.c_str()); 
⦁     
⦁                        TCHAR* fileName = new TCHAR[strFoundFile.GetLength() + 1]; 
⦁                        //wcscpy_s(*fileName, strFoundFile); 
⦁     
⦁                        // 中文路径存在问题,可以将下面的输出屏蔽,程序将静默运行 
⦁                        printf("%S已经转换为UTF-8编码", strFoundFile.GetBuffer(0)); 
⦁                        cout << endl; 
⦁     
⦁                        if (_tcslen(fileName) >0) 
⦁                        { 
⦁                            delete[] fileName; 
⦁                        } 
⦁                    } 
⦁                } 
⦁            } 
⦁        } 
⦁        finder.Close(); 
⦁    } 
⦁     
⦁    void convertGBToUTF8(CString strWritePath, const char* gb2312) 
⦁    { 
⦁        CFile fp; 
⦁        fp.Open(strWritePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, NULL); 
⦁        int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0); 
⦁        wchar_t* wstr = new wchar_t[len + 1]; 
⦁        memset(wstr, 0, len + 1); 
⦁        MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len); 
⦁        len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL); 
⦁        char* str = new char[len + 1]; 
⦁        memset(str, 0, len + 1); 
⦁        len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL); 
⦁        if (wstr) delete[] wstr; 
⦁        str[len] = '\n'; 
⦁        const unsigned char aryBOM[] = { 0xEF, 0xBB, 0xBF }; 
⦁        fp.Write(aryBOM, sizeof(aryBOM)); 
⦁        fp.Write(str, len); 
⦁        delete[] str; 
⦁        fp.Close(); 
⦁    } 


如果编译出现错误请点击,项目--属性--一般------MFC的使用选择共享DLL的方式。
 
然后将编译成功的.exe文件放在项目的目录下,比如:
 
然后选择项目,属性的与生成事件,将要转换的源码目录放在.exe 的后面即可,这样虽然在您编写的时候保存的是GBK,但是在项目进行编译之前会把指定目录下的所有C++源码文件转换为UTF-8的格式,具体格式如下:
 
当然,您也可以直接在cmd中运行该exe文件,在后面加上要转换的目录即可。格式为: convert.exe Dir
这样在编译之前就会先将源码文件格式转换为UTF-8的格式。
0 0
原创粉丝点击