PrevNextUpHome SophiaFramework UNIVERSE 5.3

18.9. Sample Code

18.9.1. Reading Data from File into Memory

The below is the code to read data from the file storage into the memory storage by using classes such as SFXFile or SFXMemory.

Example 18.13. Reading data from file storage into memory storage

class MyClass {
private:
    SFXBuffer _buffer;  // buffer to store data read from file storage into memory storage
public:
    SFCError MyClass::ReadDataFromFileToMemory(Void);
};

SFCError MyClass::ReadDataFromFileToMemory(Void)
{ 
    SFCError error;               // error value
    SFXFile file;                 // file storage
    SFXBinaryStreamReader reader; // input stream for reading from file
    SFXMemory memory;             // memory storage
    SFXBinaryStreamWriter writer; // output stream for writing to memory
    UInt08 c;                     // temporary variable

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

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

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

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

                    // read data from file storage to input 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 (reader.GetReadableSize() > 0) {

                                // when data exists in input stream buffer:

                                // read one byte data from input stream buffer into the c variable
                                reader > c;
                                if (c != '\r' & c != '\n') {    

                                    // when it is not line-feed character

                                    // write data from the c variable into output stream buffer
                                    writer < c;
                                }

                                if (writer.GetWritableSize() == 0) {

                                    // when there is no space in output stream buffer:

                                    // write data from output stream buffer into memory storage actually
                                    error = writer.Flush();
                                }
                            }
                            else { 

                                // when no data exists in input stream buffer:

                                // read data from file storage to input stream buffer
                                error = reader.Fetch();
                            }
                        }
                        if (error == SFERR_NO_ERROR) {

                            // write termination character ('\0') into output stream buffer
                            writer < static_cast<UInt08>('\0');

                            // write termination character ('\0') from output stream buffer into memory storage actually
                            if ((error = writer.Flush()) == SFERR_NO_ERROR) {

                                // get pointer to memory storage and display its content
                                TRACE("%s", memory.GetBuffer());
                            }
                        }
                    }
                    // release output stream for writing data into memory storage
                    writer.Release();
                }
                // store content of memory storage into buffer and close memory storage
                // * note that SFXMemory::Close() returns error value in this case
                error = memory.Close(&_buffer);
            }
            // release input stream for reading data from file storage
            reader.Release();
        }
        // close file storage
        file.Close();
    }
    if (error != SFERR_NO_ERROR) {

        TRACE("An error has occurred during processing! Error code: %d", error);
    }
    return error; 
}

18.9.2. Downloading an Image

The below is the code to draw the SFBImage instance into which image data is dowmloaded from the Web server using the SFXSource class.

Example 18.14. Downloading an image into the SFBImage instance using the HTTP communication

// The _http or _image variable is defined as class member variable since used in the callback function
class MyClass {
private:
    SFXHTTPConnection _http;
    SFBImageSmp _image;
public:
    Void Start(Void);
    XALLBACK_DECLARE_SFXHTTPCONNECTION(OnConnect)

    // actual callback function
    Void OnReadImage(SFCError error);  
    // dummy callback function: defined as the static member function
    static Void OnReadImageSHP(VoidPtr reference, IImage *  pIImage, AEEImageInfo * pi, int nErr);  
};

Void MyClass::Start(Void)
{
    SFCError error(SFERR_NO_ERROR);

    // HTTP connection using GET method
    // * there is no need to specify "GET" in calling the SFXHTTPConnection::SetMethod function

    // open the HTTP connection
    if ((error = _http.Open()) == SFERR_NO_ERROR) {

        // connect to the Web server
        // * the connection result will be notified to the OnConnect function
        if ((error = _http.Connect("http://www.example.com/example.bmp", XALLBACK_INTERNAL(OnConnect))) == SFERR_NO_ERROR) {

            TRACE("> connecting...");
        }
    }

    if (error != SFERR_NO_ERROR) {

        // if an error occurs
        // close the HTTP connection
        _http.Close();
    }
    return;
}

// callback function notified of the connection result
XALLBACK_IMPLEMENT_SFXHTTPCONNECTION(MyClass, OnConnect, error)
{
    SFBSourceSmp source;
    SFXSource    temp;

    if (error == SFERR_NO_ERROR) {

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

            // get the source storage that manages the HTTP response body
            if ((source = _http.GetResponseContent()) != null) {

                // create the image object in the BMP format
                if ((_image = SFBImage::NewInstance(AEECLSID_WINBMP)) == SFERR_NO_ERROR) {

                    // set the callback function notified of the result of reading image data
                    // * the result of reading image data will be notified to the OnReadImageSHP function
                    _image->Notify(OnReadImageSHP, this);

                    // set the source storage to SFXSource
                    if (temp.Open(source) == SFERR_NO_ERROR) {

                        // read image data from the source storage
                       if (_image->SetStream(temp) != SFERR_NO_ERROR) {

                            // if an error occurs
                            _http.Close();
                            TRACE("An error occurred during setting the stream!");
                        }
                        // close the source storage
                        temp.Close();
                    } else {
 
                        // if an error occurs
		        _http.Close();
                        TRACE("An error occurred during setting the source storage!");
                    }
                } else { 

                    // if an error occurs
                    _http.Close();
                    TRACE("An error occurred during creating the image object!");
                }
            } else {

               // if an error occurs
               _http.Close();
               TRACE("An error occurred during getting the source storage!");
            }
	} else {

            // if an error occurs
            _http.Close();
            TRACE("The result code is not 200!");
	}
    } else {

        // if an error occurs
        _http.Close();
        TRACE("An error occurred during connecting to the web server!");
    }
    return;
}

// dummy callback function notified of the result of reading image
Void MyClass::OnReadImageSHP(VoidPtr reference, IImage *  pIImage, AEEImageInfo * pi, int nErr)
{
    // call the actual callback function
    static_cast<MyClassPtr>(reference)->OnReadImage(nErr); 
    return;
}

// actual callback function
Void MyClass::OnReadImage(SFCError error)
{
    SFXGraphicsPtr graphics(SFXGraphics::GetInstance());
 
    if (error == SFERR_NO_ERROR) {
 
        // draw the image object at the (0, 0) coordinate
        _image->Draw(SFXGrid(0,0));

        // update the screen
        graphics->Update();
    } else {

        // if an error occurs
        TRACE("An error occurred during downloading image data!");
    }
    // close the HTTP connection
    _http.Close();
    return;
}

18.9.3. Decompressing a gzipped file

The below is the code to read and decompress the gzipped data from file into memory(the _unzipString variable) through the stream by using the SFXZIPDecoder class.

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

Example 18.15. Decompressing a gzipped file

// _decoder is declared as class member variable since used 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
    XALLBACK_DECLARE_SFXANSISTRINGSTREAMREADER(OnFetch)
};

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

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

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

            // get the input stream from the gzip decoder
            // * the stream buffer is variable since the size argument is not specified
            if ((error = _decoder.GetStreamReader(&_reader)) == SFERR_NO_ERROR) {

                // perform fetch: read the gzipped data from the gzip decoder into the stream buffer actually
                // *1. the fetch result will be notified to the OnFetch function
                // *2. the stream buffer will be expanded automatically depending on the size of data to be received
                if ((error = _reader.Fetch(XALLBACK_INTERNAL(OnFetch))) != SFERR_NO_ERROR) {

                    // if an error occurs
                    _reader.Release();
                }
            }
            if (error != SFERR_NO_ERROR) { 

                // if an error occurs
                _decoder.Close();
            }
        }
        if (error != SFERR_NO_ERROR) { 

            // if an error occurs
            _file.Close();
        }
    }
}

// callback function notified of the fetch result
XALLBACK_IMPLEMENT_SFXANSISTRINGSTREAMREADER(MyClass, OnFetch, error)
{
    if (error == SFERR_NO_ERROR) {  
        // if no error occurs in fetch

        // read data from the stream buffer into the _unzipString variable(at this time, data has already been decompressed)
        if ((error = _reader.ReadSFXAnsiString(&_unzipString)) == SFERR_NO_ERROR) {

            // display the _unzipString variable on BREW Output Window
            TRACE("%s", _unzipString.GetCString());

            _reader.Release();
        }
    }

    // termination
    _decoder.Close();
    _file.Close();
}