summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>2025-04-10 19:39:26 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2025-04-10 19:49:19 +0300
commitf1c9c2bdf74370fd75caf108faebfc13d108b3d5 (patch)
tree1b0b011fc663bdd6d7269d0567e46146f6e070bb
parent3e445c7855f8240d1f8473d127307dd967be2d25 (diff)
Use key_value to handle V4L2 field names
Extend the key_value helper with a function to retrieve the key corresponding to a value, and use it to replace the manual implementation of V4L2 field name lookup. The name comparison needs to be made case-insensitive to avoid breaking users, as yavta handles V4L2 field names in a case-insensitive way. Suggested-by: Sakari Ailus <sakari.ailus@linux.intel.com> Signed-off-by: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com> Reviewed-by: Sakari Ailus <sakari.ailus@linux.intel.com>
-rw-r--r--yavta.c50
1 files changed, 23 insertions, 27 deletions
diff --git a/yavta.c b/yavta.c
index b463f58..49f10c3 100644
--- a/yavta.c
+++ b/yavta.c
@@ -199,7 +199,7 @@ static int __key_value_get(const struct key_value *values,
unsigned int i;
for (i = 0; i < count; ++i) {
- if (!strcmp(values[i].name, name))
+ if (!strcasecmp(values[i].name, name))
return values[i].value;
}
@@ -235,12 +235,29 @@ static void __key_value_list(const struct key_value *values,
printf("\n");
}
+static const char *__key_value_name(const struct key_value *values,
+ unsigned int count, unsigned int value,
+ const char *def_value)
+{
+ unsigned int i;
+
+ for (i = 0; i < count; ++i) {
+ if (values[i].value == value)
+ return values[i].name;
+ }
+
+ return def_value;
+}
+
#define key_value_get(values, name) \
__key_value_get(values, ARRAY_SIZE(values), name)
#define key_value_list(values, type) \
__key_value_list(values, ARRAY_SIZE(values), type)
+#define key_value_name(values, value, def_value) \
+ __key_value_name(values, ARRAY_SIZE(values), value, def_value)
+
/* -----------------------------------------------------------------------------
* Format handling
*/
@@ -466,10 +483,7 @@ static const char *v4l2_format_name(unsigned int fourcc)
return name;
}
-static const struct {
- const char *name;
- enum v4l2_field field;
-} fields[] = {
+static const struct key_value v4l2_fields[] = {
{ "any", V4L2_FIELD_ANY },
{ "none", V4L2_FIELD_NONE },
{ "top", V4L2_FIELD_TOP },
@@ -482,29 +496,11 @@ static const struct {
{ "interlaced-bt", V4L2_FIELD_INTERLACED_BT },
};
-static enum v4l2_field v4l2_field_from_string(const char *name)
-{
- unsigned int i;
-
- for (i = 0; i < ARRAY_SIZE(fields); ++i) {
- if (strcasecmp(fields[i].name, name) == 0)
- return fields[i].field;
- }
-
- return -1;
-}
-
-static const char *v4l2_field_name(enum v4l2_field field)
-{
- unsigned int i;
-
- for (i = 0; i < ARRAY_SIZE(fields); ++i) {
- if (fields[i].field == field)
- return fields[i].name;
- }
+#define v4l2_field_from_string(name) \
+ key_value_get(v4l2_fields, name)
- return "unknown";
-}
+#define v4l2_field_name(field) \
+ key_value_name(v4l2_fields, field, "unknown")
static const struct key_value v4l2_colorspaces[] = {
{ "DEFAULT", V4L2_COLORSPACE_DEFAULT },