kodi
Util.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "MediaSource.h" // Definition of VECSOURCES
12 #include "utils/Digest.h"
13 
14 #include <climits>
15 #include <cmath>
16 #include <stdint.h>
17 #include <string.h>
18 #include <vector>
19 
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
24 
25 class CFileItem;
26 class CFileItemList;
27 class CURL;
28 
30 {
31  std::string name;
32  std::string language;
33  unsigned int flag = 0;
34 };
35 
36 class CUtil
37 {
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);
56 
61  static std::string GetDiscNumberFromPath(const std::string& path);
62 
67  static std::string RemoveTrailingDiscNumberSegmentFromPath(std::string path);
68 
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);
76 
77  static bool IsPicture(const std::string& strFile);
79  static std::string GetSplashPath();
80 
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();
89 
90  static void ClearSubtitles();
91  static void ScanForExternalSubtitles(const std::string& strMovie, std::vector<std::string>& vecSubtitles );
92 
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);
103 
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);
121 
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);
133 
139  static bool IsSupportedFontExtension(const std::string& fileName);
140 
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();
164 
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);
169 
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);
178  static bool SupportsWriteFileOperations(const std::string& strPath);
185  static bool SupportsReadFileOperations(const std::string& strPath);
186  static std::string GetDefaultFolderThumb(const std::string &folderThumb);
187 
188  static void InitRandomSeed();
189 
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);
196 
197 #if defined(TARGET_POSIX) && !defined(TARGET_DARWIN_TVOS)
198  //
199  // Forks to execute a shell command.
200  //
201  static bool Command(const std::vector<std::string>& arrArgs, bool waitExit = false);
202 
203  //
204  // Forks to execute an unparsed shell command line.
205  //
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);
210 
211  static bool CanBindPrivileged();
212  static bool ValidatePort(int port);
213 
217  static int GetRandomNumber();
218 
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); }
223 
226  static void CopyUserDataIfNeeded(const std::string& strPath,
227  const std::string& file,
228  const std::string& destname = "");
229 
230 #if !defined(TARGET_WINDOWS)
231 private:
232  static unsigned int s_randomSeed;
233 #endif
234 
235  protected:
241  static void GetVideoBasePathAndFileName(const std::string& videoPath,
242  std::string& basePath,
243  std::string& videoFileName);
244 
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);
255 
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);
266 
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);
277 
278 };
279 
280 
Represents a list of files.
Definition: FileItem.h:702
Definition: URL.h:21
Definition: Util.h:36
Definition: PlatformDefs.h:120
Definition: Util.h:29
Represents a file on a share.
Definition: FileItem.h:102