[release] version bump to 13.0 beta1
[vuplus_xbmc] / xbmc / filesystem / MusicDatabaseFile.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "MusicDatabaseFile.h"
22 #include "music/MusicDatabase.h"
23 #include "URL.h"
24 #include "utils/StringUtils.h"
25 #include "utils/URIUtils.h"
26
27 #include <sys/stat.h>
28
29 using namespace XFILE;
30
31 CMusicDatabaseFile::CMusicDatabaseFile(void)
32 {
33 }
34
35 CMusicDatabaseFile::~CMusicDatabaseFile(void)
36 {
37   Close();
38 }
39
40 CStdString CMusicDatabaseFile::TranslateUrl(const CURL& url)
41 {
42   CMusicDatabase musicDatabase;
43   if (!musicDatabase.Open())
44     return "";
45
46   CStdString strFileName=URIUtils::GetFileName(url.Get());
47   CStdString strExtension = URIUtils::GetExtension(strFileName);
48   URIUtils::RemoveExtension(strFileName);
49
50   if (!StringUtils::IsNaturalNumber(strFileName))
51     return "";
52
53   long idSong=atol(strFileName.c_str());
54
55   CSong song;
56   if (!musicDatabase.GetSong(idSong, song))
57     return "";
58
59   if (!URIUtils::HasExtension(song.strFileName, strExtension))
60     return "";
61
62   return song.strFileName;
63 }
64
65 bool CMusicDatabaseFile::Open(const CURL& url)
66 {
67   return m_file.Open(TranslateUrl(url));
68 }
69
70 bool CMusicDatabaseFile::Exists(const CURL& url)
71 {
72   return !TranslateUrl(url).empty();
73 }
74
75 int CMusicDatabaseFile::Stat(const CURL& url, struct __stat64* buffer)
76 {
77   return m_file.Stat(TranslateUrl(url), buffer);
78 }
79
80 unsigned int CMusicDatabaseFile::Read(void* lpBuf, int64_t uiBufSize)
81 {
82   return m_file.Read(lpBuf, uiBufSize);
83 }
84
85 int64_t CMusicDatabaseFile::Seek(int64_t iFilePosition, int iWhence /*=SEEK_SET*/)
86 {
87   return m_file.Seek(iFilePosition, iWhence);
88 }
89
90 void CMusicDatabaseFile::Close()
91 {
92   m_file.Close();
93 }
94
95 int64_t CMusicDatabaseFile::GetPosition()
96 {
97   return m_file.GetPosition();
98 }
99
100 int64_t CMusicDatabaseFile::GetLength()
101 {
102   return m_file.GetLength();
103 }
104