首页 > 代码库 > 采集元数据的C++实现

采集元数据的C++实现

我要做的是提取Test_case的名字,以及Test_case的持续时间(单位秒):下面是一段示例元数据

Start| 20140618 root (6033) | tpi 1.4 | 14:53:52 10050943579848 0 |
Start| 20814 SunOS 5.11 11.2 i86pc tcx4440-01 |
STF_ENV| STC_NAME = os-zones |
STF_ENV| STC_VERSION = 2.10.0 |
STF_ENV| STC_OS_VERSION = 5.11 |
STF_ENV| STF_EXECUTE_MODE = ipkg:amd64 |
STF_ENV| cwd = /opt/stc/suites/os/zones/tests/os/functional |
Test_Case_Start| 20858 tests/os/functional/setup | 14:53:52 10051107110387 0 |
stdout| 
stdout| 14:53:53 SUCCESS: zone_is_running zone2
stdout| zone 'zone2' is running
stdout| 
stdout| 14:53:53 SUCCESS: zone_is_running zone1
stdout| zone 'zone1' is running
stdout| 14:53:53 zones 'zone2 zone1 ' are running
Test_Case_End| 20858 tests/os/functional/setup | PASS | 14:53:53 10052165298138 0 |

我想要得到的结果是:

tests/os/functional/setup    1

由于我的元数据比较小(不会超过20MB,所以我选择用vector),源代码如下:


#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

/*
*  There are not split function, you should add one by yourself 
*/
vector<string> split(const string &str, string pattern){
    string :: size_type pos;
    vector <string> result;
    int size = str.size();
    
    int i = 0;
    for(i = 0; i < size; i++){
        pos = str.find(pattern, i);
        if(pos < size){
                string subStr = str.substr(i, pos - i);//substr(beginPosition, length) 
                
                //cout<<"i = "<<i<<" ,pos - 1 = "<<pos - 1<<endl;
                //cout<<str.substr(i, pos - i)<<endl;
                
                result.push_back(subStr);
                i = pos + pattern.size() - 1;
               //cout<<subStr<<endl;
               //cout<<"i="<<i<<endl;
        }
        
    }
    
    return result;

}
/*
*	Duration Time  = endTime - startTime
*	eg: Duration Time = 1:10:08 - 23:08:08 = (25 * 3600 + 10 * 60 + 8) - (23 * 3600 + 8 * 60 + 7)
*/
int getDurationTime(string startTime, string endTime){
	
	vector<string> startTimeVector;
	vector<string> endTimeVector;
	int startTimeSeconds = 0;
	int endTimeSeconds = 0;
	startTime = startTime+":";//1:10:08 , if we do not add ":", we can not get 08 
	endTime = endTime + ":";
	endTimeVector = split(endTime, ":");
	startTimeVector = split(startTime, ":");
	startTimeSeconds = atoi(startTimeVector[0].c_str()) * 3600 + atoi(startTimeVector[1].c_str()) * 60 + atoi(startTimeVector[2].c_str());
	endTimeSeconds = atoi(endTimeVector[0].c_str()) * 3600 + atoi(endTimeVector[1].c_str()) * 60 + atoi(endTimeVector[2].c_str());
	if(endTimeSeconds < startTimeSeconds) {
		endTimeSeconds = endTimeSeconds + 24 * 3600;
	}
//	cout<<"endTimeSeconds:"<<endTimeSeconds << "	startTimeSeconds:"<<startTimeSeconds<<endl;
//	cout<<endTimeSeconds - startTimeSeconds<<endl;
	return endTimeSeconds - startTimeSeconds;
 	
}


int main()
{
    vector< pair<string, int> > caseDurationVec;
     
    string journalInput = "journal.base";
    string journalOutput = "journal.base.out";
    
    ifstream infile(journalInput.c_str());
    ofstream outfile(journalOutput.c_str());
    
    string buffer;
    const string Case_Start = "Test_Case_Start";
    const string Case_End = "Test_Case_End";
    
    int i = 0;
    vector<string> resultVec;
    string caseStartTime = "";
    string caseEndTime = "";
    
    while(getline(infile, buffer)){
        
        if(buffer.find(Case_Start) != buffer.npos){
                resultVec =  split(buffer," ");              
                caseStartTime = resultVec[4];
               
        }
        if(buffer.find(Case_End) != buffer.npos){
                resultVec =  split(buffer," ");
                caseEndTime = resultVec[6];
                caseDurationVec.push_back(make_pair<string, int>(resultVec[2], getDurationTime(caseStartTime, caseEndTime)));
                cout<<"There are "<<i++<<"cases"<<endl; 
        }
		       
    }
    
   
    vector< pair<string, int> > :: iterator iter;
    
    for(iter = caseDurationVec.begin(); iter != caseDurationVec.end(); iter++)
    {
        outfile<<iter->first<<"    "<<iter->second<<endl;
    }
    
    infile.close();
    outfile.close();
    //system("pause");
     
    return 1;
}




采集元数据的C++实现