examples/ForumNokia/DescriptorExample/src/ManipulatingDescriptors.cpp

00001 /*
00002  * Copyright � 2008 Nokia Corporation.
00003  */
00004 
00005 #include <e32std.h>
00006 #include <e32base.h>
00007 #include <charconv.h>
00008 #include <collate.h>
00009 #include "DescriptorExamples.h"
00010 #include "StringRenderer.h"
00011 
00012 // -----------------------------------------------------------------------------
00013 // These macros are shorter versions for Rendering macros. They assume variable
00014 // 'output' derived from TDes to be in the scope where used.
00015 // -----------------------------------------------------------------------------
00016 #define RenderVariableString(aVariableName) \
00017     RenderVariableFormatted(aVariableName, output, KRenderDefault);
00018 
00019 #define RenderVariableBinary(aVariableName) \
00020     RenderVariableFormatted(aVariableName, output, KRenderContentAsBinary);
00021 
00022 #define RenderResult(aDeclaration) \
00023     RenderResultFormatted(aDeclaration, output, KRenderDefault);
00024 
00025 #define ExecuteAndRenderVariable(aDeclaration, aVariable) \
00026     ExecuteAndRenderVariableFormatted(aDeclaration, aVariable, output, \
00027                                       KRenderDefault );
00028 
00029 // -----------------------------------------------------------------------------
00030 // This example method is documented in header file "DescriptorExamples.h"
00031 // -----------------------------------------------------------------------------
00032 void CDescriptorExamples::NonModifyingMethods()
00033     {
00034     TPtr output( iViewer->GetViewBuffer() );
00035     RenderHeader( _L( "NonModifyingMethods" ), output );
00036 
00037     // ---------- Initialize an example string ----------
00038     // this declares a literal with a few characters
00039     _LIT(KCharacters, "abcdefghijklmnopqrstuvwxyz0123456789");
00040     // this casts the literal to non modifiable descriptor
00041     const TDesC &str = KCharacters();
00042     RenderVariableString(str);
00043 
00044     // ---------- Capacity ----------
00045     output.Append( _L( "\nCapacity...\n" ) );
00046 
00047     // Length() returns number of characters in descriptor
00048     RenderResult( str.Length() ); // 36
00049 
00050     // Size() returns number of bytes in descriptor - in unicode buid it is
00051     //        2*Length() and in non-unicode build it is same as Length()
00052     RenderResult( str.Size() ); // 72
00053 
00054     // ---------- Extracting portions ----------
00055     output.Append( _L( "\nExtracting portions...\n" ) );
00056 
00057     // Left(count) returns a string witn "count" number of characters
00058     // calculated from left side of the string
00059     RenderResult( str.Left(2) ); // "ab"
00060     RenderResult( str.Left(5) ); // "abcde"
00061 
00062     // Right(count) returns a string witn "count" number of characters
00063     // calculated from right side of the string
00064     RenderResult( str.Right(2) ); // "89"
00065     RenderResult( str.Right(12) ); // "yz0123456789"
00066 
00067     // Mid(index, count) returns a portion from string starting
00068     // from "index" and having "count" number of characters
00069     RenderResult( str.Mid(2,6) ); // "cdefgh"
00070     RenderResult( str.Mid(33,3) ); // "789"
00071 
00072     // if "count" is omitted the rest of the string from "index" is returned
00073     RenderResult( str.Mid(28) ); // "23456789"
00074     RenderResult( str.Mid(35) ); // "9"
00075 
00076     // ---------- Locating character ----------
00077     output.Append( _L( "\nLocating character...\n" ) );
00078 
00079     // locate index of given character
00080     RenderResult( str.Locate('d') ); // 3
00081     RenderResultFormatted( _L("1ABAD").Locate('A'),
00082         output, KRenderDefault); // "1"
00083     RenderResultFormatted( _L("1ABAD").LocateReverse('A'),
00084         output, KRenderDefault); // "1"
00085 
00086     // ---------- Compare ----------
00087     // Comparison with method Compare() uses the binary comparison approach to
00088     // compare each character in string. For example value of character
00089     // 'A' has unicode value of 65 and 'a' 97.
00090     //
00091     // Both strings could have been converted to lower or upper case to perform
00092     // case insensitive comparison. However, there exists also CompareC() method
00093     // below that introduces a method to compare in case insensitive manner.
00094     output.Append( _L( "\nCompare()\n" ) );
00095 
00096     // examplerun: str.Compare(_L("aaa"))=1
00097     RenderResultFormatted( str.Compare( _L("aaa") ),
00098         output, KRenderDefault); // >0 : str is greater
00099 
00100     // examplerun: str.Compare(_L("zzz"))=-25
00101     RenderResultFormatted( str.Compare( _L("zzz") ),
00102         output, KRenderDefault ); // <0 : str is less
00103 
00104     // examplerun: str.Compare(str)=0
00105     RenderResult( str.Compare(str) );       // 0  : str is equal
00106 
00107     // examplerun: str.Left(2).Compare(_L("ab"))=0
00108     RenderResultFormatted( str.Left(2).Compare(_L("ab")),
00109         output, KRenderDefault); // 0 : equal
00110 
00111     // examplerun: str.Left(2).Compare(_L("AB"))=32
00112     // 'A' (65) > 'a' (97)
00113     RenderResultFormatted( str.Left(2).Compare(_L("AB")),
00114         output, KRenderDefault ); // 0 : equal
00115 
00116     // ---------- Comparing (Collated) ----------
00117     // To perform collated comparisons, method
00118     //
00119     //   CompareC(TDesC&, TInt aMaxLevel, const TCollationMethod*)
00120     //
00121     // is used. The aMaxLevel parameter can be used to perform specific rules:
00122     //
00123     // 0 - only test the character identity; accents and case are ignored
00124     // 1 - test the character identity and accents; case is ignored
00125     // 2 - test the character identity, accents and case
00126     // 3 - test the Unicode value as well as the character identity,
00127     //     accents and case.
00128     //
00129     output.Append( _L( "\nCompareC()...\n" ) );
00130 
00131     // To perform case insensitive comparison, level 1 is used
00132     RenderResultFormatted( _L("ab").CompareC(_L("AB"),1,NULL),
00133         output, KRenderDefault ); // 0 : equal
00134 
00135     // note that when using collated comparison as case sensitive manner
00136     // the string "ab" is less than "AB" while using standars Compare()
00137     // the "ab" is greater than "AB"!!!
00138     RenderResultFormatted( _L("ab").CompareC(_L("AB"),2,NULL),
00139         output, KRenderDefault );
00140     RenderResultFormatted( _L("ab").Compare(_L("AB")),
00141         output, KRenderDefault );
00142 
00143     // ----------
00144     // The natural comparison ignores white space differences.
00145     // This and other specific behaviour can be changed by modifying
00146     // the standard behaviour.
00147     TCollationMethod stdMethod = *Mem::CollationMethodByIndex(0); // std impl
00148 
00149     // dont ignore punctuation and spaces
00150     TCollationMethod strictMethod = stdMethod; // copy std impl
00151     strictMethod.iFlags |= TCollationMethod::EIgnoreNone; // modify copy
00152 
00153     // use level 1 (case ignored) in both cases to compare. Then use
00154     // standard method and modified one. Examplerun:
00155     //
00156     //    _L("a  b").CompareC(_L("A B"),1,&stdMethod)=0
00157     //    _L("a  b").CompareC(_L("A B"),1,&strictMethod)=-2
00158     //
00159     RenderResultFormatted( _L("a   b").CompareC(_L("A B"),1,&stdMethod),
00160         output, KRenderDefault );
00161     RenderResultFormatted( _L("a   b").CompareC(_L("A B"),1,&strictMethod),
00162         output, KRenderDefault );
00163 
00164     iViewer->UpdateView();
00165     }
00166 
00167 // -----------------------------------------------------------------------------
00168 // This example method is documented in header file "DescriptorExamples.h"
00169 // -----------------------------------------------------------------------------
00170 void CDescriptorExamples::ModifyingMethodsL()
00171     {
00172     // this is used to point to correct position in view buffer
00173     TPtr output( iViewer->GetViewBuffer() );
00174     RenderHeader( _L( "ModifyingMethods" ), output );
00175     TBuf<10> buf10;
00176 
00177     // ---------- Setting and viewing characteristics ----------
00178     output.Append( _L("\nCharacteristics of buffer after initialization:\n") );
00179     RenderResult( buf10.MaxLength() ); // max number of chars
00180     RenderResult( buf10.MaxSize() ); // max number of bytes
00181     RenderResult( buf10.Length() ); // number of chars
00182     RenderResult( buf10.Size() ); // number of bytes
00183 
00184     TBuf<20> example(_L("Hello"));
00185     RenderVariableFormatted( example, output, KRenderCharacteristics );
00186 
00187     // first init the data in buffer with asterix
00188     output.Append( _L("\nLet's fill the buffer\n") );
00189     buf10.Fill( '*', buf10.MaxLength() );
00190     RenderVariableString( buf10 ); // max number of bytes
00191     RenderResult( buf10.Length() ); // number of chars
00192     RenderResult( buf10.Size() ); // number of bytes
00193 
00194     output.Append( _L("\nNow manipilate the length:\n") );
00195     // set the current length to three. The data that exist in the
00196     // buffer is as is
00197     ExecuteAndRenderVariable( buf10.SetLength(3), buf10 );
00198     ExecuteAndRenderVariable( buf10.SetMax(), buf10 );
00199     ExecuteAndRenderVariable( buf10.Zero(), buf10 );
00200 
00201     // ---------- Copying ----------
00202     output.Append( _L("\nCopying...\n") );
00203     TPtrC8 narrowHello( _L8( "hello" ) );
00204     RenderVariableString( narrowHello );
00205     TPtrC16 wideHello( _L16( "unIc heLLo" ) );
00206     RenderVariableString( wideHello );
00207     // copy contents of 8 bit descriptor to 16 bit descriptor
00208     ExecuteAndRenderVariable( buf10.Copy(narrowHello), buf10 );
00209     // copy contents of 16 bit descriptor to 16 bit descriptor
00210     ExecuteAndRenderVariable( buf10.Copy(wideHello), buf10 );
00211     // copy contents of 16 bit descriptor to 16 bit descriptor
00212     // and capitalize the content
00213     ExecuteAndRenderVariable( buf10.CopyCP(wideHello), buf10 );
00214     // copy and set to lower case
00215     ExecuteAndRenderVariable( buf10.CopyLC(wideHello), buf10 );
00216     // copy and set to Uppper case
00217     ExecuteAndRenderVariable( buf10.CopyUC(wideHello), buf10 );
00218 
00219     // ---------- Repeating ----------
00220     output.Append( _L("\nRepeating...\n") );
00221     TPtrC abc( _L( "aBc" ) );
00222     RenderVariableString( abc );
00223     // write abs twise to the descriptor
00224     ExecuteAndRenderVariable( buf10.Repeat((TText*)abc.Ptr(),2), buf10 );
00225     // write abs until to the maximum size
00226     ExecuteAndRenderVariable( buf10.Repeat(abc), buf10 );
00227 
00228     // ---------- Justifying ----------
00229     output.Append( _L("\nJustifying...\n") );
00230     RenderVariableString( abc );
00231     // show aligning of string "abs" to our buffer
00232     ExecuteAndRenderVariable( buf10.Justify(abc,8,ELeft,'_'), buf10 );
00233     ExecuteAndRenderVariable( buf10.Justify(abc,8,ECenter,'*'), buf10 );
00234     ExecuteAndRenderVariable( buf10.Justify(abc,10,ERight,'.'), buf10 );
00235 
00236     // ---------- Rendering numbers ----------
00237     output.Append( _L("\nRendering numbers...\n") );
00238     // decimal number
00239     ExecuteAndRenderVariable( buf10.Num(12345), buf10 );
00240     // hexadecimal number. characters in lover case
00241     ExecuteAndRenderVariable( buf10.Num(65300,EHex), buf10 );
00242     // binary number
00243     ExecuteAndRenderVariable( buf10.Num(55,EBinary), buf10 );
00244     // hexadecimal number. characters in upper case
00245     ExecuteAndRenderVariable( buf10.NumUC(65300,EHex), buf10 );
00246     // fixed width decimal number
00247     ExecuteAndRenderVariable( buf10.NumFixedWidth(25,EDecimal,6), buf10 );
00248     // fixed width hexadecimal number
00249     ExecuteAndRenderVariable( buf10.NumFixedWidth(0xf05,EHex,6), buf10 );
00250     // fixed width binary number
00251     ExecuteAndRenderVariable( buf10.NumFixedWidth(3,EBinary,8), buf10 );
00252     // fixed width hexadecimal number, upper case
00253     ExecuteAndRenderVariable( buf10.NumFixedWidthUC(0xf05,EHex,6), buf10 );
00254 
00255     // declare 64 bit number that has now only lower 32 bits set
00256     TInt64 longNum(0x12345678);
00257     ExecuteAndRenderVariable( buf10.Num(longNum,EHex), buf10 );
00258     // multiply the result so that it needs more than 32 bits and show results.
00259     // The result 123456780 > FFFFFFFF that is the maximum size 32 bit
00260     // integer can store
00261     ExecuteAndRenderVariable( buf10.Num(longNum*16,EHex), buf10 );
00262 
00263     TRealFormat realFormatter(8);
00264     ExecuteAndRenderVariable( buf10.Num(3.5,realFormatter), buf10 );
00265     ExecuteAndRenderVariable( buf10.Num(10.0/3,realFormatter), buf10 );
00266     ExecuteAndRenderVariable( buf10.Num(123.0*1000*1000*1000,realFormatter),
00267                               buf10 );
00268 
00269     // ---------- Formatting ----------
00270     output.Append( _L("\nFormatting data types\n") );
00271     TBuf<64> buf;
00272     RenderVariableFormatted( buf, output, KRenderCharacteristics );
00273 
00274     // binary format using %b
00275     ExecuteAndRenderVariableFormatted(
00276         buf.Format( _L("binary=%b" ), 33),
00277         buf,output, KRenderDefault );
00278 
00279     // integer format using %d
00280     ExecuteAndRenderVariableFormatted(
00281         buf.Format( _L("decimal=%d" ), 33),
00282         buf,output, KRenderDefault );
00283 
00284     // hexadecimal format using %x
00285     ExecuteAndRenderVariableFormatted(
00286         buf.Format( _L("hexadecimal=%x" ), 33),
00287         buf,output, KRenderDefault );
00288 
00289     // real as exponent format
00290     ExecuteAndRenderVariableFormatted(
00291         buf.Format( _L("real (exp)=%e" ), 33.43),
00292         buf,output, KRenderDefault );
00293 
00294     // real as fixed format
00295     ExecuteAndRenderVariableFormatted(
00296         buf.Format( _L("real (fixed)=%f" ), 33.43),
00297         buf,output, KRenderDefault );
00298 
00299     // pointer to descriptor
00300     TPtrC str = _L("hello");
00301     RenderVariableString( str );
00302     ExecuteAndRenderVariableFormatted(
00303         buf.Format( _L("str='%S'" ), &str),
00304         buf,output, KRenderDefault );
00305 
00306     // pointer to null terminated character array
00307     ExecuteAndRenderVariableFormatted(
00308         buf.Format( _L("str='%s'" ), str.Ptr()),
00309         buf,output, KRenderDefault );
00310 
00311     // ---------- Formatting align and padding ----------
00312     output.Append( _L("\nFormatting align and padding\n") );
00313 
00314     // padding width: 10, default pad character, default alignment
00315     ExecuteAndRenderVariableFormatted(
00316         buf.Format( _L("binary=%10b" ), 33),
00317         buf,output, KRenderDefault );
00318 
00319     // padding width: 10, '0' as pad character, default alignment
00320     ExecuteAndRenderVariableFormatted(
00321         buf.Format( _L("binary=%010b" ), 33),
00322         buf,output, KRenderDefault );
00323 
00324     // padding width: 8, '0' as pad character, default alignment
00325     ExecuteAndRenderVariableFormatted(
00326         buf.Format( _L("binary=%08d" ), 12345),
00327         buf,output, KRenderDefault );
00328 
00329     RenderVariableString( str );
00330     // padding width: 20, default pad character, default alignment
00331     ExecuteAndRenderVariableFormatted(
00332         buf.Format( _L("str='%20S'" ), &str),
00333         buf,output, KRenderDefault );
00334 
00335     // padding width: 20, '0' as pad character, default alignment
00336     ExecuteAndRenderVariableFormatted(
00337         buf.Format( _L("str='%020S'" ), &str),
00338         buf,output, KRenderDefault );
00339 
00340     // padding width: 20, default (space) as pad character, center alignment
00341     ExecuteAndRenderVariableFormatted(
00342         buf.Format( _L("str='%=20S'" ), &str),
00343         buf,output, KRenderDefault );
00344 
00345     // padding width: 20, default (space) as pad character, left alignment
00346     ExecuteAndRenderVariableFormatted(
00347         buf.Format( _L("str='%-20S'" ), &str),
00348         buf,output, KRenderDefault );
00349 
00350     // padding width: 20, default (space) as pad character, right alignment
00351     ExecuteAndRenderVariableFormatted(
00352         buf.Format( _L("str='%+20S'" ), &str),
00353         buf,output, KRenderDefault );
00354 
00355     // padding width: 20, custom pad character, center alignment
00356     ExecuteAndRenderVariableFormatted(
00357         buf.Format( _L("str='%=*20S'" ), '.', &str),
00358         buf,output, KRenderDefault );
00359 
00360     // ---------- Modifying content in other means ----------
00361     output.Append( _L("\nOther modifications...\n") );
00362     buf.Copy(_L("abcd"));
00363     RenderVariableString( buf );
00364 
00365     // insert a string to middle of buffer
00366     ExecuteAndRenderVariableFormatted(
00367         buf.Insert( 2, _L("____") ),
00368         buf, output, KRenderDefault );
00369 
00370     // replaces a portion with given string
00371     ExecuteAndRenderVariableFormatted(
00372         buf.Replace( 3, 2, _L("****") ),
00373         buf, output, KRenderDefault );
00374 
00375     // replaces but since portion size is 0, this is same
00376     // as insert( 1, _L("AA") );
00377     ExecuteAndRenderVariableFormatted(
00378         buf.Replace( 1, 0, _L("AA") ),
00379         buf, output, KRenderDefault );
00380 
00381     // replaces a portion but since no data empty, this is same
00382     // as delete( 1, 9 );
00383     ExecuteAndRenderVariableFormatted(
00384         buf.Replace( 2, 9, _L("") ),
00385         buf, output, KRenderDefault );
00386 
00387     // delete from index 1, 2 characters
00388     buf.Copy( _L("Hello!" ));
00389     RenderVariableString(buf);
00390     ExecuteAndRenderVariableFormatted(
00391         buf.Delete( 1, 2 ),
00392         buf, output, KRenderDefault );
00393 
00394     // ---------- Trimming ----------
00395     output.Append( _L("\nTrimming. The buf in each case is\n") );
00396     buf.Copy( _L( "    Hello   World!  " ) );
00397     RenderVariableString(buf);
00398 
00399     // trim from left
00400     ExecuteAndRenderVariableFormatted(
00401         buf.TrimLeft(),
00402         buf, output, KRenderDefault );
00403 
00404     // trim from right
00405     buf.Copy( _L( "    Hello   World!  " ) );
00406     ExecuteAndRenderVariableFormatted(
00407         buf.TrimRight(),
00408         buf, output, KRenderDefault );
00409 
00410     // trim from left & right
00411     buf.Copy( _L( "    Hello   World!  " ) );
00412     ExecuteAndRenderVariableFormatted(
00413         buf.Trim(),
00414         buf, output, KRenderDefault );
00415 
00416     // trim from left & right & from middle
00417     buf.Copy( _L( "    Hello   World!  " ) );
00418     ExecuteAndRenderVariableFormatted(
00419         buf.TrimAll(),
00420         buf, output, KRenderDefault );
00421 
00422     // ---------- Filling ----------
00423     output.Append( _L("\nFilling...\n") );
00424     buf.Copy( _L( "abcd" ) );
00425     RenderVariableString(buf);
00426     // fill chars from zero index to current length with given char
00427     ExecuteAndRenderVariableFormatted(
00428         buf.Fill('*'),
00429         buf, output, KRenderCharacteristics );
00430 
00431     // fill chars from zero index to 11 with given char
00432     ExecuteAndRenderVariableFormatted(
00433         buf.Fill('.', 12),
00434         buf, output, KRenderCharacteristics );
00435 
00436     // fill chars from zero index to 6 with binary zero
00437     ExecuteAndRenderVariableFormatted(
00438         buf.FillZ(6),
00439         buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00440 
00441     // change content of buffer (so length changes)
00442     ExecuteAndRenderVariableFormatted(
00443         buf.Copy( _L("Hey!") ),
00444         buf, output, KRenderDefault);
00445     // fill chars from zero index to current length binary zero
00446     ExecuteAndRenderVariableFormatted(
00447         buf.FillZ(),
00448         buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00449 
00450     // ---------- Filling ----------
00451     output.Append( _L("\nText conversions...\n") );
00452     buf.Copy( _L( "Hello World" ) );
00453     RenderVariableString(buf);
00454     ExecuteAndRenderVariableFormatted(
00455         buf.LowerCase(),
00456         buf, output, KRenderDefault);
00457 
00458     ExecuteAndRenderVariableFormatted(
00459         buf.UpperCase(),
00460         buf, output, KRenderDefault);
00461 
00462     ExecuteAndRenderVariableFormatted(
00463         buf.Capitalize(),
00464         buf, output, KRenderDefault);
00465 
00466     // ---------- Swapping ----------
00467     output.Append( _L("\nSwapping...\n") );
00468     TBuf<20> buf1( _L("buf1!") );
00469     TBuf<20> buf2( _L("buf2**") );
00470     RenderVariableString( buf1 );
00471     RenderVariableString( buf2 );
00472     output.Append( _L("After buf1.Swap(buf2)\n") );
00473     buf1.Swap(buf2);
00474     RenderVariableString( buf1 );
00475     RenderVariableString( buf2 );
00476 
00477     // ---------- AppendXXX ----------
00478     // AppendXXX() methods are similar like other methods in descriptor API.
00479     // They just append to the end of the descriptor buffer instead of
00480     // replacing the current content. That's why appending methods are not
00481     // described here.
00482 
00483     iViewer->UpdateView();
00484     }
00485 
00486 // -----------------------------------------------------------------------------
00487 // This example method is documented in header file "DescriptorExamples.h"
00488 // -----------------------------------------------------------------------------
00489 void CDescriptorExamples::CharacterConversionsL()
00490     {
00491 {
00492     // Create session to the file server
00493     RFs fsSession;
00494     User::LeaveIfError( fsSession.Connect() );
00495     CleanupClosePushL(fsSession);
00496 
00497     // Create character converter that can convert between
00498     // SMS encoding and UCS-2
00499     CCnvCharacterSetConverter *converter;
00500     converter = CCnvCharacterSetConverter::NewLC();
00501     converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierSms7Bit,
00502                                          fsSession );
00503 
00504     // Declare descriptor containing a string encoded with
00505     // 7-bit SMS encoding.
00506     const TUint8 SMSSourceBytes[] =
00507         {
00508         54,      // character '6'  (takes one byte)
00509         2,       // character '$'  (takes one byte)
00510         27, 61,  // character '~'  (takes two bytes)
00511         53,      // character '5'  (takes one byte)
00512         27, 101, // euro character (takes two bytes)
00513         };
00514     TPtrC8 SMSEncodedStr(SMSSourceBytes,sizeof(SMSSourceBytes));
00515 
00516     // Convert the SMS encoded message to the UCS-2 encoding
00517     TInt state = CCnvCharacterSetConverter::KStateDefault;
00518     TBuf<64> UCS2Buf;
00519     converter->ConvertToUnicode(UCS2Buf, SMSEncodedStr, state);
00520 
00521     // remove objects from the cleanup stack
00522     CleanupStack::PopAndDestroy(2); // converter, fsSession
00523 }
00524     // this is used to point to correct position in view buffer
00525     TPtr output( iViewer->GetViewBuffer() );
00526     CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* sets;
00527     RFs fsSession;
00528     CCnvCharacterSetConverter *converter;
00529 
00530     // ---------- List available converters ----------
00531     // This example lists all character sets converters available in the
00532     // system.
00533     RenderHeader( _L( "CharacterConversions: List converters" ), output );
00534 
00535     // open connection to file server and push the instance to the cleanup
00536     // stack
00537     User::LeaveIfError(fsSession.Connect());
00538     CleanupClosePushL(fsSession);
00539     // get list of character sets and push it to cleanup stack
00540     sets = CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableL(fsSession);
00541     CleanupStack::PushL(sets);
00542     // show names
00543     output.Append( _L( "\nList of available character converters:\n" ) );
00544     for( int i=sets->Count()-1; i>=0; i-- )
00545     {
00546         TPtrC name = sets->At(i).Name();
00547         output.AppendFormat( _L("  %S\n"), &name );
00548     }
00549 
00550     // ---------- Convert from 7-bit SMS to Unicode ----------
00551     // This example converts SMS to Unicode string.
00552     //
00553     // SMS character set consist of characters whose value is specified with 7
00554     // bits. So 128 characters are available in 7-bit SMS character set (default
00555     // alphabet). However, character set is extended so that if a character in
00556     // message is ESC (0x1B) the next character is treated as extended character
00557     // outside the default alphabet. So 7-bit SMS encoding can represent 255
00558     // different characters when using extended behaviour.
00559     //
00560     // The default alphabet is specified in ETSI "GSM 03.38". However, to
00561     // quickly view standard characters and extended ones, refer to
00562     // (http://www.ozeki.hu/index.phtml?ow_page_number=137) or to
00563     // (http://www.csoft.co.uk/character_sets/gsm.htm) or google with search
00564     // string "GSM character set" or "SMS character set".
00565     //
00566     // GSM 03.38 specification specifies that SMS message can be encoded with
00567     // four character encodings. This example codes the string with default
00568     // 7-bit SMS alphabet. SMS can consist of maximum of 160 characters. Packing
00569     // mechanism specified in GSM 03.38 makes it possible to pack 160 7-bit
00570     // characters to 140 bytes.
00571     //
00572     // Symbian provices character conversion for SMS 7-bit SMS data. However, it
00573     // doesn't use packed format specified in GSM 03.38. Instead, it treats the
00574     // binary array (of 8 bit numbers) to store 7 bit SMS characters.
00575     //
00576     // Lets assume that we have a SMS that consist of string "6$~5e" (where the
00577     // 'e' is meant to be an euro sign). The SMS and Unicode character codes for
00578     // the characters are:
00579     //
00580     // char | SMS value (7bit) | Unicode value (16 bit)
00581     // ------------------------------------------------
00582     // '6'  |  54              |   54
00583     // '$'  |   2              |   36
00584     // '~'  |  27              |  126 // Extended character coded
00585     //      |  61              |      // with two 7 bit values: ESC+code
00586     // '5'  |  53              |   53
00587     // 'e'  |  27              | 8364 // "Euro sign": Extended character
00588     //      | 101              |      // coded with two 7 bit values: ESC+code
00589     //
00590     const TUint8 SMSSourceBytes[] =
00591         {
00592         54,2,27,61,53,27,101
00593         };
00594 
00595     // create converter that converts character data between 7-bit GSM data and
00596     // Unicode
00597     converter = CCnvCharacterSetConverter::NewLC();
00598     converter->PrepareToConvertToOrFromL( KCharacterSetIdentifierSms7Bit,
00599                                           fsSession );
00600 
00601     // declare output buffer and declare a 8 bit descriptor to describe our
00602     // 7-bit SMS data.
00603     TInt state = CCnvCharacterSetConverter::KStateDefault;
00604     TBuf16<10> unicodeConverted;
00605     TPtrC8 SMSSource( SMSSourceBytes, sizeof( SMSSourceBytes ) );
00606 
00607     // convert the 7-bit SMS data to Unicode characters and store results to
00608     // variable "unicodeConverted"
00609     converter->ConvertToUnicode( unicodeConverted, SMSSource, state );
00610 
00611     // show unicode string converted from SMS ("6$~5�");
00612     RenderHeader( _L( "CharacterConversions: SMS to Unicode" ), output );
00613     output.Append( _L("SMS data consist of 7 bit character values (where euro and tilde char consist of two numbers):\n") );
00614     RenderVariableBinary( SMSSource );
00615     output.Append( _L("\nWhile unicode uses 16 bits to store each character:\n") );
00616     RenderVariableBinary( unicodeConverted );
00617     RenderVariableString( unicodeConverted );
00618 
00619     // ---------- Unicode To SMS ----------
00620     // Declare unicode string that consist "normal" and special characters
00621     RenderHeader( _L( "CharacterConversions: Unicode to SMS" ), output );
00622     
00623     //nice characters but can't be compiled with GCCE ;)
00624     //TBuf<64> unicodeSource(_L("Some nice chars: �,�,�,� \x20AC") ); // 0x20AC = euro sign, desicmal 8364;
00625     TBuf<64> unicodeSource(_L("Some nice chars: \x00F6,\x00E4,\x004F,\x00C4 \x20AC") );
00626     
00627     //� == 00F6 
00628     //� == 004F
00629     //� == 00E4
00630     //� == 00C4
00631     
00632     RenderVariableString( unicodeSource ); 
00633     
00634     RenderVariableBinary( unicodeSource );
00635 
00636     // lets convert the unicode to 7 bit SMS
00637     TBuf8<128> convertedToSMS;
00638     converter->ConvertFromUnicode( convertedToSMS, unicodeSource );
00639     RenderVariableBinary( convertedToSMS );
00640     RenderVariableString( convertedToSMS );
00641 
00642     output.Append( _L("\nEuro character is espaced in SMS encoding so number of 'chars' differ:\n") );
00643     RenderResult( unicodeSource.Length() );
00644     RenderResult( convertedToSMS.Length() );
00645 
00646     output.Append( _L("\nUnicode consist of 16 bit chars while SMS consist of 8 bit ones. Number of bytes in encodings:\n") );
00647     RenderResult( unicodeSource.Size() );
00648     RenderResult( convertedToSMS.Size() );
00649 
00650     iViewer->UpdateView();
00651     CleanupStack::PopAndDestroy(3); // fsSession, sets, converter
00652     }
00653 
00654 // -----------------------------------------------------------------------------
00655 // This example method is documented in header file "DescriptorExamples.h"
00656 // -----------------------------------------------------------------------------
00657 void CDescriptorExamples::LexicalAnalysis()
00658     {
00659     TPtr output( iViewer->GetViewBuffer() );
00660     RenderHeader( _L( "LexicalAnalysis" ), output );
00661 
00662     // TLex provides methods for parsin text. Its usage is quite well
00663     // documented in symbian documentation "Using TLex". However, converting
00664     // strings to numbers is not explained. So this example explains them.
00665     TLex lex;
00666 
00667     TInt num1;
00668     lex.Assign( _L("-254 is number") );
00669     RenderVariableString(lex.Remainder());
00670     // parse integer number
00671     ExecuteAndRenderVariableFormatted(
00672         lex.Val(num1),
00673         num1, output, KRenderDefault );
00674 
00675     TUint32 num2;
00676     lex.Assign( _L("2ff is hex number") );
00677 
00678     RenderVariableString(lex.Remainder());
00679     // parse hex number
00680     ExecuteAndRenderVariableFormatted(
00681         lex.Val(num2, EHex),
00682         num2, output, KRenderDefault );
00683 
00684     // parse float
00685     TReal32 num3;
00686     lex.Assign( _L("234.678 is real number") );
00687     RenderVariableString(lex.Remainder());
00688     // parses the value as 234.677993774414!
00689     lex.Val(num3, '.');
00690     output.Append( _L( "lex.Val(num3, '.') -> num3=" ) );
00691     TRealFormat realFormat;
00692     output.AppendNum( num3, realFormat );
00693 
00694     iViewer->UpdateView();
00695     }

Generated by  doxygen 1.6.2