2 * Copyright (C) 2005-2009 Team XBMC
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)
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.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "HTTPDirectory.h"
26 #include "utils/RegExp.h"
27 #include "settings/AdvancedSettings.h"
28 #include "utils/StringUtils.h"
29 #include "utils/CharsetConverter.h"
30 #include "utils/log.h"
31 #include "utils/URIUtils.h"
34 using namespace XFILE;
36 CHTTPDirectory::CHTTPDirectory(void){}
37 CHTTPDirectory::~CHTTPDirectory(void){}
39 bool CHTTPDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
44 CStdString strName, strLink;
45 CStdString strBasePath = url.GetFileName();
49 CLog::Log(LOGERROR, "%s - Unable to get http directory", __FUNCTION__);
54 reItem.RegComp("<a href=\"(.*)\">(.*)</a>");
56 /* read response from server into string buffer */
57 char buffer[MAX_PATH + 1024];
58 while(http.ReadString(buffer, sizeof(buffer)-1))
60 CStdString strBuffer = buffer;
61 StringUtils::RemoveCRLF(strBuffer);
63 if (reItem.RegFind(strBuffer.c_str()) >= 0)
65 strLink = reItem.GetReplaceString("\\1");
66 strName = reItem.GetReplaceString("\\2");
69 strLink = strLink.Mid(1);
71 CStdString strNameTemp = strName.Trim();
72 CStdString strLinkTemp = strLink;
73 URIUtils::RemoveSlashAtEnd(strLinkTemp);
74 URIUtils::RemoveSlashAtEnd(strNameTemp);
75 CURL::Decode(strLinkTemp);
77 if (strNameTemp == strLinkTemp)
79 g_charsetConverter.unknownToUTF8(strName);
80 URIUtils::RemoveSlashAtEnd(strName);
82 CFileItemPtr pItem(new CFileItem(strName));
83 pItem->SetPath(strBasePath + strLink);
84 pItem->SetProperty("IsHTTPDirectory", true);
86 if(URIUtils::HasSlashAtEnd(pItem->GetPath()))
87 pItem->m_bIsFolder = true;
89 url.SetFileName(pItem->GetPath());
90 pItem->SetPath(url.Get());
92 if (!pItem->m_bIsFolder && g_advancedSettings.m_bHTTPDirectoryStatFilesize)
96 pItem->m_dwSize= file.GetLength();
100 if (!pItem->m_bIsFolder && pItem->m_dwSize == 0)
103 reSize.RegComp(">([0-9.]+)(K|M|G)</td>");
104 if (reSize.RegFind(strBuffer.c_str()) >= 0)
106 double Size = atof(reSize.GetReplaceString("\\1"));
107 CStdString strUnit = reSize.GetReplaceString("\\2");
111 else if (strUnit == "G")
112 Size = Size * 1000 * 1024;
114 pItem->m_dwSize = (int64_t)(Size * 1024);
127 bool CHTTPDirectory::Exists(const char* strPath)
131 struct __stat64 buffer;
133 if( http.Stat(url, &buffer) != 0 )
138 if (buffer.st_mode == _S_IFDIR)