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

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: Contains definition of functions defined in the CMyStringReverse class. 
00028 */
00029 
00034 #include "mystringreverse.h"
00035 
00040 CMyStringReverse* CMyStringReverse::NewL(CConsoleBase* aConsole)
00041         {
00042         CMyStringReverse* self = new (ELeave)CMyStringReverse(aConsole);
00043         self->AddToScheduler();
00044         return self;
00045         }
00046 
00050 void CMyStringReverse::AddToScheduler()
00051         {
00052         CActiveScheduler::Add(this);
00053         }
00054 
00059 CMyStringReverse::CMyStringReverse(CConsoleBase* aConsole):
00060                                                                                                                 // Constructor of the base class.
00061                                                                                                                 CActive(CActive::EPriorityUserInput),
00062                                                                                                                 // Create the linked list.
00063                                                                                                                 iString(_FOFF(TLetter,iDLink)),
00064                                                                                                                 // Initialize iterators.
00065                                                                                                                 iIteratorString(iString),
00066                                                                                                                 iConsole(aConsole)
00067         {
00068         }
00069 
00079 void CMyStringReverse::RunL()
00080         {
00081         // Get the key code.
00082         TUint8 option = iConsole->KeyCode();
00083         // Print the selected option.
00084         _LIT(KTextFormat,"%c\n");
00085         iConsole->Printf(KTextFormat,option);
00086 
00087         // Check if a numeric key is pressed.
00088         TInt number = option - (TUint)'0';
00089         if(number <10 && number > -1)
00090                 {
00091                 // Handle the numeric key press.
00092                 switch(number)
00093                         {
00094                         case 1:
00095                                 {
00096                                 // The iString.AddFirst() function needs to be called.
00097                                 // Read the character to be added to the list.
00098                                 iTask = ETaskStringAddFirst;
00099                                 ReadChar();
00100                                 }
00101                                 break;
00102                         case 2:
00103                                 {
00104                                 // The iString.AddLast() function needs to be called.
00105                                 // Read the character to be added to the list.
00106                                 iTask = ETaskStringAddLast;
00107                                 ReadChar();
00108                                 }
00109                                 break;
00110                         default:
00111                                 // Stop the active scheduler.
00112                                 CActiveScheduler::Stop();
00113                         }
00114                 }
00115         else
00116                 {
00117                 // An alphabetic key is pressed.
00118                 // Check the action to be performed.
00119                 switch(iTask)
00120                         {
00121                         case ETaskNone:
00122                                 _LIT(KInvalidOption,"Invalid Option!\n");
00123                                 iConsole->Printf(KInvalidOption);
00124                                 break;
00125                         // The iString.AddFirst() function needs to be called.
00126                         case ETaskStringAddFirst:
00127                                 {
00128                                 // Create an object of the TLetter class.
00129                                 TLetter* letter = new (ELeave) TLetter(option);
00130                                 // Add the TLetter object into the list.
00131                                 iString.AddFirst(*letter);
00132                                 iTask = ETaskNone;
00133                                 // Print the data in iString and iReverseString.
00134                                 PrintStrings();
00135                                 }
00136                                 break;
00137                         case ETaskStringAddLast:
00138                                 {
00139                                 // Create an object of the TLetter class.
00140                                 TLetter* letter = new (ELeave) TLetter(option);
00141                                 // Add the TLetter object into the list.
00142                                 iString.AddLast(*letter);
00143                                 iTask = ETaskNone;
00144                                 // Print the data in iString and iReverseString.
00145                                 PrintStrings();         
00146                                 }
00147                                 break;
00148                         default:
00149                                 iTask = ETaskNone;
00150                                 break;
00151                         }
00152                 // Generate an asynchronous read request.
00153                 ReadOption();
00154                 }
00155         }
00156 
00160 void CMyStringReverse::ReadFunc()
00161         {
00162         // Wait for a key press event.
00163         iConsole->Read(iStatus);
00164         SetActive();
00165         }
00166 
00170 void CMyStringReverse::ReadOption()
00171         {
00172         // Print the menu.
00173         _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");
00174         iConsole->Printf(KTextMenu);
00175         // Generate an asynchronous read request.
00176         ReadFunc();
00177         }
00178 
00182 void CMyStringReverse::ReadChar()
00183         {
00184         _LIT(KTextReadChar,"\nEnter a character\n");
00185         iConsole->Printf(KTextReadChar);
00186         // Generate an asynchronous read request.
00187         ReadFunc();
00188         }
00189 
00193 void CMyStringReverse::PrintStrings()
00194         {
00195         _LIT(KTextSrcString,"Source String: ");
00196         iConsole->Printf(KTextSrcString);
00197         // Initialize the iterator.
00198         iIteratorString.SetToFirst();
00199 
00200         // Iterate iString.
00201         while(iIteratorString != NULL)
00202                 {
00203                 // Get the TLetter object pointed to by the iterator.
00204                 TLetter letter = *iIteratorString;
00205                 // Print the character value of the TLetter object.
00206                 _LIT(KTextChar,"%c");
00207                 iConsole->Printf(KTextChar,TUint(letter.iChar));
00208                 // Set the iterator to point to the next element.
00209                 iIteratorString++;
00210                 }
00211 
00212         _LIT(KTextRevString,"\nReverse of the String: ");
00213         iConsole->Printf(KTextRevString);
00214         // Initialize the iterator.
00215         iIteratorString.SetToLast();
00216 
00217         // Iterate iReverseString.
00218         while(iIteratorString != NULL)
00219                 {
00220                 // Get the TLetter object pointed to by the iterator.
00221                 TLetter letter = *iIteratorString;
00222                 // Print the character value of the TLetter object.
00223                 _LIT(KTextChar,"%c");
00224                 iConsole->Printf(KTextChar,TUint(letter.iChar));
00225                 // Set the iterator to point to the next element.
00226                 iIteratorString--;
00227                 }
00228         iConsole->Printf(KTextNewLine);
00229         }
00230 
00234 void CMyStringReverse::DoCancel()
00235         {
00236         if(IsActive())
00237                 {
00238                 // Cancel any outstanding read requests.
00239                 iConsole->ReadCancel();
00240                 }
00241         }
00242 
00248 CMyStringReverse::~CMyStringReverse()
00249         {
00250         // Delete the elements from the list.           
00251                 iIteratorString.SetToFirst();
00252                 // Iterate the list and delete all TLetter objects.
00253                 TLetter* ptr = iIteratorString++;
00254                 while (ptr != NULL)
00255                         {
00256                         delete ptr;
00257                         ptr = iIteratorString++;
00258                         }       
00259         }
00260 

Generated by  doxygen 1.6.2