summaryrefslogtreecommitdiff
path: root/src/package.cpp
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2023-04-01 14:16:44 +0200
committerEugen Wissner <belka@caraus.de>2023-04-01 14:17:08 +0200
commitf46a16b4a0d50b6512df2b312f7f800a9a963ca2 (patch)
tree8b385dc31c90065ba8e5a970dec9c931d68b7f43 /src/package.cpp
parent0385dbbe53cbeb89b541fa6ae659540a261bc69b (diff)
downloadslackbuilder-f46a16b4a0d50b6512df2b312f7f800a9a963ca2.tar.gz
Add an utility to list all installed packages
Diffstat (limited to 'src/package.cpp')
-rw-r--r--src/package.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/package.cpp b/src/package.cpp
new file mode 100644
index 0000000..0734a5e
--- /dev/null
+++ b/src/package.cpp
@@ -0,0 +1,76 @@
+#include "package.h"
+#include <filesystem>
+
+namespace katja
+{
+ package::package(const std::string& name, const std::string& version,
+ const std::string& architecture, const std::string& tag)
+ : m_name(name), m_version(version), m_architecture(architecture), m_tag(tag)
+ {
+ }
+
+ const std::string& package::name() const noexcept
+ {
+ return m_name;
+ }
+
+ const std::string& package::version() const noexcept
+ {
+ return m_version;
+ }
+
+ const std::string& package::architecture() const noexcept
+ {
+ return m_architecture;
+ }
+
+ const std::string& package::tag() const noexcept
+ {
+ return m_tag;
+ }
+
+ std::optional<package> package::parse(const std::string& full_name) noexcept
+ {
+ std::size_t tag_separator = full_name.find_last_of('-');
+
+ if (tag_separator == std::string::npos || tag_separator == 0)
+ {
+ return std::nullopt;
+ }
+ std::size_t architecture_separator = full_name.find_last_of('-', tag_separator - 1);
+
+ if (architecture_separator == std::string::npos || architecture_separator == 0)
+ {
+ return std::nullopt;
+ }
+ std::size_t version_separator = full_name.find_last_of('-', architecture_separator - 1);
+
+ if (version_separator == std::string::npos || version_separator == 0)
+ {
+ return std::nullopt;
+ }
+ package parsed_package{
+ full_name.substr(0, version_separator),
+ full_name.substr(version_separator + 1, architecture_separator - version_separator - 1),
+ full_name.substr(architecture_separator + 1, tag_separator - architecture_separator - 1),
+ full_name.substr(tag_separator + 1)
+ };
+ return std::make_optional(parsed_package);
+ }
+
+ std::unordered_map<std::string, package> read_package_database()
+ {
+ std::unordered_map<std::string, package> packages;
+
+ for (const auto& entry : std::filesystem::directory_iterator("/var/lib/pkgtools/packages"))
+ {
+ std::optional<package> maybe_package = package::parse(entry.path().filename());
+
+ if (maybe_package.has_value())
+ {
+ packages.insert_or_assign(maybe_package.value().name(), maybe_package.value());
+ }
+ }
+ return packages;
+ }
+}