ActiveBubbleSorter.cpp
上传用户:laixiong
上传日期:2007-03-11
资源大小:2994k
文件大小:4k
- // Copyright (c) 2006 Nokia Corporation.
- #include "ActiveBubbleSorter.h"
- #include "BubbleSortNotify.h"
- #include <e32math.h>
- _LIT(KSortDataInputFileName, "C:\private\0515DFCE\sortdata_in.dat");
- _LIT(KSortDataOutputFileName, "C:\private\0515DFCE\sortdata_out.dat");
- _LIT8(KNumberOutputFormat, "%drn");
- // Constructs a CActiveBubbleSorter object
- CActiveBubbleSorter* CActiveBubbleSorter::NewL(MBubbleSortNotify& aNotifier)
- {
- CActiveBubbleSorter* self = new (ELeave) CActiveBubbleSorter(aNotifier);
- CleanupStack::PushL(self);
- self->ConstructL();
- CleanupStack::Pop(self);
- return self;
- }
- // C++ Constructor
- CActiveBubbleSorter::CActiveBubbleSorter(MBubbleSortNotify& aNotifier)
- : CActive(CActive::EPriorityIdle),
- iNotifier(aNotifier)
- {
- CActiveScheduler::Add(this);
- }
- // C++ Destructor
- CActiveBubbleSorter::~CActiveBubbleSorter()
- {
- Cancel();
- iNumbersArray.Close();
- }
- // Symbian 2nd phase construction
- void CActiveBubbleSorter::ConstructL()
- {
- // Do nothing
- }
- // Handles the cleanup necessary if sorting is cancelled
- void CActiveBubbleSorter::DoCancel()
- {
- iNumbersArray.Reset();
- iNotifier.SortComplete(KErrCancel);
- }
- // Handles each step of the sorting procedure
- void CActiveBubbleSorter::RunL()
- {
- if (iNumbersArray[iY] > iNumbersArray[iY+1])
- {
- TInt temp = iNumbersArray[iY+1];
- iNumbersArray[iY+1] = iNumbersArray[iY];
- iNumbersArray[iY] = temp;
- }
-
- iY++;
- TInt count = iNumbersArray.Count();
-
- if (iY >= count-1)
- {
- iY = 0;
-
- if (iX < count)
- {
- iX++;
- }
- else
- {
- // Finished sorting
- WriteNumbersToFileL();
- iNumbersArray.Reset();
- iNotifier.SortComplete(iStatus.Int());
- return;
- }
- }
-
- TRequestStatus* status = &iStatus;
- User::RequestComplete(status, KErrNone);
- SetActive();
- }
- // Called to begin sorting
- void CActiveBubbleSorter::StartL()
- {
- if (IsActive())
- {
- User::Leave(KErrInUse);
- }
-
- iNumbersArray.Reset();
- ReadNumbersFromFileL();
-
- iX = 0;
- iY = 0;
-
- TRequestStatus* status = &iStatus;
- User::RequestComplete(status, KErrNone);
- SetActive();
- }
-
-
- // Reads numbers from an input file into an RArray
- void CActiveBubbleSorter::ReadNumbersFromFileL()
- {
- // Read contents of the input file into a descriptor
- RFs fs;
- User::LeaveIfError(fs.Connect());
- CleanupClosePushL(fs);
-
- RFile file;
- User::LeaveIfError(file.Open(fs, KSortDataInputFileName, EFileStreamText | EFileRead));
- CleanupClosePushL(fs);
-
- TInt sz = 0;
- User::LeaveIfError(file.Size(sz));
-
- HBufC8* buf = HBufC8::NewLC(sz);
- TPtr8 ptr = buf->Des();
- User::LeaveIfError(file.Read(ptr));
-
- // Extract numbers from the descriptor containing the contents of the input file
- TLex8 lx(ptr);
- TBool finshed = EFalse;
- while (!finshed)
- {
- if (lx.Eos())
- {
- finshed = ETrue;
- }
- else if ((lx.Peek()).IsDigit())
- {
- TInt num;
- TInt err = lx.Val(num);
- iNumbersArray.Append(num);
- }
- else
- {
- lx.Inc();
- }
- }
-
- CleanupStack::PopAndDestroy(3); // automatically closes fs and file
- }
- // Writes the numbers in the RArray to an output file
- void CActiveBubbleSorter::WriteNumbersToFileL()
- {
- RFs fs;
- User::LeaveIfError(fs.Connect());
- CleanupClosePushL(fs);
-
- TInt ret = fs.Delete(KSortDataOutputFileName);
- if (!(ret == KErrNone || ret == KErrNotFound))
- {
- User::Leave(ret);
- }
-
- RFile file;
- User::LeaveIfError(file.Create(fs, KSortDataOutputFileName, EFileStreamText | EFileWrite));
- CleanupClosePushL(fs);
-
- TBuf8<10> buf;
-
- for (TInt i = 0; i < iNumbersArray.Count(); i++)
- {
- buf.Format(KNumberOutputFormat, iNumbersArray[i]);
- file.Write(buf);
- }
-
- CleanupStack::PopAndDestroy(2); // automatically closes fs and file
- }
-
-
- // Called in the event the RunL function leaves
- TInt CActiveBubbleSorter::RunError(TInt aError)
- {
- iNumbersArray.Reset();
- iNotifier.SortComplete(aError);
-
- return KErrNone;
- }
-
- // End of file