Kodi Documentation 18.0
Kodi is an open source media player and entertainment hub.

Virtual file server control More...

Functions

 kodi::vfs::CFile::CFile ()=default
 Construct a new, unopened file. More...
 
virtual kodi::vfs::CFile::~CFile ()
 Close() is called from the destructor, so explicitly closing the file isn't required. More...
 
bool kodi::vfs::CFile::OpenFile (const std::string &filename, unsigned int flags=0)
 Open the file with filename via Kodi's CFile. Needs to be closed by calling Close() when done. More...
 
bool kodi::vfs::CFile::OpenFileForWrite (const std::string &filename, bool overwrite=false)
 Open the file with filename via Kodi's CFile in write mode. Needs to be closed by calling Close() when done. More...
 
bool kodi::vfs::CFile::CURLCreate (const std::string &url)
 Create a Curl representation. More...
 
bool kodi::vfs::CFile::CURLAddOption (CURLOptiontype type, const std::string &name, const std::string &value)
 Add options to the curl file created with CURLCreate. More...
 
bool kodi::vfs::CFile::CURLOpen (unsigned int flags=0)
 Open the curl file created with CURLCreate. More...
 
ssize_t kodi::vfs::CFile::Read (void *ptr, size_t size)
 Read from an open file. More...
 
bool kodi::vfs::CFile::ReadLine (std::string &line)
 Read a string from an open file. More...
 
ssize_t kodi::vfs::CFile::Write (const void *ptr, size_t size)
 Write to a file opened in write mode. More...
 
void kodi::vfs::CFile::Flush ()
 Flush buffered data. More...
 
int64_t kodi::vfs::CFile::Seek (int64_t position, int whence=SEEK_SET)
 Set the file's current position. More...
 
int kodi::vfs::CFile::Truncate (int64_t size)
 Truncate a file to the requested size. More...
 
int64_t kodi::vfs::CFile::GetPosition ()
 The current offset in an open file. More...
 
int64_t kodi::vfs::CFile::GetLength ()
 Get the file size of an open file. More...
 
bool kodi::vfs::CFile::AtEnd ()
 Checks the file access is on end position. More...
 
void kodi::vfs::CFile::Close ()
 Close an open file. More...
 
int kodi::vfs::CFile::GetChunkSize ()
 Get the chunk size for an open file. More...
 
const std::string kodi::vfs::CFile::GetPropertyValue (FilePropertyTypes type, const std::string &name) const
 retrieve a file property More...
 
const std::vector< std::string > kodi::vfs::CFile::GetPropertyValues (FilePropertyTypes type, const std::string &name) const
 retrieve file property values More...
 
double kodi::vfs::CFile::GetFileDownloadSpeed ()
 Get the current download speed of file if loaded from web. More...
 

Detailed Description

Virtual file server control

CFile is the class used for handling Files in Kodi. This class can be used for creating, reading, writing and modifying files. It directly provides unbuffered, binary disk input/output services

It has the header #include <kodi/Filesystem.h> be included to enjoy it.


Example:

...
/* Create the needed file handle class *&zwj;/
kodi::vfs::CFile myFile();
/* In this example we use the user path for the add-on *&zwj;/
std::string file = kodi::GetUserPath() + "/myFile.txt";
/* Now create and open the file or overwrite if present *&zwj;/
myFile.OpenFileForWrite(file, true);
const char* str = "I love Kodi!";
/* write string *&zwj;/
myFile.Write(str, sizeof(str));
/* On this way the Close() is not needed to call, becomes done from destructor *&zwj;/

Function Documentation

◆ AtEnd()

bool kodi::vfs::CFile::AtEnd ( )
inline

Checks the file access is on end position.

Returns
If you've reached the end of the file, AtEnd() returns true.

◆ CFile()

kodi::vfs::CFile::CFile ( )
default

Construct a new, unopened file.

◆ Close()

void kodi::vfs::CFile::Close ( )
inline

Close an open file.

◆ CURLAddOption()

bool kodi::vfs::CFile::CURLAddOption ( CURLOptiontype  type,
const std::string &  name,
const std::string &  value 
)
inline

Add options to the curl file created with CURLCreate.

Parameters
[in]typeoption type to set, see CURLOptiontype
[in]namename of the option
[in]valuevalue of the option
Returns
True on success or false on failure

◆ CURLCreate()

bool kodi::vfs::CFile::CURLCreate ( const std::string &  url)
inline

Create a Curl representation.

Parameters
[in]urlthe URL of the Type.
Returns
True on success or false on failure

◆ CURLOpen()

bool kodi::vfs::CFile::CURLOpen ( unsigned int  flags = 0)
inline

Open the curl file created with CURLCreate.

Parameters
[in]flags[opt] The flags to pass, see OpenFileFlags
Returns
True on success or false on failure

◆ Flush()

void kodi::vfs::CFile::Flush ( )
inline

Flush buffered data.

If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file.

The stream remains open after this call.

When a file is closed, either because of a call to close or because the class is destructed, all the buffers associated with it are automatically flushed.

◆ GetChunkSize()

int kodi::vfs::CFile::GetChunkSize ( )
inline

Get the chunk size for an open file.

Returns
The requested size. On error, the value -1 is returned.

◆ GetFileDownloadSpeed()

double kodi::vfs::CFile::GetFileDownloadSpeed ( )
inline

Get the current download speed of file if loaded from web.

Returns
The current download speed.

◆ GetLength()

int64_t kodi::vfs::CFile::GetLength ( )
inline

Get the file size of an open file.

Returns
The requested size. On error, the value -1 is returned.

◆ GetPosition()

int64_t kodi::vfs::CFile::GetPosition ( )
inline

The current offset in an open file.

Returns
The requested offset. On error, the value -1 is returned.

◆ GetPropertyValue()

const std::string kodi::vfs::CFile::GetPropertyValue ( FilePropertyTypes  type,
const std::string &  name 
) const
inline

retrieve a file property

Parameters
[in]typeThe type of the file property to retrieve the value for
[in]nameThe name of a named property value (e.g. Header)
Returns
value of requested property, empty on failure / non-existance

◆ GetPropertyValues()

const std::vector< std::string > kodi::vfs::CFile::GetPropertyValues ( FilePropertyTypes  type,
const std::string &  name 
) const
inline

retrieve file property values

Parameters
[in]typeThe type of the file property values to retrieve the value for
[in]nameThe name of the named property (e.g. Header)
Returns
values of requested property, empty vector on failure / non-existance

◆ OpenFile()

bool kodi::vfs::CFile::OpenFile ( const std::string &  filename,
unsigned int  flags = 0 
)
inline

Open the file with filename via Kodi's CFile. Needs to be closed by calling Close() when done.

Parameters
[in]filenameThe filename to open.
[in]flags[opt] The flags to pass, see OpenFileFlags
Returns
True on success or false on failure

◆ OpenFileForWrite()

bool kodi::vfs::CFile::OpenFileForWrite ( const std::string &  filename,
bool  overwrite = false 
)
inline

Open the file with filename via Kodi's CFile in write mode. Needs to be closed by calling Close() when done.

Note
Related folders becomes created if not present.
Parameters
[in]filenameThe filename to open.
[in]overwriteTrue to overwrite, false otherwise.
Returns
True on success or false on failure

◆ Read()

ssize_t kodi::vfs::CFile::Read ( void ptr,
size_t  size 
)
inline

Read from an open file.

Parameters
[in]ptrThe buffer to store the data in.
[in]sizeThe size of the buffer.
Returns
number of successfully read bytes if any bytes were read and stored in buffer, zero if no bytes are available to read (end of file was reached) or undetectable error occur, -1 in case of any explicit error

◆ ReadLine()

bool kodi::vfs::CFile::ReadLine ( std::string &  line)
inline

Read a string from an open file.

Parameters
[out]lineThe buffer to store the data in.
Returns
True when a line was read, false otherwise.

◆ Seek()

int64_t kodi::vfs::CFile::Seek ( int64_t  position,
int  whence = SEEK_SET 
)
inline

Set the file's current position.

The whence argument is optional and defaults to SEEK_SET (0)

Parameters
[in]positionthe position that you want to seek to
[in]whence[optional] offset relative to You can set the value of whence to one. of three things:
Value int Description
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.
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."
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.
Returns
Returns the resulting offset location as measured in bytes from the beginning of the file. On error, the value -1 is returned.

◆ Truncate()

int kodi::vfs::CFile::Truncate ( int64_t  size)
inline

Truncate a file to the requested size.

Parameters
[in]sizeThe new max size.
Returns
New size? On error, the value -1 is returned.

◆ Write()

ssize_t kodi::vfs::CFile::Write ( const void ptr,
size_t  size 
)
inline

Write to a file opened in write mode.

Parameters
[in]ptrPointer to the data to write, converted to a const void*.
[in]sizeSize of the data to write.
Returns
number of successfully written bytes if any bytes were written, zero if no bytes were written and no detectable error occur,-1 in case of any explicit error

◆ ~CFile()

virtual kodi::vfs::CFile::~CFile ( )
inlinevirtual

Close() is called from the destructor, so explicitly closing the file isn't required.