diff mbox series

[v10,04/27] kvargs: introduce a more flexible parsing function

Message ID 6816f4a37e4f3869422377392689ec9ff3f25e7b.1530791217.git.gaetan.rivet@6wind.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers show
Series Device querying | expand

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Gaëtan Rivet July 5, 2018, 11:48 a.m. UTC
This function permits defining additional terminating characters,
ending the parsing to arbitrary delimiters.

Signed-off-by: Gaetan Rivet <gaetan.rivet@6wind.com>
---
 lib/Makefile                             |  1 +
 lib/librte_kvargs/meson.build            |  2 ++
 lib/librte_kvargs/rte_kvargs.c           | 25 ++++++++++++++++++++
 lib/librte_kvargs/rte_kvargs.h           | 30 ++++++++++++++++++++++++
 lib/librte_kvargs/rte_kvargs_version.map |  7 ++++++
 5 files changed, 65 insertions(+)

Comments

Thomas Monjalon July 5, 2018, 10 p.m. UTC | #1
05/07/2018 13:48, Gaetan Rivet:
> This function permits defining additional terminating characters,
> ending the parsing to arbitrary delimiters.
[...]
> +__rte_experimental
> +struct rte_kvargs *rte_kvargs_parse2(const char *args,
> +		const char *const valid_keys[],
> +		const char *valid_ends);

The name is not ideal but I don't have a better idea.

Acked-by: Thomas Monjalon <thomas@monjalon.net>
Shreyansh Jain July 11, 2018, 11:55 a.m. UTC | #2
On Friday 06 July 2018 03:30 AM, Thomas Monjalon wrote:
> 05/07/2018 13:48, Gaetan Rivet:
>> This function permits defining additional terminating characters,
>> ending the parsing to arbitrary delimiters.
> [...]
>> +__rte_experimental
>> +struct rte_kvargs *rte_kvargs_parse2(const char *args,
>> +		const char *const valid_keys[],
>> +		const char *valid_ends);
> 
> The name is not ideal but I don't have a better idea.
> 
Does 'rte_kvargs_parse_delim()' seem any better?
Or, a more obvious one 'rte_kvargs_parse_with_delim()'.

[...]

As I am already late in commenting on this, I am OK changing this later 
as well (using the 'experimental' blanket) when a better suggestion 
comes along. So, from me as well:

Acked-by: Shreyansh Jain <shreyansh.jain@nxp.com>
diff mbox series

Patch

diff --git a/lib/Makefile b/lib/Makefile
index e8e903c8f..8a65525cd 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -5,6 +5,7 @@  include $(RTE_SDK)/mk/rte.vars.mk
 
 DIRS-y += librte_compat
 DIRS-$(CONFIG_RTE_LIBRTE_KVARGS) += librte_kvargs
+DEPDIRS-librte_kvargs := librte_compat
 DIRS-$(CONFIG_RTE_LIBRTE_EAL) += librte_eal
 DIRS-$(CONFIG_RTE_LIBRTE_PCI) += librte_pci
 DEPDIRS-librte_pci := librte_eal
diff --git a/lib/librte_kvargs/meson.build b/lib/librte_kvargs/meson.build
index 0a81e8da7..a1c724961 100644
--- a/lib/librte_kvargs/meson.build
+++ b/lib/librte_kvargs/meson.build
@@ -7,3 +7,5 @@  includes += include_directories('../../../lib/librte_eal/common/include')
 version = 1
 sources = files('rte_kvargs.c')
 headers = files('rte_kvargs.h')
+
+deps += 'compat'
diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c
index 747f14964..519f77679 100644
--- a/lib/librte_kvargs/rte_kvargs.c
+++ b/lib/librte_kvargs/rte_kvargs.c
@@ -168,3 +168,28 @@  rte_kvargs_parse(const char *args, const char * const valid_keys[])
 
 	return kvlist;
 }
+
+__rte_experimental
+struct rte_kvargs *
+rte_kvargs_parse2(const char *args, const char * const valid_keys[],
+		  const char *valid_ends)
+{
+	struct rte_kvargs *kvlist = NULL;
+	char *copy;
+	size_t len;
+
+	if (valid_ends == NULL)
+		return rte_kvargs_parse(args, valid_keys);
+
+	copy = strdup(args);
+	if (copy == NULL)
+		return NULL;
+
+	len = strcspn(copy, valid_ends);
+	copy[len] = '\0';
+
+	kvlist = rte_kvargs_parse(copy, valid_keys);
+
+	free(copy);
+	return kvlist;
+}
diff --git a/lib/librte_kvargs/rte_kvargs.h b/lib/librte_kvargs/rte_kvargs.h
index 51b8120b8..98c4ed1bf 100644
--- a/lib/librte_kvargs/rte_kvargs.h
+++ b/lib/librte_kvargs/rte_kvargs.h
@@ -71,6 +71,36 @@  struct rte_kvargs {
 struct rte_kvargs *rte_kvargs_parse(const char *args,
 		const char *const valid_keys[]);
 
+/**
+ * Allocate a rte_kvargs and store key/value associations from a string.
+ * This version will consider any byte from valid_ends as a possible
+ * terminating character, and will not parse beyond any of their occurrence.
+ *
+ * The function allocates and fills an rte_kvargs structure from a given
+ * string whose format is key1=value1,key2=value2,...
+ *
+ * The structure can be freed with rte_kvargs_free().
+ *
+ * @param args
+ *   The input string containing the key/value associations
+ *
+ * @param valid_keys
+ *   A list of valid keys (table of const char *, the last must be NULL).
+ *   This argument is ignored if NULL
+ *
+ * @param valid_ends
+ *   Acceptable terminating characters.
+ *   If NULL, the behavior is the same as ``rte_kvargs_parse``.
+ *
+ * @return
+ *   - A pointer to an allocated rte_kvargs structure on success
+ *   - NULL on error
+ */
+__rte_experimental
+struct rte_kvargs *rte_kvargs_parse2(const char *args,
+		const char *const valid_keys[],
+		const char *valid_ends);
+
 /**
  * Free a rte_kvargs structure
  *
diff --git a/lib/librte_kvargs/rte_kvargs_version.map b/lib/librte_kvargs/rte_kvargs_version.map
index 2030ec46c..b9fe44b98 100644
--- a/lib/librte_kvargs/rte_kvargs_version.map
+++ b/lib/librte_kvargs/rte_kvargs_version.map
@@ -8,3 +8,10 @@  DPDK_2.0 {
 
 	local: *;
 };
+
+EXPERIMENTAL {
+	global:
+
+	rte_kvargs_parse2;
+
+} DPDK_2.0;