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