/*-------------------declaration-------------------*/
int
is_wordchr(char c);
int
reverse_str(const char* in_str,
int in_str_len,
char* out_buf,
int out_buf_size,
int (*is_word_char)(char )
);
static int dump_last_token(const char* in_str,
int in_str_len,
char* out_buf,
int (*is_word_char)(char ));
/*-------------------definition-------------------*/
int
/*============================================================*Author :redguardtoo
Date :2004-5-6
Description: reverse in_str and output the result int out_buf
Return :int (by default) - 0, failed; 1, succeed
Parameters :
const char* in_str -
int in_str_len -
char* out_buf -
int out_buf_size -
int (*is_word_char(char) - callback, if a char is word charater
Note :
\*============================================================*/
reverse_str(const char* in_str,
int in_str_len,
char* out_buf,
int out_buf_size,
int (*is_word_char)(char )
)
{
int ret;
assert(in_str);
assert(in_str_len>0);
assert(out_buf);
assert(out_buf_size>0);
assert(is_word_char);
assert(in_str_len<=out_buf_size);
if(!in_str_len||!out_buf_size)
goto errorhandle1;
ret=0;
do
{
ret=dump_last_token(in_str,in_str_len,out_buf,is_word_char);
in_str_len-=ret;
out_buf+=ret;
}while(ret);
return 1;
errorhandle1:
return 0;
}
/*============================================================*Author :redguardtoo
Date :2004-5-6
Description: A token is a sequence of word chararcters
or a sequence of non-word characers
Return :int - 0, failed; >0, the length of the dumpped token
Parameters :
const char* in_str -
int in_str_len -
char* out_buf -
int (*is_word_char) -
Note :
\*============================================================*/
static int
dump_last_token(const char* in_str,
int in_str_len,
char* out_buf,
int (*is_word_char)(char ))
{
int cnt;
int rlt;
/*============================================================*Author :redguard
Date :2004-5-6
Description: if a character is a word character
Return :int -
Parameters :
char c -
Note :
\*============================================================*/
int is_wordchr(char c)
{
if(c>='a'&&c<='z')
return 1;
if(c>='A'&&c<='Z')
return 1;
if(c=='_')
return 1;
if(c>='0'&&c<='9')
return 1;
return 0;
}
/*-------------------test main routine-------------------*/
int main(int argc, char* argv[])
{
#if 1
{
char str[]="abcdef 1234";
char buf[255];
int ret;
ret=dump_last_token(str,strlen(str),buf,is_wordchr);
assert(ret==4);
assert(strcmp(buf,"1234")==0);
}/*test case 1*/
#endif
#if 1
{
char str[]="abcdef ";
char buf[255];
int ret;
ret=dump_last_token(str,strlen(str),buf,is_wordchr);
assert(ret==3);
assert(strcmp(buf," ")==0);
}/*test case 2*/
#endif