SongSave.cxx 4.02 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "pcm/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 <stdlib.h>
38

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

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

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

56 57 58
	if (!song.target.empty())
		os.Format("Target: %s\n", song.target.c_str());

59
	range_save(os, song.start_time.ToMS(), song.end_time.ToMS());
60

61
	tag_save(os, song.tag);
62

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

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

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

77
	range_save(os, song.GetStartTime().ToMS(), song.GetEndTime().ToMS());
78

79
	tag_save(os, song.GetTag());
80

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

87
DetachedSong
88
song_load(TextFile &file, const char *uri,
89
	  std::string *target_r)
90
{
91
	DetachedSong song(uri);
92

93 94
	TagBuilder tag;

95
	char *line;
96
	while ((line = file.ReadLine()) != nullptr &&
97
	       !StringIsEqual(line, SONG_END)) {
Rosen Penev's avatar
Rosen Penev committed
98
		char *colon = std::strchr(line, ':');
99
		if (colon == nullptr || colon == line) {
100
			throw FormatRuntimeError("unknown line in db: %s", line);
101 102 103
		}

		*colon++ = 0;
104
		const char *value = StripLeft(colon);
105

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

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

133 134
			song.SetStartTime(SongTime::FromMS(start_ms));
			song.SetEndTime(SongTime::FromMS(end_ms));
135
		} else {
136
			throw FormatRuntimeError("unknown line in db: %s", line);
137 138 139
		}
	}

140
	song.SetTag(tag.Commit());
141
	return song;
142
}