SongSave.cxx 3.38 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 31
#include "util/Error.hxx"
#include "util/Domain.hxx"
32

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

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

39
static constexpr Domain song_save_domain("song_save");
40

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

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

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

57
	tag_save(os, song.tag);
58

59 60
	os.Format(SONG_MTIME ": %li\n", (long)song.mtime);
	os.Format(SONG_END "\n");
61
}
62

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

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

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

72 73
	os.Format(SONG_MTIME ": %li\n", (long)song.GetLastModified());
	os.Format(SONG_END "\n");
74 75 76 77
}

DetachedSong *
song_load(TextFile &file, const char *uri,
78
	  Error &error)
79
{
80
	DetachedSong *song = new DetachedSong(uri);
81

82 83
	TagBuilder tag;

84
	char *line;
85
	while ((line = file.ReadLine()) != nullptr &&
86
	       strcmp(line, SONG_END) != 0) {
87
		char *colon = strchr(line, ':');
88
		if (colon == nullptr || colon == line) {
89
			delete song;
90

91 92
			error.Format(song_save_domain,
				     "unknown line in db: %s", line);
93
			return nullptr;
94 95 96
		}

		*colon++ = 0;
97
		const char *value = StripLeft(colon);
98

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

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

116 117
			song->SetStartTime(SongTime::FromMS(start_ms));
			song->SetEndTime(SongTime::FromMS(end_ms));
118
		} else {
119
			delete song;
120

121 122
			error.Format(song_save_domain,
				     "unknown line in db: %s", line);
123
			return nullptr;
124 125 126
		}
	}

127
	song->SetTag(tag.Commit());
128
	return song;
129
}