examples/ForumNokia/Symbian_OS_Basics_Lab_Exercises_v3_1/Lab_04309.cb2/starter/src/ActiveBubbleSorter.cpp

00001 // Copyright (c) 2006 Nokia Corporation.
00002 
00003 #include "ActiveBubbleSorter.h"
00004 #include "BubbleSortNotify.h"
00005 #include <e32math.h>
00006 
00007 _LIT(KSortDataInputFileName, "C:\\private\\0515DFCE\\sortdata_in.dat");
00008 _LIT(KSortDataOutputFileName, "C:\\private\\0515DFCE\\sortdata_out.dat");
00009 _LIT8(KNumberOutputFormat, "%d\r\n");
00010 
00011 
00012 // Constructs a CActiveBubbleSorter object
00013 CActiveBubbleSorter* CActiveBubbleSorter::NewL()
00014     {
00015     CActiveBubbleSorter* self = new (ELeave) CActiveBubbleSorter();
00016     CleanupStack::PushL(self);
00017     self->ConstructL();
00018     CleanupStack::Pop(self);
00019     return self;
00020     }
00021 
00022 
00023 // C++ Constructor
00024 CActiveBubbleSorter::CActiveBubbleSorter()
00025         : CActive(CActive::EPriorityIdle)
00026     {
00027     }
00028 
00029 
00030 // C++ Destructor
00031 CActiveBubbleSorter::~CActiveBubbleSorter()
00032     {
00033     }
00034 
00035 
00036 // Symbian 2nd phase construction
00037 void CActiveBubbleSorter::ConstructL()
00038     {
00039     // Do nothing
00040     }
00041 
00042 
00043 // Handles the cleanup necessary if sorting is cancelled
00044 void CActiveBubbleSorter::DoCancel()
00045     {
00046     }
00047 
00048 
00049 // Handles each step of the sorting procedure
00050 void CActiveBubbleSorter::RunL()
00051     {
00052     }
00053 
00054 
00055 // Called to begin sorting
00056 void CActiveBubbleSorter::StartL()
00057         {
00058         }
00059         
00060         
00061 // Reads numbers from an input file into an RArray
00062 void CActiveBubbleSorter::ReadNumbersFromFileL()
00063     {
00064     // Read contents of the input file into a descriptor
00065     RFs fs;
00066     User::LeaveIfError(fs.Connect());
00067     CleanupClosePushL(fs);
00068     
00069     RFile file;
00070     User::LeaveIfError(file.Open(fs, KSortDataInputFileName, EFileStreamText | EFileRead));
00071     CleanupClosePushL(fs);
00072     
00073     TInt sz = 0;
00074     User::LeaveIfError(file.Size(sz));
00075     
00076     HBufC8* buf = HBufC8::NewLC(sz);
00077     TPtr8 ptr = buf->Des();
00078     User::LeaveIfError(file.Read(ptr));
00079     
00080     // Extract numbers from the descriptor containing the contents of the input file
00081     TLex8 lx(ptr);
00082     TBool finshed = EFalse;
00083     while (!finshed)
00084         {
00085         if (lx.Eos())
00086                 {
00087                 finshed = ETrue;
00088                 }
00089         else if ((lx.Peek()).IsDigit()) 
00090                 {
00091                 TInt num;
00092                 TInt err = lx.Val(num);
00093                 iNumbersArray.Append(num);
00094                 }
00095         else
00096                 {
00097                 lx.Inc();
00098                 }
00099         }
00100     
00101     CleanupStack::PopAndDestroy(3); // automatically closes fs and file
00102     }
00103 
00104 
00105 // Writes the numbers in the RArray to an output file
00106 void CActiveBubbleSorter::WriteNumbersToFileL()
00107     {
00108     RFs fs;
00109     User::LeaveIfError(fs.Connect());
00110     CleanupClosePushL(fs);
00111     
00112     TInt ret = fs.Delete(KSortDataOutputFileName);
00113     if (!(ret == KErrNone || ret == KErrNotFound))
00114         {
00115         User::Leave(ret);
00116         } 
00117     
00118     RFile file;
00119     User::LeaveIfError(file.Create(fs, KSortDataOutputFileName, EFileStreamText | EFileWrite));
00120     CleanupClosePushL(fs);
00121     
00122     TBuf8<10> buf;
00123     
00124     for (TInt i = 0; i < iNumbersArray.Count(); i++)
00125         {
00126         buf.Format(KNumberOutputFormat, iNumbersArray[i]);
00127         file.Write(buf);
00128         }
00129         
00130     CleanupStack::PopAndDestroy(2); // automatically closes fs and file
00131     }
00132     
00133     
00134 // Called in the event the RunL function leaves
00135 TInt CActiveBubbleSorter::RunError(TInt aError)
00136         {
00137         iNumbersArray.Reset();
00138         iNotifier.SortComplete(aError);
00139         
00140         return KErrNone;
00141         }
00142         
00143 // End of file

Generated by  doxygen 1.6.2