examples/Base/tcharexample/tcharexample.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 // This example program demonstrates the TChar, TCharF, TCharLC and TCharUC character classes.
00015 //
00016 
00017 
00018 
00023 #include "tcharexample.h"
00024 
00025 static CConsoleBase* console;
00026 
00030 CTCharExample::CTCharExample()
00031         {
00032         }
00033 
00034 void CTCharExample::ConstructL()
00035         {
00036         _LIT(KTitle, "\r\nTChar Example" );
00037         console = Console::NewL(KTitle, TSize(KConsFullScreen, KConsFullScreen));
00038         
00039         _LIT(KWelcome, "\r\nWelcome to the TChar example application");
00040         console->Printf(KWelcome);
00041         }
00042 
00046 CTCharExample::~CTCharExample()
00047         {
00048         delete console;
00049         }
00050 
00055 CTCharExample* CTCharExample::NewL()
00056         {
00057         CTCharExample* self = new(ELeave)CTCharExample();
00058         CleanupStack::PushL(self);
00059         self->ConstructL();
00060         CleanupStack::Pop(self);
00061         return self;
00062         }
00063 
00068 void PrintIsXXXFunction(TChar aCharValue)
00069         {
00070         if(aCharValue.IsLower())
00071                 {
00072                 _LIT(KLower," The character is lowercase\r\n");
00073                 console->Printf(KLower);
00074                 }
00075         if(aCharValue.IsUpper())
00076                 {
00077                 _LIT(KUpper," The character is uppercase\r\n");
00078                 console->Printf(KUpper);
00079                 }
00080         if(aCharValue.IsAlpha())
00081                 {
00082                 _LIT(KAlpha," The character is alphabetic\r\n"); 
00083                 console->Printf(KAlpha);
00084                 }
00085         if(aCharValue.IsDigit())
00086                 {
00087                 _LIT(KDigit," The character is a digit\r\n");
00088                 console->Printf(KDigit);
00089                 }
00090         if(aCharValue.IsHexDigit())
00091                 {
00092                 _LIT(KHexDigit," The character is a hexadecimal digit\r\n");
00093                 console->Printf(KHexDigit);
00094                 }
00095         if(aCharValue.IsSpace())
00096                 {
00097                 _LIT(KSpace," The character is a space\r\n");
00098                 console->Printf(KSpace);
00099                 }
00100         if(aCharValue.IsPunctuation())
00101                 {
00102                 _LIT(KPunctuation," The character is punctuation\r\n");
00103                 console->Printf(KPunctuation);
00104                 }
00105         else if(aCharValue.IsPrint())
00106                 {
00107                 _LIT(KPrint," The character is printable\r\n");
00108                 console->Printf(KPrint);
00109                 }
00110         }
00111     
00115 void TCharIsXXXFunctions()
00116         {
00117         _LIT(KEnterAKey,"\r\nEnter a character to process with the TChar::IsXXX() functions: ");
00118         console->Printf(KEnterAKey);
00119         
00120         TChar charValue = console->Getch();
00121         _LIT(KPrintChar,"%C\r\n");
00122         console->Printf(KPrintChar, (TUint)charValue);
00123         
00124         // Get and print the character type using the IsXXX() functions.
00125         PrintIsXXXFunction(charValue);
00126         }
00127         
00131 void TCharGetXXXFunctions()
00132         {
00133         _LIT(KEnterAKey,"\r\nEnter a character to process with the TChar:GetXXX() functions: ");
00134         console->Printf(KEnterAKey);
00135         _LIT(KPrintChar,"%C\r\n");
00136         TChar charValue = console->Getch();
00137         
00138         console->Printf(KPrintChar, (TUint)charValue);
00139 
00140         // Gets the combining class of the character.
00141         TInt getCombiningClass = charValue.GetCombiningClass();
00142         _LIT(KCharCombNotClassMsg," The character does not have a combining class\r\n");
00143         _LIT(KCharCombClassMsg," The character has %d combining classes\r\n");
00144         if(getCombiningClass == 0)
00145                 console->Printf(KCharCombNotClassMsg);
00146         else
00147                 console->Printf(KCharCombClassMsg, getCombiningClass);
00148         
00149         _LIT(KENeutalWidthMsg," ENeutralWidth: This character type includes 'ambiguous width' as defined in Unicode Technical Report 11: East Asian Width\r\n");
00150         _LIT(KEHaldWidthMsg," EHalfWidth: This character occupies a single cell\r\n");
00151         _LIT(KEFullWidthMsg," EFullWidth: This character occupies 2 cells\r\n");
00152         _LIT(KENarrowMsg," ENarrow: This character is narrow and has explicit full-width counterparts\r\n");
00153         _LIT(KEWideMsg," EWide: This character is wide\r\n");
00154         _LIT(KNoWidthMsg," This character has no width defined\r\n");
00155         // Gets and prints the Chinese, Japanese, Korean (CJK) notional width of the character.
00156         TChar::TCjkWidth cjkWidth = charValue.GetCjkWidth();
00157         switch(cjkWidth)
00158                 {
00159                 case TChar::ENeutralWidth:
00160                         console->Printf(KENeutalWidthMsg);
00161                         break;
00162                 case TChar::EHalfWidth:
00163                         console->Printf(KEHaldWidthMsg);
00164                         break;
00165                 case TChar::EFullWidth:
00166                         console->Printf(KEFullWidthMsg);
00167                         break;  
00168                 case TChar::ENarrow:
00169                         console->Printf(KENarrowMsg);
00170                         break;
00171                 case TChar::EWide:
00172                         console->Printf(KEWideMsg);
00173                         break;
00174                 default:
00175                         console->Printf(KNoWidthMsg);
00176                 }
00177 
00178         // Prints the different cases if the character is alphabetic.
00179         if(charValue.IsAlpha())
00180                 {
00181                 // Gets the character after conversion to uppercase (or returns the character itself if no uppercase form exists).
00182                 TUint getCaseValue = charValue.GetUpperCase();
00183                 _LIT(KAferConv2UpMsg," The character after conversion to upper case is %C:\r\n");
00184                 console->Printf(KAferConv2UpMsg, getCaseValue);
00185         
00186                 // Gets the character after conversion to lowercase (or the character itself if no lowercase form exists).
00187                 getCaseValue = charValue.GetLowerCase();
00188                 _LIT(KAferConv2LowerMsg," The character after conversion to lower case is %C:\r\n"); 
00189                 console->Printf(KAferConv2LowerMsg, getCaseValue);
00190 
00191                 // Gets the character after conversion to title-case (or the character itself if no title-case form exists).
00192                 getCaseValue = charValue.GetTitleCase();
00193                 _LIT(KAferConv2TitleMsg," The character after conversion to title case is %C:\r\n");
00194                 console->Printf(KAferConv2TitleMsg, getCaseValue);
00195                 }
00196         }
00197         
00198 
00202 void TCharInfoFunctions()
00203         {
00204         _LIT(KEnterAKey,"\r\nTCharInfo holds all character information\r\nEnter a character to process with TCharInfo: ");
00205         console->Printf(KEnterAKey);
00206         
00207         TChar enteredCharacter = console->Getch();
00208         _LIT(KPrintChar,"%C\r\n");
00209         console->Printf(KPrintChar, (TUint)enteredCharacter);
00210         
00211         // A structure to hold information about the character.
00212         TChar::TCharInfo charInfo;
00213         
00214         enteredCharacter.GetInfo(charInfo);
00215         
00216         // Gets the combining class of the entered character.
00217         // This determines the relationship between characters in a string
00218         TInt numberOfCombiningClass = charInfo.iCombiningClass;
00219         _LIT(KCharCombNotClassMsg," The character does not have a combining class\r\n");
00220         _LIT(KCharCombClassMsg," The character has %d combining classes\r\n");
00221         if(numberOfCombiningClass <= 0)
00222                 console->Printf(KCharCombNotClassMsg);
00223         else
00224                 console->Printf(KCharCombClassMsg, numberOfCombiningClass);
00225 
00226         // Returns True if the character is mirrored.
00227         // Mirrored characters can change direction according to the directionality of the surrounding characters
00228         TBool booleanValue = charInfo.iMirrored;
00229         _LIT(KCharMirroredMsg," The character is mirrored\r\n");
00230         _LIT(KCharNotMirroredMsg," The character is not mirrored\r\n");
00231         if(booleanValue)        
00232                 console->Printf(KCharMirroredMsg);
00233         else
00234                 console->Printf(KCharNotMirroredMsg);
00235         
00236         // Gets the numeric value of the character (-1 if none, -2 if a fraction).
00237         TInt getNumericValue = charInfo.iNumericValue;
00238         _LIT(KCharNoNumericMsg," The character has no numeric value\r\n");
00239         _LIT(KCharNumericFractionMsg, " The character is a fraction\r\n");
00240         if(getNumericValue == -1)       
00241                 console->Printf(KCharNoNumericMsg);
00242         else if(getNumericValue == -2)  
00243                 console->Printf(KCharNumericFractionMsg);
00244         
00245         // Print the different cases if the character is alphabetic.
00246         if(enteredCharacter.IsAlpha())
00247                 {
00248                 // Gets and prints the title-case form of the character.
00249                 TUint caseValue = charInfo.iTitleCase;
00250                 _LIT(KAferConv2TitleMsg," The character after conversion to title case is %C\r\n");
00251                 console->Printf(KAferConv2TitleMsg, caseValue);
00252         
00253                 // Gets and prints the upper case form of the character.
00254                 caseValue = charInfo.iUpperCase;
00255                 _LIT(KAferConv2UpMsg," The character after conversion to upper case is %C\r\n");
00256                 console->Printf(KAferConv2UpMsg, caseValue);    
00257         
00258                 // Gets and prints the lower case form of the character.
00259                 caseValue = charInfo.iLowerCase;
00260                 _LIT(KAferConv2LowerMsg," The character after conversion to lower case is %C\r\n"); 
00261                 console->Printf(KAferConv2LowerMsg, caseValue); 
00262                 } 
00263         }
00264 
00265 
00269 void CTCharExample::UseTCharFunctions()
00270         {
00271         //Split the TChar functions into logical groups. 
00272         TInt userChoice = 0;
00273                 do
00274                 {
00275                 _LIT(KLoopMessage, "\r\nTChar function menu.\r\n 1 to use the IsXXX() functions,\r\n 2 to use the Getxxx() functions,\r\n 3 to use the TCharInfo class,\r\n or 0 to return to the main menu.\r\n");
00276                 console->Printf(KLoopMessage);
00277                 
00278                 userChoice = console->Getch();
00279                 
00280                 switch(userChoice) 
00281                         {
00282                         case '1':
00283                                 // Use the TChar::IsXXX() functions
00284                                 TCharIsXXXFunctions();
00285                                 break;  
00286         
00287                         case '2':
00288                                 // Use the TChar::GetXXX(); functions
00289                                 TCharGetXXXFunctions();
00290                                 break;
00291                 
00292                         case '3':
00293                                 // Use TCharInfo functions
00294                                 TCharInfoFunctions();   
00295                                 break;
00296                                         
00297                         case '0':
00298                                 // Exit
00299                                 break;
00300                                 
00301                         default:
00302                                 {
00303                                 _LIT(KEnterValidMsg, "\r\nEnter a valid character.\r\n");
00304                                 console->Printf(KEnterValidMsg);
00305                                 }
00306                         
00307                         }
00308                         
00309                 } while (userChoice != '0');
00310         }
00311 
00315 void CTCharExample::UseTCharFFunctions()
00316         {       
00317         _LIT(KEnterALowerCaseChar,"\r\nTCharF ignores character differences like case and accent.\r\n Enter a character: ");
00318         console->Printf(KEnterALowerCaseChar);
00319 
00320         // Gets a character from the console.
00321         TChar enteredLowerChar = console->Getch();
00322         _LIT(KPrintChar,"%C\r\n");
00323         console->Printf(KPrintChar, (TUint)enteredLowerChar);
00324         
00325         _LIT(KEnterAnUpperCaseChar," Enter the same character but with a different case or accent.\r\n (use the 'Alt Gr' key to produce an accent): ");
00326         console->Printf(KEnterAnUpperCaseChar);
00327 
00328         // Gets a character from the console.
00329         TChar enteredUpperChar = console->Getch();
00330         console->Printf(KPrintChar, (TUint)enteredUpperChar);
00331         
00332         // TCharF folds a specified character so that character differences like case and accents are ignored.
00333         TCharF lowerCaseValue(enteredLowerChar);
00334         TCharF upperCaseValue(enteredUpperChar);
00335         
00336         // Test both lower and upper case value using TCharF.
00337         if (lowerCaseValue == upperCaseValue)
00338                 {
00339                 _LIT(KCharEquiMsg," The characters are equivalent\r\n");
00340                 console->Printf(KCharEquiMsg);
00341                 }
00342         else
00343                 {
00344                 _LIT(KCharNotEquiMsg, " The characters are not equivalent\r\n");
00345                 console->Printf(KCharNotEquiMsg);
00346                 }               
00347         }
00348 
00352 void CTCharExample::UseTCharLCFunctions()
00353         {
00354         
00355         _LIT(KEnterACharForTCharLC,"\r\nTCharLC converts a character to lower case\r\n Enter an upper case character to process with TCharLC: ");
00356         console->Printf(KEnterACharForTCharLC);
00357 
00358         // Gets a character from the console.
00359         TChar charValue = console->Getch();
00360         _LIT(KPrintChar,"%C\r\n");
00361         console->Printf(KPrintChar, (TUint)charValue);
00362         
00363         // Convert and print the character.
00364         TCharLC convertedCharToLowerCase(charValue);
00365                 
00366         _LIT(KConvertedTCharLC, "\r\n After conversion the character is: ");
00367         console->Printf(KConvertedTCharLC);
00368         console->Printf(KPrintChar,(TUint)convertedCharToLowerCase);
00369         }
00370         
00374 void CTCharExample::UseTCharUCFunctions()
00375         {       
00376         _LIT(KEnterACharForTCharUC,"\r\nTCharUC converts a character to upper case\r\n Enter a lower case character to process with TCharLC: ");
00377         console->Printf(KEnterACharForTCharUC);
00378         _LIT(KPrintChar,"%C\r\n");
00379         // Gets a character from the console.
00380         TChar charValue = console->Getch();
00381         console->Printf(KPrintChar, (TUint)charValue);
00382         
00383         // Convert and print the character.
00384         TCharUC convertedCharUC(charValue);
00385                 
00386         _LIT(KConvertedTCharUC, "\r\n After conversion the character is: ");
00387         console->Printf(KConvertedTCharUC);
00388         console->Printf(KPrintChar,(TUint)convertedCharUC);
00389         }       
00390         
00391 
00392 void MainL()
00393         {
00394         CTCharExample* app = CTCharExample::NewL();
00395         CleanupStack::PushL(app);
00396 
00397         TInt userChoice = 0;
00398 
00399         do
00400                 {
00401                 _LIT(KLoopMessage, "\r\nPress 1 to use the TChar functions,\r\n 2 to use the TCharF function,\r\n 3 to use the TCharLC function,\r\n 4 to use the TCharUC function,\r\n or 0 to exit the application.\r\n");
00402                 console->Printf(KLoopMessage);
00403                 
00404                 userChoice = console->Getch();
00405                 
00406                 switch(userChoice) 
00407                         {
00408                         case '1':
00409                                 // Use functions belonging to the TChar character class.
00410                                 app->UseTCharFunctions();
00411                                 break;  
00412         
00413                         case '2':
00414                                 // Use functions belonging to the TCharF character class.
00415                                 app->UseTCharFFunctions();
00416                                 break;
00417                 
00418                         case '3':
00419                                 // Use functions belonging to the TCharLC character class.
00420                                 app->UseTCharLCFunctions();     
00421                                 break;
00422         
00423                         case '4':
00424                                 // Use functions belonging to the TCharUC character class.
00425                                 app->UseTCharUCFunctions();
00426                                 break;
00427                         
00428                         case '0':
00429                                 // Exit
00430                                 break;
00431                         
00432                         default:
00433                                 _LIT(KEnterValidMsg, "\r\nEnter a valid character.\r\n");                       
00434                                 console->Printf(KEnterValidMsg);
00435                         }
00436                         
00437                 } while (userChoice != '0');
00438                 
00439                 CleanupStack::PopAndDestroy(app);
00440         } 
00441 
00442 extern TInt E32Main()
00443         {
00444         __UHEAP_MARK;
00445 
00446         CTrapCleanup* cleanup = CTrapCleanup::New();
00447         if(cleanup == NULL)
00448                 {
00449                 return KErrNoMemory;
00450                 }
00451         TRAPD(err, MainL());
00452         if(err !=KErrNone)
00453                 {
00454                 _LIT(KFailed, "\nFailed to complete");
00455                 User::Panic(KFailed, err);
00456                 }       
00457         delete cleanup;
00458 
00459         __UHEAP_MARKEND;
00460         return KErrNone;
00461         }
00462   
00463   
00464   
00465   
00466  
00467  
00468  

Generated by  doxygen 1.6.2