Skip to content

Instantly share code, notes, and snippets.

@yongs2
Last active July 18, 2018 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yongs2/80ac9b55b761bd7748de1ec033fee61c to your computer and use it in GitHub Desktop.
Save yongs2/80ac9b55b761bd7748de1ec033fee61c to your computer and use it in GitHub Desktop.
my_strtok_r 함수, delimitor 사이의 공백도 리턴함
char *my_strtok_r(char *text, const char *delimitor, char **save_this)
{
int nLen = 0;
if (delimitor == NULL) {
return NULL;
}
nLen = strlen(delimitor);
if (nLen <= 0) {
return NULL;
}
if(save_this == NULL)
{
return NULL;
}
if (text != NULL)
{
/* New text. */
int i = 0;
while(text[i] != '\0')
{
if(strncmp(text+i, delimitor, nLen) == 0)
{
text[i] = '\0';
*save_this = &text[i + nLen];
//printf("[%s:%s:%d] i=%d, return save_this=0x%p\n", __FILE__, __func__, __LINE__, i, *save_this);
return text;
}
i++;
}
}
else if ((save_this != NULL) && (*save_this != NULL))
{
/* Old text. */
int i = 0;
char *start = *save_this;
while((*save_this)[i] != '\0')
{
//printf("[%s:%s:%d] i=%d, *save_this[%d]=[%c]\n", __FILE__, __func__, __LINE__, i, i, (*save_this)[i]);
if(strncmp(*save_this+i, delimitor, nLen) == 0)
{
(*save_this)[i] = '\0';
*save_this = &((*save_this)[i + nLen]);
//printf("[%s:%s:%d] i=%d, return start=0x%p\n", __FILE__, __func__, __LINE__, i, start);
return start;
}
i++;
}
*save_this = NULL;
save_this = NULL;
return start;
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment