查找文件,转移文件

#include <fstream>
#include "readTxt.h"
#include <iostream>
#include <cstring>        // for strcpy(), strcat()
#include <io.h>
#include <WINDOWS.H>
#include <map>
#include <vector>
string g_CaseFilePath("CaseFilePath:");
int numCaseFilePath = 13;

string path_BugTest("\\GGPTest\\Source\\BugTest");
string path_WhiteboxTest("\\GGPTest\\Source\\WhiteboxTest");
string path_AlgorithmTest("\\UnitTest\\AlgorithmTest");
string testCase("\\GGPTest\\TestCase");

//不考虑这些用例
string CurveSurface_CEllipsurf("CurveSurface_CEllipsurf");


//查找file中某行是否与str相同
bool IsStringInTxt(ifstream & file, pair<string, string> pair_case_file)
{
    string s;
    while(getline(file,s))
    {
        if(s == "" || s.find(g_CaseFilePath) == 0)
            continue;

        //streampos pos = infile.tellg(); //记录当前文件指针位置

        //去除 DISABLED之后再比较
        string re_s = removeDisabled(s);
        string re_fir = removeDisabled(pair_case_file.first);

        //用例和文件名分别对应
        string caseName = getName(re_s);
        string fileName = getName(getTestCaseFile(file));

        //用例全名对应也是对应
        if((re_s == re_fir) || (caseName == getName(re_fir) && fileName == pair_case_file.second)) 
            return true;

        //infile.seekg(pos); //返回原来的文件指针位置
    }
    return false;
}

bool IsStringInTxt(ifstream & file, const string & path_file)
{
    string s, new_trunk;

    //找到testcase的相对路径
    size_t pos = path_file.find(testCase);
    if(pos == string::npos)
        return false;
    new_trunk = path_file.substr(pos, path_file.size() - pos);

    if(new_trunk.empty())
        return false;

    while(getline(file,s))
    {
        pos = s.find(testCase);

        if(pos == string::npos)
            continue;
        string new_prodn = s.substr(pos, s.size() - pos);

        /*if(new_prodn == "\\GGPTest\\TestCase\\BugCase\\BuildGeometry\\BUG_GDQ_10278.txt")
            return true;*/
        if(new_prodn == new_trunk)
            return true;    
    }
    return false;
}

//查看aFile中有,而bFile中没有的行
void AfileNotInBfile(const string & aFile, const string & bFile, const string & cFile, bool flag_cpp)
{
    ifstream AInfile, BInfile;
    AInfile.open(aFile.data());
    assert(AInfile.is_open());

    BInfile.open(bFile.data());
    assert(BInfile.is_open());

    ofstream justPutout(cFile, ios::app);

    string s;
    while(getline(AInfile, s))
    {
        bool isCaseName = (s != "") &&
                          s.find(g_CaseFilePath) != 0 &&
                          s.find(CurveSurface_CEllipsurf) != 0;
        if(isCaseName)
        {
            //streampos pos = AInfile.tellg(); //记录当前文件指针位置
            bool isInBfile = true;

            //返回文件开头
            BInfile.clear();
            BInfile.seekg(0, ios::beg);
            
            string TestCaseFile;
            if(flag_cpp)
            {
                TestCaseFile = getTestCaseFile(AInfile);
                isInBfile = IsStringInTxt(BInfile, make_pair(s, getName(TestCaseFile)));
            }
            else
                isInBfile = IsStringInTxt(BInfile, s);
            //cout<<new_s<<"   "<<isInBfile<<endl;
            if(!isInBfile)
            {                
                justPutout<<s<<endl; 
                if(flag_cpp)
                {
                    justPutout<<TestCaseFile<<endl;
                    justPutout<<getPathInProdN(bFile, TestCaseFile)<<endl<<endl;
                }
                
            }            
        }
    }
    AInfile.close();
    BInfile.close();

    justPutout.close();
}

//找到某个文件夹下所有.cpp 或 .txt文件,写入到file中
void listFiles(const char * dir, const char * file, const char * flag)
{
    ofstream justPutout(file, ios::app);

    char dirNew[200];
    strcpy(dirNew, dir);
    strcat(dirNew, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索

    intptr_t handle;
    _finddata_t findData;

    handle = _findfirst(dirNew, &findData);
    if (handle == -1)        // 检查是否成功
        return;

    do
    {
        if (findData.attrib & _A_SUBDIR)
        {
            if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)
                continue;

            // 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
            strcpy(dirNew, dir);
            strcat(dirNew, "\\");
            strcat(dirNew, findData.name);

            listFiles(dirNew, file, flag);
        }
        else
            //cout <<dir<<"\\"<< findData.name << "\n";
        {
            if(string(findData.name) == "BUG_TJGJ_27688.txt")
                int ok = 5;
            string str(findData.name);
            size_t i = str.find_last_of(string("."));
            if(i != string::npos && str.substr(i, str.size() - i) == flag)
            {
                string path = string(dir) + "\\" + findData.name;
                justPutout<<path<<endl;
            }
        }
    } while (_findnext(handle, &findData) == 0);

    _findclose(handle);    // 关闭搜索句柄
    justPutout.close();
}

//遍历某个文件夹下文档,查找文档中TEST,将测试用例名称输入到文档file中
void FindTestName(const char * dir, const char * temp_file_path, const char * file)
{
    cout<<"开始查找路径下所有的测试用例......"<<endl<<endl;
    emptyFile(file);
    emptyFile(temp_file_path);
    ofstream justPutout(file, ios::app);
    //在以下三个路径中查找
    string dir_BugTest = dir + path_BugTest;
    string dir_WhiteboxTest = dir + path_WhiteboxTest;
    string dir_AlgorithmTest = dir + path_AlgorithmTest;

    //找到路径下的文件,temp_file_path临时保存文件路径名称
    listFiles(dir_BugTest.data(), temp_file_path, ".cpp");
    listFiles(dir_WhiteboxTest.data(), temp_file_path, ".cpp");
    listFiles(dir_AlgorithmTest.data(), temp_file_path, ".cpp");

    //读取每个文件
    ifstream Infile;
    Infile.open(temp_file_path);
    assert(Infile.is_open());

    //读取每个文件中的Test到file中
    string s_file, s_name;
    while(getline(Infile, s_file))
    {
        ifstream perFile;
        perFile.open(s_file.data());
        assert(perFile.is_open());
        while(getline(perFile, s_name))
        {
            size_t pos = s_name.find("TEST");
            
            if(pos == 0)
            {   
                bool flag = true;
                //找到'('的位置
                size_t pos_quot = s_name.find("(");
                if(pos_quot == string::npos)
                    continue;
                if(pos + 4 > pos_quot)
                    continue;

                //TEST_F 的情况应该有吗?
                for(size_t i=0; i < pos; i++)
                    if(s_name[i] != ' ')
                    {
                        flag = false;
                        break;
                    }
                    /*for(size_t i = pos + 4; i < pos_quot; i++)
                    if(s_name[i] != ' ')
                    {
                    flag = false;
                    break;
                    }*/
                if( !flag)
                    continue;

                //将","变为"."
                string s;  //用例名
                size_t pos_dot = s_name.find(',');
                if(pos_dot == string::npos)
                    continue;
                else
                    s_name[pos_dot] = '.';

                //找到用例名
                for(size_t i = pos_quot + 1; i < s_name.size(); i++)
                {
                    if(s_name[i] != ' ')
                    {
                        if(s_name[i] != ')')
                            s += s_name[i];
                        else
                            break;
                    }                    
                }
                justPutout<<s<<endl;
                //输出文件路径名
                justPutout<<string(g_CaseFilePath)<<s_file<<endl<<endl;
            }
        }
        perFile.close();
    }
    Infile.close();
    justPutout.close();
}

//遍历某个文件夹下文档,查找文件中TEST,并将完整的TEST写入到文本file中
string FindTestCaseCode(const string & caseName, const string & temp_file_path)
{
    //emptyFile(temp_file_path);
    最终的测试用例代码
    string caseStr;

    ifstream Infile;
    Infile.open(temp_file_path);
    assert(Infile.is_open());

    //读取每个文件中的Test到file中
    string s_file_or_name, caseCode;
    while(getline(Infile, s_file_or_name))
    {
        //如果不包含caseName就继续
        if(s_file_or_name.find(caseName) == string::npos )
            continue;

        //如果没有准确读取用例所在文件就继续
        if(!getline(Infile, s_file_or_name))
            continue;
        
        //找到用例所在的文件
        s_file_or_name = s_file_or_name.substr(numCaseFilePath, s_file_or_name.size()  - numCaseFilePath );

        string new_file;
        for(size_t i = 0; i <s_file_or_name.size(); i++)
        {
            if(s_file_or_name[i] == '\\')
                new_file += '\\\\';
            else
                new_file += s_file_or_name[i];
        }

        //打开用例所在文件
        ifstream perFile;
        perFile.open(s_file_or_name.data());
        assert(perFile.is_open());

        //读取完整的用例函数体
        while(getline(perFile, caseCode))
        {
            string s_name_new;
            for(size_t i=0; i<caseCode.size(); i++)
            {
                //将,换成.
                if(caseCode[i] == ',')
                    s_name_new += '.';

                //去除空格
                else if(caseCode[i] != ' ')
                    s_name_new += caseCode[i];
                
            }

            //判断是否是完整的测试用例
            size_t casePos1 = s_name_new.find("TEST(" + caseName + ")");
            size_t casePos2 = s_name_new.find("TEST_F(" + caseName + ")");
            if(casePos1 != 0 && casePos2 != 0)
                continue;

            //找到测试用例的函数体
            caseStr += caseCode + "\n";
            while(getline(perFile, caseCode))
            {
                if(caseCode.find('}') != 0) //顶头的"}"代表结束函数体
                    caseStr += caseCode + "\n";
                else
                    break;
            }

            //成功获得函数体
            if(!caseStr.empty())
                break;
        }
        if(!caseStr.empty())
        {
            caseStr += caseCode + "\n\n";
            return caseStr;
        }
        perFile.close();
    }

    Infile.close();
    return caseStr;
}

//查看aFile中有,而bFile中没有的用例名称写入cFile中,代码写入dFile文件中
void CaseAfileNotInBfile(const string & dir, const string & aFile, const string & bFile, const string & cFile, const string & dFile)
{
    cout<<"开始查找两个文件不同的用例名......"<<endl<<endl;
    emptyFile(cFile);
    emptyFile(dFile);

    AfileNotInBfile(aFile, bFile, cFile, true);

    ifstream File;
    File.open(cFile);
    assert(File.is_open());
    cout<<"开始查找两个文件不同的用例的函数体......"<<endl<<endl;
    string s;
    while(getline(File, s))
    {
        /*size_t pos = s.find('.');
        if(pos != string::npos) 
            s[pos] = ',';*/
        //如果不是用例名称,则跳过
        if(s.find(g_CaseFilePath) == 0)
            continue;
        
        if(s == "")
            continue;

        //找到 prodn 中对应的文件路径
        string file_prodn;
        getline(File, file_prodn);
        getline(File, file_prodn);

        //找到的用例输入到dFile中
        string code_case = FindTestCaseCode(s, aFile);
        ofstream justPutout(dFile, ios::app);
        justPutout<<code_case;
        justPutout.close();

        //将找到的测试用例写入 prodn 中的文件里
        /*if(file_prodn == "" && file_prodn.find(g_CaseFilePath) != 0)
            continue;

        file_prodn = file_prodn.substr(g_CaseFilePath.size(), file_prodn.size() - g_CaseFilePath.size());
        
        ofstream prodFile(file_prodn, ios::app);
        prodFile<<endl;

        prodFile<<code_case;
        prodFile.close();
        cout<<s<<"  正在写入prodn"<<endl;*/

        
    }
}

//清空某个文档
void emptyFile(const string & file)
{
    ofstream justPutout(file);
    justPutout.close();
}

//找到测试用例所在的文件的路径名称
string getTestCaseFile(ifstream & Infile)
{
    string fileName;
    while(getline(Infile, fileName))
    {
        if(fileName.find("CaseFilePath:") == 0)
            return fileName;
    }
    return fileName;
}

//找到测试用例的名称或文件的名称
string getName(const string & str)
{
    string name;
    size_t beginPos = 0;

    //如果是文件的路径名
    string flag = "\\";
    if(str.find(g_CaseFilePath) == 0)
    {
        beginPos = str.find_last_of(flag);
    }        

    else   //如果是测试用例的名称
        beginPos = str.find_last_of('.');
    if(beginPos != string::npos && beginPos < str.size())
        return str.substr(beginPos + 1, str.size() - beginPos - 1);

    return name;
}

string removeDisabled(const string & str)
{
    string name, str_1, str_2;
    vector<string> vec;
    vec.push_back("/*DISABLED_*/");
    vec.push_back("DISABLED_");
    for(size_t i = 0; i < vec.size(); i++)
    {
        if(getName(str).find(vec[i]) == 0)
        {
            size_t pos = str.find('.');
            str_1 = str.substr(0, pos + 1);
            str_2 = str.substr(pos + 1 + vec[i].size(), str.size() - pos - 1 - vec[i].size());
            name += str_1 + str_2;
            return name;
        }
    }
    return str;
}

//从trunk的路径获得prodn的路径 cpp文件
string getPathInProdN(const string & ProdPathNFile, const string & trunkFile)
{
    string re_trunk_file;  //trunk的相对路径
    size_t pos = 0;
    string str_path;  //prodn中的路径

    vector<string> vec;
    vec.push_back(path_BugTest);
    vec.push_back(path_WhiteboxTest);
    vec.push_back(path_AlgorithmTest);

    for(size_t i = 0; i < vec.size(); i ++)
    {
        pos = trunkFile.find(vec[i]);
        if(pos != string::npos)
        {
            re_trunk_file = trunkFile.substr(pos, trunkFile.size() - pos);
            break;
        }
    }

    if(pos == 0 || pos == string::npos)
        return str_path;

    //查找 ProdPathNFile 获得prodn中的文件路径
    ifstream File;
    File.open(ProdPathNFile);
    while(getline(File, str_path))
    {
        size_t pos_prodn = str_path.find(re_trunk_file);
        if(pos_prodn != string::npos && str_path.size() - pos_prodn == re_trunk_file.size())
            break;
    }
    return str_path;    
}

//判断某一路径下某文件是否存在
bool IsFileExit(const string & dir, const string &fileName)
{
    string new_dir = dir + "\\" + fileName;
    ifstream file;
    file.open(new_dir);
    if(file)
    {
        file.close();
        return true;
    }
    else
        return false;
}

//从trunk的路径获得prodn的路径 txt文件
string getPathInProdNTxt(const string & dir_prodn, const string & trunkFile)
{
    string re_trunk_file;  //trunk的相对路径
    size_t pos = 0;
    string str_path;  //prodn中的路径
    
    pos = trunkFile.find(testCase);
    if(pos != string::npos)
        re_trunk_file = trunkFile.substr(pos + testCase.size(), trunkFile.size() - pos - testCase.size());

    if(pos == 0 || pos == string::npos)
        return str_path;

    //re_trunk_file = re_trunk_file.substr(0, re_trunk_file.find_last_of("\\"));

    return dir_prodn + re_trunk_file;  
}

//将测试用例 .txt 文件从 trunk 转移到 prodn
void transTxtFromTrunkToProdn(const string & dir_trunk, const string & dir_prodn, const string & testcase_trunk_file, const string & testcase_prodn_file, const string & diff_file)
{
    emptyFile(testcase_trunk_file);
    emptyFile(testcase_prodn_file);
    emptyFile(diff_file);
    //找到 trunk 和 prodn 中的测试用例
    listFiles(dir_trunk.data(), testcase_trunk_file.data(), ".txt");
    listFiles(dir_prodn.data(), testcase_prodn_file.data(), ".txt");

    //找到 trunk 和 prodn 中不同的测试用例
    AfileNotInBfile(testcase_trunk_file, testcase_prodn_file, diff_file, false);

    //查找 trunk 中多的测试用例
    ifstream file;
    file.open(diff_file);

    string test_case;
    while(getline(file, test_case))
    {
        if(test_case == "")
            continue;

        //在 prodn 下的路径
        string path_prodn = getPathInProdNTxt(dir_prodn, test_case);

        //查找 prodn 下该文件是否存在
        bool IsFileInProdn= IsFileExit(path_prodn, test_case);

        //如果不存在则拷贝过去到 prodn
        //if(!IsFileInProdn)
        //    cout<<"是否拷贝了:"<<CopyFile(test_case.data(), path_prodn.data(), false)<<endl;//false代表覆盖,true不覆盖
    }
    file.close();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值