#ifndef _ANALYS_H_
#define _ANALYS_H_
#include <string.h>
#include "Object.h"
#define FGETSMAX 6*1024*1024 //获取一行数据的最大值6M
class Object;
class Analys
{
public:
Analys();
~Analys();
public:
FILE* m_Fp;
char* m_LineBuf;
char** m_Dest;
public:
Object** m_Obj;
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);
public:
/*
typedef struct object
{
Object() {};
Object(char *v, int l)
: value(v)
, length(l)
{}
char* Value;
int Length;
}Object;
*/
};
#endif
#include <iostream>
#include <stdio.h>
#include "Analys.h"
using namespace std;
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];
}
}
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;
}
/**
* 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 Readpath 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;
}
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]);
return 0;
}