2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2019-06-17 11:17:30 +02:00
|
|
|
* Copyright 2003-2019 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2008-09-07 13:35:01 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2008-09-07 13:35:01 +02:00
|
|
|
*/
|
|
|
|
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "SongSave.hxx"
|
2018-07-06 13:22:17 +02:00
|
|
|
#include "AudioParser.hxx"
|
2014-02-26 09:17:41 +01:00
|
|
|
#include "db/plugins/simple/Song.hxx"
|
2018-08-02 13:45:43 +02:00
|
|
|
#include "song/DetachedSong.hxx"
|
2013-01-02 19:52:57 +01:00
|
|
|
#include "TagSave.hxx"
|
2014-08-07 18:35:57 +02:00
|
|
|
#include "fs/io/TextFile.hxx"
|
|
|
|
#include "fs/io/BufferedOutputStream.hxx"
|
2017-02-08 08:57:22 +01:00
|
|
|
#include "tag/ParseName.hxx"
|
2013-09-05 18:22:02 +02:00
|
|
|
#include "tag/Tag.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Builder.hxx"
|
2019-05-08 15:47:58 +02:00
|
|
|
#include "time/ChronoUtil.hxx"
|
2018-07-06 13:22:17 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
|
|
|
#include "util/StringBuffer.hxx"
|
2017-07-05 17:20:02 +02:00
|
|
|
#include "util/StringStrip.hxx"
|
2016-10-29 09:45:34 +02:00
|
|
|
#include "util/RuntimeError.hxx"
|
2018-02-09 20:46:55 +01:00
|
|
|
#include "util/NumberParser.hxx"
|
2009-03-15 18:23:00 +01:00
|
|
|
|
2013-11-11 08:17:29 +01:00
|
|
|
#include <stdlib.h>
|
2009-01-03 14:52:56 +01:00
|
|
|
|
2009-11-04 18:43:16 +01:00
|
|
|
#define SONG_MTIME "mtime"
|
2009-11-01 17:51:29 +01:00
|
|
|
#define SONG_END "song_end"
|
2008-09-07 13:35:01 +02:00
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
static void
|
2014-07-30 20:58:14 +02:00
|
|
|
range_save(BufferedOutputStream &os, unsigned start_ms, unsigned end_ms)
|
2014-01-07 21:39:47 +01:00
|
|
|
{
|
|
|
|
if (end_ms > 0)
|
2014-07-30 20:58:14 +02:00
|
|
|
os.Format("Range: %u-%u\n", start_ms, end_ms);
|
2014-01-07 21:39:47 +01:00
|
|
|
else if (start_ms > 0)
|
2014-07-30 20:58:14 +02:00
|
|
|
os.Format("Range: %u-\n", start_ms);
|
2014-01-07 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2010-07-25 12:21:47 +02:00
|
|
|
void
|
2014-07-30 20:58:14 +02:00
|
|
|
song_save(BufferedOutputStream &os, const Song &song)
|
2008-09-07 13:36:05 +02:00
|
|
|
{
|
2019-09-03 20:23:46 +02:00
|
|
|
os.Format(SONG_BEGIN "%s\n", song.uri.c_str());
|
2008-09-07 13:36:05 +02:00
|
|
|
|
2014-08-28 12:49:29 +02:00
|
|
|
range_save(os, song.start_time.ToMS(), song.end_time.ToMS());
|
2010-07-25 12:09:28 +02:00
|
|
|
|
2014-07-30 20:58:14 +02:00
|
|
|
tag_save(os, song.tag);
|
2008-10-07 22:10:42 +02:00
|
|
|
|
2018-07-06 13:22:17 +02:00
|
|
|
if (song.audio_format.IsDefined())
|
|
|
|
os.Format("Format: %s\n", ToString(song.audio_format).c_str());
|
|
|
|
|
2017-01-18 13:19:13 +01:00
|
|
|
if (!IsNegative(song.mtime))
|
|
|
|
os.Format(SONG_MTIME ": %li\n",
|
|
|
|
(long)std::chrono::system_clock::to_time_t(song.mtime));
|
2014-07-30 20:58:14 +02:00
|
|
|
os.Format(SONG_END "\n");
|
2010-07-25 12:21:47 +02:00
|
|
|
}
|
2008-10-07 22:10:42 +02:00
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
void
|
2014-07-30 20:58:14 +02:00
|
|
|
song_save(BufferedOutputStream &os, const DetachedSong &song)
|
2014-01-07 21:39:47 +01:00
|
|
|
{
|
2014-07-30 20:58:14 +02:00
|
|
|
os.Format(SONG_BEGIN "%s\n", song.GetURI());
|
2014-01-07 21:39:47 +01:00
|
|
|
|
2014-08-28 06:54:19 +02:00
|
|
|
range_save(os, song.GetStartTime().ToMS(), song.GetEndTime().ToMS());
|
2014-01-07 21:39:47 +01:00
|
|
|
|
2014-07-30 20:58:14 +02:00
|
|
|
tag_save(os, song.GetTag());
|
2014-01-07 21:39:47 +01:00
|
|
|
|
2017-01-18 13:19:13 +01:00
|
|
|
if (!IsNegative(song.GetLastModified()))
|
|
|
|
os.Format(SONG_MTIME ": %li\n",
|
|
|
|
(long)std::chrono::system_clock::to_time_t(song.GetLastModified()));
|
2014-07-30 20:58:14 +02:00
|
|
|
os.Format(SONG_END "\n");
|
2014-01-07 21:39:47 +01:00
|
|
|
}
|
|
|
|
|
2017-11-26 12:18:31 +01:00
|
|
|
std::unique_ptr<DetachedSong>
|
2018-07-06 13:22:17 +02:00
|
|
|
song_load(TextFile &file, const char *uri,
|
|
|
|
AudioFormat *audio_format_r)
|
2008-09-07 13:35:01 +02:00
|
|
|
{
|
2017-11-26 12:18:31 +01:00
|
|
|
auto song = std::make_unique<DetachedSong>(uri);
|
2008-09-07 13:35:01 +02:00
|
|
|
|
2013-09-05 18:59:19 +02:00
|
|
|
TagBuilder tag;
|
|
|
|
|
2014-01-08 23:10:24 +01:00
|
|
|
char *line;
|
2013-10-19 18:19:03 +02:00
|
|
|
while ((line = file.ReadLine()) != nullptr &&
|
2019-09-04 11:09:20 +02:00
|
|
|
!StringIsEqual(line, SONG_END)) {
|
2014-01-08 23:10:24 +01:00
|
|
|
char *colon = strchr(line, ':');
|
2013-10-19 18:19:03 +02:00
|
|
|
if (colon == nullptr || colon == line) {
|
2016-10-29 09:45:34 +02:00
|
|
|
throw FormatRuntimeError("unknown line in db: %s", line);
|
2009-11-04 18:43:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*colon++ = 0;
|
2014-08-07 14:53:07 +02:00
|
|
|
const char *value = StripLeft(colon);
|
2009-11-04 18:43:16 +01:00
|
|
|
|
2014-01-08 23:10:24 +01:00
|
|
|
TagType type;
|
2009-11-04 18:47:42 +01:00
|
|
|
if ((type = tag_name_parse(line)) != TAG_NUM_OF_ITEM_TYPES) {
|
2013-09-05 18:59:19 +02:00
|
|
|
tag.AddItem(type, value);
|
2019-09-04 11:09:20 +02:00
|
|
|
} else if (StringIsEqual(line, "Time")) {
|
2018-02-09 20:46:55 +01:00
|
|
|
tag.SetDuration(SignedSongTime::FromS(ParseDouble(value)));
|
2018-07-06 13:22:17 +02:00
|
|
|
} else if (StringIsEqual(line, "Format")) {
|
|
|
|
if (audio_format_r != nullptr) {
|
|
|
|
try {
|
|
|
|
*audio_format_r =
|
|
|
|
ParseAudioFormat(value, false);
|
|
|
|
} catch (...) {
|
|
|
|
/* ignore parser errors */
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 11:09:20 +02:00
|
|
|
} else if (StringIsEqual(line, "Playlist")) {
|
|
|
|
tag.SetHasPlaylist(StringIsEqual(value, "yes"));
|
|
|
|
} else if (StringIsEqual(line, SONG_MTIME)) {
|
2017-01-18 13:19:13 +01:00
|
|
|
song->SetLastModified(std::chrono::system_clock::from_time_t(atoi(value)));
|
2019-09-04 11:09:20 +02:00
|
|
|
} else if (StringIsEqual(line, "Range")) {
|
2010-07-25 12:09:28 +02:00
|
|
|
char *endptr;
|
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
unsigned start_ms = strtoul(value, &endptr, 10);
|
|
|
|
unsigned end_ms = *endptr == '-'
|
|
|
|
? strtoul(endptr + 1, nullptr, 10)
|
|
|
|
: 0;
|
|
|
|
|
2014-08-28 06:54:19 +02:00
|
|
|
song->SetStartTime(SongTime::FromMS(start_ms));
|
|
|
|
song->SetEndTime(SongTime::FromMS(end_ms));
|
2009-07-05 08:29:52 +02:00
|
|
|
} else {
|
2016-10-29 09:45:34 +02:00
|
|
|
throw FormatRuntimeError("unknown line in db: %s", line);
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 21:39:47 +01:00
|
|
|
song->SetTag(tag.Commit());
|
2009-11-01 17:51:29 +01:00
|
|
|
return song;
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|