From e1ac3778128e849a688903e95712dc2dbcf20018 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 31 May 2019 19:29:13 +0200 Subject: [PATCH] archive/ArchiveLookup: replace output parameters with a struct --- src/archive/ArchiveLookup.cxx | 17 ++++++-------- src/archive/ArchiveLookup.hxx | 18 ++++++++++---- src/input/plugins/ArchiveInputPlugin.cxx | 13 +++++----- test/test_archive.cxx | 30 +++++++++++------------- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/archive/ArchiveLookup.cxx b/src/archive/ArchiveLookup.cxx index 44c0cfe30..34c85c180 100644 --- a/src/archive/ArchiveLookup.cxx +++ b/src/archive/ArchiveLookup.cxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -36,9 +36,8 @@ FindSlash(char *p, size_t i) noexcept return nullptr; } -bool -archive_lookup(char *pathname, const char **archive, - const char **inpath) +ArchiveLookupResult +archive_lookup(char *pathname) { size_t idx = strlen(pathname); @@ -51,19 +50,17 @@ archive_lookup(char *pathname, const char **archive, //is something found ins original path (is not an archive) if (slash == nullptr) - return false; + return {}; //its a file ? if (file_info.IsRegular()) { //so the upper should be file - *archive = pathname; - *inpath = slash + 1; - return true; + return {Path::FromFS(pathname), Path::FromFS(slash + 1)}; } else { FormatError(archive_domain, "Not a regular file: %s", pathname); - return false; + return {}; } } catch (const std::system_error &e) { if (!IsPathNotFound(e)) @@ -76,7 +73,7 @@ archive_lookup(char *pathname, const char **archive, slash = FindSlash(pathname, idx - 1); if (slash == nullptr) - return false; + return {}; *slash = 0; idx = slash - pathname; diff --git a/src/archive/ArchiveLookup.hxx b/src/archive/ArchiveLookup.hxx index 6f42ff7f5..0bc221ed5 100644 --- a/src/archive/ArchiveLookup.hxx +++ b/src/archive/ArchiveLookup.hxx @@ -1,5 +1,5 @@ /* - * Copyright 2003-2018 The Music Player Daemon Project + * Copyright 2003-2019 The Music Player Daemon Project * http://www.musicpd.org * * This program is free software; you can redistribute it and/or modify @@ -20,6 +20,17 @@ #ifndef MPD_ARCHIVE_LOOKUP_HXX #define MPD_ARCHIVE_LOOKUP_HXX +#include "fs/Path.hxx" + +struct ArchiveLookupResult { + Path archive = nullptr; + Path inside = nullptr; + + constexpr operator bool() const noexcept { + return !archive.IsNull(); + } +}; + /** * * archive_lookup is used to determine if part of pathname refers to an regular @@ -38,9 +49,8 @@ * * Throws on error. */ -bool -archive_lookup(char *pathname, const char **archive, - const char **inpath); +ArchiveLookupResult +archive_lookup(char *pathname); #endif diff --git a/src/input/plugins/ArchiveInputPlugin.cxx b/src/input/plugins/ArchiveInputPlugin.cxx index e2d5450a2..ab7c458db 100644 --- a/src/input/plugins/ArchiveInputPlugin.cxx +++ b/src/input/plugins/ArchiveInputPlugin.cxx @@ -44,9 +44,10 @@ OpenArchiveInputStream(Path path, Mutex &mutex) }; // archive_lookup will modify pname when true is returned - const char *archive, *filename; + ArchiveLookupResult l; try { - if (!archive_lookup(pname, &archive, &filename)) { + l = archive_lookup(pname); + if (l.archive.IsNull()) { FormatDebug(archive_domain, "not an archive, lookup %s failed", pname); return nullptr; @@ -57,7 +58,7 @@ OpenArchiveInputStream(Path path, Mutex &mutex) return nullptr; } - const char *suffix = Path::FromFS(archive).GetSuffix(); + const char *suffix = l.archive.GetSuffix(); if (suffix == nullptr) return nullptr; @@ -65,10 +66,10 @@ OpenArchiveInputStream(Path path, Mutex &mutex) arplug = archive_plugin_from_suffix(suffix); if (!arplug) { FormatWarning(archive_domain, - "can't handle archive %s", archive); + "can't handle archive %s", l.archive.c_str()); return nullptr; } - return archive_file_open(arplug, Path::FromFS(archive)) - ->OpenStream(filename, mutex); + return archive_file_open(arplug, l.archive) + ->OpenStream(l.inside.c_str(), mutex); } diff --git a/test/test_archive.cxx b/test/test_archive.cxx index ebab890ea..ae3b9c3b1 100644 --- a/test/test_archive.cxx +++ b/test/test_archive.cxx @@ -8,39 +8,37 @@ TEST(ArchiveTest, Lookup) { - const char *archive, *inpath; - char *path = strdup(""); - EXPECT_THROW(archive_lookup(path, &archive, &inpath), - std::system_error); + EXPECT_THROW(archive_lookup(path), std::system_error); free(path); path = strdup("."); - EXPECT_FALSE(archive_lookup(path, &archive, &inpath)); + EXPECT_FALSE(archive_lookup(path)); free(path); path = strdup("config.h"); - EXPECT_FALSE(archive_lookup(path, &archive, &inpath)); + EXPECT_FALSE(archive_lookup(path)); free(path); path = strdup("src/foo/bar"); - EXPECT_THROW(archive_lookup(path, &archive, &inpath), - std::system_error); + EXPECT_THROW(archive_lookup(path), std::system_error); free(path); fclose(fopen("dummy", "w")); path = strdup("dummy/foo/bar"); - EXPECT_TRUE(archive_lookup(path, &archive, &inpath)); - EXPECT_EQ((const char *)path, archive); - EXPECT_STREQ(archive, "dummy"); - EXPECT_STREQ(inpath, "foo/bar"); + auto result = archive_lookup(path); + EXPECT_TRUE(result); + EXPECT_EQ((const char *)path, result.archive.c_str()); + EXPECT_STREQ(result.archive.c_str(), "dummy"); + EXPECT_STREQ(result.inside.c_str(), "foo/bar"); free(path); path = strdup("config.h/foo/bar"); - EXPECT_TRUE(archive_lookup(path, &archive, &inpath)); - EXPECT_EQ((const char *)path, archive); - EXPECT_STREQ(archive, "config.h"); - EXPECT_STREQ(inpath, "foo/bar"); + result = archive_lookup(path); + EXPECT_TRUE(result); + EXPECT_EQ((const char *)path, result.archive.c_str()); + EXPECT_STREQ(result.archive.c_str(), "config.h"); + EXPECT_STREQ(result.inside.c_str(), "foo/bar"); free(path); }