Welcome to little lamb

Code » limb » commit 5c80a28

Add list_matches{_full,}() helpers

author Olivier Brunel
2023-04-05 08:36:49 UTC
committer Olivier Brunel
2023-05-20 18:06:34 UTC
parent b35a614bed08d77d96e3414c9252ff7dcfefe374

Add list_matches{_full,}() helpers

To easily print list of all possible matches from partial match after
byte_get_match{_full,}()

src/doc/bytestr.h/byte_get_match_full.3.md +4 -0
src/doc/output.h.0.md +6 -0
src/doc/output.h/list_matches_full.3.md +46 -0
src/liblimb/include/limb/output.h +12 -2
src/liblimb/output.h/list_matches.c +13 -0
src/liblimb/output.h/list_matches_full.c +20 -0

diff --git a/src/doc/bytestr.h/byte_get_match_full.3.md b/src/doc/bytestr.h/byte_get_match_full.3.md
index c48c0b3..96f0e55 100644
--- a/src/doc/bytestr.h/byte_get_match_full.3.md
+++ b/src/doc/bytestr.h/byte_get_match_full.3.md
@@ -72,3 +72,7 @@ struct user {
 int r = byte_get_match_full(&first, name, strlen(name), users,
                             offsetof(struct user, name), sizeof(*users));
 ```
+
+# SEE ALSO
+
+[list_matches_full](3)
diff --git a/src/doc/output.h.0.md b/src/doc/output.h.0.md
index 5afc8ce..c19c675 100644
--- a/src/doc/output.h.0.md
+++ b/src/doc/output.h.0.md
@@ -75,6 +75,12 @@ The following functions/macros are defined :
 : [errverb](3)
 :: Same as [err](3) but with *OLVL_VERBOSE*
 
+: [list_matches](3)
+:: To list all matching element from a partial match in an array of strings.
+
+: [list_matches_full](3)
+:: To list all matching element from a partial match in an array.
+
 : [out](3)
 :: Write given strings to *stdout* buffers with level *OLVL_NORMAL* or more,
 :: adding a LF and flushing the buffer afterwards
diff --git a/src/doc/output.h/list_matches_full.3.md b/src/doc/output.h/list_matches_full.3.md
new file mode 100644
index 0000000..7a5505e
--- /dev/null
+++ b/src/doc/output.h/list_matches_full.3.md
@@ -0,0 +1,46 @@
+% limb manual
+% list_matches_full(3)
+
+# NAME
+
+list\_matches\_full, list\_matches - list all matching element from a partial
+match in an array
+
+# SYNOPSIS
+
+    #include <limb/output.h>
+
+```pre hl
+typedef void (*obuffer_putmsgfn) (u8 level, const char * const *as, unsigned int n)
+
+void list_matches_full(obuffer_putmsgfn <em>putmsg</em>, u8 <em>level</em>, const char *<em>intro</em>,
+                       const char *<em>prefix</em>, const char *<em>sep</em>, const char *<em>outro</em>,
+                       const char *<em>str</em>, size_t <em>slen</em>, int <em>first</em>,
+                       const void *<em>list</em>, size_t <em>offset</em>, size_t <em>llen</em>)
+void list_matches(obuffer_putmsgfn <em>putmsg</em>, u8 <em>level</em>, const char *<em>intro</em>,
+                  const char *<em>prefix</em>, const char *<em>sep</em>, const char *<em>outro</em>,
+                  const char *<em>str</em>, size_t <em>slen</em>, int <em>first</em>, const char **<em>list</em>)
+```
+
+# DESCRIPTION
+
+The `list_matches_full`() function is aimed as a complement when using
+[byte_get_match_full](3), in order to list all possible matches to the user.
+
+It will use the function pointed by `putmsg` to write all possible matches to
+`str` of length `slen` found inside the array `list` using a level of `level`.
+
+First `intro` is written, then for each matching element first `prefix` then
+the element itself. `sep` is written in between each elements, and finally
+`outro` is written out with a newline, and the buffer is flushed.
+
+Each element of the array `list` must by `llen` bytes long, and must contain,
+at byte `offset` (starting from 0), a pointer to a NUL-terminated string to
+check against.
+
+The argument `first` must be the index of the first-matching element, as
+set by [byte_get_match_full](3). At least one other element must be a match.
+
+
+The `list_matches`() function is similar, but simply takes `list` as a NULL
+terminated array of NUL-terminated string pointers.
diff --git a/src/liblimb/include/limb/output.h b/src/liblimb/include/limb/output.h
index c5e8194..fea3e1d 100644
--- a/src/liblimb/include/limb/output.h
+++ b/src/liblimb/include/limb/output.h
@@ -4,12 +4,22 @@
 #ifndef LIMB_OUTPUT_H
 #define LIMB_OUTPUT_H
 
+#include <stddef.h> /* size_t */
 #include <limb/obuffers.h>
 
+typedef void (*obuffer_putmsgfn) (u8 level, const char * const *as, unsigned int n);
+extern void list_matches_full(obuffer_putmsgfn putmsg, u8 level, const char *intro,
+                              const char *prefix, const char *sep, const char *outro,
+                              const char *str, size_t slen, int first,
+                              const void *list_, size_t offset, size_t llen);
+extern void list_matches(obuffer_putmsgfn putmsg, u8 level, const char *intro,
+                         const char *prefix, const char *sep, const char *outro,
+                         const char *str, size_t slen, int first, const char **list);
+
 extern const char *PROG;
 
-#define outarray(...)       ((char const * const []) {__VA_ARGS__})
-#define outalen(...)        (sizeof(outarray(__VA_ARGS__)) / sizeof(char const *))
+#define outarray(...)       ((const char * const []) {__VA_ARGS__})
+#define outalen(...)        (sizeof(outarray(__VA_ARGS__)) / sizeof(char *))
 
 #define outmsg(l,...)       out_putmsg(l, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
 #define errmsg(l,...)       err_putmsg(l, outarray(__VA_ARGS__), outalen(__VA_ARGS__))
diff --git a/src/liblimb/output.h/list_matches.c b/src/liblimb/output.h/list_matches.c
new file mode 100644
index 0000000..430643b
--- /dev/null
+++ b/src/liblimb/output.h/list_matches.c
@@ -0,0 +1,13 @@
+/* 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/output.h>
+
+void
+list_matches(obuffer_putmsgfn putmsg, u8 level, const char *intro,
+             const char *prefix, const char *sep, const char *outro,
+             const char *str, size_t slen, int first, const char **list)
+{
+    list_matches_full(putmsg, level, intro, prefix, sep, outro,
+                      str, slen, first, list, 0, sizeof(*list));
+}
diff --git a/src/liblimb/output.h/list_matches_full.c b/src/liblimb/output.h/list_matches_full.c
new file mode 100644
index 0000000..2d8434f
--- /dev/null
+++ b/src/liblimb/output.h/list_matches_full.c
@@ -0,0 +1,20 @@
+/* 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/output.h>
+
+void
+list_matches_full(obuffer_putmsgfn putmsg, u8 level, const char *intro,
+                  const char *prefix, const char *sep, const char *outro,
+                  const char *str, size_t slen, int first,
+                  const void *list_, size_t offset, size_t llen)
+{
+    const char *list = (const char *) list_ + first * llen;
+    const char *el = * (const char **) (list + offset);
+    putmsg(level, outarray(intro, prefix, el), 3);
+
+    for (list += llen; (el = * (const char **) (list + offset)); list += llen)
+        if (!strncmp(str, el, slen))
+            putmsg(level, outarray(sep, prefix, el), 3);
+    putmsg(level, outarray(outro, PUTMSG_LFF), 2);
+}