Message ID | 1413893812-5122-8-git-send-email-ilan.peer@intel.com |
---|---|
State | Accepted |
Headers | show |
On Tue, 2014-10-21 at 08:16 -0400, Ilan Peer wrote: > From: Eliad Peller <eliad@wizery.com> > > Add helper function to get next token from a string. > +/** > + * str_token - get next token from string > + * @buf: string to tokenize. Note that the string might be modified. > + * @delim: string of delimiters > + * @context: pointer to save our context. should be initialized with > + * NULL on the first call, and passed for any further call. > + * Returns: The next token, NULL if there are no more valid tokens. > + */ > +char *str_token(char *str, const char *delim, char **context) Isn't that equivalent to strtok_r(), and could at least where that's available be implemented using it? johannes
On Wed, Oct 22, 2014 at 9:48 AM, Johannes Berg <johannes@sipsolutions.net> wrote: > On Tue, 2014-10-21 at 08:16 -0400, Ilan Peer wrote: >> From: Eliad Peller <eliad@wizery.com> >> >> Add helper function to get next token from a string. > >> +/** >> + * str_token - get next token from string >> + * @buf: string to tokenize. Note that the string might be modified. >> + * @delim: string of delimiters >> + * @context: pointer to save our context. should be initialized with >> + * NULL on the first call, and passed for any further call. >> + * Returns: The next token, NULL if there are no more valid tokens. >> + */ >> +char *str_token(char *str, const char *delim, char **context) > > Isn't that equivalent to strtok_r(), and could at least where that's > available be implemented using it? > i don't like the strok_r usage syntax. the simple "while ((token = str_token(...)))" seems much more comfortable for me. i guess internally it could be implemented using strtok_r(), but i don't see much advantage in it, and strtok_r is not defined for C99, so i preferred implementing it. Eliad.
diff --git a/src/utils/common.c b/src/utils/common.c index 9902004..e19bd3e 100644 --- a/src/utils/common.c +++ b/src/utils/common.c @@ -866,3 +866,35 @@ int random_mac_addr_keep_oui(u8 *addr) addr[0] |= 0x02; /* locally administered */ return 0; } + + +/** + * str_token - get next token from string + * @buf: string to tokenize. Note that the string might be modified. + * @delim: string of delimiters + * @context: pointer to save our context. should be initialized with + * NULL on the first call, and passed for any further call. + * Returns: The next token, NULL if there are no more valid tokens. + */ +char *str_token(char *str, const char *delim, char **context) +{ + char *end, *pos = str; + + if (*context) + pos = *context; + + while (*pos && os_strchr(delim, *pos)) + pos++; + if (!*pos) + return NULL; + + end = pos + 1; + while (*end && !os_strchr(delim, *end)) + end++; + + if (*end) + *end++ = '\0'; + + *context = end; + return pos; +} diff --git a/src/utils/common.h b/src/utils/common.h index 14d9ad1..b245244 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -534,6 +534,7 @@ void int_array_add_unique(int **res, int a); #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +char *str_token(char *str, const char *delim, char **context); void str_clear_free(char *str); void bin_clear_free(void *bin, size_t len);