examples/Base/MemMan/Cleanup/TAnyRObjects2/TAnyRObjects2.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 // AIM: To provide examples for the documentation of exceptions & traps
00015 // Situation3 - examples of cleanup-stack support for TAny* and RItems
00016 //
00017 
00018 
00019 #include "CommonFramework.h"
00020 
00021 // Name of file to be used
00022 _LIT(KFileName,"TAnyandRObjects2.dat");
00023 
00024 // Test data to be put into the file.
00025 _LIT(KTestData,"Test data for TAnyandRObjects2\n");
00026 
00027 // #include specific files
00028 #include <f32file.h>
00029 #include "EUHEXDMP.H"
00030 
00031 
00033 //
00034 // ----->  RFileWithCleanup(definition)
00035 //
00036 // Function Cleanup() and operator TCleanupItem() needed to provide 
00037 // Cleanup Stack for RFile
00038 //
00040 class RFileWithCleanup : public RFile
00041         {
00042 private:
00043         static void Cleanup(TAny *aPtr);
00044 public:
00045         operator TCleanupItem();
00046 public:
00047         void OpenL(RFs &aFs,const TDesC &aName,TUint aMode);
00048         void OpenLC(RFs &aFs,const TDesC &aName,TUint aMode);
00049         };
00050 
00051 
00053 //
00054 // ----->  RFileWithCleanup(implementation)
00055 //
00057 void RFileWithCleanup::Cleanup(TAny *aPtr)
00058         {
00059         _LIT(KMsgDoingCleanup,"Doing cleanup of file.\n");
00060         console->Printf(KMsgDoingCleanup);                                              
00061           // Invoke the Close member on the RItem at aPtr
00062         ((RFileWithCleanup *)aPtr)->Close();
00063         }
00064 
00065 RFileWithCleanup::operator TCleanupItem()
00066         {
00067         return TCleanupItem(Cleanup,this);
00068         }
00069 
00070 void RFileWithCleanup::OpenL(RFs &aFs,const TDesC &aName,TUint aMode)
00071         {
00072         User::LeaveIfError(RFile::Open(aFs,aName,aMode));
00073         }
00074 
00075 void RFileWithCleanup::OpenLC(RFs &aFs,const TDesC &aName,TUint aMode)
00076         {
00077         OpenL(aFs,aName,aMode);
00078         CleanupStack::PushL(*this);     // NB. 'this' would have been a TAny*
00079         }
00080 
00081 LOCAL_C void createDataFileL()
00082         {
00083           // utility function to create some data which we can later read
00084         RFs createFileSession;
00085         RFileWithCleanup createFile;
00086 
00087       // connect to filserver session           
00088         User::LeaveIfError(createFileSession.Connect());
00089 
00090 
00091           // create the private directory
00092           // on the writable drive
00093           // i.e. "\private\0FFFFF01\"
00094           // Note that the number 0FFFFF01 is the 
00095           // process security id taken from the 2nd UID
00096           // specified in the mmp file.
00097     createFileSession.CreatePrivatePath(RFs::GetSystemDrive());
00098     
00099       // Set the session path to
00100       // this private directory on writable drive
00101     createFileSession.SetSessionToPrivate(RFs::GetSystemDrive());
00102 
00103           // create TAnyandRObjects2.dat and open for writing
00104         User::LeaveIfError(createFile.Replace(createFileSession,
00105                                                   KFileName,
00106                                                               EFileWrite|EFileStreamText));
00107 
00108       // Note that Write() requires a TDesC8
00109           // type so we need to construct an explicit
00110           // TDesC8 type to represent the data contained
00111           // in the standard (16-bit) descriptor.
00112         TPtrC8 representation((TUint8*)(&KTestData)->Ptr(), (&KTestData)->Size());
00113 
00114           // write and commit text
00115         User::LeaveIfError(createFile.Write(representation));
00116         User::LeaveIfError(createFile.Flush());
00117         _LIT(KMsgDataWritten,"Data written to file\n");
00118         console->Printf(KMsgDataWritten);
00119 
00120           // close file and session
00121           // (NB. no LeaveIfError due to RFile.close and 
00122           // RFs.close guaranteed to complete)
00123         createFile.Close();                           // close file
00124         createFileSession.Close();                    // close file server session
00125         }
00126 
00127 void useBufferL(TPtr8& bufferPtr)
00128         {
00129         printBuffer(0,bufferPtr);
00130           // Remove following comment to force a leave
00131           // while using the buffer
00132         //User::Leave(KErrGeneral);
00133         }
00134 
00135 
00136 LOCAL_C void doExampleL()
00137     {
00138                                 // create the datafile for the example
00139         createDataFileL();
00140                         
00141                                 // create a simple buffer. In real code, you
00142                                 // would probably use an HBufC*, or an RBuf.
00143                                 // You could also use a TBuf on the stack if it's small.
00144         TText8* buffer=(TText8*) User::Alloc(100*sizeof(TText8));
00145 
00146                                 // push it to the cleanup stack: treated as TAny*
00147         CleanupStack::PushL(buffer);
00148         
00149                                 // create a pointer to the buffer
00150         TPtr8 bufferPtr(buffer,100);
00151         
00152                                 // the file session to be used
00153         RFs fsSession;
00154         _LIT(KMsgOpeningSession,"Opening session\n");
00155         console->Printf(KMsgOpeningSession);
00156         
00157                                 // open the file-server session
00158         User::LeaveIfError(fsSession.Connect());
00159         
00160                                 // the file instance myFile
00161         RFileWithCleanup myFile;
00162         _LIT(KMsgOpeningFile,"Opening file\n");
00163         console->Printf(KMsgOpeningFile);
00164         
00165                         // open the file (and leave on cleanup stack)
00166         myFile.OpenLC(fsSession,KFileName,EFileStreamText|EFileRead);   
00167         
00168                                 // read stuff from the file to the buffer (may leave)
00169         _LIT(KMsgReadingFile,"Reading file into buffer.\n");
00170         console->Printf(KMsgReadingFile);
00171         User::LeaveIfError(myFile.Read(bufferPtr));
00172       // Remove following comment to force a leave
00173           // while using the file
00174         //User::Leave(KErrGeneral);
00175 
00176                                 // destroy the file on the cleanup stack
00177         CleanupStack::PopAndDestroy();          
00178 
00179         fsSession.Close();
00180 
00181                                 // use the buffer
00182         useBufferL(bufferPtr);
00183                                 // destroy the buffer on the cleanup stack
00184         CleanupStack::PopAndDestroy();          
00185         }
00186 

Generated by  doxygen 1.6.2