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

Generated by  doxygen 1.6.2