From: Michael Tremer Date: Sat, 8 Jun 2019 12:36:10 +0000 (+0100) Subject: libpakfire: Store groups in an array X-Git-Tag: 0.9.28~1285^2~974 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ccc9d74cf760a756d7321734469a4036f074a3a;p=pakfire.git libpakfire: Store groups in an array Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/package.c b/src/_pakfire/package.c index 777ac27c2..3ecd1e523 100644 --- a/src/_pakfire/package.c +++ b/src/_pakfire/package.c @@ -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; } diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index e66bf9bfe..257dd2915 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -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"); diff --git a/src/libpakfire/include/pakfire/package.h b/src/libpakfire/include/pakfire/package.h index da7035309..d4c01dd40 100644 --- a/src/libpakfire/include/pakfire/package.h +++ b/src/libpakfire/include/pakfire/package.h @@ -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); diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index c2e9faa41..c100d3af0 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -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 */ diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 04ae7437c..4ede202a1 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -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: diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index 3b3c8236a..562c42786 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -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 diff --git a/src/libpakfire/util.c b/src/libpakfire/util.c index bfe36a111..db665ff7c 100644 --- a/src/libpakfire/util.c +++ b/src/libpakfire/util.c @@ -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; +}