Symbian
Symbian OS Library

SYMBIAN OS V9.3

[Index] [Spacer] [Previous] [Next]



Resource files example code


ReadArray: reading contents of resource file into array


Example Code for ReadArray

The example code is found in: examples\SysLibs\ResourceFiles\ReadArray

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// ReadArray.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.


//  This header file contains the class definitions for:
//  
//  CResData and CResDataArray


#include "CommonFile.hrh"

class CResData : public CBase
    {
public:
    ~CResData();
    static  CResData* NewLC(TResourceReader& aReader);
    void    ShowData(const TInt aStructNum = 0);
private:
    void    ConstructL(TResourceReader& aReader);
private:
    TInt            iWrd;       // STRUCT member type: WORD,
    TInt            iFlags;     // WORD
    TInt            iLng;       // LONG,
    TInt           iByt;     // BYTE,
    TReal           iDbl;        // DOUBLE,
    TBufC<TEXTMAX>    iTxt;       // TEXT (maximum length specified),
    HBufC*          iLtxt;          // LTEXT
    };

                
class CResDataArray : public CBase
    {
public:
    ~CResDataArray();
    static  CResDataArray* NewLC(TResourceReader& aReader);
    void    AddDataL(TResourceReader& aReader);
    void    ShowAllData();
private:
    void    ConstructL(TResourceReader& aReader);
private:
    CArrayPtrFlat<CResData>* iDataArray;
    };

CommonFile.hrh

Found in: examples\SysLibs\ResourceFiles\SigCheck

// ReadDataFromRF.HRH
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//


                // define the maximum length of the txt string
#define TEXTMAX 256

                // define some flags
#define EFlagItem1  0x0001
#define EFlagItem2  0x0002
#define EFlagItem3  0x0004
#define EFlagItem4  0x0008

#define EFlagItem5  0x0010
#define EFlagItem6  0x0020
#define EFlagItem7  0x0040
#define EFlagItem8  0x0080

#define EFlagItem9  0x0100
#define EFlagItem10 0x0200
#define EFlagItem11 0x0400
#define EFlagItem12 0x0800

#define EFlagItem13 0x1000
#define EFlagItem14 0x2000
#define EFlagItem15 0x4000
#define EFlagItem16 0x8000

ReadArray.cpp

Found in: examples\SysLibs\ResourceFiles\ReadArray

// ReadArray.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

/*
    This example shows how to load data from resource files
    into a class.
    
    It loads the resource identified as SECOND from the resource file
    This is an array of DATA structs. 

    (The resource FIRST is not used in this example)
        
    The example constructs a CResDataArray object to contain  
    an array of CResData objects, one for each DATA struct 
    in the array.
    
    The CResDataArray object can also display the individual 
    items of each DATA struct at the test console. All WORD, BYTE
    and LONG are interpreted as signed values.

    The example follows on from the ReadData example
*/
    
#include "CommonToResourceFilesEx.h"
#include "ReadArray.h"
#include <ReadArray.rsg>

                // Construct a new CResDataArray object and
                // place on the cleanup stack
CResDataArray* CResDataArray::NewLC(TResourceReader& aReader)
    {
    CResDataArray* self=new (ELeave) CResDataArray;
    CleanupStack::PushL(self);
    self->ConstructL(aReader);
    return self;
    }


                // Complete the construction of the 
                // CResDataArray object by constructing an array 
                // of (pointers to) CResData objects.
                //
                // Granularity of iDataArray is 3 because we know
                // that only 3 elements are to be added in
                // this example.
void CResDataArray::ConstructL(TResourceReader& aReader)
    {
    iDataArray = new (ELeave) CArrayPtrFlat<CResData> (3); 
    TRAPD(error,AddDataL(aReader));
    if (error)
        {
        iDataArray->ResetAndDestroy();
        delete iDataArray;
        User::Leave(error);
        }
    }


                // Destructor needs to ensure that 
                // the array of (pointers to) CResData
                // objects is destroyed.
CResDataArray::~CResDataArray()
    {
    if (iDataArray)
        {
        iDataArray->ResetAndDestroy();
        delete iDataArray;
        }
    }


                // For each DATA element within the resource,
                // construct a CResData object and add its
                // pointer into the array.
void CResDataArray::AddDataL(TResourceReader& aReader)
    {
    TInt    index;
    TInt    number;
    
                // The first WORD contains the number 
                // of DATA structs within the resource
    number = aReader.ReadInt16();
    
                // Add all newly created CResData objects 
                // to the cleanup stack before adding 
                // to the array
    for (index = 0; index < number ; index++)
        {
        CResData* resData = CResData::NewLC(aReader);
        iDataArray->AppendL(resData);
        CleanupStack::Pop(); // now resData safely in array
        }
    }

                // Show the individual resource items for 
                // each DATA struct in the resource at the 
                // test console.
void CResDataArray::ShowAllData()
    {
    _LIT(KPressAnyKeyToContinue," -->press any key to continue\n\n");
    TInt count;

    count = (iDataArray? iDataArray->Count() : 0);
    
    for (TInt index = 0; index < count; index++)
        {
        (*iDataArray)[index]->ShowData(index+1);
        console->Printf(KPressAnyKeyToContinue);
        console->Getch();
        }
    }


                // Construct a new CResData object and place 
                // on the cleanup stack
CResData* CResData::NewLC(TResourceReader& aReader)
    {
    CResData* self=new (ELeave) CResData;
    CleanupStack::PushL(self);
    self->ConstructL(aReader);
    return self;
    }


                // Complete the construction of the 
                // CResData object using the supplied
                // reource reader representing the resource data.
                // The structure of the data is assumed to be
                // defined by the resource struct DATA in "baarray.rh"
void CResData::ConstructL(TResourceReader& aReader)
    {
                // Interpret next bytes as a TInt16
    iWrd   = aReader.ReadInt16();
            
                // Interpret next bytes as a TInt16
    iFlags = aReader.ReadInt16();
                    
                // Interpret next bytes as a TInt32
    iLng   = aReader.ReadInt32();

                // Interpret the next bytes as a zero
                // terminated string. The string will have 
                // a maximum length defined by the
                // symbol TEXTMAX 
    TPtrC temp = aReader.ReadTPtrC();    
    (iTxt.Des()).Copy(temp);   
                                       
                // Interpret the next bytes as variable length text.
    iLtxt  = aReader.ReadHBufCL(); 
                    
                // Interpret the next byte as a TUInt8 (byte)
    iByt   = aReader.ReadUint8();
    
               // Interpret next bytes as a TReal
    iDbl   = aReader.ReadReal64();          
    }


                // Destructor needs to ensure that 
                // the HBufC descriptor iLtxt is destroyed.
CResData::~CResData()
    {
    delete iLtxt;
    }


                // Show the individual resource itens
                // at the test console.
void CResData::ShowData(const TInt aStructNum)
    {
    _LIT(KResourceItems,"Resource items (struct #%d):\n");
    _LIT(KResourceItems2,"Resource items:\n");
    _LIT(KWrdFormat,"wrd   = %d\n");
    _LIT(KFlags,"flags = ");
    _LIT(KEFlagItem,"EFlagItem");
    _LIT(KNewline,"\n");
    _LIT(KLngFormat,"lng   = %d\n");
    _LIT(KBytFormat,"byt   = %d\n");
    _LIT(KDblFormat,"dbl   = %S\n");
    _LIT(KTxtFormat,"txt   = %S\n");
    _LIT(KLtxtFormat,"ltxt  = %S\n");
    _LIT(KLtxt,"ltxt  = \n");

    TBuf<16>  temp;
    TRealFormat format(16,2);

    if (aStructNum)
        console->Printf(KResourceItems,aStructNum);
    else 
        console->Printf(KResourceItems2);

                //  * * * * * * * * *
    console->Printf(KWrdFormat,iWrd);

                //  * * * * * * * * *
    console->Printf(KFlags);
    TUint       mask = 1;
    TBuf<256> temp2;
    for (TInt ii = 0 ; ii < 16; ii++)
        {
        if (iFlags & mask)
            {
            temp2.Append(KEFlagItem);
            temp2.AppendNum(ii+1);
            temp2.Append('+');    
            }
        mask <<= 1;
        }
    if (temp2.Length())
        temp2.SetLength(temp2.Length()-1);
    console->Printf(temp2);
    console->Printf(KNewline);
        
                //  * * * * * * * * *
    console->Printf(KLngFormat,iLng);
    
                //  * * * * * * * * *
    console->Printf(KBytFormat,iByt);

                //  * * * * * * * * *
    temp.Num(iDbl,format);
    console->Printf(KDblFormat,&temp);

                //  * * * * * * * * *
    console->Printf(KTxtFormat,&iTxt);

                //  * * * * * * * * * 
    if (iLtxt)
        console->Printf(KLtxtFormat,iLtxt);
    else
        console->Printf(KLtxt);
    }
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// Do the example(s)

LOCAL_C void doExampleL()
    {
        // Declare a resource file
    RResourceFile resourceFile;
    
        // Open the resource file
    #if defined(__WINS__)
    _LIT(KZSystemDataArrayRsc,"Z:\\Resource\\Examples\\ReadArray.rsc");
    resourceFile.OpenL(fsSession, KZSystemDataArrayRsc);
    #endif

    #if defined(__ARM__)
    _LIT(KCSystemDataArrayRsc,"C:\\Resource\\Examples\\ReadArray.rsc");
    resourceFile.OpenL(fsSession, KCSystemDataArrayRsc);
    #endif

        // Read the second resource & construct a resource reader
    HBufC8* res = resourceFile.AllocReadLC(SECOND);
    
    TResourceReader theReader;
    theReader.SetBuffer(res);

        // Construct a CResDataArray object to contain
        // the array of CResData objects, and add the elements to it
    CResDataArray* resDataArray = CResDataArray::NewLC(theReader);

        // Can now remove resDataArray from cleanup stack
    CleanupStack::Pop();
    
        // finished with res
    CleanupStack::PopAndDestroy();

        // display all data
    resDataArray->ShowAllData();

        // finished with CResDataArray 
    delete resDataArray;
    
        // finished with resource file, so close it 
    resourceFile.Close();
    } 

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

ReadArray.mmp

// ReadArray.mmp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// using relative paths for source and userinclude directories

TARGET         ReadArray.exe
TARGETTYPE     exe
UID            0
VENDORID 0x70000001

SOURCEPATH     .
SOURCE         ReadArray.cpp

START RESOURCE ReadArray.rss
header
targetpath \Resource\Examples
END

USERINCLUDE    ..
USERINCLUDE    .
USERINCLUDE    ..\..\CommonFramework
SYSTEMINCLUDE  \Epoc32\include

LIBRARY        euser.lib efsrv.lib  bafl.lib

// ReadArray.rh
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#include "CommonFile.hrh"

// for defining a single data resource
STRUCT DATA
    {
    WORD      wrd=16;
    WORD      flags=0;
    LONG      lng;
    LTEXT     txt(TEXTMAX);// variable length but fixed max.
    LTEXT  ltxt;       // variable length
    BYTE      byt; 
    DOUBLE     dbl=0.0;
    }

// for defining an array of data
STRUCT DATAARRAY
    {
    STRUCT dataments[];
    }

// for first resource in file, for ConfirmSignature()
STRUCT SIGNATURE
    {
    LONG version;
    SRLINK self;
    }

/ ReadArray.rss
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#include "ReadArray.rh"
    
RESOURCE DATA first
    {
    wrd=0;
    flags=EFlagItem1+EFlagItem2;
    lng=2; 
    txt="This is TEXT type (NULL terminated)";
    ltxt="This is LTEXT type (leading byte count)";
    byt=255;
    dbl=99.9;
    }

RESOURCE DATAARRAY second
    {
    dataments=
        {
        DATA
            {
            //  wrd and dbl allowed to default
            flags=EFlagItem1;
            lng=654;
            txt="First element";
            ltxt="This text has a leading byte count";
            byt=-1;
            },
        DATA
            {
            wrd=999;
            flags=EFlagItem1+EFlagItem2;
            lng=3;
            txt="Second element";
            ltxt="Extremely large text indeed abcd efghijklm";
            byt=255;
            dbl=1.0;
            },
        DATA
            {
            wrd=0;
            flags=EFlagItem16;
            lng=-1;
            txt="Third element";
            ltxt="";
            byt=127;
            dbl=12.34;
                },
        DATA
            {
            wrd=-1;
            flags=EFlagItem1+EFlagItem8+EFlagItem16;
            lng=2147483647;
            txt="Fourth element";
            ltxt="{[@@@@@@@@@@@@@@]}";
            byt=128;
            dbl=-3400;
                  }
        };
    }


Description

ReadArray is an example of how to load data from resource files into a class.

It loads the resource identified as SECOND from the resource file; this is an array of DATA structures. (The resource FIRST is not used in this example)

The example constructs a CResDataArray object to contain an array of CResData objects, one for each DATA structure in the array. The CResDataArray object can also display the individual items of each DATA structure at the test console. All WORD, BYTE and LONG are interpreted as signed values.

The example follows on from the Reading from a resource file example.


Classes used

[Top]


ReadData: reading from a resource file


Example code for ReadData

ReadData.h

Found in: examples\Syslibs\ResourceFiles\ReadData

// ReadData.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.
//

//  This header file contains the class definitions for:
//  CResData
#include "CommonFile.hrh"

class CResData : public CBase
    {
public:
    ~CResData();
    static  CResData* NewLC(TResourceReader& aReader);
    void    ShowData(const TInt aStructNum = 0);
private:
    void    ConstructL(TResourceReader& aReader);
private:
    TInt            iWrd;       // STRUCT member type: WORD,
    TInt            iFlags;     // WORD
    TInt            iLng;       // LONG,
    TInt           iByt;     // BYTE,
    TReal           iDbl;        // DOUBLE,
    TBufC<TEXTMAX>  iTxt;       // LTEXT (maximum length specified),
    HBufC*          iLtxt;          // LTEXT
    };

ReadData.cpp

Found in: examples\Syslibs\ResourceFiles\ReadData

// ReadData.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

/*
    This example shows how to load data from resource files
    into a class.
    
    It loads the resource identified as resource FIRST from the resource
    file. This is a simple resource of type DATA.
        
    The example constructs a CResData object to hold the 
    individual items corresponding to the items in a 
    DATA struct.

    The CResData object can also display the individual
    items at the test console. All WORD, BYTE and LONG
    are interpreted as signed values
*/
    
#include "CommonToResourceFilesEx.h"

#include "ReadData.h"
#include <ReadData.rsg>

                // Construct a new CResData object and place 
                // on the cleanup stack
CResData* CResData::NewLC(TResourceReader& aReader)
    {
    CResData* self=new (ELeave) CResData;
    CleanupStack::PushL(self);
    self->ConstructL(aReader);
    return self;
    }


                // Complete the construction of the 
                // CResData object using the supplied
                // reource reader representing the resource data.
                // The structure of the data is assumed to be
                // defined by the resource struct DATA in "ReadData.rh"
void CResData::ConstructL(TResourceReader& aReader)
    {
                // Interpret next bytes as a TInt16
    iWrd   = aReader.ReadInt16();
            
                // Interpret next bytes as a TInt16
    iFlags = aReader.ReadInt16();
                    
                // Interpret next bytes as a TInt32
    iLng   = aReader.ReadInt32();

    iTxt    = aReader.ReadTPtrC();
                // Interpret the next byte as the length
                // of text. The text itself follows
                // this byte.
    iLtxt  = aReader.ReadHBufC16L(); 
                // Interpret the next byte as a TInt8
    iByt   = aReader.ReadInt8();
    
               // Interpret next bytes as a TReal
    iDbl   = aReader.ReadReal64();
            
    }


                // Destructor needs to ensure that 
                // the HBufC descriptor iLtxt is destroyed.
CResData::~CResData()
    {
    if (iLtxt)
        delete iLtxt;
    }


                // Show the individual resource itens
                // at the test console.
void CResData::ShowData(const TInt aStructNum)
    {
    _LIT(KResourceItems,"Resource items (struct #%d):\n");
    _LIT(KResourceItems2,"Resource items:\n");
    _LIT(KWrdFormat,"wrd   = %d\n");
    _LIT(KFlags,"flags = ");
    _LIT(KEFlagItem,"EFlagItem");
    _LIT(KNewline,"\n");
    _LIT(KLngFormat,"lng   = %d\n");
    _LIT(KBytFormat,"byt   = %d\n");
    _LIT(KDblFormat,"dbl   = %S\n");
    _LIT(KTxtFormat,"txt   = %S\n");
    _LIT(KLtxtFormat,"ltxt  = %S\n");

    TBuf<16>  temp;
    TRealFormat format(16,2);

    if (aStructNum)
        console->Printf(KResourceItems,
                           aStructNum
                          );
    else 
        console->Printf(KResourceItems2);

                //  * * * * * * * * *
    console->Printf(KWrdFormat,iWrd);

                //  * * * * * * * * *
        _LIT(KLtxt,"ltxt  = \n");
    console->Printf(KFlags);
    TUint       mask = 1;
    TBuf<256> temp2;
    for (TInt ii = 0 ; ii < 16; ii++)
        {
        if (iFlags & mask)
            {
            temp2.Append(KEFlagItem);
            temp2.AppendNum(ii+1);
            temp2.Append('+');    
            }
        mask <<= 1;
        }
    if (temp2.Length())
        temp2.SetLength(temp2.Length()-1);
    console->Printf(temp2);
    console->Printf(KNewline);
        
                //  * * * * * * * * *
    console->Printf(KLngFormat,iLng);
                //  * * * * * * * * *
    console->Printf(KTxtFormat,&iTxt);

                //  * * * * * * * * * 
    if (iLtxt)
        console->Printf(KLtxtFormat,iLtxt);
    else
        console->Printf(KLtxt);
                //  * * * * * * * * *
    console->Printf(KBytFormat,iByt);

                //  * * * * * * * * *
    temp.Num(iDbl,format);
    console->Printf(KDblFormat,&temp);
                
    }
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

// Do the example(s)

LOCAL_C void doExampleL()
    {
        // Declare a resource file
    RResourceFile resourceFile;
    
        // Open the resource file
    #if defined(__WINS__)
    _LIT(KZSystemDataRsc,"Z:\\Resource\\Examples\\ReadData.rsc");
    resourceFile.OpenL(fsSession, KZSystemDataRsc);
    #endif

    #if defined(__ARM__)
    _LIT(KCSystemDataRsc,"C:\\Resource\\Examples\\ReadData.rsc");
    resourceFile.OpenL(fsSession, KCSystemDataRsc);
    #endif
    
        // Read the first resource & construct a resource reader
    HBufC8* res = resourceFile.AllocReadLC(FIRST);

    TResourceReader theReader;
    theReader.SetBuffer(res);

        // construct a CResData object to contain
        // the simple resource of type DATA.
    CResData* resData = CResData::NewLC(theReader);

        // Can now remove resData from the cleanup stack
    CleanupStack::Pop();

        // finished with res
    CleanupStack::PopAndDestroy(); 

        // display data
    resData->ShowData();

        // destroy the CResData 
    delete resData;

        // finished with resource file, so close it.
    resourceFile.Close();
    }
     

CommonFile.hrh

Found in: examples\SysLibs\ResourceFiles\SigCheck

// ReadDataFromRF.HRH
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//


                // define the maximum length of the txt string
#define TEXTMAX 256

                // define some flags
#define EFlagItem1  0x0001
#define EFlagItem2  0x0002
#define EFlagItem3  0x0004
#define EFlagItem4  0x0008

#define EFlagItem5  0x0010
#define EFlagItem6  0x0020
#define EFlagItem7  0x0040
#define EFlagItem8  0x0080

#define EFlagItem9  0x0100
#define EFlagItem10 0x0200
#define EFlagItem11 0x0400
#define EFlagItem12 0x0800

#define EFlagItem13 0x1000
#define EFlagItem14 0x2000
#define EFlagItem15 0x4000
#define EFlagItem16 0x8000

CommonToResourceFilesEx.h

Found in: examples\Syslibs\CommonFramework

// BASTD.H
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#ifndef __BASTD_H
#define __BASTD_H

#include <e32cons.h>
#include <f32file.h>
#include <barsc.h>
#include <barsread.h>


// public
LOCAL_D CConsoleBase* console; // write all your messages to this
LOCAL_D RFs fsSession;
LOCAL_C void doExampleL(); // code this function for the real example

// private
LOCAL_C void callExampleL(); // initialize with cleanup stack, then do example

GLDEF_C TInt E32Main() // main function called by E32
    {
    _LIT(KBaflEx,"BAFLEX");
    __UHEAP_MARK;
    if (fsSession.Connect()!=KErrNone)
        return KErrGeneral;
    CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
    TRAPD(error,callExampleL()); // more initialization, then do example
    fsSession.Close();
    __ASSERT_ALWAYS(!error,User::Panic(KBaflEx,error));
    delete cleanup; // destroy clean-up stack
    __UHEAP_MARKEND;
    return 0; // and return
    }

LOCAL_C void callExampleL() // initialize and call example code under cleanup stack
    {
    _LIT(KExampleCode,"SDK Example Code");
    _LIT(KFailedLeave,"failed: leave code=%d");
    _LIT(KOk,"ok");
    _LIT(KPressAnyKey," [press any key]");

    console=Console::NewL(KExampleCode,TSize(KConsFullScreen,KConsFullScreen));
    CleanupStack::PushL(console);
    TRAPD(error,doExampleL()); // perform example function
    if (error)
        console->Printf(KFailedLeave, error);
    else 
        console->Printf(KOk);
    console->Printf(KPressAnyKey);
    console->Getch(); // get and ignore character
    CleanupStack::PopAndDestroy(); // close console
    }

#endif

ReadData.rss

Found in: examples\Syslibs\ResourceFiles\ReadData.rss and is converted into an rsg file during compilation

// ReadData.rss
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.
//
// changes made - Moved TEXT and LTEXT to position before byte and double

#include "ReadData.rh"
    
RESOURCE DATA first
    {
    wrd=0;
    flags=EFlagItem1+EFlagItem2;
    lng=2; 
    txt="This is LTEXT type (max length set)";
    ltxt="This is LTEXT type (leading byte count)";
    byt=255;
    dbl=99.9;
    }


Description

ReadData is an example of how to load data from resource files into a class.

It loads the resource identified as resource FIRST from the resource file. This is a simple resource of type DATA. The example constructs a CResData object to hold the individual items corresponding to the items in a DATA structure.

The CResData object can also display the individual items at the test console. All WORD, BYTE and LONG data are interpreted as signed values


Classes used

[Top]


ReadText: reads "Hello world! from a resource file


Example code for ReadText

ReadText.cpp

Found in: examples\Syslibs\ResourceFiles\ReadText

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// ReadText.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// This example writes the text "hello world!" from a resource file to the console
// Use it as the basis of all examples which depend on BAFL

#include "CommonToResourceFilesEx.h"
#include <ReadText.rsg>

    // do the example
LOCAL_C void doExampleL()
    {
    _LIT(KFormat,"%S\n");

    // open resource file
    RResourceFile resourceFile; // resource file pointer
        // open file, leave if error
    #if defined(__WINS__)
    _LIT(KZSystemDataHelloRsc,"Z:\\Resource\\Examples\\ReadText.rsc");
    resourceFile.OpenL(fsSession, KZSystemDataHelloRsc);
    #endif

    #if defined(__ARM__)
    _LIT(KCSystemDataHelloRsc,"C:\\Resource\\Examples\\ReadText.rsc");
    resourceFile.OpenL(fsSession, KCSystemDataHelloRsc);
    #endif


    HBufC8* dataBuffer=resourceFile.AllocReadLC(HELLO);
    TResourceReader reader;
    reader.SetBuffer(dataBuffer);
    TPtrC textdata = reader.ReadTPtrC();

        // write string to test console
    console->Printf(KFormat, &textdata);
        // clean up data buffer
    CleanupStack::PopAndDestroy(); // finished with dataBuffer
        // close resource file
    resourceFile.Close(); // finished with resourceFile
    }

// ReadText.rss
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// define sundry structures

STRUCT STRING
    {
    LTEXT text;
    }

// define resources

RESOURCE STRING hello
    {
    text="Hello world!";
    }

// ReadText.mmp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// using relative paths for source and userinclude directories

TARGET         ReadText.exe
TARGETTYPE     exe
UID            0
VENDORID 0x70000001

SOURCEPATH     .
SOURCE         ReadText.cpp

START RESOURCE ReadText.rss
header
targetpath \Resource\Examples
END

USERINCLUDE    .
USERINCLUDE    ..\..\CommonFramework
SYSTEMINCLUDE  \Epoc32\include

LIBRARY        euser.lib efsrv.lib  bafl.lib

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

ReadText.mmp

CommonToResourceFilesEx.h

Found in: examples\Syslibs\ResourceFiles\CommonFramework

// CommonFramework.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.


#ifndef __CommonFramework_H
#define __CommonFramework_H

#include <e32base.h>
#include <e32cons.h>

_LIT(KTxtEPOC32EX,"EXAMPLES");
_LIT(KTxtExampleCode,"Symbian OS Example Code");
_LIT(KFormatFailed,"failed: leave code=%d");
_LIT(KTxtOK,"ok");
_LIT(KTxtPressAnyKey," [press any key]");

// public
LOCAL_D CConsoleBase* console; // write all your messages to this
LOCAL_C void doExampleL(); // code this function for the real example

// private
LOCAL_C void callExampleL(); // initialize with cleanup stack, then do example

GLDEF_C TInt E32Main() // main function called by E32
    {
    __UHEAP_MARK;
    CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
    TRAPD(error,callExampleL()); // more initialization, then do example
    __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
    delete cleanup; // destroy clean-up stack
    __UHEAP_MARKEND;
    return 0; // and return
    }

LOCAL_C void callExampleL() // initialize and call example code under cleanup stack
    {
    console=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
    CleanupStack::PushL(console);
    TRAPD(error,doExampleL()); // perform example function
    if (error)
        console->Printf(KFormatFailed, error);
    else
        console->Printf(KTxtOK);
    console->Printf(KTxtPressAnyKey);
    console->Getch(); // get and ignore character
    CleanupStack::PopAndDestroy(); // close console
    }

#endif


Description

ReadText demonstrates reading text ("hello world!") from a resource file and writing the text to a console.


Classes used

[Top]


SigCheck: checking a resource file's signature

Example code for SigCheck

Found in: examples\Syslib\ResourceFiles\SigCheck

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// SigCheck.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

//  This example, shows how the signature of a resource file is
//  checked before it is used.


#include "CommonToResourceFilesEx.h"
#include <SigCheck.rsg> // resources


// Do the example
LOCAL_C void doExampleL()
    {
    _LIT(KFormat,"%S\n");

        // Pointer to a resource file
    RResourceFile resourceFile; 
        // open resource file (leave if error)
    #if defined(__WINS__)
    _LIT(KZSystemDataSigRsc,"Z:\\Resource\\Examples\\SigCheck.rsc");
    resourceFile.OpenL(fsSession, KZSystemDataSigRsc);
    #endif

    #if defined(__ARM__)
    _LIT(KCSystemDataSigRsc,"C:\\Resource\\Examples\\SigCheck.rsc");
    resourceFile.OpenL(fsSession, KCSystemDataSigRsc);
    #endif
        // check resource file signature
    TInt version=23; // version 023
    resourceFile.ConfirmSignatureL(version);

            // confirm, and leave if error
        // read resource from file
    HBufC8* dataBuffer=resourceFile.AllocReadLC(R_BASE_HELLO);
    TResourceReader reader;
    reader.SetBuffer(dataBuffer);
    TPtrC textdata = reader.ReadTPtrC();
        // write string to test console
    console->Printf(KFormat, &textdata);
        // clean up data buffer
    CleanupStack::PopAndDestroy(); // finished with dataBuffer
        // close resource file
    resourceFile.Close(); // finished with resourceFile
    }

// SigCheck.rss
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

NAME BASE

#include <badef.rh>

// signature, for ConfirmSignature()

RESOURCE BA_RSS_SIGNATURE 
    {
    signature=023;
    }

// define resources

RESOURCE LBUF r_base_hello
    {
    txt="Bore da, pawb!";
    }

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

SigCheck.mmp

//SigCheck.mmp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// using relative paths for source and userinclude directories

TARGET         SigCheck.exe
TARGETTYPE     exe
UID        0
VENDORID 0x70000001

SOURCEPATH     .
SOURCE         SigCheck.cpp

START RESOURCE SigCheck.rss
header
targetpath \Resource\Examples
END

USERINCLUDE    .
USERINCLUDE    ..\..\CommonFramework
SYSTEMINCLUDE  \Epoc32\include

LIBRARY        euser.lib efsrv.lib  bafl.lib


Description

SigCheck is an example of how the signature of a resource file is checked before it is used.

SigCheck reads a text string from a single resource file, SigCheck.rss, in the standard way and uses RResourceFile::ConfirmSignatureL() to check the 'identity' of the resource file.


Classes used

[Top]


MultiRead1: reading multiple resource files

Example code for MultiRead1

Found in examples\Syslibs\ResourceFiles\MultiRead1

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// MultiRead1.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

/*
    This example, together with MultiRead2, shows how to use multiple
    resource files with cross-referenced resources.

    It introduces the class CMultipleResourceFileReader, capable of
    reading resources from multiple  resource files. However, only one
    resource file is used. The example MultipleReader2 uses more than one resource file
*/

#include "CommonToResourceFilesEx.h"

#include <MultiRead1.rsg> // resources
#include "MultiRead.h"

// Do the example
void doExampleL()
    {
    _LIT(KFormat,"%S\n");

    // allocate multi-reader
    CMultipleResourceFileReader* multiReader =
            CMultipleResourceFileReader::NewLC();

        // open a resource file
    #if defined(__WINS__)
            // add MultipleReader1 version 23
    _LIT(KZSystemDataBasigbRsc,"Z:\\Resource\\Examples\\MultiRead1.rsc");
    multiReader->AddResourceFileL(KZSystemDataBasigbRsc,23);
    #endif

    #if defined(__ARM__)
            // add MultipleReader1 version 23
    _LIT(KCSystemDataBasigbRsc,"\\Resource\\Examples\\MultiRead1.rsc");
    multiReader->AddResourceFileL(KCSystemDataBasigbRsc,23);
    #endif

    // read string resource from file into a descriptor
    HBufC8* dataBuffer=multiReader->AllocReadLC(R_BASE_HELLO);
    TResourceReader reader;
    reader.SetBuffer(dataBuffer);
    TPtrC textdata = reader.ReadTPtrC();

    // write string to test console
    console->Printf(KFormat, &textdata);
    // clean up data buffer
    CleanupStack::PopAndDestroy(); // finished with dataBuffer

    // cleanup multi-reader
    CleanupStack::PopAndDestroy(); // multi-reader
    }

// MultiRead.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// define the multiple resource file reader class
// for use in MultiRead1 and MultiRead2 examples

#ifndef __MultiRead_H
#define __MultiRead_H

class CMultipleResourceFileReader : public CBase
    {
public:
    // construct/destruct
    ~CMultipleResourceFileReader();
    static CMultipleResourceFileReader* NewLC();
    // add resource file
    void AddResourceFileL(const TDesC& aName, TInt aVersion);
    HBufC8* AllocReadLC(TInt aResourceId);
private:
    // construct/destruct
    void ConstructL();
private:
    CArrayFixFlat<RResourceFile>* iResourceFiles;
    };

// construct/destruct

CMultipleResourceFileReader* CMultipleResourceFileReader::NewLC()
    {
    CMultipleResourceFileReader* self=new (ELeave) CMultipleResourceFileReader;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

void CMultipleResourceFileReader::ConstructL()
    {
    iResourceFiles=new (ELeave) CArrayFixFlat<RResourceFile> (2);
        // new array of resource files
        // typically two in an application, so use granularity 2
    }

CMultipleResourceFileReader::~CMultipleResourceFileReader()
    {
    if (iResourceFiles)
        {
        for (TInt i=0; i < iResourceFiles->Count(); i++)
            (*iResourceFiles)[i].Close(); // close each resource file
        delete iResourceFiles; // and destroy container
        }
    }

// manipulate resource files and resources

void CMultipleResourceFileReader::AddResourceFileL(const TDesC& aName, TInt aVersion)
    {
    _LIT(KAddingResourceFile,"Adding resource file %S, version %d: will be file %d\n");
    console->Printf(KAddingResourceFile,
            &aName, aVersion, iResourceFiles->Count()
            );
    RResourceFile file;
    file.OpenL(fsSession,aName); // open resource file
    TRAPD(error,file.ConfirmSignatureL(aVersion)); // confirm its signature
    if (error!=KErrNone)
        {
        file.Close();
        User::Leave(error);
        }
    TRAP(error,iResourceFiles->AppendL(file))
    if (error!=KErrNone)
        {
        file.Close();
        User::Leave(error);
        }
    return;
    }

HBufC8* CMultipleResourceFileReader::AllocReadLC(TInt aResourceId)
    {
    _LIT(KReadingResource,"Reading resource %08X\n");
    _LIT(KOwnedByFile,"   Owned by file %d\n");

    console->Printf(KReadingResource,aResourceId);
    for (TInt i=0; i < iResourceFiles->Count(); i++)
        { // scan all resource files to find owner
        RResourceFile& file=(*iResourceFiles)[i]; // get resource file
        if (!file.OwnsResourceId(aResourceId))
            continue; // continue if not owner
        console->Printf(KOwnedByFile,i);
        return file.AllocReadLC(aResourceId);
                // return resource from owning file (if present)
        }
    User::Leave(KErrNotFound);
    return 0; // can never be executed, but keeps compiler happy
    }

#endif

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

MultiRead1.mmp

// MultiRead.mmp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// using relative paths for source and userinclude directories

TARGET         MultiRead1.exe
TARGETTYPE     exe
UID            0
VENDORID 0x70000001

SOURCEPATH    .
SOURCE         MultiRead1.cpp

START RESOURCE MultiRead1.rss
header
targetpath \Resource\Examples
END

USERINCLUDE    .
USERINCLUDE    ..\..\CommonFramework
SYSTEMINCLUDE  \Epoc32\include

LIBRARY        euser.lib efsrv.lib  bafl.lib

// MultiRead.rh
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// for defining text resources
STRUCT STRING
    {
    LTEXT text;
    }

// for first resource in file, for ConfirmSignature()
STRUCT SIGNATURE
    {
    LONG version;
    SRLINK self;
    }

// MultiRead1.rss
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

NAME BASE

#include "MultiRead.rh" // include for common structures

// signature, for ConfirmSignature()

RESOURCE SIGNATURE { version=023; }

// define resources

RESOURCE STRING r_base_hello
    {
    text="Ahlan Wa Sahlan!";
    }


Description

MultiRead1 is an example of a class that is capable of using more than one resource file; MultiRead1 however uses only a single resource file.

This example, together with MultiRead2 (which uses more than one resource file), shows how to use multiple resource files with cross-referenced resources.

It introduces the class CMultipleResourceFileReader, capable of reading resources from multiple resource files.


Classes used

[Top]


MultiRead2: reading multiple resource files with cross-referenced resources

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

Example code for MultiRead2

The following files are found in: examples\Syslibs\ResourceFiles\MultiRead2

// MultiRead2.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

/*
    This example shows how to use multiple resource files with cross-referenced
    resources.  It depends on the resource file created for the MultiRead1 project.

    It introduces a second resource file, MultiRead2, which contains an LLINK
    to a resource defined in the MultiRead1 project. The important point to note is
    that the effect of introducing this second resource file is minimal: just
    another #include file; and the logic in doExampleL() would be identical whether
    the LLINK pointed to another resource in the same file or a different file.
*/

#include "CommonToResourceFilesEx.h"
#include <MultiRead2.rsg> // user resources
#include "MultiRead.h"    // definition of multi-resource-reader class

// Do the example
void doExampleL()
    {
    _LIT(KFormat,"%S\n");

    // allocate multi-reader
    CMultipleResourceFileReader* multiReader =
            CMultipleResourceFileReader::NewLC();

        // open a couple of resource files
    #if defined(__WINS__)
            // add MultiRead1 version 23
    _LIT(KZSystemDataBasigbRsc,"Z:\\Resource\\Examples\\MultiRead1.rsc");
    multiReader->AddResourceFileL(KZSystemDataBasigbRsc,23);
            // add MultiRead2 version 17
    _LIT(KZSystemDataBasiguRsc,"Z:\\Resource\\Examples\\MultiRead2.rsc");
    multiReader->AddResourceFileL(KZSystemDataBasiguRsc,17);
    #endif

    #if defined(__ARM__)
            // add MultiRead1 version 23
    _LIT(KCSystemDataBasigbRsc,"C:\\Resource\\Examples\\MultiRead1.rsc");
    multiReader->AddResourceFileL(KCSystemDataBasigbRsc,23);
            // add MultiRead2 version 17
    _LIT(KCSystemDataBasiguRsc,"C:\\Resource\\Examples\\MultiRead2.rsc");
    multiReader->AddResourceFileL(KCSystemDataBasiguRsc,17);
    #endif
    
    // read resource that returns a reference to another resource
    HBufC8* refBuffer=multiReader->AllocReadLC(R_USER_HELLOREF);
    TResourceReader theReader;
    theReader.SetBuffer(refBuffer);
    TInt referencedId=theReader.ReadInt32(); // treat resource as integer
    CleanupStack::PopAndDestroy(); // refBuffer
    // read the other resource
    HBufC8* dataBuffer=multiReader->AllocReadLC(referencedId);
    TResourceReader reader;
    reader.SetBuffer(dataBuffer);
    TPtrC textdata = reader.ReadTPtrC();

    // write string to test console
    console->Printf(KFormat, &textdata);
    // clean up data buffer
    CleanupStack::PopAndDestroy(); // finished with dataBuffer
    // cleanup multi-reader
    CleanupStack::PopAndDestroy(); // multi-reader
    }

// MultiRead2.rss
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

NAME USER

#include "MultiRead.rh" // include for common structures
#include <SigCheck.rsg> // include for symbols from SigCheck.rss

// another structure

STRUCT STRINGREF
    {
    LLINK stringlink;
    }

// signature, for ConfirmSignature()

RESOURCE SIGNATURE { version=017; }

// define resources

RESOURCE STRINGREF r_user_helloref
    {
    stringlink=R_BASE_HELLO;
    }

// MultiRead2.mmp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// using relative paths for source and userinclude directories

TARGET         MultiRead2.exe
TARGETTYPE     exe
UID            0
VENDORID 0x70000001

SOURCEPATH    .
SOURCE         MultiRead2.cpp

START RESOURCE MultiRead2.rss
header
targetpath \Resource\Examples
END

USERINCLUDE    ..\MultiRead1
USERINCLUDE    .
USERINCLUDE    ..\..\CommonFramework
SYSTEMINCLUDE  \Epoc32\include

LIBRARY        euser.lib efsrv.lib  bafl.lib

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

// build the required sigcheck.rsg first
..\sigcheck\sigcheck.mmp
MultiRead2.mmp

MultiRead.h

Found in: examples\Syslibs\ResourceFiles\MultiRead1

// MultiRead.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// define the multiple resource file reader class
// for use in MultiRead1 and MultiRead2 examples

#ifndef __MultiRead_H
#define __MultiRead_H

class CMultipleResourceFileReader : public CBase
    {
public:
    // construct/destruct
    ~CMultipleResourceFileReader();
    static CMultipleResourceFileReader* NewLC();
    // add resource file
    void AddResourceFileL(const TDesC& aName, TInt aVersion);
    HBufC8* AllocReadLC(TInt aResourceId);
private:
    // construct/destruct
    void ConstructL();
private:
    CArrayFixFlat<RResourceFile>* iResourceFiles;
    };

// construct/destruct

CMultipleResourceFileReader* CMultipleResourceFileReader::NewLC()
    {
    CMultipleResourceFileReader* self=new (ELeave) CMultipleResourceFileReader;
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }

void CMultipleResourceFileReader::ConstructL()
    {
    iResourceFiles=new (ELeave) CArrayFixFlat<RResourceFile> (2);
        // new array of resource files
        // typically two in an application, so use granularity 2
    }

CMultipleResourceFileReader::~CMultipleResourceFileReader()
    {
    if (iResourceFiles)
        {
        for (TInt i=0; i < iResourceFiles->Count(); i++)
            (*iResourceFiles)[i].Close(); // close each resource file
        delete iResourceFiles; // and destroy container
        }
    }

// manipulate resource files and resources

void CMultipleResourceFileReader::AddResourceFileL(const TDesC& aName, TInt aVersion)
    {
    _LIT(KAddingResourceFile,"Adding resource file %S, version %d: will be file %d\n");
    console->Printf(KAddingResourceFile,
            &aName, aVersion, iResourceFiles->Count()
            );
    RResourceFile file;
    file.OpenL(fsSession,aName); // open resource file
    TRAPD(error,file.ConfirmSignatureL(aVersion)); // confirm its signature
    if (error!=KErrNone)
        {
        file.Close();
        User::Leave(error);
        }
    TRAP(error,iResourceFiles->AppendL(file))
    if (error!=KErrNone)
        {
        file.Close();
        User::Leave(error);
        }
    return;
    }

HBufC8* CMultipleResourceFileReader::AllocReadLC(TInt aResourceId)
    {
    _LIT(KReadingResource,"Reading resource %08X\n");
    _LIT(KOwnedByFile,"   Owned by file %d\n");

    console->Printf(KReadingResource,aResourceId);
    for (TInt i=0; i < iResourceFiles->Count(); i++)
        { // scan all resource files to find owner
        RResourceFile& file=(*iResourceFiles)[i]; // get resource file
        if (!file.OwnsResourceId(aResourceId))
            continue; // continue if not owner
        console->Printf(KOwnedByFile,i);
        return file.AllocReadLC(aResourceId);
                // return resource from owning file (if present)
        }
    User::Leave(KErrNotFound);
    return 0; // can never be executed, but keeps compiler happy
    }

#endif


Description

MultiRead2 shows how to use multiple resource files with cross-referenced resources. It depends on the resource file created for the MultiRead1 and SigCheck projects.

It introduces a second resource file, MultiRead2, which contains an LLINK to a resource defined in the MultiRead1 project. The important point to note is that the effect of introducing this second resource file is minimal: just another #include file; and the logic in doExampleL() would be identical whether the LLINK pointed to another resource in the same file or a different file.


Build Notes

You must have built MultiRead1 and SigCheck beforehand.


Classes used