PlaylistDatabase.cxx 2.04 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "PlaylistDatabase.hxx"
21
#include "db/PlaylistVector.hxx"
22 23
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
24
#include "time/ChronoUtil.hxx"
25
#include "util/StringStrip.hxx"
26
#include "util/RuntimeError.hxx"
27

Rosen Penev's avatar
Rosen Penev committed
28 29
#include <cstring>

30 31 32
#include <stdlib.h>

void
33
playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv)
34
{
35 36 37 38 39 40 41
	for (const PlaylistInfo &pi : pv) {
		os.Format(PLAYLIST_META_BEGIN "%s\n", pi.name.c_str());
		if (!IsNegative(pi.mtime))
			os.Format("mtime: %li\n",
				  (long)std::chrono::system_clock::to_time_t(pi.mtime));
		os.Write("playlist_end\n");
	}
42 43
}

44 45
void
playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name)
46
{
47
	PlaylistInfo pm(name);
48

49 50 51
	char *line, *colon;
	const char *value;

52
	while ((line = file.ReadLine()) != nullptr &&
Rosen Penev's avatar
Rosen Penev committed
53 54
	       std::strcmp(line, "playlist_end") != 0) {
		colon = std::strchr(line, ':');
55 56 57
		if (colon == nullptr || colon == line)
			throw FormatRuntimeError("unknown line in db: %s",
						 line);
58 59

		*colon++ = 0;
60
		value = StripLeft(colon);
61

Rosen Penev's avatar
Rosen Penev committed
62
		if (std::strcmp(line, "mtime") == 0)
63
			pm.mtime = std::chrono::system_clock::from_time_t(strtol(value, nullptr, 10));
64 65 66
		else
			throw FormatRuntimeError("unknown line in db: %s",
						 line);
67 68
	}

69
	pv.UpdateOrInsert(std::move(pm));
70
}