00001 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). 00002 // All rights reserved. 00003 // This component and the accompanying materials are made available 00004 // under the terms of "Eclipse Public License v1.0" 00005 // which accompanies this distribution, and is available 00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html". 00007 // 00008 // Initial Contributors: 00009 // Nokia Corporation - initial contribution. 00010 // 00011 // Contributors: 00012 // 00013 // Description: 00014 // Contains definition of functions defined in the CLongNumber class. 00015 // 00016 00017 00018 00023 #include "longnumber.h" 00024 00030 CLongNumber* CLongNumber::NewL(CConsoleBase* aConsole) 00031 { 00032 CLongNumber* self = new (ELeave) CLongNumber(aConsole); 00033 self->AddToScheduler(); 00034 return self; 00035 } 00036 00041 CLongNumber::CLongNumber(CConsoleBase* aConsole): 00042 // Constructor of the base class. 00043 CActive(CActive::EPriorityUserInput), 00044 // Create the linked list. 00045 iNumber(_FOFF(TDigit,iSLink)), 00046 iConsole(aConsole), 00047 // Initialize the iterator. 00048 iIterator(iNumber) 00049 { 00050 } 00051 00052 00056 void CLongNumber::AddToScheduler() 00057 { 00058 CActiveScheduler::Add(this); 00059 } 00060 00064 void CLongNumber::RunL() 00065 { 00066 // Get the key code. 00067 TUint8 option = iConsole->KeyCode(); 00068 // Print the selected option. 00069 _LIT(KTextFormat,"%c\n"); 00070 iConsole->Printf(KTextFormat,option); 00071 00072 TInt number = option - (TUint)'0'; 00073 // The user has entered a number. 00074 if(number <10 && number > -1) 00075 { 00076 // Append to the list based on iOption. 00077 switch(iOption) 00078 { 00079 // Call the TSglQue::AddFirst() function. 00080 case EAddFirst: 00081 { 00082 // Create an object of the TDigit class. 00083 TDigit* digit = new (ELeave) TDigit; 00084 // Set TDigit::iDigit to the number entered by the user. 00085 digit->Set(number); 00086 // Add TDigit to the start of the list. 00087 iNumber.AddFirst(*digit); 00088 iOption = ETOptionNone; 00089 } 00090 // Print the number. 00091 PrintNumber(); 00092 break; 00093 // Call the TSglQue::AddLast() function. 00094 case EAddLast: 00095 { 00096 // Create an object of the TDigit class. 00097 TDigit* digit = new (ELeave) TDigit; 00098 // Set TDigit::iDigit to the number entered by the user. 00099 digit->Set(number); 00100 // Add TDigit to the end of the list. 00101 iNumber.AddLast(*digit); 00102 iOption = ETOptionNone; 00103 } 00104 // Print the number. 00105 PrintNumber(); 00106 break; 00107 case ETOptionNone: 00108 _LIT(KInvalidOption,"Invalid Option!\n"); 00109 iConsole->Printf(KInvalidOption); 00110 break; 00111 default: 00112 iOption = ETOptionNone; 00113 break; 00114 } 00115 // Generate an asynchronous read request. 00116 ReadNumber(); 00117 } 00118 // The user has not entered a number. 00119 // Hence, process this request as an option, 00120 // which represented by the TOption enumeration. 00121 else 00122 { 00123 switch(option) 00124 { 00125 case 'f': 00126 { 00127 // Add first. 00128 iOption = EAddFirst; 00129 _LIT(KTextEnterDigit,"\nEnter a digit\n"); 00130 iConsole->Printf(KTextEnterDigit); 00131 // Read a digit. 00132 ReadFunc(); 00133 } 00134 break; 00135 case 'l': 00136 { 00137 // Add last. 00138 iOption = EAddLast; 00139 _LIT(KTextEnterDigit,"\nEnter a digit\n"); 00140 iConsole->Printf(KTextEnterDigit); 00141 // Read a digit. 00142 ReadFunc(); 00143 } 00144 break; 00145 case 'r': 00146 { 00147 if(iNumber.IsEmpty()) 00148 { 00149 _LIT(KTextEmpty,"Empty Queue !\n"); 00150 iConsole->Printf(KTextEmpty); 00151 ReadNumber(); 00152 break; 00153 } 00154 // Remove the last element in the list. 00155 TDigit* ptr = iNumber.Last(); 00156 iNumber.Remove(*ptr); 00157 delete ptr; 00158 // Print the number. 00159 PrintNumber(); 00160 // Read a digit. 00161 ReadNumber(); 00162 } 00163 break; 00164 default: 00165 CActiveScheduler::Stop(); 00166 } 00167 } 00168 } 00169 00173 void CLongNumber::ReadFunc() 00174 { 00175 // Wait for a key press event. 00176 iConsole->Read(iStatus); 00177 SetActive(); 00178 } 00179 00183 void CLongNumber::ReadNumber() 00184 { 00185 _LIT(KTextMenu,"\nMenu\n***\nAdd [f]irst digit to number\nAdd [l]ast digit to number\n[r]emove last digit from number\nAny other NON-NUMERIC key to stop\n***\n"); 00186 iConsole->Printf(KTextMenu); 00187 ReadFunc(); 00188 } 00189 00193 void CLongNumber::PrintNumber() 00194 { 00195 if(iNumber.IsEmpty()) 00196 { 00197 _LIT(KTextEmpty,"Empty Queue !\n"); 00198 iConsole->Printf(KTextEmpty); 00199 } 00200 _LIT(KTextNumber,"The number is: "); 00201 iConsole->Printf(KTextNumber); 00202 // Set the iterator to the first element of the list. 00203 iIterator.SetToFirst(); 00204 00205 // Iterate the list. 00206 while(iIterator != NULL) 00207 { 00208 // Get the digit. 00209 TDigit digit = *iIterator; 00210 // Print the digit. 00211 _LIT(KTextInt,"%d"); 00212 iConsole->Printf(KTextInt,digit.Get()); 00213 // Go to the next element of the list. 00214 iIterator++; 00215 } 00216 } 00217 00221 void CLongNumber::InitializeIter() 00222 { 00223 iIterator.SetToFirst(); 00224 } 00225 00232 TBool CLongNumber::GetNumber(TInt& aValue) 00233 { 00234 if(iIterator == NULL) 00235 { 00236 // The iterator has reached the end of the list. 00237 return EFalse; 00238 } 00239 TDigit digit = *iIterator; 00240 aValue = digit.Get(); 00241 return ETrue; 00242 } 00243 00248 TBool CLongNumber::Slide() 00249 { 00250 // Set the iterator to point to the next element. 00251 iIterator++; 00252 if(iIterator == NULL) 00253 { 00254 // The iterator has reached the end of the list. 00255 return EFalse; 00256 } 00257 return ETrue; 00258 } 00259 00263 void CLongNumber::DoCancel() 00264 { 00265 if(IsActive()) 00266 { 00267 // Cancel any outstanding read requests. 00268 iConsole->ReadCancel(); 00269 } 00270 } 00271 00277 CLongNumber::~CLongNumber() 00278 { 00279 // Delete the elements from the list 00280 if(iIterator) 00281 { 00282 iIterator.SetToFirst(); 00283 // Iterate the list and delete all TDigit objects 00284 TDigit *ptr = iIterator++; 00285 while (ptr != NULL) 00286 { 00287 delete ptr; 00288 ptr = iIterator++; 00289 } 00290 } 00291 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.