]> pd.if.org Git - zpackage/blobdiff - lib/parse.c
separate parse from findpkg
[zpackage] / lib / parse.c
diff --git a/lib/parse.c b/lib/parse.c
new file mode 100644 (file)
index 0000000..5009c01
--- /dev/null
@@ -0,0 +1,72 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+
+#include "zpm.h"
+
+#include "sqlite3.h"
+
+#define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__)
+
+int zpm_parse_package(char *pstr, char *name, char *ver, int *rel) {
+       if (name) *name = 0;
+       if (ver) *ver = 0;
+       if (rel) *rel = 0;
+       int havename = 0;
+       int havever = 0;
+       int haverel = 0;
+
+       /* string - ver - rel */
+       /* rel is all digits */
+       /* possible forms:
+        * ^(.+)-([0-9][^-]*)-([\d+])$
+        * ^(.+)-([0-9][^-]*)$
+        * ^(.+)$
+        * The main problem in parsing is that the package name itself
+        * can contain a '-', so you can't just split on '-'
+        * Also, the version can be just digits.
+        */
+
+       /* everything up to the first '-' is in the name */
+       while (*pstr) {
+               if (*pstr == '\'' || !isgraph(*pstr)) {
+                       return 0;
+               }
+               if (*pstr == '-' && isdigit(*(pstr+1))) {
+                       break;
+               }
+               if (name) {
+                       *name++ = *pstr;
+               }
+               pstr++;
+               havename = 1;
+       }
+       if (name) *name = 0;
+       if (*pstr == '-') {
+               pstr++;
+       }
+       while (*pstr && *pstr != '-') {
+               if (*pstr == '\'' || !isgraph(*pstr)) {
+                       return 0;
+               }
+               if (ver) {
+                       *ver++ = *pstr;
+               }
+               pstr++;
+               havever = 1;
+       }
+       if (ver) *ver = 0;
+       if (*pstr == '-') {
+               pstr++;
+       }
+       /* TODO use strtol */
+       if (rel && *pstr) {
+               haverel = 1;
+               *rel = atoi(pstr);
+       }
+
+       return havename + havever + haverel;
+}