#ifndef _ANALYS_H_
#define _ANALYS_H_
#include <string.h>
#include "Object.h"
#include "ArgParam.h"
#define FGETSMAX 6*1024*1024 //获取一行数据的最大倿M
//class Object;
#define MAXNUMTHREAD 1000
class Analys
{
public:
Analys();
~Analys();
public:
FILE* m_Fp;
char* m_LineBuf;
char** m_Dest;
pthread_t *m_ThreadId;
FileParam* pFilePara;
public:
Object** m_Obj;
ArgParam* m_Arg;
public:
int Segment(char* str,char* delim,char** &dest, int* pCount, Object** ob);
int LoadData(Object** obj, int dataCnt, FILE* fp, char *path);
int RWData(char* ReadPath, char* delim, char* WritePath);
//void* AdjustFun(void *arg);
int CreateThread(int numThread, char* path, ArgParam* arg);
public:
/*typedef struct Arg
{
char* ReadPath;
char* delim;
char* WritePath;
}ArgParam;*/
/*
typedef struct object
{
Object() {};
Object(char *v, int l)
: value(v)
, length(l)
{}
char* Value;
int Length;
}Object;
*/
};
/*typedef struct Arg
{
char* ReadPath;
char* delim;
char* WritePath;
}ArgParam;*/
//void* AdjustFun(ArgParam *arg);
void* AdjustFun(void *arg);
#endif
#include <iostream>
#include <stdio.h>#include <pthread.h>
#include <unistd.h>
#include "Analys.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_destroy(&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,FILE* Rfp,FILE* Wfp)
{
int count = 0;
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;
FileParam* arg = (FileParam*)argg;
int ret = pAnl.RWData(arg->ReadPath, arg->delim, arg->WritePath,arg->Rfp,arg->Wfp);
if(ret != 0) {
cout<<"RWData error."<<endl;
}
return NULL;
}
/**
* note :指定线程的数量,读取目录下的文件
*
* @param numThread:线程的数量
* @param path:要读的路径
* @param arg:创建线程的参数
*
* @return
*/
int Analys::CreateThread(int numThread, char* path, ArgParam* arg)
{
int i;
int err;
void *state;
for(i = 0; i<numThread; i++) {
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;
}
while(i--) {
pthread_join(m_ThreadId[i], &state);
}
return 0;
}
int main(int argc, char *argv[])
{
if(argc != 3) {
cout<<"enter Paramter error!"<<endl;
return -1;
}
Analys an;
//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];
FILE* Rfp = NULL;
FILE* Wfp = NULL;
an.m_Arg->Rfp = fopen(ReadPath, "r");
if(an.m_Arg->Rfp == NULL) {
cout<<"open Readpath fail !"<<endl;
return -1;
}
an.m_Arg->Wfp = fopen(WritePath, "wa");
if(an.m_Arg->Wfp == NULL) {
cout<<"open Readpath fail !"<<endl;
return -1;
}
an.CreateThread(1, "asdfa", an.m_Arg);
//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;
}