![]() ![]() ![]()
|
BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1 |


The SFXFile class is for creating a file, reading from file or writing to file, obtaining file information, and creating a temporary file.
Sample code for reading data from a file.
SFCError error; // error value SFXFile file; // instance of SFXFile class 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 (!reader.Ends()) { // repeat until end of stream is reached // read data from file to input stream actually // up to 1024 bytes of data can be read since buffer size is specified if ((error = reader.Fetch()) != SFERR_NO_ERROR) { // if error occurs break; } // read data from input stream to tempString if ((error = reader.ReadSFXAnsiString(&tempString)) != SFERR_NO_ERROR) { // if error occurs break; } // append tempString onto stringFromFile stringFromFile += tempString; } reader.Release(); } file.Close(); }
Sample code for writing data to a file.
SFCError error; // error value SFXFile file; // instance of SFXFile class 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 to file if ((error = file.GetStreamWriter(bufferSize, &writer)) == SFERR_NO_ERROR) { for (; 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) { // if error occurs break; } // write data from output stream to file actually if ((error = writer.Flush()) != SFERR_NO_ERROR) { // if error occurs break; } } writer.Release(); } file.Close(); }
| Constructor/Destructor |
|---|
|
SFXFile( Void ) Constructor of SFXFile class.
|
|
SFXFile(
SInt32 cache
) Constructor of SFXFile class.
|
|
~SFXFile( Void ) Destructor of SFXFile class.
|
| Public Functions | |
|---|---|
| Void |
Cancel( Void ) Cancel the registered callback function.
|
| Void |
Close( Void ) Close a file.
|
| static SFCError |
Create(
SFXPathConstRef path
, Bool force = false
) Create a file.
|
| static SFCError |
DeviceFreeSpace(
UInt32Ptr result
) Get the free space of device.
|
| static SFCError |
DeviceTotalSpace(
UInt32Ptr result
) Get the device capacity.
|
| static SFCError |
Exists(
SFXPathConstRef path
, BoolPtr result
) Check whether the file exists or not.
|
| SInt32 |
GetCacheSize( Void ) Get the cache size when manipulating a file.
|
| static SFCError |
GetCreateDate(
SFXPathConstRef path
, SFXDatePtr result
) Get the creation date of file.
|
| SFBFileSmpConstRef |
GetSFBFile( Void ) Get the file interface used internally.
|
| static SFCError |
GetSize(
SFXPathConstRef path
, UInt32Ptr result
) Get the file size.
|
| SFCError |
GetStreamReader(
UInt32 size
, SFXStreamReaderPtr result
) GetStreamReader( SFXStreamReaderPtr result ) Get the input stream for reading data from file.
|
| SFCError |
GetStreamWriter(
UInt32 size
, SFXStreamWriterPtr result
) GetStreamWriter( SFXStreamWriterPtr result ) Get the output stream for writing data to 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 file path that does not duplicate existing ones.
|
| static SFCError |
IsReadOnly(
SFXPathConstRef path
, BoolPtr result
) Check whether the file is read only or not.
|
| static SFCError |
IsSystem(
SFXPathConstRef path
, BoolPtr result
) Check whether the file is system file or not.
|
| SFCError |
OpenReadOnly(
SFXPathConstRef path
) Open the file in read only mode.
|
| SFCError |
OpenReadWrite(
SFXPathConstRef path
, Bool force = false
) Open the file in read/write mode.
|
| SFCError |
Read(
VoidPtr buffer
, UInt32Ptr size
) Read data from file.
|
| static SFCError |
Remove(
SFXPathConstRef path
) Delete a file.
|
| static SFCError |
Rename(
SFXPathConstRef from
, SFXPathConstRef to
) Move or rename a file.
|
| SFCError |
ScheduleRead(
CallbackSPP spp
, VoidPtr reference
) Register the callback function and schedule to read data from file.
|
| 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.
|
| SFCError |
SeekEnd(
SInt32 distance
) Move the file pointer by the specified amount from the end of file.
|
| SFCError |
SeekStart(
SInt32 distance
) Move the file pointer by the specified amount from the head of file.
|
| Void |
SetCacheSize(
SInt32 size
) Set the cache size when manipulating a file.
|
| UInt32 |
Tell( Void ) Get the present position of file pointer.
|
| SFCError |
Truncate(
UInt32 position
) Truncate the file at the specified position.
|
| SFCError |
Write(
VoidConstPtr buffer
, UInt32Ptr size
) Write data to file.
|
| Types |
|---|
|
CallbackSPP
(inherits from SFXStorage)
The prototype of the callback function for the SFXStorage class.
|
[ public, explicit ] SFXFile(Void);
[ public, explicit ]
SFXFile(
SInt32 cache // cache size
);
[ public, virtual ] ~SFXFile(Void);
Close an opened file.
[ public, virtual ] Void Cancel(Void);
[ public ] Void Close(Void);
Cancel the callback function and close the open file.
SFXFile file;
...
file.Close(); // close file
SFXFile::Create | SFXFile::OpenReadOnly | SFXFile::OpenReadWrite
[ public, static ] SFCError Create( SFXPathConstRef path // file path Bool force = false // recursively create directory or not? );
If the force argument is set to true, create the directories which do not exist in the specified path.
// create file forcedly // create parent directory if not exist SFXFile::Create(SFXPath("/dir1/data.txt"), true);
UInt32 space;
SFXFile::DeviceFreeSpace(&space); // get free space of device
UInt32 space;
SFXFile::DeviceTotalSpace(&space); // get device capacity
[ public, static ] SFCError Exists( SFXPathConstRef path // file path to check BoolPtr result // pointer to the result );
Bool b; // return value of Exists function is of SFCError type // b: set by Exists function whether specifiled file exists or not SFXFile::Exists(SFXPath("/dir1/data.txt"), &b); if (b) { // when file exist ... }
[ public, const ] SInt32 GetCacheSize(Void);
[ public, static ] SFCError GetCreateDate( SFXPathConstRef path // file path SFXDatePtr result // pointer to the result );
SFXDate date;
// get creation date of file
SFXFile::GetCreateDate(SFXPath("/dir1/data.txt"), &date);
[ public, const ] SFBFileSmpConstRef GetSFBFile(Void);
Get the file interface (instance of SFBFile class) that is used internally.
[ public, static ] SFCError GetSize( SFXPathConstRef path // file path UInt32Ptr result // pointer to the result );
UInt32 size;
SFXFile::GetSize(SFXPath("/dir1/data.txt"), &size); // file size is stored in the size variable
[ 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 );
The buffer size of the input stream can be specified. Use the SFXBinaryStreamReader, SFXAnsiStringStreamReader, or SFXWideStringStreamReader class for the input stream properly according to the type of data to read.
SFXFile file; SFXAnsiStringStreamReader reader; SFXAnsiString temp; SFXAnsiString string; // variable in which data read from file is stored SFXPath path("/data.txt"); // file name // open file in ReadOnly mode if (file.OpenReadOnly(path) == SFERR_NO_ERROR) { // when success // get input stream for reading data from file file.GetStreamReader(1024, &reader); // repeat until end of file is reached while (!reader.Ends()) { if (reader.GetReadableSize() == 0) { // read data from file to input stream actually reader.Fetch(); } // read data from input stream to temp reader >> temp; string += temp; } file.Close(); // close file }
SFXFile::GetStreamWriter | SFXBinaryStreamReader | SFXAnsiStringStreamReader | SFXWideStringStreamReader
[ public, virtual ] SFCError GetStreamWriter( UInt32 size // size SFXStreamWriterPtr result // pointer to the output stream );
[ public, virtual ] SFCError GetStreamWriter( SFXStreamWriterPtr result // pointer to the output stream );
The buffer size of the output stream can be specified. Use the SFXBinaryStreamWriter, SFXAnsiStringStreamWriter, or SFXWideStringStreamWriter class for the output stream properly according to the written data type
SFXFile file;
SFXAnsiStringStreamWriter writer;
SFXAnsiString string("abcdefg"); // string to write to file
SFXPath path("/data.txt"); // file name
// open file
if (file.OpenReadWrite(path) == SFERR_NO_ERROR) {
// when success
// get output stream for writing data to file
file.GetStreamWriter(string.GetLength(), &writer);
// write data from string to output stream
writer << string;
// write data from output stream to file actually
writer.Flush();
file.Close(); // close file
}
SFXFile::GetStreamReader | SFXBinaryStreamWriter | SFXAnsiStringStreamWriter | SFXWideStringStreamWriter
[ public, static ] SFCError GetTemporaryPath( SFXPathConstRef path // directory path in which the temporary file will be created SFXPathPtr result // pointer to the temporary file path );
The SFXFile::GetTemporaryPath function returns the temporary file path that does not duplicate existing ones.
![]() |
Note |
|---|---|
| To get the permanent file which is not temporary one, use the SFXFile::GetUniquePath function. | |
![]() |
Note |
|---|---|
| The SFXFile::GetTemporaryPath function internally calls the SFXFile::GetUniquePath function by specifying "sfx" as the prefix argument and "fle.tmp" as the suffix argument respectively. | |
SFXPath dir("/");
SFXPath path;
if (SFXFile::GetTemporaryPath(path, &path) == SFERR_NO_ERROR) {
// file path like "/sfx7182CBD4fle.tmp" will be created
TRACE("%s", path.Get().GetCString());
}
[ 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 );
Since the file name is generated using a random number, this function might fail in case many files have already existed.
![]() |
Note |
|---|---|
| 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. | |
![]() |
Note |
|---|---|
| To get the temporary file path which will be deleted soon, use the SFXFile::GetTemporaryPath function. | |
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());
}
[ public, static ] SFCError IsReadOnly( SFXPathConstRef path // file path to check BoolPtr result // pointer to the result );
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 ... }
[ public, static ] SFCError IsSystem( SFXPathConstRef path // file path to check BoolPtr result // pointer to the result );
[ public ] SFCError OpenReadOnly( SFXPathConstRef path // file path to open );
SFXFile file; // open file in read only mode if (file.OpenReadOnly(SFXPath("/dir1/data.txt")) == SFERR_NO_ERROR) { ... // close file file.Close(); }
[ public ] SFCError OpenReadWrite( SFXPathConstRef path // file path Bool force = false // whether or not to create the file when it does not exist );
SFXFile file; // open file in read/write mode if (file.OpenReadWrite(SFXPath("/dir1/data.txt")) == SFERR_NO_ERROR) { ... // close file file.Close(); }
[ public, virtual ] SFCError Read( VoidPtr buffer // buffer to store data read from file UInt32Ptr size // size of data read from file(in bytes) );
Copy a file (src.txt) to another file (dst.txt).
SFXFile fin; SFXFile fout; SFXBinaryStreamReader reader; SFXBinaryStreamWriter writer; SFXBuffer buffer; // open input file in read only mode if (fin.OpenReadOnly(SFXPath("/src.txt")) == SFERR_NO_ERROR) { // open output file in read/ write mode if (fout.OpenReadWrite(SFXPath("/dst.txt")) == SFERR_NO_ERROR) { fin.GetStreamReader(1024, &reader); // get input stream for reading fout.GetStreamWriter(1024, &writer); // get output stream for writing UInt08 c; // repeat until end of file is reached while (!reader.Ends()) { buffer.SetSize(reader.GetReadableSize()); reader.Fetch(); // read data from file to input stream reader.Read(&buffer); // read data from input stream to buffer writer.Write(buffer); // write data from buffer to output stream writer.Flush(); // write data from output stream to file } fout.Close(); // close output file } fin.Close(); // close input file }
SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::Seek | SFXFile::Write
[ public, static ] SFCError Remove( SFXPathConstRef path // file path );
if (SFXFile::Remove(SFXPath("/dir/data.txt")) == SFERR_NO_ERROR) {
...
}
[ public, static ] SFCError Rename( SFXPathConstRef from // file path before changing SFXPathConstRef to // file path after changing );
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) {
...
}
Rename data.txt in dir1 directory to file.txt
if (SFXFile::Rename(SFXPath("/dir1/data.txt"), SFXPath("/dir1/file.txt"))
== SFERR_NO_ERROR) {
...
}
[ public, virtual ] SFCError ScheduleRead( CallbackSPP spp // callback function pointer VoidPtr reference // user data passed to callback function );
The SFXFile::ScheduleRead function is used to register the callback function in which the SFXFile::Read function is used to read data from file.
![]() |
Note |
|---|---|
| When the return value of SFXFile::Read function is AEE_STREAM_WOULDBLOCK or there is still remaining data to read, register the callback function using the SFXFile::ScheduleRead function, and then schedule the SFXFile::Read function in the callback function again. | |
class MyClass {
SFXFile _file;
Void Function(Void);
CALLBACK_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(CALLBACK_FUNCTION(_SFXFile_OnRead));// register the callback function
}
}
// callback function
CALLBACK_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(CALLBACK_FUNCTION(_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(CALLBACK_FUNCTION(_SFXFile_OnRead)); // register the callback function
break;
}
return;
}
[ public, virtual ] SFCError ScheduleWrite( CallbackSPP spp // callback function pointer VoidPtr reference // user data passed to callback function );
Return SFERR_UNSUPPORTED.
To move the file pointer backward, set a negative value as moving amount to the distance argument.
When the file is opened in read only mode, if the position of file pointer after moved by this function is under the head of file or over the end of file, this operation will fail.
When the file is opened in read/write mode, if the position of file pointer after moved is over the end of file, the file will be expanded to that position. And if the position of file pointer after moved is under the head of file, this operation will fail.
To move the file pointer backward, set a negative value as moving amount to the distance argument.
When the file is opened in read only mode, if the position of file pointer after moved by this function is under the head of file or over the end of file, this operation will fail.
When the file is opened in read/write mode, if the position of file pointer after moved is over the end of file, the file will be expanded to that position. And if the position of file pointer after moved is under the head of file, this operation will fail.
To move the file pointer backward, set a negative value as moving amount to the distance argument.
When the file is opened in read only mode, if the position of file pointer after moved by this function is under the head of file or over the end of file, this operation will fail.
When the file is opened in read/write mode, if the position of file pointer after moved is over the end of file, the file will be expanded to that position. And if the position of file pointer after moved is under the head of file, this operation will fail.
[ public, const ] UInt32 Tell(Void);
// truncate file at present position of file pointer
file.Truncate(file.Tell());
// truncate file at present position of file pointer
file.Truncate(file.Tell());
SFXFile::Seek | SFXFile::SeekStart | SFXFile::SeekEnd | SFXFile::Tell
[ public, virtual ] SFCError Write( VoidConstPtr buffer // buffer to write UInt32Ptr size // writen data size in bytes );
Write data to file without using the output stream.
SFXFile::OpenReadOnly | SFXFile::OpenReadWrite | SFXFile::Read SFXFile::Seek |
|
Copyright (C) 2002 - 2009 Sophia Cradle, Inc. All Rights Reserved. |
![]() ![]() ![]()
|