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

Generated by  doxygen 1.6.2