111

来源:互联网 发布:wings战队夺冠 知乎 编辑:程序博客网 时间:2024/06/10 21:22
#include <iostream>#include <stdio.h>#include <pthread.h>#include <unistd.h>#include "Analys.h"#include <dirent.h>#include <sys/types.h>using namespace std;pthread_mutex_t g_mutex_Msg;//pthread_mutex_lock(&g_mutex_Msg);//pthread_mutex_unlock(&g_mutex_Msg);Analys::Analys(){        m_Fp = NULL;        m_LineBuf = new char[FGETSMAX];          m_Obj = new Object*[128];        for(int i = 0; i < 128; i++) {                m_Obj[i] = new Object[128];          }        m_Dest  = new char*[128];          for(int i = 0; i < 128; i++) {                m_Dest[i] = new char[128];          }        m_Arg = new ArgParam;        //pthread_t thread[MAXNUMTHREAD];        m_ThreadId = new pthread_t[MAXNUMTHREAD];                //pthread_mutex_init(&g_mutex_Msg,NULL);}Analys::~Analys(){        for(int i = 0; i<128; i++) {                delete []m_Dest[i];                delete []m_Obj[i];          }        delete[]m_Dest;         delete[]m_Obj;         delete m_LineBuf;        delete m_ThreadId;        delete m_Arg;        //pthread_mutex_destory(&g_mutex_Msg);}/** * note :切分 字符串 * * @param str: 待分割的字符串 * @param delim: 分隔符字符串  * @param dest: 保存分割后的每个字符串 * @param pCount: 记录一行中分割所得的字符串个数、也就是所谓的列数 * * @return : 正常返回0,字符串为空或者长度为0,则返回-1 */int Analys::Segment(char* str, char* delim, char** &dest, int* pCount, Object** ob){        char* tmp;        *pCount = 0;        if(NULL == str || 0 == strlen(str)) {                return -1;        }        if(NULL == delim || 0 == strlen(delim)) {                return -1;        }        tmp = strtok(str, delim);        int n = 0;        while(tmp != NULL) {               int tmpLength = strlen(tmp);               //if(tmp[tmpLength] == '\n' || tmp[tmpLength] == '\r') {  //判断行尾是回车还是换行,若是,换成'\0'               //        cout<<"出现换行或者回车"<<endl;               //        tmp[tmpLength] = '\0';               //}               (*ob)->Length = tmpLength;               (*ob)->Value = tmp;               memcpy(dest[n++], tmp, tmpLength);               (*dest)[tmpLength] = '\0';               ob++;               (*pCount)++;               tmp = strtok(NULL, delim);        }        return 0;}/** * node:将读取到的一行数据写到文件中 * * @param obj: * @param dataCnt:列数(读取一行的文件) * @param path:文件的路径 * * @return  */int Analys::LoadData(Object** obj, int dataCnt, FILE* fp, char *path){        //FILE *fp;        int i;        //fp = fopen(path, "a");        if(fp == NULL) {                cout<<"file error"<<endl;        }        for(i=0; i<dataCnt-1; i++) {                fprintf(fp,"%s\t", obj[i]->Value);        }        fprintf(fp, "%s\n", obj[i]->Value);        //fclose(fp);        return 0;}/** * note 将ReadPath ;路径的文件,读取并解析,然后写到WritePath路径下。 * * @param ReadPath :读取文件的路径 * @param delim :解析文件的分隔符 * @param WritePath :读完要写入的文件路径  * * @return  */int Analys::RWData(char* ReadPath, char* delim, char* WritePath){        int count = 0;        FILE* Rfp = NULL;        FILE* Wfp = NULL;        Rfp = fopen(ReadPath, "r");        if(Rfp == NULL) {                cout<<"open Readpath fail !"<<endl;                return -1;        }        Wfp = fopen(WritePath, "wa");        if(Wfp == NULL) {                cout<<"open WritePath fail !"<<endl;                return -1;        }        while(fgets(m_LineBuf, FGETSMAX, Rfp) != NULL) {                //cout<<"读取的一行:"<<m_LineBuf<<endl;                 int lineBufLen = strlen(m_LineBuf);                                //判断行尾是回车还是换行,若是,换成'\0'                if(m_LineBuf[lineBufLen-1] == '\n' /*|| m_LineBuf[m_LineBufLen-1] == '\r'*/) {                          //cout<<"出现换行或者回车"<<endl;                        m_LineBuf[lineBufLen-1] = '\0';                }                Segment(m_LineBuf, delim, m_Dest, &count, m_Obj);                  LoadData(m_Obj, count, Wfp, WritePath);        }        fclose(Rfp);        fclose(Wfp);        return 0;}/** *   * * @param arg * * @return  */void* AdjustFun(void *argg){        Analys pAnl;        ArgParam* arg = (ArgParam*)argg;        int ret = pAnl.RWData(arg->ReadPath, arg->delim, arg->WritePath);        if(ret != 0) {                cout<<"RWData error."<<" ---arg->WritePath is "<<arg->WritePath<<"---readpath:"<<arg->ReadPath<<endl;        }        return NULL;}int Analys::MultThreadDispose(int numThread, char* dirPath, char* destPath){        //判断线程数是否合理        if(numThread <= 0) {                cout<<"thread number error."<<endl;                return -1;        }        //判断路径是否为空        if(dirPath == NULL) {                cout<<"dirPath error."<<endl;                return -1;        }        FileListNode* fileHeadNode = CreateFileList(dirPath);        if(fileHeadNode == NULL) {                cout<<"the fileHeadNode is NULL."<<endl;                return -1;        }                int err;        int i = 0;        ArgParam *arg;        //从链表中一个一个出来,分布线程去读取,注意从链表出来时,要加锁。while(fileHeadNode)        while(fileHeadNode != NULL) {                pthread_mutex_lock(&g_mutex_Msg);                err = pthread_create(&m_ThreadId[i], NULL, AdjustFun, NULL);        }                return 0;}/** * note :指定线程的数量,读取目录下的文件 * * @param numThread:线程的数量 * @param path:要读的路径 * @param arg:创建线程的参数 * * @return  */int Analys::CreateThread(int numThread, char* ReadPath, char* WritePath, char* delim){        DIR* dir;        struct dirent *ptr;        ArgParam* arg;        arg = new ArgParam;        arg->delim = delim;        dir = opendir(ReadPath);        if(dir == NULL) {                cout<<"Open dirrent error."<<endl;                return -1;        }                int i = 0;        int err;        void *state;        string tmp;        int tmpLen;        while(NULL != (ptr = readdir(dir))) {        //for(i = 0; i<numThread ; i++) {                if(ptr->d_type != 8) {                        continue;                }                arg->ReadPath = ptr->d_name;                string s1 = WritePath;                string s2 = "/";                string s3 = ptr->d_name;                tmp = s1 + s2 + s3 + "Copy.log";                cout<<"WritePath is :"<<tmp;                tmpLen = tmp.length();                arg->WritePath = new char[tmpLen+1];//(char*)malloc((tmpLen+1)*sizeof(char));                tmp.copy(arg->WritePath, tmpLen, 0);                cout<<"-----arg->WritePath:"<<arg->WritePath<<endl;                FILE *fp = fopen(arg->WritePath, "wa");                if(fp == NULL) {                        cout<<"open file error."<<endl;                } else {                        cout<<"open file success."<<endl;                        fclose(fp);                                        }                cout<<"creating thread no."<<i<<" :";                err = pthread_create(&m_ThreadId[i], NULL, AdjustFun, (void*)arg);                if(err != 0) {                        cout<<"create thread error."<<endl;                        return -1;                }                cout<<" success."<<endl;                if(i < numThread) {                        i++;                }else if(i == numThread) {                        i = 0;                }                sleep(10);                delete arg->WritePath;        }        for(int j = 0; j < numThread; j++) {                pthread_join(m_ThreadId[j], &state);                cout<<"thread "<<j<<" is destory."<<endl;        }        delete arg;        return 0;}/** * note 预处理目录,把目录中的文件名放在链表里,用于之后的多线程读取文件 * * @param dirPath * * @return 成功,返回链表的头节点 */FileListNode* Analys::CreateFileList(char* dirPath){        DIR* dir;        struct dirent *ptr;        FileListNode* pHead = NULL;        FileListNode* temp;        FileListNode* pEnd;        dir = opendir(dirPath);        if(dir == NULL) {                cout<<"open dirpath error."<<endl;                return NULL;        }        while((ptr = readdir(dir)) != NULL) {                temp = new FileListNode;                temp->filePath = ptr->d_name;                if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) {   ///current dir OR parrent dir                        continue;                } else if(ptr->d_type == 8) {   ///file                        //printf("d_name:%s/%s\n",basePath,ptr->d_name);                        if(NULL == pHead) {                                pHead = temp;                        } else {                                pEnd->pNext = temp;                        }                        pEnd = temp;                }                /*else if(ptr->d_type == 10) {   ///link file                        printf("d_name:%s/%s\n",basePath,ptr->d_name);                } else if(ptr->d_type == 4) {   ///dir                        memset(base,'\0',sizeof(base));                        strcpy(base,basePath);                        strcat(base,"/");                        strcat(base,ptr->d_name);                        readFileList(base);                }*/        }                closedir(dir);        return pHead;}int main(int argc, char *argv[]){        /*if(argc != 3) {                cout<<"enter Paramter error!"<<endl;                return -1;        }*/        Analys an;        FileListNode* head;        head = an.CreateFileList(argv[1]);        while(head) {                //cout<<argv[1]<<"/"<<head->filePath<<endl;                head = head->pNext;        }        //an.RWData(argv[1], "\t", argv[2]);        //void *state;                //ArgParam* arg;        //arg = new ArgParam;                //an.m_Arg->ReadPath = argv[1];        //an.m_Arg->delim = "\t";        //an.m_Arg->WritePath = argv[2];                an.CreateThread(4, argv[1], argv[2], "\t");                //int err;        //pthread_t thread_id;        //err = pthread_create(&thread_id, NULL, AdjustFun, (void*)arg);        //if(err != 0) {        //        cout<<"create thread erro."<<endl;        //        return -1;        //}        //pthread_join(thread_id, &state);                 //delete arg;        return 0;}

0 0