epoc32/include/tultextresourceutils.h
author William Roberts <williamr@symbian.org>
Tue, 16 Mar 2010 16:12:26 +0000
branchSymbian2
changeset 2 2fe1408b6811
permissions -rw-r--r--
Final list of Symbian^2 public API header files
williamr@2
     1
/*
williamr@2
     2
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@2
     3
* All rights reserved.
williamr@2
     4
* This component and the accompanying materials are made available
williamr@2
     5
* under the terms of the License "Symbian Foundation License v1.0" to Symbian Foundation members and "Symbian Foundation End User License Agreement v1.0" to non-members
williamr@2
     6
* which accompanies this distribution, and is available
williamr@2
     7
* at the URL "http://www.symbianfoundation.org/legal/licencesv10.html".
williamr@2
     8
*
williamr@2
     9
* Initial Contributors:
williamr@2
    10
* Nokia Corporation - initial contribution.
williamr@2
    11
*
williamr@2
    12
* Contributors:
williamr@2
    13
*
williamr@2
    14
* Description:
williamr@2
    15
*
williamr@2
    16
*/
williamr@2
    17
williamr@2
    18
williamr@2
    19
williamr@2
    20
#ifndef __TULTEXTRESOURCEUTILS_H__
williamr@2
    21
#define __TULTEXTRESOURCEUTILS_H__
williamr@2
    22
williamr@2
    23
#include <e32std.h>
williamr@2
    24
#include <e32base.h>	// class CArrayFix
williamr@2
    25
#include <bamdesca.h>	// class MDesCArray
williamr@2
    26
#include <biditext.h>	// class TBidiText
williamr@2
    27
class CCoeEnv;
williamr@2
    28
williamr@2
    29
williamr@2
    30
/**
williamr@2
    31
Utility that provides methods to load and format resource strings.
williamr@2
    32
String Loader API provides an interface to load and format resource strings
williamr@2
    33
that may contain parameter(s) (\%U for (unicode) text or or \%N for numerical).
williamr@2
    34
Resource strings are usually defined in an RSS file.
williamr@2
    35
williamr@2
    36
The API consists of the TulTextResourceUtils class. All methods are static, so there is
williamr@2
    37
no need to explicitly allocate memory for the interface class.
williamr@2
    38
The implementation needs a CCoeEnv instance to access for example the
williamr@2
    39
resource files.
williamr@2
    40
williamr@2
    41
williamr@2
    42
Usage:
williamr@2
    43
williamr@2
    44
Applications load and format resource strings from normal resources with
williamr@2
    45
static methods of the TulTextResourceUtils class. The loading is done with the LoadL
williamr@2
    46
and LoadLC methods and with the Load method in situations where memory
williamr@2
    47
allocation from the heap is not possible. Formatting is done automatically
williamr@2
    48
after loading in the LoadL and LoadLC methods, but it can also be done
williamr@2
    49
separately with the Format method in situations where memory allocation from
williamr@2
    50
the heap is not possible. For reading the resource strings with the Load,
williamr@2
    51
LoadL and LoadLC methods, the user should provide a pointer to CCoeEnv for
williamr@2
    52
efficiency reasons. If the pointer is not provided, the implementation uses
williamr@2
    53
the CCoeEnv::Static method internally to get it.
williamr@2
    54
williamr@2
    55
Different size displays can handle different length strings. To take full
williamr@2
    56
advantage of this fact, TulTextResourceUtils supports resource strings with multiple
williamr@2
    57
options for strings, separated by the character 0x0001. Each such string can
williamr@2
    58
contain the same or different sub string keys (\%U and \%N). TulTextResourceUtils returns
williamr@2
    59
all strings, it is the responsibility of the caller to parse the result and
williamr@2
    60
choose the proper string to display.
williamr@2
    61
williamr@2
    62
Setting the maximum sub string length may be done in the text resources. Sub
williamr@2
    63
string maximum lengths can be localized separately for every language.
williamr@2
    64
The maximum sub string length is of the format: \%U[NN]
williamr@2
    65
where NN is a number [01..99]. Please note that NN must always consist of two
williamr@2
    66
characters, i.e. if the sub string maximum length is eight characters, the
williamr@2
    67
value to be used is 08, not plain 8. If the number of characters exceeds the
williamr@2
    68
maximum length, the sub string is cut to fit and the last character is
williamr@2
    69
replaced with an ellipsis character.
williamr@2
    70
williamr@2
    71
The following examples describe the usage of the String Loader API.
williamr@2
    72
williamr@2
    73
Usage when one TInt is added:
williamr@2
    74
williamr@2
    75
@code
williamr@2
    76
 // In .loc -file
williamr@2
    77
 // #define text_example "You have %N undone tasks."
williamr@2
    78
williamr@2
    79
 // In .rss -file
williamr@2
    80
 // RESOURCE TBUF r_text_example { buf = text_example; }
williamr@2
    81
williamr@2
    82
 // (In the .cpp -file)
williamr@2
    83
 #include <coeutils.h>
williamr@2
    84
williamr@2
    85
 // Get CCoeEnv instance
williamr@2
    86
 CEikonEnv* iEikonEnv = CEikonEnv::Static();
williamr@2
    87
williamr@2
    88
 TInt number(324);
williamr@2
    89
williamr@2
    90
 // Method reads a resource string with memory allocation
williamr@2
    91
 // and replaces the first %N-string in it with replacement TInt.
williamr@2
    92
 // The heap descriptor must be destroyed when it is no longer needed.
williamr@2
    93
 // iEikonEnv is needed for loading the resource string.
williamr@2
    94
 HBufC* stringholder = TulTextResourceUtils::LoadL(R_TEXT_EXAMPLE, number, iEikonEnv);
williamr@2
    95
williamr@2
    96
 // The 'number' is added to the resource string. The result is
williamr@2
    97
 // that stringholder points to a heap descriptor containing string:
williamr@2
    98
 // "You have 324 undone tasks."
williamr@2
    99
williamr@2
   100
 // Delete the heap descriptor
williamr@2
   101
 delete stringholder;
williamr@2
   102
@endcode
williamr@2
   103
williamr@2
   104
williamr@2
   105
Usage when several strings are added:
williamr@2
   106
williamr@2
   107
An index can be included to parameters. Several parameters can have same index
williamr@2
   108
if the same replacement is needed multiple times.
williamr@2
   109
williamr@2
   110
@code
williamr@2
   111
 // In .loc -file
williamr@2
   112
 // #define text_example "I'm %2U%1U %3U%0U fine."
williamr@2
   113
williamr@2
   114
 // In .rss -file
williamr@2
   115
 // RESOURCE TBUF r_text_example { buf = text_example; }
williamr@2
   116
williamr@2
   117
 // In the .cpp -file
williamr@2
   118
 #include <coeutils.h>
williamr@2
   119
williamr@2
   120
 // Get CCoeEnv instance
williamr@2
   121
 CEikonEnv* iEikonEnv = CEikonEnv::Static();
williamr@2
   122
williamr@2
   123
 CDesCArrayFlat* strings = new CDesCArrayFlat(4);
williamr@2
   124
 CleanupStack::PushL(strings);
williamr@2
   125
williamr@2
   126
 strings->AppendL(_L("orking")); //First string
williamr@2
   127
williamr@2
   128
 strings->AppendL(_L("ll")); //Second string
williamr@2
   129
williamr@2
   130
 strings->AppendL(_L("sti")); //Third string
williamr@2
   131
williamr@2
   132
 strings->AppendL(_L("w")); //Fourth string
williamr@2
   133
williamr@2
   134
 // Method reads a resource string with memory allocation and replaces
williamr@2
   135
 // the %(index)U strings in it with replacement strings from an array.
williamr@2
   136
 // The heap descriptor must be destroyed when it is no longer needed.
williamr@2
   137
 // iEikonEnv is needed for loading the resource string.
williamr@2
   138
 HBufC* stringholder = TulTextResourceUtils::LoadL(R_TEXT_EXAMPLE, *strings, iEikonEnv);
williamr@2
   139
williamr@2
   140
 // Four strings are added to the resource string. The result is
williamr@2
   141
 // that stringholder points to a heap descriptor containing string:
williamr@2
   142
 // "I'm still working fine."
williamr@2
   143
williamr@2
   144
 // Pop and delete strings array
williamr@2
   145
 CleanupStack::PopAndDestroy();
williamr@2
   146
williamr@2
   147
 // Delete the heap descriptor
williamr@2
   148
 delete stringholder;
williamr@2
   149
@endcode
williamr@2
   150
williamr@2
   151
williamr@2
   152
Usage with scalable UI support:
williamr@2
   153
williamr@2
   154
@code
williamr@2
   155
 // In .loc -file
williamr@2
   156
 // #define TEXT_EXAMPLE "You have missed %N messages from %U."<0x0001>"Missed %N msgs from %U."<0x0001>"Missed %N msgs."
williamr@2
   157
williamr@2
   158
 // In .rss -file
williamr@2
   159
 // RESOURCE TBUF R_TEXT_EXAMPLE { buf = TEXT_EXAMPLE; }
williamr@2
   160
williamr@2
   161
 // In the .cpp -file
williamr@2
   162
 #include <coeutils.h>
williamr@2
   163
williamr@2
   164
 // Get CCoeEnv instance
williamr@2
   165
 CEikonEnv* iEikonEnv = CEikonEnv::Static();
williamr@2
   166
williamr@2
   167
 TInt number(12);
williamr@2
   168
 _LIT(name, "John Doe");
williamr@2
   169
williamr@2
   170
 // Method reads a resource string with memory allocation,
williamr@2
   171
 // replaces all %N strings in it with a replacement TInt and
williamr@2
   172
 // all %U strings in it with a replacement string.
williamr@2
   173
 // The heap descriptor must be destroyed  when it is no longer needed.
williamr@2
   174
 // iEikonEnv is needed for loading the resource string.
williamr@2
   175
 HBufC stringholder = TulTextResourceUtils::LoadL(R_TEXT_EXAMPLE, name, number, iEikonEnv);
williamr@2
   176
williamr@2
   177
 // The number and name are added to the resource string. The result is
williamr@2
   178
 // that stringholder points to a heap descriptor containing string:
williamr@2
   179
 // "You have missed 12 messages from John Doe.\001Missed 12 msgs from John
williamr@2
   180
 // Doe.\001Missed 12 msgs."
williamr@2
   181
williamr@2
   182
 // Delete the heap descriptor
williamr@2
   183
 delete stringholder;
williamr@2
   184
@endcode
williamr@2
   185
williamr@2
   186
williamr@2
   187
Error handling:
williamr@2
   188
williamr@2
   189
The leave mechanism of the Symbian OS environment is used to handle memory
williamr@2
   190
exhaustion. The panic mechanism is used to handle programming errors while
williamr@2
   191
debugging. TulTextResourceUtils panics for seven different reasons. The panic 
williamr@2
   192
category is named TulTextResourceUtils. The panic codes are:
williamr@2
   193
williamr@2
   194
- ETooFewArguments        = 0 (Unsolved parameters in resource string.)
williamr@2
   195
- ETooManyArguments       = 1 (Already solved all parameters in  resource string.)
williamr@2
   196
- EKeyStringNotFound      = 2 (The key string wasn't found in formatting.)
williamr@2
   197
- EInvalidIndex           = 3 (Invalid index in Format-method)
williamr@2
   198
- EDescriptorTooSmall     = 4 (Too small destination descriptor.)
williamr@2
   199
- ECCoeEnvNotInitialized  = 5 (CCoeEnv is not initialized)
williamr@2
   200
- EInvalidSubstitute      = 6 (Substituted string contains KSubStringSeparator)
williamr@2
   201
williamr@2
   202
@publishedAll
williamr@2
   203
@released
williamr@2
   204
*/
williamr@2
   205
NONSHARABLE_CLASS(TulTextResourceUtils)
williamr@2
   206
    {
williamr@2
   207
public:
williamr@2
   208
    IMPORT_C static void Load(TDes& aDest, TInt aResourceId, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   209
    IMPORT_C static void Format(TDes& aDest, const TDesC& aSource, TInt aPosition, TInt aSubs);
williamr@2
   210
    IMPORT_C static void Format(TDes& aDest, const TDesC& aSource, TInt aPosition, const TDesC& aSubs);
williamr@2
   211
    IMPORT_C static HBufC* LoadL(TInt aResourceId, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   212
    IMPORT_C static HBufC* LoadL(TInt aResourceId, TInt aInt, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   213
    IMPORT_C static HBufC* LoadL(TInt aResourceId, const TDesC& aString, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   214
    IMPORT_C static HBufC* LoadL(TInt aResourceId, const TDesC& aString, TInt aInt, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   215
    IMPORT_C static HBufC* LoadL(TInt aResourceId, const CArrayFix<TInt>& aInts, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   216
    IMPORT_C static HBufC* LoadL(TInt aResourceId, const MDesCArray& aStrings, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   217
    IMPORT_C static HBufC* LoadL(TInt aResourceId, const MDesCArray& aStrings, const CArrayFix<TInt>& aInts, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   218
    IMPORT_C static HBufC* LoadLC(TInt aResourceId, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   219
    IMPORT_C static HBufC* LoadLC(TInt aResourceId, TInt aInt, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   220
    IMPORT_C static HBufC* LoadLC(TInt aResourceId, const TDesC& aString, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   221
    IMPORT_C static HBufC* LoadLC(TInt aResourceId, const TDesC& aString, TInt aInt, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   222
    IMPORT_C static HBufC* LoadLC(TInt aResourceId, const CArrayFix<TInt>& aInts, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   223
    IMPORT_C static HBufC* LoadLC(TInt aResourceId, const MDesCArray& aStrings, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   224
    IMPORT_C static HBufC* LoadLC(TInt aResourceId, const MDesCArray& aStrings, const CArrayFix<TInt>& aInts, CCoeEnv* aLoaderEnv = NULL);
williamr@2
   225
private:
williamr@2
   226
    TulTextResourceUtils();
williamr@2
   227
    TulTextResourceUtils(const TulTextResourceUtils&);	// Prohibit copy constructor
williamr@2
   228
    TulTextResourceUtils& operator= (const TulTextResourceUtils&);	// Prohibit assigment operator
williamr@2
   229
    static HBufC* FormatStringL(const TDesC& aSource, const TDesC& aKey, const TDesC& aSubs, TBidiText::TDirectionality aDir);
williamr@2
   230
    static HBufC* FormatStringL(const TDesC& aSource, const TDesC& aKey, const TDesC& aSubs,
williamr@2
   231
        						 TBidiText::TDirectionality aDirectionality, TInt& aParamCount, TInt aSubCount);
williamr@2
   232
    static HBufC* FormatStringL(TDesC& aSource, const CArrayFix<TInt>& aInts, TInt aMax, TBidiText::TDirectionality aDir);
williamr@2
   233
    static HBufC* FormatStringL(TDesC& aSource, const MDesCArray& aStrings, TInt aMax, TBidiText::TDirectionality aDir);
williamr@2
   234
    static TInt Formater(TDes& aDest, const TDesC& aSource, const TDesC& aKey,
williamr@2
   235
        					const TDesC& aSubs, TBidiText::TDirectionality aDirectionality);
williamr@2
   236
williamr@2
   237
    static void KeyStringFormater(TDes& aDest, const TText& aKey, TInt aPosition, const TDesC& aKeyString);
williamr@2
   238
    static TBidiText::TDirectionality ResolveDirectionality(TDes& aText, TBool* aFound);
williamr@2
   239
    static TInt GetParamCount(const TDesC& aText, TInt aIndex = -1);
williamr@2
   240
    static TInt GetSubStringCount(const TDesC& aText);
williamr@2
   241
        
williamr@2
   242
    static TBidiText::TDirectionality DirectionalityL(const TDesC& aText, TBool* aFound);
williamr@2
   243
    static HBufC* ResolveSubStringDirsL(TDes& aText, TInt aCount, TBool* aMarker);
williamr@2
   244
    static HBufC* ResolveSubStringL(TDes& aText, TBool* aMarker);
williamr@2
   245
    static void RemoveNoDirMarkers(TDes& aText);
williamr@2
   246
    static void FormatL(TDes& aDest, const TDesC& aSource, const TDesC& aKeybuf, const TDesC& aSubs);
williamr@2
   247
    };
williamr@2
   248
williamr@2
   249
williamr@2
   250
#endif	// __TULTEXTRESOURCEUTILS_H__