linux format 进度条

来源:互联网 发布:淘宝api查询商品 编辑:程序博客网 时间:2024/05/23 18:16
#include<vector>#include <fstream>#include <iostream>#include <sstream>#include <string>#include <cstdlib>#include <assert.h>using namespace std;typedef void * (*MYTHREADFN)(void*);std::string& trim(std::string &s){    if (s.empty())    {        return s;    }    s.erase(0,s.find_first_not_of(" "));    s.erase(s.find_last_not_of(" ") + 1);    return s;}template <typename T>void SplitString(std::vector<std::string> & subStrList, std::string const &srcStr, T delimiter){    std::string::size_type begIdx = srcStr.find_first_not_of(delimiter);    while (std::string::npos!=begIdx)    {        std::string::size_type endIdx = srcStr.find_first_of(delimiter, begIdx);        if (std::string::npos==endIdx)            endIdx = srcStr.length();        subStrList.push_back(std::string(srcStr, begIdx, endIdx-begIdx));        begIdx = srcStr.find_first_not_of(delimiter, endIdx);    }}void Progress(void* data){   string file = "/var/log/trace/format_disk.log";   double current, total;while (percent <=98){    sleep (10);
    //c++ ifstream 这样一次性读取整个文件.if it is c, it need fopen file and read one by one line;    std::ifstream ifs(file.c_str(), std::fstream::in);    string s1;    string demi1;    vector <string> va1;    if(!ifs)  //c++ don;t simple print err log; it become throw exception. C++ is becoming more and more like Java         throw std::runtime_error("Open file error: " + filename);    //obtain size of file in Byte    ifs.seekg(0, ifs.end);    int length = ifs.tellg();      std::string line;    //check '\n' from second character because the last character is '\n'    int index = -2;    while(length)    {        char c;        ifs.seekg (index, ifs.end);        ifs.get(c);        //check '\n' from end to begin        if(c == '\n')        {            //get the the last line when finding its corresponding beginning            std::getline(ifs, line);            //convet characters to int through istringstream class provided in c++            std::istringstream iss(line);             s1 = trim(line);             string a("/");            SplitString(va1,line,a);            current=atof(va1[0].c_str());            total=atof(va1[1].c_str());            percent = (current*100)/total;            std::cout << "percent=" << percent << std::endl;            break;        }        length--;        index--;    }      for (int i=0;i<percent*100;i++)           cout <<"=";      cout <<percent<<endl;}}bool PipeCommandEx(const string &sCmd, string & output,int & ret){        FILE * fp = NULL;        char line[1024] = {0};        char *line_ptr = line;      //popen execute shell command        if (NULL == (fp = popen(sCmd.c_str(), "r")))        {                cout <<"popen fail"<<endl;                return false;        }        while (fgets(line, sizeof(line), fp))        {                output += line_ptr;                output += "\n";        }        ret = pclose(fp);        if (ret == 0)        {                return true;        }        else        {                return false;        }}typedef void * (*MYTHREADFN)(void*);pthread_t CreateThread(MYTHREADFN threadfn, void * arg){    pthread_attr_t attr;    pthread_attr_init(&attr);    pthread_t tid;    int const rv = pthread_create(&tid, &attr, threadfn, arg);    assert(0==rv);    pthread_attr_destroy(&attr);    return tid;}main(){string cmd;string output;int ret;pthread_t tid_;tid_= CreateThread((MYTHREADFN)Progress, NULL);
//tr is repace, [;cntrl:] 所有控制字符; ecs,ack ,bel etccmd = "yes y|mkfs.ext4 -m 0 -O none -b 4096 /dev/sdb | tr '[:cntrl:]' '\n' | tr -s '\n' >/var/log/trace/format_disk.log";    if(PipeCommandEx(cmd, output, ret) || (ret != 0))    {        cout <<"pipe shell fail"<<endl;    }cout <<"format finish"<<endl;pthread_join(tid_, 0);}