Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / utils / URIUtils.h
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 #pragma once
21
22 #include "StdString.h"
23
24 class CURL;
25
26 class URIUtils
27 {
28 public:
29   URIUtils(void);
30   virtual ~URIUtils(void);
31   static bool IsInPath(const CStdString &uri, const CStdString &baseURI);
32
33   static CStdString GetDirectory(const CStdString &strFilePath);
34   static const CStdString GetFileName(const CStdString& strFileNameAndPath);
35
36   static CStdString GetExtension(const CStdString& strFileName);
37
38   /*!
39    \brief Check if there is a file extension
40    \param strFileName Path or URL to check
41    \return \e true if strFileName have an extension.
42    \note Returns false when strFileName is empty.
43    \sa GetExtension
44    */
45   static bool HasExtension(const CStdString& strFileName);
46
47   /*!
48    \brief Check if filename have any of the listed extensions
49    \param strFileName Path or URL to check
50    \param strExtensions List of '.' prefixed lowercase extensions seperated with '|'
51    \return \e true if strFileName have any one of the extensions.
52    \note The check is case insensitive for strFileName, but requires
53          strExtensions to be lowercase. Returns false when strFileName or
54          strExtensions is empty.
55    \sa GetExtension
56    */
57   static bool HasExtension(const CStdString& strFileName, const CStdString& strExtensions);
58
59   static void RemoveExtension(CStdString& strFileName);
60   static CStdString ReplaceExtension(const CStdString& strFile,
61                                      const CStdString& strNewExtension);
62   static void Split(const CStdString& strFileNameAndPath, 
63                     CStdString& strPath, CStdString& strFileName);
64   static void Split(const std::string& strFileNameAndPath, 
65                     std::string& strPath, std::string& strFileName);
66   static CStdStringArray SplitPath(const CStdString& strPath);
67
68   static void GetCommonPath(CStdString& strPath, const CStdString& strPath2);
69   static CStdString GetParentPath(const CStdString& strPath);
70   static bool GetParentPath(const CStdString& strPath, CStdString& strParent);
71
72   /* \brief Change the base path of a URL: fromPath/fromFile -> toPath/toFile
73     Handles changes in path separator and filename URL encoding if necessary to derive toFile.
74     \param fromPath the base path of the original URL
75     \param fromFile the filename portion of the original URL
76     \param toPath the base path of the resulting URL
77     \return the full path.
78    */
79   static std::string ChangeBasePath(const std::string &fromPath, const std::string &fromFile, const std::string &toPath);
80
81   static CStdString SubstitutePath(const CStdString& strPath, bool reverse = false);
82
83   static bool IsAddonsPath(const CStdString& strFile);
84   static bool IsSourcesPath(const CStdString& strFile);
85   static bool IsCDDA(const CStdString& strFile);
86   static bool IsDAAP(const CStdString& strFile);
87   static bool IsDAV(const CStdString& strFile);
88   static bool IsDOSPath(const CStdString &path);
89   static bool IsDVD(const CStdString& strFile);
90   static bool IsFTP(const CStdString& strFile);
91   static bool IsHD(const CStdString& strFileName);
92   static bool IsHDHomeRun(const CStdString& strFile);
93   static bool IsSlingbox(const CStdString& strFile);
94   static bool IsHTSP(const CStdString& strFile);
95   static bool IsInArchive(const CStdString& strFile);
96   static bool IsInRAR(const CStdString& strFile);
97   static bool IsInternetStream(const CURL& url, bool bStrictCheck = false);
98   static bool IsInAPK(const CStdString& strFile);
99   static bool IsInZIP(const CStdString& strFile);
100   static bool IsISO9660(const CStdString& strFile);
101   static bool IsLiveTV(const CStdString& strFile);
102   static bool IsPVRRecording(const CStdString& strFile);
103   static bool IsMultiPath(const CStdString& strPath);
104   static bool IsMusicDb(const CStdString& strFile);
105   static bool IsMythTV(const CStdString& strFile);
106   static bool IsNfs(const CStdString& strFile);  
107   static bool IsAfp(const CStdString& strFile);    
108   static bool IsOnDVD(const CStdString& strFile);
109   static bool IsOnLAN(const CStdString& strFile);
110   static bool IsHostOnLAN(const CStdString& hostName, bool offLineCheck = false);
111   static bool IsPlugin(const CStdString& strFile);
112   static bool IsScript(const CStdString& strFile);
113   static bool IsRAR(const CStdString& strFile);
114   static bool IsRemote(const CStdString& strFile);
115   static bool IsSmb(const CStdString& strFile);
116   static bool IsSpecial(const CStdString& strFile);
117   static bool IsStack(const CStdString& strFile);
118   static bool IsTuxBox(const CStdString& strFile);
119   static bool IsUPnP(const CStdString& strFile);
120   static bool IsURL(const CStdString& strFile);
121   static bool IsVideoDb(const CStdString& strFile);
122   static bool IsVTP(const CStdString& strFile);
123   static bool IsAPK(const CStdString& strFile);
124   static bool IsZIP(const CStdString& strFile);
125   static bool IsArchive(const CStdString& strFile);
126   static bool IsBluray(const CStdString& strFile);
127   static bool IsAndroidApp(const CStdString& strFile);
128   static bool IsLibraryFolder(const CStdString& strFile);
129   static bool IsLibraryContent(const std::string& strFile);
130
131   static void AddSlashAtEnd(std::string& strFolder);
132   static bool HasSlashAtEnd(const std::string& strFile, bool checkURL = false);
133   static void RemoveSlashAtEnd(std::string& strFolder);
134   static bool CompareWithoutSlashAtEnd(const CStdString& strPath1, const CStdString& strPath2);
135   static std::string FixSlashesAndDups(const std::string& path, const char slashCharacter = '/', const size_t startFrom = 0);
136   /**
137    * Convert path to form without duplicated slashes and without relative directories
138    * Strip duplicated slashes
139    * Resolve and remove relative directories ("/../" and "/./")
140    * Will ignore slashes with other direction than specified
141    * Will not resolve path starting from relative directory
142    * @warning Don't use with "protocol://path"-style URLs
143    * @param path string to process
144    * @param slashCharacter character to use as directory delimiter
145    * @return transformed path
146    */
147   static std::string CanonicalizePath(const std::string& path, const char slashCharacter = '\\');
148
149   static void CreateArchivePath(CStdString& strUrlPath,
150                                 const CStdString& strType,
151                                 const CStdString& strArchivePath,
152                                 const CStdString& strFilePathInArchive,
153                                 const CStdString& strPwd="");
154
155   static CStdString AddFileToFolder(const CStdString &strFolder, const CStdString &strFile);
156
157   static bool ProtocolHasParentInHostname(const CStdString& prot);
158   static bool ProtocolHasEncodedHostname(const CStdString& prot);
159   static bool ProtocolHasEncodedFilename(const CStdString& prot);
160
161   /*!
162    \brief Cleans up the given path by resolving "." and ".."
163    and returns it.
164
165    This methods goes through the given path and removes any "."
166    (as it states "this directory") and resolves any ".." by
167    removing the previous directory from the path. This is done
168    for file paths and host names (in case of VFS paths).
169
170    \param path Path to be cleaned up
171    \return Actual path without any "." or ".."
172    */
173   static std::string GetRealPath(const std::string &path);
174
175   /*!
176    \brief Updates the URL encoded hostname of the given path
177
178    This method must only be used to update paths encoded with
179    the old (Eden) URL encoding implementation to the new (Frodo)
180    URL encoding implementation (which does not URL encode -_.!().
181
182    \param strFilename Path to update
183    \return True if the path has been updated/changed otherwise false
184    */
185   static bool UpdateUrlEncoding(std::string &strFilename);
186
187 private:
188   static std::string resolvePath(const std::string &path);
189 };
190