Symbian3/SDK/Source/GUID-FD273259-2282-5353-847D-853D483C37BC.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Tue, 20 Jul 2010 12:00:49 +0100
changeset 13 48780e181b38
parent 0 89d6a7a84779
permissions -rw-r--r--
Week 28 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 1897 and Bug 1522.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-FD273259-2282-5353-847D-853D483C37BC" xml:lang="en"><title>Using
TDesC8 Class</title><shortdesc>Use TDescC8 for interfaces which takes binary data or explicit
narrow text, regardless of the build variant, but which does not need to change
the data. </shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<section id="GUID-F1F7E9F9-0B79-5F45-9C67-587A74BBE209"><title>Using in a
function interface</title> <p>Interfaces which take binary data or narrow
text, use descriptors in the specification of that interface. All 8 bit concrete
descriptors are derived from <codeph>TDesC8</codeph> which means that the
interface can accept any 8 bit descriptor.</p> <p>The following code fragment
shows the most common function prototype pattern.</p> <codeblock id="GUID-2679659B-7A39-5C43-8B18-878BDE54AD3E" xml:space="preserve">void ClassX::foo(const TDesC8&amp; anArg);</codeblock> <p>The use of <codeph>TDesC8</codeph> ensures that the data cannot be modified
through the descriptor; <codeph>const</codeph> is an extra guarantee that
the data cannot be changed.</p> <p>In practice, nearly all code uses the build
independent variant, <codeph>TDesC</codeph>, unless an explicit 8 bit or 16
bit build variant is required.</p> </section>
<section id="GUID-2342D6CC-9781-5353-A2D4-EBA6FAC6E0E2"><title>Extract leftmost
part of data</title> <p>The code fragment shows how the leftmost part of data
in a descriptor can be accessed, using the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-0593A69B-B19A-3D2C-861A-216892E60180"><apiname>TDesC8::Left()</apiname></xref> member
function.</p> <p>The behaviour is the same for the build independent variant, <xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with <xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref> and <xref href="GUID-6DF731E4-5691-31C4-BEE0-03A3873F15EC.dita"><apiname>TPtrC8</apiname></xref> with <xref href="GUID-5CD07A27-E3ED-3273-A560-680501468C91.dita"><apiname>TPtrC</apiname></xref>.</p> <codeblock id="GUID-2707349F-04BD-59BC-8639-4794F506AFD0" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;8&gt; str(KData);
...
str.Left(4);</codeblock> <p>The call to <codeph>Left()</codeph> returns a
non-modifiable pointer descriptor representing the data string "<codeph>abcd</codeph>";
this has length 4. The original data contained in, and represented by, the
non-modifiable buffer descriptor <codeph>str</codeph>, is not changed in any
way.</p> <codeblock id="GUID-13D0C4D6-1534-56AF-B32E-4DB886555C69" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;8&gt; str(KData);
...
str.Left(256);</codeblock> <p>This call to <codeph>Left()</codeph> returns
a non-modifiable pointer descriptor representing the data string "<codeph>abcdefg</codeph>",
i.e. the whole content of the descriptor <codeph>str</codeph>; this has length
7.</p> <p>Note that the following call to <codeph>Left()</codeph> results
in a panic.</p> <codeblock id="GUID-1C8BAE4F-E649-5950-BD1C-2C7ED471B443" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;8&gt; str(KData);
...
str.Left(-1);           // Panic !</codeblock> </section>
<section id="GUID-1842A134-AFBF-5282-8052-424C7BF66963"><title>Extract rightmost
part of data</title> <p>The code fragment shows how the rightmost part of
data in a descriptor can be accessed, using the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-4113AD10-171C-38C0-B3B5-93FF0BFCBBC5"><apiname>TDesC8::Right()</apiname></xref> member
function.</p> <p>The behaviour is the same for the build independent variant, <xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with <xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref> and <xref href="GUID-6DF731E4-5691-31C4-BEE0-03A3873F15EC.dita"><apiname>TPtrC8</apiname></xref> with <xref href="GUID-5CD07A27-E3ED-3273-A560-680501468C91.dita"><apiname>TPtrC</apiname></xref>.</p> <codeblock id="GUID-146EBCF8-AE0A-52BD-AC1D-25A61B223AE6" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;8&gt; str(KData);
...
str.Right(4);</codeblock> <p>The call to <codeph>Right()</codeph> returns
a non-modifiable pointer descriptor representing the data string "<codeph>defg</codeph>";
this has length 4. The original data contained in, and represented by, the
non-modifiable buffer descriptor <codeph>str</codeph>, is not changed in any
way.</p> <codeblock id="GUID-79443C9B-FBE6-5B2F-B1BB-9610D6CB90A5" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;8&gt; str(KData);
...
str.Right(256);</codeblock> <p>This call to <codeph>Right()</codeph> returns
a non-modifiable pointer descriptor representing the data string "<codeph>abcdefg</codeph>",
i.e. the whole content of the descriptor <codeph>str</codeph>; this has length
7.</p> <p>Note that the following call to <codeph>Right()</codeph> results
in a panic.</p> <codeblock id="GUID-D9BF00DD-87BB-5125-B8F0-35B28D6E6E14" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;8&gt; str(KData);
...
str.Right(-1);          // Panic !</codeblock> </section>
<section id="GUID-C72FC6C5-5D89-5797-B1E1-F8BD913F1CC7"><title>Extract middle
portion of the data</title> <p>The code fragment shows how a portion of data
within a descriptor can be accessed, using the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-E02A80AB-4479-376A-82FC-099BA20F6A01"><apiname>TDesC8::Mid()</apiname></xref> member
function. Each call to<codeph>Mid()</codeph> returns a non-modifiable pointer
descriptor representing the selected portions of data.</p> <p>The behaviour
is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>, replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref> and <xref href="GUID-6DF731E4-5691-31C4-BEE0-03A3873F15EC.dita"><apiname>TPtrC8</apiname></xref> with<xref href="GUID-5CD07A27-E3ED-3273-A560-680501468C91.dita"><apiname>TPtrC</apiname></xref>.</p> <codeblock id="GUID-80455C29-5126-5C80-8DCC-8135AC7A23E2" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8 str(KData);
...
str.Mid(0);   //returns TPtrC8 representing "abcdefg";  length is 7
str.Mid(1);   //returns TPtrC8 representing "bcdefg";   length is 6
str.Mid(6);   //returns TPtrC8 representing "g";        length is 1
str.Mid(3,3); //returns TPtrC8 representing "def";      length is 3
str.Mid(0,7); //returns TPtrC8 representing "abcdefg";  length is 7
...
str.Mid(8);   // Panics !
str.Mid(3,5); // Panics !</codeblock> </section>
<section id="GUID-884F8704-067A-5DF2-9FCE-7AC6FC8A2CD2"><title>Comparing data</title> <p>This
code fragment shows the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-1C79CC6C-9C60-3C91-84C0-84363B5A7B70"><apiname>TDesC8::Compare()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-E09FF7E7-8954-516E-AC1F-00F1B0675D19" xml:space="preserve">_LIT8(Kabcd,  "abcd");
_LIT8(Kabcde, "abcde");
_LIT8(Kabc,   "abc");
_LIT8(Kabcx,  "abcx");
...
TBufC8&lt;8&gt; str(Kabcd);
...
str.Compare(Kabcde);    // returns -ve
str.Compare(Kabc);      // returns +ve
str.Compare(Kabcd);     // returns zero
str.Compare(Kabcx);     // returns -ve</codeblock> <p>This result of the comparison
means that:</p> <ul>
<li id="GUID-CE717629-1A33-5C6F-BBC0-F69B86297D8F"><p>"abcd" is less than
"abcde".</p> </li>
<li id="GUID-22640DC7-3446-5310-AC92-9E04ACE2B8BF"><p>"abcd" is greater than
"abc".</p> </li>
<li id="GUID-4A5A34DB-9B24-5F9B-A4CF-B07D5D93DDF0"><p>"abcd" is equal to "abcd".</p> </li>
<li id="GUID-71EF4AC1-8C0B-5A4A-8484-DBD6D0551A6C"><p>"abcd" is less than
"abcx".</p> </li>
</ul> </section>
<section id="GUID-F8DC34F6-C155-5085-9E76-2135603A802B"><title>Locating a
character</title> <p>This code fragment shows the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-2D247FCC-C975-39C4-947F-A1E89C273033"><apiname>TDesC8::Locate()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-A03FF85A-2A87-5DE3-81B9-BF1FB050CF1F" xml:space="preserve">_LIT8(Kabcd,"abcd");
TBufC8&lt;8&gt; str(Kabcd);
...
str.Locate('d');   // returns 3
str.Locate('a');   // returns 0
str.Locate('b');   // returns 1
str.Locate('x');   // returns KErrNotFound</codeblock> </section>
<section id="GUID-F7BEE7D3-AF21-5774-BFC1-97C8CFFE08ED"><title>Finding data</title> <p>This
code fragment shows the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-1D939364-6C89-3CAA-BC0D-2F20BFBCF725"><apiname>TDesC8::Find()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-94D64D41-0247-57AD-85D7-08B91686602A" xml:space="preserve">_LIT8(KAtoZ,"abcdefghijklmnopqrstuvwxyz");
TBufC8&lt;32&gt; str(KAtoZ);
...
_LIT8(KFind1,"abc");
str.Find(KFind1);            // returns 0

_LIT8(KFInd2,"bcde");
str.Find(KFInd2);            // returns 1

_LIT8(KFind3,"uvwxyz");
str.Find(KFind3);            // returns 20

_LIT8(KFind4,"0123");
str.Find(KFind4);            // returns KErrNotFound

_LIT8(KFind5,"abcdefghijklmnopqrstuvwxyz01");
str.Find(KFind5);            // returns KErrNotFound

str.Find(KNullDesC8);        // returns 0</codeblock> </section>
<section id="GUID-D192C3B7-1418-55BE-B70D-143D3FDD82DB"><title>Pattern matching</title> <p>This
code fragment shows the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-9BCB8339-2937-362D-85FF-3E5AE3A2CAFD"><apiname>TDesC8::Match()</apiname></xref> function.</p> <p>The
behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-9B7216C7-F4BD-551F-895B-A8D6D9F8CC28" xml:space="preserve">_LIT8(KAtoZ,"abcdefghijklmnopqrstuvwxyz");
TBufC8&lt;32&gt; str(KAtoZ);
    ...
_LIT8(KMatch1,"*ijk*");
str.Match(KMatch1);          //returns -&gt; 8

_LIT8(KMatch2,"*i?k*");
str.Match(KMatch2);          //        -&gt; 8

_LIT8(KMatch3,"ijk*");
str.Match(KMatch3);          //        -&gt; KErrNotFound

_LIT8(KMatch4,"abcd");
str.Match(KMatch4);          //        -&gt; KErrNotFound

_LIT8(KMatch5,"*i*mn*");
str.Match(KMatch5);          //        -&gt; 8

_LIT8(KMatch6,"abcdef*");
str.Match(KMatch6);          //        -&gt; 0

_LIT8(KMatch7,"*");
str.Match(KMatch7);          //        -&gt; 0

_LIT8(KMatch8,"*y*");
str.Match(KMatch8);          //        -&gt; 24

_lit8(KMatch9,"*i??k*");
str.Match(KMatch9);          //        -&gt; KErrNotFound</codeblock> <p>To test
for the existence of a pattern within a text string, the pattern must start
and end with an '<codeph>*</codeph>'.</p> </section>
<section id="GUID-B53507BB-D45B-5440-A025-B9690E15FACD"><title>Referencing
a data item</title> <p>The code fragment shows how a data item can be referenced
using<codeph>operator[]()</codeph>.</p> <p>The behaviour is the same for the
build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>, replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>.</p> <codeblock id="GUID-8C4F4C8F-303F-5690-949B-851EF3BB52DE" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;8&gt; str(KData);
...
str[0];                 // returns reference to 'a'
str[3];                 // returns reference to 'd'
str[7];                 // Panics !!</codeblock> </section>
<section id="GUID-83CFA0F7-A209-5E69-BE6B-4D5B17626202"><title>Creating a
heap descriptor</title> <p>The code fragments show how a heap descriptor is
created from an existing descriptor using the <xref href="GUID-FB97E0A3-352A-316F-97C6-69E4741A8120.dita#GUID-FB97E0A3-352A-316F-97C6-69E4741A8120/GUID-E6A17C44-EADD-3224-A16A-96263F16D004"><apiname>TDesC8::AllocL()</apiname></xref> member
function.</p> <p>The behaviour is the same for the build independent variant,<xref href="GUID-52D07F46-2162-380C-A775-C3BB335C42F5.dita"><apiname>TDesC</apiname></xref>,
replacing <xref href="GUID-964AAE0D-DA22-399E-8EFB-5DB1B58DA0A4.dita"><apiname>_LIT8</apiname></xref> with <xref href="GUID-B47B3E37-4FAC-327C-B626-49E09DB147FC.dita"><apiname>_LIT</apiname></xref>, <xref href="GUID-5B1CA2E7-E3A7-3AF8-9EB0-662E130C45DA.dita"><apiname>TBufC8</apiname></xref> with<xref href="GUID-4D64E4B7-6BEE-3900-A115-460FE5B2D79E.dita"><apiname>TBufC</apiname></xref>,
and<xref href="GUID-2A528453-0279-3E47-838C-F8A8D29B88F1.dita"><apiname>HBufC8</apiname></xref> with<xref href="GUID-A103FB19-60B3-3E45-97A5-1F295934ACA1.dita"><apiname>HBufC</apiname></xref>.</p> <codeblock id="GUID-6AF24F59-9224-59F0-84A2-9766EAFBCF79" xml:space="preserve">_LIT8(KData,"abcdefg");
TBufC8&lt;16&gt; str(KData);
...
HBufC8* ptr;
...
ptr = str.AllocL();     //Creates and returns address of
...                     //heap descriptor. The new heap descriptor
...                     //contains a copy of the original data.
ptr-&gt;Length();           //Returns 7; the length of "abcdfeg"</codeblock> </section>
</conbody><related-links>
<link href="GUID-7830BAAB-40DD-5E55-84B5-8DCA888E68E7.dita#GUID-7830BAAB-40DD-5E55-84B5-8DCA888E68E7/GUID-235E272D-6F5A-5740-8150-9698C8C8D55D">
<linktext>Using TDesC                   in a function interface</linktext>
</link>
</related-links></concept>