examples/SysLibs/ResourceFiles/ReadArray/ReadArray.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 SECOND from the resource file
00017 // This is an array of DATA structs. 
00018 // (The resource FIRST is not used in this example)
00019 // The example constructs a CResDataArray object to contain  
00020 // an array of CResData objects, one for each DATA struct 
00021 // in the array.
00022 // The CResDataArray object can also display the individual 
00023 // items of each DATA struct at the test console. All WORD, BYTE
00024 // and LONG are interpreted as signed values.
00025 // The example follows on from the ReadData example
00026 //
00027 
00028 
00029         
00030 #include "CommonToResourceFilesEx.h"
00031 #include "ReadArray.h"
00032 #include <readarray.rsg>
00033 
00034                                 // Construct a new CResDataArray object and
00035                                 // place on the cleanup stack
00036 CResDataArray* CResDataArray::NewLC(TResourceReader& aReader)
00037         {
00038         CResDataArray* self=new (ELeave) CResDataArray;
00039         CleanupStack::PushL(self);
00040         self->ConstructL(aReader);
00041         return self;
00042         }
00043 
00044 
00045                                 // Complete the construction of the 
00046                                 // CResDataArray object by constructing an array 
00047                                 // of (pointers to) CResData objects.
00048                                 //
00049                                 // Granularity of iDataArray is 3 because we know
00050                                 // that only 3 elements are to be added in
00051                                 // this example.
00052 void CResDataArray::ConstructL(TResourceReader& aReader)
00053         {
00054         iDataArray = new (ELeave) CArrayPtrFlat<CResData> (3); 
00055         TRAPD(error,AddDataL(aReader));
00056         if (error)
00057                 {
00058                 iDataArray->ResetAndDestroy();
00059                 delete iDataArray;
00060                 User::Leave(error);
00061                 }
00062         }
00063 
00064 
00065                                 // Destructor needs to ensure that 
00066                                 // the array of (pointers to) CResData
00067                                 // objects is destroyed.
00068 CResDataArray::~CResDataArray()
00069         {
00070         if (iDataArray)
00071                 {
00072                 iDataArray->ResetAndDestroy();
00073                 delete iDataArray;
00074                 }
00075         }
00076 
00077 
00078                                 // For each DATA element within the resource,
00079                                 // construct a CResData object and add its
00080                                 // pointer into the array.
00081 void CResDataArray::AddDataL(TResourceReader& aReader)
00082         {
00083         TInt    index;
00084         TInt    number;
00085         
00086                                 // The first WORD contains the number 
00087                                 // of DATA structs within the resource
00088         number = aReader.ReadInt16();
00089         
00090                                 // Add all newly created CResData objects 
00091                                 // to the cleanup stack before adding 
00092                                 // to the array
00093         for (index = 0; index < number ; index++)
00094                 {
00095                 CResData* resData = CResData::NewLC(aReader);
00096                 iDataArray->AppendL(resData);
00097                 CleanupStack::Pop(); // now resData safely in array
00098                 }
00099         }
00100 
00101                                 // Show the individual resource items for 
00102                                 // each DATA struct in the resource at the 
00103                                 // test console.
00104 void CResDataArray::ShowAllData()
00105         {
00106         _LIT(KPressAnyKeyToContinue," -->press any key to continue\n\n");
00107         TInt count;
00108 
00109         count = (iDataArray? iDataArray->Count() : 0);
00110         
00111         for (TInt index = 0; index < count; index++)
00112                 {
00113                 (*iDataArray)[index]->ShowData(index+1);
00114                 console->Printf(KPressAnyKeyToContinue);
00115                 console->Getch();
00116                 }
00117         }
00118 
00119 
00120                                 // Construct a new CResData object and place 
00121                                 // on the cleanup stack
00122 CResData* CResData::NewLC(TResourceReader& aReader)
00123         {
00124         CResData* self=new (ELeave) CResData;
00125         CleanupStack::PushL(self);
00126         self->ConstructL(aReader);
00127         return self;
00128         }
00129 
00130 
00131                                 // Complete the construction of the 
00132                                 // CResData object using the supplied
00133                                 // reource reader representing the resource data.
00134                                 // The structure of the data is assumed to be
00135                                 // defined by the resource struct DATA in "baarray.rh"
00136 void CResData::ConstructL(TResourceReader& aReader)
00137         {
00138                                 // Interpret next bytes as a TInt16
00139         iWrd   = aReader.ReadInt16();
00140                         
00141                                 // Interpret next bytes as a TInt16
00142         iFlags = aReader.ReadInt16();
00143                                         
00144                                 // Interpret next bytes as a TInt32
00145         iLng   = aReader.ReadInt32();
00146 
00147                                 // Interpret the next bytes as a zero
00148                                 // terminated string. The string will have 
00149                                 // a maximum length defined by the
00150                                 // symbol TEXTMAX 
00151         TPtrC temp = aReader.ReadTPtrC();       
00152         (iTxt.Des()).Copy(temp);   
00153                                                                                 
00154                                 // Interpret the next bytes as variable length text.
00155         iLtxt  = aReader.ReadHBufCL(); 
00156                                         
00157                                 // Interpret the next byte as a TUInt8 (byte)
00158         iByt   = aReader.ReadUint8();
00159         
00160                                 // Interpret next bytes as a TReal
00161         iDbl   = aReader.ReadReal64();                  
00162         }
00163 
00164 
00165                                 // Destructor needs to ensure that 
00166                                 // the HBufC descriptor iLtxt is destroyed.
00167 CResData::~CResData()
00168         {
00169         delete iLtxt;
00170         }
00171 
00172 
00173                                 // Show the individual resource itens
00174                                 // at the test console.
00175 void CResData::ShowData(const TInt aStructNum)
00176         {
00177         _LIT(KResourceItems,"Resource items (struct #%d):\n");
00178         _LIT(KResourceItems2,"Resource items:\n");
00179         _LIT(KWrdFormat,"wrd   = %d\n");
00180         _LIT(KFlags,"flags = ");
00181         _LIT(KEFlagItem,"EFlagItem");
00182         _LIT(KNewline,"\n");
00183         _LIT(KLngFormat,"lng   = %d\n");
00184         _LIT(KBytFormat,"byt   = %d\n");
00185         _LIT(KDblFormat,"dbl   = %S\n");
00186         _LIT(KTxtFormat,"txt   = %S\n");
00187         _LIT(KLtxtFormat,"ltxt  = %S\n");
00188         _LIT(KLtxt,"ltxt  = \n");
00189 
00190         TBuf<16>        temp;
00191         TRealFormat     format(16,2);
00192 
00193         if (aStructNum)
00194                 console->Printf(KResourceItems,aStructNum);
00195         else 
00196                 console->Printf(KResourceItems2);
00197 
00198                                 //  * * * * * * * * *
00199         console->Printf(KWrdFormat,iWrd);
00200 
00201                                 //  * * * * * * * * *
00202         console->Printf(KFlags);
00203         TUint           mask = 1;
00204         TBuf<256>       temp2;
00205         for (TInt ii = 0 ; ii < 16; ii++)
00206                 {
00207                 if (iFlags & mask)
00208                         {
00209                         temp2.Append(KEFlagItem);
00210                         temp2.AppendNum(ii+1);
00211                         temp2.Append('+');    
00212                         }
00213                 mask <<= 1;
00214                 }
00215         if (temp2.Length())
00216                 temp2.SetLength(temp2.Length()-1);
00217         console->Printf(temp2);
00218         console->Printf(KNewline);
00219                 
00220                                 //  * * * * * * * * *
00221         console->Printf(KLngFormat,iLng);
00222         
00223                                 //  * * * * * * * * *
00224         console->Printf(KBytFormat,iByt);
00225 
00226                                 //  * * * * * * * * *
00227         temp.Num(iDbl,format);
00228         console->Printf(KDblFormat,&temp);
00229 
00230                                 //  * * * * * * * * *
00231         console->Printf(KTxtFormat,&iTxt);
00232 
00233                                 //  * * * * * * * * *   
00234         if (iLtxt)
00235                 console->Printf(KLtxtFormat,iLtxt);
00236         else
00237             console->Printf(KLtxt);
00238         }
00241 
00242 // Do the example(s)
00243 
00244 LOCAL_C void doExampleL()
00245     {
00246                 // Declare a resource file
00247         RResourceFile resourceFile;
00248         
00249         // open resource file on the emulator(__WINS__  is defined for the Windows emulator)
00250         // (leave if error)
00251         #if defined(__WINS__)
00252         _LIT(KZSystemDataArrayRsc,"Z:\\Resource\\apps\\ReadArray.rsc");
00253         resourceFile.OpenL(fsSession, KZSystemDataArrayRsc);
00254         #endif
00255 
00256         // open a resource file on the target phone
00257         // ( __EPOC32__ is defined for all target hardware platforms regardless of processor type/hardware architecture)
00258         #if defined(__EPOC32__)
00259         _LIT(KCSystemDataArrayRsc,"Z:\\Resource\\Apps\\ReadArray.rsc");
00260         resourceFile.OpenL(fsSession, KCSystemDataArrayRsc);
00261         #endif
00262 
00263         // Read the second resource & construct a resource reader
00264         HBufC8* res = resourceFile.AllocReadLC(SECOND);
00265         
00266         TResourceReader theReader;
00267         theReader.SetBuffer(res);
00268 
00269         // Construct a CResDataArray object to contain
00270         // the array of CResData objects, and add the elements to it
00271         CResDataArray* resDataArray = CResDataArray::NewLC(theReader);
00272 
00273         // Can now remove resDataArray from cleanup stack
00274         CleanupStack::Pop();
00275         
00276         // finished with res
00277         CleanupStack::PopAndDestroy();
00278 
00279         // display all data
00280         resDataArray->ShowAllData();
00281 
00282         // finished with CResDataArray 
00283         delete resDataArray;
00284         
00285         // finished with resource file, so close it 
00286         resourceFile.Close();
00287         } 
00288 

Generated by  doxygen 1.6.2