Skip to contentSkip to conversation

settings: Add l_settings_{set,get}_bytes

PATCH In review Andrew Zaborowski 2 participants 3 messages Sep 16, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 settings: Add l_settings_{set,get}_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(+)
ell/ell.sym +2 -0
index 044cf3c..66e0f08 100644
@@ -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;
ell/settings.c +37 -0
index dac45c9..68f7a4a 100644
@@ -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)
 {
ell/settings.h +7 -0
index e4203af..f95ed9b 100644
@@ -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,
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 unit: Test l_settings_{get,set}_bytes

---
 unit/test-settings.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
unit/test-settings.c +14 -0
index fa3a533..a25197a 100644
@@ -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);
 }
 
Signature · 1 line
2.25.1
#2 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

---
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