examples/Base/BufsAndStrings/Desc/Buffer/Buffer.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 the basic ideas of
00015 // buffer descriptors.
00016 //
00017 
00018 #include "CommonFramework.h"
00019 
00020 //
00021 // Common literal text
00022 //
00023 _LIT(KTxtHelloWorld,"Hello World!");
00024 _LIT(KTxtRepText,"Replacement text");
00025 _LIT(KTxtTBufC,"TBufC: ");
00026 _LIT(KTxtTPtr,"TPtr:  ");
00027 
00028 //
00029 // Common Format strings
00030 //
00031 _LIT(KCommonFormat2,"\"%S\"; ");
00032 _LIT(KCommonFormat3,"Descriptor at %x; Ptr()=%x; ");
00033 _LIT(KCommonFormat6,"\"%S\"; Ptr()=%x; Length()=%d; Size()=%d\n");
00034 _LIT(KCommonFormat7,"\"%S\"; Ptr()=%x; Length()=%d; Size()=%d; ");
00035 _LIT(KCommonFormat8,"\nMaxLength()=%d\n");
00036 _LIT(KCommonFormat9,"Length()=%d; Size()=%d;\n");
00037 _LIT(KCommonFormat10,"MaxLength()=%d\n");
00038 
00039 
00040 LOCAL_C void doExampleL()
00041     {                              
00042                                 // Set up an area and initialise to a 
00043                                 // C style string (including the NULL).
00044         TText cstr[13] =  {'H', 'e' ,'l' ,'l' ,'o', ' ',
00045                                        'W', 'o','r', 'l', 'd', '!', '\0'};
00046 
00047                                 // Construct a TBufC using the NULL 
00048                                 // terminated string in cstr to initialise
00049                                 // it.
00050         TBufC<16> bufc1(&cstr[0]);
00051 
00052                                 // Look at the address of the C string
00053         _LIT(KFormat1,"C string at %x; \n");
00054         console->Printf(KFormat1,&cstr[0]);
00055                                                                                                         
00056                                 // Look at: 
00057                                 //   1. Descriptor content
00058                                 //   2. Address of descriptor
00059                                 //   3. Address of descriptor data area
00060                                 //   4. Length of descriptor
00061                                 //   5. Size of descriptor
00062                                 //         
00063                                 // Address of descriptor data area is 
00064                                 // different from the address of cstr but
00065                                 // is offset 4 from the start of the 
00066                                 // descriptor itself.
00067                                 //
00068                                 // Descriptor length is 12.
00069                                 //
00070                                 // The template parameter value defines 
00071                                 // the length of the descriptor data area 
00072                                 // and, therefore, governs its size 
00073                                 // (depending on the build variant).
00074                                 // Size of data is 24
00075         console->Printf(KCommonFormat2,&bufc1);
00076         console->Printf(KCommonFormat3,&bufc1,bufc1.Ptr());
00077         _LIT(KFormat4,"Length()=%d; Size()=%d\n");
00078         console->Printf(KFormat4,bufc1.Length(),bufc1.Size());  
00079                                                                                         
00080                                 // If the TBufC is to hold string data on
00081                                 // construction, use a _LIT macro.
00082         TBufC<16> bufc2(KTxtHelloWorld);
00083 
00084                                 // Cannot modify existing data but can replace
00085                                 // it entirely using assignment operator. 
00086                                 // The replacement text must not have a length 
00087                                 // greater than 16
00088         bufc2 = KTxtRepText;
00089         _LIT(KFormat5,"\"%S\"; Length()=%d; Size()=%d\n");
00090         console->Printf(KFormat5,&bufc2,bufc2.Length(),bufc2.Size());
00091 
00092                                 // Replacing text which has a length > 16
00093                                 // causes panic !!
00094                                 // 
00095                                 // Remove the "//" marks on the next two lines
00096                                 // to see this happen
00097         //_LIT(KTxtRepTextPanic,"Text replacement causes panic");
00098         //bufc2 = KTxtRepTextPanic;
00099     
00100         
00101                                 // The Des() function returns a TPtr to the
00102                                 // TBufC.
00103                                 // The TBufC data can be changed through 
00104                                 // the TPtr.
00105                                 // The maximum length of the TPtr is the 
00106                                 // value of the TBufC template parameter,
00107                                 // i.e. 16 
00108 
00109         bufc2 = KTxtHelloWorld;
00110         TPtr ptr = bufc2.Des();
00111         
00112         console->Printf(KTxtTBufC);
00113         console->Printf(KCommonFormat6,
00114                                         &bufc2,
00115                                         bufc2.Ptr(),
00116                                         bufc2.Length(),
00117                                         bufc2.Size()
00118                                    );
00119         
00120         console->Printf(KTxtTPtr);
00121         console->Printf(KCommonFormat7,
00122                                         &ptr,
00123                                         ptr.Ptr(),
00124                                         ptr.Length(),
00125                                         ptr.Size()
00126                                    );
00127 
00128         console->Printf(KCommonFormat8,ptr.MaxLength());
00129 
00130                                 // Now change the TBufC data through
00131                                 // the TPtr. This is OK provided the length
00132                                 // of the changed data does not exceed 16.
00133                                 //
00134                                 // The following change deletes the last 
00135                                 // character (the "!") and appends 
00136                                 // the characters " & Hi".
00137                                 //
00138                                 // Note that the length of both the TBufC 
00139                                 // and the TPtr reflect the changed data.
00140         _LIT(KTxtAndHi," & Hi");
00141         ptr.Delete((ptr.Length()-1),1);
00142         ptr.Append(KTxtAndHi);
00143 
00144         console->Printf(KTxtTBufC);
00145         console->Printf(KCommonFormat6,
00146                                         &bufc2,
00147                                         bufc2.Ptr(),
00148                                         bufc2.Length(),
00149                                         bufc2.Size()
00150                                    );
00151 
00152         console->Printf(KTxtTPtr);
00153         console->Printf(KCommonFormat7,
00154                                         &ptr,
00155                                         ptr.Ptr(),
00156                                         ptr.Length(),
00157                                         ptr.Size()
00158                                    );
00159         console->Printf(KCommonFormat8,ptr.MaxLength());
00160 
00161         // * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00162         // * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00163         
00164         _LIT(KTxtBasicConcepts,"\n-->TBuf basic concepts");
00165         console->Printf(KTxtBasicConcepts);
00166         _LIT(KTxtPressToContinue," (press any key to continue)\n");
00167         console->Printf(KTxtPressToContinue);
00168         console->Getch();
00169 
00170                                 // Construct a TBuf using a Literal                        
00171         TBuf<16> buf(KTxtHelloWorld);
00172         
00173                                 // Look at: 
00174                                 //   1. Descriptor content
00175                                 //   2. Address of descriptor
00176                                 //   3. Address of descriptor data area
00177                                 //   4. Length of descriptor
00178                                 //   5. Size of descriptor
00179                                 //   6. Maximum length of descriptor      
00180                                 //
00181                                 // Like a TBufC, the address of the descriptor
00182                                 // data area is offset 4 from the start of the
00183                                 // descriptor itself.
00184                                 //
00185                                 // Descriptor length is 12.
00186                                 //
00187                                 // The template parameter value defines 
00188                                 // the maximum length of the descriptor. 
00189                                 // and, therefore, governs its size 
00190                                 // (depending on the build variant).
00191                                 // Size of data is 24
00192                                                                 
00193         console->Printf(KCommonFormat2,&buf);
00194         console->Printf(KCommonFormat3,&buf,buf.Ptr());
00195         console->Printf(KCommonFormat9,buf.Length(),buf.Size());        
00196         console->Printf(KCommonFormat10,buf.MaxLength());
00197                                                 
00198                                 // The data can be modified
00199         buf.Append('@');
00200         console->Printf(KCommonFormat2,&buf);
00201         console->Printf(KCommonFormat9,buf.Length(),buf.Size());        
00202         console->Printf(KCommonFormat10,buf.MaxLength());
00203 
00204                             // Length can be changed; data represented
00205                             // by the descriptor is now "Hel"
00206         buf.SetLength(3);
00207         console->Printf(KCommonFormat2,&buf);
00208         console->Printf(KCommonFormat9,buf.Length(),buf.Size());        
00209         console->Printf(KCommonFormat10,buf.MaxLength());
00210 
00211                                 // Length can be zeroised; no data is now 
00212                                 // represented by the descriptor but 
00213                                 // the maximum length is still 16
00214         buf.Zero();
00215         console->Printf(KCommonFormat2,&buf);
00216         console->Printf(KCommonFormat9,buf.Length(),buf.Size());        
00217         console->Printf(KCommonFormat10,buf.MaxLength());
00218                                                                                                           
00219                                 // The data can be replaced entirely 
00220                                 // using the assignment operator.
00221                                 // The replacement text must not have a
00222                                 // length greater than 16.
00223         buf = KTxtRepText;
00224         console->Printf(KCommonFormat2,&buf);
00225         console->Printf(KCommonFormat9,buf.Length(),buf.Size());        
00226         console->Printf(KCommonFormat10,buf.MaxLength());
00227         
00228                                 // Replacing text which has a length > 16
00229                                 // causes panic !!
00230                                 // 
00231                                 // Remove the "//" marks on the next two lines
00232                                 // to see this happen
00233         //_LIT(KTxtRepTextPanic,"Text replacement causes panic");
00234         //buf = _L("Text replacement causes panic!");
00235     }
00236 
00237 
00238 
00239 
00240 
00241         
00242         

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