examples/SysLibs/ResourceFiles/ReadArray/ReadArray.cpp

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 This example shows how to load data from resource files
00029 into a class.
00030 It loads the resource identified as SECOND from the resource file
00031 This is an array of DATA structs. 
00032 (The resource FIRST is not used in this example)
00033 The example constructs a CResDataArray object to contain  
00034 an array of CResData objects, one for each DATA struct 
00035 in the array.
00036 The CResDataArray object can also display the individual 
00037 items of each DATA struct at the test console. All WORD, BYTE
00038 and LONG are interpreted as signed values.
00039 The example follows on from the ReadData example 
00040 */
00041 
00042 
00043 
00044 
00045         
00046 #include "CommonToResourceFilesEx.h"
00047 #include "ReadArray.h"
00048 #include <readarray.rsg>
00049 
00050                                 // Construct a new CResDataArray object and
00051                                 // place on the cleanup stack
00052 CResDataArray* CResDataArray::NewLC(TResourceReader& aReader)
00053         {
00054         CResDataArray* self=new (ELeave) CResDataArray;
00055         CleanupStack::PushL(self);
00056         self->ConstructL(aReader);
00057         return self;
00058         }
00059 
00060 
00061                                 // Complete the construction of the 
00062                                 // CResDataArray object by constructing an array 
00063                                 // of (pointers to) CResData objects.
00064                                 //
00065                                 // Granularity of iDataArray is 3 because we know
00066                                 // that only 3 elements are to be added in
00067                                 // this example.
00068 void CResDataArray::ConstructL(TResourceReader& aReader)
00069         {
00070         iDataArray = new (ELeave) CArrayPtrFlat<CResData> (3); 
00071         TRAPD(error,AddDataL(aReader));
00072         if (error)
00073                 {
00074                 iDataArray->ResetAndDestroy();
00075                 delete iDataArray;
00076                 User::Leave(error);
00077                 }
00078         }
00079 
00080 
00081                                 // Destructor needs to ensure that 
00082                                 // the array of (pointers to) CResData
00083                                 // objects is destroyed.
00084 CResDataArray::~CResDataArray()
00085         {
00086         if (iDataArray)
00087                 {
00088                 iDataArray->ResetAndDestroy();
00089                 delete iDataArray;
00090                 }
00091         }
00092 
00093 
00094                                 // For each DATA element within the resource,
00095                                 // construct a CResData object and add its
00096                                 // pointer into the array.
00097 void CResDataArray::AddDataL(TResourceReader& aReader)
00098         {
00099         TInt    index;
00100         TInt    number;
00101         
00102                                 // The first WORD contains the number 
00103                                 // of DATA structs within the resource
00104         number = aReader.ReadInt16();
00105         
00106                                 // Add all newly created CResData objects 
00107                                 // to the cleanup stack before adding 
00108                                 // to the array
00109         for (index = 0; index < number ; index++)
00110                 {
00111                 CResData* resData = CResData::NewLC(aReader);
00112                 iDataArray->AppendL(resData);
00113                 CleanupStack::Pop(); // now resData safely in array
00114                 }
00115         }
00116 
00117                                 // Show the individual resource items for 
00118                                 // each DATA struct in the resource at the 
00119                                 // test console.
00120 void CResDataArray::ShowAllData()
00121         {
00122         _LIT(KPressAnyKeyToContinue," -->press any key to continue\n\n");
00123         TInt count;
00124 
00125         count = (iDataArray? iDataArray->Count() : 0);
00126         
00127         for (TInt index = 0; index < count; index++)
00128                 {
00129                 (*iDataArray)[index]->ShowData(index+1);
00130                 console->Printf(KPressAnyKeyToContinue);
00131                 console->Getch();
00132                 }
00133         }
00134 
00135 
00136                                 // Construct a new CResData object and place 
00137                                 // on the cleanup stack
00138 CResData* CResData::NewLC(TResourceReader& aReader)
00139         {
00140         CResData* self=new (ELeave) CResData;
00141         CleanupStack::PushL(self);
00142         self->ConstructL(aReader);
00143         return self;
00144         }
00145 
00146 
00147                                 // Complete the construction of the 
00148                                 // CResData object using the supplied
00149                                 // reource reader representing the resource data.
00150                                 // The structure of the data is assumed to be
00151                                 // defined by the resource struct DATA in "baarray.rh"
00152 void CResData::ConstructL(TResourceReader& aReader)
00153         {
00154                                 // Interpret next bytes as a TInt16
00155         iWrd   = aReader.ReadInt16();
00156                         
00157                                 // Interpret next bytes as a TInt16
00158         iFlags = aReader.ReadInt16();
00159                                         
00160                                 // Interpret next bytes as a TInt32
00161         iLng   = aReader.ReadInt32();
00162 
00163                                 // Interpret the next bytes as a zero
00164                                 // terminated string. The string will have 
00165                                 // a maximum length defined by the
00166                                 // symbol TEXTMAX 
00167         TPtrC temp = aReader.ReadTPtrC();       
00168         (iTxt.Des()).Copy(temp);   
00169                                                                                 
00170                                 // Interpret the next bytes as variable length text.
00171         iLtxt  = aReader.ReadHBufCL(); 
00172                                         
00173                                 // Interpret the next byte as a TUInt8 (byte)
00174         iByt   = aReader.ReadUint8();
00175         
00176                                 // Interpret next bytes as a TReal
00177         iDbl   = aReader.ReadReal64();                  
00178         }
00179 
00180 
00181                                 // Destructor needs to ensure that 
00182                                 // the HBufC descriptor iLtxt is destroyed.
00183 CResData::~CResData()
00184         {
00185         delete iLtxt;
00186         }
00187 
00188 
00189                                 // Show the individual resource itens
00190                                 // at the test console.
00191 void CResData::ShowData(const TInt aStructNum)
00192         {
00193         _LIT(KResourceItems,"Resource items (struct #%d):\n");
00194         _LIT(KResourceItems2,"Resource items:\n");
00195         _LIT(KWrdFormat,"wrd   = %d\n");
00196         _LIT(KFlags,"flags = ");
00197         _LIT(KEFlagItem,"EFlagItem");
00198         _LIT(KNewline,"\n");
00199         _LIT(KLngFormat,"lng   = %d\n");
00200         _LIT(KBytFormat,"byt   = %d\n");
00201         _LIT(KDblFormat,"dbl   = %S\n");
00202         _LIT(KTxtFormat,"txt   = %S\n");
00203         _LIT(KLtxtFormat,"ltxt  = %S\n");
00204         _LIT(KLtxt,"ltxt  = \n");
00205 
00206         TBuf<16>        temp;
00207         TRealFormat     format(16,2);
00208 
00209         if (aStructNum)
00210                 console->Printf(KResourceItems,aStructNum);
00211         else 
00212                 console->Printf(KResourceItems2);
00213 
00214                                 //  * * * * * * * * *
00215         console->Printf(KWrdFormat,iWrd);
00216 
00217                                 //  * * * * * * * * *
00218         console->Printf(KFlags);
00219         TUint           mask = 1;
00220         TBuf<256>       temp2;
00221         for (TInt ii = 0 ; ii < 16; ii++)
00222                 {
00223                 if (iFlags & mask)
00224                         {
00225                         temp2.Append(KEFlagItem);
00226                         temp2.AppendNum(ii+1);
00227                         temp2.Append('+');    
00228                         }
00229                 mask <<= 1;
00230                 }
00231         if (temp2.Length())
00232                 temp2.SetLength(temp2.Length()-1);
00233         console->Printf(temp2);
00234         console->Printf(KNewline);
00235                 
00236                                 //  * * * * * * * * *
00237         console->Printf(KLngFormat,iLng);
00238         
00239                                 //  * * * * * * * * *
00240         console->Printf(KBytFormat,iByt);
00241 
00242                                 //  * * * * * * * * *
00243         temp.Num(iDbl,format);
00244         console->Printf(KDblFormat,&temp);
00245 
00246                                 //  * * * * * * * * *
00247         console->Printf(KTxtFormat,&iTxt);
00248 
00249                                 //  * * * * * * * * *   
00250         if (iLtxt)
00251                 console->Printf(KLtxtFormat,iLtxt);
00252         else
00253             console->Printf(KLtxt);
00254         }
00257 
00258 // Do the example(s)
00259 
00260 LOCAL_C void doExampleL()
00261     {
00262                 // Declare a resource file
00263         RResourceFile resourceFile;
00264         
00265         // open resource file on the emulator(__WINS__  is defined for the Windows emulator)
00266         // (leave if error)
00267         #if defined(__WINS__)
00268         _LIT(KZSystemDataArrayRsc,"Z:\\Resource\\apps\\ReadArray.rsc");
00269         resourceFile.OpenL(fsSession, KZSystemDataArrayRsc);
00270         #endif
00271 
00272         // open a resource file on the target phone
00273         // ( __EPOC32__ is defined for all target hardware platforms regardless of processor type/hardware architecture)
00274         #if defined(__EPOC32__)
00275         _LIT(KCSystemDataArrayRsc,"Z:\\Resource\\Apps\\ReadArray.rsc");
00276         resourceFile.OpenL(fsSession, KCSystemDataArrayRsc);
00277         #endif
00278 
00279         // Read the second resource & construct a resource reader
00280         HBufC8* res = resourceFile.AllocReadLC(SECOND);
00281         
00282         TResourceReader theReader;
00283         theReader.SetBuffer(res);
00284 
00285         // Construct a CResDataArray object to contain
00286         // the array of CResData objects, and add the elements to it
00287         CResDataArray* resDataArray = CResDataArray::NewLC(theReader);
00288 
00289         // Can now remove resDataArray from cleanup stack
00290         CleanupStack::Pop();
00291         
00292         // finished with res
00293         CleanupStack::PopAndDestroy();
00294 
00295         // display all data
00296         resDataArray->ShowAllData();
00297 
00298         // finished with CResDataArray 
00299         delete resDataArray;
00300         
00301         // finished with resource file, so close it 
00302         resourceFile.Close();
00303         } 
00304 

Generated by  doxygen 1.6.2