examples/Base/ArraysAndLists/linkedlist/dbllist/src/mystringreverse.cpp

Go to the documentation of this file.
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 CMyStringReverse class.
00015 // 
00016 //
00017 
00022 #include "mystringreverse.h"
00023 
00028 CMyStringReverse* CMyStringReverse::NewL(CConsoleBase* aConsole)
00029         {
00030         CMyStringReverse* self = new (ELeave)CMyStringReverse(aConsole);
00031         self->AddToScheduler();
00032         return self;
00033         }
00034 
00038 void CMyStringReverse::AddToScheduler()
00039         {
00040         CActiveScheduler::Add(this);
00041         }
00042 
00047 CMyStringReverse::CMyStringReverse(CConsoleBase* aConsole):
00048                                                                                                                 // Constructor of the base class.
00049                                                                                                                 CActive(CActive::EPriorityUserInput),
00050                                                                                                                 // Create the linked list.
00051                                                                                                                 iString(_FOFF(TLetter,iDLink)),
00052                                                                                                                 // Initialize iterators.
00053                                                                                                                 iIteratorString(iString),
00054                                                                                                                 iConsole(aConsole)
00055         {
00056         }
00057 
00067 void CMyStringReverse::RunL()
00068         {
00069         // Get the key code.
00070         TUint8 option = iConsole->KeyCode();
00071         // Print the selected option.
00072         _LIT(KTextFormat,"%c\n");
00073         iConsole->Printf(KTextFormat,option);
00074 
00075         // Check if a numeric key is pressed.
00076         TInt number = option - (TUint)'0';
00077         if(number <10 && number > -1)
00078                 {
00079                 // Handle the numeric key press.
00080                 switch(number)
00081                         {
00082                         case 1:
00083                                 {
00084                                 // The iString.AddFirst() function needs to be called.
00085                                 // Read the character to be added to the list.
00086                                 iTask = ETaskStringAddFirst;
00087                                 ReadChar();
00088                                 }
00089                                 break;
00090                         case 2:
00091                                 {
00092                                 // The iString.AddLast() function needs to be called.
00093                                 // Read the character to be added to the list.
00094                                 iTask = ETaskStringAddLast;
00095                                 ReadChar();
00096                                 }
00097                                 break;
00098                         default:
00099                                 // Stop the active scheduler.
00100                                 CActiveScheduler::Stop();
00101                         }
00102                 }
00103         else
00104                 {
00105                 // An alphabetic key is pressed.
00106                 // Check the action to be performed.
00107                 switch(iTask)
00108                         {
00109                         case ETaskNone:
00110                                 _LIT(KInvalidOption,"Invalid Option!\n");
00111                                 iConsole->Printf(KInvalidOption);
00112                                 break;
00113                         // The iString.AddFirst() function needs to be called.
00114                         case ETaskStringAddFirst:
00115                                 {
00116                                 // Create an object of the TLetter class.
00117                                 TLetter* letter = new (ELeave) TLetter(option);
00118                                 // Add the TLetter object into the list.
00119                                 iString.AddFirst(*letter);
00120                                 iTask = ETaskNone;
00121                                 // Print the data in iString and iReverseString.
00122                                 PrintStrings();
00123                                 }
00124                                 break;
00125                         case ETaskStringAddLast:
00126                                 {
00127                                 // Create an object of the TLetter class.
00128                                 TLetter* letter = new (ELeave) TLetter(option);
00129                                 // Add the TLetter object into the list.
00130                                 iString.AddLast(*letter);
00131                                 iTask = ETaskNone;
00132                                 // Print the data in iString and iReverseString.
00133                                 PrintStrings();         
00134                                 }
00135                                 break;
00136                         default:
00137                                 iTask = ETaskNone;
00138                                 break;
00139                         }
00140                 // Generate an asynchronous read request.
00141                 ReadOption();
00142                 }
00143         }
00144 
00148 void CMyStringReverse::ReadFunc()
00149         {
00150         // Wait for a key press event.
00151         iConsole->Read(iStatus);
00152         SetActive();
00153         }
00154 
00158 void CMyStringReverse::ReadOption()
00159         {
00160         // Print the menu.
00161         _LIT(KTextMenu,"\nEnter\n1-> Add a character to the beginning of the string\n2-> Add a character to the end of the string\nAny other NUMBER to stop\n");
00162         iConsole->Printf(KTextMenu);
00163         // Generate an asynchronous read request.
00164         ReadFunc();
00165         }
00166 
00170 void CMyStringReverse::ReadChar()
00171         {
00172         _LIT(KTextReadChar,"\nEnter a character\n");
00173         iConsole->Printf(KTextReadChar);
00174         // Generate an asynchronous read request.
00175         ReadFunc();
00176         }
00177 
00181 void CMyStringReverse::PrintStrings()
00182         {
00183         _LIT(KTextSrcString,"Source String: ");
00184         iConsole->Printf(KTextSrcString);
00185         // Initialize the iterator.
00186         iIteratorString.SetToFirst();
00187 
00188         // Iterate iString.
00189         while(iIteratorString != NULL)
00190                 {
00191                 // Get the TLetter object pointed to by the iterator.
00192                 TLetter letter = *iIteratorString;
00193                 // Print the character value of the TLetter object.
00194                 _LIT(KTextChar,"%c");
00195                 iConsole->Printf(KTextChar,TUint(letter.iChar));
00196                 // Set the iterator to point to the next element.
00197                 iIteratorString++;
00198                 }
00199 
00200         _LIT(KTextRevString,"\nReverse of the String: ");
00201         iConsole->Printf(KTextRevString);
00202         // Initialize the iterator.
00203         iIteratorString.SetToLast();
00204 
00205         // Iterate iReverseString.
00206         while(iIteratorString != NULL)
00207                 {
00208                 // Get the TLetter object pointed to by the iterator.
00209                 TLetter letter = *iIteratorString;
00210                 // Print the character value of the TLetter object.
00211                 _LIT(KTextChar,"%c");
00212                 iConsole->Printf(KTextChar,TUint(letter.iChar));
00213                 // Set the iterator to point to the next element.
00214                 iIteratorString--;
00215                 }
00216         iConsole->Printf(KTextNewLine);
00217         }
00218 
00222 void CMyStringReverse::DoCancel()
00223         {
00224         if(IsActive())
00225                 {
00226                 // Cancel any outstanding read requests.
00227                 iConsole->ReadCancel();
00228                 }
00229         }
00230 
00236 CMyStringReverse::~CMyStringReverse()
00237         {
00238         // Delete the elements from the list.           
00239                 iIteratorString.SetToFirst();
00240                 // Iterate the list and delete all TLetter objects.
00241                 TLetter* ptr = iIteratorString++;
00242                 while (ptr != NULL)
00243                         {
00244                         delete ptr;
00245                         ptr = iIteratorString++;
00246                         }       
00247         }
00248 

Generated by  doxygen 1.6.2