]> pd.if.org Git - zpackage/blobdiff - lib/parse.c
fix vercmp bugs
[zpackage] / lib / parse.c
index 5009c01bff3decc5964459457dcd795d3bb6aed8..2451e3f007b332a9b49926313fa75c31c0020f7a 100644 (file)
@@ -7,10 +7,81 @@
 
 #include "zpm.h"
 
-#include "sqlite3.h"
-
 #define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__)
 
+int zpm_parse_version(const char *pstr, struct zpm_version_info *info) {
+       int cur = 0, ch;
+
+       info->verstr = pstr;
+       info->name = 0;
+       info->namelen = 0;
+       info->version = 0;
+       info->verlen = 0;
+       info->release = -1;
+       info->rellen = 0;
+       info->relstr = 0;
+
+       if (!pstr) {
+               return 0;
+       }
+
+       /* skip leading whitespace */
+       while (isspace(pstr[cur])) {
+               cur++;
+       }
+
+       /* get the name, if any */
+       if (isalpha(pstr[cur])) {
+               info->name = pstr + cur;
+               for (ch = pstr[cur]; ch; ch = pstr[++cur]) {
+                       if (ch == '-') {
+                               if (isdigit(pstr[cur+1])) {
+                                       break;
+                               }
+                       }
+                       if (!isgraph(ch)) {
+                               break;
+                       }
+                       info->namelen++;
+               }
+       }
+
+       while (pstr[cur] == '-') {
+               cur++;
+       }
+
+       /* should be pointing at a digit.  if not, we're done */
+       if (isdigit(pstr[cur])) {
+               info->version = pstr + cur;
+               for (ch = pstr[cur]; ch; ch = pstr[++cur]) {
+                       if (ch == '-') {
+                               break;
+                       }
+                       if (!isgraph(ch)) {
+                               break;
+                       }
+                       info->verlen++;
+               }
+       }
+
+       while (pstr[cur] == '-') {
+               cur++;
+       }
+
+       if (isdigit(pstr[cur])) {
+               info->relstr = pstr + cur;
+               while (isdigit(pstr[cur++])) {
+                       info->rellen++;
+               }
+       }
+
+       if (info->relstr) {
+               info->release = atoi(info->relstr);
+       }
+
+       return info->namelen || info->verlen || info->rellen;
+}
+
 int zpm_parse_package(char *pstr, char *name, char *ver, int *rel) {
        if (name) *name = 0;
        if (ver) *ver = 0;