Browse Source

libdpkg: Refactor varbuf_add_source_version() out from virt_source_version()

Guillem Jover 11 years ago
parent
commit
7d93495f41
4 changed files with 40 additions and 17 deletions
  1. 4 0
      lib/dpkg/dpkg-db.h
  2. 1 0
      lib/dpkg/libdpkg.map
  3. 1 17
      lib/dpkg/pkg-format.c
  4. 34 0
      lib/dpkg/pkg-show.c

+ 4 - 0
lib/dpkg/dpkg-db.h

@@ -379,6 +379,10 @@ const char *pkgbin_name(struct pkginfo *pkg, struct pkgbin *pkgbin,
                         enum pkg_name_arch_when pnaw);
 const char *pkg_name(struct pkginfo *pkg, enum pkg_name_arch_when pnaw);
 
+void
+varbuf_add_source_version(struct varbuf *vb,
+                          const struct pkginfo *pkg, const struct pkgbin *pkgbin);
+
 const char *pkg_want_name(const struct pkginfo *pkg);
 const char *pkg_status_name(const struct pkginfo *pkg);
 const char *pkg_eflag_name(const struct pkginfo *pkg);

+ 1 - 0
lib/dpkg/libdpkg.map

@@ -262,6 +262,7 @@ LIBDPKG_PRIVATE {
 	pkg_sorter_by_nonambig_name_arch;
 	varbuf_add_pkgbin_name;
 	varbuf_add_archqual;
+	varbuf_add_source_version;
 	pkgbin_name;
 	pkg_name;
 	pkgbin_summary;

+ 1 - 17
lib/dpkg/pkg-format.c

@@ -303,23 +303,7 @@ virt_source_version(struct varbuf *vb,
                     const struct pkginfo *pkg, const struct pkgbin *pkgbin,
                     enum fwriteflags flags, const struct fieldinfo *fip)
 {
-	const char *version;
-	size_t len;
-
-	if (pkgbin->source)
-		version = strchr(pkgbin->source, '(');
-	else
-		version = NULL;
-
-	if (version == NULL) {
-		varbufversion(vb, &pkgbin->version, vdew_nonambig);
-	} else {
-		version++;
-
-		len = strcspn(version, ")");
-
-		varbuf_add_buf(vb, version, len);
-	}
+	varbuf_add_source_version(vb, pkg, pkgbin);
 }
 
 static const struct fieldinfo virtinfos[] = {

+ 34 - 0
lib/dpkg/pkg-show.c

@@ -289,3 +289,37 @@ pkg_sorter_by_nonambig_name_arch(const void *a, const void *b)
 		return -1;
 	}
 }
+
+/**
+ * Add a string representation of the source package version to a varbuf.
+ *
+ * It parses the Source field (if present), and extracts the optional
+ * version enclosed in parenthesis. Otherwise it fallsback to use the
+ * binary package version. It NUL terminates the varbuf.
+ *
+ * @param vb      The varbuf struct to modify.
+ * @param pkg     The package to consider.
+ * @param pkgbin  The binary package instance to consider.
+ */
+void
+varbuf_add_source_version(struct varbuf *vb,
+                          const struct pkginfo *pkg, const struct pkgbin *pkgbin)
+{
+	const char *version;
+	size_t len;
+
+	if (pkgbin->source)
+		version = strchr(pkgbin->source, '(');
+	else
+		version = NULL;
+
+	if (version == NULL) {
+		varbufversion(vb, &pkgbin->version, vdew_nonambig);
+	} else {
+		version++;
+
+		len = strcspn(version, ")");
+
+		varbuf_add_buf(vb, version, len);
+	}
+}