PrevNextUpHome SophiaFramework UNIVERSE 5.3
SFXFile
Class for operating a file.
#include <SFXFile.h.hpp>
class SFXFile : public SFXStorage;
SFMTYPEDEFCLASS(SFXFile)

Inheritance diagram

 Inheritance diagram of SFXFileClass

Collaboration diagram

 Collaboration diagram of SFXFileClass

Description

The SFXFile class is used to perform the file operations such as creating a file, moving a file, deleting a file, reading or writing data from or into the file storage with or without the stream, retrieving file attribute information, making a temporary file etc.

Data can be read from /written into the file storage using the SFXFile class as follows:

How to use the SFXFile class

  1. Create the SFXFile instance.
  2. Open the SFXFile storage with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function.
  3. Get the input / output stream with the SFXFile::GetStreamReader / SFXFile::GetStreamWriter function.
  4. Read / write data from / into the SFXFile storage through the input / output stream.
  5. * If the input / output stream is not used, data can be read from / written into the SFXFile storage with the SFXFile::Read / SFXFile::Write function.
  6. Close the SFXFile storage with the SFXFile::Close function.
[Tip] File open mode

There are two kinds of file open modes: the Read Only mode and the Read Write mode.

In case of the file input, open the file in the read only mode with the SFXFile::OpenReadOnly function.

In case of the file output, open the file in the Read Write mode with the SFXFile::OpenReadWrite function.

[Note] File pointer

The SFXFile class has the file pointer. The SFXFile::Read / SFXFile::Write function reads / writes data from / into the file storage through the file pointer, which will advance by the amount of the read / written data.

It is possible to move the file pointer with the SFXFile::Seek / SFXFile::SeekStart / SFXFile::SeekEnd.

Or, when data is read or written using the stream, the file pointer will advance by the amount of the read / written data too.

The below is the code to read data from file.

SFCError error;  // error value
SFXFile file;    // SFXFile instance
SFXAnsiStringStreamReader reader; // input stream for reading data from file
SFXAnsiString stringFromFile;     // string to be read from file
SFXAnsiString tempString;

// open file in ReadOnly mode
if ((error = file.OpenReadOnly(SFXPath("/dir1/data.txt"))) == SFERR_NO_ERROR) {

    // get input stream for reading data from file(buffer size = 1024)
    if ((error = file.GetStreamReader(1024, &reader)) == SFERR_NO_ERROR) {

        while ((error == SFERR_NO_ERROR) && !reader.Ends()) { 

            // * repeat the following processing until the end of the file is reached

            // read data from file to input stream actually
            // up to 1024 byte of data can be read since buffer size is specified
            if ((error = reader.Fetch()) == SFERR_NO_ERROR) {

                // read data from input stream to tempString
                if ((error = reader.ReadSFXAnsiString(&tempString)) == SFERR_NO_ERROR) { 

                    // append tempString onto stringFromFile
                    stringFromFile += tempString;
                }
            }
        }
        reader.Release();
    }
    file.Close();
}
if (error != SFERR_NO_ERROR) {

    // if an error occurs
    ...
}

The below is the code to write data into file.

SFCError error;  // error value
SFXFile file;    // SFXFile instance 
SFXAnsiStringStreamWriter writer; // output stream for writing data to file
SFXAnsiString string("abcdefghijklmnopqrstuvwxyz");  // data to be written
ACharConstPtr p = string.GetBuffer();                // pointer to string
ACharConstPtr endOfString = p + string.GetLength();  // end of string

SInt32 bufferSize = 1024;

// open file in Read/Write mode
if ((error = file.OpenReadWrite(SFXPath("/dir1/data.txt"))) == SFERR_NO_ERROR) {

    // get output stream for writing data into file
    if ((error = file.GetStreamWriter(bufferSize, &writer)) == SFERR_NO_ERROR) {

        for (; (error == SFERR_NO_ERROR) && (_p < _endOfString) ; _p += bufferSize) { 

            // write size
            SInt32 size = (endOfString - p < bufferSize) ? endOfString - p : bufferSize;

            // write string to output stream
            if ((error = writer.Write(p, size)) == SFERR_NO_ERROR) {

                // write data from output stream to file actually 
                error = writer.Flush();
            }
        }
        writer.Release();
    }
    file.Close();
}
if (error != SFERR_NO_ERROR) {

    // if an error occurs
    ...
}

Reference

Storage | Stream | SFXDirectory | SFXPath | File Class

Member

Constructor/Destructor
SFXFile( Void )
Constructor of the SFXFile class.
SFXFile( SInt32 cache )
Constructor of the SFXFile class.
~SFXFile( Void )
Destructor of the SFXFile class.
Public Functions
SFCError AsSFBAStream( SFBAStreamSmpPtr result )
Convert the SFBFile instance internally managed by this file storage into the SFBAStream instance.
SFCError AsSFBSource( SFBSourceSmpPtr result )
Convert the SFBFile instance internally managed by this file storage into the SFBSource instance.
Void Cancel( Void )
Cancel the registered callback function.
Void Close( Void )
Close this file.
static
SFCError
Create( SFXPathConstRef path , Bool force = false )
Create the specified file.
static
SFCError
DeviceFreeSpace( UInt32Ptr result )
Get the number of free bytes currently available in the file system.
static
SFCError
DeviceTotalSpace( UInt32Ptr result )
Get the total room in the file system.
static
SFCError
Exists( SFXPathConstRef path , BoolPtr result )
Check whether or not the specified file exists.
SInt32 GetCacheSize( Void )
Get the cache size for operating this file storage.
static
SFCError
GetCreateDate( SFXPathConstRef path , SFXDatePtr result )
Get the creation date of the specified file.
SFXPathConstRef GetFilePath( Void )
Get the file path of this file storage.
SFBFileSmpConstRef GetSFBFile( Void )
Get the SFBFile instance managed internally by this file storage
SFBFileMgrSmpConstRef GetSFBFileMgr( Void )
Get the SFBFileMgr instance managed internally by this file storage
static
SFCError
GetSize( SFXPathConstRef path , UInt32Ptr result )
Get the size of the specified file. [in bytes]
SFCError GetStreamReader( UInt32 size , SFXStreamReaderPtr result )
Get the input stream for reading data from this file.
SFCError GetStreamReader( SFXStreamReaderPtr result )
Get the input stream for reading data from this file.
SFCError GetStreamWriter( UInt32 size , SFXStreamWriterPtr result )
Get the output stream for writing data onto this file.
SFCError GetStreamWriter( SFXStreamWriterPtr result )
Get the output stream for writing data onto this file.
static
SFCError
GetTemporaryPath( SFXPathConstRef path , SFXPathPtr result )
Get the temporary file path.
static
SFCError
GetUniquePath( SFXPathConstRef path , SFXAnsiStringConstRef prefix , SFXAnsiStringConstRef suffix , SFXPathPtr result )
Get the unique file path that does not duplicate existing ones.
static
SFCError
IsReadOnly( SFXPathConstRef path , BoolPtr result )
Check whether or not the specified file is read only.
static
SFCError
IsSystem( SFXPathConstRef path , BoolPtr result )
Check whether or not the specified file is a system file.
SFCError OpenReadOnly( SFXPathConstRef path )
Open this file in read only mode.
SFCError OpenReadWrite( SFXPathConstRef path , Bool force = false )
Open this file in read/write mode.
SFCError Read( VoidPtr buffer , UInt32Ptr size )
Read data from this file storage without input stream.
static
SFCError
Remove( SFXPathConstRef path )
Delete the specified file.
static
SFCError
Rename( SFXPathConstRef from , SFXPathConstRef to )
Move or rename the specified file.
SFCError ScheduleRead( CallbackSPP spp , VoidPtr reference )
Schedule to read data from this file storage without input stream.
SFCError ScheduleWrite( CallbackSPP spp , VoidPtr reference )
[This function cannot be used now]
SFCError Seek( SInt32 distance )
Move the file pointer by the specified amount from the current position of this file storage. [in bytes]
SFCError SeekEnd( SInt32 distance )
Move the file pointer by the specified amount from the end position of this file storage. [in bytes]
SFCError SeekStart( SInt32 distance )
Move the file pointer by the specified amount from the start position of this file storage. [in bytes]
Void SetCacheSize( SInt32 size )
Set the cache size for operating this file storage.
UInt32 Tell( Void )
Get the current position of the file pointer of this file storage.
SFCError Truncate( UInt32 position )
Truncate this file storage at the specified position.
SFCError Write( VoidConstPtr buffer , UInt32Ptr size )
Write data into this file storage without output stream.
Types
CallbackSPP (inherits from SFXStorage)
Type of the callback function for the Storage class.

SFXFile::SFXFile
Constructor of the SFXFile class.
[ public, explicit ]
SFXFile(Void);
[ public, explicit ]
SFXFile(
    SInt32 cache   // cache size
);

Description

In this constructor, cache size for file operations can be set by specifying the cache argument.

With this setting, file system performance can be tuned.

[Note] Note

When the cache argument is specified, this constructor internally calls the SFBFile::SetCacheSize function.

Reference

SFBFile::SetCacheSize | SFXFile::SetCacheSize | BREW API IFILE_SetCacheSize


SFXFile::~SFXFile
Destructor of the SFXFile class.
[ public, virtual ]
~SFXFile(Void);

Description

This destructor calls the SFXFile::Close function.

[Note] Note

Registered callback functions will be canceled.

Reference

SFXFile::Close


SFXFile::AsSFBAStream
Convert the SFBFile instance internally managed by this file storage into the SFBAStream instance.
[ public, virtual, const ]
SFCError AsSFBAStream(
    SFBAStreamSmpPtr result   // pointer to the SFBAStream instance
);

Argument

result

Specify the pointer to the SFBAStream instance.

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this file storage has been closed: SFERR_INVALID_STATE
  • If the result argument is null: SFERR_INVALID_PARAM

Description

This function converts the SFBFile instance internally managed by this file storage into the SFBAStream instance.

As a result, a pointer to the SFBAStream instance will be returned into the result argument.

[Note] Note
The contents of the file storage can be handled as the SFBAStream instance.

Reference

SFBFile | SFBAStream


SFXFile::AsSFBSource
Convert the SFBFile instance internally managed by this file storage into the SFBSource instance.
[ public, virtual, const ]
SFCError AsSFBSource(
    SFBSourceSmpPtr result   // pointer to the SFBSource instance
);

引数

result

Specify the pointer to the SFBSource instance.

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this file storage has been closed: SFERR_INVALID_STATE
  • If the result argument is null: SFERR_INVALID_PARAM

Description

This function converts the SFBFile instance internally managed by this file storage into the SFBSource instance.

As a result, a pointer to the SFBSource instance will be returned into the result argument.

[Note] Note

This function internally calls the BREW API ISOURCEUTIL_SourceFromAStream function.

[Note] Note
The contents of the file storage can be handled as the SFBSource instance.

Reference

SFBFile | SFBSource | BREW API ISOURCEUTIL_SourceFromAStream


SFXFile::Cancel
Cancel the registered callback function.
[ public, virtual ]
Void Cancel(Void);

Description

This function cancels scheduling with the SFXFile::ScheduleRead function.

Concretely, the registered callback functions are canceled.

[Note] Note

This function is called in the SFXFile::Close function.

Reference

SFXFile::ScheduleRead | SFXFile::Close


SFXFile::Close
Close this file.
[ public ]
Void Close(Void);

Description

This function closes this file.

Concretely, this function calls the SFXFile::Cancel function and releases the SFBFile instance that this file internally manages.

[Note] Note

The registered callback functions will be canceled.

[Note] Note

This function is called in the SFXFile::~SFXFile destructor.

[Tip] Tip

When suspending, resources should be released by calling this function.

Example

SFXFile file;

...

file.Close();  // close file

Reference

SFXFile::Cancel | SFXFile::~SFXFile | SFXFile::Create | SFXFile::OpenReadWrite | SFXFile::OpenReadOnly | SFBFile


SFXFile::Create
Create the specified file.
[ public, static ]
SFCError Create(
    SFXPathConstRef path   // file path
    Bool force = false     // whether or not to create directories recursively
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the path argument is a directory: SFERR_INVALID_PARAM
  • Otherwise: error that BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

This function creates the file specified in the path argument.

If the force argument is set to true, the directories which do not exist in the specified path will be created automatically.

[Note] Note

This function calls the SFBFileMgr::OpenFile function internally.

Example

// create file forcedly
// * the parent directories will be automatically created  if not exist
SFXFile::Create(SFXPath("/dir1/data.txt"), true);

Reference

SFBFileMgr::OpenFile | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::Remove | BREW API IFILEMGR_OpenFile | BREW API IFILEMGR_GetLastError


SFXFile::DeviceFreeSpace
Get the number of free bytes currently available in the file system.
[ public, static ]
SFCError DeviceFreeSpace(
    UInt32Ptr result   // pointer to the result
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null: SFERR_INVALID_PARAM
  • Otherwise: error that the BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

This function gets the number of free bytes currently available in the file system.

The number of free bytes currently available on file system will be returned to the result argument. [in bytes]

[Note] Note

This function internally calls the SFBFileMgr::GetFreeSpace function.

[Caution] Caution

In case of the Simulator, if the space available is larger than the maximum 32-bit integer value, the maximum value of a 32-bit integer is returned(0xFFFFFFFF).

Example

UInt32 space;
// get the number of free bytes currently available on file system
SFXFile::DeviceFreeSpace(&space); 

Reference

SFBFileMgr::GetFreeSpace | SFXFile::DeviceTotalSpace | BREW API IFILE_IFILEMGR_GetFreeSpace | BREW API IFILEMGR_GetLastError


SFXFile::DeviceTotalSpace
Get the total room in the file system.
[ public, static ]
SFCError DeviceTotalSpace(
    UInt32Ptr result   // pointer to the result
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null: SFERR_INVALID_PARAM
  • Otherwise: error that the BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

This function gets the total room in the file system.

The the total room in the file system will be returned to the result argument. [in bytes]

[Note] Note

This function internally calls the SFBFileMgr::GetFreeSpace function.

[Caution] Caution

In case of the Simulator, if the total room is larger than the maximum 32-bit integer value, the maximum value of a 32-bit integer is returned(0xFFFFFFFF).

Example

UInt32 space;
// get the total room in the file system
SFXFile::DeviceTotalSpace(&space); 

Reference

SFBFileMgr::GetFreeSpace | SFXFile::DeviceFreeSpace | BREW API IFILE_IFILEMGR_GetFreeSpace | BREW API IFILEMGR_GetLastError


SFXFile::Exists
Check whether or not the specified file exists.
[ public, static ]
SFCError Exists(
    SFXPathConstRef path   // file path to check
    BoolPtr result         // pointer to the result
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null or the path argument is not a file: SFERR_INVALID_PARAM
  • If failed: SFERR_FAILED

Description

The function checks whether or not the file specified in the path argument exists. The result will be returned to the result argument.

[Note] Note

This function internally calls the SFBFileMgr::Test function.

Example

Bool b;

// return value of the SFXFile::Exists function is of the SFCError type
// b: whether or not the specifiled file exists

SFXFile::Exists(SFXPath("/dir1/data.txt"), &b);

if (b) {
    // when file exist
    ...

}

Reference

SFBFileMgr::Test | SFXFile::IsReadOnly | SFXFile::IsSystem | BREW API IFILEMGR_Test


SFXFile::GetCacheSize
Get the cache size for operating this file storage.
[ public, const ]
SInt32 GetCacheSize(Void);

Return value

Cache size for operating this file storage. [in bytes]

Description

This function gets the cache size for operating this file storage, which is set with the SFXFile::SetCacheSize function or the SFXFile::SFXFile constructor. [in bytes]

Reference

SFXFile::SetCacheSize | SFXFile::SFXFile


SFXFile::GetCreateDate
Get the creation date of the specified file.
[ public, static ]
SFCError GetCreateDate(
    SFXPathConstRef path   // file path
    SFXDatePtr result      // pointer to the result
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null or the path argument is not a file: SFERR_INVALID_PARAM
  • Otherwise: error that the BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

The function gets creation date of the file specified in the path argument, which will be returned to the result argument.

[Note] Note

This function internally calls the SFBFileMgr::GetInfo function.

Example

SFXDate date; // date

// get the creation date of the file
SFXFile::GetCreateDate(SFXPath("/dir1/data.txt"), &date);

Reference

SFBFileMgr::GetInfo | BREW API IFILE_IFILEMGR_GetInfo | BREW API IFILEMGR_GetLastError


SFXFile::GetFilePath
Get the file path of this file storage.
[ public, const ]
SFXPathConstRef GetFilePath(Void);

Return value

  • If this file storage is open: path of the opened file
  • Otherwise: empty path(return value of the SFXPath::EmptyInstance function)

Description

This function gets the file path of this file storage.

If this file storage is closed, an empty path(return value of the SFXPath::EmptyInstance function) will be returned.

Example

SFXFile file;
SFXPath path("/data.txt");

// 1: 
TRACE("1: %s", file.GetFilePath().Get().GetCString());

if (file.OpenReadOnly(path) == SFERR_NO_ERROR) {

    // 2: /data.txt
    TRACE("2: %s", file.GetFilePath().Get().GetCString());

    file.Close();

    // 3: 
    TRACE("3: %s", file.GetFilePath().Get().GetCString());
}

Reference

SFXPath | SFXFile::OpenReadOnly | SFXFile::Close | SFXPath::EmptyInstance


SFXFile::GetSFBFile
Get the SFBFile instance managed internally by this file storage
[ public, const ]
SFBFileSmpConstRef GetSFBFile(Void);

Return value

SFBFile instance managed internally by this file storage.

Description

This function gets the SFBFile instance managed internally by this file storage.

Reference

SFBFile | BREW API IFile


SFXFile::GetSFBFileMgr
Get the SFBFileMgr instance managed internally by this file storage
[ public, const ]
SFBFileMgrSmpConstRef GetSFBFileMgr(Void);

Return value

SFBFileMgr instance managed internally by this file storage.

Description

This function gets the SFBFileMgr instance managed internally by this file storage.

Reference

SFBFileMgr | BREW API IFileMgr


SFXFile::GetSize
Get the size of the specified file. [in bytes]
[ public, static ]
SFCError GetSize(
    SFXPathConstRef path   // file path
    UInt32Ptr result       // pointer to the result
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null or the path argument is a directory: SFERR_INVALID_PARAM
  • Otherwise: error that the BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

The function gets the size of the file specified in the path argument, which will be returned to the result argument.

[Note] Note

This function internally calls the SFBFileMgr::GetInfo function.

Example

UInt32 size;

SFXFile::GetSize(SFXPath("/dir1/data.txt"), &size); // file size is stored in the size variable

Reference

SFBFileMgr::GetInfo | BREW API IFILE_IFILEMGR_GetInfo | BREW API IFILEMGR_GetLastError


SFXFile::GetStreamReader
Get the input stream for reading data from this file.
[ public, virtual ]
SFCError GetStreamReader(
    UInt32 size                 // buffer size
    SFXStreamReaderPtr result   // pointer to the input stream
);
[ public, virtual ]
SFCError GetStreamReader(
    SFXStreamReaderPtr result   // pointer to the input stream
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null: SFERR_INVALID_PARAM
  • If file is not open: SFERR_INVALID_STATE
  • If insufficient memory: SFERR_NO_MEMORY

Description

This function gets the input stream for reading data from this file.

If the size argument is specified, the buffer size of the input stream will be fixed with the specified value. Otherwise, the buffer size will be variable and the SFXElasticStreamReader class will be used internally.

[Tip] Tip
You have to use the SFXBinaryStreamReader, SFXAnsiStringStreamReader, or SFXWideStringStreamReader class for the input stream properly depending on the type of data to be read.

Example

SFXFile file;
SFXAnsiStringStreamReader reader;
SFXAnsiString temp;
SFXAnsiString string;       // variable to store the string data which will be read from the file
SFXPath path("/data.txt");  // file name

// open the file in the ReadOnly mode
if (file.OpenReadOnly(path) == SFERR_NO_ERROR) {
    // if succeeds
    
    // get the input stream for reading data from the file
    file.GetStreamReader(1024, &reader);
    
    // repeat the below until the end of file is reached
    while (!reader.Ends()) {
        if (reader.GetReadableSize() == 0) {
            // read data from the file onto input stream actually
            reader.Fetch();
        }
        // read data from the input stream to the temp variable
        reader >> temp;
        string += temp;
    }
    file.Close();  // close the file
}

Reference

SFXFile::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader


SFXFile::GetStreamWriter
Get the output stream for writing data onto this file.
[ public, virtual ]
SFCError GetStreamWriter(
    UInt32 size                 // buffer size
    SFXStreamWriterPtr result   // pointer to the output stream
);
[ public, virtual ]
SFCError GetStreamWriter(
    SFXStreamWriterPtr result   // pointer to the output stream
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null: SFERR_INVALID_PARAM
  • If file is not open: SFERR_INVALID_STATE
  • If insufficient memory: SFERR_NO_MEMORY

Description

This function gets the output stream for writing data onto this file.

If the size argument is specified, the buffer size of the output stream will be fixed with the specified value. Otherwise, the buffer size will be variable and the SFXElasticStreamWriter class will be used internally.

[Tip] Tip
You have to use the SFXBinaryStreamWriter, SFXAnsiStringStreamWriter, or SFXWideStringStreamWriter class for the output stream properly depending on the type of data to write.

Example

SFXFile file;
SFXAnsiStringStreamWriter writer;
SFXAnsiString string("abcdefg"); // variable to store the string data which will be written into the file, which is initialized with "abcdefg"
SFXPath path("/data.txt");       // file name

// open the file
if (file.OpenReadWrite(path) == SFERR_NO_ERROR) {
    // when success
    
    // get the output stream for writing data into the file
    file.GetStreamWriter(string.GetLength(), &writer);
    // write data from the string variable onto output stream
    writer << string;
    // write data from the output stream into the file actually
    writer.Flush();
    
    file.Close();  // close the file
}

Reference

SFXFile::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter


SFXFile::GetTemporaryPath
Get the temporary file path.
[ public, static ]
SFCError GetTemporaryPath(
    SFXPathConstRef path   // directory path in which the temporary file will be created
    SFXPathPtr result      // pointer to the temporary file path
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null or the path argument is a directory: SFERR_INVALID_PARAM
  • If failed: SFERR_FAILED

Description

This function gets the temporary file path that does not duplicate existing ones.

[Note] Note
To get the permanent file path which is not temporary one, use the SFXFile::GetUniquePath function.
[Note] Note

This function internally calls the SFXFile::GetUniquePath function by specifying "sfx" as the prefix argument and "fle.tmp" as the suffix argument.

Example

SFXPath dir("/");
SFXPath path;

if (SFXFile::GetTemporaryPath(path, &path) == SFERR_NO_ERROR) {
    // file path like "/sfx7182CBD4fle.tmp" will be obtained
    TRACE("%s", path.Get().GetCString());
}

Reference

SFXFile::GetUniquePath | SFXDirectory::GetTemporaryPath


SFXFile::GetUniquePath
Get the unique file path that does not duplicate existing ones.
[ public, static ]
SFCError GetUniquePath(
    SFXPathConstRef path           // directory path in which the file will be created
    SFXAnsiStringConstRef prefix   // prefix of file name
    SFXAnsiStringConstRef suffix   // suffix of file name
    SFXPathPtr result              // pointer to the file path
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null or the path argument is a directory: SFERR_INVALID_PARAM
  • If failed: SFERR_FAILED

Description

This function gets the unique file path that does not duplicate existing ones.

[Note] Note

Since the file name is generated using a random number, this function might fail in case many files have already existed.

If the file name generated using a random number has already existed, repeat to retry this operation up to 64 times.

Nevertheless, in case the file path that does not duplicate existing ones can not be generated, this function will fail and return SFERR_FAILED.

[Tip] Tip

To get the temporary file path which will be deleted soon, use the SFXFile::GetTemporaryPath function.

Example

SFXPath dir("/");
SFXPath path;

if (SFXFile::GetUniquePath(dir, "sfx", ".dat", &path) == SFERR_NO_ERROR) {
    // file path like "/sfx7182CBD4.dat" will be obtained
    TRACE("%s", path.Get().GetCString());
}

Reference

SFXDirectory::GetUniquePath | SFXFile::GetTemporaryPath


SFXFile::IsReadOnly
Check whether or not the specified file is read only.
[ public, static ]
SFCError IsReadOnly(
    SFXPathConstRef path   // file path to check
    BoolPtr result         // pointer to the result
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null or the path argument is not a file: SFERR_INVALID_PARAM
  • Otherwise: error that the BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

The functionc checks whether or not the file specified in the path argument is read only. The result will be returned to the result argument.

[Note] Note

This function internally calls the SFBFileMgr::GetInfo function.

Example

Bool b;

// return value of isReadOnly function is of SFCError type
// b: set by isReadOnly function whether specifiled file is read only or not

SFXFile::IsReadOnly(SFXPath("/dir1/data.txt"), &b);

if (b) {
    // when file is read only
    ...
}

Reference

SFBFileMgr::GetInfo | SFXFile::Exists | SFXFile::IsSystem | BREW API IFILEMGR_GetInfo | BREW API IFILEMGR_GetLastError


SFXFile::IsSystem
Check whether or not the specified file is a system file.
[ public, static ]
SFCError IsSystem(
    SFXPathConstRef path   // file path to check
    BoolPtr result         // pointer to the result
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the result argument is null or the path argument is not a file: SFERR_INVALID_PARAM
  • Otherwise: error that the BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

The functionc checks whether or not the file specified in the path argument is a system file. The result will be returned to the result argument.

[Note] Note

This function internally calls the SFBFileMgr::GetInfo function.

Reference

SFBFileMgr::GetInfo | SFXFile::Exists | SFXFile::IsReadOnly | BREW API IFILEMGR_GetInfo | BREW API IFILEMGR_GetLastError


SFXFile::OpenReadOnly
Open this file in read only mode.
[ public ]
SFCError OpenReadOnly(
    SFXPathConstRef path   // file path to open
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this file has already been opened: SFERR_INVALID_STATE
  • If the path argument is a directory: SFERR_INVALID_PARAM
  • If other error occurs: error that BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

This function opens the specified file in read only mode.

[Note] Note

This function internally calls the SFBFileMgr::OpenFile function.

Example

SFXFile file;

// open file in read only mode
if (file.OpenReadOnly(SFXPath("/dir1/data.txt")) == SFERR_NO_ERROR) {
    
    ...
    
    // close file
    file.Close();
}

Reference

SFBFileMgr::OpenFile | SFXFile::Create | SFXFile::OpenReadWrite | BREW API IFILEMGR_OpenFile | BREW API IFILEMGR_GetLastError


SFXFile::OpenReadWrite
Open this file in read/write mode.
[ public ]
SFCError OpenReadWrite(
    SFXPathConstRef path   // file path
    Bool force = false     // whether or not to create the file when it does not exist
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this file has already been opened: SFERR_INVALID_STATE
  • If the path argument is a directory: SFERR_INVALID_PARAM
  • If other error occurs: error that BREW API IFILEMGR_GetLastError function returns or SFERR_FAILED

Description

This function opens the specified file in read/write mode.

[Tip] Tip

If true is specified in the force argument, the specified file will be created when it does not exist.

[Note] Note

This function internally calls the SFBFileMgr::OpenFile function.

Example

SFXFile file;

// open file in read/write mode
if (file.OpenReadWrite(SFXPath("/dir1/data.txt")) == SFERR_NO_ERROR) {

    ...

    // close file
    file.Close();
}

Reference

SFBFileMgr::OpenFile | SFXFile::Create | SFXFile::OpenReadOnly | BREW API IFILEMGR_OpenFile | BREW API IFILEMGR_GetLastError


SFXFile::Read
Read data from this file storage without input stream.
[ public, virtual ]
SFCError Read(
    VoidPtr buffer   // buffer to read data
    UInt32Ptr size   
// before this function is called: size of the data to be read from the file storage; 
just after this function is returned: size of the data to be read from the file storage actually
);

Argument

buffer

Specify the buffer to read data.

size

Before calling this function, specify the buffer size to read data from the file storage. Just after this function is returned, the size of data to be read from the file storage actually will be stored into this argument.

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this file storage is not opened with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function: SFERR_INVALID_STATE
  • If the size argument is null: SFERR_INVALID_PARAM
  • If retry is necessary: AEE_STREAM_WOULDBLOCK
  • If end of this file : EFILEEOF

Description

This function reads data from this file without input stream.

*1. If this function returns AEE_STREAM_WOULDBLOCK, it will be necessary to register a callback function with the SFXFile::ScheduleRead function where data will be read with this function.

*2. If this function returns the value other than SFERR_NO_ERROR, the specified callback function will not be called.

[Note] Note

This function internally calls the SFBAStream::Read function.

[Note] Prerequisite

This file needs to be opened with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function before this function is called.

Reference

SFBAStream::Read | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::ScheduleRead | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::Seek | SFXFile::Write | BREW API IFILE_Read


SFXFile::Remove
Delete the specified file.
[ public, static ]
SFCError Remove(
    SFXPathConstRef path   // file path
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the path argument is a directory: SFERR_INVALID_PARAM
  • Otherwise: error value that the BREW API IFILEMGR_GetLastError function returns, or SFERR_FAILED

Description

This function deletes the file specified in the path argument.

[Note] Note

This function calls the SFBFileMgr::Remove function internally.

Example

if (SFXFile::Remove(SFXPath("/dir/data.txt")) == SFERR_NO_ERROR) {
   ...
}

Reference

SFBFileMgr::Remove | SFXFile::Create | SFXDirectory::Remove | BREW API IFILE_Remove | BREW API IFILEMGR_GetLastError


SFXFile::Rename
Move or rename the specified file.
[ public, static ]
SFCError Rename(
    SFXPathConstRef from   // file path before changing
    SFXPathConstRef to     // file path after changing
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If the "to" or "from" argument is a directory: SFERR_INVALID_PARAM
  • Otherwise: error value that the BREW API IFILEMGR_GetLastError function returns, or SFERR_FAILED

Description

This function moves or renames the specified file.

[Note] Note

This function calls the SFBFileMgr::Rename function internally.

Example

The below is the code to move "data.txt" from the "dir1" directory to the "dir2" directory.

if (SFXFile::Rename(SFXPath("/dir1/data.txt"), SFXPath("/dir2/data.txt"))
    == SFERR_NO_ERROR) {
    ...
}

The below is the code to rename "data.txt" in the "dir1" directory to "file.txt".

if (SFXFile::Rename(SFXPath("/dir1/data.txt"), SFXPath("/dir1/file.txt")) == SFERR_NO_ERROR) {
    ...
}

Reference

SFBFileMgr::Rename | BREW API IFILE_IFILEMGR_Rename | BREW API IFILEMGR_GetLastError


SFXFile::ScheduleRead
Schedule to read data from this file storage without input stream.
[ public, virtual ]
SFCError ScheduleRead(
    CallbackSPP spp     // callback function pointer
    VoidPtr reference   // data passed to callback function
);

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this file has not been opened, or reading data has already been scheduled: SFERR_INVALID_STATE

Description

This function schedules to read data from this file storage without input stream.

Concretely, this function registers the callback function that will read data with the SFXFile::Read function. The registered callback function will be called by BREW AEE when it is possible to read data.

The status of this file is "under scheduling to read data" until the callback function is called. And after the callback function is called, it will become the status before calling this function, i.e., "this file is opened".

If you call the SFXFile::Cancel function before the callback function is called, reading data will be canceled and the status will be returned to the status before calling this function, i.e., "this file is opened".

* If the SFXFile::Read function returns AEE_STREAM_WOULDBLOCK, it will be necessary to register a callback function with this function where data will be read with the SFXFile::Read function.

[Note] Note

This function internally calls the SFBAStream::Readable function.

[Note] Prerequisite

This file needs to be opened with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function before this function is called.

And if this file has been already scheduled to read data, the SFERR_INVALID_STATE error will be returned.

Example

class MyClass {
    SFXFile _file;
    Void Function(Void);
    XALLBACK_DECLARE_SFXFILE(_SFXFile_OnRead)
};

Void MyClass::Function(Void)
{
    SFXPath  path("/dir1/data.txt");
    
    // read file in read only mode
    if (_file.OpenReadOnly(path) == SFERR_NO_ERROR) {
        // schedule to read data from file
        _file.ScheduleRead(XALLBACK_INTERNAL(_SFXFile_OnRead));// register the callback function  
    }
}

// callback function notified that data can be read
XALLBACK_IMPLEMENT_SFXFILE(MyClass, _SFXFile_OnRead, error)
{
    SFXBuffer buffer;
    UInt32 size;

    buffer.SetSize(32);  // allocate buffer for reading data from file

     size = buffer.GetSize();  
     // read data from file to buffer
     switch (_file.Read(buffer.GetBuffer(), &size)) {
         case SFERR_NO_ERROR:
             // display the read data
             buffer.SetSize(size + 1);
             buffer[buffer.GetSize() + 1] = '\0';
             TRACE("%s", SFXAnsiString(buffer).GetCString());
             // schedule to read data from file
             _file.ScheduleRead(XALLBACK_INTERNAL(_SFXFile_OnRead));  // register the callback function
             break;
         case EFILEEOF:
             _file.Close();  // close file
             break;
         case AEE_STREAM_WOULDBLOCK:
             // schedule to read data from file
             _file.ScheduleRead(XALLBACK_INTERNAL(_SFXFile_OnRead));  // register the callback function
             break;
        }
    return;
}

Reference

SFBAStream::Readable | SFXFile::Read | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::Cancel | BREW API IFILE_Readable


SFXFile::ScheduleWrite
[This function cannot be used now]
[ public, virtual ]
SFCError ScheduleWrite(
    CallbackSPP spp     // callback function pointer
    VoidPtr reference   // data passed to callback function
);

Description

Return SFERR_UNSUPPORTED.


SFXFile::Seek
Move the file pointer by the specified amount from the current position of this file storage. [in bytes]
[ public ]
SFCError Seek(
    SInt32 distance   // moving amount
);

Return value

Description

This function moves the file pointer by the specified amount from the current position of this file storage. [in bytes]

If the file pointer is moved by the specified amount to point to the position before the start or after the end of this file storage, it will be set to the start or the end respectively.

To move the file pointer backward, set the distance argument to a negative value as the moving amount.

When this file storage is opened in the read only mode, if the file pointer is moved to point to the position before the start or after the end of this file storage, this operation will fail.

When this file storage is opened in the read/write mode, if the file pointer is moved to point to the position after the end of this file storage, this file storage will be expanded to that position. And if the position of file pointer is moved to point to the position before the start of this file storage, this operation will fail.

[Note] Note

This function calls the SFBFile::Seek function internally.

[Note] Prerequisite

This file storage needs to be opened with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function before this function is called.

Reference

SFBFile::Seek | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::SeekStart | SFXFile::SeekEnd | SFXFile::Tell | BREW API IFILE_Seek | BREW API IFILEMGR_GetLastError


SFXFile::SeekEnd
Move the file pointer by the specified amount from the end position of this file storage. [in bytes]
[ public ]
SFCError SeekEnd(
    SInt32 distance   // moving amount
);

Return value

Description

This function moves the file pointer by the specified amount from the end position of this file storage. [in bytes]

If the file pointer is moved by the specified amount to point to the position before the start or after the end of this file storage, it will be set to the start or the end respectively.

To move the file pointer backward, set the distance argument to a negative value as the moving amount.

When this file storage is opened in the read only mode, if the file pointer is moved to point to the position before the start or after the end of this file storage, this operation will fail.

When this file storage is opened in the read/write mode, if the file pointer is moved to point to the position after the end of this file storage, this file storage will be expanded to that position. And if the position of file pointer is moved to point to the position before the start of this file storage, this operation will fail.

[Note] Note

This function calls the SFBFile::Seek function internally.

[Note] Prerequisite

This file storage needs to be opened with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function before this function is called.

Reference

SFBFile::Seek | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::Seek | SFXFile::SeekStart | SFXFile::Tell | BREW API IFILE_Seek | BREW API IFILEMGR_GetLastError


SFXFile::SeekStart
Move the file pointer by the specified amount from the start position of this file storage. [in bytes]
[ public ]
SFCError SeekStart(
    SInt32 distance   // moving amount
);

Return value

Description

This function moves the file pointer by the specified amount from the start position of this file storage. [in bytes]

If the file pointer is moved by the specified amount to point to the position before the start or after the end of this file storage, it will be set to the start or the end respectively.

To move the file pointer backward, set the distance argument to a negative value as the moving amount.

When this file storage is opened in the read only mode, if the file pointer is moved to point to the position before the start or after the end of this file storage, this operation will fail.

When this file storage is opened in the read/write mode, if the file pointer is moved to point to the position after the end of this file storage, this file storage will be expanded to that position. And if the position of file pointer is moved to point to the position before the start of this file storage, this operation will fail.

[Note] Note

This function calls the SFBFile::Seek function internally.

[Note] Prerequisite

This file storage needs to be opened with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function before this function is called.

Reference

SFBFile::Seek | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::Seek | SFXFile::SeekEnd | SFXFile::Tell | BREW API IFILE_Seek | BREW API IFILEMGR_GetLastError


SFXFile::SetCacheSize
Set the cache size for operating this file storage.
[ public ]
Void SetCacheSize(
    SInt32 size   // cache size
);

Description

This function sets the cache size for operating this file storage.

With this function, performance tuning for the file operation can be performed.

[Note] Note

This cashe size for operating this file storage can be also set with the SFXFile::SFXFile constructor.

[Note] Note

This function calls the SFBFile::SetCacheSize function internally.

Reference

SFBFile::SetCacheSize | SFXFile::SFXFile | SFXFile::GetCacheSize | BREW API IFILE_SetCacheSize


SFXFile::Tell
Get the current position of the file pointer of this file storage.
[ public, const ]
UInt32 Tell(Void);

Return value

  • If this file storage is opened: current position of the file pointer of this file storage
  • If this file storage is closed: 0

Description

This function gets the current position of the file pointer of this file storage.

[Note] Note

This function calls the SFBFile::Seek function internally.

Example

// truncate file at the current position of the file pointer
file.Truncate(file.Tell());

Reference

SFBFile::Seek | SFXFile::Seek | SFXFile::SeekStart | SFXFile::SeekEnd | BREW API IFILE_Seek


SFXFile::Truncate
Truncate this file storage at the specified position.
[ public ]
SFCError Truncate(
    UInt32 position   // position to truncate
);

Return value

Description

This function truncates this file storage at the specified position.

[Note] Note

This function calls the SFBFile::Truncate function internally.

[Note] Prerequisite

This file storage needs to be opened with the SFXFile::OpenReadOnly / SFXFile::OpenReadWrite function before this function is called.

Example

// truncate file at present position of file pointer
file.Truncate(file.Tell());

Reference

SFBFile::Truncate | SFXFile::Seek | SFXFile::SeekStart | SFXFile::SeekEnd | SFXFile::Tell | BREW API IFILE_Truncate | BREW API IFILEMGR_GetLastError


SFXFile::Write
Write data into this file storage without output stream.
[ public, virtual ]
SFCError Write(
    VoidConstPtr buffer   // buffer to write data into
    UInt32Ptr size        
// before this function is called: size of the buffer to write data into the file storage; 
just after this function is returned: size of the data to be written into the file storage actually

);

Argument

buffer

Specify the buffer to write data into.

size

Before calling this function, specify the size of the buffer to write data into the file storage. Just after this function is returned, the size of data to be written into the file storage actually will be stored into this argument.

Return value

  • If succeeds: SFERR_NO_ERROR
  • If this file storage is not opened with the SFXFile::OpenReadWrite function: SFERR_INVALID_STATE
  • If the size argument is null: SFERR_INVALID_PARAM
  • If end of file : EFILEEOF

Description

This function writes data into this file storage without output stream.

[Note] Note

This function calls the SFBFile::Write function internally.

[Note] Prerequisite

This file storage needs to be opened with the SFXFile::OpenReadWrite function before this function is called.

Reference

SFBFile::Write | SFXFile::OpenReadWrite | SFXFile::Read | SFXFile::Seek | BREW API IFILE_Write