2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2010-07-21 08:58:15 +02:00
|
|
|
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "PlaylistDatabase.hxx"
|
2014-01-27 11:05:21 +01:00
|
|
|
#include "db/PlaylistVector.hxx"
|
2022-11-28 21:58:21 +01:00
|
|
|
#include "lib/fmt/RuntimeError.hxx"
|
2021-12-03 14:02:07 +01:00
|
|
|
#include "io/LineReader.hxx"
|
|
|
|
#include "io/BufferedOutputStream.hxx"
|
2019-05-08 15:47:58 +02:00
|
|
|
#include "time/ChronoUtil.hxx"
|
2017-07-05 17:20:02 +02:00
|
|
|
#include "util/StringStrip.hxx"
|
2010-07-21 08:58:15 +02:00
|
|
|
|
2022-07-13 14:06:28 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-05-01 04:25:55 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2010-07-21 08:58:15 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
void
|
2014-07-30 20:58:14 +02:00
|
|
|
playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv)
|
2010-07-21 08:58:15 +02:00
|
|
|
{
|
2017-02-11 23:09:33 +01:00
|
|
|
for (const PlaylistInfo &pi : pv) {
|
2022-07-13 12:54:58 +02:00
|
|
|
os.Fmt(FMT_STRING(PLAYLIST_META_BEGIN "{}\n"), pi.name);
|
2017-02-11 23:09:33 +01:00
|
|
|
if (!IsNegative(pi.mtime))
|
2022-07-13 12:54:58 +02:00
|
|
|
os.Fmt(FMT_STRING("mtime: {}\n"),
|
|
|
|
std::chrono::system_clock::to_time_t(pi.mtime));
|
2017-02-11 23:09:33 +01:00
|
|
|
os.Write("playlist_end\n");
|
|
|
|
}
|
2010-07-21 08:58:15 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 09:45:34 +02:00
|
|
|
void
|
2021-12-03 14:16:32 +01:00
|
|
|
playlist_metadata_load(LineReader &file, PlaylistVector &pv, const char *name)
|
2010-07-21 08:58:15 +02:00
|
|
|
{
|
2017-02-11 23:09:33 +01:00
|
|
|
PlaylistInfo pm(name);
|
2013-01-02 19:52:57 +01:00
|
|
|
|
2010-07-21 08:58:15 +02:00
|
|
|
char *line, *colon;
|
|
|
|
const char *value;
|
|
|
|
|
2013-10-02 08:13:28 +02:00
|
|
|
while ((line = file.ReadLine()) != nullptr &&
|
2020-05-01 04:25:55 +02:00
|
|
|
std::strcmp(line, "playlist_end") != 0) {
|
|
|
|
colon = std::strchr(line, ':');
|
2016-10-29 09:45:34 +02:00
|
|
|
if (colon == nullptr || colon == line)
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("unknown line in db: {}", line);
|
2010-07-21 08:58:15 +02:00
|
|
|
|
|
|
|
*colon++ = 0;
|
2014-08-07 14:53:07 +02:00
|
|
|
value = StripLeft(colon);
|
2010-07-21 08:58:15 +02:00
|
|
|
|
2020-05-01 04:25:55 +02:00
|
|
|
if (std::strcmp(line, "mtime") == 0)
|
2017-02-11 23:09:33 +01:00
|
|
|
pm.mtime = std::chrono::system_clock::from_time_t(strtol(value, nullptr, 10));
|
2016-10-29 09:45:34 +02:00
|
|
|
else
|
2022-11-28 21:58:21 +01:00
|
|
|
throw FmtRuntimeError("unknown line in db: {}", line);
|
2010-07-21 08:58:15 +02:00
|
|
|
}
|
|
|
|
|
2013-01-02 22:16:52 +01:00
|
|
|
pv.UpdateOrInsert(std::move(pm));
|
2010-07-21 08:58:15 +02:00
|
|
|
}
|