SongSave.cxx 3.92 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 "SongSave.hxx"
21
#include "AudioParser.hxx"
22
#include "db/plugins/simple/Song.hxx"
23
#include "song/DetachedSong.hxx"
24
#include "TagSave.hxx"
25 26
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
27
#include "tag/ParseName.hxx"
28
#include "tag/Tag.hxx"
29
#include "tag/Builder.hxx"
30
#include "time/ChronoUtil.hxx"
31 32
#include "util/StringAPI.hxx"
#include "util/StringBuffer.hxx"
33
#include "util/StringStrip.hxx"
34
#include "util/RuntimeError.hxx"
35
#include "util/NumberParser.hxx"
36

37
#include <string.h>
38
#include <stdlib.h>
39

40
#define SONG_MTIME "mtime"
41
#define SONG_END "song_end"
42

43
static void
44
range_save(BufferedOutputStream &os, unsigned start_ms, unsigned end_ms)
45 46
{
	if (end_ms > 0)
47
		os.Format("Range: %u-%u\n", start_ms, end_ms);
48
	else if (start_ms > 0)
49
		os.Format("Range: %u-\n", start_ms);
50 51
}

52
void
53
song_save(BufferedOutputStream &os, const Song &song)
54
{
55
	os.Format(SONG_BEGIN "%s\n", song.uri);
56

57
	range_save(os, song.start_time.ToMS(), song.end_time.ToMS());
58

59
	tag_save(os, song.tag);
60

61 62 63
	if (song.audio_format.IsDefined())
		os.Format("Format: %s\n", ToString(song.audio_format).c_str());

64 65 66
	if (!IsNegative(song.mtime))
		os.Format(SONG_MTIME ": %li\n",
			  (long)std::chrono::system_clock::to_time_t(song.mtime));
67
	os.Format(SONG_END "\n");
68
}
69

70
void
71
song_save(BufferedOutputStream &os, const DetachedSong &song)
72
{
73
	os.Format(SONG_BEGIN "%s\n", song.GetURI());
74

75
	range_save(os, song.GetStartTime().ToMS(), song.GetEndTime().ToMS());
76

77
	tag_save(os, song.GetTag());
78

79 80 81
	if (!IsNegative(song.GetLastModified()))
		os.Format(SONG_MTIME ": %li\n",
			  (long)std::chrono::system_clock::to_time_t(song.GetLastModified()));
82
	os.Format(SONG_END "\n");
83 84
}

85
std::unique_ptr<DetachedSong>
86 87
song_load(TextFile &file, const char *uri,
	  AudioFormat *audio_format_r)
88
{
89
	auto song = std::make_unique<DetachedSong>(uri);
90

91 92
	TagBuilder tag;

93
	char *line;
94
	while ((line = file.ReadLine()) != nullptr &&
95
	       strcmp(line, SONG_END) != 0) {
96
		char *colon = strchr(line, ':');
97
		if (colon == nullptr || colon == line) {
98
			throw FormatRuntimeError("unknown line in db: %s", line);
99 100 101
		}

		*colon++ = 0;
102
		const char *value = StripLeft(colon);
103

104
		TagType type;
105
		if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) {
106
			tag.AddItem(type, value);
107
		} else if (strcmp(line, "Time") == 0) {
108
			tag.SetDuration(SignedSongTime::FromS(ParseDouble(value)));
109 110 111 112 113 114 115 116 117
		} else if (StringIsEqual(line, "Format")) {
			if (audio_format_r != nullptr) {
				try {
					*audio_format_r =
						ParseAudioFormat(value, false);
				} catch (...) {
					/* ignore parser errors */
				}
			}
118
		} else if (strcmp(line, "Playlist") == 0) {
119
			tag.SetHasPlaylist(strcmp(value, "yes") == 0);
120
		} else if (strcmp(line, SONG_MTIME) == 0) {
121
			song->SetLastModified(std::chrono::system_clock::from_time_t(atoi(value)));
122 123 124
		} else if (strcmp(line, "Range") == 0) {
			char *endptr;

125 126 127 128 129
			unsigned start_ms = strtoul(value, &endptr, 10);
			unsigned end_ms = *endptr == '-'
				? strtoul(endptr + 1, nullptr, 10)
				: 0;

130 131
			song->SetStartTime(SongTime::FromMS(start_ms));
			song->SetEndTime(SongTime::FromMS(end_ms));
132
		} else {
133
			throw FormatRuntimeError("unknown line in db: %s", line);
134 135 136
		}
	}

137
	song->SetTag(tag.Commit());
138
	return song;
139
}