00001
00002
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
00014
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
00031
00032 void CDescriptorExamples::NonModifyingMethods()
00033 {
00034 TPtr output( iViewer->GetViewBuffer() );
00035 RenderHeader( _L( "NonModifyingMethods" ), output );
00036
00037
00038
00039 _LIT(KCharacters, "abcdefghijklmnopqrstuvwxyz0123456789");
00040
00041 const TDesC &str = KCharacters();
00042 RenderVariableString(str);
00043
00044
00045 output.Append( _L( "\nCapacity...\n" ) );
00046
00047
00048 RenderResult( str.Length() );
00049
00050
00051
00052 RenderResult( str.Size() );
00053
00054
00055 output.Append( _L( "\nExtracting portions...\n" ) );
00056
00057
00058
00059 RenderResult( str.Left(2) );
00060 RenderResult( str.Left(5) );
00061
00062
00063
00064 RenderResult( str.Right(2) );
00065 RenderResult( str.Right(12) );
00066
00067
00068
00069 RenderResult( str.Mid(2,6) );
00070 RenderResult( str.Mid(33,3) );
00071
00072
00073 RenderResult( str.Mid(28) );
00074 RenderResult( str.Mid(35) );
00075
00076
00077 output.Append( _L( "\nLocating character...\n" ) );
00078
00079
00080 RenderResult( str.Locate('d') );
00081 RenderResultFormatted( _L("1ABAD").Locate('A'),
00082 output, KRenderDefault);
00083 RenderResultFormatted( _L("1ABAD").LocateReverse('A'),
00084 output, KRenderDefault);
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094 output.Append( _L( "\nCompare()\n" ) );
00095
00096
00097 RenderResultFormatted( str.Compare( _L("aaa") ),
00098 output, KRenderDefault);
00099
00100
00101 RenderResultFormatted( str.Compare( _L("zzz") ),
00102 output, KRenderDefault );
00103
00104
00105 RenderResult( str.Compare(str) );
00106
00107
00108 RenderResultFormatted( str.Left(2).Compare(_L("ab")),
00109 output, KRenderDefault);
00110
00111
00112
00113 RenderResultFormatted( str.Left(2).Compare(_L("AB")),
00114 output, KRenderDefault );
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129 output.Append( _L( "\nCompareC()...\n" ) );
00130
00131
00132 RenderResultFormatted( _L("ab").CompareC(_L("AB"),1,NULL),
00133 output, KRenderDefault );
00134
00135
00136
00137
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
00145
00146
00147 TCollationMethod stdMethod = *Mem::CollationMethodByIndex(0);
00148
00149
00150 TCollationMethod strictMethod = stdMethod;
00151 strictMethod.iFlags |= TCollationMethod::EIgnoreNone;
00152
00153
00154
00155
00156
00157
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
00169
00170 void CDescriptorExamples::ModifyingMethodsL()
00171 {
00172
00173 TPtr output( iViewer->GetViewBuffer() );
00174 RenderHeader( _L( "ModifyingMethods" ), output );
00175 TBuf<10> buf10;
00176
00177
00178 output.Append( _L("\nCharacteristics of buffer after initialization:\n") );
00179 RenderResult( buf10.MaxLength() );
00180 RenderResult( buf10.MaxSize() );
00181 RenderResult( buf10.Length() );
00182 RenderResult( buf10.Size() );
00183
00184 TBuf<20> example(_L("Hello"));
00185 RenderVariableFormatted( example, output, KRenderCharacteristics );
00186
00187
00188 output.Append( _L("\nLet's fill the buffer\n") );
00189 buf10.Fill( '*', buf10.MaxLength() );
00190 RenderVariableString( buf10 );
00191 RenderResult( buf10.Length() );
00192 RenderResult( buf10.Size() );
00193
00194 output.Append( _L("\nNow manipilate the length:\n") );
00195
00196
00197 ExecuteAndRenderVariable( buf10.SetLength(3), buf10 );
00198 ExecuteAndRenderVariable( buf10.SetMax(), buf10 );
00199 ExecuteAndRenderVariable( buf10.Zero(), buf10 );
00200
00201
00202 output.Append( _L("\nCopying...\n") );
00203 TPtrC8 narrowHello( _L8( "hello" ) );
00204 RenderVariableString( narrowHello );
00205 TPtrC16 wideHello( _L16( "unIc heLLo" ) );
00206 RenderVariableString( wideHello );
00207
00208 ExecuteAndRenderVariable( buf10.Copy(narrowHello), buf10 );
00209
00210 ExecuteAndRenderVariable( buf10.Copy(wideHello), buf10 );
00211
00212
00213 ExecuteAndRenderVariable( buf10.CopyCP(wideHello), buf10 );
00214
00215 ExecuteAndRenderVariable( buf10.CopyLC(wideHello), buf10 );
00216
00217 ExecuteAndRenderVariable( buf10.CopyUC(wideHello), buf10 );
00218
00219
00220 output.Append( _L("\nRepeating...\n") );
00221 TPtrC abc( _L( "aBc" ) );
00222 RenderVariableString( abc );
00223
00224 ExecuteAndRenderVariable( buf10.Repeat((TText*)abc.Ptr(),2), buf10 );
00225
00226 ExecuteAndRenderVariable( buf10.Repeat(abc), buf10 );
00227
00228
00229 output.Append( _L("\nJustifying...\n") );
00230 RenderVariableString( abc );
00231
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
00237 output.Append( _L("\nRendering numbers...\n") );
00238
00239 ExecuteAndRenderVariable( buf10.Num(12345), buf10 );
00240
00241 ExecuteAndRenderVariable( buf10.Num(65300,EHex), buf10 );
00242
00243 ExecuteAndRenderVariable( buf10.Num(55,EBinary), buf10 );
00244
00245 ExecuteAndRenderVariable( buf10.NumUC(65300,EHex), buf10 );
00246
00247 ExecuteAndRenderVariable( buf10.NumFixedWidth(25,EDecimal,6), buf10 );
00248
00249 ExecuteAndRenderVariable( buf10.NumFixedWidth(0xf05,EHex,6), buf10 );
00250
00251 ExecuteAndRenderVariable( buf10.NumFixedWidth(3,EBinary,8), buf10 );
00252
00253 ExecuteAndRenderVariable( buf10.NumFixedWidthUC(0xf05,EHex,6), buf10 );
00254
00255
00256 TInt64 longNum(0x12345678);
00257 ExecuteAndRenderVariable( buf10.Num(longNum,EHex), buf10 );
00258
00259
00260
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
00270 output.Append( _L("\nFormatting data types\n") );
00271 TBuf<64> buf;
00272 RenderVariableFormatted( buf, output, KRenderCharacteristics );
00273
00274
00275 ExecuteAndRenderVariableFormatted(
00276 buf.Format( _L("binary=%b" ), 33),
00277 buf,output, KRenderDefault );
00278
00279
00280 ExecuteAndRenderVariableFormatted(
00281 buf.Format( _L("decimal=%d" ), 33),
00282 buf,output, KRenderDefault );
00283
00284
00285 ExecuteAndRenderVariableFormatted(
00286 buf.Format( _L("hexadecimal=%x" ), 33),
00287 buf,output, KRenderDefault );
00288
00289
00290 ExecuteAndRenderVariableFormatted(
00291 buf.Format( _L("real (exp)=%e" ), 33.43),
00292 buf,output, KRenderDefault );
00293
00294
00295 ExecuteAndRenderVariableFormatted(
00296 buf.Format( _L("real (fixed)=%f" ), 33.43),
00297 buf,output, KRenderDefault );
00298
00299
00300 TPtrC str = _L("hello");
00301 RenderVariableString( str );
00302 ExecuteAndRenderVariableFormatted(
00303 buf.Format( _L("str='%S'" ), &str),
00304 buf,output, KRenderDefault );
00305
00306
00307 ExecuteAndRenderVariableFormatted(
00308 buf.Format( _L("str='%s'" ), str.Ptr()),
00309 buf,output, KRenderDefault );
00310
00311
00312 output.Append( _L("\nFormatting align and padding\n") );
00313
00314
00315 ExecuteAndRenderVariableFormatted(
00316 buf.Format( _L("binary=%10b" ), 33),
00317 buf,output, KRenderDefault );
00318
00319
00320 ExecuteAndRenderVariableFormatted(
00321 buf.Format( _L("binary=%010b" ), 33),
00322 buf,output, KRenderDefault );
00323
00324
00325 ExecuteAndRenderVariableFormatted(
00326 buf.Format( _L("binary=%08d" ), 12345),
00327 buf,output, KRenderDefault );
00328
00329 RenderVariableString( str );
00330
00331 ExecuteAndRenderVariableFormatted(
00332 buf.Format( _L("str='%20S'" ), &str),
00333 buf,output, KRenderDefault );
00334
00335
00336 ExecuteAndRenderVariableFormatted(
00337 buf.Format( _L("str='%020S'" ), &str),
00338 buf,output, KRenderDefault );
00339
00340
00341 ExecuteAndRenderVariableFormatted(
00342 buf.Format( _L("str='%=20S'" ), &str),
00343 buf,output, KRenderDefault );
00344
00345
00346 ExecuteAndRenderVariableFormatted(
00347 buf.Format( _L("str='%-20S'" ), &str),
00348 buf,output, KRenderDefault );
00349
00350
00351 ExecuteAndRenderVariableFormatted(
00352 buf.Format( _L("str='%+20S'" ), &str),
00353 buf,output, KRenderDefault );
00354
00355
00356 ExecuteAndRenderVariableFormatted(
00357 buf.Format( _L("str='%=*20S'" ), '.', &str),
00358 buf,output, KRenderDefault );
00359
00360
00361 output.Append( _L("\nOther modifications...\n") );
00362 buf.Copy(_L("abcd"));
00363 RenderVariableString( buf );
00364
00365
00366 ExecuteAndRenderVariableFormatted(
00367 buf.Insert( 2, _L("____") ),
00368 buf, output, KRenderDefault );
00369
00370
00371 ExecuteAndRenderVariableFormatted(
00372 buf.Replace( 3, 2, _L("****") ),
00373 buf, output, KRenderDefault );
00374
00375
00376
00377 ExecuteAndRenderVariableFormatted(
00378 buf.Replace( 1, 0, _L("AA") ),
00379 buf, output, KRenderDefault );
00380
00381
00382
00383 ExecuteAndRenderVariableFormatted(
00384 buf.Replace( 2, 9, _L("") ),
00385 buf, output, KRenderDefault );
00386
00387
00388 buf.Copy( _L("Hello!" ));
00389 RenderVariableString(buf);
00390 ExecuteAndRenderVariableFormatted(
00391 buf.Delete( 1, 2 ),
00392 buf, output, KRenderDefault );
00393
00394
00395 output.Append( _L("\nTrimming. The buf in each case is\n") );
00396 buf.Copy( _L( " Hello World! " ) );
00397 RenderVariableString(buf);
00398
00399
00400 ExecuteAndRenderVariableFormatted(
00401 buf.TrimLeft(),
00402 buf, output, KRenderDefault );
00403
00404
00405 buf.Copy( _L( " Hello World! " ) );
00406 ExecuteAndRenderVariableFormatted(
00407 buf.TrimRight(),
00408 buf, output, KRenderDefault );
00409
00410
00411 buf.Copy( _L( " Hello World! " ) );
00412 ExecuteAndRenderVariableFormatted(
00413 buf.Trim(),
00414 buf, output, KRenderDefault );
00415
00416
00417 buf.Copy( _L( " Hello World! " ) );
00418 ExecuteAndRenderVariableFormatted(
00419 buf.TrimAll(),
00420 buf, output, KRenderDefault );
00421
00422
00423 output.Append( _L("\nFilling...\n") );
00424 buf.Copy( _L( "abcd" ) );
00425 RenderVariableString(buf);
00426
00427 ExecuteAndRenderVariableFormatted(
00428 buf.Fill('*'),
00429 buf, output, KRenderCharacteristics );
00430
00431
00432 ExecuteAndRenderVariableFormatted(
00433 buf.Fill('.', 12),
00434 buf, output, KRenderCharacteristics );
00435
00436
00437 ExecuteAndRenderVariableFormatted(
00438 buf.FillZ(6),
00439 buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00440
00441
00442 ExecuteAndRenderVariableFormatted(
00443 buf.Copy( _L("Hey!") ),
00444 buf, output, KRenderDefault);
00445
00446 ExecuteAndRenderVariableFormatted(
00447 buf.FillZ(),
00448 buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00449
00450
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
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
00478
00479
00480
00481
00482
00483 iViewer->UpdateView();
00484 }
00485
00486
00487
00488
00489 void CDescriptorExamples::CharacterConversionsL()
00490 {
00491 {
00492
00493 RFs fsSession;
00494 User::LeaveIfError( fsSession.Connect() );
00495 CleanupClosePushL(fsSession);
00496
00497
00498
00499 CCnvCharacterSetConverter *converter;
00500 converter = CCnvCharacterSetConverter::NewLC();
00501 converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierSms7Bit,
00502 fsSession );
00503
00504
00505
00506 const TUint8 SMSSourceBytes[] =
00507 {
00508 54,
00509 2,
00510 27, 61,
00511 53,
00512 27, 101,
00513 };
00514 TPtrC8 SMSEncodedStr(SMSSourceBytes,sizeof(SMSSourceBytes));
00515
00516
00517 TInt state = CCnvCharacterSetConverter::KStateDefault;
00518 TBuf<64> UCS2Buf;
00519 converter->ConvertToUnicode(UCS2Buf, SMSEncodedStr, state);
00520
00521
00522 CleanupStack::PopAndDestroy(2);
00523 }
00524
00525 TPtr output( iViewer->GetViewBuffer() );
00526 CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* sets;
00527 RFs fsSession;
00528 CCnvCharacterSetConverter *converter;
00529
00530
00531
00532
00533 RenderHeader( _L( "CharacterConversions: List converters" ), output );
00534
00535
00536
00537 User::LeaveIfError(fsSession.Connect());
00538 CleanupClosePushL(fsSession);
00539
00540 sets = CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableL(fsSession);
00541 CleanupStack::PushL(sets);
00542
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
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 const TUint8 SMSSourceBytes[] =
00591 {
00592 54,2,27,61,53,27,101
00593 };
00594
00595
00596
00597 converter = CCnvCharacterSetConverter::NewLC();
00598 converter->PrepareToConvertToOrFromL( KCharacterSetIdentifierSms7Bit,
00599 fsSession );
00600
00601
00602
00603 TInt state = CCnvCharacterSetConverter::KStateDefault;
00604 TBuf16<10> unicodeConverted;
00605 TPtrC8 SMSSource( SMSSourceBytes, sizeof( SMSSourceBytes ) );
00606
00607
00608
00609 converter->ConvertToUnicode( unicodeConverted, SMSSource, state );
00610
00611
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
00620
00621 RenderHeader( _L( "CharacterConversions: Unicode to SMS" ), output );
00622
00623
00624
00625 TBuf<64> unicodeSource(_L("Some nice chars: \x00F6,\x00E4,\x004F,\x00C4 \x20AC") );
00626
00627
00628
00629
00630
00631
00632 RenderVariableString( unicodeSource );
00633
00634 RenderVariableBinary( unicodeSource );
00635
00636
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);
00652 }
00653
00654
00655
00656
00657 void CDescriptorExamples::LexicalAnalysis()
00658 {
00659 TPtr output( iViewer->GetViewBuffer() );
00660 RenderHeader( _L( "LexicalAnalysis" ), output );
00661
00662
00663
00664
00665 TLex lex;
00666
00667 TInt num1;
00668 lex.Assign( _L("-254 is number") );
00669 RenderVariableString(lex.Remainder());
00670
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
00680 ExecuteAndRenderVariableFormatted(
00681 lex.Val(num2, EHex),
00682 num2, output, KRenderDefault );
00683
00684
00685 TReal32 num3;
00686 lex.Assign( _L("234.678 is real number") );
00687 RenderVariableString(lex.Remainder());
00688
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 }