00001
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
00013 CActiveBubbleSorter* CActiveBubbleSorter::NewL(MBubbleSortNotify& aNotifier)
00014 {
00015 CActiveBubbleSorter* self = new (ELeave) CActiveBubbleSorter(aNotifier);
00016 CleanupStack::PushL(self);
00017 self->ConstructL();
00018 CleanupStack::Pop(self);
00019 return self;
00020 }
00021
00022
00023
00024 CActiveBubbleSorter::CActiveBubbleSorter(MBubbleSortNotify& aNotifier)
00025 : CActive(CActive::EPriorityIdle),
00026 iNotifier(aNotifier)
00027 {
00028 CActiveScheduler::Add(this);
00029 }
00030
00031
00032
00033 CActiveBubbleSorter::~CActiveBubbleSorter()
00034 {
00035 Cancel();
00036 iNumbersArray.Close();
00037 }
00038
00039
00040
00041 void CActiveBubbleSorter::ConstructL()
00042 {
00043
00044 }
00045
00046
00047
00048 void CActiveBubbleSorter::DoCancel()
00049 {
00050 iNumbersArray.Reset();
00051 iNotifier.SortComplete(KErrCancel);
00052 }
00053
00054
00055
00056 void CActiveBubbleSorter::RunL()
00057 {
00058 if (iNumbersArray[iY] > iNumbersArray[iY+1])
00059 {
00060 TInt temp = iNumbersArray[iY+1];
00061 iNumbersArray[iY+1] = iNumbersArray[iY];
00062 iNumbersArray[iY] = temp;
00063 }
00064
00065 iY++;
00066 TInt count = iNumbersArray.Count();
00067
00068 if (iY >= count-1)
00069 {
00070 iY = 0;
00071
00072 if (iX < count)
00073 {
00074 iX++;
00075 }
00076 else
00077 {
00078
00079 WriteNumbersToFileL();
00080 iNumbersArray.Reset();
00081 iNotifier.SortComplete(iStatus.Int());
00082 return;
00083 }
00084 }
00085
00086 TRequestStatus* status = &iStatus;
00087 User::RequestComplete(status, KErrNone);
00088 SetActive();
00089 }
00090
00091
00092
00093 void CActiveBubbleSorter::StartL()
00094 {
00095 if (IsActive())
00096 {
00097 User::Leave(KErrInUse);
00098 }
00099
00100 iNumbersArray.Reset();
00101 ReadNumbersFromFileL();
00102
00103 iX = 0;
00104 iY = 0;
00105
00106 TRequestStatus* status = &iStatus;
00107 User::RequestComplete(status, KErrNone);
00108 SetActive();
00109 }
00110
00111
00112
00113 void CActiveBubbleSorter::ReadNumbersFromFileL()
00114 {
00115
00116 RFs fs;
00117 User::LeaveIfError(fs.Connect());
00118 CleanupClosePushL(fs);
00119
00120 RFile file;
00121 User::LeaveIfError(file.Open(fs, KSortDataInputFileName, EFileStreamText | EFileRead));
00122 CleanupClosePushL(fs);
00123
00124 TInt sz = 0;
00125 User::LeaveIfError(file.Size(sz));
00126
00127 HBufC8* buf = HBufC8::NewLC(sz);
00128 TPtr8 ptr = buf->Des();
00129 User::LeaveIfError(file.Read(ptr));
00130
00131
00132 TLex8 lx(ptr);
00133 TBool finshed = EFalse;
00134 while (!finshed)
00135 {
00136 if (lx.Eos())
00137 {
00138 finshed = ETrue;
00139 }
00140 else if ((lx.Peek()).IsDigit())
00141 {
00142 TInt num;
00143 TInt err = lx.Val(num);
00144 iNumbersArray.Append(num);
00145 }
00146 else
00147 {
00148 lx.Inc();
00149 }
00150 }
00151
00152 CleanupStack::PopAndDestroy(3);
00153 }
00154
00155
00156
00157 void CActiveBubbleSorter::WriteNumbersToFileL()
00158 {
00159 RFs fs;
00160 User::LeaveIfError(fs.Connect());
00161 CleanupClosePushL(fs);
00162
00163 TInt ret = fs.Delete(KSortDataOutputFileName);
00164 if (!(ret == KErrNone || ret == KErrNotFound))
00165 {
00166 User::Leave(ret);
00167 }
00168
00169 RFile file;
00170 User::LeaveIfError(file.Create(fs, KSortDataOutputFileName, EFileStreamText | EFileWrite));
00171 CleanupClosePushL(fs);
00172
00173 TBuf8<10> buf;
00174
00175 for (TInt i = 0; i < iNumbersArray.Count(); i++)
00176 {
00177 buf.Format(KNumberOutputFormat, iNumbersArray[i]);
00178 file.Write(buf);
00179 }
00180
00181 CleanupStack::PopAndDestroy(2);
00182 }
00183
00184
00185
00186 TInt CActiveBubbleSorter::RunError(TInt aError)
00187 {
00188 iNumbersArray.Reset();
00189 iNotifier.SortComplete(aError);
00190
00191 return KErrNone;
00192 }
00193
00194