10 int zpm_parse_package(char *pstr, char *name, char *ver, int *rel) {
18 /* string - ver - rel */
19 /* rel is all digits */
21 * ^(.+)-([0-9][^-]*)-([\d+])$
24 * The main problem in parsing is that the package name itself
25 * can contain a '-', so you can't just split on '-'
26 * Also, the version can be just digits.
29 /* everything up to the first '-' is in the name */
31 if (*pstr == '-' && isdigit(*(pstr+1))) {
44 while (*pstr && *pstr != '-') {
61 return havename + havever + haverel;
65 * flags 0 = find installed
66 * flags 1 = skip installed
67 * flags 2 = skip not installed
70 struct zpm_package *zpm_package_alloc(struct zpm *zpm) {
71 struct zpm_package *pkg;
73 pkg = malloc(sizeof *pkg);
81 void zpm_sqlite_error(struct zpm *zpm) {
83 if (zpm->errmsg) free(zpm->errmsg);
84 zpm->errmsg = zpm_strdup((char *)sqlite3_errmsg(zpm->db));
87 char *zpm_findpkg(struct zpm *zpm, char *pkgstr) {
88 char *select = "select pkgid, package, version, release from packages_pkgid";
89 char *group = "group by package having max( version||'-'||release collate vercmp) order by length(package), package, version||'-'||release collate vercmp limit 1";
92 "status = 'installed'",
94 "status != 'installed'"
98 // char *order = "order by package, version collate vercmp, cast(release as integer)";
106 /* null pkgstr find "best" package
107 * best is shortest complete package if any are complete
108 * shortest incomplete if any are incomplete
109 * where more than one version is in those sets, best is
110 * latest as determined by vercmp
113 /* given a package name, get the packages */
114 /* no package name, get all */
116 // char where[1024] = "";
118 zpm_parse_package(pkgstr, package, version, &release);
120 /* install a collation function */
123 /* TODO allow more args to nail down version and release */
125 sql = sqlite3_str_new(zpm->db);
126 sqlite3_str_appendall(sql, select);
127 sqlite3_str_appendf(sql, " where package = %q", package);
129 sqlite3_str_appendf(sql, " and version = %q", version);
132 sqlite3_str_appendf(sql, " and release = %d", release);
134 sqlite3_str_appendf(sql, " %s", group);
135 sqlite3_str_appendf(sql, " limit 1");
137 if (sqlite3_str_errcode(sql)) {
139 sqlite3_free(sqlite3_str_finish(sql));
143 query = sqlite3_str_finish(sql);
144 sqlite3_prepare_v2(zpm->db, query, strlen(query), &stmt, NULL);
150 switch (sqlite3_step(stmt)) {
152 zpm->pkgid = zpm_strdup((char *)sqlite3_column_text(stmt, 0));
158 zpm_sqlite_error(zpm);
161 sqlite3_finalize(stmt);