zoukankan      html  css  js  c++  java
  • 查找文件中的中文并替换,以支持程序中英文(国际化)

              手头的项目需要国际化,一种常用的实现方式是搞两个语言文件,KEY-VALUE形式。Ini文件是常用的key-value实现。

              比如 chinese_file.ini           ID_LOGIN_SUCCESS = 成功

                      english_file.ini            ID_LOGIN_SUCCESS= success

             程序启动时,使用其中的某种文件,并加载到hashmap中。程序使用到多语言的地方使用GetTrueText(KEY) 。

             这样无论哪国的语言,程序里面只有看到的key。

             目前,程序里面都是中文写死的,第一步就是找到中文写死的地方,这个人工去找的话,肯定是崩溃了。因此写了个小程序。

             

    #include <fstream>
    #include <string>
    #include <iostream>
    #include <assert.h>
    #include <windows.h>
    using namespace std;
    
    /*
    */
    std::string outpath;
    const std::string note_one = "//";
    const std::string note_two_begin = "/*";
    const std::string note_two_end = "*/";
    const std::string sss = "应该有我啊";
    bool HasChineseInFile(const std::string& filepath)
    {
        int dotpos = filepath.rfind('.');
        if (dotpos == string::npos)
        {
            return false;
        }
        string file_ext = filepath.substr(dotpos+1, -1);
        //主要读取.h .cpp .xml,别的暂时不读
        if (file_ext != "h" &&file_ext != "cpp" && file_ext != "xml")
        {
            return false;
        }
        //不能读自己,否则有死循环    成果物一般不会命名h cpp xml,暂时注释
    //     if (filepath.find(outpath)!=string::npos)
    //     {
    //         return true;
    //     }
        bool bRet = false;
        ifstream in(filepath.c_str(), ios::in);
        ofstream out(outpath.c_str(), ios::out | ios::app);
        assert(out);
        std::string buff;
        if (in && in.is_open())
        {
            int line = 1;
            bool bfind_note_two = false;
            int pos = -1;
            int start = 0;
            while (getline(in, buff))
            {
                //去掉注释的内容
                size_t till = 0;
                if (bfind_note_two)
                {
                    // xxxxxxxxxx*/xxxxxxx
                    pos = buff.find(note_two_end);
                    if (pos!=string::npos)
                    {
                        start = pos;
                        pos = buff.length();
                        bfind_note_two = false;
                    }
                    else
                    {
                        ++line;
                        continue;
                    }
                }
                else
                {
                    start = 0;
                    //尝试找note_one
                    pos = buff.find(note_one);
                    if (pos == string::npos)
                    {
                        //尝试找note_tow
                        pos = buff.find(note_two_begin);
                        if (pos != string::npos)
                        {
                            bfind_note_two = true;
                            //这里还要考虑 “xxf/*xxxxxxxxxxx*/xxxx”这样的模式
                            int findend = buff.find(note_two_end,pos);
                            if (findend!=string::npos)
                            {
                                //这种情况简单整行扫描算了
                                pos = buff.length();
                                bfind_note_two = false;
                            }
                        }
                    }
                    if (pos == string::npos)
                    {
                        //没有找到,可以查找整行
                        pos = buff.length();
                    }
                }
                if (pos == 0)
                {
                    ++line;
                    continue;
                }
                for (int u = start; u < pos; u++)
                {
                    if (buff[u] & 0x80)
                    {
                        out << filepath << ":" << line << endl;
                        bRet = true;
                        break;
                    }
                }
                ++line;
            }
        }
        in.close();
        out.close();
        return bRet;
    }
    
    bool IsDirectory(const std::string& pstrPath)
    {
        DWORD dw = GetFileAttributesA(pstrPath.c_str());
        if (dw == INVALID_FILE_ATTRIBUTES)
        {
            return false;
        }
        return (dw & FILE_ATTRIBUTE_DIRECTORY) != 0;
    }
    
    
    bool RecursiveFind(const std::string& pstrFolder)
    {
        /*检查输入目录是否是合法目录*/
        if (!IsDirectory(pstrFolder))
        {
            return false;
        }
        std::string strFind = pstrFolder;
        if (*strFind.rbegin() != '\' &&
            *strFind.rbegin() != '/')
        {
            strFind.append("\");
        }
        strFind.append("*.*");
        /*打开文件查找,查看源目录中是否存在匹配的文件*/
        /*调用FindFile后,必须调用FindNextFile才能获得查找文件的信息*/
        WIN32_FIND_DATAA wfd;
        HANDLE hFind = FindFirstFileA(strFind.c_str(), &wfd);
        if (hFind == INVALID_HANDLE_VALUE)
        {
            return false;
        }
        do
        {
            std::string strSubFolder;
            if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                if (wfd.cFileName[0] == L'.')
                {
                    continue;
                }
                else
                {
                    strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
                    RecursiveFind(strSubFolder);
                }
            }
            else
            {
                strSubFolder = strFind.substr(0, strFind.length() - 3) + wfd.cFileName;
                HasChineseInFile(strSubFolder.c_str());
            }
        } while (FindNextFileA(hFind, &wfd));
    
        FindClose(hFind);
        return true;
    }
    
    //支持命令行加入参数,1、输入路径、2、输出路径
    int _tmain(int argc, char *argv[])
    {
        std::string inpath;
    
        if (argc>1)
        {
            inpath = argv[1];
        }
        if (argc>2)
        {
            outpath = argv[2];
        }
        while (inpath.empty())
        {
            cout << "请输入查找的目录或者文件" << endl;
            cin >> inpath;
            if (GetFileAttributesA(inpath.c_str()) == INVALID_FILE_ATTRIBUTES)
            {
                cout << "文件的路径有误" << endl;
                inpath.clear();
            }
        }
        while (outpath.empty())
        {
            cout << "请输入输出结果的文件" << endl;
            cin >> outpath;
            ofstream out(outpath);
            if (!out)
            {
                cout << "文件的路径有误" << endl;
                outpath.clear();
            }
            out.close();
        }
        //这里有遍历目录的概念
        if (IsDirectory(inpath))
        {
            RecursiveFind(inpath);
        }
        else
        {
            HasChineseInFile(inpath);
        }
    
        return 0;
    }

              

  • 相关阅读:
    java并发计算的几种基本使用示例
    axios、ajax和xhr前端发送测试
    Spring注解
    Android菜鸟教程笔记
    普通二叉树操作
    MyBatis
    mysql的select语句总结与索引使用
    sys.argv的意义[转]
    硬件小白学习之路(1)稳压芯片LM431
    FPGA小白学习之路(6)串口波特率问题的处理
  • 原文地址:https://www.cnblogs.com/xuhuajie/p/11990792.html
Copyright © 2011-2022 走看看