examples/Base/BufsAndStrings/Desc/NonModifier/NonModifier.cpp

00001 // Copyright (c) 2000-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 // Examples to demonstrate some of the non-modifying member
00015 // functions of descriptors.
00016 //
00017 
00018 #include "CommonFramework.h"
00019 
00020 //
00021 // Common literal text
00022 //
00023 _LIT(KTxtPressToContinue," (press any key to continue)\n");
00024 _LIT(KTxtnotfound,"NOT FOUND");
00025 _LIT(KTxtfound,"found");
00026 _LIT(KTxtlessthan,"  is less than     ");
00027 _LIT(KTxtgreaterthan,"  is greater than  ");
00028 _LIT(KTxtequalto,"  is equal to      ");
00029 
00030 //
00031 // Common Format strings
00032 //
00033 _LIT(KCommonFormat2,"Length()=%d; Size()=%d\n");
00034 _LIT(KCommonFormat8,"\"%S\"  Char %c is at pos %d (%S)\n");
00035 _LIT(KCommonFormat9,"%- 8S   pos %2d  (%S)\n");
00036 //
00037 // Compare strings
00038 //
00039 _LIT(KTxtCompstr1,"Hello World!@@");
00040 _LIT(KTxtCompstr2,"Hello");
00041 _LIT(KTxtCompstr3,"Hello Worl");
00042 _LIT(KTxtCompstr4,"Hello World!");
00043 _LIT(KTxtCompstr5,"hello world!");
00044 _LIT(KTxtCompstr6,"Hello World ");
00045 _LIT(KTxtCompstr7,"Hello World@");
00046 
00047 //
00048 // Match strings
00049 //
00050 _LIT(KTxtMatchstr1,"*World*");
00051 _LIT(KTxtMatchstr2,"*W?rld*");
00052 _LIT(KTxtMatchstr3,"Wor*");
00053 _LIT(KTxtMatchstr4,"Hello");
00054 _LIT(KTxtMatchstr5,"*W*");
00055 _LIT(KTxtMatchstr6,"hello*");
00056 _LIT(KTxtMatchstr7,"*");
00057 
00058 
00059 // Do the example
00060 LOCAL_C void doExampleL()
00061     {
00062     TInt            index;
00063         TInt            pos;
00064         TPtrC           genptr;
00065                                                                                                                         
00066                                 // Use a TBufC to demonstrate some of these
00067                                 // and use the standard "Hello World!" text
00068         _LIT(KTxtHelloWorld,"Hello World!");
00069         const TBufC<16> bufc(KTxtHelloWorld);
00070         
00071                         //                              
00072                         // Right() & Mid()  * * * * * * * * * * * *  
00073                 //      
00074         _LIT(KTxtRightMid,"\n--->Right() & Mid()\n");
00075         console->Printf(KTxtRightMid);
00076 
00077                                 // Look at the content of bufc
00078         _LIT(KFormat1,"      TBufC: \"%S\"; Ptr()=%x; ");
00079         console->Printf(KFormat1,&bufc,bufc.Ptr());
00080 
00081         console->Printf(KCommonFormat2,bufc.Length(),bufc.Size());
00082 
00083                                 // Construct a TPtrC to represent the right
00084                                 // hand 5 data items. The function Left()
00085                                 // is similar.
00086         TPtrC ptrc1 = bufc.Right(5);
00087 
00088                                 // ptrc's data is "orld!"
00089                                 // Length of ptrc is 5
00090                                 // ptrc's data area address = bufc's data area
00091                                 // address + 7
00092         _LIT(KFormat3,"Right TPtrC: \"%S\"; Ptr()=%x; ");
00093         console->Printf(KFormat3,&ptrc1,ptrc1.Ptr());
00094         console->Printf(KCommonFormat2,ptrc1.Length(),ptrc1.Size());
00095 
00096                                 // Construct a TPtrC to represent the 6 data
00097                                 // items offset 3 from the start of bufc's
00098                                 // data area.   
00099         TPtrC ptrc2 = bufc.Mid(3,6);     
00100         
00101                                 // ptrc's data is "lo Wor"
00102                                 // Length of ptrc is 6
00103                                 // ptrc's data area address = buf's data area
00104                                 // address + 3
00105         _LIT(KFormat4,"  Mid TPtrC: \"%S\"; Ptr()=%x; ");
00106         console->Printf(KFormat4,&ptrc2,ptrc2.Ptr());
00107         console->Printf(KCommonFormat2,ptrc2.Length(),ptrc2.Size());
00108                                 // In practice, there is often no need to 
00109                                 // assign the returned TPtrC to another TPtrC.
00110                                 // For example, the following code puts a 
00111                                 // value of 3 in pos; this is the offset
00112                                 // of char 'W' within the chars "lo Wor"  
00113                                 // (see later for more detail on Locate())
00114                                 
00115         pos = (bufc.Mid(3,6)).Locate('W');
00116         _LIT(KFormat5,"(bufc.Mid(3,6)).Locate('W') returns %d\n");
00117         console->Printf(KFormat5,pos);
00118 
00119                                 // Want the 13 right hand data items.
00120                                 // This is > current length of bufc
00121                                 // causing panic !!
00122                                 // 
00123                                 // Remove the "//" marks on the next line
00124                                 // to see this happen
00125         //TPtrC ptrc3 = bufc.Right(13);
00126     
00127                         //                              
00128                         // Compare() & CompareF()   * * * * * * * * *
00129                         //
00130         _LIT(KTxtCompare,"\n--->Compare() & CompareF()");
00131         console->Printf(KTxtCompare);
00132         console->Printf(KTxtPressToContinue);  //" (press any key to continue)\n"
00133         console->Getch();
00134 
00135                                 // Can compare any kind of data.
00136                                 // For binary data just use Compare().
00137                                 // For text use Compare(), CompareF() or 
00138                                 // CompareC(). Using the Compare() function,
00139                                 // case is important so that, below, the 4th 
00140                                 // comparison is equal but the 5th is unequal.
00141     
00142         const TBufC<16> compstr[7] =  {*&KTxtCompstr1, // "Hello World!@@"
00143                                                                    *&KTxtCompstr2, // "Hello"
00144                                                            *&KTxtCompstr3, // "Hello Worl"
00145                                                                    *&KTxtCompstr4, // "Hello World!"
00146                                                                    *&KTxtCompstr5, // "hello world!"
00147                                                                    *&KTxtCompstr6, // "Hello World "
00148                                                                    *&KTxtCompstr7  // "Hello World@"
00149                                                                   };
00150 
00151         for (index = 0; index < 7; index++)
00152                 {
00153                 
00154                 if ( (bufc.Compare(compstr[index])) < 0 )
00155                         genptr.Set(KTxtlessthan);
00156                 else if ( (bufc.Compare(compstr[index])) > 0)
00157                                 genptr.Set(KTxtgreaterthan);
00158                          else genptr.Set(KTxtequalto);
00159             _LIT(KFormat6,"\"%S\"%S\"%S\"\n");          
00160                 console->Printf(KFormat6,&bufc,&genptr,&compstr[index]);
00161                 }
00162 
00163                                 // CompareF() ignores case so that now,
00164                                 // both the 4th and the 5th comparsions
00165                                 // are equal.
00166                                 // NOTE that the behaviour of CompareF() is locale dependent.
00167         for (index = 3; index < 5; index++)
00168                 {
00169                 if ( (bufc.CompareF(compstr[index])) < 0 )
00170                         genptr.Set(KTxtlessthan);
00171                 else if ( (bufc.CompareF(compstr[index])) > 0)
00172                                 genptr.Set(KTxtgreaterthan);
00173                          else genptr.Set(KTxtequalto);
00174                 _LIT(KTxtusingCF," (using CompareF())");
00175                 _LIT(KFormat7,"\"%S\"%S\"%S\"%S\n");
00176                 console->Printf(KFormat7,&bufc,&genptr,&compstr[index],&KTxtusingCF);
00177                 }
00178 
00179                 //                              
00180                         // Locate(), LocateF(), LocateReverse()     * * *
00181                         //
00182                         // NOTE that the behaviour of LocateF() is locale dependent.
00183         _LIT(KTxtLocate,"\n--->Locate(), LocateF() & LocateReverse()");
00184         console->Printf(KTxtLocate);
00185         console->Printf(KTxtPressToContinue);  //" (press any key to continue)\n"
00186         console->Getch();
00187                                 
00188                                 // Locate the positions (i.e. the offsets) of
00189                                 // these characters in "Hello World!"                     
00190         TChar ch[4] = {'H', '!', 'o', 'w'};
00191                                                         
00192                                 // using Locate().
00193                                 // Note that 'w' is not found because the
00194                                 // function is case sensitive
00195         _LIT(KTxtUsingLocate,"using Locate() \n");
00196         console->Printf(KTxtUsingLocate);
00197         for (index = 0  ; index < 4; index++)
00198                 {
00199                 pos = bufc.Locate(ch[index]);
00200 
00201                 if (pos < 0)
00202                         genptr.Set(KTxtnotfound);
00203                 else 
00204                         genptr.Set(KTxtfound);
00205 
00206                 console->Printf(KCommonFormat8,&bufc,TUint(ch[index]),pos,&genptr);
00207                 }
00208 
00209                                 // using LocateF()
00210                                 // Note that 'w' is found because the
00211                                 // function is NOT case sensitive.
00212                                 //
00213                                 // NOTE that the behaviour of LocateF() is locale dependent.
00214         _LIT(KTxtUsingLocateF,"using LocateF() \n");
00215         console->Printf(KTxtUsingLocateF);
00216         for (index = 0  ; index < 4; index++)
00217                 {
00218                 pos = bufc.LocateF(ch[index]);
00219 
00220                 if (pos < 0)
00221                         genptr.Set(KTxtnotfound);
00222                 else 
00223                         genptr.Set(KTxtfound);
00224 
00225                 console->Printf(KCommonFormat8,&bufc,TUint(ch[index]),pos,&genptr); 
00226                 }
00227         
00228                         // using LocateReverse()
00229                         // Note that the 2nd char 'o' is found this time
00230         _LIT(KTxtUsingLocateReverse,"using LocateReverse() \n");
00231         console->Printf(KTxtUsingLocateReverse);
00232         for (index = 0  ; index < 4; index++)
00233                 {
00234                 pos = bufc.LocateReverse(ch[index]);
00235 
00236                 if (pos < 0)
00237                         genptr.Set(KTxtnotfound);
00238                 else 
00239                         genptr.Set(KTxtfound);
00240 
00241                 console->Printf(KCommonFormat8,&bufc,TUint(ch[index]),pos,&genptr);
00242                 }
00243                 
00244                         //                              
00245                         // Match() & MatchF()   * * * * * * *
00246                         //
00247                         //
00248                         // NOTE that the behaviour of MatchF() is locale dependent.
00249         _LIT(KTxtMatch,"\n--->Match()");
00250         console->Printf(KTxtMatch);
00251         console->Printf(KTxtPressToContinue);  //" (press any key to continue)\n"
00252         console->Getch();
00253 
00254         
00255     TBufC<8> matchstr[7] =  {*&KTxtMatchstr1, // "*World*"
00256                                                          *&KTxtMatchstr2, // "*W?rld*"
00257                                                          *&KTxtMatchstr3, // "Wor*"
00258                                                          *&KTxtMatchstr4, // "Hello"
00259                                                          *&KTxtMatchstr5, // "*W*"
00260                                                          *&KTxtMatchstr6, // "hello*"
00261                                                          *&KTxtMatchstr7  // "*" 
00262                                                         };
00263 
00264 
00265         _LIT(KFormat10,"\"%S\"\n");
00266         console->Printf(KFormat10,&bufc);
00267                          
00268                                 // using Match()
00269         for (index = 0  ; index < 7; index++)
00270                 {
00271                 pos = bufc.Match(matchstr[index]);
00272 
00273                 if (pos < 0)
00274                         genptr.Set(KTxtnotfound);
00275                 else 
00276                         genptr.Set(KTxtfound);
00277                 
00278                 console->Printf(KCommonFormat9,&matchstr[index],pos,&genptr); 
00279                 }
00280 
00281                                 // using MatchF()
00282                                 //   
00283                                 // Note the different result when matching 
00284                                 // the 6th string, where case is ignored.
00285                                 //
00286                                 // NOTE that the behaviour of MatchF() is locale dependent.
00287         _LIT(KTxtMatchF,"\n--->MatchF()");
00288         console->Printf(KTxtMatchF);
00289         console->Printf(KTxtPressToContinue);  //" (press any key to continue)\n"
00290         console->Getch();
00291         
00292         for (index = 0  ; index < 7; index++)
00293                 {
00294                 pos = bufc.MatchF(matchstr[index]);
00295 
00296                 if (pos < 0)
00297                         genptr.Set(KTxtnotfound);
00298                 else 
00299                         genptr.Set(KTxtfound);
00300 
00301                 console->Printf(KCommonFormat9,&matchstr[index],pos,&genptr); 
00302                 }
00303         }
00304 
00305 
00306 
00307 
00308 
00309         
00310         

Generated on Thu Jan 21 10:32:55 2010 for TB10.1 Example Applications by  doxygen 1.5.3