00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <e32std.h>
00032 #include <e32base.h>
00033 #include <charconv.h>
00034 #include <collate.h>
00035 #include "DescriptorExamples.h"
00036 #include "StringRenderer.h"
00037
00038
00039
00040
00041
00042 #define RenderVariableString(aVariableName) \
00043 RenderVariableFormatted(aVariableName, output, KRenderDefault);
00044
00045 #define RenderVariableBinary(aVariableName) \
00046 RenderVariableFormatted(aVariableName, output, KRenderContentAsBinary);
00047
00048 #define RenderResult(aDeclaration) \
00049 RenderResultFormatted(aDeclaration, output, KRenderDefault);
00050
00051 #define ExecuteAndRenderVariable(aDeclaration, aVariable) \
00052 ExecuteAndRenderVariableFormatted(aDeclaration, aVariable, output, \
00053 KRenderDefault );
00054
00055
00056
00057
00058 void CDescriptorExamples::NonModifyingMethods()
00059 {
00060 TPtr output( iViewer->GetViewBuffer() );
00061 RenderHeader( _L( "NonModifyingMethods" ), output );
00062
00063
00064
00065 _LIT(KCharacters, "abcdefghijklmnopqrstuvwxyz0123456789");
00066
00067 const TDesC &str = KCharacters();
00068 RenderVariableString(str);
00069
00070
00071 output.Append( _L( "\nCapacity...\n" ) );
00072
00073
00074 RenderResult( str.Length() );
00075
00076
00077
00078 RenderResult( str.Size() );
00079
00080
00081 output.Append( _L( "\nExtracting portions...\n" ) );
00082
00083
00084
00085 RenderResult( str.Left(2) );
00086 RenderResult( str.Left(5) );
00087
00088
00089
00090 RenderResult( str.Right(2) );
00091 RenderResult( str.Right(12) );
00092
00093
00094
00095 RenderResult( str.Mid(2,6) );
00096 RenderResult( str.Mid(33,3) );
00097
00098
00099 RenderResult( str.Mid(28) );
00100 RenderResult( str.Mid(35) );
00101
00102
00103 output.Append( _L( "\nLocating character...\n" ) );
00104
00105
00106 RenderResult( str.Locate('d') );
00107 RenderResultFormatted( _L("1ABAD").Locate('A'),
00108 output, KRenderDefault);
00109 RenderResultFormatted( _L("1ABAD").LocateReverse('A'),
00110 output, KRenderDefault);
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 output.Append( _L( "\nCompare()\n" ) );
00121
00122
00123 RenderResultFormatted( str.Compare( _L("aaa") ),
00124 output, KRenderDefault);
00125
00126
00127 RenderResultFormatted( str.Compare( _L("zzz") ),
00128 output, KRenderDefault );
00129
00130
00131 RenderResult( str.Compare(str) );
00132
00133
00134 RenderResultFormatted( str.Left(2).Compare(_L("ab")),
00135 output, KRenderDefault);
00136
00137
00138
00139 RenderResultFormatted( str.Left(2).Compare(_L("AB")),
00140 output, KRenderDefault );
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 output.Append( _L( "\nCompareC()...\n" ) );
00156
00157
00158 RenderResultFormatted( _L("ab").CompareC(_L("AB"),1,NULL),
00159 output, KRenderDefault );
00160
00161
00162
00163
00164 RenderResultFormatted( _L("ab").CompareC(_L("AB"),2,NULL),
00165 output, KRenderDefault );
00166 RenderResultFormatted( _L("ab").Compare(_L("AB")),
00167 output, KRenderDefault );
00168
00169
00170
00171
00172
00173 TCollationMethod stdMethod = *Mem::CollationMethodByIndex(0);
00174
00175
00176 TCollationMethod strictMethod = stdMethod;
00177 strictMethod.iFlags |= TCollationMethod::EIgnoreNone;
00178
00179
00180
00181
00182
00183
00184
00185 RenderResultFormatted( _L("a b").CompareC(_L("A B"),1,&stdMethod),
00186 output, KRenderDefault );
00187 RenderResultFormatted( _L("a b").CompareC(_L("A B"),1,&strictMethod),
00188 output, KRenderDefault );
00189
00190 iViewer->UpdateView();
00191 }
00192
00193
00194
00195
00196 void CDescriptorExamples::ModifyingMethodsL()
00197 {
00198
00199 TPtr output( iViewer->GetViewBuffer() );
00200 RenderHeader( _L( "ModifyingMethods" ), output );
00201 TBuf<10> buf10;
00202
00203
00204 output.Append( _L("\nCharacteristics of buffer after initialization:\n") );
00205 RenderResult( buf10.MaxLength() );
00206 RenderResult( buf10.MaxSize() );
00207 RenderResult( buf10.Length() );
00208 RenderResult( buf10.Size() );
00209
00210 TBuf<20> example(_L("Hello"));
00211 RenderVariableFormatted( example, output, KRenderCharacteristics );
00212
00213
00214 output.Append( _L("\nLet's fill the buffer\n") );
00215 buf10.Fill( '*', buf10.MaxLength() );
00216 RenderVariableString( buf10 );
00217 RenderResult( buf10.Length() );
00218 RenderResult( buf10.Size() );
00219
00220 output.Append( _L("\nNow manipilate the length:\n") );
00221
00222
00223 ExecuteAndRenderVariable( buf10.SetLength(3), buf10 );
00224 ExecuteAndRenderVariable( buf10.SetMax(), buf10 );
00225 ExecuteAndRenderVariable( buf10.Zero(), buf10 );
00226
00227
00228 output.Append( _L("\nCopying...\n") );
00229 TPtrC8 narrowHello( _L8( "hello" ) );
00230 RenderVariableString( narrowHello );
00231 TPtrC16 wideHello( _L16( "unIc heLLo" ) );
00232 RenderVariableString( wideHello );
00233
00234 ExecuteAndRenderVariable( buf10.Copy(narrowHello), buf10 );
00235
00236 ExecuteAndRenderVariable( buf10.Copy(wideHello), buf10 );
00237
00238
00239 ExecuteAndRenderVariable( buf10.CopyCP(wideHello), buf10 );
00240
00241 ExecuteAndRenderVariable( buf10.CopyLC(wideHello), buf10 );
00242
00243 ExecuteAndRenderVariable( buf10.CopyUC(wideHello), buf10 );
00244
00245
00246 output.Append( _L("\nRepeating...\n") );
00247 TPtrC abc( _L( "aBc" ) );
00248 RenderVariableString( abc );
00249
00250 ExecuteAndRenderVariable( buf10.Repeat((TText*)abc.Ptr(),2), buf10 );
00251
00252 ExecuteAndRenderVariable( buf10.Repeat(abc), buf10 );
00253
00254
00255 output.Append( _L("\nJustifying...\n") );
00256 RenderVariableString( abc );
00257
00258 ExecuteAndRenderVariable( buf10.Justify(abc,8,ELeft,'_'), buf10 );
00259 ExecuteAndRenderVariable( buf10.Justify(abc,8,ECenter,'*'), buf10 );
00260 ExecuteAndRenderVariable( buf10.Justify(abc,10,ERight,'.'), buf10 );
00261
00262
00263 output.Append( _L("\nRendering numbers...\n") );
00264
00265 ExecuteAndRenderVariable( buf10.Num(12345), buf10 );
00266
00267 ExecuteAndRenderVariable( buf10.Num(65300,EHex), buf10 );
00268
00269 ExecuteAndRenderVariable( buf10.Num(55,EBinary), buf10 );
00270
00271 ExecuteAndRenderVariable( buf10.NumUC(65300,EHex), buf10 );
00272
00273 ExecuteAndRenderVariable( buf10.NumFixedWidth(25,EDecimal,6), buf10 );
00274
00275 ExecuteAndRenderVariable( buf10.NumFixedWidth(0xf05,EHex,6), buf10 );
00276
00277 ExecuteAndRenderVariable( buf10.NumFixedWidth(3,EBinary,8), buf10 );
00278
00279 ExecuteAndRenderVariable( buf10.NumFixedWidthUC(0xf05,EHex,6), buf10 );
00280
00281
00282 TInt64 longNum(0x12345678);
00283 ExecuteAndRenderVariable( buf10.Num(longNum,EHex), buf10 );
00284
00285
00286
00287 ExecuteAndRenderVariable( buf10.Num(longNum*16,EHex), buf10 );
00288
00289 TRealFormat realFormatter(8);
00290 ExecuteAndRenderVariable( buf10.Num(3.5,realFormatter), buf10 );
00291 ExecuteAndRenderVariable( buf10.Num(10.0/3,realFormatter), buf10 );
00292 ExecuteAndRenderVariable( buf10.Num(123.0*1000*1000*1000,realFormatter),
00293 buf10 );
00294
00295
00296 output.Append( _L("\nFormatting data types\n") );
00297 TBuf<64> buf;
00298 RenderVariableFormatted( buf, output, KRenderCharacteristics );
00299
00300
00301 ExecuteAndRenderVariableFormatted(
00302 buf.Format( _L("binary=%b" ), 33),
00303 buf,output, KRenderDefault );
00304
00305
00306 ExecuteAndRenderVariableFormatted(
00307 buf.Format( _L("decimal=%d" ), 33),
00308 buf,output, KRenderDefault );
00309
00310
00311 ExecuteAndRenderVariableFormatted(
00312 buf.Format( _L("hexadecimal=%x" ), 33),
00313 buf,output, KRenderDefault );
00314
00315
00316 ExecuteAndRenderVariableFormatted(
00317 buf.Format( _L("real (exp)=%e" ), 33.43),
00318 buf,output, KRenderDefault );
00319
00320
00321 ExecuteAndRenderVariableFormatted(
00322 buf.Format( _L("real (fixed)=%f" ), 33.43),
00323 buf,output, KRenderDefault );
00324
00325
00326 TPtrC str = _L("hello");
00327 RenderVariableString( str );
00328 ExecuteAndRenderVariableFormatted(
00329 buf.Format( _L("str='%S'" ), &str),
00330 buf,output, KRenderDefault );
00331
00332
00333 ExecuteAndRenderVariableFormatted(
00334 buf.Format( _L("str='%s'" ), str.Ptr()),
00335 buf,output, KRenderDefault );
00336
00337
00338 output.Append( _L("\nFormatting align and padding\n") );
00339
00340
00341 ExecuteAndRenderVariableFormatted(
00342 buf.Format( _L("binary=%10b" ), 33),
00343 buf,output, KRenderDefault );
00344
00345
00346 ExecuteAndRenderVariableFormatted(
00347 buf.Format( _L("binary=%010b" ), 33),
00348 buf,output, KRenderDefault );
00349
00350
00351 ExecuteAndRenderVariableFormatted(
00352 buf.Format( _L("binary=%08d" ), 12345),
00353 buf,output, KRenderDefault );
00354
00355 RenderVariableString( str );
00356
00357 ExecuteAndRenderVariableFormatted(
00358 buf.Format( _L("str='%20S'" ), &str),
00359 buf,output, KRenderDefault );
00360
00361
00362 ExecuteAndRenderVariableFormatted(
00363 buf.Format( _L("str='%020S'" ), &str),
00364 buf,output, KRenderDefault );
00365
00366
00367 ExecuteAndRenderVariableFormatted(
00368 buf.Format( _L("str='%=20S'" ), &str),
00369 buf,output, KRenderDefault );
00370
00371
00372 ExecuteAndRenderVariableFormatted(
00373 buf.Format( _L("str='%-20S'" ), &str),
00374 buf,output, KRenderDefault );
00375
00376
00377 ExecuteAndRenderVariableFormatted(
00378 buf.Format( _L("str='%+20S'" ), &str),
00379 buf,output, KRenderDefault );
00380
00381
00382 ExecuteAndRenderVariableFormatted(
00383 buf.Format( _L("str='%=*20S'" ), '.', &str),
00384 buf,output, KRenderDefault );
00385
00386
00387 output.Append( _L("\nOther modifications...\n") );
00388 buf.Copy(_L("abcd"));
00389 RenderVariableString( buf );
00390
00391
00392 ExecuteAndRenderVariableFormatted(
00393 buf.Insert( 2, _L("____") ),
00394 buf, output, KRenderDefault );
00395
00396
00397 ExecuteAndRenderVariableFormatted(
00398 buf.Replace( 3, 2, _L("****") ),
00399 buf, output, KRenderDefault );
00400
00401
00402
00403 ExecuteAndRenderVariableFormatted(
00404 buf.Replace( 1, 0, _L("AA") ),
00405 buf, output, KRenderDefault );
00406
00407
00408
00409 ExecuteAndRenderVariableFormatted(
00410 buf.Replace( 2, 9, _L("") ),
00411 buf, output, KRenderDefault );
00412
00413
00414 buf.Copy( _L("Hello!" ));
00415 RenderVariableString(buf);
00416 ExecuteAndRenderVariableFormatted(
00417 buf.Delete( 1, 2 ),
00418 buf, output, KRenderDefault );
00419
00420
00421 output.Append( _L("\nTrimming. The buf in each case is\n") );
00422 buf.Copy( _L( " Hello World! " ) );
00423 RenderVariableString(buf);
00424
00425
00426 ExecuteAndRenderVariableFormatted(
00427 buf.TrimLeft(),
00428 buf, output, KRenderDefault );
00429
00430
00431 buf.Copy( _L( " Hello World! " ) );
00432 ExecuteAndRenderVariableFormatted(
00433 buf.TrimRight(),
00434 buf, output, KRenderDefault );
00435
00436
00437 buf.Copy( _L( " Hello World! " ) );
00438 ExecuteAndRenderVariableFormatted(
00439 buf.Trim(),
00440 buf, output, KRenderDefault );
00441
00442
00443 buf.Copy( _L( " Hello World! " ) );
00444 ExecuteAndRenderVariableFormatted(
00445 buf.TrimAll(),
00446 buf, output, KRenderDefault );
00447
00448
00449 output.Append( _L("\nFilling...\n") );
00450 buf.Copy( _L( "abcd" ) );
00451 RenderVariableString(buf);
00452
00453 ExecuteAndRenderVariableFormatted(
00454 buf.Fill('*'),
00455 buf, output, KRenderCharacteristics );
00456
00457
00458 ExecuteAndRenderVariableFormatted(
00459 buf.Fill('.', 12),
00460 buf, output, KRenderCharacteristics );
00461
00462
00463 ExecuteAndRenderVariableFormatted(
00464 buf.FillZ(6),
00465 buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00466
00467
00468 ExecuteAndRenderVariableFormatted(
00469 buf.Copy( _L("Hey!") ),
00470 buf, output, KRenderDefault);
00471
00472 ExecuteAndRenderVariableFormatted(
00473 buf.FillZ(),
00474 buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00475
00476
00477 output.Append( _L("\nText conversions...\n") );
00478 buf.Copy( _L( "Hello World" ) );
00479 RenderVariableString(buf);
00480 ExecuteAndRenderVariableFormatted(
00481 buf.LowerCase(),
00482 buf, output, KRenderDefault);
00483
00484 ExecuteAndRenderVariableFormatted(
00485 buf.UpperCase(),
00486 buf, output, KRenderDefault);
00487
00488 ExecuteAndRenderVariableFormatted(
00489 buf.Capitalize(),
00490 buf, output, KRenderDefault);
00491
00492
00493 output.Append( _L("\nSwapping...\n") );
00494 TBuf<20> buf1( _L("buf1!") );
00495 TBuf<20> buf2( _L("buf2**") );
00496 RenderVariableString( buf1 );
00497 RenderVariableString( buf2 );
00498 output.Append( _L("After buf1.Swap(buf2)\n") );
00499 buf1.Swap(buf2);
00500 RenderVariableString( buf1 );
00501 RenderVariableString( buf2 );
00502
00503
00504
00505
00506
00507
00508
00509 iViewer->UpdateView();
00510 }
00511
00512
00513
00514
00515 void CDescriptorExamples::CharacterConversionsL()
00516 {
00517 {
00518
00519 RFs fsSession;
00520 User::LeaveIfError( fsSession.Connect() );
00521 CleanupClosePushL(fsSession);
00522
00523
00524
00525 CCnvCharacterSetConverter *converter;
00526 converter = CCnvCharacterSetConverter::NewLC();
00527 converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierSms7Bit,
00528 fsSession );
00529
00530
00531
00532 const TUint8 SMSSourceBytes[] =
00533 {
00534 54,
00535 2,
00536 27, 61,
00537 53,
00538 27, 101,
00539 };
00540 TPtrC8 SMSEncodedStr(SMSSourceBytes,sizeof(SMSSourceBytes));
00541
00542
00543 TInt state = CCnvCharacterSetConverter::KStateDefault;
00544 TBuf<64> UCS2Buf;
00545 converter->ConvertToUnicode(UCS2Buf, SMSEncodedStr, state);
00546
00547
00548 CleanupStack::PopAndDestroy(2);
00549 }
00550
00551 TPtr output( iViewer->GetViewBuffer() );
00552 CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* sets;
00553 RFs fsSession;
00554 CCnvCharacterSetConverter *converter;
00555
00556
00557
00558
00559 RenderHeader( _L( "CharacterConversions: List converters" ), output );
00560
00561
00562
00563 User::LeaveIfError(fsSession.Connect());
00564 CleanupClosePushL(fsSession);
00565
00566 sets = CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableL(fsSession);
00567 CleanupStack::PushL(sets);
00568
00569 output.Append( _L( "\nList of available character converters:\n" ) );
00570 for( int i=sets->Count()-1; i>=0; i-- )
00571 {
00572 TPtrC name = sets->At(i).Name();
00573 output.AppendFormat( _L(" %S\n"), &name );
00574 }
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616 const TUint8 SMSSourceBytes[] =
00617 {
00618 54,2,27,61,53,27,101
00619 };
00620
00621
00622
00623 converter = CCnvCharacterSetConverter::NewLC();
00624 converter->PrepareToConvertToOrFromL( KCharacterSetIdentifierSms7Bit,
00625 fsSession );
00626
00627
00628
00629 TInt state = CCnvCharacterSetConverter::KStateDefault;
00630 TBuf16<10> unicodeConverted;
00631 TPtrC8 SMSSource( SMSSourceBytes, sizeof( SMSSourceBytes ) );
00632
00633
00634
00635 converter->ConvertToUnicode( unicodeConverted, SMSSource, state );
00636
00637
00638 RenderHeader( _L( "CharacterConversions: SMS to Unicode" ), output );
00639 output.Append( _L("SMS data consist of 7 bit character values (where euro and tilde char consist of two numbers):\n") );
00640 RenderVariableBinary( SMSSource );
00641 output.Append( _L("\nWhile unicode uses 16 bits to store each character:\n") );
00642 RenderVariableBinary( unicodeConverted );
00643 RenderVariableString( unicodeConverted );
00644
00645
00646
00647 RenderHeader( _L( "CharacterConversions: Unicode to SMS" ), output );
00648
00649
00650
00651 TBuf<64> unicodeSource(_L("Some nice chars: \x00F6,\x00E4,\x004F,\x00C4 \x20AC") );
00652
00653
00654
00655
00656
00657
00658 RenderVariableString( unicodeSource );
00659
00660 RenderVariableBinary( unicodeSource );
00661
00662
00663 TBuf8<128> convertedToSMS;
00664 converter->ConvertFromUnicode( convertedToSMS, unicodeSource );
00665 RenderVariableBinary( convertedToSMS );
00666 RenderVariableString( convertedToSMS );
00667
00668 output.Append( _L("\nEuro character is espaced in SMS encoding so number of 'chars' differ:\n") );
00669 RenderResult( unicodeSource.Length() );
00670 RenderResult( convertedToSMS.Length() );
00671
00672 output.Append( _L("\nUnicode consist of 16 bit chars while SMS consist of 8 bit ones. Number of bytes in encodings:\n") );
00673 RenderResult( unicodeSource.Size() );
00674 RenderResult( convertedToSMS.Size() );
00675
00676 iViewer->UpdateView();
00677 CleanupStack::PopAndDestroy(3);
00678 }
00679
00680
00681
00682
00683 void CDescriptorExamples::LexicalAnalysis()
00684 {
00685 TPtr output( iViewer->GetViewBuffer() );
00686 RenderHeader( _L( "LexicalAnalysis" ), output );
00687
00688
00689
00690
00691 TLex lex;
00692
00693 TInt num1;
00694 lex.Assign( _L("-254 is number") );
00695 RenderVariableString(lex.Remainder());
00696
00697 ExecuteAndRenderVariableFormatted(
00698 lex.Val(num1),
00699 num1, output, KRenderDefault );
00700
00701 TUint32 num2;
00702 lex.Assign( _L("2ff is hex number") );
00703
00704 RenderVariableString(lex.Remainder());
00705
00706 ExecuteAndRenderVariableFormatted(
00707 lex.Val(num2, EHex),
00708 num2, output, KRenderDefault );
00709
00710
00711 TReal32 num3;
00712 lex.Assign( _L("234.678 is real number") );
00713 RenderVariableString(lex.Remainder());
00714
00715 lex.Val(num3, '.');
00716 output.Append( _L( "lex.Val(num3, '.') -> num3=" ) );
00717 TRealFormat realFormat;
00718 output.AppendNum( num3, realFormat );
00719
00720 iViewer->UpdateView();
00721 }