examples/Base/BufsAndStrings/Desc/Modifier/Modifier.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 modifying member
00015 // functions of descriptors.
00016 //
00017 
00018 #include "CommonFramework.h"
00019 
00020 //
00021 // Common literal text 
00022 //
00023 _LIT(KPressAnyKeyToContinue," (press any key to continue)\n");
00024 _LIT(KTextHelloWorld,"Hello World!");
00025 _LIT(KTextHello,"Hello");
00026 _LIT(KTextXYZ,"XYZ");
00027 _LIT(KTextDinner,"D\214ner \205 la Fran\207ais");
00028 
00029 //
00030 // Common format strings
00031 //
00032 
00033 _LIT(KFormatfourex,"%4x");
00034 _LIT(KFormatBufLenSizeMax,"\"%S\"; %d; %d; %d\n");
00035 _LIT(KFormatLenSizeMax,"; %d; %d; %d\n");
00036 _LIT(KFormatBufLen,"\"%S\"; Length()=%d; ");
00037 _LIT(KFormatSizeMax,"Size()=%d; MaxLength()=%d\n");
00038 _LIT(KFormatCommon,"0x%02x ");
00039 
00040                         // Define and implement
00041                         // the overflow handler used
00042                         // by the AppendFormat() example.
00043 
00044 class TestOverflow : public TDesOverflow
00045         {
00046         void Overflow(TDes& aDes);
00047         };
00048 
00049 
00050 void TestOverflow::Overflow(TDes& aDes)
00051         {
00052         _LIT(KTextDescOverflow,"Descriptor overflow - maxlength %d\n");
00053         console->Printf(KTextDescOverflow,aDes.MaxLength());
00054         }
00055 
00056 
00057 // Do the example
00058 LOCAL_C void doExampleL()
00059     {                              
00060 
00061         TInt index;
00062         TInt counter;
00063 
00064                                 // Use a TBuf to demonstrate some of these
00065         TBuf<32> buf(KTextHelloWorld);
00066                 
00067                         //                              
00068                         // Copy(),CopyF(), CopyUC() & CopyCP()  * * * 
00069                         //
00070                         // Note that CopyF() CopyUC() & CopyCP() are locale dependent
00071 
00072         _LIT(KTitleCopy,"\n--->Copy(),CopyF(),CopyUC() & CopyCP()\n");
00073         _LIT(KNoteAboutLocale1,"\nNote that the behaviour of CopyF(), CopyUC() & CopyCP() is\n");
00074         _LIT(KNoteAboutLocale2,"dependent on the locale.\n");
00075         console->Printf(KTitleCopy);
00076         console->Printf(KNoteAboutLocale1);
00077         console->Printf(KNoteAboutLocale2);
00078                                                 
00079                                 // Show buf's content,length,size and
00080                                 // maximum size.
00081         console->Printf(KFormatBufLen,&buf,buf.Length());
00082         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00083 
00084                                 // Copy normal
00085         buf.Copy(KTextDinner);
00086 
00087                                 // Show buf's content,length,size and 
00088                                 // maximum size.
00089         console->Printf(KFormatBufLen,&buf,buf.Length());
00090         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00091         
00092 
00093                                 // Copy folded - accents are not preserved
00094                                 //
00095                                 // (NB the display may use a different code
00096                                 // page to that used by the base, in which 
00097                                 // case the accents will seem to be preserved)
00098         buf.CopyF(KTextDinner);
00099                                 
00100                                 // Show buf's new content,length,size
00101                                 // and max size
00102         console->Printf(KFormatBufLen,&buf,buf.Length());
00103         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00104                                 // Copy uppercase
00105                                 // Note that accents are preserved.
00106         buf.CopyUC(KTextDinner);
00107                                 
00108                                 // Show buf's new content,length,size
00109                                 // and maximum size
00110         console->Printf(KFormatBufLen,&buf,buf.Length());
00111         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00112 
00113                                 // Copy capitalised
00114                                 // Note that accents are preserved.
00115         _LIT(KTextCatOnMat,"tHe CaT sAt On ThE mAt - voil\205");
00116         buf.CopyCP(KTextCatOnMat);
00117                                 
00118                                 // Show buf's new content,length,size
00119                                 // and max size
00120         console->Printf(KFormatBufLen,&buf,buf.Length());
00121         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00122                                                 
00123                                 // Length of copied text > 32 causes panic !!
00124                                 // 
00125                                 // Remove the "//" marks on the next two lines
00126                                 // to see this happen
00127         //_LIT(KTxtCausePanic1,"Panic caused by an attempt to replace text.");
00128         //buf.Copy(KTxtCausePanic1);
00129     
00130                         //
00131                         // Append() & operator+=  * * * * * * * * * 
00132                         //
00133         _LIT(KTitleAppend,"\n--->Append & Operator+=");
00134         console->Printf(KTitleAppend);
00135         console->Printf(KPressAnyKeyToContinue);
00136         console->Getch();
00137 
00138                                 // Show buf's content,length,size and
00139                                 // maximum size.
00140         buf = KTextHelloWorld;
00141         console->Printf(KFormatBufLen,&buf,buf.Length());
00142         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00143 
00144                                 // Append:
00145                                 //     1. a single character
00146                                 //     2. a descriptor
00147                                 //     3. another descrptor using operator +=
00148                                 //
00149                                 // Generates the string
00150                                 // "Hello World!@XYZ##" in buf
00151         buf.Append('@');
00152         buf.Append(KTextXYZ);
00153         _LIT(KTextHashHash,"##");
00154         buf += KTextHashHash;
00155         console->Printf(KFormatBufLen,&buf,buf.Length());
00156         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00157 
00158                         //                              
00159                         // Swap()  * * * * * * * * * * * * * * * 
00160                         //
00161         _LIT(KTitleSwap,"\n--->Swap()");
00162         console->Printf(KTitleSwap);
00163         console->Printf(KPressAnyKeyToContinue);
00164         console->Getch();
00165         
00166         _LIT(KWhatANiceDay,"What a nice day");
00167         TBuf<16> altbuf1(KWhatANiceDay);
00168         buf = KTextHelloWorld;
00169 
00170                                 // Show buf and altbuf BEFORE swap
00171         _LIT(KTextBefore,"          BEFORE...\n");
00172         console->Printf(KTextBefore);
00173         console->Printf(KFormatBufLen,&buf,buf.Length());
00174         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00175         
00176         console->Printf(KFormatBufLen,&altbuf1,altbuf1.Length());
00177         console->Printf(KFormatSizeMax,altbuf1.Size(),altbuf1.MaxLength());
00178         
00179                                 // now swap the descriptors and look at
00180                                 // buf and altbuf again;
00181                                 // The content, length and size of the 
00182                                 // descriptors have swapped; their maximum 
00183                                 // lengths have NOT changed.
00184         buf.Swap(altbuf1);
00185 
00186         _LIT(KTextAfter,"          AFTER...\n");
00187         console->Printf(KTextAfter);
00188         console->Printf(KFormatBufLen,&buf,buf.Length());
00189         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00190         
00191         console->Printf(KFormatBufLen,&altbuf1,altbuf1.Length());
00192         console->Printf(KFormatSizeMax,altbuf1.Size(),altbuf1.MaxLength()
00193                                           );
00194 
00195                                 // Swap is ok provided the maximum length
00196                                 // of each descriptor is big enough to 
00197                                 // hold the other's data.
00198         
00199                                 // altbuf2 is too small to accommodate 
00200                                 // buf's data !!
00201                                 // 
00202                                 // Remove the "//" marks on the next three lines
00203                                 // to see this panic
00204         //_LIT(KTxtxxx,"xxx");
00205         //TBuf<8>  altbuf2(KTxtxxx);
00206         //buf = KTextHelloWorld;
00207         //buf.Swap(altbuf2);
00208     
00209         
00210                         //                              
00211                         // Repeat()  * * * * * * * * * * * * * * * 
00212                         //
00213         _LIT(KTitleRepeat,"\n--->Repeat()");
00214         console->Printf(KTitleRepeat);
00215         console->Printf(KPressAnyKeyToContinue);
00216         console->Getch();
00217 
00218                                 // Set current length of buf to 16 and
00219                                 // copy repeat the characters "Hello".
00220                                 // The descriptor is filled up to 
00221                                 // its CURRENT LENGTH.
00222                                 // Result is the 16 charcters
00223                                 // "HelloHelloHelloH"
00224                                 //
00225                                 // Note the truncation. 
00226         buf.SetLength(16);
00227         buf.Repeat(KTextHello);
00228 
00229                                 // Look at it.
00230         console->Printf(KFormatBufLen,&buf,buf.Length());
00231         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00232 
00233                                 // Now set the length to 8 characters
00234                                 // and do Repeat again.
00235                                 // Result is the 8 characters
00236                                 // "HelloHel"
00237 
00238         buf.SetLength(8);
00239         buf.Repeat(KTextHello);
00240 
00241                                 // Look at it
00242         console->Printf(KFormatBufLen,&buf,buf.Length());
00243         console->Printf(KFormatSizeMax,buf.Size(),buf.MaxLength());
00244         
00245                         //                              
00246                         // Insert() & Delete()  * * * * * * * * *
00247                         //
00248         _LIT(KTitleInsert,"\n--->Insert() & Delete()");
00249         console->Printf(KTitleInsert);
00250         console->Printf(KPressAnyKeyToContinue);
00251         console->Getch();
00252 
00253                                 // set buf to contain the text "abc" and 
00254                                 // look at it
00255         _LIT(KTextAbc,"abc");
00256         buf = KTextAbc;
00257         console->Printf(KFormatBufLenSizeMax,
00258                                         &buf,
00259                                         buf.Length(),
00260                                         buf.Size(),
00261                                         buf.MaxLength()
00262                                    );
00263 
00264                                 // Insert the descriptor at the beginning
00265                                 // of buf to give "XYZabc"
00266                                 //
00267         
00268         buf.Insert(0,KTextXYZ);
00269 
00270                                 // Look at it
00271         console->Printf(KFormatBufLenSizeMax,
00272                                         &buf,
00273                                         buf.Length(),
00274                                         buf.Size(),
00275                                         buf.MaxLength()
00276                                    );
00277         
00278                                 // Now insert another descriptor at pos 2
00279                                 // to give "XijklmnYZabc"
00280                                 //
00281         _LIT(KTextijklmn,"ijklmn");
00282         buf.Insert(1,KTextijklmn);
00283 
00284                                 // Show result
00285         console->Printf(KFormatBufLenSizeMax,
00286                                         &buf,
00287                                         buf.Length(),
00288                                         buf.Size(),
00289                                         buf.MaxLength()
00290                                    );
00291                                                                         
00292         
00293                                 // Insertion point out of range
00294                                 // (> current length of descriptor)
00295                                 // 
00296                                 // Remove the "//" marks on the next line
00297                                 // to see this panic
00298         //_LIT(KTxtqwerty,"qwerty");
00299         //buf.Insert(buf.Length()+1,KTxtqwerty);
00300         
00301         
00302                                 // Resulting length of data
00303                                 // is > maximum length.
00304                                 // 
00305                                 // Remove the "//" marks on the next line
00306                                 // to see this panic
00307         //_LIT(KTxtatoz,"abcdefghijklmnopqrstuvwxyz");
00308         //buf.Insert(12,KTxtatoz);
00309         
00310         
00311                                 // Now delete the 3 data 
00312                                 // items (characters) at the start
00313                                 // of buf to give "klmnYZabc"
00314         buf.Delete(0,3);
00315 
00316                                 // Show result
00317         console->Printf(KFormatBufLenSizeMax,
00318                                         &buf,
00319                                         buf.Length(),
00320                                         buf.Size(),
00321                                         buf.MaxLength()
00322                                    );
00323         
00324                                 // Now delete the 4 data items (characters) at
00325                                 // position 4 to give "klmnc"
00326                                 //
00327         buf.Delete(4,4);
00328         
00329                                 // Show result
00330         console->Printf(KFormatBufLenSizeMax,
00331                                         &buf,
00332                                         buf.Length(),
00333                                         buf.Size(),
00334                                         buf.MaxLength()
00335                                    );
00336         
00337                                 // An excessive length for deletion is 
00338                                 // truncated to a sensible value. Deletes 
00339                                 // all the data starting at pos 1 to 
00340                                 // give "k"
00341                                 // (Note that the length actually
00342                                 //  deleted is 4 NOT 25000).
00343                                                         
00344         buf.Delete(1,25000);
00345         
00346                                 // Show result
00347         console->Printf(KFormatBufLenSizeMax,
00348                                     &buf,
00349                                     buf.Length(),
00350                                     buf.Size(),
00351                                     buf.MaxLength()
00352                                    );
00353                  
00354                         //                              
00355                         // TrimLeft() & TrimRight()  * * * * * * * * *
00356                         //
00357         _LIT(KTitleTrimLeft,"\n--->TrimLeft() & TrimRight()");
00358         console->Printf(KTitleTrimLeft);
00359         console->Printf(KPressAnyKeyToContinue);
00360         console->Getch();
00361 
00362                                 // set buf up and show the detail
00363         buf.Fill(' ',18);
00364         buf.Replace(3,(&KTextHelloWorld)->Length(),KTextHelloWorld);
00365         console->Printf(KFormatBufLenSizeMax,  
00366                         &buf,
00367                                         buf.Length(),
00368                                         buf.Size(),
00369                                         buf.MaxLength()
00370                                    );
00371 
00372                                 // Remove left hand spaces      
00373         buf.TrimLeft(); 
00374         console->Printf(KFormatBufLenSizeMax,  
00375                         &buf,
00376                                         buf.Length(),
00377                                         buf.Size(),
00378                                         buf.MaxLength()
00379                                    );
00380 
00381                                 // Remove right hand spaces     
00382         buf.TrimRight();        
00383         console->Printf(KFormatBufLenSizeMax,  
00384                         &buf,
00385                                         buf.Length(),
00386                                         buf.Size(),
00387                                         buf.MaxLength()
00388                                    );
00389 
00390                                 // Trim() removes both left and right
00391                                 // hand spaces 
00392 
00393                  
00394                         //                              
00395                         // FillZ() & Fill()  * * * * * * * * * *  
00396                         //
00397         _LIT(KTitleFill,"\n--->FillZ() & Fill()");
00398         console->Printf(KTitleFill);
00399         console->Printf(KPressAnyKeyToContinue);
00400         console->Getch();
00401                             
00402                                 // Set current length of buf
00403                                 // Fillz() fills buf up to its current length
00404                                 // with 0x00.
00405         buf.SetLength(8);
00406         buf.FillZ();
00407 
00408                                 // Show it
00409         counter = buf.Length();
00410         for (index = 0; index < counter; index++)
00411                 console->Printf(KFormatCommon,buf[index]);
00412                 
00413         console->Printf(KFormatLenSizeMax,
00414                             buf.Length(),
00415                                         buf.Size(),
00416                                         buf.MaxLength()
00417                                    );
00418 
00419                                 // Fills buf up to its current length 
00420                                 // with 'A'
00421         buf.Fill('A');
00422         
00423                                 // show it
00424         console->Printf(KFormatBufLenSizeMax,  
00425                         &buf,
00426                                         buf.Length(),
00427                                         buf.Size(),
00428                                         buf.MaxLength()
00429                                    );
00430         
00431                         //                              
00432                         // Num(), NumUC(), AppendNum() & AppendNumUC()
00433                         //
00434         _LIT(KTitleNum,"\n--->Num(), NumUC(), AppendNum()");
00435         console->Printf(KTitleNum);
00436         console->Printf(KPressAnyKeyToContinue);
00437         console->Getch();
00438 
00439                                 // convert a signed integer to 
00440                                 // decimal characters
00441         buf.Num(-176);
00442 
00443                                 // show it
00444         console->Printf(KFormatBufLenSizeMax,  
00445                         &buf,
00446                                         buf.Length(),
00447                                         buf.Size(),
00448                                         buf.MaxLength()
00449                                    );
00450 
00451                                 // convert another signed integer
00452         buf.Num(12345);
00453 
00454                                 // show it
00455         console->Printf(KFormatBufLenSizeMax,  
00456                         &buf,
00457                                     buf.Length(),
00458                                         buf.Size(),
00459                                         buf.MaxLength()
00460                                    );
00461 
00462                                 // convert an unsigned integer
00463         TUint nosign = 176;
00464         
00465                                 // ... into decimal characters (the default,
00466                                 // if not explicitly specified)
00467         buf.Num(nosign);
00468         console->Printf(KFormatBufLenSizeMax,  
00469                         &buf,
00470                                     buf.Length(),
00471                                         buf.Size(),
00472                                         buf.MaxLength()
00473                                    );
00474                                         
00475                                 // ... into binary characters
00476         buf.Num(nosign,EBinary);
00477         console->Printf(KFormatBufLenSizeMax,  
00478                         &buf,
00479                                         buf.Length(),
00480                                         buf.Size(),
00481                                         buf.MaxLength()
00482                                    );
00483         
00484                                 // ... into octal characters
00485         buf.Num(nosign,EOctal);
00486         console->Printf(KFormatBufLenSizeMax,  
00487                         &buf,
00488                                         buf.Length(),
00489                                         buf.Size(),
00490                                         buf.MaxLength()
00491                                    );
00492         
00493                                 // ... into lower case hex characters
00494         buf.Num(nosign,EHex);
00495         console->Printf(KFormatBufLenSizeMax,  
00496                         &buf,
00497                                         buf.Length(),
00498                                         buf.Size(),
00499                                         buf.MaxLength()
00500                                    );                             
00501         
00502                                 // ... into upper case hex characters
00503         buf.NumUC(nosign,EHex);
00504         console->Printf(KFormatBufLenSizeMax,  
00505                         &buf,
00506                                     buf.Length(),
00507                                     buf.Size(),
00508                                     buf.MaxLength()
00509                                    );
00510                         
00511                                 // Append functions the same, except 
00512                                 // they append the converted data.
00513                                 // Put "xyz" into the descriptor, convert
00514                                 // and concatenate the variations.
00515 
00516         _LIT(KTextxyz,"xyz");                           
00517         buf = KTextxyz;
00518 
00519         buf.AppendNum(nosign);
00520         buf.AppendNum(nosign,EBinary);
00521         buf.AppendNum(nosign,EOctal);
00522         buf.AppendNum(nosign,EHex);
00523         buf.AppendNumUC(nosign,EHex);
00524         console->Printf(KFormatBufLenSizeMax,  
00525                         &buf,
00526                                         buf.Length(),
00527                                         buf.Size(),
00528                                         buf.MaxLength()
00529                                    );
00530                                                 
00531                         //                              
00532                         // Justify() & AppendJustify()* * * 
00533                         //
00534         _LIT(KTitleJustify,"\n--->Justify() & AppendJustify()");
00535         console->Printf(KTitleJustify);
00536         console->Printf(KPressAnyKeyToContinue);
00537         
00538         console->Getch();
00539 
00540                                 // source descriptor for this example has
00541                                 // length 12. 
00542         TBufC<40> src(KTextHelloWorld);
00543                                 
00544                                 // target field in buf has width 16 (NB this
00545                                 // is greater than the length of the descriptor 
00546                                 // src).
00547                                 // src is copied into target field, aligned left 
00548                                 // and padded with '@' characters.
00549                                 //
00550                                 // length of buf becomes the same as the 
00551                                 // specified width, i.e 16 
00552         buf.Justify(src,16,ELeft,'@');
00553         console->Printf(KFormatBufLenSizeMax,  
00554                         &buf,
00555                                         buf.Length(),
00556                                         buf.Size(),
00557                                         buf.MaxLength()
00558                                    );
00559         
00560                                 // target field in buf has width 16 (NB this
00561                                 // is greater than the length of the descriptor
00562                                 // src).
00563                                 // src is copied into target field, aligned centre 
00564                                 // and padded with '@' characters
00565                                 //
00566                                 // length of buf becomes the same as the
00567                                 // specified width, i.e 16 
00568         buf.Justify(src,16,ECenter,'@');
00569         console->Printf(KFormatBufLenSizeMax,  
00570                         &buf,
00571                                         buf.Length(),
00572                                         buf.Size(),
00573                                         buf.MaxLength()
00574                                    );
00575         
00576                                 // target field in buf has width 10 (NB this
00577                                 // is smaller than the length of the descriptor
00578                                 // src).
00579                                 // src is copied into target field but truncated
00580                                 // to 10 characters and, therefore, alignment and 
00581                                 // padding information not used.
00582                                 //
00583                                 // length of buf becomes the same as the 
00584                                 // width, i.e 10 
00585         buf.Justify(src,10,ECenter,'@');
00586         console->Printf(KFormatBufLenSizeMax,  
00587                         &buf,
00588                                         buf.Length(),
00589                                         buf.Size(),
00590                                         buf.MaxLength()
00591                                    );
00592                 
00593                                 // target field in buf is set to the length of
00594                                 // the descriptor src (whatever it currently is)
00595                                 //
00596                                 // src copied into target field. No padding and
00597                                 // no truncatiuon needed and so the
00598                                 // alignment and padding information is not used.
00599                                 //
00600                                 // length of buf becomes the same as the length
00601                                 // of src i.e 12 
00602         buf.Justify(src,KDefaultJustifyWidth,ECenter,'@');
00603         console->Printf(KFormatBufLenSizeMax,  
00604                         &buf,
00605                                         buf.Length(),
00606                                         buf.Size(),
00607                                         buf.MaxLength()
00608                                    );
00609         
00610         
00611         
00612                                 // implied width > maximum length of buf.
00613                                 // 
00614                                 // Remove the "//" marks on the next two lines
00615                                 // to see this panic
00616         //_LIT(KTxtPanicCausingText,"Panic causing text because length > 32");
00617         //src = KTxtPanicCausingText;
00618         //buf.Justify(src,KDefaultJustifyWidth,ECenter,'@' );
00619         
00620 
00621          
00622                                 // explicit width > maximum length of buf
00623                                 // 
00624                                 // Remove the "//" marks on the next line
00625                                 // to see this panic
00626         //buf.Justify(src,33,ECenter,'@');
00627         
00628 
00629                                 // AppendJustify() is  similar but target 
00630                                 // field appended to descriptor.
00631                                 //
00632                                 // Target field in buf has width 16 
00633                                 // (NB greater than length of descriptor 
00634                                 // src) and is appended to existing content
00635                                 // of buf.
00636                                 // src copied into target field, 
00637                                 // aligned left and padded with '@' 
00638                                 // characters
00639                                 //
00640                                 // Resulting length of buf is the length
00641                                 // of "ABCD" plus the width of the target
00642                                 // field (16) giving a value of 20.
00643         _LIT(KTextABCD,"ABCD");
00644         buf = KTextABCD;
00645         buf.AppendJustify(src,16,ELeft,'@');
00646         console->Printf(KFormatBufLenSizeMax,  
00647                         &buf,
00648                                         buf.Length(),
00649                                         buf.Size(),
00650                                         buf.MaxLength()
00651                                    );
00652 
00653                         //                              
00654                         // Format() & AppendFormat() * * * 
00655                         //
00656         _LIT(KTitleFormatAndAppendFormat,"\n--->Format() & AppendFormat()");
00657         console->Printf(KTitleFormatAndAppendFormat);
00658         console->Printf(KPressAnyKeyToContinue);
00659         console->Getch();
00660 
00661                                 // use tgt as a target descriptor to show 
00662                                 // the results of format()
00663         TBuf<128> tgt;
00664         
00665                                 // The basic %<type> format.
00666                                 // Interprets the arguments as signed/unsigned
00667                                 // integers and generates the appropriate
00668                                 // character representations. The characters only
00669                                 // occupy the space needed to represent them.
00670                                 //
00671                                 // Generates:"1000001 A 65 101 65 41"
00672                                 //
00673                                 // format string is:
00674                                 // "%b %c %d %o %u %x"
00675 
00676         _LIT(KFormat1,"%b %c %d %o %u %x");
00677         tgt.Format(KFormat1,65,65,65,65,65,65);
00678     console->Printf(KFormatBufLenSizeMax,  
00679                         &tgt,
00680                                         tgt.Length(),
00681                                         tgt.Size(),
00682                                         tgt.MaxLength()
00683                                    );                   
00684 
00685                                 // Interprets the argument as an unsigned
00686                                 // integer; the generated (hex) characters are
00687                                 // right aligned and padded to the left
00688                                 // with '0' to make a total of 4 characters.
00689                                 //
00690                         // Generates:"0041"
00691                                 //
00692                                 // format string is:
00693                                 // "%04x"
00694         _LIT(KFormat2,"%04x");
00695         tgt.Format(KFormat2,65);
00696     console->Printf(KFormatBufLenSizeMax,  
00697                         &tgt,
00698                                         tgt.Length(),
00699                                         tgt.Size(),
00700                                         tgt.MaxLength()
00701                                    );
00702                                                                                 
00703                                 // Interprets the argument as an unsigned
00704                                 // integer;the generated (hex) characters are
00705                                 // right aligned and padded to the left with 
00706                                 // blanks to make a total of 4 characters.
00707                                 //
00708                         // Generates:"  41"
00709                                 //
00710                                 // format string is:
00711                                 // "%4x"
00712         tgt.Format(KFormatfourex,65);
00713     console->Printf(KFormatBufLenSizeMax,  
00714                         &tgt,
00715                                         tgt.Length(),
00716                                         tgt.Size(),
00717                                         tgt.MaxLength()
00718                                    );                   
00719                                                                                 
00720                                 // Interprets the argument as an unsigned
00721                                 // integer; the generated (hex) characters are
00722                                 // right aligned and padded to the left with 
00723                                 // blanks to make a total of 4 characters.
00724                                 //
00725                         // Generates:"1fff" 
00726                                 //
00727                                 // NB the correct hex value is "1ffff"
00728                                 // but width specified as 4 resulting 
00729                                 // in TRUNCATION.
00730                                 // 
00731                                 //
00732                                 // format string is:
00733                                 // "%4x"
00734         tgt.Format(KFormatfourex,131071);
00735     console->Printf(KFormatBufLenSizeMax,  
00736                         &tgt,
00737                                         tgt.Length(),
00738                                         tgt.Size(),
00739                                         tgt.MaxLength()
00740                                    );                                                                                   
00741         
00742                                 // The '*' means that the width of the output
00743                                 // is taken from the 1st argument in the list; 
00744                                 // Interprets the 2nd argument as an unsigned
00745                                 // integer.
00746                                 // The generated (hex) characters are right 
00747                                 // aligned and padded to the left with blanks
00748                                 // to make a total of 4 characters.
00749                                 //
00750                         // Generates:"  41"
00751                                 //
00752                                 // format string is:
00753                                 // "%*x"
00754         _LIT(KFormat4,"%*x");
00755         tgt.Format(KFormat4,4,65);
00756     console->Printf(KFormatBufLenSizeMax,  
00757                         &tgt,
00758                                     tgt.Length(),
00759                                     tgt.Size(),
00760                                     tgt.MaxLength()
00761                                    );                   
00762                 
00763                                 // Interprets the 1st argument as a signed
00764                                 // integer. 
00765                                 //
00766                                 // The generated (decimal) characters are 
00767                                 // right aligned and padded to the left 
00768                                 // with '$' to make a total of 4 characters.
00769                                 //
00770                                 // These are then followed by the
00771                                 // 4 characters ".00 "
00772                                 //                                                                                         
00773                                 // Interprets the 2nd argument as a descriptor.
00774                                 // The characters within the descriptor are
00775                                 // appended to those already generated.
00776                                 //
00777                         // Generates:"$$65.00 over"
00778                                 //
00779                                 // Format string is:
00780                                 // "%+$4d.00 %S"
00781                                 //
00782     
00783         _LIT(KTextover,"over");
00784         _LIT(KFormat5,"%+$4d.00 %S");
00785         tgt.Format(KFormat5,65,&KTextover);
00786     console->Printf(KFormatBufLenSizeMax,  
00787                         &tgt,
00788                                         tgt.Length(),
00789                                         tgt.Size(),
00790                                         tgt.MaxLength()
00791                                    );
00792         
00793         
00794                                 // The * means that the width of the 
00795                                 // output is taken from the 1st argument 
00796                                 // in the list.
00797                                 //
00798                                 // Interprets the 2nd argument as a descriptor.
00799                                 // The characters copied from it are right 
00800                                 // aligned and padded on the left with
00801                                 // '0' characters
00802                                 //
00803                         // Generates:"000000fred"
00804                                 //
00805                                 // Format string is:
00806                                 // "%+0*S"
00807                                 //
00808         
00809         _LIT(KTextfred,"fred");
00810         _LIT(KFormat6,"%+0*S");
00811         tgt.Format(KFormat6,10,&KTextfred);
00812     console->Printf(KFormatBufLenSizeMax,  
00813                         &tgt,
00814                                         tgt.Length(),
00815                                         tgt.Size(),
00816                                         tgt.MaxLength()
00817                                    );                          
00818     
00819 
00820                                 // The * means that the fill character is 
00821                                 // taken from the 1st argument in the list.
00822                                 //
00823                                 // Interprets the 2nd argument as an unsigned
00824                                 // integer.
00825                                 // The generated characters are centrally 
00826                                 // aligned and padded on the left and right
00827                                 // with '*' to make 6 characters.
00828                                 //
00829                         // Generates:"**41**"
00830                                 //
00831                                 // Format string is:
00832                                 // "%=*6x"
00833                                 //
00834         
00835         _LIT(KFormat7,"%=*6x");
00836         tgt.Format(KFormat7,'*',65);
00837     console->Printf(KFormatBufLenSizeMax,  
00838                         &tgt,
00839                                         tgt.Length(),
00840                                         tgt.Size(),
00841                                         tgt.MaxLength()
00842                                    );
00843                                                                     
00844                                 // The 1st '*' means that the fill character 
00845                                 // is taken from the 1st argument in the list.
00846                                 //
00847                                 // The 2nd '*' means that the width is taken
00848                                 // from the 2nd argument in the list
00849                                 //
00850                                 // Interprets the 3rd argument as a signed
00851                                 // integer.
00852                                 // The generated characters are right aligned
00853                                 // and padded on the left with '.' to make
00854                                 // 10 characters.
00855                                 //
00856                         // Generates:".......-65"
00857                                 //
00858                                 // Format string is:
00859                                 // "%+**d"
00860                                 //
00861                 
00862         _LIT(KFormat8,"%+**d");
00863         tgt.Format(KFormat8,'.',10,(-65));
00864     console->Printf(KFormatBufLenSizeMax,  
00865                         &tgt,
00866                                     tgt.Length(),
00867                                         tgt.Size(),
00868                                         tgt.MaxLength()
00869                                    );
00870                                                                    
00871                 // Same as the previous example but the 
00872                 // characters are left aligned and padded to
00873                 // the right with '.' characters.
00874                                 //
00875                         // Generates:"-65......."              
00876                                 //
00877                                 // Format string is:
00878                                 // "%-**d"
00879                                 //
00880         
00881         _LIT(KFormat9,"%-**d");     
00882         tgt.Format(KFormat9,'.',10,(-65));
00883     console->Printf(KFormatBufLenSizeMax,  
00884                         &tgt,
00885                                         tgt.Length(),
00886                                         tgt.Size(),
00887                                         tgt.MaxLength()
00888                                    );                                                  
00889                               
00890                 // Generates 6 fill characters 'A'.
00891                 // Makes no use of the argument list.
00892                 //
00893                 // Generates: "AAAAAA"                
00894                                 //
00895                                 // Format string is:
00896                                 // "%-A6p"
00897                                 //
00898         
00899         _LIT(KFormat10,"%-A6p");            
00900         tgt.Format(KFormat10,65);
00901         console->Printf(KFormatBufLenSizeMax,  
00902                         &tgt,
00903                                         tgt.Length(),
00904                                         tgt.Size(),
00905                                         tgt.MaxLength()
00906                                    );
00907 
00908                  // Interpret the argument as a TInt64 type.        
00909         _LIT(KFormat10a,"%Li");  
00910         
00911         TInt64   var(65);
00912         tgt.Format(KFormat10a,var);
00913         
00914         console->Printf(KFormatBufLenSizeMax,  
00915                         &tgt,
00916                                         tgt.Length(),
00917                                         tgt.Size(),
00918                                         tgt.MaxLength()
00919                            );
00920             
00921 
00922 
00923 
00924                                 // Interpret the argument as an unsigned integer
00925                                 // and convert to a two byte numeric 
00926                                 // representation (most significant byte first)
00927                                 //
00928                                 // So 4660 = 0x1234 => 1st byte contains 0x12
00929                                 //                     2nd byte contains 0x34 
00930                                 //
00931                                 // NB This is same for both ASCII & UNICODE build
00932                                 //
00933                                 // Format string is:
00934                                 // "%m"
00935                                 //
00936          
00937         _LIT(KFormat11,"%m");
00938         tgt.Format(KFormat11,4660);
00939         
00940         counter = tgt.Length();
00941         for (index = 0; index < counter; index++)
00942                 console->Printf(KFormatCommon,tgt[index]);
00943                 
00944         console->Printf(KFormatLenSizeMax,
00945                                         tgt.Length(),
00946                                         tgt.Size(),
00947                                         tgt.MaxLength()
00948                                    );
00949 
00950 
00951 
00952 
00953 
00954                                 // Interpret the argument as an unsigned integer
00955                                 // and convert to a four byte numeric 
00956                                 // representation (most significant byte first)
00957                                 //
00958                                 // So 4660 = 0x1234 => 1st byte contains 0x00
00959                                 //                     2nd byte contains 0x00 
00960                                 //                                         3rd byte contains 0x12
00961                                 //                     4th byte contains 0x34
00962                                 // NB This is same for both ASCII & UNICODE build         
00963                                 //
00964                                 // Format string is:
00965                                 // "%M"
00966                                 //
00967         
00968         _LIT(KFormat12,"%M");
00969         tgt.Format(KFormat12,4660);                                                
00970         
00971         counter = tgt.Length(); 
00972     for (index = 0; index < counter; index++)
00973                 console->Printf(KFormatCommon,tgt[index]);
00974                 
00975         console->Printf(KFormatLenSizeMax,
00976                                         tgt.Length(),
00977                                         tgt.Size(),
00978                                         tgt.MaxLength()
00979                                    );
00980 
00981                                 // Interpret the argument as an unsigned integer
00982                                 // and convert to a two byte numeric 
00983                                 // representation (least significant byte first)
00984                                 //
00985                                 // So 4660 = 0x1234 => 1st byte contains 0x34
00986                                 //                     2nd byte contains 0x12 
00987                                 //
00988                                 // NB This is same for both ASCII & UNICODE build
00989                                 //
00990                                 // Format string is:
00991                                 // "%w"
00992                                 //
00993       
00994         _LIT(KFormat13,"%w");
00995         tgt.Format(KFormat13,4660);
00996         
00997         counter = tgt.Length();
00998     for (index = 0; index < counter; index++)
00999                 console->Printf(KFormatCommon,tgt[index]);
01000                 
01001         console->Printf(KFormatLenSizeMax,
01002                                         tgt.Length(),
01003                                         tgt.Size(),
01004                                         tgt.MaxLength()
01005                                    );
01006 
01007                                 // Interpret the argument as an unsigned integer
01008                                 // and convert to a four byte numeric 
01009                                 // representation (least significant byte first)
01010                                 //
01011                                 // So 4660 = 0x1234 => 1st byte contains 0x34
01012                                 //                     2nd byte contains 0x12 
01013                                 //                                         3rd byte contains 0x00
01014                                 //                     4th byte contains 0x00
01015                                 // NB This is same for both ASCII & UNICODE build         
01016                                 //
01017                                 // Format string is:
01018                                 // "%W"
01019                                 //
01020 
01021         _LIT(KFormat14,"%W");
01022         tgt.Format(KFormat14,4660);                                                
01023         
01024         counter = tgt.Length(); 
01025     for (index = 0; index < counter; index++)
01026                 console->Printf(KFormatCommon,tgt[index]);
01027                 
01028         console->Printf(KFormatLenSizeMax,
01029                                         tgt.Length(),
01030                                         tgt.Size(),
01031                                         tgt.MaxLength()
01032                                    );
01033         
01034         console->Printf(KPressAnyKeyToContinue);
01035         console->Getch();
01036         
01037         
01038                         //                              
01039                         // AppendFormat() illustrating use of the
01040                         // overflow handler 
01041                         //
01042         _LIT(KTitleOverflowHandler,"\n--->AppendFormat() & the overflow handler");
01043         console->Printf(KTitleOverflowHandler);
01044         console->Printf(KPressAnyKeyToContinue);
01045         console->Getch();
01046 
01047                                 // use the TestOverflow class as the overflow 
01048                                 // handler
01049         TBuf<16>     overtgt;
01050         TestOverflow theoverflow;
01051         
01052                                 // prime "overtgt" with data (of length 14)
01053         _LIT(KTextabcdefghijklmn,"abcdefghijklmn");
01054         overtgt = KTextabcdefghijklmn;
01055 
01056                                 // Format string contains just literal
01057                                 // text and no embedded commands.
01058                                 // Length of literal text is 3
01059                                 // 14+3 > max length of "overtgt" and 
01060                                 // so theoverflow.Overflow() will be invoked
01061         
01062         _LIT(KTextopq,"opq");
01063         overtgt.AppendFormat(KTextopq,&theoverflow);
01064 
01065                                 // NB omitting the 2nd parameter, giving:
01066                                 // overtgt.AppendFormat(KTextopq);
01067                                 // results in a panic.
01068     }
01069         
01070 
01071 
01072 
01073 
01074 
01075 
01076         
01077         

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