PlaylistDatabase.cxx 2.04 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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.
 */

#include "config.h"
21
#include "PlaylistDatabase.hxx"
22
#include "db/PlaylistVector.hxx"
23 24
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
25
#include "util/StringStrip.hxx"
26
#include "util/ChronoUtil.hxx"
27
#include "util/RuntimeError.hxx"
28 29 30 31 32

#include <string.h>
#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 &&
53 54
	       strcmp(line, "playlist_end") != 0) {
		colon = 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 62

		if (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
}