Welcome to little lamb

Code » limb » commit b35a614

Add obuffer_parse_level()

author Olivier Brunel
2023-04-02 17:34:12 UTC
committer Olivier Brunel
2023-05-20 18:06:34 UTC
parent d54fb7dd32a95bdc03acaab9b1d4ad580d2f104d

Add obuffer_parse_level()

src/doc/obuffer.h.0.md +3 -0
src/doc/obuffer.h/obuffer_parse_level.3.md +35 -0
src/liblimb/include/limb/obuffer.h +2 -0
src/liblimb/obuffer.h/obuffer_parse_level.c +15 -0

diff --git a/src/doc/obuffer.h.0.md b/src/doc/obuffer.h.0.md
index 58ae323..4323dba 100644
--- a/src/doc/obuffer.h.0.md
+++ b/src/doc/obuffer.h.0.md
@@ -45,6 +45,9 @@ The following structures are defined :
 
 The following functions are defined :
 
+: [obuffer_parse_level](3)
+:: To parse a string representing an output level.
+
 : [obuffer_put](3)
 :: To write given data to an output buffer (if levels match)
 
diff --git a/src/doc/obuffer.h/obuffer_parse_level.3.md b/src/doc/obuffer.h/obuffer_parse_level.3.md
new file mode 100644
index 0000000..7582d5f
--- /dev/null
+++ b/src/doc/obuffer.h/obuffer_parse_level.3.md
@@ -0,0 +1,35 @@
+% limb manual
+% obuffer_parse_level(3)
+
+# NAME
+
+obuffer\_parse\_level - parse a string representing an output level
+
+# SYNOPSIS
+
+    #include <limb/obuffer.h>
+
+```pre hl
+u8 obuffer_parse_level(const char *<em>str</em>, size_t <em>len</em>)
+```
+
+# DESCRIPTION
+
+The `obuffer_parse_level`() function will scan the content of `str` up to `len`
+byte, looking for an output level.
+
+Recognized strings are, in order :
+
+- silent
+- quiet
+- normal
+- verbose
+- debug
+- maximum
+
+Matching can be partial, according to the rule of [byte_get_match](3).
+
+# RETURN VALUE
+
+The `obuffer_parse_level`() function returns the output level value on success.
+Otherwise it returns (u8) -1.
diff --git a/src/liblimb/include/limb/obuffer.h b/src/liblimb/include/limb/obuffer.h
index b2d2ef2..dd998f7 100644
--- a/src/liblimb/include/limb/obuffer.h
+++ b/src/liblimb/include/limb/obuffer.h
@@ -28,4 +28,6 @@ extern void obuffer_putmsg(obuffer *obuf, u8 level, const char * const *as, unsi
 extern void obuffers_putmsg(obuffer *obufs, unsigned int nbufs,
                             u8 level, const char * const *as, unsigned int n);
 
+extern u8 obuffer_parse_level(const char *str, size_t len);
+
 #endif /* LIMB_OBUFFER_H */
diff --git a/src/liblimb/obuffer.h/obuffer_parse_level.c b/src/liblimb/obuffer.h/obuffer_parse_level.c
new file mode 100644
index 0000000..077e89a
--- /dev/null
+++ b/src/liblimb/obuffer.h/obuffer_parse_level.c
@@ -0,0 +1,15 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <limb/bytestr.h>
+#include <limb/obuffer.h>
+
+u8
+obuffer_parse_level(const char *str, size_t len)
+{
+    const char *levels[] = { "silent", "quiet", "normal", "verbose", "debug", "maximum", 0 };
+    int m = byte_get_match(NULL, str, len, levels);
+    if (m < 0) return (u8) -1;
+    u8 lvl[] = { OLVL_SILENT, OLVL_QUIET, OLVL_NORMAL, OLVL_VERBOSE, OLVL_DEBUG, OLVL_MAXIMUM };
+    return lvl[m];
+}