diff options
Diffstat (limited to 'xbmc/addons/kodi-dev-kit/include/kodi/Filesystem.h')
| -rw-r--r-- | xbmc/addons/kodi-dev-kit/include/kodi/Filesystem.h | 2367 |
1 files changed, 2367 insertions, 0 deletions
diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/Filesystem.h b/xbmc/addons/kodi-dev-kit/include/kodi/Filesystem.h new file mode 100644 index 0000000..1cf05fe --- /dev/null +++ b/xbmc/addons/kodi-dev-kit/include/kodi/Filesystem.h | |||
| @@ -0,0 +1,2367 @@ | |||
| 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 "AddonBase.h" | ||
| 12 | #include "c-api/filesystem.h" | ||
| 13 | |||
| 14 | #ifdef __cplusplus | ||
| 15 | |||
| 16 | #include <cstring> | ||
| 17 | #include <map> | ||
| 18 | #include <vector> | ||
| 19 | |||
| 20 | namespace kodi | ||
| 21 | { | ||
| 22 | namespace vfs | ||
| 23 | { | ||
| 24 | |||
| 25 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ | ||
| 26 | // Main page text for filesystem group by Doxygen. | ||
| 27 | //{{{ | ||
| 28 | |||
| 29 | //============================================================================== | ||
| 30 | /// | ||
| 31 | /// @defgroup cpp_kodi_vfs Interface - kodi::vfs | ||
| 32 | /// @ingroup cpp | ||
| 33 | /// @brief **Virtual filesystem functions**\n | ||
| 34 | /// Offers classes and functions for access to the Virtual File Server (VFS) | ||
| 35 | /// which you can use to manipulate files and folders. | ||
| 36 | /// | ||
| 37 | /// This system allow the use of ["Special Protocol"](https://kodi.wiki/view/Special_protocol) | ||
| 38 | /// where is Kodi's solution to platform dependent directories. Common directory | ||
| 39 | /// names are assigned a <b>`special://[name]`</b> path which is passed around | ||
| 40 | /// inside Kodi and then translated to the platform specific path before the | ||
| 41 | /// operating system sees it. This helps keep most of the platform mess | ||
| 42 | /// centralized in the code.\n | ||
| 43 | /// To become a correct path back can be @ref TranslateSpecialProtocol() used. | ||
| 44 | /// | ||
| 45 | /// It has the header @ref Filesystem.h "#include <kodi/Filesystem.h>" be | ||
| 46 | /// included to enjoy it. | ||
| 47 | /// | ||
| 48 | //------------------------------------------------------------------------------ | ||
| 49 | |||
| 50 | //============================================================================== | ||
| 51 | /// @defgroup cpp_kodi_vfs_Defs Definitions, structures and enumerators | ||
| 52 | /// @ingroup cpp_kodi_vfs | ||
| 53 | /// @brief **Virtual file Server definition values**\n | ||
| 54 | /// All to VFS system functions associated data structures. | ||
| 55 | /// | ||
| 56 | //------------------------------------------------------------------------------ | ||
| 57 | |||
| 58 | //============================================================================== | ||
| 59 | /// @defgroup cpp_kodi_vfs_Directory 1. Directory functions | ||
| 60 | /// @ingroup cpp_kodi_vfs | ||
| 61 | /// @brief **Globally available directories related functions**\n | ||
| 62 | /// Used to perform typical operations with it. | ||
| 63 | /// | ||
| 64 | //------------------------------------------------------------------------------ | ||
| 65 | |||
| 66 | //============================================================================== | ||
| 67 | /// @defgroup cpp_kodi_vfs_File 2. File functions | ||
| 68 | /// @ingroup cpp_kodi_vfs | ||
| 69 | /// @brief **Globally available file related functions**\n | ||
| 70 | /// Used to perform typical operations with it. | ||
| 71 | /// | ||
| 72 | //------------------------------------------------------------------------------ | ||
| 73 | |||
| 74 | //============================================================================== | ||
| 75 | /// @defgroup cpp_kodi_vfs_General 3. General functions | ||
| 76 | /// @ingroup cpp_kodi_vfs | ||
| 77 | /// @brief **Other globally available functions**\n | ||
| 78 | /// Used to perform typical operations with it. | ||
| 79 | /// | ||
| 80 | //------------------------------------------------------------------------------ | ||
| 81 | |||
| 82 | //}}} | ||
| 83 | |||
| 84 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ | ||
| 85 | // "C++" related filesystem definitions | ||
| 86 | //{{{ | ||
| 87 | |||
| 88 | //============================================================================== | ||
| 89 | /// @defgroup cpp_kodi_vfs_Defs_FileStatus class FileStatus | ||
| 90 | /// @ingroup cpp_kodi_vfs_Defs | ||
| 91 | /// @brief **File information status**\n | ||
| 92 | /// Used on kodi::vfs::StatFile() to get detailed information about a file. | ||
| 93 | /// | ||
| 94 | //@{ | ||
| 95 | class ATTRIBUTE_HIDDEN FileStatus : public kodi::addon::CStructHdl<FileStatus, STAT_STRUCTURE> | ||
| 96 | { | ||
| 97 | public: | ||
| 98 | /*! \cond PRIVATE */ | ||
| 99 | FileStatus() { memset(m_cStructure, 0, sizeof(STAT_STRUCTURE)); } | ||
| 100 | FileStatus(const FileStatus& channel) : CStructHdl(channel) {} | ||
| 101 | FileStatus(const STAT_STRUCTURE* channel) : CStructHdl(channel) {} | ||
| 102 | FileStatus(STAT_STRUCTURE* channel) : CStructHdl(channel) {} | ||
| 103 | /*! \endcond */ | ||
| 104 | |||
| 105 | /// @defgroup cpp_kodi_vfs_Defs_FileStatus_Help *Value Help* | ||
| 106 | /// @ingroup cpp_kodi_vfs_Defs_FileStatus | ||
| 107 | /// ---------------------------------------------------------------------------- | ||
| 108 | /// | ||
| 109 | /// <b>The following table contains values that can be set with @ref cpp_kodi_vfs_Defs_FileStatus :</b> | ||
| 110 | /// | Name | Type | Set call | Get call | ||
| 111 | /// |------|------|----------|---------- | ||
| 112 | /// | **ID of device containing file** | `uint32_t` | @ref FileStatus::SetDeviceId "SetDeviceId" | @ref FileStatus::GetDeviceId "GetDeviceId" | ||
| 113 | /// | **Represent file serial numbers** | `uint64_t` | @ref FileStatus::SetFileSerialNumber "SetFileSerialNumber" | @ref FileStatus::GetFileSerialNumber "GetFileSerialNumber" | ||
| 114 | /// | **Total size, in bytes** | `uint64_t` | @ref FileStatus::SetSize "SetSize" | @ref FileStatus::GetSize "GetSize" | ||
| 115 | /// | **Time of last access** | `time_t` | @ref FileStatus::SetAccessTime "SetAccessTime" | @ref FileStatus::GetAccessTime "GetAccessTime" | ||
| 116 | /// | **Time of last modification** | `time_t` | @ref FileStatus::SetModificationTime "SetModificationTime" | @ref FileStatus::GetModificationTime "GetModificationTime" | ||
| 117 | /// | **Time of last status change** | `time_t` | @ref FileStatus::SetStatusTime "SetStatusTime" | @ref FileStatus::GetStatusTime "GetStatusTime" | ||
| 118 | /// | **Stat url is a directory** | `bool` | @ref FileStatus::SetIsDirectory "SetIsDirectory" | @ref FileStatus::GetIsDirectory "GetIsDirectory" | ||
| 119 | /// | **Stat url as a symbolic link** | `bool` | @ref FileStatus::SetIsSymLink "SetIsSymLink" | @ref FileStatus::GetIsSymLink "GetIsSymLink" | ||
| 120 | /// | **Stat url as a block special** | `bool` | @ref FileStatus::SetIsBlock "SetIsBlock" | @ref FileStatus::GetIsBlock "GetIsBlock" | ||
| 121 | /// | **Stat url as a character special** | `bool` | @ref FileStatus::SetIsCharacter "SetIsCharacter" | @ref FileStatus::GetIsCharacter "GetIsCharacter" | ||
| 122 | /// | **Stat url as a FIFO special** | `bool` | @ref FileStatus::SetIsFifo "SetIsFifo" | @ref FileStatus::GetIsFifo "GetIsFifo" | ||
| 123 | /// | **Stat url as a regular** | `bool` | @ref FileStatus::SetIsRegular "SetIsRegular" | @ref FileStatus::GetIsRegular "GetIsRegular" | ||
| 124 | /// | **Stat url as a socket** | `bool` | @ref FileStatus::SetIsSocket "SetIsSocket" | @ref FileStatus::GetIsSocket "GetIsSocket" | ||
| 125 | /// | ||
| 126 | |||
| 127 | /// @addtogroup cpp_kodi_vfs_Defs_FileStatus | ||
| 128 | /// @copydetails cpp_kodi_vfs_Defs_FileStatus_Help | ||
| 129 | //@{ | ||
| 130 | |||
| 131 | /// @brief Set ID of device containing file. | ||
| 132 | void SetDeviceId(uint32_t deviceId) { m_cStructure->deviceId = deviceId; } | ||
| 133 | |||
| 134 | /// @brief Get ID of device containing file. | ||
| 135 | uint32_t GetDeviceId() const { return m_cStructure->deviceId; } | ||
| 136 | |||
| 137 | /// @brief Set the file serial number, which distinguishes this file from all other files on the same device. | ||
| 138 | void SetFileSerialNumber(uint64_t fileSerialNumber) { m_cStructure->fileSerialNumber = fileSerialNumber; } | ||
| 139 | |||
| 140 | /// @brief Get the file serial number, which distinguishes this file from all other files on the same device. | ||
| 141 | uint64_t GetFileSerialNumber() const { return m_cStructure->fileSerialNumber; } | ||
| 142 | |||
| 143 | /// @brief Set total size, in bytes. | ||
| 144 | void SetSize(uint64_t size) { m_cStructure->size = size; } | ||
| 145 | |||
| 146 | /// @brief Get total size, in bytes. | ||
| 147 | uint64_t GetSize() const { return m_cStructure->size; } | ||
| 148 | |||
| 149 | /// @brief Set time of last access. | ||
| 150 | void SetAccessTime(time_t accessTime) { m_cStructure->accessTime = accessTime; } | ||
| 151 | |||
| 152 | /// @brief Get time of last access. | ||
| 153 | time_t GetAccessTime() const { return m_cStructure->accessTime; } | ||
| 154 | |||
| 155 | /// @brief Set time of last modification. | ||
| 156 | void SetModificationTime(time_t modificationTime) | ||
| 157 | { | ||
| 158 | m_cStructure->modificationTime = modificationTime; | ||
| 159 | } | ||
| 160 | |||
| 161 | /// @brief Get time of last modification. | ||
| 162 | time_t GetModificationTime() const { return m_cStructure->modificationTime; } | ||
| 163 | |||
| 164 | /// @brief Set time of last status change. | ||
| 165 | void SetStatusTime(time_t statusTime) { m_cStructure->statusTime = statusTime; } | ||
| 166 | |||
| 167 | /// @brief Get time of last status change. | ||
| 168 | time_t GetStatusTime() const { return m_cStructure->statusTime; } | ||
| 169 | |||
| 170 | /// @brief Set the stat url is a directory. | ||
| 171 | void SetIsDirectory(bool isDirectory) { m_cStructure->isDirectory = isDirectory; } | ||
| 172 | |||
| 173 | /// @brief The stat url is a directory if returns true. | ||
| 174 | bool GetIsDirectory() const { return m_cStructure->isDirectory; } | ||
| 175 | |||
| 176 | /// @brief Set stat url as a symbolic link. | ||
| 177 | void SetIsSymLink(bool isSymLink) { m_cStructure->isSymLink = isSymLink; } | ||
| 178 | |||
| 179 | /// @brief Get stat url is a symbolic link. | ||
| 180 | bool GetIsSymLink() const { return m_cStructure->isSymLink; } | ||
| 181 | |||
| 182 | /// @brief Set stat url as a block special. | ||
| 183 | void SetIsBlock(bool isBlock) { m_cStructure->isBlock = isBlock; } | ||
| 184 | |||
| 185 | /// @brief Get stat url is a block special. | ||
| 186 | bool GetIsBlock() const { return m_cStructure->isBlock; } | ||
| 187 | |||
| 188 | /// @brief Set stat url as a character special. | ||
| 189 | void SetIsCharacter(bool isCharacter) { m_cStructure->isCharacter = isCharacter; } | ||
| 190 | |||
| 191 | /// @brief Get stat url is a character special. | ||
| 192 | bool GetIsCharacter() const { return m_cStructure->isCharacter; } | ||
| 193 | |||
| 194 | /// @brief Set stat url as a FIFO special. | ||
| 195 | void SetIsFifo(bool isFifo) { m_cStructure->isFifo = isFifo; } | ||
| 196 | |||
| 197 | /// @brief Get stat url is a FIFO special. | ||
| 198 | bool GetIsFifo() const { return m_cStructure->isFifo; } | ||
| 199 | |||
| 200 | /// @brief Set stat url as a regular. | ||
| 201 | void SetIsRegular(bool isRegular) { m_cStructure->isRegular = isRegular; } | ||
| 202 | |||
| 203 | /// @brief Get stat url is a regular. | ||
| 204 | bool GetIsRegular() const { return m_cStructure->isRegular; } | ||
| 205 | |||
| 206 | /// @brief Set stat url is a socket. | ||
| 207 | void SetIsSocket(bool isSocket) { m_cStructure->isSocket = isSocket; } | ||
| 208 | |||
| 209 | /// @brief Get stat url is a regular. | ||
| 210 | bool GetIsSocket() const { return m_cStructure->isSocket; } | ||
| 211 | //@} | ||
| 212 | }; | ||
| 213 | //@} | ||
| 214 | //------------------------------------------------------------------------------ | ||
| 215 | |||
| 216 | //============================================================================== | ||
| 217 | /// @defgroup cpp_kodi_vfs_Defs_CacheStatus class CacheStatus | ||
| 218 | /// @ingroup cpp_kodi_vfs_Defs | ||
| 219 | /// @brief **Cache information status**\n | ||
| 220 | /// Used on kodi::vfs::CFile::IoControlGetCacheStatus() to get running cache | ||
| 221 | /// status of proccessed stream. | ||
| 222 | /// | ||
| 223 | //@{ | ||
| 224 | class ATTRIBUTE_HIDDEN CacheStatus | ||
| 225 | : public kodi::addon::CStructHdl<CacheStatus, VFS_CACHE_STATUS_DATA> | ||
| 226 | { | ||
| 227 | public: | ||
| 228 | /*! \cond PRIVATE */ | ||
| 229 | CacheStatus() { memset(m_cStructure, 0, sizeof(VFS_CACHE_STATUS_DATA)); } | ||
| 230 | CacheStatus(const CacheStatus& channel) : CStructHdl(channel) {} | ||
| 231 | CacheStatus(const VFS_CACHE_STATUS_DATA* channel) : CStructHdl(channel) {} | ||
| 232 | CacheStatus(VFS_CACHE_STATUS_DATA* channel) : CStructHdl(channel) {} | ||
| 233 | /*! \endcond */ | ||
| 234 | |||
| 235 | /// @defgroup cpp_kodi_vfs_Defs_CacheStatus_Help *Value Help* | ||
| 236 | /// @ingroup cpp_kodi_vfs_Defs_CacheStatus | ||
| 237 | /// ---------------------------------------------------------------------------- | ||
| 238 | /// | ||
| 239 | /// <b>The following table contains values that can be set with @ref cpp_kodi_vfs_Defs_CacheStatus :</b> | ||
| 240 | /// | Name | Type | Set call | Get call | ||
| 241 | /// |------|------|----------|---------- | ||
| 242 | /// | **Number of bytes cached** | `uint64_t` | @ref CacheStatus::SetForward "SetForward" | @ref CacheStatus::GetForward "GetForward" | ||
| 243 | /// | **Maximum number of bytes per second** | `unsigned int` | @ref CacheStatus::SetMaxRate "SetMaxRate" | @ref CacheStatus::GetMaxRate "GetMaxRate" | ||
| 244 | /// | **Average read rate from source file** | `unsigned int` | @ref CacheStatus::SetCurrentRate "SetCurrentRate" | @ref CacheStatus::GetCurrentRate "GetCurrentRate" | ||
| 245 | /// | **Cache low speed condition detected** | `bool` | @ref CacheStatus::SetLowspeed "SetLowspeed" | @ref CacheStatus::GetLowspeed "GetLowspeed" | ||
| 246 | /// | ||
| 247 | |||
| 248 | /// @addtogroup cpp_kodi_vfs_Defs_CacheStatus | ||
| 249 | /// @copydetails cpp_kodi_vfs_Defs_CacheStatus_Help | ||
| 250 | //@{ | ||
| 251 | |||
| 252 | /// @brief Set number of bytes cached forward of current position. | ||
| 253 | void SetForward(uint64_t forward) { m_cStructure->forward = forward; } | ||
| 254 | |||
| 255 | /// @brief Get number of bytes cached forward of current position. | ||
| 256 | uint64_t GetForward() { return m_cStructure->forward; } | ||
| 257 | |||
| 258 | /// @brief Set maximum number of bytes per second cache is allowed to fill. | ||
| 259 | void SetMaxRate(unsigned int maxrate) { m_cStructure->maxrate = maxrate; } | ||
| 260 | |||
| 261 | /// @brief Set maximum number of bytes per second cache is allowed to fill. | ||
| 262 | unsigned int GetMaxRate() { return m_cStructure->maxrate; } | ||
| 263 | |||
| 264 | /// @brief Set average read rate from source file since last position change. | ||
| 265 | void SetCurrentRate(unsigned int currate) { m_cStructure->currate = currate; } | ||
| 266 | |||
| 267 | /// @brief Get average read rate from source file since last position change. | ||
| 268 | unsigned int GetCurrentRate() { return m_cStructure->currate; } | ||
| 269 | |||
| 270 | /// @brief Set cache low speed condition detected. | ||
| 271 | void SetLowspeed(bool lowspeed) { m_cStructure->lowspeed = lowspeed; } | ||
| 272 | |||
| 273 | /// @brief Get cache low speed condition detected. | ||
| 274 | bool GetLowspeed() { return m_cStructure->lowspeed; } | ||
| 275 | |||
| 276 | //@} | ||
| 277 | }; | ||
| 278 | //@} | ||
| 279 | //------------------------------------------------------------------------------ | ||
| 280 | |||
| 281 | //============================================================================== | ||
| 282 | /// @defgroup cpp_kodi_vfs_Defs_HttpHeader class HttpHeader | ||
| 283 | /// @ingroup cpp_kodi_vfs_Defs | ||
| 284 | /// @brief **HTTP header information**\n | ||
| 285 | /// The class used to access HTTP header information and get his information. | ||
| 286 | /// | ||
| 287 | /// Used on @ref kodi::vfs::GetHttpHeader(). | ||
| 288 | /// | ||
| 289 | /// ---------------------------------------------------------------------------- | ||
| 290 | /// | ||
| 291 | /// @copydetails cpp_kodi_vfs_Defs_HttpHeader_Help | ||
| 292 | /// | ||
| 293 | ///@{ | ||
| 294 | class ATTRIBUTE_HIDDEN HttpHeader | ||
| 295 | { | ||
| 296 | public: | ||
| 297 | //========================================================================== | ||
| 298 | /// @brief Http header parser class constructor. | ||
| 299 | /// | ||
| 300 | HttpHeader() | ||
| 301 | { | ||
| 302 | using namespace ::kodi::addon; | ||
| 303 | |||
| 304 | CAddonBase::m_interface->toKodi->kodi_filesystem->http_header_create( | ||
| 305 | CAddonBase::m_interface->toKodi->kodiBase, &m_handle); | ||
| 306 | } | ||
| 307 | //-------------------------------------------------------------------------- | ||
| 308 | |||
| 309 | //========================================================================== | ||
| 310 | /// @brief Class destructor. | ||
| 311 | /// | ||
| 312 | ~HttpHeader() | ||
| 313 | { | ||
| 314 | using namespace ::kodi::addon; | ||
| 315 | |||
| 316 | CAddonBase::m_interface->toKodi->kodi_filesystem->http_header_free( | ||
| 317 | CAddonBase::m_interface->toKodi->kodiBase, &m_handle); | ||
| 318 | } | ||
| 319 | //-------------------------------------------------------------------------- | ||
| 320 | |||
| 321 | /// @defgroup cpp_kodi_vfs_Defs_HttpHeader_Help *Value Help* | ||
| 322 | /// @ingroup cpp_kodi_vfs_Defs_HttpHeader | ||
| 323 | /// | ||
| 324 | /// <b>The following table contains values that can be get with @ref cpp_kodi_vfs_Defs_HttpHeader :</b> | ||
| 325 | /// | Description | Type | Get call | ||
| 326 | /// |-------------|------|------------ | ||
| 327 | /// | **Get the value associated with this parameter of these HTTP headers** | `std::string` | @ref HttpHeader::GetValue "GetValue" | ||
| 328 | /// | **Get the values as list associated with this parameter of these HTTP headers** | `std::vector<std::string>` | @ref HttpHeader::GetValues "GetValues" | ||
| 329 | /// | **Get the full header string associated with these HTTP headers** | `std::string` | @ref HttpHeader::GetHeader "GetHeader" | ||
| 330 | /// | **Get the mime type associated with these HTTP headers** | `std::string` | @ref HttpHeader::GetMimeType "GetMimeType" | ||
| 331 | /// | **Get the charset associated with these HTTP headers** | `std::string` | @ref HttpHeader::GetCharset "GetCharset" | ||
| 332 | /// | **The protocol line associated with these HTTP headers** | `std::string` | @ref HttpHeader::GetProtoLine "GetProtoLine" | ||
| 333 | /// | ||
| 334 | |||
| 335 | /// @addtogroup cpp_kodi_vfs_Defs_HttpHeader | ||
| 336 | ///@{ | ||
| 337 | |||
| 338 | //========================================================================== | ||
| 339 | /// @brief Get the value associated with this parameter of these HTTP | ||
| 340 | /// headers. | ||
| 341 | /// | ||
| 342 | /// @param[in] param The name of the parameter a value is required for | ||
| 343 | /// @return The value found | ||
| 344 | /// | ||
| 345 | std::string GetValue(const std::string& param) const | ||
| 346 | { | ||
| 347 | using namespace ::kodi::addon; | ||
| 348 | |||
| 349 | if (!m_handle.handle) | ||
| 350 | return ""; | ||
| 351 | |||
| 352 | std::string protoLine; | ||
| 353 | char* string = m_handle.get_value(CAddonBase::m_interface->toKodi->kodiBase, m_handle.handle, | ||
| 354 | param.c_str()); | ||
| 355 | if (string != nullptr) | ||
| 356 | { | ||
| 357 | protoLine = string; | ||
| 358 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 359 | string); | ||
| 360 | } | ||
| 361 | return protoLine; | ||
| 362 | } | ||
| 363 | //-------------------------------------------------------------------------- | ||
| 364 | |||
| 365 | //========================================================================== | ||
| 366 | /// @brief Get the values as list associated with this parameter of these | ||
| 367 | /// HTTP headers. | ||
| 368 | /// | ||
| 369 | /// @param[in] param The name of the parameter values are required for | ||
| 370 | /// @return The values found | ||
| 371 | /// | ||
| 372 | std::vector<std::string> GetValues(const std::string& param) const | ||
| 373 | { | ||
| 374 | using namespace kodi::addon; | ||
| 375 | |||
| 376 | if (!m_handle.handle) | ||
| 377 | return std::vector<std::string>(); | ||
| 378 | |||
| 379 | int numValues = 0; | ||
| 380 | char** res(m_handle.get_values(CAddonBase::m_interface->toKodi->kodiBase, m_handle.handle, | ||
| 381 | param.c_str(), &numValues)); | ||
| 382 | if (res) | ||
| 383 | { | ||
| 384 | std::vector<std::string> vecReturn; | ||
| 385 | for (int i = 0; i < numValues; ++i) | ||
| 386 | { | ||
| 387 | vecReturn.emplace_back(res[i]); | ||
| 388 | } | ||
| 389 | CAddonBase::m_interface->toKodi->free_string_array(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 390 | res, numValues); | ||
| 391 | return vecReturn; | ||
| 392 | } | ||
| 393 | return std::vector<std::string>(); | ||
| 394 | } | ||
| 395 | //-------------------------------------------------------------------------- | ||
| 396 | |||
| 397 | //========================================================================== | ||
| 398 | /// @brief Get the full header string associated with these HTTP headers. | ||
| 399 | /// | ||
| 400 | /// @return The header as a string | ||
| 401 | /// | ||
| 402 | std::string GetHeader() const | ||
| 403 | { | ||
| 404 | using namespace ::kodi::addon; | ||
| 405 | |||
| 406 | if (!m_handle.handle) | ||
| 407 | return ""; | ||
| 408 | |||
| 409 | std::string header; | ||
| 410 | char* string = m_handle.get_header(CAddonBase::m_interface->toKodi->kodiBase, m_handle.handle); | ||
| 411 | if (string != nullptr) | ||
| 412 | { | ||
| 413 | header = string; | ||
| 414 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 415 | string); | ||
| 416 | } | ||
| 417 | return header; | ||
| 418 | } | ||
| 419 | //-------------------------------------------------------------------------- | ||
| 420 | |||
| 421 | //========================================================================== | ||
| 422 | /// @brief Get the mime type associated with these HTTP headers. | ||
| 423 | /// | ||
| 424 | /// @return The mime type | ||
| 425 | /// | ||
| 426 | std::string GetMimeType() const | ||
| 427 | { | ||
| 428 | using namespace ::kodi::addon; | ||
| 429 | |||
| 430 | if (!m_handle.handle) | ||
| 431 | return ""; | ||
| 432 | |||
| 433 | std::string protoLine; | ||
| 434 | char* string = | ||
| 435 | m_handle.get_mime_type(CAddonBase::m_interface->toKodi->kodiBase, m_handle.handle); | ||
| 436 | if (string != nullptr) | ||
| 437 | { | ||
| 438 | protoLine = string; | ||
| 439 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 440 | string); | ||
| 441 | } | ||
| 442 | return protoLine; | ||
| 443 | } | ||
| 444 | //-------------------------------------------------------------------------- | ||
| 445 | |||
| 446 | //========================================================================== | ||
| 447 | /// @brief Get the charset associated with these HTTP headers. | ||
| 448 | /// | ||
| 449 | /// @return The charset | ||
| 450 | /// | ||
| 451 | std::string GetCharset() const | ||
| 452 | { | ||
| 453 | using namespace ::kodi::addon; | ||
| 454 | |||
| 455 | if (!m_handle.handle) | ||
| 456 | return ""; | ||
| 457 | |||
| 458 | std::string protoLine; | ||
| 459 | char* string = m_handle.get_charset(CAddonBase::m_interface->toKodi->kodiBase, m_handle.handle); | ||
| 460 | if (string != nullptr) | ||
| 461 | { | ||
| 462 | protoLine = string; | ||
| 463 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 464 | string); | ||
| 465 | } | ||
| 466 | return protoLine; | ||
| 467 | } | ||
| 468 | //-------------------------------------------------------------------------- | ||
| 469 | |||
| 470 | //========================================================================== | ||
| 471 | /// @brief The protocol line associated with these HTTP headers. | ||
| 472 | /// | ||
| 473 | /// @return The protocol line | ||
| 474 | /// | ||
| 475 | std::string GetProtoLine() const | ||
| 476 | { | ||
| 477 | using namespace ::kodi::addon; | ||
| 478 | |||
| 479 | if (!m_handle.handle) | ||
| 480 | return ""; | ||
| 481 | |||
| 482 | std::string protoLine; | ||
| 483 | char* string = | ||
| 484 | m_handle.get_proto_line(CAddonBase::m_interface->toKodi->kodiBase, m_handle.handle); | ||
| 485 | if (string != nullptr) | ||
| 486 | { | ||
| 487 | protoLine = string; | ||
| 488 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 489 | string); | ||
| 490 | } | ||
| 491 | return protoLine; | ||
| 492 | } | ||
| 493 | //-------------------------------------------------------------------------- | ||
| 494 | |||
| 495 | ///@} | ||
| 496 | |||
| 497 | KODI_HTTP_HEADER m_handle; | ||
| 498 | }; | ||
| 499 | ///@} | ||
| 500 | //---------------------------------------------------------------------------- | ||
| 501 | |||
| 502 | //============================================================================== | ||
| 503 | /// @defgroup cpp_kodi_vfs_CDirEntry class CDirEntry | ||
| 504 | /// @ingroup cpp_kodi_vfs_Defs | ||
| 505 | /// | ||
| 506 | /// @brief **Virtual file server directory entry**\n | ||
| 507 | /// This class is used as an entry for files and folders in | ||
| 508 | /// kodi::vfs::GetDirectory(). | ||
| 509 | /// | ||
| 510 | /// | ||
| 511 | /// ------------------------------------------------------------------------ | ||
| 512 | /// | ||
| 513 | /// **Example:** | ||
| 514 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 515 | /// #include <kodi/Filesystem.h> | ||
| 516 | /// | ||
| 517 | /// ... | ||
| 518 | /// | ||
| 519 | /// std::vector<kodi::vfs::CDirEntry> items; | ||
| 520 | /// kodi::vfs::GetDirectory("special://temp", "", items); | ||
| 521 | /// | ||
| 522 | /// fprintf(stderr, "Directory have %lu entries\n", items.size()); | ||
| 523 | /// for (unsigned long i = 0; i < items.size(); i++) | ||
| 524 | /// { | ||
| 525 | /// char buff[20]; | ||
| 526 | /// time_t now = items[i].DateTime(); | ||
| 527 | /// strftime(buff, 20, "%Y-%m-%d %H:%M:%S", gmtime(&now)); | ||
| 528 | /// fprintf(stderr, " - %04lu -- Folder: %s -- Name: %s -- Path: %s -- Time: %s\n", | ||
| 529 | /// i+1, | ||
| 530 | /// items[i].IsFolder() ? "yes" : "no ", | ||
| 531 | /// items[i].Label().c_str(), | ||
| 532 | /// items[i].Path().c_str(), | ||
| 533 | /// buff); | ||
| 534 | /// } | ||
| 535 | /// ~~~~~~~~~~~~~ | ||
| 536 | /// | ||
| 537 | /// It has the header @ref Filesystem.h "#include <kodi/Filesystem.h>" be included | ||
| 538 | /// to enjoy it. | ||
| 539 | /// | ||
| 540 | //@{ | ||
| 541 | class ATTRIBUTE_HIDDEN CDirEntry | ||
| 542 | { | ||
| 543 | public: | ||
| 544 | //============================================================================ | ||
| 545 | /// @ingroup cpp_kodi_vfs_CDirEntry | ||
| 546 | /// @brief Constructor for VFS directory entry | ||
| 547 | /// | ||
| 548 | /// @param[in] label [opt] Name to use for entry | ||
| 549 | /// @param[in] path [opt] Used path of the entry | ||
| 550 | /// @param[in] folder [opt] If set entry used as folder | ||
| 551 | /// @param[in] size [opt] If used as file, his size defined there | ||
| 552 | /// @param[in] dateTime [opt] Date time of the entry | ||
| 553 | /// | ||
| 554 | CDirEntry(const std::string& label = "", | ||
| 555 | const std::string& path = "", | ||
| 556 | bool folder = false, | ||
| 557 | int64_t size = -1, | ||
| 558 | time_t dateTime = 0) | ||
| 559 | : m_label(label), m_path(path), m_folder(folder), m_size(size), m_dateTime(dateTime) | ||
| 560 | { | ||
| 561 | } | ||
| 562 | //---------------------------------------------------------------------------- | ||
| 563 | |||
| 564 | //============================================================================ | ||
| 565 | // @note Not for addon development itself needed, thats why below is | ||
| 566 | // disabled for doxygen! | ||
| 567 | // | ||
| 568 | // @ingroup cpp_kodi_vfs_CDirEntry | ||
| 569 | // @brief Constructor to create own copy | ||
| 570 | // | ||
| 571 | // @param[in] dirEntry pointer to own class type | ||
| 572 | // | ||
| 573 | explicit CDirEntry(const VFSDirEntry& dirEntry) | ||
| 574 | : m_label(dirEntry.label ? dirEntry.label : ""), | ||
| 575 | m_path(dirEntry.path ? dirEntry.path : ""), | ||
| 576 | m_folder(dirEntry.folder), | ||
| 577 | m_size(dirEntry.size), | ||
| 578 | m_dateTime(dirEntry.date_time) | ||
| 579 | { | ||
| 580 | } | ||
| 581 | //---------------------------------------------------------------------------- | ||
| 582 | |||
| 583 | /// @defgroup cpp_kodi_vfs_CDirEntry_Help *Value Help* | ||
| 584 | /// @ingroup cpp_kodi_vfs_CDirEntry | ||
| 585 | /// -------------------------------------------------------------------------- | ||
| 586 | /// | ||
| 587 | /// <b>The following table contains values that can be set with @ref cpp_kodi_vfs_CDirEntry :</b> | ||
| 588 | /// | Name | Type | Set call | Get call | Clear call | | ||
| 589 | /// |------|------|----------|----------|------------| | ||
| 590 | /// | **Directory entry name** | `std::string` | @ref CDirEntry::SetLabel "SetLabel" | @ref CDirEntry::Label "Label" | | | ||
| 591 | /// | **Title of entry** | `std::string` | @ref CDirEntry::SetTitle "SetTitle" | @ref CDirEntry::Title "Title" | | | ||
| 592 | /// | **Path of the entry** | `std::string` | @ref CDirEntry::SetPath "SetPath" | @ref CDirEntry::Path "Path" | | | ||
| 593 | /// | **Entry is folder** | `bool` | @ref CDirEntry::SetFolder "SetFolder" | @ref CDirEntry::IsFolder "IsFolder" | | | ||
| 594 | /// | **The size of the file** | `int64_t` | @ref CDirEntry::SetSize "SetSize" | @ref CDirEntry::Size "Size" | | | ||
| 595 | /// | **File time and date** | `time_t` | @ref CDirEntry::SetDateTime "SetDateTime" | @ref CDirEntry::DateTime "DateTime" | | | ||
| 596 | /// | **Property entries** | `std::string, std::string` | @ref CDirEntry::AddProperty "AddProperty" | @ref CDirEntry::GetProperties "GetProperties" | @ref CDirEntry::ClearProperties "ClearProperties" | ||
| 597 | /// | ||
| 598 | |||
| 599 | /// @addtogroup cpp_kodi_vfs_CDirEntry | ||
| 600 | /// @copydetails cpp_kodi_vfs_CDirEntry_Help | ||
| 601 | //@{ | ||
| 602 | |||
| 603 | //============================================================================ | ||
| 604 | /// @brief Get the directory entry name. | ||
| 605 | /// | ||
| 606 | /// @return Name of the entry | ||
| 607 | /// | ||
| 608 | const std::string& Label(void) const { return m_label; } | ||
| 609 | //---------------------------------------------------------------------------- | ||
| 610 | |||
| 611 | //============================================================================ | ||
| 612 | /// @brief Get the optional title of entry. | ||
| 613 | /// | ||
| 614 | /// @return Title of the entry, if exists | ||
| 615 | /// | ||
| 616 | const std::string& Title(void) const { return m_title; } | ||
| 617 | //---------------------------------------------------------------------------- | ||
| 618 | |||
| 619 | //============================================================================ | ||
| 620 | /// @brief Get the path of the entry. | ||
| 621 | /// | ||
| 622 | /// @return File system path of the entry | ||
| 623 | /// | ||
| 624 | const std::string& Path(void) const { return m_path; } | ||
| 625 | //---------------------------------------------------------------------------- | ||
| 626 | |||
| 627 | //============================================================================ | ||
| 628 | /// @brief Used to check entry is folder. | ||
| 629 | /// | ||
| 630 | /// @return true if entry is a folder | ||
| 631 | /// | ||
| 632 | bool IsFolder(void) const { return m_folder; } | ||
| 633 | //---------------------------------------------------------------------------- | ||
| 634 | |||
| 635 | //============================================================================ | ||
| 636 | /// @brief If file, the size of the file. | ||
| 637 | /// | ||
| 638 | /// @return Defined file size | ||
| 639 | /// | ||
| 640 | int64_t Size(void) const { return m_size; } | ||
| 641 | //---------------------------------------------------------------------------- | ||
| 642 | |||
| 643 | //============================================================================ | ||
| 644 | /// @brief Get file time and date for a new entry. | ||
| 645 | /// | ||
| 646 | /// @return The with time_t defined date and time of file | ||
| 647 | /// | ||
| 648 | time_t DateTime() { return m_dateTime; } | ||
| 649 | //---------------------------------------------------------------------------- | ||
| 650 | |||
| 651 | //============================================================================ | ||
| 652 | /// @brief Set the label name. | ||
| 653 | /// | ||
| 654 | /// @param[in] label name of entry | ||
| 655 | /// | ||
| 656 | void SetLabel(const std::string& label) { m_label = label; } | ||
| 657 | //---------------------------------------------------------------------------- | ||
| 658 | |||
| 659 | //============================================================================ | ||
| 660 | /// @brief Set the title name. | ||
| 661 | /// | ||
| 662 | /// @param[in] title title name of entry | ||
| 663 | /// | ||
| 664 | void SetTitle(const std::string& title) { m_title = title; } | ||
| 665 | //---------------------------------------------------------------------------- | ||
| 666 | |||
| 667 | //============================================================================ | ||
| 668 | /// @brief Set the path of the entry. | ||
| 669 | /// | ||
| 670 | /// @param[in] path path of entry | ||
| 671 | /// | ||
| 672 | void SetPath(const std::string& path) { m_path = path; } | ||
| 673 | //---------------------------------------------------------------------------- | ||
| 674 | |||
| 675 | //============================================================================ | ||
| 676 | /// @brief Set the entry defined as folder. | ||
| 677 | /// | ||
| 678 | /// @param[in] folder If true becomes entry defined as folder | ||
| 679 | /// | ||
| 680 | void SetFolder(bool folder) { m_folder = folder; } | ||
| 681 | //---------------------------------------------------------------------------- | ||
| 682 | |||
| 683 | //============================================================================ | ||
| 684 | /// @brief Set a file size for a new entry. | ||
| 685 | /// | ||
| 686 | /// @param[in] size Size to set for dir entry | ||
| 687 | /// | ||
| 688 | void SetSize(int64_t size) { m_size = size; } | ||
| 689 | //---------------------------------------------------------------------------- | ||
| 690 | |||
| 691 | //============================================================================ | ||
| 692 | /// @brief Set file time and date for a new entry. | ||
| 693 | /// | ||
| 694 | /// @param[in] dateTime The with time_t defined date and time of file | ||
| 695 | /// | ||
| 696 | void SetDateTime(time_t dateTime) { m_dateTime = dateTime; } | ||
| 697 | //---------------------------------------------------------------------------- | ||
| 698 | |||
| 699 | //============================================================================ | ||
| 700 | /// @brief Add a by string defined property entry to directory entry. | ||
| 701 | /// | ||
| 702 | /// @note A property can be used to add some special information about a file | ||
| 703 | /// or directory entry, this can be used on other places to do the right work | ||
| 704 | /// of them. | ||
| 705 | /// | ||
| 706 | /// @param[in] id Identification name of property | ||
| 707 | /// @param[in] value The property value to add by given id | ||
| 708 | /// | ||
| 709 | void AddProperty(const std::string& id, const std::string& value) { m_properties[id] = value; } | ||
| 710 | //---------------------------------------------------------------------------- | ||
| 711 | |||
| 712 | //============================================================================ | ||
| 713 | /// @brief Clear all present properties. | ||
| 714 | /// | ||
| 715 | void ClearProperties() { m_properties.clear(); } | ||
| 716 | //---------------------------------------------------------------------------- | ||
| 717 | |||
| 718 | //============================================================================ | ||
| 719 | /// @brief Get the present properties list on directory entry. | ||
| 720 | /// | ||
| 721 | /// @return map with all present properties | ||
| 722 | /// | ||
| 723 | const std::map<std::string, std::string>& GetProperties() const { return m_properties; } | ||
| 724 | //---------------------------------------------------------------------------- | ||
| 725 | |||
| 726 | //@} | ||
| 727 | |||
| 728 | private: | ||
| 729 | std::string m_label; | ||
| 730 | std::string m_title; | ||
| 731 | std::string m_path; | ||
| 732 | std::map<std::string, std::string> m_properties; | ||
| 733 | bool m_folder; | ||
| 734 | int64_t m_size; | ||
| 735 | time_t m_dateTime; | ||
| 736 | }; | ||
| 737 | //@} | ||
| 738 | //------------------------------------------------------------------------------ | ||
| 739 | |||
| 740 | //}}} | ||
| 741 | |||
| 742 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ | ||
| 743 | // "C++" Directory related functions | ||
| 744 | //{{{ | ||
| 745 | |||
| 746 | //============================================================================== | ||
| 747 | /// @ingroup cpp_kodi_vfs_Directory | ||
| 748 | /// @brief Make a directory. | ||
| 749 | /// | ||
| 750 | /// The kodi::vfs::CreateDirectory() function shall create a | ||
| 751 | /// new directory with name path. | ||
| 752 | /// | ||
| 753 | /// The newly created directory shall be an empty directory. | ||
| 754 | /// | ||
| 755 | /// @param[in] path Path to the directory. | ||
| 756 | /// @return Upon successful completion, CreateDirectory() shall return true. | ||
| 757 | /// Otherwise false shall be returned, no directory shall be created. | ||
| 758 | /// | ||
| 759 | /// | ||
| 760 | /// ------------------------------------------------------------------------- | ||
| 761 | /// | ||
| 762 | /// **Example:** | ||
| 763 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 764 | /// #include <kodi/Filesystem.h> | ||
| 765 | /// ... | ||
| 766 | /// std::string directory = "C:\\my_dir"; | ||
| 767 | /// bool ret = kodi::vfs::CreateDirectory(directory); | ||
| 768 | /// fprintf(stderr, "Directory '%s' successfull created: %s\n", directory.c_str(), ret ? "yes" : "no"); | ||
| 769 | /// ... | ||
| 770 | /// ~~~~~~~~~~~~~ | ||
| 771 | /// | ||
| 772 | inline bool ATTRIBUTE_HIDDEN CreateDirectory(const std::string& path) | ||
| 773 | { | ||
| 774 | using namespace kodi::addon; | ||
| 775 | |||
| 776 | return CAddonBase::m_interface->toKodi->kodi_filesystem->create_directory( | ||
| 777 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 778 | } | ||
| 779 | //------------------------------------------------------------------------------ | ||
| 780 | |||
| 781 | //============================================================================== | ||
| 782 | /// @ingroup cpp_kodi_vfs_Directory | ||
| 783 | /// @brief Verifying the Existence of a Directory. | ||
| 784 | /// | ||
| 785 | /// The kodi::vfs::DirectoryExists() method determines whether | ||
| 786 | /// a specified folder exists. | ||
| 787 | /// | ||
| 788 | /// @param[in] path Path to the directory. | ||
| 789 | /// @return True when it exists, false otherwise. | ||
| 790 | /// | ||
| 791 | /// | ||
| 792 | /// ------------------------------------------------------------------------- | ||
| 793 | /// | ||
| 794 | /// **Example:** | ||
| 795 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 796 | /// #include <kodi/Filesystem.h> | ||
| 797 | /// ... | ||
| 798 | /// std::string directory = "C:\\my_dir"; | ||
| 799 | /// bool ret = kodi::vfs::DirectoryExists(directory); | ||
| 800 | /// fprintf(stderr, "Directory '%s' present: %s\n", directory.c_str(), ret ? "yes" : "no"); | ||
| 801 | /// ... | ||
| 802 | /// ~~~~~~~~~~~~~ | ||
| 803 | /// | ||
| 804 | inline bool ATTRIBUTE_HIDDEN DirectoryExists(const std::string& path) | ||
| 805 | { | ||
| 806 | using namespace kodi::addon; | ||
| 807 | |||
| 808 | return CAddonBase::m_interface->toKodi->kodi_filesystem->directory_exists( | ||
| 809 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 810 | } | ||
| 811 | //------------------------------------------------------------------------------ | ||
| 812 | |||
| 813 | //============================================================================== | ||
| 814 | /// @ingroup cpp_kodi_vfs_Directory | ||
| 815 | /// @brief Removes a directory. | ||
| 816 | /// | ||
| 817 | /// The kodi::vfs::RemoveDirectory() function shall remove a | ||
| 818 | /// directory whose name is given by path. | ||
| 819 | /// | ||
| 820 | /// @param[in] path Path to the directory. | ||
| 821 | /// @param[in] recursive [opt] Remove directory recursive (default is false) | ||
| 822 | /// @return Upon successful completion, the function RemoveDirectory() shall | ||
| 823 | /// return true. Otherwise, false shall be returned, and errno set | ||
| 824 | /// to indicate the error. If false is returned, the named directory | ||
| 825 | /// shall not be changed. | ||
| 826 | /// | ||
| 827 | /// | ||
| 828 | /// ------------------------------------------------------------------------- | ||
| 829 | /// | ||
| 830 | /// **Example:** | ||
| 831 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 832 | /// #include <kodi/Filesystem.h> | ||
| 833 | /// ... | ||
| 834 | /// bool ret = kodi::vfs::RemoveDirectory("C:\\my_dir"); | ||
| 835 | /// ... | ||
| 836 | /// ~~~~~~~~~~~~~ | ||
| 837 | /// | ||
| 838 | inline bool ATTRIBUTE_HIDDEN RemoveDirectory(const std::string& path, bool recursive = false) | ||
| 839 | { | ||
| 840 | using namespace kodi::addon; | ||
| 841 | |||
| 842 | if (!recursive) | ||
| 843 | return CAddonBase::m_interface->toKodi->kodi_filesystem->remove_directory( | ||
| 844 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 845 | else | ||
| 846 | return CAddonBase::m_interface->toKodi->kodi_filesystem->remove_directory_recursive( | ||
| 847 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 848 | } | ||
| 849 | //------------------------------------------------------------------------------ | ||
| 850 | |||
| 851 | //============================================================================== | ||
| 852 | /// @ingroup cpp_kodi_vfs_Directory | ||
| 853 | /// @brief Lists a directory. | ||
| 854 | /// | ||
| 855 | /// Return the list of files and directories which have been found in the | ||
| 856 | /// specified directory and which respect the given constraint. | ||
| 857 | /// | ||
| 858 | /// It can handle the normal OS dependent paths and also the special virtual | ||
| 859 | /// filesystem from Kodi what starts with \b special://. | ||
| 860 | /// | ||
| 861 | /// @param[in] path The path in which the files and directories are located. | ||
| 862 | /// @param[in] mask Mask to filter out requested files, e.g. "*.avi|*.mpg" to | ||
| 863 | /// files with this ending. | ||
| 864 | /// @param[out] items The returned list directory entries. | ||
| 865 | /// @return True if listing was successful, false otherwise. | ||
| 866 | /// | ||
| 867 | /// | ||
| 868 | /// ------------------------------------------------------------------------- | ||
| 869 | /// | ||
| 870 | /// **Example:** | ||
| 871 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 872 | /// #include <kodi/Filesystem.h> | ||
| 873 | /// | ||
| 874 | /// std::vector<kodi::vfs::CDirEntry> items; | ||
| 875 | /// kodi::vfs::GetDirectory("special://temp", "", items); | ||
| 876 | /// | ||
| 877 | /// fprintf(stderr, "Directory have %lu entries\n", items.size()); | ||
| 878 | /// for (unsigned long i = 0; i < items.size(); i++) | ||
| 879 | /// { | ||
| 880 | /// fprintf(stderr, " - %04lu -- Folder: %s -- Name: %s -- Path: %s\n", | ||
| 881 | /// i+1, | ||
| 882 | /// items[i].IsFolder() ? "yes" : "no ", | ||
| 883 | /// items[i].Label().c_str(), | ||
| 884 | /// items[i].Path().c_str()); | ||
| 885 | /// } | ||
| 886 | /// ~~~~~~~~~~~~~ | ||
| 887 | inline bool ATTRIBUTE_HIDDEN GetDirectory(const std::string& path, | ||
| 888 | const std::string& mask, | ||
| 889 | std::vector<kodi::vfs::CDirEntry>& items) | ||
| 890 | { | ||
| 891 | using namespace kodi::addon; | ||
| 892 | |||
| 893 | VFSDirEntry* dir_list = nullptr; | ||
| 894 | unsigned int num_items = 0; | ||
| 895 | if (CAddonBase::m_interface->toKodi->kodi_filesystem->get_directory( | ||
| 896 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str(), mask.c_str(), &dir_list, | ||
| 897 | &num_items)) | ||
| 898 | { | ||
| 899 | if (dir_list) | ||
| 900 | { | ||
| 901 | for (unsigned int i = 0; i < num_items; ++i) | ||
| 902 | items.emplace_back(dir_list[i]); | ||
| 903 | |||
| 904 | CAddonBase::m_interface->toKodi->kodi_filesystem->free_directory( | ||
| 905 | CAddonBase::m_interface->toKodi->kodiBase, dir_list, num_items); | ||
| 906 | } | ||
| 907 | |||
| 908 | return true; | ||
| 909 | } | ||
| 910 | return false; | ||
| 911 | } | ||
| 912 | //------------------------------------------------------------------------------ | ||
| 913 | |||
| 914 | //}}} | ||
| 915 | |||
| 916 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ | ||
| 917 | // "C++" File related functions | ||
| 918 | //{{{ | ||
| 919 | |||
| 920 | //============================================================================== | ||
| 921 | /// @ingroup cpp_kodi_vfs_File | ||
| 922 | /// @brief Check if a file exists. | ||
| 923 | /// | ||
| 924 | /// @param[in] filename The filename to check. | ||
| 925 | /// @param[in] usecache Check in file cache. | ||
| 926 | /// @return true if the file exists false otherwise. | ||
| 927 | /// | ||
| 928 | /// | ||
| 929 | /// ------------------------------------------------------------------------- | ||
| 930 | /// | ||
| 931 | /// **Example:** | ||
| 932 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 933 | /// #include <kodi/Filesystem.h> | ||
| 934 | /// ... | ||
| 935 | /// bool exists = kodi::vfs::FileExists("special://temp/kodi.log"); | ||
| 936 | /// fprintf(stderr, "Log file should be always present, is it present? %s\n", exists ? "yes" : "no"); | ||
| 937 | /// ~~~~~~~~~~~~~ | ||
| 938 | /// | ||
| 939 | inline bool ATTRIBUTE_HIDDEN FileExists(const std::string& filename, bool usecache = false) | ||
| 940 | { | ||
| 941 | using namespace kodi::addon; | ||
| 942 | |||
| 943 | return CAddonBase::m_interface->toKodi->kodi_filesystem->file_exists( | ||
| 944 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str(), usecache); | ||
| 945 | } | ||
| 946 | //------------------------------------------------------------------------------ | ||
| 947 | |||
| 948 | //============================================================================== | ||
| 949 | /// @ingroup cpp_kodi_vfs_File | ||
| 950 | /// @brief Get file status. | ||
| 951 | /// | ||
| 952 | /// These function return information about a file. Execute (search) | ||
| 953 | /// permission is required on all of the directories in path that | ||
| 954 | /// lead to the file. | ||
| 955 | /// | ||
| 956 | /// The call return a stat structure, which contains the on | ||
| 957 | /// @ref cpp_kodi_vfs_Defs_FileStatus defined values. | ||
| 958 | /// | ||
| 959 | /// @warning Not all of the OS file systems implement all of the time fields. | ||
| 960 | /// | ||
| 961 | /// @param[in] filename The filename to read the status from. | ||
| 962 | /// @param[out] buffer The file status is written into this buffer. | ||
| 963 | /// @return On success, trur is returned. On error, false is returned | ||
| 964 | /// | ||
| 965 | /// | ||
| 966 | /// @copydetails cpp_kodi_vfs_Defs_FileStatus_Help | ||
| 967 | /// | ||
| 968 | /// ------------------------------------------------------------------------- | ||
| 969 | /// | ||
| 970 | /// **Example:** | ||
| 971 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 972 | /// #include <kodi/Filesystem.h> | ||
| 973 | /// ... | ||
| 974 | /// kodi::vfs::FileStatus statFile; | ||
| 975 | /// int ret = kodi::vfs::StatFile("special://temp/kodi.log", statFile); | ||
| 976 | /// fprintf(stderr, "deviceId (ID of device containing file) = %u\n" | ||
| 977 | /// "size (total size, in bytes) = %lu\n" | ||
| 978 | /// "accessTime (time of last access) = %lu\n" | ||
| 979 | /// "modificationTime (time of last modification) = %lu\n" | ||
| 980 | /// "statusTime (time of last status change) = %lu\n" | ||
| 981 | /// "isDirectory (The stat url is a directory) = %s\n" | ||
| 982 | /// "isSymLink (The stat url is a symbolic link) = %s\n" | ||
| 983 | /// "Return value = %i\n", | ||
| 984 | /// statFile.GetDeviceId(), | ||
| 985 | /// statFile.GetSize(), | ||
| 986 | /// statFile.GetAccessTime(), | ||
| 987 | /// statFile.GetModificationTime(), | ||
| 988 | /// statFile.GetStatusTime(), | ||
| 989 | /// statFile.GetIsDirectory() ? "true" : "false", | ||
| 990 | /// statFile.GetIsSymLink() ? "true" : "false", | ||
| 991 | /// ret); | ||
| 992 | /// ~~~~~~~~~~~~~ | ||
| 993 | /// | ||
| 994 | inline bool ATTRIBUTE_HIDDEN StatFile(const std::string& filename, kodi::vfs::FileStatus& buffer) | ||
| 995 | { | ||
| 996 | using namespace kodi::addon; | ||
| 997 | |||
| 998 | return CAddonBase::m_interface->toKodi->kodi_filesystem->stat_file( | ||
| 999 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str(), buffer); | ||
| 1000 | } | ||
| 1001 | //------------------------------------------------------------------------------ | ||
| 1002 | |||
| 1003 | //============================================================================== | ||
| 1004 | /// @ingroup cpp_kodi_vfs_File | ||
| 1005 | /// @brief Deletes a file. | ||
| 1006 | /// | ||
| 1007 | /// @param[in] filename The filename to delete. | ||
| 1008 | /// @return The file was successfully deleted. | ||
| 1009 | /// | ||
| 1010 | /// | ||
| 1011 | /// ------------------------------------------------------------------------- | ||
| 1012 | /// | ||
| 1013 | /// **Example:** | ||
| 1014 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1015 | /// #include <kodi/Filesystem.h> | ||
| 1016 | /// #include <kodi/gui/DialogFileBrowser.h> | ||
| 1017 | /// #include <kodi/gui/DialogOK.h> | ||
| 1018 | /// ... | ||
| 1019 | /// std::string filename; | ||
| 1020 | /// if (kodi::gui::DialogFileBrowser::ShowAndGetFile("local", "", | ||
| 1021 | /// "Test File selection and delete of them!", | ||
| 1022 | /// filename)) | ||
| 1023 | /// { | ||
| 1024 | /// bool successed = kodi::vfs::DeleteFile(filename); | ||
| 1025 | /// if (!successed) | ||
| 1026 | /// kodi::gui::DialogOK::ShowAndGetInput("Error", "Delete of File", filename, "failed!"); | ||
| 1027 | /// else | ||
| 1028 | /// kodi::gui::DialogOK::ShowAndGetInput("Information", "Delete of File", filename, "successfull done."); | ||
| 1029 | /// } | ||
| 1030 | /// ~~~~~~~~~~~~~ | ||
| 1031 | /// | ||
| 1032 | inline bool ATTRIBUTE_HIDDEN DeleteFile(const std::string& filename) | ||
| 1033 | { | ||
| 1034 | using namespace kodi::addon; | ||
| 1035 | |||
| 1036 | return CAddonBase::m_interface->toKodi->kodi_filesystem->delete_file( | ||
| 1037 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str()); | ||
| 1038 | } | ||
| 1039 | //------------------------------------------------------------------------------ | ||
| 1040 | |||
| 1041 | //============================================================================== | ||
| 1042 | /// @ingroup cpp_kodi_vfs_File | ||
| 1043 | /// @brief Rename a file name. | ||
| 1044 | /// | ||
| 1045 | /// @param[in] filename The filename to copy. | ||
| 1046 | /// @param[in] newFileName The new filename | ||
| 1047 | /// @return true if successfully renamed | ||
| 1048 | /// | ||
| 1049 | /// | ||
| 1050 | inline bool ATTRIBUTE_HIDDEN RenameFile(const std::string& filename, const std::string& newFileName) | ||
| 1051 | { | ||
| 1052 | using namespace kodi::addon; | ||
| 1053 | |||
| 1054 | return CAddonBase::m_interface->toKodi->kodi_filesystem->rename_file( | ||
| 1055 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str(), newFileName.c_str()); | ||
| 1056 | } | ||
| 1057 | //------------------------------------------------------------------------------ | ||
| 1058 | |||
| 1059 | //============================================================================== | ||
| 1060 | /// @ingroup cpp_kodi_vfs_File | ||
| 1061 | /// @brief Copy a file from source to destination. | ||
| 1062 | /// | ||
| 1063 | /// @param[in] filename The filename to copy. | ||
| 1064 | /// @param[in] destination The destination to copy file to | ||
| 1065 | /// @return true if successfully copied | ||
| 1066 | /// | ||
| 1067 | /// | ||
| 1068 | inline bool ATTRIBUTE_HIDDEN CopyFile(const std::string& filename, const std::string& destination) | ||
| 1069 | { | ||
| 1070 | using namespace kodi::addon; | ||
| 1071 | |||
| 1072 | return CAddonBase::m_interface->toKodi->kodi_filesystem->copy_file( | ||
| 1073 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str(), destination.c_str()); | ||
| 1074 | } | ||
| 1075 | //------------------------------------------------------------------------------ | ||
| 1076 | |||
| 1077 | //}}} | ||
| 1078 | |||
| 1079 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ | ||
| 1080 | // "C++" General filesystem functions | ||
| 1081 | //{{{ | ||
| 1082 | |||
| 1083 | //============================================================================== | ||
| 1084 | /// @ingroup cpp_kodi_vfs_General | ||
| 1085 | /// @brief Retrieve MD5sum of a file. | ||
| 1086 | /// | ||
| 1087 | /// @param[in] path Path to the file to MD5sum | ||
| 1088 | /// @return MD5 sum of the file | ||
| 1089 | /// | ||
| 1090 | /// | ||
| 1091 | /// ------------------------------------------------------------------------- | ||
| 1092 | /// | ||
| 1093 | /// **Example:** | ||
| 1094 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1095 | /// #include <kodi/Filesystem.h> | ||
| 1096 | /// #include <kodi/gui/DialogFileBrowser.h> | ||
| 1097 | /// ... | ||
| 1098 | /// std::string md5; | ||
| 1099 | /// std::string filename; | ||
| 1100 | /// if (kodi::gui::DialogFileBrowser::ShowAndGetFile("local", "*.avi|*.mpg|*.mp4", | ||
| 1101 | /// "Test File selection to get MD5", | ||
| 1102 | /// filename)) | ||
| 1103 | /// { | ||
| 1104 | /// md5 = kodi::vfs::GetFileMD5(filename); | ||
| 1105 | /// fprintf(stderr, "MD5 of file '%s' is %s\n", md5.c_str(), filename.c_str()); | ||
| 1106 | /// } | ||
| 1107 | /// ~~~~~~~~~~~~~ | ||
| 1108 | /// | ||
| 1109 | inline std::string ATTRIBUTE_HIDDEN GetFileMD5(const std::string& path) | ||
| 1110 | { | ||
| 1111 | using namespace kodi::addon; | ||
| 1112 | |||
| 1113 | std::string strReturn; | ||
| 1114 | char* strMd5 = CAddonBase::m_interface->toKodi->kodi_filesystem->get_file_md5( | ||
| 1115 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 1116 | if (strMd5 != nullptr) | ||
| 1117 | { | ||
| 1118 | if (std::strlen(strMd5)) | ||
| 1119 | strReturn = strMd5; | ||
| 1120 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, strMd5); | ||
| 1121 | } | ||
| 1122 | return strReturn; | ||
| 1123 | } | ||
| 1124 | //------------------------------------------------------------------------------ | ||
| 1125 | |||
| 1126 | //============================================================================== | ||
| 1127 | /// @ingroup cpp_kodi_vfs_General | ||
| 1128 | /// @brief Returns a thumb cache filename. | ||
| 1129 | /// | ||
| 1130 | /// @param[in] filename Path to file | ||
| 1131 | /// @return Cache filename | ||
| 1132 | /// | ||
| 1133 | /// | ||
| 1134 | /// ------------------------------------------------------------------------ | ||
| 1135 | /// | ||
| 1136 | /// **Example:** | ||
| 1137 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1138 | /// #include <kodi/Filesystem.h> | ||
| 1139 | /// #include <kodi/gui/DialogFileBrowser.h> | ||
| 1140 | /// ... | ||
| 1141 | /// std::string thumb; | ||
| 1142 | /// std::string filename; | ||
| 1143 | /// if (kodi::gui::DialogFileBrowser::ShowAndGetFile("local", "*.avi|*.mpg|*.mp4", | ||
| 1144 | /// "Test File selection to get Thumnail", | ||
| 1145 | /// filename)) | ||
| 1146 | /// { | ||
| 1147 | /// thumb = kodi::vfs::GetCacheThumbName(filename); | ||
| 1148 | /// fprintf(stderr, "Thumb name of file '%s' is %s\n", thumb.c_str(), filename.c_str()); | ||
| 1149 | /// } | ||
| 1150 | /// ~~~~~~~~~~~~~ | ||
| 1151 | /// | ||
| 1152 | inline std::string ATTRIBUTE_HIDDEN GetCacheThumbName(const std::string& filename) | ||
| 1153 | { | ||
| 1154 | using namespace kodi::addon; | ||
| 1155 | |||
| 1156 | std::string strReturn; | ||
| 1157 | char* strThumbName = CAddonBase::m_interface->toKodi->kodi_filesystem->get_cache_thumb_name( | ||
| 1158 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str()); | ||
| 1159 | if (strThumbName != nullptr) | ||
| 1160 | { | ||
| 1161 | if (std::strlen(strThumbName)) | ||
| 1162 | strReturn = strThumbName; | ||
| 1163 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 1164 | strThumbName); | ||
| 1165 | } | ||
| 1166 | return strReturn; | ||
| 1167 | } | ||
| 1168 | //------------------------------------------------------------------------------ | ||
| 1169 | |||
| 1170 | //============================================================================== | ||
| 1171 | /// @ingroup cpp_kodi_vfs_General | ||
| 1172 | /// @brief Make filename valid. | ||
| 1173 | /// | ||
| 1174 | /// Function to replace not valid characters with '_'. It can be also | ||
| 1175 | /// compared with original before in a own loop until it is equal | ||
| 1176 | /// (no invalid characters). | ||
| 1177 | /// | ||
| 1178 | /// @param[in] filename Filename to check and fix | ||
| 1179 | /// @return The legal filename | ||
| 1180 | /// | ||
| 1181 | /// | ||
| 1182 | /// ------------------------------------------------------------------------ | ||
| 1183 | /// | ||
| 1184 | /// **Example:** | ||
| 1185 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1186 | /// #include <kodi/Filesystem.h> | ||
| 1187 | /// ... | ||
| 1188 | /// std::string fileName = "///\\jk???lj????.mpg"; | ||
| 1189 | /// std::string legalName = kodi::vfs::MakeLegalFileName(fileName); | ||
| 1190 | /// fprintf(stderr, "Legal name of '%s' is '%s'\n", fileName.c_str(), legalName.c_str()); | ||
| 1191 | /// | ||
| 1192 | /// /* Returns as legal: 'jk___lj____.mpg' */ | ||
| 1193 | /// ~~~~~~~~~~~~~ | ||
| 1194 | /// | ||
| 1195 | inline std::string ATTRIBUTE_HIDDEN MakeLegalFileName(const std::string& filename) | ||
| 1196 | { | ||
| 1197 | using namespace kodi::addon; | ||
| 1198 | |||
| 1199 | std::string strReturn; | ||
| 1200 | char* strLegalFileName = CAddonBase::m_interface->toKodi->kodi_filesystem->make_legal_filename( | ||
| 1201 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str()); | ||
| 1202 | if (strLegalFileName != nullptr) | ||
| 1203 | { | ||
| 1204 | if (std::strlen(strLegalFileName)) | ||
| 1205 | strReturn = strLegalFileName; | ||
| 1206 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 1207 | strLegalFileName); | ||
| 1208 | } | ||
| 1209 | return strReturn; | ||
| 1210 | } | ||
| 1211 | //------------------------------------------------------------------------------ | ||
| 1212 | |||
| 1213 | //============================================================================== | ||
| 1214 | /// @ingroup cpp_kodi_vfs_General | ||
| 1215 | /// @brief Make directory name valid. | ||
| 1216 | /// | ||
| 1217 | /// Function to replace not valid characters with '_'. It can be also | ||
| 1218 | /// compared with original before in a own loop until it is equal | ||
| 1219 | /// (no invalid characters). | ||
| 1220 | /// | ||
| 1221 | /// @param[in] path Directory name to check and fix | ||
| 1222 | /// @return The legal directory name | ||
| 1223 | /// | ||
| 1224 | /// | ||
| 1225 | /// ------------------------------------------------------------------------ | ||
| 1226 | /// | ||
| 1227 | /// **Example:** | ||
| 1228 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1229 | /// #include <kodi/Filesystem.h> | ||
| 1230 | /// ... | ||
| 1231 | /// std::string path = "///\\jk???lj????\\hgjkg"; | ||
| 1232 | /// std::string legalPath = kodi::vfs::MakeLegalPath(path); | ||
| 1233 | /// fprintf(stderr, "Legal name of '%s' is '%s'\n", path.c_str(), legalPath.c_str()); | ||
| 1234 | /// | ||
| 1235 | /// /* Returns as legal: '/jk___lj____/hgjkg' */ | ||
| 1236 | /// ~~~~~~~~~~~~~ | ||
| 1237 | /// | ||
| 1238 | inline std::string ATTRIBUTE_HIDDEN MakeLegalPath(const std::string& path) | ||
| 1239 | { | ||
| 1240 | using namespace kodi::addon; | ||
| 1241 | |||
| 1242 | std::string strReturn; | ||
| 1243 | char* strLegalPath = CAddonBase::m_interface->toKodi->kodi_filesystem->make_legal_path( | ||
| 1244 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 1245 | if (strLegalPath != nullptr) | ||
| 1246 | { | ||
| 1247 | if (std::strlen(strLegalPath)) | ||
| 1248 | strReturn = strLegalPath; | ||
| 1249 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 1250 | strLegalPath); | ||
| 1251 | } | ||
| 1252 | return strReturn; | ||
| 1253 | } | ||
| 1254 | //------------------------------------------------------------------------------ | ||
| 1255 | |||
| 1256 | //============================================================================== | ||
| 1257 | /// @ingroup cpp_kodi_vfs_General | ||
| 1258 | /// @brief Returns the translated path. | ||
| 1259 | /// | ||
| 1260 | /// @param[in] source String or unicode - Path to format | ||
| 1261 | /// @return A human-readable string suitable for logging | ||
| 1262 | /// | ||
| 1263 | /// @note Only useful if you are coding for both Linux and Windows. e.g. | ||
| 1264 | /// Converts 'special://masterprofile/script_data' -> | ||
| 1265 | /// '/home/user/.kodi/UserData/script_data' on Linux. | ||
| 1266 | /// | ||
| 1267 | /// | ||
| 1268 | /// ------------------------------------------------------------------------ | ||
| 1269 | /// | ||
| 1270 | /// **Example:** | ||
| 1271 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1272 | /// #include <kodi/Filesystem.h> | ||
| 1273 | /// ... | ||
| 1274 | /// std::string path = kodi::vfs::TranslateSpecialProtocol("special://masterprofile/script_data"); | ||
| 1275 | /// fprintf(stderr, "Translated path is: %s\n", path.c_str()); | ||
| 1276 | /// ... | ||
| 1277 | /// ~~~~~~~~~~~~~ | ||
| 1278 | /// or | ||
| 1279 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1280 | /// #include <kodi/Filesystem.h> | ||
| 1281 | /// ... | ||
| 1282 | /// fprintf(stderr, "Directory 'special://temp' is '%s'\n", kodi::vfs::TranslateSpecialProtocol("special://temp").c_str()); | ||
| 1283 | /// ... | ||
| 1284 | /// ~~~~~~~~~~~~~ | ||
| 1285 | /// | ||
| 1286 | inline std::string ATTRIBUTE_HIDDEN TranslateSpecialProtocol(const std::string& source) | ||
| 1287 | { | ||
| 1288 | using namespace kodi::addon; | ||
| 1289 | |||
| 1290 | std::string strReturn; | ||
| 1291 | char* protocol = CAddonBase::m_interface->toKodi->kodi_filesystem->translate_special_protocol( | ||
| 1292 | CAddonBase::m_interface->toKodi->kodiBase, source.c_str()); | ||
| 1293 | if (protocol != nullptr) | ||
| 1294 | { | ||
| 1295 | if (std::strlen(protocol)) | ||
| 1296 | strReturn = protocol; | ||
| 1297 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 1298 | protocol); | ||
| 1299 | } | ||
| 1300 | return strReturn; | ||
| 1301 | } | ||
| 1302 | //------------------------------------------------------------------------------ | ||
| 1303 | |||
| 1304 | //============================================================================== | ||
| 1305 | /// @ingroup cpp_kodi_vfs_General | ||
| 1306 | /// @brief Retrieves information about the amount of space that is available on | ||
| 1307 | /// a disk volume. | ||
| 1308 | /// | ||
| 1309 | /// Path can be also with Kodi's special protocol. | ||
| 1310 | /// | ||
| 1311 | /// @param[in] path Path for where to check | ||
| 1312 | /// @param[out] capacity The total number of bytes in the file system | ||
| 1313 | /// @param[out] free The total number of free bytes in the file system | ||
| 1314 | /// @param[out] available The total number of free bytes available to a | ||
| 1315 | /// non-privileged process | ||
| 1316 | /// @return true if successfully done and set | ||
| 1317 | /// | ||
| 1318 | /// @warning This only works with paths belonging to OS. If <b>"special://"</b> | ||
| 1319 | /// is used, it must point to a place on your own OS. | ||
| 1320 | /// | ||
| 1321 | /// | ||
| 1322 | /// ------------------------------------------------------------------------ | ||
| 1323 | /// | ||
| 1324 | /// **Example:** | ||
| 1325 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1326 | /// #include <climits> // for ULLONG_MAX | ||
| 1327 | /// #include <kodi/Filesystem.h> | ||
| 1328 | /// ... | ||
| 1329 | /// std::string path = "special://temp"; | ||
| 1330 | /// uint64_t capacity = ULLONG_MAX; | ||
| 1331 | /// uint64_t free = ULLONG_MAX; | ||
| 1332 | /// uint64_t available = ULLONG_MAX; | ||
| 1333 | /// kodi::vfs::GetDiskSpace(path, capacity, free, available); | ||
| 1334 | /// fprintf(stderr, "Path '%s' sizes:\n", path.c_str()); | ||
| 1335 | /// fprintf(stderr, " - capacity: %lu MByte\n", capacity / 1024 / 1024); | ||
| 1336 | /// fprintf(stderr, " - free: %lu MByte\n", free / 1024 / 1024); | ||
| 1337 | /// fprintf(stderr, " - available: %lu MByte\n", available / 1024 / 1024); | ||
| 1338 | /// ~~~~~~~~~~~~~ | ||
| 1339 | /// | ||
| 1340 | inline bool ATTRIBUTE_HIDDEN GetDiskSpace(const std::string& path, | ||
| 1341 | uint64_t& capacity, | ||
| 1342 | uint64_t& free, | ||
| 1343 | uint64_t& available) | ||
| 1344 | { | ||
| 1345 | using namespace kodi::addon; | ||
| 1346 | |||
| 1347 | return CAddonBase::m_interface->toKodi->kodi_filesystem->get_disk_space( | ||
| 1348 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str(), &capacity, &free, &available); | ||
| 1349 | } | ||
| 1350 | //------------------------------------------------------------------------------ | ||
| 1351 | |||
| 1352 | //============================================================================== | ||
| 1353 | /// @ingroup cpp_kodi_vfs_General | ||
| 1354 | /// @brief Return the file name from given complate path string. | ||
| 1355 | /// | ||
| 1356 | /// @param[in] path The complete path include file and directory | ||
| 1357 | /// @return Filename from path | ||
| 1358 | /// | ||
| 1359 | /// | ||
| 1360 | /// ------------------------------------------------------------------------ | ||
| 1361 | /// | ||
| 1362 | /// **Example:** | ||
| 1363 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1364 | /// #include <kodi/Filesystem.h> | ||
| 1365 | /// ... | ||
| 1366 | /// std::string fileName = kodi::vfs::GetFileName("special://temp/kodi.log"); | ||
| 1367 | /// fprintf(stderr, "File name is '%s'\n", fileName.c_str()); | ||
| 1368 | /// ~~~~~~~~~~~~~ | ||
| 1369 | /// | ||
| 1370 | inline std::string ATTRIBUTE_HIDDEN GetFileName(const std::string& path) | ||
| 1371 | { | ||
| 1372 | /* find the last slash */ | ||
| 1373 | const size_t slash = path.find_last_of("/\\"); | ||
| 1374 | return path.substr(slash + 1); | ||
| 1375 | } | ||
| 1376 | //------------------------------------------------------------------------------ | ||
| 1377 | |||
| 1378 | //============================================================================== | ||
| 1379 | /// @ingroup cpp_kodi_vfs_General | ||
| 1380 | /// @brief Return the directory name from given complate path string. | ||
| 1381 | /// | ||
| 1382 | /// @param[in] path The complete path include file and directory | ||
| 1383 | /// @return Directory name from path | ||
| 1384 | /// | ||
| 1385 | /// | ||
| 1386 | /// ------------------------------------------------------------------------ | ||
| 1387 | /// | ||
| 1388 | /// **Example:** | ||
| 1389 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1390 | /// #include <kodi/Filesystem.h> | ||
| 1391 | /// ... | ||
| 1392 | /// std::string dirName = kodi::vfs::GetDirectoryName("special://temp/kodi.log"); | ||
| 1393 | /// fprintf(stderr, "Directory name is '%s'\n", dirName.c_str()); | ||
| 1394 | /// ~~~~~~~~~~~~~ | ||
| 1395 | /// | ||
| 1396 | inline std::string ATTRIBUTE_HIDDEN GetDirectoryName(const std::string& path) | ||
| 1397 | { | ||
| 1398 | // Will from a full filename return the directory the file resides in. | ||
| 1399 | // Keeps the final slash at end and possible |option=foo options. | ||
| 1400 | |||
| 1401 | size_t iPosSlash = path.find_last_of("/\\"); | ||
| 1402 | if (iPosSlash == std::string::npos) | ||
| 1403 | return ""; // No slash, so no path (ignore any options) | ||
| 1404 | |||
| 1405 | size_t iPosBar = path.rfind('|'); | ||
| 1406 | if (iPosBar == std::string::npos) | ||
| 1407 | return path.substr(0, iPosSlash + 1); // Only path | ||
| 1408 | |||
| 1409 | return path.substr(0, iPosSlash + 1) + path.substr(iPosBar); // Path + options | ||
| 1410 | } | ||
| 1411 | //------------------------------------------------------------------------------ | ||
| 1412 | |||
| 1413 | //============================================================================== | ||
| 1414 | /// @ingroup cpp_kodi_vfs_General | ||
| 1415 | /// @brief Remove the slash on given path name. | ||
| 1416 | /// | ||
| 1417 | /// @param[in,out] path The complete path | ||
| 1418 | /// | ||
| 1419 | /// | ||
| 1420 | /// ------------------------------------------------------------------------ | ||
| 1421 | /// | ||
| 1422 | /// **Example:** | ||
| 1423 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1424 | /// #include <kodi/Filesystem.h> | ||
| 1425 | /// ... | ||
| 1426 | /// std::string dirName = "special://temp/"; | ||
| 1427 | /// kodi::vfs::RemoveSlashAtEnd(dirName); | ||
| 1428 | /// fprintf(stderr, "Directory name is '%s'\n", dirName.c_str()); | ||
| 1429 | /// ~~~~~~~~~~~~~ | ||
| 1430 | /// | ||
| 1431 | inline void ATTRIBUTE_HIDDEN RemoveSlashAtEnd(std::string& path) | ||
| 1432 | { | ||
| 1433 | if (!path.empty()) | ||
| 1434 | { | ||
| 1435 | char last = path[path.size() - 1]; | ||
| 1436 | if (last == '/' || last == '\\') | ||
| 1437 | path.erase(path.size() - 1); | ||
| 1438 | } | ||
| 1439 | } | ||
| 1440 | //------------------------------------------------------------------------------ | ||
| 1441 | |||
| 1442 | //============================================================================== | ||
| 1443 | /// @ingroup cpp_kodi_vfs_General | ||
| 1444 | /// @brief Return a size aligned to the chunk size at least as large as the | ||
| 1445 | /// chunk size. | ||
| 1446 | /// | ||
| 1447 | /// @param[in] chunk The chunk size | ||
| 1448 | /// @param[in] minimum The minimum size (or maybe the minimum number of chunks?) | ||
| 1449 | /// @return The aligned size | ||
| 1450 | /// | ||
| 1451 | inline unsigned int ATTRIBUTE_HIDDEN GetChunkSize(unsigned int chunk, unsigned int minimum) | ||
| 1452 | { | ||
| 1453 | if (chunk) | ||
| 1454 | return chunk * ((minimum + chunk - 1) / chunk); | ||
| 1455 | else | ||
| 1456 | return minimum; | ||
| 1457 | } | ||
| 1458 | //------------------------------------------------------------------------------ | ||
| 1459 | |||
| 1460 | //============================================================================== | ||
| 1461 | /// @ingroup cpp_kodi_vfs_General | ||
| 1462 | /// @brief Checks the given path contains a known internet protocol. | ||
| 1463 | /// | ||
| 1464 | /// About following protocols are the path checked: | ||
| 1465 | /// | Protocol | Return true condition | Protocol | Return true condition | ||
| 1466 | /// |----------|-----------------------|----------|----------------------- | ||
| 1467 | /// | **dav** | strictCheck = true | **rtmps** | always | ||
| 1468 | /// | **davs** | strictCheck = true | **rtmpt** | always | ||
| 1469 | /// | **ftp** | strictCheck = true | **rtmpte** | always | ||
| 1470 | /// | **ftps** | strictCheck = true | **rtp** | always | ||
| 1471 | /// | **http** | always | **rtsp** | always | ||
| 1472 | /// | **https**| always | **sdp** | always | ||
| 1473 | /// | **mms** | always | **sftp** | strictCheck = true | ||
| 1474 | /// | **mmsh** | always | **stack** | always | ||
| 1475 | /// | **mmst** | always | **tcp** | always | ||
| 1476 | /// | **rtmp** | always | **udp** | always | ||
| 1477 | /// | **rtmpe**| always | | | | ||
| 1478 | /// | ||
| 1479 | /// @param[in] path To checked path/URL | ||
| 1480 | /// @param[in] strictCheck [opt] If True the set of protocols used will be | ||
| 1481 | /// extended to include ftp, ftps, dav, davs and sftp. | ||
| 1482 | /// @return True if path is to a internet stream, false otherwise | ||
| 1483 | /// | ||
| 1484 | /// | ||
| 1485 | /// ------------------------------------------------------------------------ | ||
| 1486 | /// | ||
| 1487 | /// **Example:** | ||
| 1488 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1489 | /// #include <kodi/Filesystem.h> | ||
| 1490 | /// ... | ||
| 1491 | /// // Check should return false | ||
| 1492 | /// fprintf(stderr, "File name 1 is internet stream '%s' (should no)\n", | ||
| 1493 | /// kodi::vfs::IsInternetStream("D:/my-file.mkv") ? "yes" : "no"); | ||
| 1494 | /// | ||
| 1495 | /// // Check should return true | ||
| 1496 | /// fprintf(stderr, "File name 2 is internet stream '%s' (should yes)\n", | ||
| 1497 | /// kodi::vfs::IsInternetStream("http://distribution.bbb3d.renderfarming.net/video/mp4/bbb_sunflower_1080p_30fps_normal.mp4") ? "yes" : "no"); | ||
| 1498 | /// | ||
| 1499 | /// // Check should return false | ||
| 1500 | /// fprintf(stderr, "File name 1 is internet stream '%s' (should no)\n", | ||
| 1501 | /// kodi::vfs::IsInternetStream("ftp://do-somewhere.com/the-file.mkv") ? "yes" : "no", false); | ||
| 1502 | /// | ||
| 1503 | /// // Check should return true | ||
| 1504 | /// fprintf(stderr, "File name 1 is internet stream '%s' (should yes)\n", | ||
| 1505 | /// kodi::vfs::IsInternetStream("ftp://do-somewhere.com/the-file.mkv") ? "yes" : "no", true); | ||
| 1506 | /// ~~~~~~~~~~~~~ | ||
| 1507 | /// | ||
| 1508 | inline bool ATTRIBUTE_HIDDEN IsInternetStream(const std::string& path, bool strictCheck = false) | ||
| 1509 | { | ||
| 1510 | using namespace kodi::addon; | ||
| 1511 | |||
| 1512 | return CAddonBase::m_interface->toKodi->kodi_filesystem->is_internet_stream( | ||
| 1513 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str(), strictCheck); | ||
| 1514 | } | ||
| 1515 | //------------------------------------------------------------------------------ | ||
| 1516 | |||
| 1517 | //============================================================================== | ||
| 1518 | /// @ingroup cpp_kodi_vfs_General | ||
| 1519 | /// @brief Checks whether the specified path refers to a local network. | ||
| 1520 | /// | ||
| 1521 | /// In difference to @ref IsHostOnLAN() include this more deeper checks where | ||
| 1522 | /// also handle Kodi's special protocol and stacks. | ||
| 1523 | /// | ||
| 1524 | /// @param[in] path To checked path | ||
| 1525 | /// @return True if path is on LAN, false otherwise | ||
| 1526 | /// | ||
| 1527 | /// @note Check includes @ref IsHostOnLAN() too. | ||
| 1528 | /// | ||
| 1529 | /// | ||
| 1530 | /// ------------------------------------------------------------------------ | ||
| 1531 | /// | ||
| 1532 | /// **Example:** | ||
| 1533 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1534 | /// #include <kodi/Filesystem.h> | ||
| 1535 | /// ... | ||
| 1536 | /// // Check should return true | ||
| 1537 | /// bool lan = kodi::vfs::IsOnLAN("smb://path/to/file"); | ||
| 1538 | /// ~~~~~~~~~~~~~ | ||
| 1539 | /// | ||
| 1540 | inline bool ATTRIBUTE_HIDDEN IsOnLAN(const std::string& path) | ||
| 1541 | { | ||
| 1542 | using namespace kodi::addon; | ||
| 1543 | |||
| 1544 | return CAddonBase::m_interface->toKodi->kodi_filesystem->is_on_lan( | ||
| 1545 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 1546 | } | ||
| 1547 | //------------------------------------------------------------------------------ | ||
| 1548 | |||
| 1549 | //============================================================================== | ||
| 1550 | /// @ingroup cpp_kodi_vfs_General | ||
| 1551 | /// @brief Checks specified path for external network. | ||
| 1552 | /// | ||
| 1553 | /// @param[in] path To checked path | ||
| 1554 | /// @return True if path is remote, false otherwise | ||
| 1555 | /// | ||
| 1556 | /// @note This does not apply to the local network. | ||
| 1557 | /// | ||
| 1558 | /// | ||
| 1559 | /// ------------------------------------------------------------------------ | ||
| 1560 | /// | ||
| 1561 | /// **Example:** | ||
| 1562 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1563 | /// #include <kodi/Filesystem.h> | ||
| 1564 | /// ... | ||
| 1565 | /// // Check should return true | ||
| 1566 | /// bool remote = kodi::vfs::IsRemote("http://path/to/file"); | ||
| 1567 | /// ~~~~~~~~~~~~~ | ||
| 1568 | /// | ||
| 1569 | inline bool ATTRIBUTE_HIDDEN IsRemote(const std::string& path) | ||
| 1570 | { | ||
| 1571 | using namespace kodi::addon; | ||
| 1572 | |||
| 1573 | return CAddonBase::m_interface->toKodi->kodi_filesystem->is_remote( | ||
| 1574 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 1575 | } | ||
| 1576 | //------------------------------------------------------------------------------ | ||
| 1577 | |||
| 1578 | //============================================================================== | ||
| 1579 | /// @ingroup cpp_kodi_vfs_General | ||
| 1580 | /// @brief Checks whether the given path refers to the own system. | ||
| 1581 | /// | ||
| 1582 | /// @param[in] path To checked path | ||
| 1583 | /// @return True if path is local, false otherwise | ||
| 1584 | /// | ||
| 1585 | inline bool ATTRIBUTE_HIDDEN IsLocal(const std::string& path) | ||
| 1586 | { | ||
| 1587 | using namespace kodi::addon; | ||
| 1588 | |||
| 1589 | return CAddonBase::m_interface->toKodi->kodi_filesystem->is_local( | ||
| 1590 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 1591 | } | ||
| 1592 | //------------------------------------------------------------------------------ | ||
| 1593 | |||
| 1594 | //============================================================================== | ||
| 1595 | /// @ingroup cpp_kodi_vfs_General | ||
| 1596 | /// @brief Checks specified path is a regular URL, e.g. "someprotocol://path/to/file" | ||
| 1597 | /// | ||
| 1598 | /// @return True if file item is URL, false otherwise | ||
| 1599 | /// | ||
| 1600 | /// | ||
| 1601 | /// ------------------------------------------------------------------------ | ||
| 1602 | /// | ||
| 1603 | /// **Example:** | ||
| 1604 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1605 | /// #include <kodi/Filesystem.h> | ||
| 1606 | /// ... | ||
| 1607 | /// | ||
| 1608 | /// bool isURL; | ||
| 1609 | /// // Check should return true | ||
| 1610 | /// isURL = kodi::vfs::IsURL("someprotocol://path/to/file"); | ||
| 1611 | /// | ||
| 1612 | /// // Check should return false | ||
| 1613 | /// isURL = kodi::vfs::IsURL("/path/to/file"); | ||
| 1614 | /// ~~~~~~~~~~~~~ | ||
| 1615 | /// | ||
| 1616 | inline bool ATTRIBUTE_HIDDEN IsURL(const std::string& path) | ||
| 1617 | { | ||
| 1618 | using namespace kodi::addon; | ||
| 1619 | |||
| 1620 | return CAddonBase::m_interface->toKodi->kodi_filesystem->is_url( | ||
| 1621 | CAddonBase::m_interface->toKodi->kodiBase, path.c_str()); | ||
| 1622 | } | ||
| 1623 | //-------------------------------------------------------------------------- | ||
| 1624 | |||
| 1625 | //============================================================================ | ||
| 1626 | /// @ingroup cpp_kodi_vfs_General | ||
| 1627 | /// @brief To get HTTP header information. | ||
| 1628 | /// | ||
| 1629 | /// @param[in] url URL source of the data | ||
| 1630 | /// @param[out] header The @ref cpp_kodi_vfs_Defs_HttpHeader | ||
| 1631 | /// @return true if successfully done, otherwise false | ||
| 1632 | /// | ||
| 1633 | /// | ||
| 1634 | /// ------------------------------------------------------------------------ | ||
| 1635 | /// | ||
| 1636 | /// @copydetails cpp_kodi_vfs_Defs_HttpHeader_Help | ||
| 1637 | /// | ||
| 1638 | /// ------------------------------------------------------------------------ | ||
| 1639 | /// | ||
| 1640 | /// **Example:** | ||
| 1641 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1642 | /// #include <kodi/Filesystem.h> | ||
| 1643 | /// ... | ||
| 1644 | /// kodi::vfs::HttpHeader header; | ||
| 1645 | /// bool ret = kodi::vfs::GetHttpHeader(url, header); | ||
| 1646 | /// ... | ||
| 1647 | /// ~~~~~~~~~~~~~ | ||
| 1648 | /// | ||
| 1649 | inline bool ATTRIBUTE_HIDDEN GetHttpHeader(const std::string& url, HttpHeader& header) | ||
| 1650 | { | ||
| 1651 | using namespace ::kodi::addon; | ||
| 1652 | |||
| 1653 | return CAddonBase::m_interface->toKodi->kodi_filesystem->get_http_header( | ||
| 1654 | CAddonBase::m_interface->toKodi->kodiBase, url.c_str(), &header.m_handle); | ||
| 1655 | } | ||
| 1656 | //---------------------------------------------------------------------------- | ||
| 1657 | |||
| 1658 | //============================================================================ | ||
| 1659 | /// @ingroup cpp_kodi_vfs_General | ||
| 1660 | /// @brief Get file mime type. | ||
| 1661 | /// | ||
| 1662 | /// @param[in] url URL source of the data | ||
| 1663 | /// @param[out] mimeType the mime type of the URL | ||
| 1664 | /// @param[in] useragent to be used when retrieving the MimeType [opt] | ||
| 1665 | /// @return true if successfully done, otherwise false | ||
| 1666 | /// | ||
| 1667 | /// | ||
| 1668 | /// ------------------------------------------------------------------------ | ||
| 1669 | /// | ||
| 1670 | /// **Example:** | ||
| 1671 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1672 | /// #include <kodi/Filesystem.h> | ||
| 1673 | /// ... | ||
| 1674 | /// std::string mimeType;. | ||
| 1675 | /// if (kodi::vfs::GetMimeType(url, mimeType)) | ||
| 1676 | /// fprintf(stderr, "The mime type is '%s'\n", mimeType.c_str()); | ||
| 1677 | /// ... | ||
| 1678 | /// ~~~~~~~~~~~~~ | ||
| 1679 | /// | ||
| 1680 | inline bool ATTRIBUTE_HIDDEN GetMimeType(const std::string& url, | ||
| 1681 | std::string& mimeType, | ||
| 1682 | const std::string& useragent = "") | ||
| 1683 | { | ||
| 1684 | using namespace ::kodi::addon; | ||
| 1685 | |||
| 1686 | char* cMimeType = nullptr; | ||
| 1687 | bool ret = CAddonBase::m_interface->toKodi->kodi_filesystem->get_mime_type( | ||
| 1688 | CAddonBase::m_interface->toKodi->kodiBase, url.c_str(), &cMimeType, useragent.c_str()); | ||
| 1689 | if (cMimeType != nullptr) | ||
| 1690 | { | ||
| 1691 | mimeType = cMimeType; | ||
| 1692 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 1693 | cMimeType); | ||
| 1694 | } | ||
| 1695 | return ret; | ||
| 1696 | } | ||
| 1697 | //---------------------------------------------------------------------------- | ||
| 1698 | |||
| 1699 | //============================================================================ | ||
| 1700 | /// @ingroup cpp_kodi_vfs_General | ||
| 1701 | /// @brief Get file content-type. | ||
| 1702 | /// | ||
| 1703 | /// @param[in] url URL source of the data | ||
| 1704 | /// @param[out] content The returned type | ||
| 1705 | /// @param[in] useragent to be used when retrieving the MimeType [opt] | ||
| 1706 | /// @return true if successfully done, otherwise false | ||
| 1707 | /// | ||
| 1708 | /// | ||
| 1709 | /// ------------------------------------------------------------------------ | ||
| 1710 | /// | ||
| 1711 | /// **Example:** | ||
| 1712 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1713 | /// #include <kodi/Filesystem.h> | ||
| 1714 | /// ... | ||
| 1715 | /// std::string content;. | ||
| 1716 | /// if (kodi::vfs::GetContentType(url, content)) | ||
| 1717 | /// fprintf(stderr, "The content type is '%s'\n", content.c_str()); | ||
| 1718 | /// ... | ||
| 1719 | /// ~~~~~~~~~~~~~ | ||
| 1720 | /// | ||
| 1721 | inline bool ATTRIBUTE_HIDDEN GetContentType(const std::string& url, | ||
| 1722 | std::string& content, | ||
| 1723 | const std::string& useragent = "") | ||
| 1724 | { | ||
| 1725 | using namespace ::kodi::addon; | ||
| 1726 | |||
| 1727 | char* cContent = nullptr; | ||
| 1728 | bool ret = CAddonBase::m_interface->toKodi->kodi_filesystem->get_content_type( | ||
| 1729 | CAddonBase::m_interface->toKodi->kodiBase, url.c_str(), &cContent, useragent.c_str()); | ||
| 1730 | if (cContent != nullptr) | ||
| 1731 | { | ||
| 1732 | content = cContent; | ||
| 1733 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 1734 | cContent); | ||
| 1735 | } | ||
| 1736 | return ret; | ||
| 1737 | } | ||
| 1738 | //---------------------------------------------------------------------------- | ||
| 1739 | |||
| 1740 | //============================================================================ | ||
| 1741 | /// @ingroup cpp_kodi_vfs_General | ||
| 1742 | /// @brief Get cookies stored by CURL in RFC 2109 format. | ||
| 1743 | /// | ||
| 1744 | /// @param[in] url URL source of the data | ||
| 1745 | /// @param[out] cookies The text list of available cookies | ||
| 1746 | /// @return true if successfully done, otherwise false | ||
| 1747 | /// | ||
| 1748 | /// | ||
| 1749 | /// ------------------------------------------------------------------------ | ||
| 1750 | /// | ||
| 1751 | /// **Example:** | ||
| 1752 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1753 | /// #include <kodi/Filesystem.h> | ||
| 1754 | /// ... | ||
| 1755 | /// std::string url = ""; | ||
| 1756 | /// std::string cookies; | ||
| 1757 | /// bool ret = kodi::vfs::GetCookies(url, cookies); | ||
| 1758 | /// fprintf(stderr, "Cookies from URL '%s' are '%s' (return was %s)\n", | ||
| 1759 | /// url.c_str(), cookies.c_str(), ret ? "true" : "false"); | ||
| 1760 | /// ... | ||
| 1761 | /// ~~~~~~~~~~~~~ | ||
| 1762 | /// | ||
| 1763 | inline bool ATTRIBUTE_HIDDEN GetCookies(const std::string& url, std::string& cookies) | ||
| 1764 | { | ||
| 1765 | using namespace ::kodi::addon; | ||
| 1766 | |||
| 1767 | char* cCookies = nullptr; | ||
| 1768 | bool ret = CAddonBase::m_interface->toKodi->kodi_filesystem->get_cookies( | ||
| 1769 | CAddonBase::m_interface->toKodi->kodiBase, url.c_str(), &cCookies); | ||
| 1770 | if (cCookies != nullptr) | ||
| 1771 | { | ||
| 1772 | cookies = cCookies; | ||
| 1773 | CAddonBase::m_interface->toKodi->free_string(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 1774 | cCookies); | ||
| 1775 | } | ||
| 1776 | return ret; | ||
| 1777 | } | ||
| 1778 | //---------------------------------------------------------------------------- | ||
| 1779 | |||
| 1780 | //}}} | ||
| 1781 | |||
| 1782 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ | ||
| 1783 | // "C++" CFile class | ||
| 1784 | //{{{ | ||
| 1785 | |||
| 1786 | //============================================================================== | ||
| 1787 | /// @defgroup cpp_kodi_vfs_CFile 4. class CFile | ||
| 1788 | /// @ingroup cpp_kodi_vfs | ||
| 1789 | /// | ||
| 1790 | /// @brief **Creatable class for virtual file server control**\n | ||
| 1791 | /// To perform file read/write with Kodi's filesystem parts. | ||
| 1792 | /// | ||
| 1793 | /// CFile is the class used for handling Files in Kodi. This class can be used | ||
| 1794 | /// for creating, reading, writing and modifying files. It directly provides unbuffered, binary disk input/output services | ||
| 1795 | /// | ||
| 1796 | /// It has the header @ref Filesystem.h "#include <kodi/Filesystem.h>" be included | ||
| 1797 | /// to enjoy it. | ||
| 1798 | /// | ||
| 1799 | /// | ||
| 1800 | /// ------------------------------------------------------------------------ | ||
| 1801 | /// | ||
| 1802 | /// **Example:** | ||
| 1803 | /// ~~~~~~~~~~~~~{.cpp} | ||
| 1804 | /// #include <kodi/Filesystem.h> | ||
| 1805 | /// | ||
| 1806 | /// ... | ||
| 1807 | /// | ||
| 1808 | /// /* Create the needed file handle class */ | ||
| 1809 | /// kodi::vfs::CFile myFile(); | ||
| 1810 | /// | ||
| 1811 | /// /* In this example we use the user path for the add-on */ | ||
| 1812 | /// std::string file = kodi::GetUserPath() + "/myFile.txt"; | ||
| 1813 | /// | ||
| 1814 | /// /* Now create and open the file or overwrite if present */ | ||
| 1815 | /// myFile.OpenFileForWrite(file, true); | ||
| 1816 | /// | ||
| 1817 | /// const char* str = "I love Kodi!"; | ||
| 1818 | /// | ||
| 1819 | /// /* write string */ | ||
| 1820 | /// myFile.Write(str, sizeof(str)); | ||
| 1821 | /// | ||
| 1822 | /// /* On this way the Close() is not needed to call, becomes done from destructor */ | ||
| 1823 | /// | ||
| 1824 | /// ~~~~~~~~~~~~~ | ||
| 1825 | /// | ||
| 1826 | //@{ | ||
| 1827 | class ATTRIBUTE_HIDDEN CFile | ||
| 1828 | { | ||
| 1829 | public: | ||
| 1830 | //============================================================================ | ||
| 1831 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1832 | /// @brief Construct a new, unopened file. | ||
| 1833 | /// | ||
| 1834 | CFile() = default; | ||
| 1835 | //---------------------------------------------------------------------------- | ||
| 1836 | |||
| 1837 | //============================================================================ | ||
| 1838 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1839 | /// @brief <b>`Close()`</b> is called from the destructor, so explicitly | ||
| 1840 | /// closing the file isn't required. | ||
| 1841 | /// | ||
| 1842 | virtual ~CFile() { Close(); } | ||
| 1843 | //-------------------------------------------------------------------------- | ||
| 1844 | |||
| 1845 | //========================================================================== | ||
| 1846 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1847 | /// @brief Open the file with filename via Kodi's @ref cpp_kodi_vfs_CFile | ||
| 1848 | /// "CFile". Needs to be closed by calling Close() when done. | ||
| 1849 | /// | ||
| 1850 | /// @param[in] filename The filename to open. | ||
| 1851 | /// @param[in] flags [opt] The flags to pass, see @ref OpenFileFlags | ||
| 1852 | /// @return True on success or false on failure | ||
| 1853 | /// | ||
| 1854 | bool OpenFile(const std::string& filename, unsigned int flags = 0) | ||
| 1855 | { | ||
| 1856 | using namespace kodi::addon; | ||
| 1857 | |||
| 1858 | Close(); | ||
| 1859 | m_file = CAddonBase::m_interface->toKodi->kodi_filesystem->open_file( | ||
| 1860 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str(), flags); | ||
| 1861 | return m_file != nullptr; | ||
| 1862 | } | ||
| 1863 | //-------------------------------------------------------------------------- | ||
| 1864 | |||
| 1865 | //========================================================================== | ||
| 1866 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1867 | /// @brief Open the file with filename via Kodi's @ref cpp_kodi_vfs_CFile | ||
| 1868 | /// "CFile" in write mode. Needs to be closed by calling Close() when | ||
| 1869 | /// done. | ||
| 1870 | /// | ||
| 1871 | /// @note Related folders becomes created if not present. | ||
| 1872 | /// | ||
| 1873 | /// @param[in] filename The filename to open. | ||
| 1874 | /// @param[in] overwrite True to overwrite, false otherwise. | ||
| 1875 | /// @return True on success or false on failure | ||
| 1876 | /// | ||
| 1877 | bool OpenFileForWrite(const std::string& filename, bool overwrite = false) | ||
| 1878 | { | ||
| 1879 | using namespace kodi::addon; | ||
| 1880 | |||
| 1881 | Close(); | ||
| 1882 | |||
| 1883 | // Try to open the file. If it fails, check if we need to create the directory first | ||
| 1884 | // This way we avoid checking if the directory exists every time | ||
| 1885 | m_file = CAddonBase::m_interface->toKodi->kodi_filesystem->open_file_for_write( | ||
| 1886 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str(), overwrite); | ||
| 1887 | if (!m_file) | ||
| 1888 | { | ||
| 1889 | std::string cacheDirectory = kodi::vfs::GetDirectoryName(filename); | ||
| 1890 | if (CAddonBase::m_interface->toKodi->kodi_filesystem->directory_exists( | ||
| 1891 | CAddonBase::m_interface->toKodi->kodiBase, cacheDirectory.c_str()) || | ||
| 1892 | CAddonBase::m_interface->toKodi->kodi_filesystem->create_directory( | ||
| 1893 | CAddonBase::m_interface->toKodi->kodiBase, cacheDirectory.c_str())) | ||
| 1894 | m_file = CAddonBase::m_interface->toKodi->kodi_filesystem->open_file_for_write( | ||
| 1895 | CAddonBase::m_interface->toKodi->kodiBase, filename.c_str(), overwrite); | ||
| 1896 | } | ||
| 1897 | return m_file != nullptr; | ||
| 1898 | } | ||
| 1899 | //-------------------------------------------------------------------------- | ||
| 1900 | |||
| 1901 | //========================================================================== | ||
| 1902 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1903 | /// @brief Check file is opened. | ||
| 1904 | /// | ||
| 1905 | /// @return True on open or false on closed or failure | ||
| 1906 | /// | ||
| 1907 | bool IsOpen() const { return m_file != nullptr; } | ||
| 1908 | //-------------------------------------------------------------------------- | ||
| 1909 | |||
| 1910 | //========================================================================== | ||
| 1911 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1912 | /// @brief Close an open file. | ||
| 1913 | /// | ||
| 1914 | void Close() | ||
| 1915 | { | ||
| 1916 | using namespace kodi::addon; | ||
| 1917 | |||
| 1918 | if (!m_file) | ||
| 1919 | return; | ||
| 1920 | CAddonBase::m_interface->toKodi->kodi_filesystem->close_file( | ||
| 1921 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 1922 | m_file = nullptr; | ||
| 1923 | } | ||
| 1924 | //-------------------------------------------------------------------------- | ||
| 1925 | |||
| 1926 | //========================================================================== | ||
| 1927 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1928 | /// @brief Create a Curl representation | ||
| 1929 | /// | ||
| 1930 | /// @param[in] url The URL of the Type. | ||
| 1931 | /// @return True on success or false on failure | ||
| 1932 | /// | ||
| 1933 | bool CURLCreate(const std::string& url) | ||
| 1934 | { | ||
| 1935 | using namespace kodi::addon; | ||
| 1936 | |||
| 1937 | m_file = CAddonBase::m_interface->toKodi->kodi_filesystem->curl_create( | ||
| 1938 | CAddonBase::m_interface->toKodi->kodiBase, url.c_str()); | ||
| 1939 | return m_file != nullptr; | ||
| 1940 | } | ||
| 1941 | //-------------------------------------------------------------------------- | ||
| 1942 | |||
| 1943 | //========================================================================== | ||
| 1944 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1945 | /// @brief Add options to the curl file created with CURLCreate. | ||
| 1946 | /// | ||
| 1947 | /// @param[in] type Option type to set, see @ref CURLOptiontype | ||
| 1948 | /// @param[in] name Name of the option | ||
| 1949 | /// @param[in] value Value of the option | ||
| 1950 | /// @return True on success or false on failure | ||
| 1951 | /// | ||
| 1952 | bool CURLAddOption(CURLOptiontype type, const std::string& name, const std::string& value) | ||
| 1953 | { | ||
| 1954 | using namespace kodi::addon; | ||
| 1955 | |||
| 1956 | if (!m_file) | ||
| 1957 | { | ||
| 1958 | kodi::Log(ADDON_LOG_ERROR, "kodi::vfs::CURLCreate(...) needed to call before!"); | ||
| 1959 | return false; | ||
| 1960 | } | ||
| 1961 | return CAddonBase::m_interface->toKodi->kodi_filesystem->curl_add_option( | ||
| 1962 | CAddonBase::m_interface->toKodi->kodiBase, m_file, type, name.c_str(), value.c_str()); | ||
| 1963 | } | ||
| 1964 | //-------------------------------------------------------------------------- | ||
| 1965 | |||
| 1966 | //========================================================================== | ||
| 1967 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1968 | /// @brief Open the curl file created with CURLCreate. | ||
| 1969 | /// | ||
| 1970 | /// @param[in] flags [opt] The flags to pass, see @ref OpenFileFlags | ||
| 1971 | /// @return True on success or false on failure | ||
| 1972 | /// | ||
| 1973 | bool CURLOpen(unsigned int flags = 0) | ||
| 1974 | { | ||
| 1975 | using namespace kodi::addon; | ||
| 1976 | |||
| 1977 | if (!m_file) | ||
| 1978 | { | ||
| 1979 | kodi::Log(ADDON_LOG_ERROR, "kodi::vfs::CURLCreate(...) needed to call before!"); | ||
| 1980 | return false; | ||
| 1981 | } | ||
| 1982 | return CAddonBase::m_interface->toKodi->kodi_filesystem->curl_open( | ||
| 1983 | CAddonBase::m_interface->toKodi->kodiBase, m_file, flags); | ||
| 1984 | } | ||
| 1985 | //-------------------------------------------------------------------------- | ||
| 1986 | |||
| 1987 | //========================================================================== | ||
| 1988 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 1989 | /// @brief Read from an open file. | ||
| 1990 | /// | ||
| 1991 | /// @param[in] ptr The buffer to store the data in. | ||
| 1992 | /// @param[in] size The size of the buffer. | ||
| 1993 | /// @return number of successfully read bytes if any bytes were read and | ||
| 1994 | /// stored in buffer, zero if no bytes are available to read (end of | ||
| 1995 | /// file was reached) or undetectable error occur, -1 in case of any | ||
| 1996 | /// explicit error | ||
| 1997 | /// | ||
| 1998 | ssize_t Read(void* ptr, size_t size) | ||
| 1999 | { | ||
| 2000 | using namespace kodi::addon; | ||
| 2001 | |||
| 2002 | if (!m_file) | ||
| 2003 | return -1; | ||
| 2004 | return CAddonBase::m_interface->toKodi->kodi_filesystem->read_file( | ||
| 2005 | CAddonBase::m_interface->toKodi->kodiBase, m_file, ptr, size); | ||
| 2006 | } | ||
| 2007 | //-------------------------------------------------------------------------- | ||
| 2008 | |||
| 2009 | //========================================================================== | ||
| 2010 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2011 | /// @brief Read a string from an open file. | ||
| 2012 | /// | ||
| 2013 | /// @param[out] line The buffer to store the data in. | ||
| 2014 | /// @return True when a line was read, false otherwise. | ||
| 2015 | /// | ||
| 2016 | bool ReadLine(std::string& line) | ||
| 2017 | { | ||
| 2018 | using namespace kodi::addon; | ||
| 2019 | |||
| 2020 | line.clear(); | ||
| 2021 | if (!m_file) | ||
| 2022 | return false; | ||
| 2023 | // TODO: Read 1024 chars into buffer. If file position advanced that many | ||
| 2024 | // chars, we didn't hit a newline. Otherwise, if file position is 1 or 2 | ||
| 2025 | // past the number of bytes read, we read (and skipped) a newline sequence. | ||
| 2026 | char buffer[1025]; | ||
| 2027 | if (CAddonBase::m_interface->toKodi->kodi_filesystem->read_file_string( | ||
| 2028 | CAddonBase::m_interface->toKodi->kodiBase, m_file, buffer, sizeof(buffer))) | ||
| 2029 | { | ||
| 2030 | line = buffer; | ||
| 2031 | return !line.empty(); | ||
| 2032 | } | ||
| 2033 | return false; | ||
| 2034 | } | ||
| 2035 | //-------------------------------------------------------------------------- | ||
| 2036 | |||
| 2037 | //========================================================================== | ||
| 2038 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2039 | /// @brief Write to a file opened in write mode. | ||
| 2040 | /// | ||
| 2041 | /// @param[in] ptr Pointer to the data to write, converted to a <b>`const void*`</b>. | ||
| 2042 | /// @param[in] size Size of the data to write. | ||
| 2043 | /// @return number of successfully written bytes if any bytes were written, | ||
| 2044 | /// zero if no bytes were written and no detectable error occur,-1 | ||
| 2045 | /// in case of any explicit error | ||
| 2046 | /// | ||
| 2047 | ssize_t Write(const void* ptr, size_t size) | ||
| 2048 | { | ||
| 2049 | using namespace kodi::addon; | ||
| 2050 | |||
| 2051 | if (!m_file) | ||
| 2052 | return -1; | ||
| 2053 | return CAddonBase::m_interface->toKodi->kodi_filesystem->write_file( | ||
| 2054 | CAddonBase::m_interface->toKodi->kodiBase, m_file, ptr, size); | ||
| 2055 | } | ||
| 2056 | //-------------------------------------------------------------------------- | ||
| 2057 | |||
| 2058 | //========================================================================== | ||
| 2059 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2060 | /// @brief Flush buffered data. | ||
| 2061 | /// | ||
| 2062 | /// If the given stream was open for writing (or if it was open for updating | ||
| 2063 | /// and the last i/o operation was an output operation) any unwritten data | ||
| 2064 | /// in its output buffer is written to the file. | ||
| 2065 | /// | ||
| 2066 | /// The stream remains open after this call. | ||
| 2067 | /// | ||
| 2068 | /// When a file is closed, either because of a call to close or because the | ||
| 2069 | /// class is destructed, all the buffers associated with it are | ||
| 2070 | /// automatically flushed. | ||
| 2071 | /// | ||
| 2072 | void Flush() | ||
| 2073 | { | ||
| 2074 | using namespace kodi::addon; | ||
| 2075 | |||
| 2076 | if (!m_file) | ||
| 2077 | return; | ||
| 2078 | CAddonBase::m_interface->toKodi->kodi_filesystem->flush_file( | ||
| 2079 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2080 | } | ||
| 2081 | //-------------------------------------------------------------------------- | ||
| 2082 | |||
| 2083 | //========================================================================== | ||
| 2084 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2085 | /// @brief Set the file's current position. | ||
| 2086 | /// | ||
| 2087 | /// The whence argument is optional and defaults to SEEK_SET (0) | ||
| 2088 | /// | ||
| 2089 | /// @param[in] position the position that you want to seek to | ||
| 2090 | /// @param[in] whence [optional] offset relative to You can set the value of | ||
| 2091 | /// whence to one of three things: | ||
| 2092 | /// | Value | int | Description | | ||
| 2093 | /// |:--------:|:---:|:----------------------------------------------------| | ||
| 2094 | /// | SEEK_SET | 0 | position is relative to the beginning of the file. This is probably what you had in mind anyway, and is the most commonly used value for whence. | ||
| 2095 | /// | SEEK_CUR | 1 | position is relative to the current file pointer position. So, in effect, you can say, "Move to my current position plus 30 bytes," or, "move to my current position minus 20 bytes." | ||
| 2096 | /// | SEEK_END | 2 | position is relative to the end of the file. Just like SEEK_SET except from the other end of the file. Be sure to use negative values for offset if you want to back up from the end of the file, instead of going past the end into oblivion. | ||
| 2097 | /// | ||
| 2098 | /// @return Returns the resulting offset location as measured in bytes from | ||
| 2099 | /// the beginning of the file. On error, the value -1 is returned. | ||
| 2100 | /// | ||
| 2101 | int64_t Seek(int64_t position, int whence = SEEK_SET) | ||
| 2102 | { | ||
| 2103 | using namespace kodi::addon; | ||
| 2104 | |||
| 2105 | if (!m_file) | ||
| 2106 | return -1; | ||
| 2107 | return CAddonBase::m_interface->toKodi->kodi_filesystem->seek_file( | ||
| 2108 | CAddonBase::m_interface->toKodi->kodiBase, m_file, position, whence); | ||
| 2109 | } | ||
| 2110 | //-------------------------------------------------------------------------- | ||
| 2111 | |||
| 2112 | //========================================================================== | ||
| 2113 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2114 | /// @brief Truncate a file to the requested size. | ||
| 2115 | /// | ||
| 2116 | /// @param[in] size The new max size. | ||
| 2117 | /// @return New size? On error, the value -1 is returned. | ||
| 2118 | /// | ||
| 2119 | int Truncate(int64_t size) | ||
| 2120 | { | ||
| 2121 | using namespace kodi::addon; | ||
| 2122 | |||
| 2123 | if (!m_file) | ||
| 2124 | return -1; | ||
| 2125 | return CAddonBase::m_interface->toKodi->kodi_filesystem->truncate_file( | ||
| 2126 | CAddonBase::m_interface->toKodi->kodiBase, m_file, size); | ||
| 2127 | } | ||
| 2128 | //-------------------------------------------------------------------------- | ||
| 2129 | |||
| 2130 | //========================================================================== | ||
| 2131 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2132 | /// @brief The current offset in an open file. | ||
| 2133 | /// | ||
| 2134 | /// @return The requested offset. On error, the value -1 is returned. | ||
| 2135 | /// | ||
| 2136 | int64_t GetPosition() const | ||
| 2137 | { | ||
| 2138 | using namespace kodi::addon; | ||
| 2139 | |||
| 2140 | if (!m_file) | ||
| 2141 | return -1; | ||
| 2142 | return CAddonBase::m_interface->toKodi->kodi_filesystem->get_file_position( | ||
| 2143 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2144 | } | ||
| 2145 | //-------------------------------------------------------------------------- | ||
| 2146 | |||
| 2147 | //========================================================================== | ||
| 2148 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2149 | /// @brief Get the file size of an open file. | ||
| 2150 | /// | ||
| 2151 | /// @return The requested size. On error, the value -1 is returned. | ||
| 2152 | /// | ||
| 2153 | int64_t GetLength() const | ||
| 2154 | { | ||
| 2155 | using namespace kodi::addon; | ||
| 2156 | |||
| 2157 | if (!m_file) | ||
| 2158 | return -1; | ||
| 2159 | return CAddonBase::m_interface->toKodi->kodi_filesystem->get_file_length( | ||
| 2160 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2161 | } | ||
| 2162 | //-------------------------------------------------------------------------- | ||
| 2163 | |||
| 2164 | //========================================================================== | ||
| 2165 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2166 | /// @brief Checks the file access is on end position. | ||
| 2167 | /// | ||
| 2168 | /// @return If you've reached the end of the file, AtEnd() returns true. | ||
| 2169 | /// | ||
| 2170 | bool AtEnd() const | ||
| 2171 | { | ||
| 2172 | using namespace kodi::addon; | ||
| 2173 | |||
| 2174 | if (!m_file) | ||
| 2175 | return true; | ||
| 2176 | int64_t length = CAddonBase::m_interface->toKodi->kodi_filesystem->get_file_length( | ||
| 2177 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2178 | int64_t position = CAddonBase::m_interface->toKodi->kodi_filesystem->get_file_position( | ||
| 2179 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2180 | return position >= length; | ||
| 2181 | } | ||
| 2182 | //-------------------------------------------------------------------------- | ||
| 2183 | |||
| 2184 | //========================================================================== | ||
| 2185 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2186 | /// @brief Get the chunk size for an open file. | ||
| 2187 | /// | ||
| 2188 | /// @return The requested size. On error, the value -1 is returned. | ||
| 2189 | /// | ||
| 2190 | int GetChunkSize() const | ||
| 2191 | { | ||
| 2192 | using namespace kodi::addon; | ||
| 2193 | |||
| 2194 | if (!m_file) | ||
| 2195 | return -1; | ||
| 2196 | return CAddonBase::m_interface->toKodi->kodi_filesystem->get_file_chunk_size( | ||
| 2197 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2198 | } | ||
| 2199 | //-------------------------------------------------------------------------- | ||
| 2200 | |||
| 2201 | //========================================================================== | ||
| 2202 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2203 | /// @brief To check seek possible on current stream by file. | ||
| 2204 | /// | ||
| 2205 | /// @return true if seek possible, false if not | ||
| 2206 | /// | ||
| 2207 | bool IoControlGetSeekPossible() const | ||
| 2208 | { | ||
| 2209 | using namespace kodi::addon; | ||
| 2210 | |||
| 2211 | if (!m_file) | ||
| 2212 | return false; | ||
| 2213 | return CAddonBase::m_interface->toKodi->kodi_filesystem->io_control_get_seek_possible( | ||
| 2214 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2215 | } | ||
| 2216 | //-------------------------------------------------------------------------- | ||
| 2217 | |||
| 2218 | //========================================================================== | ||
| 2219 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2220 | /// @brief To check a running stream on file for state of his cache. | ||
| 2221 | /// | ||
| 2222 | /// @param[in] status Information about current cache status | ||
| 2223 | /// @return true if successfull done, false otherwise | ||
| 2224 | /// | ||
| 2225 | /// | ||
| 2226 | /// @copydetails cpp_kodi_vfs_Defs_CacheStatus_Help | ||
| 2227 | /// | ||
| 2228 | bool IoControlGetCacheStatus(CacheStatus& status) const | ||
| 2229 | { | ||
| 2230 | using namespace kodi::addon; | ||
| 2231 | |||
| 2232 | if (!m_file) | ||
| 2233 | return false; | ||
| 2234 | return CAddonBase::m_interface->toKodi->kodi_filesystem->io_control_get_cache_status( | ||
| 2235 | CAddonBase::m_interface->toKodi->kodiBase, m_file, status); | ||
| 2236 | } | ||
| 2237 | //-------------------------------------------------------------------------- | ||
| 2238 | |||
| 2239 | //========================================================================== | ||
| 2240 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2241 | /// @brief Unsigned int with speed limit for caching in bytes per second. | ||
| 2242 | /// | ||
| 2243 | /// @param[in] rate Cache rate size to use | ||
| 2244 | /// @return true if successfull done, false otherwise | ||
| 2245 | /// | ||
| 2246 | bool IoControlSetCacheRate(unsigned int rate) | ||
| 2247 | { | ||
| 2248 | using namespace kodi::addon; | ||
| 2249 | |||
| 2250 | if (!m_file) | ||
| 2251 | return false; | ||
| 2252 | return CAddonBase::m_interface->toKodi->kodi_filesystem->io_control_set_cache_rate( | ||
| 2253 | CAddonBase::m_interface->toKodi->kodiBase, m_file, rate); | ||
| 2254 | } | ||
| 2255 | //-------------------------------------------------------------------------- | ||
| 2256 | |||
| 2257 | //========================================================================== | ||
| 2258 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2259 | /// @brief Enable/disable retry within the protocol handler (if supported). | ||
| 2260 | /// | ||
| 2261 | /// @param[in] retry To set the retry, true for use, false for not | ||
| 2262 | /// @return true if successfull done, false otherwise | ||
| 2263 | /// | ||
| 2264 | bool IoControlSetRetry(bool retry) | ||
| 2265 | { | ||
| 2266 | using namespace kodi::addon; | ||
| 2267 | |||
| 2268 | if (!m_file) | ||
| 2269 | return false; | ||
| 2270 | return CAddonBase::m_interface->toKodi->kodi_filesystem->io_control_set_retry( | ||
| 2271 | CAddonBase::m_interface->toKodi->kodiBase, m_file, retry); | ||
| 2272 | } | ||
| 2273 | //-------------------------------------------------------------------------- | ||
| 2274 | |||
| 2275 | //========================================================================== | ||
| 2276 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2277 | /// @brief Retrieve a file property. | ||
| 2278 | /// | ||
| 2279 | /// @param[in] type The type of the file property to retrieve the value for | ||
| 2280 | /// @param[in] name The name of a named property value (e.g. Header) | ||
| 2281 | /// @return value of requested property, empty on failure / non-existance | ||
| 2282 | /// | ||
| 2283 | const std::string GetPropertyValue(FilePropertyTypes type, const std::string& name) const | ||
| 2284 | { | ||
| 2285 | using namespace kodi::addon; | ||
| 2286 | |||
| 2287 | if (!m_file) | ||
| 2288 | { | ||
| 2289 | kodi::Log(ADDON_LOG_ERROR, | ||
| 2290 | "kodi::vfs::CURLCreate(...) needed to call before GetPropertyValue!"); | ||
| 2291 | return ""; | ||
| 2292 | } | ||
| 2293 | std::vector<std::string> values = GetPropertyValues(type, name); | ||
| 2294 | if (values.empty()) | ||
| 2295 | { | ||
| 2296 | return ""; | ||
| 2297 | } | ||
| 2298 | return values[0]; | ||
| 2299 | } | ||
| 2300 | //-------------------------------------------------------------------------- | ||
| 2301 | |||
| 2302 | //========================================================================== | ||
| 2303 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2304 | /// @brief Retrieve file property values. | ||
| 2305 | /// | ||
| 2306 | /// @param[in] type The type of the file property values to retrieve the value for | ||
| 2307 | /// @param[in] name The name of the named property (e.g. Header) | ||
| 2308 | /// @return values of requested property, empty vector on failure / non-existance | ||
| 2309 | /// | ||
| 2310 | const std::vector<std::string> GetPropertyValues(FilePropertyTypes type, | ||
| 2311 | const std::string& name) const | ||
| 2312 | { | ||
| 2313 | using namespace kodi::addon; | ||
| 2314 | |||
| 2315 | if (!m_file) | ||
| 2316 | { | ||
| 2317 | kodi::Log(ADDON_LOG_ERROR, | ||
| 2318 | "kodi::vfs::CURLCreate(...) needed to call before GetPropertyValues!"); | ||
| 2319 | return std::vector<std::string>(); | ||
| 2320 | } | ||
| 2321 | int numValues = 0; | ||
| 2322 | char** res(CAddonBase::m_interface->toKodi->kodi_filesystem->get_property_values( | ||
| 2323 | CAddonBase::m_interface->toKodi->kodiBase, m_file, type, name.c_str(), &numValues)); | ||
| 2324 | if (res) | ||
| 2325 | { | ||
| 2326 | std::vector<std::string> vecReturn; | ||
| 2327 | for (int i = 0; i < numValues; ++i) | ||
| 2328 | { | ||
| 2329 | vecReturn.emplace_back(res[i]); | ||
| 2330 | } | ||
| 2331 | CAddonBase::m_interface->toKodi->free_string_array(CAddonBase::m_interface->toKodi->kodiBase, | ||
| 2332 | res, numValues); | ||
| 2333 | return vecReturn; | ||
| 2334 | } | ||
| 2335 | return std::vector<std::string>(); | ||
| 2336 | } | ||
| 2337 | //-------------------------------------------------------------------------- | ||
| 2338 | |||
| 2339 | //========================================================================== | ||
| 2340 | /// @ingroup cpp_kodi_vfs_CFile | ||
| 2341 | /// @brief Get the current download speed of file if loaded from web. | ||
| 2342 | /// | ||
| 2343 | /// @return The current download speed. | ||
| 2344 | /// | ||
| 2345 | double GetFileDownloadSpeed() const | ||
| 2346 | { | ||
| 2347 | using namespace kodi::addon; | ||
| 2348 | |||
| 2349 | if (!m_file) | ||
| 2350 | return 0.0; | ||
| 2351 | return CAddonBase::m_interface->toKodi->kodi_filesystem->get_file_download_speed( | ||
| 2352 | CAddonBase::m_interface->toKodi->kodiBase, m_file); | ||
| 2353 | } | ||
| 2354 | //-------------------------------------------------------------------------- | ||
| 2355 | |||
| 2356 | private: | ||
| 2357 | void* m_file = nullptr; | ||
| 2358 | }; | ||
| 2359 | //@} | ||
| 2360 | //------------------------------------------------------------------------------ | ||
| 2361 | |||
| 2362 | //}}} | ||
| 2363 | |||
| 2364 | } /* namespace vfs */ | ||
| 2365 | } /* namespace kodi */ | ||
| 2366 | |||
| 2367 | #endif /* __cplusplus */ | ||
