summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-10-02 22:34:26 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-10-02 22:34:26 -0700
commit696f28e5296c3b39eac56eed4d20560bf06cba38 (patch)
tree4896b365f8f884b75a88a19433d2066d3f919879
parentde4e95a888cdc1ed490caceb984407e679c0965f (diff)
til: introduce til_value_to_pos() helper
This copies from checkers_value_to_pos() turning it into a libtil helper. It's a common operation in setup_funcs to map a setting value to a numeric position in the list of values.
-rw-r--r--src/til.c21
-rw-r--r--src/til.h1
2 files changed, 22 insertions, 0 deletions
diff --git a/src/til.c b/src/til.c
index 8fa3b48..2ebc0a8 100644
--- a/src/til.c
+++ b/src/til.c
@@ -669,3 +669,24 @@ int til_fragmenter_tile64(til_module_context_t *context, const til_fb_fragment_t
{
return til_fb_fragment_tile_single(fragment, 64, number, res_fragment);
}
+
+
+/* Helper for mapping a value to a position in the supplied NULL-terminated lsit of options.
+ * Primarily useful for modules when mapping a settings value to an entry in a values array.
+ * Returns 0 on success w/position @res_pos, -ENOENT when not found.
+ */
+int til_value_to_pos(const char **options, const char *value, unsigned *res_pos)
+{
+ assert(options);
+ assert(value);
+ assert(res_pos);
+
+ for (unsigned i = 0; options[i]; i++) {
+ if (!strcasecmp(value, options[i])) {
+ *res_pos = i;
+ return 0;
+ }
+ }
+
+ return -ENOENT;
+}
diff --git a/src/til.h b/src/til.h
index 9fb8b5b..9049a0b 100644
--- a/src/til.h
+++ b/src/til.h
@@ -58,5 +58,6 @@ int til_fragmenter_noop_per_cpu(til_module_context_t *context, const til_fb_frag
int til_fragmenter_slice_per_cpu(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
int til_fragmenter_slice_per_cpu_x16(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
int til_fragmenter_tile64(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
+int til_value_to_pos(const char **options, const char *value, unsigned *res_pos);
#endif
© All Rights Reserved