Symbian3/SDK/Source/GUID-EA107811-240C-5F95-922B-7C042FA00A87.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 13 Aug 2010 16:47:46 +0100
changeset 14 578be2adaf3e
parent 13 48780e181b38
permissions -rw-r--r--
Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582

<?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 task
  PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd">
<task id="GUID-EA107811-240C-5F95-922B-7C042FA00A87" xml:lang="en"><title>Reading a String Resource </title><abstract><p>The simplest use of a resource file involves reading
string resources and interpreting the source data. </p></abstract><prolog><metadata><keywords/></metadata></prolog><taskbody>
<context id="GUID-FD651A99-73F0-4CDD-9B4F-7C3A1DE8C4BE"><p>Resources are defined in terms of structs which are accessed
and read by the <xref href="GUID-AA6DEF11-6F23-3523-A9F7-F15DF918E1BA.dita"><apiname>RResourceFile</apiname></xref> class, and interpreted
by the <xref href="GUID-B375D32E-1F91-30FA-8605-A2E163111F9A.dita"><apiname>TResourceReader</apiname></xref>. An application may also
use resources from multiple files simultaneously. See <xref href="GUID-98903A01-CD04-5345-84AE-2E440CE06E11.dita">Resource File examples</xref> for more information. </p><p>Consider a sample resource file where
a struct of STRING is defined having a single member of type <codeph>LTEXT</codeph>: </p><codeblock xml:space="preserve">// define structures
STRUCT STRING
    {
    LTEXT text;
    }
// define resources
RESOURCE STRING hello
    {
    text=Bonjour tout le monde!;
    }
</codeblock> </context>
<steps id="GUID-F147E8CF-65D1-5025-BA85-E0D171DBEFFD">
<step id="GUID-0BAEB54B-5A6E-59F6-B14E-FAF0A3D15F27"><cmd/>
<info>Compile the resource file using the resource compiler (rcomp)
to generate an <filepath>.rsc</filepath> file and an <filepath>.rsg</filepath> file. </info>
<info><p>For information on resource compiler, see <b>Symbian^3 Tools
Guide &gt; Building</b>. </p></info>
<substeps id="GUID-0B421705-A5CD-5FF7-BB95-C17559811267">
<substep id="GUID-673E32C9-665C-5388-AE17-780A149F8837"><cmd/>
<info> <filepath>.rsc</filepath> file contains the resource data;
this is the resource file that must be referred to at run-time by
the <codeph>RResourceFile</codeph> class in the C++ code. </info>
</substep>
<substep id="GUID-AEE254EB-0716-5478-B1DF-F8ED8E4FF8E1"><cmd/>
<info> <filepath>.rsg</filepath> file is a <keyword>generated    
                 header</keyword> file that contains <codeph>#define</codeph> statements for each resource defined in the source file. In the
resource file generated here, the only resource is called <codeph>hello</codeph> and the generated header file contains: </info>
<stepxmp><codeblock id="GUID-385BB8E3-FF77-5A95-8F65-043E72412959" xml:space="preserve">#define HELLO 1</codeblock> </stepxmp>
<info>Note that the name in the generated header file is converted
to upper case. </info>
</substep>
</substeps>
</step>
<step id="GUID-3367B1DD-C2E9-5780-A77A-7F574D2EC541"><cmd/>
<info> <codeph>#include</codeph> the <filepath>.rsg</filepath> file
in the file containing the C++ code, to access the resource IDs generated
by the resource compiler. For example, for a project refered as <codeph>ReadText</codeph>, this might be: </info>
<stepxmp><codeblock id="GUID-AC615F67-6331-500C-B91E-ADD6A94464FB" xml:space="preserve">#include ReadText.rsg</codeblock> </stepxmp>
</step>
<step id="GUID-ACC66DA2-4AC8-536B-9912-7CE632C0A8DF"><cmd/>
<info>Initialize the <codeph>RResourceFile</codeph> object in the
C++ program, specifying the name of the resource file: </info>
<stepxmp><codeblock id="GUID-5CF79231-E42C-51DF-9002-FD1ED8AB21C1" xml:space="preserve">    
RResourceFile resourceFile;
resourceFile.OpenL( fsSession,_L( Z:\\system\\data\\ReadText.rsc ) );</codeblock> </stepxmp>
<info>Note: To access the resource file contents, a session with the
file server must be started using an instance of <xref href="GUID-E263C747-946F-35AA-9F1D-41833BD350FC.dita"><apiname>RFs</apiname></xref> class. </info>
</step>
<step id="GUID-CA53A8D1-E13E-50A8-96A7-B6B397B82DB3"><cmd/>
<info>Use one of the three functions (<xref href="GUID-AA6DEF11-6F23-3523-A9F7-F15DF918E1BA.dita#GUID-AA6DEF11-6F23-3523-A9F7-F15DF918E1BA/GUID-9B16E6B8-84A4-37DF-BBB4-EC8B04FDB14C"><apiname>RResourceFile::AllocReadLC()</apiname></xref>, <xref href="GUID-AA6DEF11-6F23-3523-A9F7-F15DF918E1BA.dita#GUID-AA6DEF11-6F23-3523-A9F7-F15DF918E1BA/GUID-8CEE29E0-78F3-3DD6-B068-85C8E0403884"><apiname>RResourceFile::AllocReadL()</apiname></xref> or <xref href="GUID-AA6DEF11-6F23-3523-A9F7-F15DF918E1BA.dita#GUID-AA6DEF11-6F23-3523-A9F7-F15DF918E1BA/GUID-DF6DFFD5-E923-3C4E-87F0-BC4EE7C8B586"><apiname>RResourceFile::ReadL()</apiname></xref> to read a resource as shown in the following code fragment: </info>
<stepxmp><codeblock id="GUID-92AC4270-4A4D-52F1-B5C4-CFF6FBD19700" xml:space="preserve">HBufC8* dataBuffer = resourceFile.AllocReadLC( HELLO );</codeblock> </stepxmp>
</step>
<step id="GUID-77F9A976-6E09-5158-B515-256F4B878CDB"><cmd/>
<info>Interpret the resource data using a <codeph>TResourceReader</codeph> object. This provides access to the text string through a pointer
descriptor as shown in the following code fragment: </info>
<stepxmp><codeblock id="GUID-CB3BE44D-72F5-5E15-8D1D-52F513D9D980" xml:space="preserve">
TResourceReader theReader;
...
theReader.SetBuffer( datafBuffer );
TPtrC textdata = reader.ReadTPtrC();</codeblock> </stepxmp>
</step>
</steps>
<result id="GUID-565451D8-6C9E-4F1C-BC15-BFA4F3DD6D1D"><p>In this example, once the resource data is no longer needed,
the heap descriptor, dataBuffer, can be removed from the cleanup stack
and destroyed as shown in the code fragment: </p><codeblock xml:space="preserve">CleanupStack::PopAndDestroy();</codeblock><p>When all operations on the resource file are complete, the resource
file can be closed using the RResourceFile::close() function as:  </p><codeblock xml:space="preserve">resourceFile.Close();</codeblock></result>
<example><title>Example</title> <p>Consider a resource constructed
from the following definition. </p> <codeblock id="GUID-538D959C-A85A-5675-9B14-ED81AB860F47" xml:space="preserve">RESOURCE ARRAY anarray
{
items=
    {
    LBUF { txt="Esc"; },
    LBUF { txt="Enter"; },
    LBUF { txt="Tab"; },
    LBUF { txt="Del"; },
    LBUF { txt="Space"; }
    };
}</codeblock> <p>A <codeph>TPtrC</codeph> representing the second
item can be constructed using the <codeph>ReadTPtrC()</codeph> function.
The example simply takes the length of the text <codeph>Enter</codeph>: </p> <codeblock id="GUID-9817910D-0661-513D-BB3A-9557D70688AD" xml:space="preserve">// open the resource file
...
HBufC8* res = resourceFile.AllocReadLC( ANARRAY );
TResourceReader theReader;
...
TInt len;
len = ( theReader.ReadTPtrC( 1,res ) ).Length(); // len == 5
...</codeblock> </example>
</taskbody><related-links>
<link href="GUID-3AF1F492-3D7D-5F8C-B1AE-16FBD8224775.dita"><linktext>Resource
files</linktext></link>
</related-links></task>