📄 activebubblesorter.cpp
字号:
// Copyright (c) 2006 Nokia Corporation.
#include <AknAppUi.h>
#include <eikapp.h>
#include <e32math.h>
#include "ActiveBubbleSorter.h"
#include "BubbleSortNotify.h"
_LIT( KSortDataInputFileName, "sortdata_in.dat" );
_LIT( KSortDataOutputFileName, "sortdata_out.dat" );
_LIT8( KNumberOutputFormat, "%d\r\n" );
// 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();
}
void CActiveBubbleSorter::SyncSortL()
{
iNumbersArray.Reset();
ReadNumbersFromFileL();
iX = 0;
iY = 0;
iCount = iNumbersArray.Count();
SortL();
}
void CActiveBubbleSorter::SortL()
{
for ( iX = 0; iX < iCount; iX ++ )
{
for ( iY = iX + 1; iY < iCount; iY ++ )
{
if (iNumbersArray[ iX ] >= iNumbersArray[ iY ])
{
TInt temp = iNumbersArray[ iY ];
iNumbersArray[ iY ] = iNumbersArray[ iX ];
iNumbersArray[ iX ] = temp;
}
}
}
WriteNumbersToFileL();
iNumbersArray.Reset();
iNotifier.SortComplete( KErrNone );
}
// 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);
TFileName fileName;
TParse parse;
parse.SetNoWild( iAvkonAppUi->Application()->AppFullName(), NULL, NULL);
#if defined(__WINS__) || defined(__WINSCW__)
_LIT( KDriveLetter, "c:" );
fileName.Copy( KDriveLetter );
#else
fileName.Copy( parse.Drive() );
#endif
TFileName privatePath;
fs.PrivatePath( privatePath );
fileName.Append( privatePath );
fileName.Append( KSortDataInputFileName );
RFile file;
User::LeaveIfError(file.Open(fs, fileName, 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);
TFileName fileName;
TParse parse;
parse.SetNoWild( iAvkonAppUi->Application()->AppFullName(), NULL, NULL);
#if defined(__WINS__) || defined(__WINSCW__)
_LIT( KDriveLetter, "c:" );
fileName.Copy( KDriveLetter );
#else
fileName.Copy( parse.Drive() );
#endif
TFileName privatePath;
fs.PrivatePath( privatePath );
fileName.Append( privatePath );
fileName.Append( KSortDataOutputFileName );
TInt ret = fs.Delete( fileName );
if (!(ret == KErrNone || ret == KErrNotFound))
{
User::Leave(ret);
}
RFile file;
User::LeaveIfError(file.Create(fs, fileName, 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -