]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: Store groups in an array
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 12:36:10 +0000 (13:36 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 8 Jun 2019 12:36:10 +0000 (13:36 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/package.c
src/libpakfire/archive.c
src/libpakfire/include/pakfire/package.h
src/libpakfire/include/pakfire/util.h
src/libpakfire/libpakfire.sym
src/libpakfire/package.c
src/libpakfire/util.c

index 777ac27c22e10e13a05a215316ea1bd7a4de8771..3ecd1e5238ebc84efca187746af3314b5623311a 100644 (file)
@@ -289,9 +289,11 @@ static void Package_set_url(PackageObject* self, PyObject* value) {
 }
 
 static PyObject* Package_get_groups(PackageObject* self) {
-       char** groups = pakfire_package_get_groups(self->package);
+       const char* s = pakfire_package_get_groups(self->package);
 
        PyObject* list = PyList_New(0);
+
+       char** groups = pakfire_split_string(s, ',');
        char* group;
 
        while ((group = *groups++) != NULL) {
@@ -303,28 +305,15 @@ static PyObject* Package_get_groups(PackageObject* self) {
        }
 
        pakfire_free(groups);
-       Py_INCREF(list);
+
        return list;
 }
 
 static int Package_set_groups(PackageObject* self, PyObject* value) {
-       if (!PySequence_Check(value)) {
-               PyErr_SetString(PyExc_AttributeError, "Expected a sequence.");
-               return -1;
-       }
-
-       const int length = PySequence_Length(value);
-       const char* groups[length + 1];
-
-       for (int i = 0; i < length; i++) {
-               PyObject* item = PySequence_GetItem(value, i);
-               groups[i] = PyUnicode_AsUTF8(item);
-
-               Py_DECREF(item);
-       }
-       groups[length] = NULL;
+       const char* groups = PyUnicode_AsUTF8(value);
 
-       pakfire_package_set_groups(self->package, groups);
+       if (groups)
+               pakfire_package_set_groups(self->package, groups);
 
        return 0;
 }
index e66bf9bfe685db54eeaeb21fedc9c924b5e74db9..257dd2915015cf493ebdcdd8a796a2ce9fb09f5e 100644 (file)
@@ -1120,14 +1120,11 @@ PAKFIRE_EXPORT PakfirePackage pakfire_archive_make_package(PakfireArchive archiv
        }
 
        // Set groups
-#warning broken
-#if 0
        char* groups = pakfire_archive_get(archive, "package.groups");
        if (groups) {
                pakfire_package_set_groups(pkg, groups);
                pakfire_free(groups);
        }
-#endif
 
        // Set maintainer
        char* maintainer = pakfire_archive_get(archive, "package.maintainer");
index da7035309755c458c09b0984a855e46e88978e35..d4c01dd4020f5024319708a3e62dbe1f5f720daf 100644 (file)
@@ -64,9 +64,8 @@ const char* pakfire_package_get_license(PakfirePackage pkg);
 void pakfire_package_set_license(PakfirePackage pkg, const char* license);
 const char* pakfire_package_get_url(PakfirePackage pkg);
 void pakfire_package_set_url(PakfirePackage pkg, const char* url);
-char** pakfire_package_get_groups(PakfirePackage pkg);
-void pakfire_package_set_groups(PakfirePackage pkg, const char** grouplist);
-int pakfire_package_is_in_group(PakfirePackage pkg, const char* group);
+const char* pakfire_package_get_groups(PakfirePackage pkg);
+void pakfire_package_set_groups(PakfirePackage pkg, const char* groups);
 const char* pakfire_package_get_vendor(PakfirePackage pkg);
 void pakfire_package_set_vendor(PakfirePackage pkg, const char* vendor);
 const char* pakfire_package_get_maintainer(PakfirePackage pkg);
index c2e9faa418aa9448dc966e7ce8e2cf7f900e2677..c100d3af01259d0788fe16560ffc00f6b273e17f 100644 (file)
@@ -59,5 +59,6 @@ void init_libgcrypt();
 int pakfire_read_file_into_buffer(FILE* f, char** buffer, size_t* len);
 
 size_t pakfire_string_to_size(const char* s);
+char** pakfire_split_string(const char* s, char delim);
 
 #endif /* PAKFIRE_UTIL_H */
index 04ae7437cc0fed05031778f53d35ea9231faeb08..4ede202a1e3e71f0ea61172b604c9170e56812f6 100644 (file)
@@ -170,7 +170,6 @@ global:
        pakfire_package_get_version;
        pakfire_package_id;
        pakfire_package_is_installed;
-       pakfire_package_is_in_group;
        pakfire_package_join_evr;
        pakfire_package_ref;
        pakfire_package_set_arch;
@@ -355,6 +354,7 @@ global:
        pakfire_get_errno;
        pakfire_path_join;
        pakfire_read_file_into_buffer;
+       pakfire_split_string;
        pakfire_string_to_size;
 
 local:
index 3b3c8236ab7e9b897e4e5f2a7cce2adadd2fea9d..562c427863325bd26bcf79f0c1d35e9a8875f46d 100644 (file)
@@ -355,7 +355,16 @@ static void pakfire_package_set_string(PakfirePackage pkg, int key, const char*
        if (!value)
                value = "";
 
-       solvable_set_str(s, key, value);
+       solvable_set_poolstr(s, key, value);
+}
+
+static void pakfire_package_add_string_array(PakfirePackage pkg, int key, const char* value) {
+       Solvable* s = get_solvable(pkg);
+
+       if (!value)
+               return;
+
+       solvable_add_poolstr_array(s, key, value);
 }
 
 PAKFIRE_EXPORT const char* pakfire_package_get_uuid(PakfirePackage pkg) {
@@ -406,90 +415,17 @@ PAKFIRE_EXPORT void pakfire_package_set_url(PakfirePackage pkg, const char* url)
        pakfire_package_set_string(pkg, SOLVABLE_URL, url);
 }
 
-PAKFIRE_EXPORT char** pakfire_package_get_groups(PakfirePackage pkg) {
-       const char* groups = pakfire_package_get_string(pkg, SOLVABLE_GROUP);
-
-       // Return nothing when the string is empty
-       if (!groups)
-               return NULL;
-
-       // Copy string to stack and count spaces
-       char buffer[strlen(groups) + 2];
-
-       size_t count = 1;
-       for (unsigned int i = 0; i < strlen(groups) + 1; i++) {
-               buffer[i] = groups[i];
-
-               if (groups[i] == ' ') {
-                       buffer[i] = '\0';
-                       count++;
-               }
-       }
-
-       // Allocate an array of sufficient size
-       char** grouplist = pakfire_malloc(sizeof(*grouplist) * (count + 1));
-
-       // Copy strings to heap one by one
-       unsigned int i = 0;
-       char* p = buffer;
-       while (*p) {
-               grouplist[i++] = pakfire_strdup(p);
-
-               // Move pointer to the next string
-               p += strlen(p) + 1;
-       }
-
-       // Terminate array
-       grouplist[count] = NULL;
-
-       return grouplist;
+PAKFIRE_EXPORT const char* pakfire_package_get_groups(PakfirePackage pkg) {
+       return pakfire_package_get_string(pkg, SOLVABLE_GROUP);
 }
 
-static char* pakfire_package_make_group_string(const char** grouplist) {
-       char* s = NULL;
-
-       while (grouplist && *grouplist) {
-               if (s)
-                       asprintf(&s, "%s %s", s, *grouplist);
-               else
-                       asprintf(&s, "%s", *grouplist);
+PAKFIRE_EXPORT void pakfire_package_set_groups(PakfirePackage pkg, const char* groups) {
+       char** list = pakfire_split_string(groups, ' ');
 
-               // Move pointer forward
-               grouplist++;
+       while (list && *list) {
+               pakfire_package_add_string_array(pkg, SOLVABLE_GROUP, *list);
+               list++;
        }
-
-       return s;
-}
-
-PAKFIRE_EXPORT void pakfire_package_set_groups(PakfirePackage pkg, const char** grouplist) {
-       char* s = pakfire_package_make_group_string(grouplist);
-
-       pakfire_package_set_string(pkg, SOLVABLE_GROUP, s);
-       pakfire_free(s);
-}
-
-PAKFIRE_EXPORT int pakfire_package_is_in_group(PakfirePackage pkg, const char* group) {
-       char** grouplist = pakfire_package_get_groups(pkg);
-       if (!grouplist)
-               return -1;
-
-       int ret = 1;
-
-       // Walk through all groups
-       while (*grouplist) {
-               // Check if the group name matches
-               if (strcmp(*grouplist, group) == 0) {
-                       ret = 0;
-               }
-
-               // Free the element in the group list
-               pakfire_free(*grouplist);
-               grouplist++;
-       }
-
-       pakfire_free(grouplist);
-
-       return ret;
 }
 
 PAKFIRE_EXPORT const char* pakfire_package_get_vendor(PakfirePackage pkg) {
@@ -837,14 +773,9 @@ PAKFIRE_EXPORT char* pakfire_package_dump(PakfirePackage pkg, int flags) {
                pakfire_package_dump_add_lines(&string, _("Description"), description);
 
        // Groups
-       char** groups = pakfire_package_get_groups(pkg);
+       const char* groups = pakfire_package_get_groups(pkg);
        if (groups) {
-               char* s = pakfire_package_make_group_string((const char**)groups);
-
-               if (s) {
-                       pakfire_package_dump_add_lines(&string, _("Groups"), s);
-                       pakfire_free(s);
-               }
+               pakfire_package_dump_add_lines(&string, _("Groups"), groups);
        }
 
        // URL
index bfe36a11161b5a466002141be94f6a817c87535d..db665ff7c992f02833fa07c0897ec4574eb2eefa 100644 (file)
@@ -328,3 +328,36 @@ PAKFIRE_EXPORT size_t pakfire_string_to_size(const char* s) {
 
        return 0;
 }
+
+PAKFIRE_EXPORT char** pakfire_split_string(const char* s, char delim) {
+       // Copy string to stack and count spaces
+       char buffer[strlen(s) + 2];
+
+       size_t count = 1;
+       for (unsigned int i = 0; i < strlen(s) + 1; i++) {
+               buffer[i] = s[i];
+
+               if (s[i] == delim) {
+                       buffer[i] = '\0';
+                       count++;
+               }
+       }
+
+       // Allocate an array of sufficient size
+       char** ret = pakfire_malloc(sizeof(*ret) * (count + 1));
+
+       // Copy strings to heap one by one
+       unsigned int i = 0;
+       char* p = buffer;
+       while (*p) {
+               ret[i++] = pakfire_strdup(p);
+
+               // Move pointer to the next string
+               p += strlen(p) + 1;
+       }
+
+       // Terminate array
+       ret[count] = NULL;
+
+       return ret;
+}