From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f193.google.com (mail-wr0-f193.google.com [209.85.128.193]) by dpdk.org (Postfix) with ESMTP id E0ECF5F72 for ; Wed, 21 Mar 2018 18:16:04 +0100 (CET) Received: by mail-wr0-f193.google.com with SMTP id c24so5987765wrc.6 for ; Wed, 21 Mar 2018 10:16:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=6wind-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=6SjHYFp2UmNXSHWd7rqzlnjp+TP5H4pyim5hgvUbVTU=; b=qRyQ1pRON2+dn3D+OswrCy5RiaF2rkFk738bR8etsYtvynabhdD+TtF2FcoHlnEde7 3I1KMc792XjElgxb7h5+3Xp9mMb2gyxUFCqkq2TZo8v9oRRVJ4hVhE7lr87y07y2JnQf Nn/qjBG1rxCLCotQjIy0nCqUU4EsecKLaTNQkwwUpmARWnIF1KOuD8lZ6YxWaZ4/5Up5 xYMpabJl+I8mi1Ng6xQukfn4hZ+Ehg1095Gl8+l9a0J0W2MvSyKOv5/+FnnM8BFWv9WU uc/E9ntW7ec4le4jDHCtFxo5kquOd5CpznefBA8GUopvBbRIk/fZ4gA0jQqa+U9VQHHL 6gnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=6SjHYFp2UmNXSHWd7rqzlnjp+TP5H4pyim5hgvUbVTU=; b=Xkc8wuxzPaHcB+0bSVeOknQDgvIJwW+dSG2n1OX4i0bvKawTRAdxI9rgBlEQ0waOeq SKT7b7TSEdiKX4i5GDbXDsFQQnO37y6F2JSkC6f2o2lMalca5T4YB6Utcg5TOD1yemBO zsY5p6t2+8qsW1C7nN6GXRJUjCIs+wXs+eKYqxWJls+mBXrEWKRvflCQ++I1Se/wKIUz 6gGhsisPnoog84yLIHji2GN9bt5ETFPodfPXOtEi5GgfUiHwalOmUhBGlJdMGpMIXsgY 2e3eZN+3Lkd5cqvFD8IjjlZk67BTUdP+dBcSa0QntDmLU6W460vOzVA1qk78E5EvPnRY 5jIg== X-Gm-Message-State: AElRT7GqAPIEoQGMWlzrt2yOEXEsOFkg1v1IifoqI7UAewPK/hOBtUUT J+sy/jyImW8kj+4TIoHDgster/OE X-Google-Smtp-Source: AG47ELvlLVmxZniZAKUqm9JmG/VuEc7X9hdy3rotytkMAb05aJzA27f60/In+M3y9cS2CYuA1fCPqw== X-Received: by 10.223.176.144 with SMTP id i16mr16407972wra.70.1521652563938; Wed, 21 Mar 2018 10:16:03 -0700 (PDT) Received: from bidouze.dev.6wind.com. (host.78.145.23.62.rev.coltfrance.com. [62.23.145.78]) by smtp.gmail.com with ESMTPSA id l10sm4224101wrf.37.2018.03.21.10.16.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Mar 2018 10:16:02 -0700 (PDT) From: Gaetan Rivet To: dev@dpdk.org Cc: Gaetan Rivet Date: Wed, 21 Mar 2018 18:15:25 +0100 Message-Id: X-Mailer: git-send-email 2.11.0 In-Reply-To: References: In-Reply-To: References: Subject: [dpdk-dev] [PATCH v2 04/18] eal: add lightweight kvarg parsing utility X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Mar 2018 17:16:05 -0000 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