aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils
diff options
context:
space:
mode:
authorMax Stepanov <Max.Stepanov@intel.com>2015-07-02 13:21:19 (GMT)
committerJouni Malinen <j@w1.fi>2015-07-26 21:47:19 (GMT)
commitadd59757519aa8222c9c3b2f9ece60137a28b8e7 (patch)
treef844929702c1916b621329155378504a6e2a328b /src/utils
parente999e56c2c878c26f2c9ed61da1af9dce652e228 (diff)
downloadhostap-add59757519aa8222c9c3b2f9ece60137a28b8e7.zip
hostap-add59757519aa8222c9c3b2f9ece60137a28b8e7.tar.gz
hostap-add59757519aa8222c9c3b2f9ece60137a28b8e7.tar.bz2
utils: Add cstr_token() function
Add an auxiliary cstr_token() function to get a token from a const char string. The function usage syntax is similar to str_token(), but unlike str_token() the function doesn't modify the buffer of the string. Change str_token() function implementation to use cstr_token(). Signed-off-by: Max Stepanov <Max.Stepanov@intel.com> Reviewed-by: Andrei Otcheretianski <andrei.otcheretianski@intel.com> Reviewed-by: Ilan Peer <ilan.peer@intel.com>
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/common.c63
-rw-r--r--src/utils/common.h1
2 files changed, 47 insertions, 17 deletions
diff --git a/src/utils/common.c b/src/utils/common.c
index 5cf0d57..660e9fc 100644
--- a/src/utils/common.c
+++ b/src/utils/common.c
@@ -973,6 +973,48 @@ int random_mac_addr_keep_oui(u8 *addr)
/**
+ * cstr_token - Get next token from const char string
+ * @str: a constant string to tokenize
+ * @delim: a string of delimiters
+ * @last: a pointer to a character following the returned token
+ * It has to be set to NULL for the first call and passed for any
+ * futher call.
+ * Returns: a pointer to token position in str or NULL
+ *
+ * This function is similar to str_token, but it can be used with both
+ * char and const char strings. Differences:
+ * - The str buffer remains unmodified
+ * - The returned token is not a NULL terminated string, but a token
+ * position in str buffer. If a return value is not NULL a size
+ * of the returned token could be calculated as (last - token).
+ */
+const char * cstr_token(const char *str, const char *delim, const char **last)
+{
+ const char *end, *token = str;
+
+ if (!str || !delim || !last)
+ return NULL;
+
+ if (*last)
+ token = *last;
+
+ while (*token && os_strchr(delim, *token))
+ token++;
+
+ if (!*token)
+ return NULL;
+
+ end = token + 1;
+
+ while (*end && !os_strchr(delim, *end))
+ end++;
+
+ *last = end;
+ return token;
+}
+
+
+/**
* str_token - Get next token from a string
* @buf: String to tokenize. Note that the string might be modified.
* @delim: String of delimiters
@@ -982,25 +1024,12 @@ int random_mac_addr_keep_oui(u8 *addr)
*/
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++;
+ char *token = (char *) cstr_token(str, delim, (const char **) context);
- if (*end)
- *end++ = '\0';
+ if (token && **context)
+ *(*context)++ = '\0';
- *context = end;
- return pos;
+ return token;
}
diff --git a/src/utils/common.h b/src/utils/common.h
index aab4fc8..0b9cc3d 100644
--- a/src/utils/common.h
+++ b/src/utils/common.h
@@ -528,6 +528,7 @@ void bin_clear_free(void *bin, size_t len);
int random_mac_addr(u8 *addr);
int random_mac_addr_keep_oui(u8 *addr);
+const char * cstr_token(const char *str, const char *delim, const char **last);
char * str_token(char *str, const char *delim, char **context);
size_t utf8_escape(const char *inp, size_t in_size,
char *outp, size_t out_size);