PrevNextUpHome BREW C++ Class Library & GUI Framework & XML Middleware : SophiaFramework 4.1

17.3. Sample Code

17.3.1. Read from the file into the memory

To manipulate memory through the stream, use the SFXMemory class.

Example 17.35. Read from the file into the memory

SFCError error;               // error value
SFXFile file;                 // input file
SFXBinaryStreamReader reader; // input stream for reading from file

SFXMemory memory;             // memory storage
SFXBinaryStreamWriter writer; // output stream for writing to memory

UInt08 c;

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

    // get input stream
    if ((error = file.GetStreamReader(1024, &reader)) == SFERR_NO_ERROR) {

        // open memory storage
        if ((error = memory.Open()) == SFERR_NO_ERROR) {

            // get output stream
            if ((error = memory.GetStreamWriter(1024, &writer))
                == SFERR_NO_ERROR) {

                // read from file to stream buffer
                if ((error = reader.Fetch()) == SFERR_NO_ERROR) {

                    // repeat until end of input stream is reached
                    while (error == SFERR_NO_ERROR &
                        !reader.Ends()) {

                        // if there is data in input stream buffer
                        if (reader.GetReadableSize() > 0) {
                            reader >> c;                  // read one byte data from input stream
                            if (c != '\r' & c != '\n') {    // if it is not line-feed character
                                writer << c;              // write to output stream
                            }
                        }
                        else { // when there is no data in input stream buffer
                            error = reader.Fetch();
                        }
                        if (error == SFERR_NO_ERROR) {
                            // when there is no space in output stream buffer
                            if (writer.GetWritableSize() == 0) {
                                error = writer.Flush();
                            }
                        }
                    }
                    if (error == SFERR_NO_ERROR) {

                        // write termination character ('\0')
                        writer << static_cast<UInt08>('\0');

                        if ((error = writer.Flush()) == SFERR_NO_ERROR) {
                            // get pointer to written data and display them
                            TRACE("%s", memory.GetBuffer());

                            // store result
                            SFXBuffer buffer;
                            error = memory.Close(&buffer);
                        }
                    }
                }
            }
            writer.Release();
        }
        reader.Release();
    }
    file.Close();
}

17.3.2. Download Images

To manipulate the data of BREW ISource through the stream, use the SFXSource class.

Example 17.36. Download the image as SFBImage using the HTTP communication

// connection processing is omitted

// callback function called after HTTP connection is established
CALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    SFBSourceSmp source;
    SFXSource temp;

    if (error == SFERR_NO_ERROR) {
        if (_http.GetResultCode() == 200) {

            // get SFBSourceSmp instance that manages content part
            if ((source = _http.GetResponseContent()) != null) {

                // convert SFBSourceSmp into SFXStorage
                // since SFXHTTPConnection instance has right to manage data in content part
                // SFXSource instance may be destroyed immediately
                if (temp.Open(source) == SFERR_NO_ERROR) {
                    if (_image.SetStorage(temp) == SFERR_NO_ERROR)    {
                        // set SFXSource instance to SFBImage instance
                    }
                }
            }
        }
    }
    return;
}

17.3.3. Decompress the gzipped file

To decompress the gzipped file through the stream, use the SFXZIPDecoder class.

Example 17.37. Decompress the gzipped file

// SFXZIPDecoder instance is declared as class member variable for the callback function
class MyClass {
private:
    SFXFile _file;                      // input file
    SFXZIPDecoder _decoder;             // gzip decoder
    SFXAnsiStringStreamReader  _reader; // input stream
    SFXAnsiString _unzipString;         // decompressed string
public:
    Void Start(Void);

    // callback function
    CALLBACK_DECLARE_SFXANSISTRINGSTREAMREADER(OnFetch)
};

Void MyClass::Start(Void)
{
    SFCError error; // error value

    // open file in read only mode
    if ((error = _file.OpenReadOnly(SFXPath("/testdata.tar.gz")))
        == SFERR_NO_ERROR) {

        // register file storage to gzip decoder
        if ((error = _decoder.Open(_file)) == SFERR_NO_ERROR) {

            // get input stream from gzip decoder
            if ((error = _decoder.GetStreamReader(&_reader))
                == SFERR_NO_ERROR) {

                // read gzipped data (completion of reading gzipped data will be notified to OnFetch function )
                if ((error = _reader.Fetch(CALLBACK_FUNCTION(OnFetch)))
                    != SFERR_NO_ERROR) {
                    // when error occurs
                    _reader.Release();
                }
            }
            if (error != SFERR_NO_ERROR) { 
                // when error occurs
                _decoder.Close();
            }
        }
        if (error != SFERR_NO_ERROR) { 
            // when error occurs
            _file.Close();
        }
    }
}

// callback function notified of completion of reading data
CALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMREADER(MyClass, OnFetch, error)
{
    if (error == SFERR_NO_ERROR) {  
        // if no error

        // read decompressed data
        if ((error = _reader.ReadSFXAnsiString(&_unzipString)) == SFERR_NO_ERROR) {

            // display string
            TRACE("%s", _unzipString.GetCString());
        }
    }
    // termination
    _decoder.Close();
    _file.Close();
}