aboutsummaryrefslogtreecommitdiffstats
path: root/src/db/plugins/simple/Directory.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2014-02-26 19:48:37 +0100
committerMax Kellermann <max@duempel.org>2014-02-26 19:50:46 +0100
commit69a42fc901d06af2f90d7046e153dc1273f19beb (patch)
tree672f869bd68f193343b98c10f5b946c031731410 /src/db/plugins/simple/Directory.cxx
parent525789cd36c0dcf5a9b8a9fe25944197e9fc19cf (diff)
downloadmpd-69a42fc901d06af2f90d7046e153dc1273f19beb.tar.gz
mpd-69a42fc901d06af2f90d7046e153dc1273f19beb.tar.xz
mpd-69a42fc901d06af2f90d7046e153dc1273f19beb.zip
db/simple/Directory: LookupDirectory() return remaining URI
Code can now be reused in LookupSong().
Diffstat (limited to 'src/db/plugins/simple/Directory.cxx')
-rw-r--r--src/db/plugins/simple/Directory.cxx55
1 files changed, 28 insertions, 27 deletions
diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx
index 05de51963..a81021b47 100644
--- a/src/db/plugins/simple/Directory.cxx
+++ b/src/db/plugins/simple/Directory.cxx
@@ -120,38 +120,49 @@ Directory::PruneEmpty()
}
}
-Directory *
+Directory::LookupResult
Directory::LookupDirectory(const char *uri)
{
assert(holding_db_lock());
assert(uri != nullptr);
if (isRootDirectory(uri))
- return this;
+ return { this, nullptr };
char *duplicated = xstrdup(uri), *name = duplicated;
Directory *d = this;
- while (1) {
+ while (true) {
char *slash = strchr(name, '/');
- if (slash == name) {
- d = nullptr;
+ if (slash == name)
break;
- }
if (slash != nullptr)
*slash = '\0';
- d = d->FindChild(name);
- if (d == nullptr || slash == nullptr)
+ Directory *tmp = d->FindChild(name);
+ if (tmp == nullptr)
+ /* not found */
+ break;
+
+ d = tmp;
+
+ if (slash == nullptr) {
+ /* found everything */
+ name = nullptr;
break;
+ }
name = slash + 1;
}
free(duplicated);
- return d;
+ const char *rest = name == nullptr
+ ? nullptr
+ : uri + (name - duplicated);
+
+ return { d, rest };
}
void
@@ -197,26 +208,16 @@ Directory::LookupSong(const char *uri)
assert(holding_db_lock());
assert(uri != nullptr);
- char *duplicated = xstrdup(uri);
- char *base = strrchr(duplicated, '/');
-
- Directory *d = this;
- if (base != nullptr) {
- *base++ = 0;
- d = d->LookupDirectory(duplicated);
- if (d == nullptr) {
- free(duplicated);
- return nullptr;
- }
- } else
- base = duplicated;
-
- Song *song = d->FindSong(base);
- assert(song == nullptr || song->parent == d);
+ auto r = LookupDirectory(uri);
+ if (r.uri == nullptr)
+ /* it's a directory */
+ return nullptr;
- free(duplicated);
- return song;
+ if (strchr(r.uri, '/') != nullptr)
+ /* refers to a URI "below" the actual song */
+ return nullptr;
+ return r.directory->FindSong(r.uri);
}
static int