From mboxrd@z Thu Jan 1 00:00:00 1970 From: Gaetan Rivet Subject: [PATCH v1 04/18] eal: add lightweight kvarg parsing utility Date: Thu, 15 Mar 2018 18:49:34 +0100 Message-ID: <0c0ddcb98c0230c5925fa53ff7fe7b9192d5f96f.1521124599.git.gaetan.rivet@6wind.com> References: Cc: Gaetan Rivet To: dev@dpdk.org Return-path: Received: from mail-wr0-f194.google.com (mail-wr0-f194.google.com [209.85.128.194]) by dpdk.org (Postfix) with ESMTP id 766607CE1 for ; Thu, 15 Mar 2018 18:50:16 +0100 (CET) Received: by mail-wr0-f194.google.com with SMTP id m12so9116132wrm.13 for ; Thu, 15 Mar 2018 10:50:16 -0700 (PDT) In-Reply-To: In-Reply-To: References: List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" This library offers a quick way to parse parameters passed with a key=value syntax. A single function is needed and finds the relevant element within the text. No dynamic allocation is performed. It is possible to chain the parsing of each pairs for quickly scanning a list. This utility is private to the EAL and should allow avoiding having to move around the more complete librte_kvargs. Signed-off-by: Gaetan Rivet --- lib/librte_eal/common/eal_common_dev.c | 38 ++++++++++++++++++++++++++++++++++ lib/librte_eal/common/eal_private.h | 34 ++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c index cd071442f..4032f1bd8 100644 --- a/lib/librte_eal/common/eal_common_dev.c +++ b/lib/librte_eal/common/eal_common_dev.c @@ -13,10 +13,48 @@ #include #include #include +#include #include #include "eal_private.h" +/* EAL-private function. */ +int +rte_parse_kv(const char *str, struct rte_kvarg *kv) +{ + const char *equal; + const char *end; + + if (str == NULL || str[0] == '\0') + return 1; + equal = strchr(str, '='); + if (equal == NULL) { + rte_errno = EINVAL; + return -1; + } + end = strchr(equal + 1, ','); + end = end ? end : strchr(equal + 1, '/'); + end = end ? end : strchr(equal + 1, '\0'); + if (end == NULL) { + rte_errno = ENODEV; + return -1; + } + if (kv == NULL) + return 0; + snprintf(kv->data, sizeof(kv->data), "%s", str); + kv->key = &kv->data[0]; + strchr(kv->data, end[0])[0] = '\0'; + if (strchr(kv->data, '=')) { + kv->value = strchr(kv->data, '=') + 1; + strchr(kv->data, '=')[0] = '\0'; + } + if (end[0] == '\0') + kv->next = NULL; + else + kv->next = end + 1; + return 0; +} + static int cmp_detached_dev_name(const struct rte_device *dev, const void *_name) { diff --git a/lib/librte_eal/common/eal_private.h b/lib/librte_eal/common/eal_private.h index 0b2877000..d2774a3ad 100644 --- a/lib/librte_eal/common/eal_private.h +++ b/lib/librte_eal/common/eal_private.h @@ -205,4 +205,38 @@ struct rte_bus *rte_bus_find_by_device_name(const char *str); int rte_mp_channel_init(void); +/* + * Lightweight kvarg parsing library. + */ + +#define RTE_MAX_KVARG_LEN 64 + +/** + * Kvarg representation. + */ +struct rte_kvarg { + char *key; /**< points the key in the data. */ + char *value; /**< points the value in the data. */ + const char *next; /**< next token to parse, if any. */ + char data[RTE_MAX_KVARG_LEN + 1]; /**< local copy of key and value. */ +}; + +/** + * Parse one kvarg. + * + * The key-value pair must be shorter than the rte_kvarg data size. + * + * @param[in] str + * text to parse. + * + * @param[out] kv + * kvarg structure to fill. + * + * @return + * 0 if parsing succeeded. + * >0 if there was nothing to parse. + * <0 on error, rte_errno is set. + */ +int rte_parse_kv(const char *str, struct rte_kvarg *kv); + #endif /* _EAL_PRIVATE_H_ */ -- 2.11.0