examples/SysLibs/ResourceFiles/ReadData/ReadData.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // This example shows how to load data from resource files
00015 // into a class.
00016 // It loads the resource identified as resource FIRST from the resource
00017 // file. This is a simple resource of type DATA.
00018 // The example constructs a CResData object to hold the 
00019 // individual items corresponding to the items in a 
00020 // DATA struct.
00021 // The CResData object can also display the individual
00022 // items at the test console. All WORD, BYTE and LONG
00023 // are interpreted as signed values
00024 //
00025 
00026 
00027         
00028 #include "CommonToResourceFilesEx.h"
00029 
00030 #include "ReadData.h"
00031 #include <readdata.rsg>
00032 
00033                                 // Construct a new CResData object and place 
00034                                 // on the cleanup stack
00035 CResData* CResData::NewLC(TResourceReader& aReader)
00036         {
00037         CResData* self=new (ELeave) CResData;
00038         CleanupStack::PushL(self);
00039         self->ConstructL(aReader);
00040         return self;
00041         }
00042 
00043 
00044                                 // Complete the construction of the 
00045                                 // CResData object using the supplied
00046                                 // reource reader representing the resource data.
00047                                 // The structure of the data is assumed to be
00048                                 // defined by the resource struct DATA in "ReadData.rh"
00049 void CResData::ConstructL(TResourceReader& aReader)
00050         {
00051                                 // Interpret next bytes as a TInt16
00052         iWrd   = aReader.ReadInt16();
00053                         
00054                                 // Interpret next bytes as a TInt16
00055         iFlags = aReader.ReadInt16();
00056                                         
00057                                 // Interpret next bytes as a TInt32
00058         iLng   = aReader.ReadInt32();
00059 
00060         iTxt    = aReader.ReadTPtrC();
00061                                 // Interpret the next byte as the length
00062                                 // of text. The text itself follows
00063                                 // this byte.
00064         iLtxt  = aReader.ReadHBufC16L(); 
00065                                 // Interpret the next byte as a TInt8
00066         iByt   = aReader.ReadInt8();
00067         
00068                                 // Interpret next bytes as a TReal
00069         iDbl   = aReader.ReadReal64();
00070                         
00071         }
00072 
00073 
00074                                 // Destructor needs to ensure that 
00075                                 // the HBufC descriptor iLtxt is destroyed.
00076 CResData::~CResData()
00077         {
00078         if (iLtxt)
00079                 delete iLtxt;
00080         }
00081 
00082 
00083                                 // Show the individual resource itens
00084                                 // at the test console.
00085 void CResData::ShowData(const TInt aStructNum)
00086         {
00087         _LIT(KResourceItems,"Resource items (struct #%d):\n");
00088         _LIT(KResourceItems2,"Resource items:\n");
00089         _LIT(KWrdFormat,"wrd   = %d\n");
00090         _LIT(KFlags,"flags = ");
00091         _LIT(KEFlagItem,"EFlagItem");
00092         _LIT(KNewline,"\n");
00093         _LIT(KLngFormat,"lng   = %d\n");
00094         _LIT(KBytFormat,"byt   = %d\n");
00095         _LIT(KDblFormat,"dbl   = %S\n");
00096         _LIT(KTxtFormat,"txt   = %S\n");
00097         _LIT(KLtxtFormat,"ltxt  = %S\n");
00098 
00099         TBuf<16>        temp;
00100         TRealFormat     format(16,2);
00101 
00102         if (aStructNum)
00103                 console->Printf(KResourceItems,
00104                                                    aStructNum
00105                                                   );
00106         else 
00107                 console->Printf(KResourceItems2);
00108 
00109                                 //  * * * * * * * * *
00110         console->Printf(KWrdFormat,iWrd);
00111 
00112                                 //  * * * * * * * * *
00113                 _LIT(KLtxt,"ltxt  = \n");
00114         console->Printf(KFlags);
00115         TUint           mask = 1;
00116         TBuf<256>       temp2;
00117         for (TInt ii = 0 ; ii < 16; ii++)
00118                 {
00119                 if (iFlags & mask)
00120                         {
00121                         temp2.Append(KEFlagItem);
00122                         temp2.AppendNum(ii+1);
00123                         temp2.Append('+');    
00124                         }
00125                 mask <<= 1;
00126                 }
00127         if (temp2.Length())
00128                 temp2.SetLength(temp2.Length()-1);
00129         console->Printf(temp2);
00130         console->Printf(KNewline);
00131                 
00132                                 //  * * * * * * * * *
00133         console->Printf(KLngFormat,iLng);
00134                                 //  * * * * * * * * *
00135         console->Printf(KTxtFormat,&iTxt);
00136 
00137                                 //  * * * * * * * * *   
00138         if (iLtxt)
00139                 console->Printf(KLtxtFormat,iLtxt);
00140         else
00141             console->Printf(KLtxt);
00142                                 //  * * * * * * * * *
00143         console->Printf(KBytFormat,iByt);
00144 
00145                                 //  * * * * * * * * *
00146         temp.Num(iDbl,format);
00147         console->Printf(KDblFormat,&temp);
00148                                 
00149         }
00152 
00153 // Do the example(s)
00154 
00155 LOCAL_C void doExampleL()
00156     {
00157                 // Declare a resource file
00158         RResourceFile resourceFile;
00159         
00160                 // open resource file on the emulator(__WINS__  is defined for the Windows emulator)
00161                 //(leave if error)
00162         #if defined(__WINS__)
00163         _LIT(KZSystemDataRsc,"Z:\\Resource\\Apps\\ReadData.rsc");
00164         resourceFile.OpenL(fsSession, KZSystemDataRsc);
00165         #endif
00166 
00167                 // open a resource file on the target phone
00168                 // ( __EPOC32__ is defined for all target hardware platforms regardless of processor type/hardware architecture)
00169         #if defined(__EPOC32__)
00170         _LIT(KCSystemDataRsc,"Z:\\Resource\\Apps\\ReadData.rsc");
00171         resourceFile.OpenL(fsSession, KCSystemDataRsc);
00172         #endif
00173         
00174                 // Read the first resource & construct a resource reader
00175         HBufC8* res = resourceFile.AllocReadLC(FIRST);
00176 
00177         TResourceReader theReader;
00178         theReader.SetBuffer(res);
00179 
00180                 // construct a CResData object to contain
00181                 // the simple resource of type DATA.
00182         CResData* resData = CResData::NewLC(theReader);
00183 
00184                 // Can now remove resData from the cleanup stack
00185         CleanupStack::Pop();
00186 
00187                 // finished with res
00188         CleanupStack::PopAndDestroy(); 
00189 
00190                 // display data
00191         resData->ShowData();
00192 
00193                 // destroy the CResData 
00194         delete resData;
00195 
00196                 // finished with resource file, so close it.
00197         resourceFile.Close();
00198         }
00199          
00200 

Generated by  doxygen 1.6.2