All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] settings: Add l_settings_{set,get}_bytes
@ 2020-09-16  1:11 Andrew Zaborowski
  2020-09-16  1:11 ` [PATCH 2/2] unit: Test l_settings_{get,set}_bytes Andrew Zaborowski
  2020-09-16 21:32 ` [PATCH 1/2] settings: Add l_settings_{set,get}_bytes Denis Kenzior
  0 siblings, 2 replies; 3+ messages in thread
From: Andrew Zaborowski @ 2020-09-16  1:11 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 2701 bytes --]

---
I went for "bytes" instead of "hexstring" because the latter is the
encoding while the former is the type of data stored.
---
 ell/ell.sym    |  2 ++
 ell/settings.c | 37 +++++++++++++++++++++++++++++++++++++
 ell/settings.h |  7 +++++++
 3 files changed, 46 insertions(+)

diff --git a/ell/ell.sym b/ell/ell.sym
index 044cf3c..66e0f08 100644
--- a/ell/ell.sym
+++ b/ell/ell.sym
@@ -444,6 +444,8 @@ global:
 	l_settings_set_double;
 	l_settings_get_float;
 	l_settings_set_float;
+	l_settings_get_bytes;
+	l_settings_set_bytes;
 	l_settings_remove_group;
 	l_settings_remove_key;
 	l_settings_has_embedded_group;
diff --git a/ell/settings.c b/ell/settings.c
index dac45c9..68f7a4a 100644
--- a/ell/settings.c
+++ b/ell/settings.c
@@ -1325,6 +1325,43 @@ LIB_EXPORT bool l_settings_set_float(struct l_settings *settings,
 	return l_settings_set_value(settings, group_name, key, buf);
 }
 
+LIB_EXPORT uint8_t *l_settings_get_bytes(const struct l_settings *settings,
+						const char *group_name,
+						const char *key,
+						size_t *out_len)
+{
+	const char *value = l_settings_get_value(settings, group_name, key);
+
+	if (!value)
+		return NULL;
+
+	if (value[0] == '\0') {
+		*out_len = 0;
+
+		/* Return something that can be l_freed but is not a NULL */
+		return l_memdup("", 1);
+	}
+
+	return l_util_from_hexstring(value, out_len);
+}
+
+LIB_EXPORT bool l_settings_set_bytes(struct l_settings *settings,
+					const char *group_name, const char *key,
+					const uint8_t *value, size_t value_len)
+{
+	char *buf;
+
+	if (unlikely(!settings || !value))
+		return false;
+
+	if (value_len)
+		buf = l_util_hexstring(value, value_len);
+	else
+		buf = l_strdup("");
+
+	return set_value(settings, group_name, key, buf);
+}
+
 LIB_EXPORT bool l_settings_remove_group(struct l_settings *settings,
 					const char *group_name)
 {
diff --git a/ell/settings.h b/ell/settings.h
index e4203af..f95ed9b 100644
--- a/ell/settings.h
+++ b/ell/settings.h
@@ -119,6 +119,13 @@ bool l_settings_get_float(const struct l_settings *settings,
 bool l_settings_set_float(struct l_settings *settings, const char *group_name,
 				const char *key, float in);
 
+uint8_t *l_settings_get_bytes(const struct l_settings *settings,
+				const char *group_name, const char *key,
+				size_t *out_len);
+bool l_settings_set_bytes(struct l_settings *settings, const char *group_name,
+				const char *key,
+				const uint8_t *value, size_t value_len);
+
 bool l_settings_remove_key(struct l_settings *settings, const char *group_name,
 				const char *key);
 bool l_settings_remove_group(struct l_settings *settings,
-- 
2.25.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] unit: Test l_settings_{get,set}_bytes
  2020-09-16  1:11 [PATCH 1/2] settings: Add l_settings_{set,get}_bytes Andrew Zaborowski
@ 2020-09-16  1:11 ` Andrew Zaborowski
  2020-09-16 21:32 ` [PATCH 1/2] settings: Add l_settings_{set,get}_bytes Denis Kenzior
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Zaborowski @ 2020-09-16  1:11 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1032 bytes --]

---
 unit/test-settings.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/unit/test-settings.c b/unit/test-settings.c
index fa3a533..a25197a 100644
--- a/unit/test-settings.c
+++ b/unit/test-settings.c
@@ -434,6 +434,8 @@ static void test_set_methods(const void *test_data)
 	bool b;
 	const char *v;
 	char *s;
+	uint8_t *bytes;
+	size_t len;
 
 	settings = l_settings_new();
 
@@ -503,6 +505,18 @@ static void test_set_methods(const void *test_data)
 	assert(v);
 	assert(!strcmp(v, "\\s\\\\Text\t\\n\\r\\\\"));
 
+	/* Bytes test */
+	assert(l_settings_set_bytes(settings, "Main", "Bytes",
+					(uint8_t []) { 10, 11, 0, 12, 13 }, 5));
+	bytes = l_settings_get_bytes(settings, "Main", "Bytes", &len);
+	assert(bytes);
+	assert(len == 5);
+	assert(!memcmp(bytes, (uint8_t []) { 10, 11, 0, 12, 13 }, 5));
+	l_free(bytes);
+	v = l_settings_get_value(settings, "Main", "Bytes");
+	assert(v);
+	assert(!strcmp(v, "0a0b000c0d"));
+
 	l_settings_free(settings);
 }
 
-- 
2.25.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH 1/2] settings: Add l_settings_{set,get}_bytes
  2020-09-16  1:11 [PATCH 1/2] settings: Add l_settings_{set,get}_bytes Andrew Zaborowski
  2020-09-16  1:11 ` [PATCH 2/2] unit: Test l_settings_{get,set}_bytes Andrew Zaborowski
@ 2020-09-16 21:32 ` Denis Kenzior
  1 sibling, 0 replies; 3+ messages in thread
From: Denis Kenzior @ 2020-09-16 21:32 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 414 bytes --]

Hi Andrew,

On 9/15/20 8:11 PM, Andrew Zaborowski wrote:
> ---
> I went for "bytes" instead of "hexstring" because the latter is the
> encoding while the former is the type of data stored.
> ---
>   ell/ell.sym    |  2 ++
>   ell/settings.c | 37 +++++++++++++++++++++++++++++++++++++
>   ell/settings.h |  7 +++++++
>   3 files changed, 46 insertions(+)
> 

Both applied, thanks.

Regards,
-Denis

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-09-16 21:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-16  1:11 [PATCH 1/2] settings: Add l_settings_{set,get}_bytes Andrew Zaborowski
2020-09-16  1:11 ` [PATCH 2/2] unit: Test l_settings_{get,set}_bytes Andrew Zaborowski
2020-09-16 21:32 ` [PATCH 1/2] settings: Add l_settings_{set,get}_bytes Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.