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

Generated by  doxygen 1.6.2