PlaylistDatabase.cxx 1.89 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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/StringUtil.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
	for (const PlaylistInfo &pi : pv)
35 36 37 38
		os.Format(PLAYLIST_META_BEGIN "%s\n"
			  "mtime: %li\n"
			  "playlist_end\n",
			  pi.name.c_str(), (long)pi.mtime);
39 40
}

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

46 47 48
	char *line, *colon;
	const char *value;

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

		*colon++ = 0;
57
		value = StripLeft(colon);
58 59

		if (strcmp(line, "mtime") == 0)
60
			pm.mtime = strtol(value, nullptr, 10);
61 62 63
		else
			throw FormatRuntimeError("unknown line in db: %s",
						 line);
64 65
	}

66
	pv.UpdateOrInsert(std::move(pm));
67
}