3 * Copyright (C) 2005-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
27 #include "utils/StringUtils.h"
28 #include "MediaSource.h"
30 // A list of filesystem types for LegalPath/FileName
32 #define LEGAL_WIN32_COMPAT 1
44 struct sortstringbyname
46 bool operator()(const CStdString& strItem1, const CStdString& strItem2)
48 CStdString strLine1 = strItem1;
49 CStdString strLine2 = strItem2;
50 StringUtils::ToLower(strLine1);
51 StringUtils::ToLower(strLine2);
52 return strcmp(strLine1.c_str(), strLine2.c_str()) < 0;
56 struct ExternalStreamInfo
62 ExternalStreamInfo() : flag(0){};
70 static void CleanString(const CStdString& strFileName, CStdString& strTitle, CStdString& strTitleAndYear, CStdString& strYear, bool bRemoveExtension = false, bool bCleanChars = true);
71 static CStdString GetTitleFromPath(const CStdString& strFileNameAndPath, bool bIsFolder = false);
72 static void GetQualifiedFilename(const CStdString &strBasePath, CStdString &strFilename);
73 static void RunShortcut(const char* szPath);
74 static void GetHomePath(CStdString& strPath, const CStdString& strTarget = "XBMC_HOME");
75 static bool IsPVR(const CStdString& strFile);
76 static bool IsHTSP(const CStdString& strFile);
77 static bool IsLiveTV(const CStdString& strFile);
78 static bool IsTVRecording(const CStdString& strFile);
79 static bool ExcludeFileOrFolder(const CStdString& strFileOrFolder, const CStdStringArray& regexps);
80 static void GetFileAndProtocol(const CStdString& strURL, CStdString& strDir);
81 static int GetDVDIfoTitle(const CStdString& strPathFile);
83 static bool IsPicture(const CStdString& strFile);
85 /*! \brief retrieve MD5sum of a file
86 \param strPath - path to the file to MD5sum
87 \return md5 sum of the file
89 static CStdString GetFileMD5(const CStdString& strPath);
90 static bool GetDirectoryName(const CStdString& strFileName, CStdString& strDescription);
91 static void GetDVDDriveIcon( const CStdString& strPath, CStdString& strIcon );
92 static void RemoveTempFiles();
93 static void ClearTempFonts();
95 static void ClearSubtitles();
96 static void ScanForExternalSubtitles(const CStdString& strMovie, std::vector<CStdString>& vecSubtitles );
97 static int ScanArchiveForSubtitles( const CStdString& strArchivePath, const CStdString& strMovieFileNameNoExt, std::vector<CStdString>& vecSubtitles );
98 static void GetExternalStreamDetailsFromFilename(const CStdString& strMovie, const CStdString& strSubtitles, ExternalStreamInfo& info);
99 static bool FindVobSubPair( const std::vector<CStdString>& vecSubtitles, const CStdString& strIdxPath, CStdString& strSubPath );
100 static bool IsVobSub( const std::vector<CStdString>& vecSubtitles, const CStdString& strSubPath );
101 static int64_t ToInt64(uint32_t high, uint32_t low);
102 static CStdString GetNextFilename(const CStdString &fn_template, int max);
103 static CStdString GetNextPathname(const CStdString &path_template, int max);
104 static void StatToStatI64(struct _stati64 *result, struct stat *stat);
105 static void Stat64ToStatI64(struct _stati64 *result, struct __stat64 *stat);
106 static void StatI64ToStat64(struct __stat64 *result, struct _stati64 *stat);
107 static void Stat64ToStat(struct stat *result, struct __stat64 *stat);
108 #ifdef TARGET_WINDOWS
109 static void Stat64ToStat64i32(struct _stat64i32 *result, struct __stat64 *stat);
111 static bool CreateDirectoryEx(const CStdString& strPath);
113 #ifdef TARGET_WINDOWS
114 static CStdString MakeLegalFileName(const CStdString &strFile, int LegalType=LEGAL_WIN32_COMPAT);
115 static CStdString MakeLegalPath(const CStdString &strPath, int LegalType=LEGAL_WIN32_COMPAT);
117 static CStdString MakeLegalFileName(const CStdString &strFile, int LegalType=LEGAL_NONE);
118 static CStdString MakeLegalPath(const CStdString &strPath, int LegalType=LEGAL_NONE);
120 static CStdString ValidatePath(const CStdString &path, bool bFixDoubleSlashes = false); ///< return a validated path, with correct directory separators.
122 static bool IsUsingTTFSubtitles();
124 /*! \brief Split a comma separated parameter list into separate parameters.
125 Takes care of the case where we may have a quoted string containing commas, or we may
126 have a function (i.e. parentheses) with multiple parameters as a single parameter.
130 foo, bar(param1, param2), foo
134 "foo", "bar(param1, param2)", and "foo".
136 \param paramString the string to break up
137 \param parameters the returned parameters
139 static void SplitParams(const CStdString ¶mString, std::vector<CStdString> ¶meters);
140 static void SplitExecFunction(const CStdString &execString, CStdString &function, std::vector<CStdString> ¶meters);
141 static int GetMatchingSource(const CStdString& strPath, VECSOURCES& VECSOURCES, bool& bIsSourceName);
142 static CStdString TranslateSpecialSource(const CStdString &strSpecial);
143 static void DeleteDirectoryCache(const CStdString &prefix = "");
144 static void DeleteMusicDatabaseDirectoryCache();
145 static void DeleteVideoDatabaseDirectoryCache();
146 static CStdString MusicPlaylistsLocation();
147 static CStdString VideoPlaylistsLocation();
149 static void GetSkinThemes(std::vector<CStdString>& vecTheme);
150 static void GetRecursiveListing(const CStdString& strPath, CFileItemList& items, const CStdString& strMask, bool bUseFileDirectories=false);
151 static void GetRecursiveDirsListing(const CStdString& strPath, CFileItemList& items);
152 static void ForceForwardSlashes(CStdString& strPath);
154 static double AlbumRelevance(const CStdString& strAlbumTemp1, const CStdString& strAlbum1, const CStdString& strArtistTemp1, const CStdString& strArtist1);
155 static bool MakeShortenPath(CStdString StrInput, CStdString& StrOutput, size_t iTextMaxLength);
156 /*! \brief Checks wether the supplied path supports Write file operations (e.g. Rename, Delete, ...)
158 \param strPath the path to be checked
160 \return true if Write file operations are supported, false otherwise
162 static bool SupportsWriteFileOperations(const CStdString& strPath);
163 /*! \brief Checks wether the supplied path supports Read file operations (e.g. Copy, ...)
165 \param strPath the path to be checked
167 \return true if Read file operations are supported, false otherwise
169 static bool SupportsReadFileOperations(const CStdString& strPath);
170 static CStdString GetDefaultFolderThumb(const CStdString &folderThumb);
173 static bool TestSplitExec();
174 static bool TestGetQualifiedFilename();
175 static bool TestMakeLegalPath();
178 static void InitRandomSeed();
180 // Get decimal integer representation of roman digit, ivxlcdm are valid
181 // return 0 for other chars;
182 static int LookupRomanDigit(char roman_digit);
183 // Translate a string of roman numerals to decimal a decimal integer
184 // return -1 on error, valid range is 1-3999
185 static int TranslateRomanNumeral(const char* roman_numeral);
188 // this will run the command using sudo in a new process.
189 // the user that runs xbmc should be allowed to issue the given sudo command.
190 // in order to allow a user to run sudo without supplying the password you'll need to edit sudoers
192 // and add a line at the end defining the user and allowed commands
193 static bool SudoCommand(const CStdString &strCommand);
196 // Forks to execute a shell command.
198 static bool Command(const CStdStringArray& arrArgs, bool waitExit = false);
201 // Forks to execute an unparsed shell command line.
203 static bool RunCommandLine(const CStdString& cmdLine, bool waitExit = false);
205 static CStdString ResolveExecutablePath();
206 static CStdString GetFrameworksPath(bool forPython = false);
208 static bool CanBindPrivileged();
209 static bool ValidatePort(int port);
212 * \brief Thread-safe random number generation
214 static int GetRandomNumber();
216 #if !defined(TARGET_WINDOWS)
218 static unsigned int s_randomSeed;