]> pd.if.org Git - zpackage/blob - lib/parse.c
5009c01bff3decc5964459457dcd795d3bb6aed8
[zpackage] / lib / parse.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <ctype.h>
7
8 #include "zpm.h"
9
10 #include "sqlite3.h"
11
12 #define DMARK fprintf(stderr, "mark %s %s:%d\n", __FILE__, __func__, __LINE__)
13
14 int zpm_parse_package(char *pstr, char *name, char *ver, int *rel) {
15         if (name) *name = 0;
16         if (ver) *ver = 0;
17         if (rel) *rel = 0;
18         int havename = 0;
19         int havever = 0;
20         int haverel = 0;
21
22         /* string - ver - rel */
23         /* rel is all digits */
24         /* possible forms:
25          * ^(.+)-([0-9][^-]*)-([\d+])$
26          * ^(.+)-([0-9][^-]*)$
27          * ^(.+)$
28          * The main problem in parsing is that the package name itself
29          * can contain a '-', so you can't just split on '-'
30          * Also, the version can be just digits.
31          */
32
33         /* everything up to the first '-' is in the name */
34         while (*pstr) {
35                 if (*pstr == '\'' || !isgraph(*pstr)) {
36                         return 0;
37                 }
38                 if (*pstr == '-' && isdigit(*(pstr+1))) {
39                         break;
40                 }
41                 if (name) {
42                         *name++ = *pstr;
43                 }
44                 pstr++;
45                 havename = 1;
46         }
47         if (name) *name = 0;
48         if (*pstr == '-') {
49                 pstr++;
50         }
51         while (*pstr && *pstr != '-') {
52                 if (*pstr == '\'' || !isgraph(*pstr)) {
53                         return 0;
54                 }
55                 if (ver) {
56                         *ver++ = *pstr;
57                 }
58                 pstr++;
59                 havever = 1;
60         }
61         if (ver) *ver = 0;
62         if (*pstr == '-') {
63                 pstr++;
64         }
65         /* TODO use strtol */
66         if (rel && *pstr) {
67                 haverel = 1;
68                 *rel = atoi(pstr);
69         }
70
71         return havename + havever + haverel;
72 }