From patchwork Thu Jul 2 13:21:19 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilan Peer X-Patchwork-Id: 490629 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from maxx.maxx.shmoo.com (maxx.shmoo.com [205.134.188.171]) by ozlabs.org (Postfix) with ESMTP id 6C76E1402B8 for ; Thu, 2 Jul 2015 23:22:12 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id D6E2A17C664; Thu, 2 Jul 2015 09:22:10 -0400 (EDT) X-Virus-Scanned: amavisd-new at maxx.shmoo.com Received: from maxx.maxx.shmoo.com ([127.0.0.1]) by localhost (maxx.shmoo.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id hAxWMVNubsVY; Thu, 2 Jul 2015 09:22:10 -0400 (EDT) Received: from maxx.shmoo.com (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id 6FB5517C666; Thu, 2 Jul 2015 09:21:50 -0400 (EDT) X-Original-To: mailman-post+hostap@maxx.shmoo.com Delivered-To: mailman-post+hostap@maxx.shmoo.com Received: from localhost (localhost [127.0.0.1]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id 0CC6317C664 for ; Thu, 2 Jul 2015 09:21:49 -0400 (EDT) X-Virus-Scanned: amavisd-new at maxx.shmoo.com Received: from maxx.maxx.shmoo.com ([127.0.0.1]) by localhost (maxx.shmoo.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Y1anJvrvkXcW for ; Thu, 2 Jul 2015 09:21:42 -0400 (EDT) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by maxx.maxx.shmoo.com (Postfix) with ESMTP id 18B6B17C645 for ; Thu, 2 Jul 2015 09:21:41 -0400 (EDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP; 02 Jul 2015 06:21:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.15,393,1432623600"; d="scan'208";a="754788766" Received: from unknown (HELO ipeer-dev-3.jer.intel.com) ([10.12.217.137]) by fmsmga002.fm.intel.com with ESMTP; 02 Jul 2015 06:21:39 -0700 From: Ilan Peer To: hostap@lists.shmoo.com Subject: [PATCH 1/9] utils: Add cstr_token() function Date: Thu, 2 Jul 2015 16:21:19 +0300 Message-Id: <1435843287-25665-2-git-send-email-ilan.peer@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1435843287-25665-1-git-send-email-ilan.peer@intel.com> References: <1435843287-25665-1-git-send-email-ilan.peer@intel.com> Cc: Max Stepanov X-BeenThere: hostap@lists.shmoo.com X-Mailman-Version: 2.1.11 Precedence: list List-Id: HostAP Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: hostap-bounces@lists.shmoo.com Errors-To: hostap-bounces@lists.shmoo.com From: Max Stepanov 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 Reviewed-by: Andrei Otcheretianski Reviewed-by: Ilan Peer --- src/utils/common.c | 65 +++++++++++++++++++++++++++++++++++++++--------------- src/utils/common.h | 1 + 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/utils/common.c b/src/utils/common.c index 5cf0d57..40e937c 100644 --- a/src/utils/common.c +++ b/src/utils/common.c @@ -973,34 +973,63 @@ int random_mac_addr_keep_oui(u8 *addr) /** - * str_token - Get next token from a 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. + * 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). */ -char * str_token(char *str, const char *delim, char **context) +const char *cstr_token(const char *str, const char *delim, const char **last) { - char *end, *pos = str; + const char *end, *token = str; + + if (!str || !delim || !last) + return NULL; - if (*context) - pos = *context; + if (*last) + token = *last; - while (*pos && os_strchr(delim, *pos)) - pos++; - if (!*pos) + while (*token && os_strchr(delim, *token)) + token++; + + if (!*token) return NULL; - end = pos + 1; + end = token + 1; + while (*end && !os_strchr(delim, *end)) end++; - if (*end) - *end++ = '\0'; + *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 + * @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 *token = (char *)cstr_token(str, delim, (const char **)context); + + if (token && **context) + *(*context)++ = '\0'; - *context = end; - return pos; + return token; } diff --git a/src/utils/common.h b/src/utils/common.h index 88318f5..f184f8a 100644 --- a/src/utils/common.h +++ b/src/utils/common.h @@ -549,6 +549,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);