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

20
#include "config.h"
21
#include "SongSave.hxx"
22
#include "db/plugins/simple/Song.hxx"
23
#include "DetachedSong.hxx"
24
#include "TagSave.hxx"
25 26
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
27
#include "tag/Tag.hxx"
28
#include "tag/TagBuilder.hxx"
29
#include "util/StringUtil.hxx"
30
#include "util/RuntimeError.hxx"
31

32
#include <string.h>
33
#include <stdlib.h>
34

35
#define SONG_MTIME "mtime"
36
#define SONG_END "song_end"
37

38
static void
39
range_save(BufferedOutputStream &os, unsigned start_ms, unsigned end_ms)
40 41
{
	if (end_ms > 0)
42
		os.Format("Range: %u-%u\n", start_ms, end_ms);
43
	else if (start_ms > 0)
44
		os.Format("Range: %u-\n", start_ms);
45 46
}

47
void
48
song_save(BufferedOutputStream &os, const Song &song)
49
{
50
	os.Format(SONG_BEGIN "%s\n", song.uri);
51

52
	range_save(os, song.start_time.ToMS(), song.end_time.ToMS());
53

54
	tag_save(os, song.tag);
55

56 57
	os.Format(SONG_MTIME ": %li\n", (long)song.mtime);
	os.Format(SONG_END "\n");
58
}
59

60
void
61
song_save(BufferedOutputStream &os, const DetachedSong &song)
62
{
63
	os.Format(SONG_BEGIN "%s\n", song.GetURI());
64

65
	range_save(os, song.GetStartTime().ToMS(), song.GetEndTime().ToMS());
66

67
	tag_save(os, song.GetTag());
68

69 70
	os.Format(SONG_MTIME ": %li\n", (long)song.GetLastModified());
	os.Format(SONG_END "\n");
71 72 73
}

DetachedSong *
74
song_load(TextFile &file, const char *uri)
75
{
76
	DetachedSong *song = new DetachedSong(uri);
77

78 79
	TagBuilder tag;

80
	char *line;
81
	while ((line = file.ReadLine()) != nullptr &&
82
	       strcmp(line, SONG_END) != 0) {
83
		char *colon = strchr(line, ':');
84
		if (colon == nullptr || colon == line) {
85
			delete song;
86

87
			throw FormatRuntimeError("unknown line in db: %s", line);
88 89 90
		}

		*colon++ = 0;
91
		const char *value = StripLeft(colon);
92

93
		TagType type;
94
		if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) {
95
			tag.AddItem(type, value);
96
		} else if (strcmp(line, "Time") == 0) {
97
			tag.SetDuration(SignedSongTime::FromS(atof(value)));
98
		} else if (strcmp(line, "Playlist") == 0) {
99
			tag.SetHasPlaylist(strcmp(value, "yes") == 0);
100
		} else if (strcmp(line, SONG_MTIME) == 0) {
101
			song->SetLastModified(atoi(value));
102 103 104
		} else if (strcmp(line, "Range") == 0) {
			char *endptr;

105 106 107 108 109
			unsigned start_ms = strtoul(value, &endptr, 10);
			unsigned end_ms = *endptr == '-'
				? strtoul(endptr + 1, nullptr, 10)
				: 0;

110 111
			song->SetStartTime(SongTime::FromMS(start_ms));
			song->SetEndTime(SongTime::FromMS(end_ms));
112
		} else {
113
			delete song;
114

115
			throw FormatRuntimeError("unknown line in db: %s", line);
116 117 118
		}
	}

119
	song->SetTag(tag.Commit());
120
	return song;
121
}