纯c语言读取csv标准格式的文件,并进行字符的转码

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

/*Í·Îļþº¯ÊýÉùÃ÷*/#ifndef_PARSE_CSV_QUEUE_H#define_PARSE_CSV_QUEUE_H#include<stdio.h>#include<stdlib.h>#include<ctype.h>#include<string.h>#ifdef__cplusplusextern"C"{#endif#defineQUEUE_LEN1000typedefstructcsv_node{structcsv_node*_node_next;void*_data;}csv_node;typedefstructcsv_queue{structcsv_node*_queue_head;structcsv_node*_queue_tail;unsignedint_queue_length;}csv_queue;externcsv_node*init_node(csv_node*);externcsv_queue*init_queue(csv_queue*);externcsv_node*push_queue(csv_queue*,void*);externvoid*pop_queue(csv_queue*);externvoid*queue_head(csv_queue*);externvoid*queue_tail(csv_queue*);externintqueue_length(csv_queue*);externintqueue_destroy(csv_queue**);externintcsvstr_conv_link(csv_queue*,constunsignedchar*,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;returnnd;}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;returnque;}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;que->_queue_length++;}returntmp_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);returndt;}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;}intqueue_length(csv_queue*que){return(que==(void*)0||que->_queue_length==0)?0:que->_queue_length;}intqueue_destroy(csv_queue**que){intdestroy_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;returndestroy_num;}intcsvstr_conv_link(csv_queue*que,constunsignedchar*csv_str,intflag_chr,intbreak_chr){inti=0;unsignedchar*tmp_break_pos,*break_pos,*element,*tmp_csv,*parse_csv;intelement_len,csv_len=strlen(csv_str);tmp_csv=(unsignedchar*)malloc(sizeof(unsignedchar)*(csv_len+2));memset((void*)tmp_csv,'\0',(csv_len+1)*sizeof(unsignedchar));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=(unsignedchar*)malloc((element_len+2)*sizeof(unsignedchar));memset((void*)element,'\0',(element_len+2)*sizeof(unsignedchar));strcpy(element,parse_csv+1);if(push_queue(que,(void*)element)==(void*)0)returni;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=(unsignedchar*)malloc((element_len+2)*sizeof(unsignedchar));memset((void*)element,'\0',(element_len+2)*sizeof(unsignedchar));strcpy(element,parse_csv+1);if(push_queue(que,(void*)element)==(void*)0)returni;i++;free(tmp_csv);returni;}/*Ö÷º¯Êý*/#include<unistd.h>#include<fcntl.h>#include<iconv.h>#include<memory.h>#include"csv_queue.h"#defineBUF_SIZE4096intcode_convert(char*from_charset,char*to_charset,char*inbuf,size_tinlen,char*outbuf,size_toutlen){iconv_tcd;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);return0;}intgbk2utf(char*inbuf,size_tinlen,char*outbuf,size_toutlen){returncode_convert("GBK","UTF-8",inbuf,inlen,outbuf,outlen);}intutf2gbk2(char*inbuf,size_tinlen,char*outbuf,size_toutlen){returncode_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)){charin_buf[BUF_SIZE+1],*out_buf,*tmp_read_buf,*tmp_out_buf,*line_pos;intconv_beforelen,conv_afterlen,block_num=0;FILE*instream;FILE*out

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功