|
김지영 님이 쓰신 글 :
: 받아올때 aaa@bbbbbbb@123245@qwe123 이라는 스트링을 받아왔다면
: aaa
: bbbbbbb
: 123245
: qwe123
:
: 이렇게 끊고 싶은데 어떤명령을 써야할지 잘 모르겠어요..
: T.T 좀 알려주세요
음 굳이 AnsiString 를 사용안하신다면...
음..리눅스의 펄을 사용해보신 분들은...split이라는 함수를 아실겁니다..
이함수가 무척편리하죠...문자열을 원하는 형태로 자르는데 사용하거든요..
이거를 c로 구현한게 있어서 소개할께요..
간단한 예제도 올립니다..정말 간단한..ㅡ,.ㅡ;
사용방법은 밑에..split.c split.h를 포함시켜주고요..
char **toks;
char *ptr = "this is test programing";
int num;
toks = split(ptr, " ", 6, &num, 0);
//이런식으로 사용하시면...toks[0]=this toks[1] = is toks[2]=test toks[3]=programing가 들어가
고..
//num에는 4가 리턴됩니다..두번재 인자에는 공백으로 문자열을 자른거고요..원하는 문자로 얼마든지 자를
수 있습니다..
님이 원하시는건...split(ptr, "@", ,,,) 이런식으로 자르시면 원하는 결과를 얻으실거에요..ㅅㅅ;
아시는 분은 아시겠지만...이함수는 리눅스 IDS인 스노트에 있는 소스입니다..
----- split.c ------
#include "split.h"
char **split(char *str, char *sep, int max_strs, int *toks, char meta)
{
char **retstr; /* 2D array which is returned to caller */
char *idx; /* index pointer into str */
char *end; /* ptr to end of str */
char *sep_end; /* ptr to end of seperator string */
char *sep_idx; /* index ptr into seperator string */
int len = 0; /* length of current token string */
int curr_str = 0; /* current index into the 2D return array */
char last_char = (char)0xFF;
#ifdef DEBUG
printf("[*] Splitting string: %s\n", str);
printf("curr_str = %d\n", curr_str);
#endif
/* find the ends of the respective passed strings so our while()
loops know where to stop */
sep_end = sep + strlen(sep);
end = str + strlen(str);
/* remove trailing whitespace */
while(isspace((int)*(end-1)) && ((end-1) >= str)) *(--end) = '\0'; /* -1 because of NULL */
/* set our indexing pointers */
sep_idx = sep;
idx = str;
/* alloc space for the return string, this is where the pointers to the
tokens will be stored */
retstr = (char **) malloc((sizeof(char **) * max_strs));
max_strs--;
#ifdef DEBUG
printf("max_strs = %d curr_str = %d\n", max_strs, curr_str);
#endif
/* loop thru each letter in the string being tokenized */
while (idx < end)
{
/* loop thru each seperator string char */
while (sep_idx < sep_end)
{
/* if the current string-indexed char matches the current
seperator char... */
if ((*idx == *sep_idx) && (last_char != meta))
{
/* if there's something to store... */
if (len > 0)
{
#ifdef DEBUG
printf("Allocating %d bytes for token ", len + 1);
fflush(stdout);
#endif
if(curr_str <= max_strs)
{
/* allocate space for the new token */
retstr[curr_str] = (char *) malloc((sizeof(char) * len) + 1);
/* make sure we got a good allocation */
if (retstr[curr_str] == NULL)
{
fprintf(stderr, "msplit() got NULL substring malloc!\n");
exit(1);
}
/* copy the token into the return string array */
memcpy(retstr[curr_str], (idx - len), len);
retstr[curr_str][len] = 0;
#ifdef DEBUG
printf("tok[%d]: %s\n", curr_str, retstr[curr_str]);
fflush(stdout);
#endif
/* twiddle the necessary pointers and vars */
len = 0;
curr_str++;
#ifdef DEBUG
printf("curr_str = %d\n", curr_str);
printf("max_strs = %d curr_str = %d\n", max_strs, curr_str);
#endif
last_char = *idx;
idx++;
}
#ifdef DEBUG
printf("Checking if curr_str (%d) >= max_strs (%d)\n", curr_str, max_strs);
#endif
/* if we've gotten all the tokens requested, return the list */
if (curr_str >= max_strs)
{
while(isspace((int)*idx)) idx++;
len = end - idx;
#ifdef DEBUG
printf("Finishing up...\n");
printf("Allocating %d bytes for last token ", len + 1);
fflush(stdout);
#endif
retstr[curr_str] = (char *) malloc((sizeof(char) * len) + 1);
if (retstr[curr_str] == NULL)
printf("Got NULL back from substr malloc\n");
memcpy(retstr[curr_str], idx, len);
retstr[curr_str][len] = 0;
#ifdef DEBUG
printf("tok[%d]: %s\n", curr_str, retstr[curr_str]);
fflush(stdout);
#endif
*toks = curr_str + 1;
#ifdef DEBUG
printf("max_strs = %d curr_str = %d\n", max_strs, curr_str);
printf("mSplit got %d tokens!\n", *toks);
fflush(stdout);
#endif
return retstr;
}
}
else /* otherwise, the previous char was a seperator as well,
and we should just continue */
{
last_char = *idx;
idx++;
/* make sure to reset this so we test all the sep. chars */
sep_idx = sep;
len = 0;
}
}
else
{
/* go to the next seperator */
sep_idx++;
}
}
sep_idx = sep;
len++;
last_char = *idx;
idx++;
}
/* put the last string into the list */
if (len > 0)
{
#ifdef DEBUG
printf("Allocating %d bytes for last token ", len + 1);
fflush(stdout);
#endif
retstr[curr_str] = (char *) malloc((sizeof(char) * len) + 1);
if (retstr[curr_str] == NULL)
printf("Got NULL back from substr malloc\n");
memcpy(retstr[curr_str], (idx - len), len);
retstr[curr_str][len] = 0;
#ifdef DEBUG
printf("tok[%d]: %s\n", curr_str, retstr[curr_str]);
fflush(stdout);
#endif
*toks = curr_str + 1;
}
#ifdef DEBUG
printf("mSplit got %d tokens!\n", *toks);
fflush(stdout);
#endif
/* return the token list */
return retstr;
}
split.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char **split(char *str, char *sep, int max_str, int *toks, char meta);
|