SongSave.cxx 3.46 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/ParseName.hxx"
28
#include "tag/Tag.hxx"
29
#include "tag/Builder.hxx"
30
#include "util/ChronoUtil.hxx"
31
#include "util/StringStrip.hxx"
32
#include "util/RuntimeError.hxx"
33

34
#include <string.h>
35
#include <stdlib.h>
36

37
#define SONG_MTIME "mtime"
38
#define SONG_END "song_end"
39

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

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

54
	range_save(os, song.start_time.ToMS(), song.end_time.ToMS());
55

56
	tag_save(os, song.tag);
57

58 59 60
	if (!IsNegative(song.mtime))
		os.Format(SONG_MTIME ": %li\n",
			  (long)std::chrono::system_clock::to_time_t(song.mtime));
61
	os.Format(SONG_END "\n");
62
}
63

64
void
65
song_save(BufferedOutputStream &os, const DetachedSong &song)
66
{
67
	os.Format(SONG_BEGIN "%s\n", song.GetURI());
68

69
	range_save(os, song.GetStartTime().ToMS(), song.GetEndTime().ToMS());
70

71
	tag_save(os, song.GetTag());
72

73 74 75
	if (!IsNegative(song.GetLastModified()))
		os.Format(SONG_MTIME ": %li\n",
			  (long)std::chrono::system_clock::to_time_t(song.GetLastModified()));
76
	os.Format(SONG_END "\n");
77 78
}

79
std::unique_ptr<DetachedSong>
80
song_load(TextFile &file, const char *uri)
81
{
82
	auto song = std::make_unique<DetachedSong>(uri);
83

84 85
	TagBuilder tag;

86
	char *line;
87
	while ((line = file.ReadLine()) != nullptr &&
88
	       strcmp(line, SONG_END) != 0) {
89
		char *colon = strchr(line, ':');
90
		if (colon == nullptr || colon == line) {
91
			throw FormatRuntimeError("unknown line in db: %s", line);
92 93 94
		}

		*colon++ = 0;
95
		const char *value = StripLeft(colon);
96

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

109 110 111 112 113
			unsigned start_ms = strtoul(value, &endptr, 10);
			unsigned end_ms = *endptr == '-'
				? strtoul(endptr + 1, nullptr, 10)
				: 0;

114 115
			song->SetStartTime(SongTime::FromMS(start_ms));
			song->SetEndTime(SongTime::FromMS(end_ms));
116
		} else {
117
			throw FormatRuntimeError("unknown line in db: %s", line);
118 119 120
		}
	}

121
	song->SetTag(tag.Commit());
122
	return song;
123
}