PlaylistDatabase.cxx 2.09 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 27
#include "util/Error.hxx"
#include "util/Domain.hxx"
28 29 30 31

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

32
static constexpr Domain playlist_database_domain("playlist_database");
33 34

void
35
playlist_vector_save(BufferedOutputStream &os, const PlaylistVector &pv)
36
{
37
	for (const PlaylistInfo &pi : pv)
38 39 40 41
		os.Format(PLAYLIST_META_BEGIN "%s\n"
			  "mtime: %li\n"
			  "playlist_end\n",
			  pi.name.c_str(), (long)pi.mtime);
42 43 44
}

bool
45
playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name,
46
		       Error &error)
47
{
48
	PlaylistInfo pm(name, 0);
49

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

53
	while ((line = file.ReadLine()) != nullptr &&
54 55
	       strcmp(line, "playlist_end") != 0) {
		colon = strchr(line, ':');
56
		if (colon == nullptr || colon == line) {
57 58
			error.Format(playlist_database_domain,
				     "unknown line in db: %s", line);
59 60 61 62
			return false;
		}

		*colon++ = 0;
63
		value = StripLeft(colon);
64 65

		if (strcmp(line, "mtime") == 0)
66
			pm.mtime = strtol(value, nullptr, 10);
67
		else {
68 69
			error.Format(playlist_database_domain,
				     "unknown line in db: %s", line);
70 71 72 73
			return false;
		}
	}

74
	pv.UpdateOrInsert(std::move(pm));
75 76
	return true;
}