/*头文件函数声明*/
#ifndef _PARSE_CSV_QUEUE_H
#define _PARSE_CSV_QUEUE_H
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
#define QUEUE_LEN 1000
typedef struct csv_node{
struct csv_node *_node_next;
void *_data;
}csv_node;
typedef struct csv_queue{
struct csv_node *_queue_head;
struct csv_node *_queue_tail;
unsigned int _queue_length;
}csv_queue;
extern csv_node *init_node(csv_node *);
extern csv_queue *init_queue(csv_queue *);
extern csv_node *push_queue(csv_queue *,void *);
extern void *pop_queue(csv_queue *);
extern void *queue_head(csv_queue *);
extern void *queue_tail(csv_queue *);
extern int queue_length(csv_queue *);
extern int queue_destroy(csv_queue **);
extern int csvstr_conv_link(csv_queue *,const unsigned char *,int,int);
#ifdef __cplusplus
}
#endif
#endif
/
*源文件函数定义*/
#include "csv_queue.h"
csv_node *init_node(csv_node *nd){
if(nd==(void*)0){
if((nd=(csv_node *)malloc(sizeof(csv_node)))==(void*)0)
return (void*)0;
}
nd->_node_next=(void*)0;
nd->_data=(void*)0;
return nd;
}
csv_queue *init_queue(csv_queue *que){
if(que==(void*)0){
if((que=(csv_queue*)malloc(sizeof(csv_queue)))==(void*)0)
return (void*)0;
}
que->_queue_head=que->_queue_tail=(void*)0;
que->_queue_length=0;
return que;
}
csv_node *push_queue(csv_queue *que,void *dt){
csv_node *tmp_nd;
if(que==(void*)0)
return (void*)0;
tmp_nd=init_node((void*)0);
tmp_nd->_data=dt;
if(que->_queue_length==0){
que->_queue_head=que->_queue_tail=tmp_nd;
que->_queue_length++;
}
else{
que->_queue_tail->_node_next=tmp_nd;
que->_queue_tail=tmp_nd;
fprintf格式
que->_queue_length++;
}
return tmp_nd;
}
void *pop_queue(csv_queue *que){
csv_node *tmp_nd;
void *dt;
if(que==(void*)0||que->_queue_length==0)
return (void*)0;
tmp_nd=que->_queue_head;
dt=tmp_nd->_data;
que->_queue_head=tmp_nd->_node_next;
que->_queue_length--;
if(que->_queue_length==0)
que->_queue_head=que->_queue_tail=(void*)0;
free(tmp_nd);
return dt;
}
void *queue_tail(csv_queue *que){
return (que==(void*)0||que->_queue_length==0)?(void*)0:que->_queue_tail->_data;
}
void *queue_head(csv_queue *que){
return (que==(void*)0||que->_queue_length==0)?(void*)0:que->_queue_head->_data;
}
int queue_length(csv_queue *que){
return (que==(void*)0||que->_queue_length==0)?0:que->_queue_length;
}
int queue_destroy(csv_queue **que){
int destroy_num=0;
if(que==(void*)0||*que==(void*)0){
return -1;
}
while((*que)->_queue_head!=(void*)0){
destroy_num++;
free(pop_queue( *que));
}
free(*que);
*que=(void*)0;
return destroy_num;
}
int csvstr_conv_link(csv_queue *que,const unsigned char *csv_str,int flag_chr,int break_chr){
int i=0;
unsigned char *tmp_break_pos,*break_pos,*element,*tmp_csv,*parse_csv;
int element_len,csv_len=strlen(csv_str)
;
tmp_csv=(unsigned char*)malloc(sizeof(unsigned char)*(csv_len+2));
memset((void*)tmp_csv,'\0',(csv_len+1)*sizeof(unsigned char));
parse_csv=tmp_csv;
strcpy(parse_csv,csv_str);
if(que==(void*)0||csv_str==(void*)0||csv_len==0)
return -1;
if(parse_csv[0]!=flag_chr||parse_csv[csv_len-1]!=flag_chr)
return -1;
if(!ispunct(break_chr)||!ispunct(flag_chr))
return -1;
tmp_break_pos=(void*)0;
while((break_pos=strchr(parse_csv,break_chr))!=(void*)0){
if(*(break_pos-1)==*(break_pos+1)&&*(break_pos+1)==flag_chr){
*(break_pos-1)='\0';
if(tmp_break_pos!=(void*)0){
parse_csv=tmp_break_pos;
}
element_len=strlen(parse_csv+1);
element=(unsigned char*)malloc((element_len+2)*sizeof(unsigned char));
memset((void*)element,'\0',(element_len+2)*sizeof(unsigned char));
strcpy(element,parse_csv+1);
if(push_queue(que,(void*)element)==(void*)0)
return i;
i++;
parse_csv=break_pos+1;
tmp_break_pos=(void*)0;
}
else{
if(tmp_break_pos==(void*)0)
tmp_break_pos=parse_csv;
parse_csv=break_pos+1;
}
}
if(tmp_break_pos!=(void*)0)
parse_csv=tmp_break_pos;
element_len=strlen(parse_csv);
parse_csv[element_len-1]='\0';
element_len=strlen(parse_csv);
element=(unsigned char*)malloc((element_len+2)*sizeof(unsigned char));
memset((void*)element,'\0',(element_len+2)*sizeof(unsigned char));
strcpy(element,parse_csv+1);
if(push_queue(que,(void*)element)==(void*)0)
return i;
i++;
free(tmp_csv);
return i;
}
/*主函数*/
#include <unistd.h>
#include <fcntl.h>
#include <iconv.h>
#include <memory.h>
#include "csv_queue.h"
#define BUF_SIZE 4096
int code_convert(char *from_charset,char *to_charset,char *inbuf, size_t inlen,char *outbuf, size_t outlen){
iconv_t cd;
char **pin = &inbuf;
char **pout = &outbuf;
cd = iconv_open(to_charset,from_charset);
if (cd==0) return -1;
memset(outbuf,0,outlen);
if (iconv(cd, pin, &inlen,pout, &outlen)==-1) return -1;
iconv_close(cd);
return 0;
}
int gbk2utf(char *inbuf, size_t inlen,char *outbuf, size_t outlen){
return code_convert("GBK","UTF-8",inbuf,inlen,outbuf,outlen);
}
int utf2gbk2(char *inbuf, size_t inlen,char *outbuf, size_t outlen){
return code_convert("UTF-8","GBK",inbuf,inlen,outbuf,outlen);
}
char *read_stream(void *in_s,void *out_s,int (*code_convert)(char *,size_t,char *,size_t)){
char in_buf[BUF_SIZE+1],*out_buf,*tmp_read_buf,*tmp_out_buf,*line_pos;
int conv_beforelen,conv_afterlen,block_num=0;
FILE *instream;
FILE *outstream;
if(in_s==(void*)0)
return (void*)0;
instream=(FILE *)in_s;
outstream=(FILE *)out_s;
out_buf=(void*)0;
while(fgets(in_buf,BUF_SIZE+1,instream)!=(void*)0){
if((line_pos=strrchr(in_buf,'\n'))!=(void*)0||feof(instream)){
/*文本文件为DOS格式:最后一行没有换行符'\n',且换行符'\n'会插入'\r'*/
if(!feof(instream)){
*line_pos='\0';
if(*(line_pos-1)=='\r'){
*(line_pos-1)='\0';
}
}
conv_beforelen=block_num*BUF_SIZE+strlen(in_buf);
out_buf=(char *)realloc((void*)out_buf,sizeof(char)*(conv_beforelen+1));
if(block_num==0)
memset((void*)out_buf,'\0',sizeof(char)*(conv_beforelen+1));
strncat(out_buf,in_buf,strlen(in_buf)+1);
if((void*)code_convert!=(void*)0){
conv_afterlen=conv_beforelen*4;
tmp_out_buf=(char *)malloc(sizeof(char *)*conv_afterlen);
memset((void*)tmp_out_buf,'\0',conv_afterlen);
if(code_convert(out_buf,(size_t)conv_beforelen,tmp_out_buf,(size_t)conv_afterlen)==-1)
return (void*)0;
free(out_buf);
out_buf=tmp_out_buf;
}
if(outstream!=(void*)0)
fprintf(outstream,"%s\n",out_buf);
break;
}
else{
block_num++;
out_buf=(char *)realloc((void*)out_buf,sizeof(char)*(block_num*BUF_SIZE+1));
if(block_num==1)
memset((void*)out_buf,'\0',sizeof(char)*(BUF_SIZE+1));
strncat(out_buf,in_buf,BUF_SIZE+1);
}
}
return out_buf;
}
int main(int argc,char **argv){
int gbk2utf();
csv_queue *csv_q,*csv_e;
FILE *fcsv;
FILE *recsv;
char *tmp,flag=' ';
int i=0;
csv_q=init_queue((void*)0);
csv_e=init_queue((void*)0);
tmp=(void*)0;
if((recsv=fopen(argv[2],"w"))==(void*)0){
perror("file open faild!");
exit(-1);
}
if((fcsv=fopen(argv[1],"r"))==(void*)0){
perror("file open faild!");
exit(-1);
}
while((tmp=read_stream(fcsv,stdout,gbk2utf))!=(void*)0){
csvstr_conv_link(csv_e,tmp,'"',',');
push_queue(csv_q,(void*)csv_e);
csv_e=init_queue((void*)0);
free(tmp);
i++;
}
printf("read:%d\n",i);
i=0;
while(queue_length(csv_q)){
csv_e=(csv_queue*)queue_head(csv_q);
while(queue_length(csv_e)){
fprintf(recsv,"%c%s",flag,(char *)pop_queue(csv_e));
if(flag==' ')
flag=',';
}
fprintf(recsv,"\n");
pop_queue(csv_q);
flag=' ';
i++;
}
printf("write:%d\n",i);
fclose(recsv);
free(csv_q);
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。