diff mbox series

Use key_value to handle V4L2 field names

Message ID 20250410164251.15352-1-laurent.pinchart@ideasonboard.com
State New
Headers show
Series Use key_value to handle V4L2 field names | expand

Commit Message

Laurent Pinchart April 10, 2025, 4:42 p.m. UTC
From: Laurent Pinchart <laurent.pinchart+renesas@ideasonboard.com>

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>
---
 yavta.c | 50 +++++++++++++++++++++++---------------------------
 1 file changed, 23 insertions(+), 27 deletions(-)


base-commit: 3e445c7855f8240d1f8473d127307dd967be2d25
diff mbox series

Patch

diff --git a/yavta.c b/yavta.c
index b463f5841100..49f10c371aa6 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;
+#define v4l2_field_from_string(name) \
+	key_value_get(v4l2_fields, name)
 
-	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;
-	}
-
-	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 },