PlaylistDatabase.cxx 2.03 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "PlaylistVector.hxx"
23
#include "TextFile.hxx"
24
#include "util/StringUtil.hxx"
25 26
#include "util/Error.hxx"
#include "util/Domain.hxx"
27 28 29 30

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

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

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

bool
44
playlist_metadata_load(TextFile &file, PlaylistVector &pv, const char *name,
45
		       Error &error)
46
{
47
	PlaylistInfo pm(name, 0);
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
		if (colon == nullptr || colon == line) {
56 57
			error.Format(playlist_database_domain,
				     "unknown line in db: %s", line);
58 59 60 61
			return false;
		}

		*colon++ = 0;
62
		value = strchug_fast(colon);
63 64

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

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