PlaylistDatabase.cxx 2.02 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 28 29 30 31

#include <string.h>
#include <stdlib.h>

void
32
playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv)
33
{
34 35 36 37 38 39 40
	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");
	}
41 42
}

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

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

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

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

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

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