]> git.ipfire.org Git - pakfire.git/commitdiff
util: Add a unified function to parse dependencies
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 30 Apr 2021 07:53:25 +0000 (07:53 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 30 Apr 2021 07:53:25 +0000 (07:53 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c
src/libpakfire/db.c
src/libpakfire/include/pakfire/util.h
src/libpakfire/parser.c
src/libpakfire/util.c

index 9825e864f51cc98b9944296e9831a6ffcfb1c108..f9e417e997bcdd9ece589da5d2e1983e161606ef 100644 (file)
@@ -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(
index 20a8602795342b7c446a9d9e338faa8fb8d808f9..7241301180cdd3fb7e71e11881eead63d233cba2 100644 (file)
@@ -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);
                }
        }
 
index e8e32fdd7e3e6a5a7a47f2d32c590f401c287700..bed30a62202ccd96da38b0977cdae9da44a9a700 100644 (file)
@@ -33,6 +33,8 @@
 #include <pakfire/types.h>
 
 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)
index a79413a71080b7009a9eb86f8c39b4b7d1a3920e..b22e32d03b5fd891281ecd66543593486b95bf4d 100644 (file)
@@ -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
index 0032cdd5fd7a9a6bac7ea9e9783bd46ddab44c8a..9956fc867a1b31cae70cd260061654848a0453bc 100644 (file)
@@ -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));
 }