[WASAPI] fix stream types and frequencies enumeration
[xbmc.git] / xbmc / Util.h
blob74adf916cafd51bab45a9a2e3d4ddfb503ffb9f9
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "MediaSource.h" // Definition of VECSOURCES
12 #include "utils/Digest.h"
14 #include <climits>
15 #include <cmath>
16 #include <stdint.h>
17 #include <string.h>
18 #include <vector>
20 // A list of filesystem types for LegalPath/FileName
21 #define LEGAL_NONE 0
22 #define LEGAL_WIN32_COMPAT 1
23 #define LEGAL_FATX 2
25 class CFileItem;
26 class CFileItemList;
27 class CURL;
29 struct ExternalStreamInfo
31 std::string name;
32 std::string language;
33 unsigned int flag = 0;
36 class CUtil
38 CUtil() = delete;
39 public:
40 static void CleanString(const std::string& strFileName,
41 std::string& strTitle,
42 std::string& strTitleAndYear,
43 std::string& strYear,
44 bool bRemoveExtension = false,
45 bool bCleanChars = true);
46 static bool GetFilenameIdentifier(const std::string& fileName,
47 std::string& identifierType,
48 std::string& identifier);
49 static bool GetFilenameIdentifier(const std::string& fileName,
50 std::string& identifierType,
51 std::string& identifier,
52 std::string& match);
53 static bool HasFilenameIdentifier(const std::string& fileName);
54 static std::string GetTitleFromPath(const CURL& url, bool bIsFolder = false);
55 static std::string GetTitleFromPath(const std::string& strFileNameAndPath, bool bIsFolder = false);
57 /*! \brief Return the disc number in case the last segment of given path ends with 'Disc n'.
58 Will look for 'Disc', 'Disk' and the locale specific spelling.
59 \return the disc number as string if found, empty string otherwise.
61 static std::string GetDiscNumberFromPath(const std::string& path);
63 /*! \brief Remove last segment of the given path if it matches 'Disc n'.
64 Will look for 'Disc', 'Disk' and the locale specific spelling.
65 \return the given path with last segment removed if it matches 'Disc n', unchanged path otherwise.
67 static std::string RemoveTrailingDiscNumberSegmentFromPath(std::string path);
69 static void GetQualifiedFilename(const std::string &strBasePath, std::string &strFilename);
70 static void RunShortcut(const char* szPath);
71 static std::string GetHomePath(
72 const std::string& strTarget = "KODI_HOME"); // default target is "KODI_HOME"
73 static bool ExcludeFileOrFolder(const std::string& strFileOrFolder, const std::vector<std::string>& regexps);
74 static void GetFileAndProtocol(const std::string& strURL, std::string& strDir);
75 static int GetDVDIfoTitle(const std::string& strPathFile);
77 static bool IsPicture(const std::string& strFile);
78 /// Get resolved filesystem location of splash image
79 static std::string GetSplashPath();
81 /*! \brief retrieve MD5sum of a file
82 \param strPath - path to the file to MD5sum
83 \return md5 sum of the file
85 static std::string GetFileDigest(const std::string& strPath, KODI::UTILITY::CDigest::Type type);
86 static bool GetDirectoryName(const std::string& strFileName, std::string& strDescription);
87 static void GetDVDDriveIcon(const std::string& strPath, std::string& strIcon);
88 static void RemoveTempFiles();
90 static void ClearSubtitles();
91 static void ScanForExternalSubtitles(const std::string& strMovie, std::vector<std::string>& vecSubtitles );
93 /** \brief Retrieves stream info of external associated files, e.g., subtitles, for a given video.
94 * \param[in] videoPath The full path of the video file.
95 * \param[in] associatedFile A file that provides additional streams for the given video file.
96 * \return stream info for the given associatedFile
98 static ExternalStreamInfo GetExternalStreamDetailsFromFilename(const std::string& videoPath, const std::string& associatedFile);
99 static bool FindVobSubPair( const std::vector<std::string>& vecSubtitles, const std::string& strIdxPath, std::string& strSubPath );
100 static bool IsVobSub(const std::vector<std::string>& vecSubtitles, const std::string& strSubPath);
101 static std::string GetVobSubSubFromIdx(const std::string& vobSubIdx);
102 static std::string GetVobSubIdxFromSub(const std::string& vobSub);
104 /** \brief Retrieves paths of external audio files for a given video.
105 * \param[in] videoPath The full path of the video file.
106 * \param[out] vecAudio A vector containing the full paths of all found external audio files.
108 static void ScanForExternalAudio(const std::string& videoPath, std::vector<std::string>& vecAudio);
109 static int64_t ToInt64(uint32_t high, uint32_t low);
110 static std::string GetNextFilename(const std::string &fn_template, int max);
111 static std::string GetNextPathname(const std::string &path_template, int max);
112 static void StatToStatI64(struct _stati64 *result, struct stat *stat);
113 static void StatToStat64(struct __stat64 *result, const struct stat *stat);
114 static void Stat64ToStatI64(struct _stati64 *result, struct __stat64 *stat);
115 static void StatI64ToStat64(struct __stat64 *result, struct _stati64 *stat);
116 static void Stat64ToStat(struct stat *result, struct __stat64 *stat);
117 #ifdef TARGET_WINDOWS
118 static void Stat64ToStat64i32(struct _stat64i32 *result, struct __stat64 *stat);
119 #endif
120 static bool CreateDirectoryEx(const std::string& strPath);
122 #ifdef TARGET_WINDOWS
123 static std::string MakeLegalFileName(std::string strFile, int LegalType = LEGAL_WIN32_COMPAT);
124 static std::string MakeLegalPath(std::string strPath, int LegalType = LEGAL_WIN32_COMPAT);
125 #else
126 static std::string MakeLegalFileName(std::string strFile, int LegalType = LEGAL_NONE);
127 static std::string MakeLegalPath(std::string strPath, int LegalType = LEGAL_NONE);
128 #endif
129 static std::string ValidatePath(
130 std::string path,
131 bool bFixDoubleSlashes =
132 false); ///< return a validated path, with correct directory separators.
135 * \brief Check if a filename contains a supported font extension.
136 * \param filename The filename to check
137 * \return True if it is supported, otherwise false
139 static bool IsSupportedFontExtension(const std::string& fileName);
141 /*! \brief Split a comma separated parameter list into separate parameters.
142 Takes care of the case where we may have a quoted string containing commas, or we may
143 have a function (i.e. parentheses) with multiple parameters as a single parameter.
147 foo, bar(param1, param2), foo
149 will return:
151 "foo", "bar(param1, param2)", and "foo".
153 \param paramString the string to break up
154 \param parameters the returned parameters
156 static void SplitParams(const std::string& paramString, std::vector<std::string>& parameters);
157 static int GetMatchingSource(const std::string& strPath, VECSOURCES& VECSOURCES, bool& bIsSourceName);
158 static std::string TranslateSpecialSource(const std::string &strSpecial);
159 static void DeleteDirectoryCache(const std::string &prefix = "");
160 static void DeleteMusicDatabaseDirectoryCache();
161 static void DeleteVideoDatabaseDirectoryCache();
162 static std::string MusicPlaylistsLocation();
163 static std::string VideoPlaylistsLocation();
165 static void GetSkinThemes(std::vector<std::string>& vecTheme);
166 static void GetRecursiveListing(const std::string& strPath, CFileItemList& items, const std::string& strMask, unsigned int flags = 0 /* DIR_FLAG_DEFAULTS */);
167 static void GetRecursiveDirsListing(const std::string& strPath, CFileItemList& items, unsigned int flags = 0 /* DIR_FLAG_DEFAULTS */);
168 static void ForceForwardSlashes(std::string& strPath);
170 static double AlbumRelevance(const std::string& strAlbumTemp1, const std::string& strAlbum1, const std::string& strArtistTemp1, const std::string& strArtist1);
171 static bool MakeShortenPath(std::string StrInput, std::string& StrOutput, size_t iTextMaxLength);
172 /*! \brief Checks whether the supplied path supports Write file operations (e.g. Rename, Delete, ...)
174 \param strPath the path to be checked
176 \return true if Write file operations are supported, false otherwise
178 static bool SupportsWriteFileOperations(const std::string& strPath);
179 /*! \brief Checks whether the supplied path supports Read file operations (e.g. Copy, ...)
181 \param strPath the path to be checked
183 \return true if Read file operations are supported, false otherwise
185 static bool SupportsReadFileOperations(const std::string& strPath);
186 static std::string GetDefaultFolderThumb(const std::string &folderThumb);
188 static void InitRandomSeed();
190 // Get decimal integer representation of roman digit, ivxlcdm are valid
191 // return 0 for other chars;
192 static int LookupRomanDigit(char roman_digit);
193 // Translate a string of roman numerals to decimal a decimal integer
194 // return -1 on error, valid range is 1-3999
195 static int TranslateRomanNumeral(const char* roman_numeral);
197 #if defined(TARGET_POSIX) && !defined(TARGET_DARWIN_TVOS)
199 // Forks to execute a shell command.
201 static bool Command(const std::vector<std::string>& arrArgs, bool waitExit = false);
204 // Forks to execute an unparsed shell command line.
206 static bool RunCommandLine(const std::string& cmdLine, bool waitExit = false);
207 #endif
208 static std::string ResolveExecutablePath();
209 static std::string GetFrameworksPath(bool forPython = false);
211 static bool CanBindPrivileged();
212 static bool ValidatePort(int port);
215 * \brief Thread-safe random number generation
217 static int GetRandomNumber();
219 static int64_t ConvertSecsToMilliSecs(double secs) { return static_cast<int64_t>(secs * 1000); }
220 static double ConvertMilliSecsToSecs(int64_t offset) { return offset / 1000.0; }
221 static int64_t ConvertMilliSecsToSecsInt(int64_t offset) { return offset / 1000; }
222 static int64_t ConvertMilliSecsToSecsIntRounded(int64_t offset) { return ConvertMilliSecsToSecsInt(offset + 499); }
224 /** \brief Copy files from the application bundle over to the user data directory in Application Support/Kodi.
226 static void CopyUserDataIfNeeded(const std::string& strPath,
227 const std::string& file,
228 const std::string& destname = "");
230 #if !defined(TARGET_WINDOWS)
231 private:
232 static unsigned int s_randomSeed;
233 #endif
235 protected:
236 /** \brief Retrieves the base path and the filename of a given video.
237 * \param[in] videoPath The full path of the video file.
238 * \param[out] basePath The base path of the given video.
239 * \param[out] videoFileName The file name of the given video..
241 static void GetVideoBasePathAndFileName(const std::string& videoPath,
242 std::string& basePath,
243 std::string& videoFileName);
245 /** \brief Retrieves FileItems that could contain associated files of a given video.
246 * \param[in] videoPath The full path of the video file.
247 * \param[in] item_exts A | separated string of extensions specifying the associated files.
248 * \param[in] sub_dirs A vector of sub directory names to look for.
249 * \param[out] items A List of FileItems to scan for associated files.
251 static void GetItemsToScan(const std::string& videoPath,
252 const std::string& item_exts,
253 const std::vector<std::string>& sub_dirs,
254 CFileItemList& items);
256 /** \brief Searches for associated files of a given video.
257 * \param[in] videoName The name of the video file.
258 * \param[in] items A List of FileItems to scan for associated files.
259 * \param[in] item_exts A vector of extensions specifying the associated files.
260 * \param[out] associatedFiles A vector containing the full paths of all found associated files.
262 static void ScanPathsForAssociatedItems(const std::string& videoName,
263 const CFileItemList& items,
264 const std::vector<std::string>& item_exts,
265 std::vector<std::string>& associatedFiles);
267 /** \brief Searches in an archive for associated files of a given video.
268 * \param[in] strArchivePath The full path of the archive.
269 * \param[in] videoNameNoExt The filename of the video without extension for which associated files should be retrieved.
270 * \param[in] item_exts A vector of extensions specifying the associated files.
271 * \param[out] associatedFiles A vector containing the full paths of all found associated files.
273 static int ScanArchiveForAssociatedItems(const std::string& strArchivePath,
274 const std::string& videoNameNoExt,
275 const std::vector<std::string>& item_exts,
276 std::vector<std::string>& associatedFiles);