examples/SysLibs/ResourceFiles/ReadData/ReadData.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 resource FIRST from the resource
00031 file. This is a simple resource of type DATA.
00032 The example constructs a CResData object to hold the 
00033 individual items corresponding to the items in a 
00034 DATA struct.
00035 The CResData object can also display the individual
00036 items at the test console. All WORD, BYTE and LONG
00037 are interpreted as signed values  
00038 */
00039 
00040 
00041 
00042 
00043         
00044 #include "CommonToResourceFilesEx.h"
00045 
00046 #include "ReadData.h"
00047 #include <readdata.rsg>
00048 
00049                                 // Construct a new CResData object and place 
00050                                 // on the cleanup stack
00051 CResData* CResData::NewLC(TResourceReader& aReader)
00052         {
00053         CResData* self=new (ELeave) CResData;
00054         CleanupStack::PushL(self);
00055         self->ConstructL(aReader);
00056         return self;
00057         }
00058 
00059 
00060                                 // Complete the construction of the 
00061                                 // CResData object using the supplied
00062                                 // reource reader representing the resource data.
00063                                 // The structure of the data is assumed to be
00064                                 // defined by the resource struct DATA in "ReadData.rh"
00065 void CResData::ConstructL(TResourceReader& aReader)
00066         {
00067                                 // Interpret next bytes as a TInt16
00068         iWrd   = aReader.ReadInt16();
00069                         
00070                                 // Interpret next bytes as a TInt16
00071         iFlags = aReader.ReadInt16();
00072                                         
00073                                 // Interpret next bytes as a TInt32
00074         iLng   = aReader.ReadInt32();
00075 
00076         iTxt    = aReader.ReadTPtrC();
00077                                 // Interpret the next byte as the length
00078                                 // of text. The text itself follows
00079                                 // this byte.
00080         iLtxt  = aReader.ReadHBufC16L(); 
00081                                 // Interpret the next byte as a TInt8
00082         iByt   = aReader.ReadInt8();
00083         
00084                                 // Interpret next bytes as a TReal
00085         iDbl   = aReader.ReadReal64();
00086                         
00087         }
00088 
00089 
00090                                 // Destructor needs to ensure that 
00091                                 // the HBufC descriptor iLtxt is destroyed.
00092 CResData::~CResData()
00093         {
00094         if (iLtxt)
00095                 delete iLtxt;
00096         }
00097 
00098 
00099                                 // Show the individual resource itens
00100                                 // at the test console.
00101 void CResData::ShowData(const TInt aStructNum)
00102         {
00103         _LIT(KResourceItems,"Resource items (struct #%d):\n");
00104         _LIT(KResourceItems2,"Resource items:\n");
00105         _LIT(KWrdFormat,"wrd   = %d\n");
00106         _LIT(KFlags,"flags = ");
00107         _LIT(KEFlagItem,"EFlagItem");
00108         _LIT(KNewline,"\n");
00109         _LIT(KLngFormat,"lng   = %d\n");
00110         _LIT(KBytFormat,"byt   = %d\n");
00111         _LIT(KDblFormat,"dbl   = %S\n");
00112         _LIT(KTxtFormat,"txt   = %S\n");
00113         _LIT(KLtxtFormat,"ltxt  = %S\n");
00114 
00115         TBuf<16>        temp;
00116         TRealFormat     format(16,2);
00117 
00118         if (aStructNum)
00119                 console->Printf(KResourceItems,
00120                                                    aStructNum
00121                                                   );
00122         else 
00123                 console->Printf(KResourceItems2);
00124 
00125                                 //  * * * * * * * * *
00126         console->Printf(KWrdFormat,iWrd);
00127 
00128                                 //  * * * * * * * * *
00129                 _LIT(KLtxt,"ltxt  = \n");
00130         console->Printf(KFlags);
00131         TUint           mask = 1;
00132         TBuf<256>       temp2;
00133         for (TInt ii = 0 ; ii < 16; ii++)
00134                 {
00135                 if (iFlags & mask)
00136                         {
00137                         temp2.Append(KEFlagItem);
00138                         temp2.AppendNum(ii+1);
00139                         temp2.Append('+');    
00140                         }
00141                 mask <<= 1;
00142                 }
00143         if (temp2.Length())
00144                 temp2.SetLength(temp2.Length()-1);
00145         console->Printf(temp2);
00146         console->Printf(KNewline);
00147                 
00148                                 //  * * * * * * * * *
00149         console->Printf(KLngFormat,iLng);
00150                                 //  * * * * * * * * *
00151         console->Printf(KTxtFormat,&iTxt);
00152 
00153                                 //  * * * * * * * * *   
00154         if (iLtxt)
00155                 console->Printf(KLtxtFormat,iLtxt);
00156         else
00157             console->Printf(KLtxt);
00158                                 //  * * * * * * * * *
00159         console->Printf(KBytFormat,iByt);
00160 
00161                                 //  * * * * * * * * *
00162         temp.Num(iDbl,format);
00163         console->Printf(KDblFormat,&temp);
00164                                 
00165         }
00168 
00169 // Do the example(s)
00170 
00171 LOCAL_C void doExampleL()
00172     {
00173                 // Declare a resource file
00174         RResourceFile resourceFile;
00175         
00176                 // open resource file on the emulator(__WINS__  is defined for the Windows emulator)
00177                 //(leave if error)
00178         #if defined(__WINS__)
00179         _LIT(KZSystemDataRsc,"Z:\\Resource\\Apps\\ReadData.rsc");
00180         resourceFile.OpenL(fsSession, KZSystemDataRsc);
00181         #endif
00182 
00183                 // open a resource file on the target phone
00184                 // ( __EPOC32__ is defined for all target hardware platforms regardless of processor type/hardware architecture)
00185         #if defined(__EPOC32__)
00186         _LIT(KCSystemDataRsc,"Z:\\Resource\\Apps\\ReadData.rsc");
00187         resourceFile.OpenL(fsSession, KCSystemDataRsc);
00188         #endif
00189         
00190                 // Read the first resource & construct a resource reader
00191         HBufC8* res = resourceFile.AllocReadLC(FIRST);
00192 
00193         TResourceReader theReader;
00194         theReader.SetBuffer(res);
00195 
00196                 // construct a CResData object to contain
00197                 // the simple resource of type DATA.
00198         CResData* resData = CResData::NewLC(theReader);
00199 
00200                 // Can now remove resData from the cleanup stack
00201         CleanupStack::Pop();
00202 
00203                 // finished with res
00204         CleanupStack::PopAndDestroy(); 
00205 
00206                 // display data
00207         resData->ShowData();
00208 
00209                 // destroy the CResData 
00210         delete resData;
00211 
00212                 // finished with resource file, so close it.
00213         resourceFile.Close();
00214         }
00215          
00216 

Generated by  doxygen 1.6.2