字符串分割_链表
/*str_tok.h*/
#ifndef _H_STR_TOK
#define _H_STR_TOK
#include <string.h>
#include <stdio.h>
#include "FPL_types.h"
typedef struct __NODE{
char *pch;
struct __NODE *next;
}_NODE;
typedef struct __STRCK_TOK{
int _MAXLEN;
char *pStr;
_NODE* phead;
void (*setMaxLen)(struct __STRCK_TOK *,int);
int (*getMaxLen)(struct __STRCK_TOK *);
int (*setLnk)(struct __STRCK_TOK *,char *pin,const char* delimit);
bool (*search)(struct __STRCK_TOK *,char *);
void (*dump)(struct __STRCK_TOK* str_tok);
void (*dinit)(struct __STRCK_TOK* str_tok);
}_STRCK_TOK;
#endif
/*
Module Name:
str_tok.c
Abstract:
将一个字符串按照指定的分隔符存储在一个链表中
本模块把一些常用的操作封装在一个结构体内。
使用前,在定义结构体之后,必须使用str_tok_init进行结构体里的函数指针赋值,
然后使用setMaxLen设置分割后的子字符串的最大长度(考虑字符串最后的null字符)
程序最后必须调用dinit释放内存
Functions:
setMaxLen
getMaxLen
setLnk
dinit
dump
search
str_tok_init
Author:
ffcs huangbt 20090220
*/
#include "str_tok.h"
void setMaxLen(struct __STRCK_TOK *str_tok,int val)
{
str_tok->_MAXLEN=val;
}
int getMaxLen(struct __STRCK_TOK* str_tok)
{
return str_tok->_MAXLEN;
}
int setLnk(struct __STRCK_TOK* str_tok,char *pin,const char* delimit)
{
char *token;
_NODE *phead=NULL;
str_tok->pStr=pin;
if(!str_tok)
return -1;
if(!pin||!*pin)
return -1;
if(!delimit||!*delimit)
return -1;
if(str_tok->_MAXLEN<=0)
return -1;
token=strtok(pin,delimit);
while(token!=NULL)
{
char *pAlloc=(char *)malloc(str_tok->_MAXLEN*sizeof(char));
_NODE *p=(_NODE *)malloc(sizeof(_NODE));
if(pAlloc==NULL)
return -1;
if(p==NULL)
return -1;
p->pch=pAlloc;
if(strlen(token)>str_tok->_MAXLEN-1)
{
fprintf(stderr,"fail at file %s,line %d\n",__FILE__,__LINE__);
return -1;
}
strcpy(p->pch,token);
p->next=NULL;
if(phead==NULL)
phead=p;
else
{
p->next=phead;
phead=p;
}
token=strtok(NULL,delimit);
}
str_tok->phead=phead;
}
void dinit(struct __STRCK_TOK* str_tok)
{
_NODE *p;
if(!str_tok->phead)
return;
p=str_tok->phead;
while(p)
{
_NODE *ptmp=p;
p=p->next;
free(ptmp->pch);
free(ptmp);
}
}
void dump(struct __STRCK_TOK* str_tok)
{
_NODE *p;
if(!str_tok||!str_tok->phead)
return;
p=str_tok->phead;
while(p)
{
_NODE *ptmp=p;
p=p->next;
fprintf(stdout,"%s,",ptmp->pch);
}
fprintf(stdout,"\n");
}
bool search(struct __STRCK_TOK* str_tok,char *str)
{
_NODE *p;
if(!str_tok||!str_tok->phead)
return false;
p=str_tok->phead;
while(p)
{
if(strcmp(p->pch,str)==0)
return true;
p=p->next;
}
return false;
}
void str_tok_init(struct __STRCK_TOK* str_tok)
{
str_tok->phead=NULL;
str_tok->_MAXLEN=0;
str_tok->dump=dump;
str_tok->getMaxLen=getMaxLen;
str_tok->search=search;
str_tok->setLnk=setLnk;
str_tok->setMaxLen=setMaxLen;
str_tok->dinit=dinit;
}
/*
void main()
{
char ch1[256]="1|2|3|4|5|6|7|8|9";
char ch2[256]="a|b|c|d|e|f|g|h|i";
_STRCK_TOK strck_tok[2];
int i;
for(i=0;i<2;i++)
{
_STRCK_TOK *p_strck_tok=&strck_tok[i];
str_tok_init(&strck_tok[i]);
p_strck_tok->setMaxLen(p_strck_tok,10);
if(i==0)
{
if(p_strck_tok->setLnk(p_strck_tok,ch1,"|")==-1)
printf("fail!\n");
}
else if(i==1)
{
if(p_strck_tok->setLnk(p_strck_tok,ch2,"|")==-1)
printf("fail!\n");
}
p_strck_tok->dump(p_strck_tok);
if(p_strck_tok->search(p_strck_tok,"1"))
fprintf(stdout,"1 exist!\n");
else
fprintf(stdout,"1 not exist!\n");
if(p_strck_tok->search(p_strck_tok,"a"))
fprintf(stdout,"a exist!\n");
else
fprintf(stdout,"a not exist!\n");
}
(&strck_tok[0])->dinit(&strck_tok[0]);
(&strck_tok[1])->dinit(&strck_tok[1]);
}*/
#ifndef _H_STR_TOK
#define _H_STR_TOK
#include <string.h>
#include <stdio.h>
#include "FPL_types.h"
typedef struct __NODE{
char *pch;
struct __NODE *next;
}_NODE;
typedef struct __STRCK_TOK{
int _MAXLEN;
char *pStr;
_NODE* phead;
void (*setMaxLen)(struct __STRCK_TOK *,int);
int (*getMaxLen)(struct __STRCK_TOK *);
int (*setLnk)(struct __STRCK_TOK *,char *pin,const char* delimit);
bool (*search)(struct __STRCK_TOK *,char *);
void (*dump)(struct __STRCK_TOK* str_tok);
void (*dinit)(struct __STRCK_TOK* str_tok);
}_STRCK_TOK;
#endif
/*
Module Name:
str_tok.c
Abstract:
将一个字符串按照指定的分隔符存储在一个链表中
本模块把一些常用的操作封装在一个结构体内。
使用前,在定义结构体之后,必须使用str_tok_init进行结构体里的函数指针赋值,
然后使用setMaxLen设置分割后的子字符串的最大长度(考虑字符串最后的null字符)
程序最后必须调用dinit释放内存
Functions:
setMaxLen
getMaxLen
setLnk
dinit
dump
search
str_tok_init
Author:
ffcs huangbt 20090220
*/
#include "str_tok.h"
void setMaxLen(struct __STRCK_TOK *str_tok,int val)
{
str_tok->_MAXLEN=val;
}
int getMaxLen(struct __STRCK_TOK* str_tok)
{
return str_tok->_MAXLEN;
}
int setLnk(struct __STRCK_TOK* str_tok,char *pin,const char* delimit)
{
char *token;
_NODE *phead=NULL;
str_tok->pStr=pin;
if(!str_tok)
return -1;
if(!pin||!*pin)
return -1;
if(!delimit||!*delimit)
return -1;
if(str_tok->_MAXLEN<=0)
return -1;
token=strtok(pin,delimit);
while(token!=NULL)
{
char *pAlloc=(char *)malloc(str_tok->_MAXLEN*sizeof(char));
_NODE *p=(_NODE *)malloc(sizeof(_NODE));
if(pAlloc==NULL)
return -1;
if(p==NULL)
return -1;
p->pch=pAlloc;
if(strlen(token)>str_tok->_MAXLEN-1)
{
fprintf(stderr,"fail at file %s,line %d\n",__FILE__,__LINE__);
return -1;
}
strcpy(p->pch,token);
p->next=NULL;
if(phead==NULL)
phead=p;
else
{
p->next=phead;
phead=p;
}
token=strtok(NULL,delimit);
}
str_tok->phead=phead;
}
void dinit(struct __STRCK_TOK* str_tok)
{
_NODE *p;
if(!str_tok->phead)
return;
p=str_tok->phead;
while(p)
{
_NODE *ptmp=p;
p=p->next;
free(ptmp->pch);
free(ptmp);
}
}
void dump(struct __STRCK_TOK* str_tok)
{
_NODE *p;
if(!str_tok||!str_tok->phead)
return;
p=str_tok->phead;
while(p)
{
_NODE *ptmp=p;
p=p->next;
fprintf(stdout,"%s,",ptmp->pch);
}
fprintf(stdout,"\n");
}
bool search(struct __STRCK_TOK* str_tok,char *str)
{
_NODE *p;
if(!str_tok||!str_tok->phead)
return false;
p=str_tok->phead;
while(p)
{
if(strcmp(p->pch,str)==0)
return true;
p=p->next;
}
return false;
}
void str_tok_init(struct __STRCK_TOK* str_tok)
{
str_tok->phead=NULL;
str_tok->_MAXLEN=0;
str_tok->dump=dump;
str_tok->getMaxLen=getMaxLen;
str_tok->search=search;
str_tok->setLnk=setLnk;
str_tok->setMaxLen=setMaxLen;
str_tok->dinit=dinit;
}
/*
void main()
{
char ch1[256]="1|2|3|4|5|6|7|8|9";
char ch2[256]="a|b|c|d|e|f|g|h|i";
_STRCK_TOK strck_tok[2];
int i;
for(i=0;i<2;i++)
{
_STRCK_TOK *p_strck_tok=&strck_tok[i];
str_tok_init(&strck_tok[i]);
p_strck_tok->setMaxLen(p_strck_tok,10);
if(i==0)
{
if(p_strck_tok->setLnk(p_strck_tok,ch1,"|")==-1)
printf("fail!\n");
}
else if(i==1)
{
if(p_strck_tok->setLnk(p_strck_tok,ch2,"|")==-1)
printf("fail!\n");
}
p_strck_tok->dump(p_strck_tok);
if(p_strck_tok->search(p_strck_tok,"1"))
fprintf(stdout,"1 exist!\n");
else
fprintf(stdout,"1 not exist!\n");
if(p_strck_tok->search(p_strck_tok,"a"))
fprintf(stdout,"a exist!\n");
else
fprintf(stdout,"a not exist!\n");
}
(&strck_tok[0])->dinit(&strck_tok[0]);
(&strck_tok[1])->dinit(&strck_tok[1]);
}*/