Cleanup version string handling.
authorgraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Thu, 12 Nov 2009 05:02:35 +0000 (05:02 +0000)
committergraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Thu, 12 Nov 2009 05:02:35 +0000 (05:02 +0000)
git-svn-id: http://opkg.googlecode.com/svn/trunk@286 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/pkg.c
libopkg/pkg_parse.c

index b1f45dd274eea3bcc5658f36d3034d88bccf2573..bb3da1e4eaeb36f647f66717de57b53f27ff1b92 100644 (file)
@@ -945,32 +945,27 @@ int abstract_pkg_name_compare(const void *p1, const void *p2)
 }
 
 
-char *pkg_version_str_alloc(pkg_t *pkg)
+char *
+pkg_version_str_alloc(pkg_t *pkg)
 {
-     char *complete_version;
-     char *epoch_str;
-     char *revision_str;
-
-     if (pkg->epoch) {
-         sprintf_alloc(&epoch_str, "%d:", pkg->epoch);
-     } else {
-         epoch_str = xstrdup("");
-     }
-
-     if (pkg->revision && strlen(pkg->revision)) {
-         sprintf_alloc(&revision_str, "-%s", pkg->revision);
-     } else {
-         revision_str = xstrdup("");
-     }
-
-
-     sprintf_alloc(&complete_version, "%s%s%s",
-                  epoch_str, pkg->version, revision_str);
-
-     free(epoch_str);
-     free(revision_str);
+       char *version;
+
+       if (pkg->epoch) {
+               if (pkg->revision)
+                       sprintf_alloc(&version, "%d:%s-%s",
+                               pkg->epoch, pkg->version, pkg->revision);
+               else
+                       sprintf_alloc(&version, "%d:%s",
+                               pkg->epoch, pkg->version);
+       } else {
+               if (pkg->revision)
+                       sprintf_alloc(&version, "%s-%s",
+                               pkg->version, pkg->revision);
+               else
+                       version = xstrdup(pkg->version);
+       }
 
-     return complete_version;
+       return version;
 }
 
 str_list_t *pkg_get_installed_files(pkg_t *pkg)
index 44338f03d5bd84ffc3f096037db97c87b0fdb725..a8aa231e5312d2cf5f03dd235a42bc1b89c452e0 100644 (file)
@@ -103,57 +103,37 @@ static void parseConffiles(pkg_t * pkg, const char * raw)
     }
 }    
 
-int parseVersion(pkg_t *pkg, const char *raw)
+int
+parseVersion(pkg_t *pkg, const char *vstr)
 {
-  char *colon, *eepochcolon;
-  char *hyphen;
-  unsigned long epoch;
-
-  if (!*raw) {
-      fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
-      return EINVAL;
-  }
-
-  if (strncmp(raw, "Version:", 8) == 0) {
-      raw += 8;
-  }
-  while (*raw && isspace(*raw)) {
-      raw++;
-  }
-  
-  colon= strchr(raw,':');
-  if (colon) {
-    epoch= strtoul(raw,&eepochcolon,10);
-    if (colon != eepochcolon) {
-       fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
-       return EINVAL;
-    }
-    if (!*++colon) {
-       fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
-       return EINVAL;
-    }
-    raw= colon;
-    pkg->epoch= epoch;
-  } else {
-    pkg->epoch= 0;
-  }
+       char *colon;
 
-  pkg->revision = "";
+       if (strncmp(vstr, "Version:", 8) == 0)
+               vstr += 8;
 
-  if (!pkg->version)
-  {
-    pkg->version= xcalloc(1, strlen(raw)+1);
-    strcpy(pkg->version, raw);
-  }
+       while (*vstr && isspace(*vstr))
+               vstr++;
 
-  hyphen= strrchr(pkg->version,'-');
+       colon = strchr(vstr, ':');
+       if (colon) {
+               errno = 0;
+               pkg->epoch = strtoul(vstr, NULL, 10);
+               if (errno) {
+                       fprintf(stderr, "%s: %s: invalid epoch: %s\n",
+                               __FUNCTION__, pkg->name, strerror(errno));
+               }
+               vstr = ++colon;
+       } else {
+               pkg->epoch= 0;
+       }
+
+       pkg->version= xstrdup(vstr);
+       pkg->revision = strrchr(pkg->version,'-');
 
-  if (hyphen) {
-    *hyphen++= 0;
-      pkg->revision = hyphen;
-  }
+       if (pkg->revision)
+               *pkg->revision++ = '\0';
 
-  return 0;
+       return 0;
 }
 
 static int