Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]

#include <tultextresourceutils.h>
Link against: etul.lib
This item is not part of the S60 5th Edition SDK

Class TulTextResourceUtils

class TulTextResourceUtils;

Description

Utility that provides methods to load and format resource strings. String Loader API provides an interface to load and format resource strings that may contain parameter(s) (%U for (unicode) text or or %N for numerical). Resource strings are usually defined in an RSS file.

The API consists of the TulTextResourceUtils class. All methods are static, so there is no need to explicitly allocate memory for the interface class. The implementation needs a CCoeEnv instance to access for example the resource files.

Usage:

Applications load and format resource strings from normal resources with static methods of the TulTextResourceUtils class. The loading is done with the LoadL and LoadLC methods and with the Load method in situations where memory allocation from the heap is not possible. Formatting is done automatically after loading in the LoadL and LoadLC methods, but it can also be done separately with the Format method in situations where memory allocation from the heap is not possible. For reading the resource strings with the Load, LoadL and LoadLC methods, the user should provide a pointer to CCoeEnv for efficiency reasons. If the pointer is not provided, the implementation uses the CCoeEnv::Static() method internally to get it.

Different size displays can handle different length strings. To take full advantage of this fact, TulTextResourceUtils supports resource strings with multiple options for strings, separated by the character 0x0001. Each such string can contain the same or different sub string keys (%U and %N). TulTextResourceUtils returns all strings, it is the responsibility of the caller to parse the result and choose the proper string to display.

Setting the maximum sub string length may be done in the text resources. Sub string maximum lengths can be localized separately for every language. The maximum sub string length is of the format: %U[NN] where NN is a number [01..99]. Please note that NN must always consist of two characters, i.e. if the sub string maximum length is eight characters, the value to be used is 08, not plain 8. If the number of characters exceeds the maximum length, the sub string is cut to fit and the last character is replaced with an ellipsis character.

The following examples describe the usage of the String Loader API.

Usage when one TInt is added:

 // In .loc -file
 // #define text_example "You have %N undone tasks."

 // In .rss -file
 // RESOURCE TBUF r_text_example { buf = text_example; }

 // (In the .cpp -file)
 #include <coeutils.h>

 // Get CCoeEnv instance
 CEikonEnv* iEikonEnv = CEikonEnv::Static();

 TInt number(324);

 // Method reads a resource string with memory allocation
 // and replaces the first %N-string in it with replacement TInt.
 // The heap descriptor must be destroyed when it is no longer needed.
 // iEikonEnv is needed for loading the resource string.
 HBufC* stringholder = TulTextResourceUtils::LoadL(R_TEXT_EXAMPLE, number, iEikonEnv);

 // The 'number' is added to the resource string. The result is
 // that stringholder points to a heap descriptor containing string:
 // "You have 324 undone tasks."

 // Delete the heap descriptor
 delete stringholder;

Usage when several strings are added:

An index can be included to parameters. Several parameters can have same index if the same replacement is needed multiple times.

 // In .loc -file
 // #define text_example "I'm %2U%1U %3U%0U fine."

 // In .rss -file
 // RESOURCE TBUF r_text_example { buf = text_example; }

 // In the .cpp -file
 #include <coeutils.h>

 // Get CCoeEnv instance
 CEikonEnv* iEikonEnv = CEikonEnv::Static();

 CDesCArrayFlat* strings = new CDesCArrayFlat(4);
 CleanupStack::PushL(strings);

 strings->AppendL(_L("orking")); //First string

 strings->AppendL(_L("ll")); //Second string

 strings->AppendL(_L("sti")); //Third string

 strings->AppendL(_L("w")); //Fourth string

 // Method reads a resource string with memory allocation and replaces
 // the %(index)U strings in it with replacement strings from an array.
 // The heap descriptor must be destroyed when it is no longer needed.
 // iEikonEnv is needed for loading the resource string.
 HBufC* stringholder = TulTextResourceUtils::LoadL(R_TEXT_EXAMPLE, *strings, iEikonEnv);

 // Four strings are added to the resource string. The result is
 // that stringholder points to a heap descriptor containing string:
 // "I'm still working fine."

 // Pop and delete strings array
 CleanupStack::PopAndDestroy();

 // Delete the heap descriptor
 delete stringholder;

Usage with scalable UI support:

 // In .loc -file
 // #define TEXT_EXAMPLE "You have missed %N messages from %U."<0x0001>"Missed %N msgs from %U."<0x0001>"Missed %N msgs."

 // In .rss -file
 // RESOURCE TBUF R_TEXT_EXAMPLE { buf = TEXT_EXAMPLE; }

 // In the .cpp -file
 #include <coeutils.h>

 // Get CCoeEnv instance
 CEikonEnv* iEikonEnv = CEikonEnv::Static();

 TInt number(12);
 _LIT(name, "John Doe");

 // Method reads a resource string with memory allocation,
 // replaces all %N strings in it with a replacement TInt and
 // all %U strings in it with a replacement string.
 // The heap descriptor must be destroyed  when it is no longer needed.
 // iEikonEnv is needed for loading the resource string.
 HBufC stringholder = TulTextResourceUtils::LoadL(R_TEXT_EXAMPLE, name, number, iEikonEnv);

 // The number and name are added to the resource string. The result is
 // that stringholder points to a heap descriptor containing string:
 // "You have missed 12 messages from John Doe.\001Missed 12 msgs from John
 // Doe.\001Missed 12 msgs."

 // Delete the heap descriptor
 delete stringholder;

Error handling:

The leave mechanism of the Symbian OS environment is used to handle memory exhaustion. The panic mechanism is used to handle programming errors while debugging. TulTextResourceUtils panics for seven different reasons. The panic category is named TulTextResourceUtils. The panic codes are:

Members

Defined in TulTextResourceUtils:


Member functions


Load(TDes &,TInt,CCoeEnv *)

IMPORT_C static void Load(TDes &aDest, TInt aResourceId, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string without memory allocation. The loaded string is stored in the destination TDes&. Because this method doesn't allocate memory the destination descriptor must be long enough.

Parameters

TDes16 &aDest

Reference to the descriptor where the resource string is loaded.

TInt aResourceId

The numeric ID of the resource string to be read.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Panic codes

ECCoeEnvNotInitialized

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.


Format(TDes &,const TDesC &,TInt,TInt)

IMPORT_C static void Format(TDes &aDest, const TDesC &aSource, TInt aPosition, TInt aSubs);

Description

Parameters

TDes16 &aDest

const TDesC16 &aSource

TInt aPosition

TInt aSubs


Format(TDes &,const TDesC &,TInt,const TDesC &)

IMPORT_C static void Format(TDes &aDest, const TDesC &aSource, TInt aPosition, const TDesC &aSubs);

Description

Formats a resource string without memory allocation. The formatted string is stored in the destination TDes&. Since this method doesn't allocate memory the destination descriptor must be long enough. In aPosition -1 means that there is no index in the key string and all %U-strings in the original string are replaced with aSubs. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

One small sample describing the usage of the method.

 // Load example string "%0U %1U" defined in rss- and loc-files.
 // %0U stands for weekday string and %1U for date string.
 HBufC* timeFormat = TulTextResourceUtils::LoadLC(R_TIME_FORMAT, iEikonEnv);

 // The replacing string.
 _LIT(dateString, "02/10/2006");

 TulTextResourceUtils::Format(destBuf, timeFormat, 
                       1, // %1U stands for date string
                       dateString);

 // After returning destBuf contains string "%0U 02/10/2006".

Parameters

TDes16 &aDest

Reference to the descriptor where the resource string is formatted.

const TDesC16 &aSource

Reference to the original string.

TInt aPosition

The index of the key string.

const TDesC16 &aSubs

Reference to the replacing string.

Panic codes

EInvalidIndex

In debug build if the index of the key string is invalid.

EDescriptorTooSmall

In debug build if the length of the destination descriptor is too small.

EKeyStringNotFound

In debug build if the key string 'U' wasn't found, aDest is empty.


LoadL(TInt,CCoeEnv *)

IMPORT_C static HBufC* LoadL(TInt aResourceId, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the resource string. The calling program must destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.


LoadL(TInt,TInt,CCoeEnv *)

IMPORT_C static HBufC* LoadL(TInt aResourceId, TInt aInt, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation and replaces the first %N-string in it with replacement TInt. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

TInt aInt

The replacing TInt.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. The calling program must destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

EKeyStringNotFound

In debug build if the key string 'N' wasn't found in formatting.


LoadL(TInt,const TDesC &,CCoeEnv *)

IMPORT_C static HBufC* LoadL(TInt aResourceId, const TDesC &aString, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation and replaces the first %U-string in it with replacement string. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const TDesC16 &aString

Reference to the replacing string.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. The calling program must destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

EKeyStringNotFound

In debug build if the key string 'U' wasn't found in formatting.


LoadL(TInt,const TDesC &,TInt,CCoeEnv *)

IMPORT_C static HBufC* LoadL(TInt aResourceId, const TDesC &aString, TInt aInt, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation, replaces the first %N-string in it with replacement TInt and the first %U-string in it with replacement string. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const TDesC16 &aString

Reference to the replacing string.

TInt aInt

The replacing TInt.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. The calling program must destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

EKeyStringNotFound

In debug build if the key string 'N' or 'U' wasn't found in formatting.


LoadL(TInt,const CArrayFix< TInt > &,CCoeEnv *)

IMPORT_C static HBufC* LoadL(TInt aResourceId, const CArrayFix< TInt > &aInts, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation and replaces the %(index)N-strings in it with replacement TInts from an array. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const CArrayFix< TInt > &aInts

Reference to the array including the replacing TInts.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. The calling program must destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

ETooManyArguments

In debug build if too many replacing elements in aInts array.


LoadL(TInt,const MDesC16Array &,CCoeEnv *)

IMPORT_C static HBufC* LoadL(TInt aResourceId, const MDesC16Array &aStrings, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation and replaces the %(index)U-strings in it with replacement strings from an array. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const MDesC16Array &aStrings

Reference to the array including pointers to the replacing strings.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. The calling program must destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

ETooManyArguments

In debug build if too many replacing elements in aStrings array.


LoadL(TInt,const MDesC16Array &,const CArrayFix< TInt > &,CCoeEnv *)

IMPORT_C static HBufC* LoadL(TInt aResourceId, const MDesC16Array &aStrings, const CArrayFix< TInt > &aInts, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation and replaces the %(index)U-strings in it with replacement strings from an array and the %(index)N-strings in it with replacement TInts from another array. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const MDesC16Array &aStrings

Reference to the array including pointers to the replacing strings.

const CArrayFix< TInt > &aInts

Reference to the array including the replacing TInts.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. The calling program must destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

ETooManyArguments

In debug build if too many replacing elements in aStrings and/or aInts arrays.


LoadLC(TInt,CCoeEnv *)

IMPORT_C static HBufC* LoadLC(TInt aResourceId, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation and pushes the string onto the cleanup stack.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the resource string. This pointer is in the cleanup stack. The calling program should pop and destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.


LoadLC(TInt,TInt,CCoeEnv *)

IMPORT_C static HBufC* LoadLC(TInt aResourceId, TInt aInt, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation, replaces the first %N-string in it with replacement TInt and pushes the string onto the cleanup stack. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

TInt aInt

the replacing TInt.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

pointer to a heap descriptor containing the formatted resource string. This pointer is in the cleanup stack. The calling program should pop and destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

EKeyStringNotFound

In debug build if the key string 'N' wasn't found in formatting.


LoadLC(TInt,const TDesC &,CCoeEnv *)

IMPORT_C static HBufC* LoadLC(TInt aResourceId, const TDesC &aString, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation, replaces the first %U-string in it with replacement string and pushes the string onto the cleanup stack. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const TDesC16 &aString

Reference to the replacing string.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. This pointer is in the cleanup stack. The calling program should pop and destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

EKeyStringNotFound

In debug build if the key string 'U' wasn't found in formatting.


LoadLC(TInt,const TDesC &,TInt,CCoeEnv *)

IMPORT_C static HBufC* LoadLC(TInt aResourceId, const TDesC &aString, TInt aInt, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation, replaces the first %N-string in it with replacement TInt and the first %U-string in it with replacement string and pushes the string onto the cleanup stack. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const TDesC16 &aString

Reference to the replacing string.

TInt aInt

The replacing TInt.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. This pointer is in the cleanup stack. The calling program should pop and destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

EKeyStringNotFound

In debug build if the key string 'N' or 'U' wasn't found in formatting.


LoadLC(TInt,const CArrayFix< TInt > &,CCoeEnv *)

IMPORT_C static HBufC* LoadLC(TInt aResourceId, const CArrayFix< TInt > &aInts, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation, replaces the %(index)N-strings in it with replacement TInts from an array and pushes the string onto the cleanup stack. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const CArrayFix< TInt > &aInts

Reference to the array including the replacing TInts.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. This pointer is in the cleanup stack. The calling program should pop and destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

ETooManyArguments

In debug build if too many replacing elements in aInts array.


LoadLC(TInt,const MDesC16Array &,CCoeEnv *)

IMPORT_C static HBufC* LoadLC(TInt aResourceId, const MDesC16Array &aStrings, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation, replaces the %(index)U-strings in it with replacement strings from an array and pushes the string onto the cleanup stack. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const MDesC16Array &aStrings

Reference to the array including pointers to the replacing strings.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. This pointer is in the cleanup stack. The calling program should pop and destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

ETooManyArguments

In debug build if too many replacing elements in aStrings array.


LoadLC(TInt,const MDesC16Array &,const CArrayFix< TInt > &,CCoeEnv *)

IMPORT_C static HBufC* LoadLC(TInt aResourceId, const MDesC16Array &aStrings, const CArrayFix< TInt > &aInts, CCoeEnv *aLoaderEnv=0);

Description

Reads a resource string with memory allocation, replaces the %(index)U-strings in it with replacement strings from an array. Replaces the %(index)N-strings with replacement TInts from another array. Pushes the string onto the cleanup stack. In debug builds the Symbian OS panic mechanism is used to notify programming errors.

Parameters

TInt aResourceId

The numeric ID of the resource string to be read.

const MDesC16Array &aStrings

Reference to the array including pointers to the replacing strings.

const CArrayFix< TInt > &aInts

Reference to the array including the replacing TInts.

CCoeEnv *aLoaderEnv

Pointer to the control environment. If user doesn't give this, CCoeEnv::Static() is called to get it.

Return value

HBufC16 *

Pointer to a heap descriptor containing the formatted resource string. This pointer is in the cleanup stack. The calling program should pop and destroy the heap descriptor when it is no longer needed.

Leave codes

KErrNotSupported

Parameter aLoaderEnv is NULL and CCoeEnv::Static returned NULL.

Panic codes

ETooManyArguments

In debug build if too many replacing elements in aStrings and/or aInts arrays.