查詢檔案,轉移檔案
#include #include “readTxt.h” #include #include // for strcpy(), strcat() #include <io.h> #include <WINDOWS.H> #include
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 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 == "")
exit(0);
//在 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不覆蓋
}
}