From: Michael Tremer Date: Fri, 30 Apr 2021 07:53:25 +0000 (+0000) Subject: util: Add a unified function to parse dependencies X-Git-Tag: 0.9.28~1285^2~182 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b81a64ec13321aa7b67b4e8dcef7993c6b71212c;p=pakfire.git util: Add a unified function to parse dependencies Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index 9825e864f..f9e417e99 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -1520,27 +1520,8 @@ PAKFIRE_EXPORT PakfirePackage pakfire_archive_make_package(PakfireArchive archiv for (const struct __relation* relation = relations; relation->type; relation++) { char* relations = pakfire_archive_get(archive, "dependencies", relation->type); - if (!relations) - continue; - - char* p = relations; - while (*p) { - char* e = strchr(p, '\n'); - - // Terminate the string - if (e) - *e = '\0'; - - // Add the dependency - relation->func(pkg, p); - - // End loop when we reached the end - if (!e) - break; - - // Or continue at the next line - p = e + 1; - } + if (relations) + pakfire_parse_deps(archive->pakfire, pkg, relation->func, relations); } // Import filelist @@ -1548,9 +1529,6 @@ PAKFIRE_EXPORT PakfirePackage pakfire_archive_make_package(PakfireArchive archiv pakfire_package_set_filelist(pkg, archive->filelist); return pkg; - -ERROR: - return NULL; } struct pakfire_scriptlet* pakfire_archive_get_scriptlet( diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index 20a860279..724130118 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -1492,30 +1492,7 @@ static int pakfire_db_load_package(struct pakfire_db* db, PakfireRepo repo, sqli for (const struct dependency* deps = dependencies; deps->field; deps++) { const char* relations = (const char*)sqlite3_column_text(stmt, deps->field); if (relations) { - // Copy list - char* p = strdupa(relations); - if (!p) { - r = 1; - goto ERROR; - } - - while (*p) { - char* e = strchr(p, '\n'); - - // Terminate the string - if (e) - *e = '\0'; - - // Add the dependency - deps->func(pkg, p); - - // End loop when we reached the end - if (!e) - break; - - // Or continue at the next line - p = e + 1; - } + pakfire_parse_deps(db->pakfire, pkg, deps->func, relations); } } diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index e8e32fdd7..bed30a622 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -33,6 +33,8 @@ #include Id pakfire_parse_dep(Pakfire pakfire, const char* s); +void pakfire_parse_deps(Pakfire pakfire, PakfirePackage pkg, + void (*func)(PakfirePackage pkg, const char* dep), const char* deps); #define pakfire_string_format(s, fmt, ...) snprintf(s, sizeof(s) - 1, fmt, __VA_ARGS__) #define pakfire_string_set(s, value) pakfire_string_format(s, "%s", value) diff --git a/src/libpakfire/parser.c b/src/libpakfire/parser.c index a79413a71..b22e32d03 100644 --- a/src/libpakfire/parser.c +++ b/src/libpakfire/parser.c @@ -846,31 +846,12 @@ PAKFIRE_EXPORT int pakfire_parser_create_package(PakfireParser parser, free(value); } + // Fetch build dependencies if (is_source) { - // Fetch build dependencies char* requires = pakfire_parser_get(parser, "build", "requires"); - - if (requires && *requires) { - char* p = requires; - - while (*p) { - char* e = strchr(p, '\n'); - - // Terminate the string - if (e) - *e = '\0'; - - // Add the dependency - pakfire_package_add_requires(*pkg, p); - - // End loop when we reached the end - if (!e) - break; - - // Or continue at the next line - p = e + 1; - } - } + if (requires && *requires) + pakfire_parse_deps(parser->pakfire, *pkg, + pakfire_package_add_requires, requires); } // All okay diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index 0032cdd5f..9956fc867 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -128,6 +128,29 @@ Id pakfire_parse_dep(Pakfire pakfire, const char* s) { return id; } +void pakfire_parse_deps(Pakfire pakfire, PakfirePackage pkg, + void (*func)(PakfirePackage pkg, const char* dep), const char* deps) { + char* p = strdupa(deps); + + while (*p) { + char* e = strchr(p, '\n'); + + // Terminate the string + if (e) + *e = '\0'; + + // Add the dependency + func(pkg, p); + + // End loop when we reached the end + if (!e) + break; + + // Or continue at the next line + p = e + 1; + } +} + int pakfire_string_startswith(const char* s, const char* prefix) { return !strncmp(s, prefix, strlen(prefix)); }