FileSystemTools

Type Aliases

FILE (

RED_OPENFILE_FUNCTION

bool(*

RED_CLOSEFILE_FUNCTION

bool(*

RED_READFILE_FUNCTION

bool(*

RED_WRITEFILE_FUNCTION

bool(*

RED_SEEKFILE_FUNCTION

Functions

RED_RC

FileExists

RED_RC

FileSize

RED_RC

FileSeek

RED_RC

FilePosition

RED_RC

OpenFile

RED_RC

CloseFile

RED_RC

ReadFromFile

RED_RC

ReadFromFile

RED_RC

ReadLineFromFile

RED_RC

WriteToFile

RED_RC

WriteToFile

void

SetOpenFunction

RED_OPENFILE_FUNCTION

GetOpenFunction

void

SetCloseFunction

RED_CLOSEFILE_FUNCTION

GetCloseFunction

void

SetReadFunction

RED_READFILE_FUNCTION

GetReadFunction

void

SetWriteFunction

RED_WRITEFILE_FUNCTION

GetWriteFunction

void

SetSeekFunction

RED_SEEKFILE_FUNCTION

GetSeekFunction

String

GetWorkingDirectory

void

SetWorkingDirectory

Detailed Description

class FileSystemTools : public RED::Object

Standard file system API.

@related The .red File Format, class RED::IREDFile

The RED::FileSystemTools class defines the I/O methods used to read, write and parse files. By default, the RED::FileSystemTools class uses the standard C I/O routines (fopen, fclose, …). All methods can be overloaded.

Public Types

typedef FILE *(*RED_OPENFILE_FUNCTION)(const RED::String &iFileName, const RED::String &iMode)

RED_OPENFILE_FUNCTION: Type definition for the file open callback.

typedef bool (*RED_CLOSEFILE_FUNCTION)(FILE *iFileHandle)

RED_CLOSEFILE_FUNCTION: Type definition for the file close callback.

typedef bool (*RED_READFILE_FUNCTION)(FILE *iFileHandle, unsigned char *oData, RED::uint64 iDataSize)

RED_READFILE_FUNCTION: Type definition for the file read callback.

typedef bool (*RED_WRITEFILE_FUNCTION)(FILE *iFileHandle, const unsigned char *oData, RED::uint64 iDataSize)

RED_WRITEFILE_FUNCTION: Type definition for the file write callback.

typedef bool (*RED_SEEKFILE_FUNCTION)(RED::uint64 nOffset)

RED_SEEKFILE_FUNCTION: Type definition for the file seek callback.

Public Static Functions

static RED_RC FileExists(const RED::String &iResourcePath, int iAccessMode)

Checks the existence of a file or directory, with permissions.

This method checks that the file or folder resource identified by iResourcePath exists, and can be accessed provided the given resource permission in iAccessMode.

Parameters
  • iResourcePathRED::String path or name of the searched resource.

  • iAccessMode – Requested permission. Only one permission flag should be provided: RED_FILE_EXISTS, RED_FILE_READ_OK or RED_FILE_WRITE_OK.

Returns

RED_OK when the resource exists and can be accessed with the requested permissions,

RED_BAD_PARAM if invalid parameters were provided to the routine,

RED_ACCESS_DENIED if the resource exists, but can’t be accessed with the requested iAccessMode,

RED_FILE_NOT_FOUND if the resource does not exists,

RED_FAIL otherwise.

static RED_RC FileSize(RED::uint64 &oSize, FILE *iFileHandler)

Returns the size of the input file.

Parameters
  • oSize – Reference to the returned file size.

  • iFileHandler – Handler to the file.

Returns

RED_OK when the resource exists and can be accessed with the requested permissions,

RED_BAD_PARAM if invalid parameters were provided to the routine,

RED_FAIL otherwise.

static RED_RC FileSeek(FILE *iFileHandler, RED::uint64 iPosition, unsigned int iBase)

Seeks into a file at a given position.

This method works exactly as the fseek C function does.

Parameters
  • iFileHandler – Handler to the file.

  • iPosition – New position inside the file.

  • iBase – Base in which the iPosition is expressed.

Returns

RED_OK on success,

RED_BAD_PARAM if invalid parameters were provided to the routine,

RED_FAIL otherwise.

static RED_RC FilePosition(RED::uint64 &oPosition, FILE *iFileHandler)

Gets the current position inside the given file.

This method returns the same result than the ftell C function.

Parameters
  • oPosition – Reference to the returned file position.

  • iFileHandler – Handler to the file.

Returns

RED_OK on success,

RED_BAD_PARAM if invalid parameters were provided to the routine,

RED_FAIL otherwise.

static RED_RC OpenFile(FILE *&oHandler, const RED::String &iFilename, const RED::String &iMode)

Opens the specified file with an access mode.

This routine opens a file with the requested access mode.

The result of this routine is a file handler returned in oFileHandler, with a return code set to RED_OK. If the routine fails to open the file with the requested permissions, oFileHandler is set to NULL, and the return code is set to the corresponding found error.

This routine may be used to create a new file, when the “w” (write) mode is set.

Parameters
  • oHandler – FILE type handler returned by the routine for further usage.

  • iFilename – Access path to the file to be loaded. May be an absolute path or a relative path.

  • iMode – Standard ANSI modes supported by the fopen routine: usually a combination of “r”,”w”,”a”, for respectively read, write and append modes, plus possible modifiers “t” for text and “b” for binary file write. Refer to the ANSI specifications for the list of all possible modes. Note that the mode is not specified as an unicode string, but as a basic (char *) string.

Returns

RED_OK when the file was successfully accessed from the file system, RED_BAD_PARAM if invalid parameters were provided to the routine, RED_ACCESS_DENIED when the file could not be accessed with the requested rights, RED_FILE_NOT_FOUND when the file could not be found using iFilename, or RED_FAIL when an unexpected error occurred.

static RED_RC CloseFile(FILE *iHandler)

Closes a file specified by it’s handler.

This routine closes a file that was opened using the REDOpenFile function preciously. The file handler must be provided to correctly close the file.

Parameters

iHandler – Handler to the file that needs to be closed.

Returns

RED_OK when the file could be successfully closed, RED_FAIL otherwise, most likely meaning that an invalid handler was provided to the call.

static RED_RC ReadFromFile(unsigned char *oBuffer, RED::uint64 &oRead, FILE *iHandler, RED::uint64 iLength)

Reads a string buffer from file.

This function tries to read a chunk of data of iLength from the file identified by ioHandler. oRead is set to the really read number of bytes, that have been stored in ioBuffer. ioBuffer must have been sufficiently allocated before the call.

Parameters
  • oBuffer – Target buffer with the read contents.

  • oRead – Number of effectively read bytes. Can be returned between 0 and iLength depending on what could be read.

  • iHandler – Handler to the read file.

  • iLength – Requested byte length to read.

Returns

RED_OK when the requested length was read,

RED_BAD_PARAM if the method has received an invalid parameter.

static RED_RC ReadFromFile(unsigned char *oBuffer, RED::uint64 &oRead, FILE *iHandler, RED::uint64 iOffset, RED::uint64 iLength)

Reads a string buffer from a file starting at a position.

This function tries to read a chunk of data of iLength from the file identified by ioHandler, starting at iOffset bytes from the beginning of the file. oRead is set to the really read number of bytes, that have been stored in ioBuffer. ioBuffer must have been sufficiently allocated before the call.

Parameters
  • oBuffer – Target buffer with the read contents.

  • oRead – Number of effectively read bytes. Can be returned between 0 and iLength depending on what could be read.

  • iHandler – Handler to the read file.

  • iOffset – Offset in bytes from the beginning of the file to start reading from. the value must be positive.

  • iLength – Requested byte length to read. If zero, the method replaces the current reading position to iOffset.

Returns

RED_OK when the requested length was read,

RED_BAD_PARAM if the method has received an invalid parameter.

static RED_RC ReadLineFromFile(unsigned char *oBuffer, RED::uint64 &oRead, bool &oEnd, FILE *iHandler, RED::uint64 iLength)

Reads a single line from a text file.

This function reads chars from a text file until either the end of file is reached or the end of the current text line is reached or the buffer size specified by iLength is reached.

Parameters
  • oBuffer – Target buffer with the read contents.

  • oRead – Number of effectively read bytes. Does not include the line ending character.

  • oEnd – Returned set to true if end of file has been reached.

  • iHandler – Handler to the read file.

  • iLength – Size of the target buffer. Must be greater than 0.

Returns

RED_OK if the line was successfully read,

RED_BAD_PARAM if the method has received an invalid parameter,

RED_FILE_DATA_ERROR when the reading could not be completed for the requested bytes count.

static RED_RC WriteToFile(FILE *iHandler, const unsigned char *iBuffer, RED::uint64 iLength)

Writes a string buffer in a file.

This function tries to write the provided iBuffer in the file identified by iHandler. iLength bytes are written to the file.

Parameters
  • iHandler – Handler to the written file.

  • iBufferString buffer written down to the file.

  • iLength – Number of bytes to write from iBuffer.

Returns

RED_OK when the requested length was successfully written, RED_FILE_WRITE_ERROR when the writing could not be completed for the number of requested bytes, RED_BAD_PARAM if the method has received an invalid parameter.

static RED_RC WriteToFile(FILE *iHandler, const RED::String &iString)

Writes a string buffer in a file.

This function tries to write the provided iString in the file identified by iHandler.

Parameters
  • iHandler – Handler to the written file.

  • iStringString written down to the file.

Returns

RED_OK when the requested length was successfully written, RED_FAIL when the writing could not be completed for the number of requested bytes, RED_BAD_PARAM if the method has received an invalid parameter.

static void SetOpenFunction(RED_OPENFILE_FUNCTION iOpenCB)

Sets a custom open function.

The custom open function will be called in place of the standard fopen C function.

If set to NULL, the REDFileSystemTools will use the default standard C function.

Parameters

iOpenCB – new open function to use.

static RED_OPENFILE_FUNCTION GetOpenFunction()

Gets the custom open function.

Returns

A pointer to the custom open function if it exists, NULL otherwise.

static void SetCloseFunction(RED_CLOSEFILE_FUNCTION iCloseCB)

Sets a custom close function.

The custom close function will be called in place of the standard fclose C function.

If set to NULL, the REDFileSystemTools will use the default standard C function.

Parameters

iCloseCB – new close function to use.

static RED_CLOSEFILE_FUNCTION GetCloseFunction()

Gets the custom close function.

Returns

A pointer to the custom close function if it exists, NULL otherwise.

static void SetReadFunction(RED_READFILE_FUNCTION iReadCB)

Sets a custom read function.

The custom open function will be called in place of the standard fread C function.

If set to NULL, the REDFileSystemTools will use the default standard C function.

Parameters

iReadCB – new read function to use.

static RED_READFILE_FUNCTION GetReadFunction()

Gets the custom read function.

Returns

A pointer to the custom read function if it exists, NULL otherwise.

static void SetWriteFunction(RED_WRITEFILE_FUNCTION iWriteCB)

Sets a custom write function.

The custom open function will be called in place of the standard fwrite C function.

If set to NULL, the REDFileSystemTools will use the default standard C function.

Parameters

iWriteCB – new write function to use.

static RED_WRITEFILE_FUNCTION GetWriteFunction()

Gets the custom write function.

Returns

A pointer to the custom write function if it exists, NULL otherwise.

static void SetSeekFunction(RED_SEEKFILE_FUNCTION iSeekCB)

Sets a custom seek function.

The custom open function will be called in place of the standard fseek C function.

If set to NULL, the REDFileSystemTools will use the default standard C function.

Parameters

iSeekCB – new seek function to use.

static RED_SEEKFILE_FUNCTION GetSeekFunction()

Gets the custom seek function.

Returns

A pointer to the custom seek function if it exists, NULL otherwise.

static RED::String GetWorkingDirectory()

Gets the current working directory.

Returns

The path of the current directory.

static void SetWorkingDirectory(const RED::String &iDefaultDir)

Sets the current working directory.

Parameters

iDefaultDirString containing the path to the working directory.