[cstdstring] remove IsEmpty() and replace with empty()
[vuplus_xbmc] / xbmc / filesystem / ZipDirectory.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 "ZipDirectory.h"
22 #include "utils/log.h"
23 #include "utils/CharsetConverter.h"
24 #include "utils/URIUtils.h"
25 #include "Util.h"
26 #include "URL.h"
27 #include "ZipManager.h"
28 #include "FileItem.h"
29 #include "utils/StringUtils.h"
30
31 #include <vector>
32
33 using namespace std;
34 namespace XFILE
35 {
36   CZipDirectory::CZipDirectory()
37   {
38   }
39
40   CZipDirectory::~CZipDirectory()
41   {
42   }
43
44   bool CZipDirectory::GetDirectory(const CStdString& strPathOrig, CFileItemList& items)
45   {
46     CStdString strPath;
47
48     /* if this isn't a proper archive path, assume it's the path to a archive file */
49     if( !StringUtils::StartsWithNoCase(strPathOrig, "zip://") )
50       URIUtils::CreateArchivePath(strPath, "zip", strPathOrig, "");
51     else
52       strPath = strPathOrig;
53
54     CURL url(strPath);
55
56     CStdString strArchive = url.GetHostName();
57     CStdString strOptions = url.GetOptions();
58     CStdString strPathInZip = url.GetFileName();
59
60     url.SetOptions(""); // delete options to have a clean path to add stuff too
61     url.SetFileName(""); // delete filename too as our names later will contain it
62
63     CStdString strSlashPath = url.Get();
64
65     CStdString strBuffer;
66
67     // the RAR code depends on things having a "/" at the end of the path
68     URIUtils::AddSlashAtEnd(strSlashPath);
69
70     vector<SZipEntry> entries;
71     // turn on fast lookups
72     bool bWasFast(items.GetFastLookup());
73     items.SetFastLookup(true);
74     if (!g_ZipManager.GetZipList(strPath,entries))
75       return false;
76
77     vector<std::string> baseTokens;
78     if (!strPathInZip.empty())
79       StringUtils::Tokenize(strPathInZip,baseTokens,"/");
80
81     for (vector<SZipEntry>::iterator ze=entries.begin();ze!=entries.end();++ze)
82     {
83       CStdString strEntryName(ze->name);
84       strEntryName.Replace('\\','/');
85       if (strEntryName == strPathInZip) // skip the listed dir
86         continue;
87
88       vector<std::string> pathTokens;
89       StringUtils::Tokenize(strEntryName,pathTokens,"/");
90       if (pathTokens.size() < baseTokens.size()+1)
91         continue;
92
93       bool bAdd=true;
94       strEntryName = "";
95       for ( unsigned int i=0;i<baseTokens.size();++i )
96       {
97         if (pathTokens[i] != baseTokens[i])
98         {
99           bAdd = false;
100           break;
101         }
102         strEntryName += pathTokens[i] + "/";
103       }
104       if (!bAdd)
105         continue;
106
107       strEntryName += pathTokens[baseTokens.size()];
108       char c=ze->name[strEntryName.size()];
109       if (c == '/' || c == '\\')
110         strEntryName += '/';
111       bool bIsFolder = false;
112       if (strEntryName[strEntryName.size()-1] != '/') // this is a file
113       {
114         strBuffer = strSlashPath + strEntryName + strOptions;
115       }
116       else
117       { // this is new folder. add if not already added
118         bIsFolder = true;
119         strBuffer = strSlashPath + strEntryName + strOptions;
120         if (items.Contains(strBuffer)) // already added
121           continue;
122       }
123
124       CFileItemPtr pFileItem(new CFileItem);
125
126       if (g_charsetConverter.isValidUtf8(pathTokens[baseTokens.size()]))
127         g_charsetConverter.utf8ToStringCharset(pathTokens[baseTokens.size()]);
128
129       pFileItem->SetLabel(pathTokens[baseTokens.size()]);
130       if (bIsFolder)
131       {
132         pFileItem->m_dwSize = 0;
133         URIUtils::AddSlashAtEnd(strBuffer);
134       }
135       else
136         pFileItem->m_dwSize = ze->usize;
137       pFileItem->SetPath(strBuffer);
138       pFileItem->m_bIsFolder = bIsFolder;
139       pFileItem->m_idepth = ze->method;
140       items.Add(pFileItem);
141     }
142     items.SetFastLookup(bWasFast);
143     return true;
144   }
145
146   bool CZipDirectory::ContainsFiles(const CStdString& strPath)
147   {
148     vector<SZipEntry> items;
149     g_ZipManager.GetZipList(strPath,items);
150     if (items.size())
151     {
152       if (items.size() > 1)
153         return true;
154
155       return false;
156     }
157
158     return false;
159   }
160 }
161