2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "PlayListB4S.h"
13 #include "filesystem/File.h"
14 #include "music/tags/MusicInfoTag.h"
15 #include "utils/StringUtils.h"
16 #include "utils/URIUtils.h"
17 #include "utils/XBMCTinyXML2.h"
18 #include "utils/XMLUtils.h"
19 #include "utils/log.h"
24 using namespace XFILE
;
26 /* ------------------------ example b4s playlist file ---------------------------------
27 <?xml version="1.0" encoding='UTF-8' standalone="yes"?>
29 <!-- Generated by: Nullsoft Winamp3 version 3.0d -->
30 <playlist num_entries="2" label="Playlist 001">
31 <entry Playstring="file:E:\Program Files\Winamp3\demo.mp3">
35 <entry Playstring="file:E:\Program Files\Winamp3\demo.mp3">
41 ------------------------ end of example b4s playlist file ---------------------------------*/
43 namespace KODI::PLAYLIST
46 CPlayListB4S::CPlayListB4S(void) = default;
48 CPlayListB4S::~CPlayListB4S(void) = default;
51 bool CPlayListB4S::LoadData(std::istream
& stream
)
55 std::string
b4sStream(std::istreambuf_iterator
<char>(stream
), {});
57 xmlDoc
.Parse(b4sStream
);
61 CLog::Log(LOGERROR
, "Unable to parse B4S info Error: {}", xmlDoc
.ErrorStr());
65 auto* pRootElement
= xmlDoc
.RootElement();
69 auto* pPlayListElement
= pRootElement
->FirstChildElement("playlist");
70 if (!pPlayListElement
)
73 m_strPlayListName
= XMLUtils::GetAttribute(pPlayListElement
, "label");
75 auto* pEntryElement
= pPlayListElement
->FirstChildElement("entry");
82 std::string strFileName
= XMLUtils::GetAttribute(pEntryElement
, "Playstring");
83 size_t iColon
= strFileName
.find(':');
84 if (iColon
!= std::string::npos
)
87 strFileName
.erase(0, iColon
);
89 if (strFileName
.size())
91 auto* pNodeInfo
= pEntryElement
->FirstChildElement("Name");
92 auto* pNodeLength
= pEntryElement
->FirstChildElement("Length");
96 lDuration
= atol(pNodeLength
->FirstChild()->Value());
100 std::string strInfo
= pNodeInfo
->FirstChild()->Value();
101 strFileName
= URIUtils::SubstitutePath(strFileName
);
102 CUtil::GetQualifiedFilename(m_strBasePath
, strFileName
);
103 CFileItemPtr
newItem(new CFileItem(strInfo
));
104 newItem
->SetPath(strFileName
);
105 newItem
->GetMusicInfoTag()->SetDuration(lDuration
);
109 pEntryElement
= pEntryElement
->NextSiblingElement();
114 void CPlayListB4S::Save(const std::string
& strFileName
) const
116 if (!m_vecItems
.size()) return ;
117 std::string strPlaylist
= strFileName
;
118 strPlaylist
= CUtil::MakeLegalPath(std::move(strPlaylist
));
120 if (!file
.OpenForWrite(strPlaylist
, true))
122 CLog::Log(LOGERROR
, "Could not save B4S playlist: [{}]", strPlaylist
);
126 write
+= StringUtils::Format("<?xml version={}1.0{} encoding='UTF-8' standalone={}yes{}?>\n", 34,
128 write
+= StringUtils::Format("<WinampXML>\n");
129 write
+= StringUtils::Format(" <playlist num_entries=\"{0}\" label=\"{1}\">\n",
130 m_vecItems
.size(), m_strPlayListName
);
131 for (int i
= 0; i
< (int)m_vecItems
.size(); ++i
)
133 const CFileItemPtr item
= m_vecItems
[i
];
134 write
+= StringUtils::Format(" <entry Playstring={}file:{}{}>\n", 34, item
->GetPath(), 34);
135 write
+= StringUtils::Format(" <Name>{}</Name>\n", item
->GetLabel().c_str());
137 StringUtils::Format(" <Length>{}</Length>\n", item
->GetMusicInfoTag()->GetDuration());
139 write
+= StringUtils::Format(" </playlist>\n");
140 write
+= StringUtils::Format("</WinampXML>\n");
141 file
.Write(write
.c_str(), write
.size());
145 } // namespace KODI::PLAYLIST