Преглед изворни кода

libdpkg: Add support for fallback namevalues

This will allow to designate a namevalue entry as the fallback return
value in case none of the rest did match.
Guillem Jover пре 15 година
родитељ
комит
902cc15dc8
2 измењених фајлова са 12 додато и 5 уклоњено
  1. 9 4
      lib/dpkg/namevalue.c
  2. 3 1
      lib/dpkg/namevalue.h

+ 9 - 4
lib/dpkg/namevalue.c

@@ -2,7 +2,7 @@
  * libdpkg - Debian packaging suite library routines
  * namevalue.c - name value structure handling
  *
- * Copyright © 2010 Guillem Jover <guillem@debian.org>
+ * Copyright © 2010-2011 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,11 +28,16 @@
 const struct namevalue *
 namevalue_find_by_name(const struct namevalue *head, const char *str)
 {
-	const struct namevalue *nv;
+	const struct namevalue *nv, *nv_fallback = NULL;
 
-	for (nv = head; nv->name; nv++)
+	for (nv = head; nv->name; nv++) {
+		if (nv->length == 0) {
+			nv_fallback = nv;
+			continue;
+		}
 		if (strncasecmp(str, nv->name, nv->length) == 0)
 			return nv;
+	}
 
-	return NULL;
+	return nv_fallback;
 }

+ 3 - 1
lib/dpkg/namevalue.h

@@ -3,7 +3,7 @@
  * namevalue.h - name value structure handling
  *
  * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
- * Copyright © 2009-2010 Guillem Jover <guillem@debian.org>
+ * Copyright © 2009-2011 Guillem Jover <guillem@debian.org>
  *
  * This is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -34,6 +34,8 @@ struct namevalue {
 
 #define NAMEVALUE_DEF(n, v) \
 	[v] = { .name = n, .value = v, .length = sizeof(n) - 1 }
+#define NAMEVALUE_FALLBACK_DEF(n, v) \
+	[v] = { .name = n, .value = v, .length = 0 }
 
 const struct namevalue *namevalue_find_by_name(const struct namevalue *head,
                                                const char *str);