examples/ForumNokia/DescriptorExample/src/ManipulatingDescriptors.cpp

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

Generated by  doxygen 1.6.2