examples/QtQuick/descriptors/manipulatingdescriptors_symbian.cpp

00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation
00006 **
00007 **
00008 ** $QT_BEGIN_LICENSE:BSD$
00009 ** You may use this file under the terms of the BSD license as follows:
00010 **
00011 ** "Redistribution and use in source and binary forms, with or without
00012 ** modification, are permitted provided that the following conditions are
00013 ** met:
00014 **   * Redistributions of source code must retain the above copyright
00015 **     notice, this list of conditions and the following disclaimer.
00016 **   * Redistributions in binary form must reproduce the above copyright
00017 **     notice, this list of conditions and the following disclaimer in
00018 **     the documentation and/or other materials provided with the
00019 **     distribution.
00020 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00021 **     the names of its contributors may be used to endorse or promote
00022 **     products derived from this software without specific prior written
00023 **     permission.
00024 **
00025 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00028 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00029 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00030 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00031 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00032 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00033 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00034 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00036 ** $QT_END_LICENSE$
00037 **
00038 ****************************************************************************/
00039 
00040 
00041 
00042 // System Includes.
00043 #include <e32std.h>
00044 #include <e32base.h>
00045 #include <charconv.h>
00046 #include <platform/collate.h>
00047 
00049 #include "stringrenderer_symbian.h"
00050 #include "descriptorexamples_symbian.h"
00051 
00058 #define RenderVariableString(aVariableName) \
00059     RenderVariableFormatted(aVariableName, output, KRenderDefault);
00060 
00061 #define RenderVariableBinary(aVariableName) \
00062     RenderVariableFormatted(aVariableName, output, KRenderContentAsBinary);
00063 
00064 #define RenderResult(aDeclaration) \
00065     RenderResultFormatted(aDeclaration, output, KRenderDefault);
00066 
00067 #define ExecuteAndRenderVariable(aDeclaration, aVariable) \
00068     ExecuteAndRenderVariableFormatted(aDeclaration, aVariable, output, \
00069                                       KRenderDefault );
00070 
00075 void CDescriptorExamples::NonModifyingMethods()
00076     {
00077     _LIT(KNonModifyingMethods,"NonModifyingMethods");
00078     TPtr output( iViewer->GetViewBuffer() );
00079     RenderHeader( KNonModifyingMethods, output );
00080 
00081     // ---------- Initialize an example string ----------
00082     // this declares a literal with a few characters
00083     _LIT(KCharacters, "abcdefghijklmnopqrstuvwxyz0123456789");
00084     // this casts the literal to non modifiable descriptor
00085     const TDesC &str = KCharacters();
00086     RenderVariableString(str);
00087 
00088     // ---------- Capacity ----------
00089     _LIT(KCapacityMsg,"\nCapacity...\n");
00090     output.Append( KCapacityMsg );
00091 
00092     // Length() returns number of characters in descriptor
00093     RenderResult( str.Length() ); // 36
00094 
00095     // Size() returns number of bytes in descriptor - in unicode buid it is
00096     //        2*Length() and in non-unicode build it is same as Length()
00097     RenderResult( str.Size() ); // 72
00098 
00099     // ---------- Extracting portions ----------
00100     _LIT(KExtractionMsg,"\nExtracting portions...\n");
00101     output.Append( KExtractionMsg );
00102 
00103     // Left(count) returns a string witn "count" number of characters
00104     // calculated from left side of the string
00105     RenderResult( str.Left(2) ); // "ab"
00106     RenderResult( str.Left(5) ); // "abcde"
00107 
00108     // Right(count) returns a string witn "count" number of characters
00109     // calculated from right side of the string
00110     RenderResult( str.Right(2) ); // "89"
00111     RenderResult( str.Right(12) ); // "yz0123456789"
00112 
00113     // Mid(index, count) returns a portion from string starting
00114     // from "index" and having "count" number of characters
00115     RenderResult( str.Mid(2,6) ); // "cdefgh"
00116     RenderResult( str.Mid(33,3) ); // "789"
00117 
00118     // if "count" is omitted the rest of the string from "index" is returned
00119     RenderResult( str.Mid(28) ); // "23456789"
00120     RenderResult( str.Mid(35) ); // "9"
00121 
00122     // ---------- Locating character ----------
00123     _LIT(KLocatingMsg, "\nLocating character...\n");
00124     output.Append( KLocatingMsg );
00125 
00126     // locate index of given character
00127     _LIT(KString, "1ABAD");
00128     RenderResult( str.Locate('d') ); // 3
00129     RenderResultFormatted( KString().Locate('A'),
00130         output, KRenderDefault); // "1"
00131     RenderResultFormatted( KString().LocateReverse('A'),
00132         output, KRenderDefault); // "1"
00133 
00134     // ---------- Compare ----------
00135     // Comparison with method Compare() uses the binary comparison approach to
00136     // compare each character in string. For example value of character
00137     // 'A' has unicode value of 65 and 'a' 97.
00138     //
00139     // Both strings could have been converted to lower or upper case to perform
00140     // case insensitive comparison. However, there exists also CompareC() method
00141     // below that introduces a method to compare in case insensitive manner.
00142     _LIT(KCompareMsg, "\nCompare()\n");
00143     output.Append( KCompareMsg );
00144 
00145     // examplerun: str.Compare(_L("aaa"))=1
00146     _LIT(KString1, "aaa");
00147     RenderResultFormatted( str.Compare( KString1 ),
00148         output, KRenderDefault); // >0 : str is greater
00149 
00150     // examplerun: str.Compare(_L("zzz"))=-25
00151     _LIT(KString2,"zzz");
00152     RenderResultFormatted( str.Compare( KString2 ),
00153         output, KRenderDefault ); // <0 : str is less
00154 
00155     // examplerun: str.Compare(str)=0
00156     RenderResult( str.Compare(str) );       // 0  : str is equal
00157 
00158     // examplerun: str.Left(2).Compare(_L("ab"))=0
00159     _LIT(KString3,"ab");
00160     RenderResultFormatted( str.Left(2).Compare(KString3),
00161         output, KRenderDefault); // 0 : equal
00162 
00163     // examplerun: str.Left(2).Compare(_L("AB"))=32
00164     // 'A' (65) > 'a' (97)
00165      _LIT(KString4,"AB");
00166     RenderResultFormatted( str.Left(2).Compare(KString4),
00167         output, KRenderDefault ); // 0 : equal
00168 
00169     // ---------- Comparing (Collated) ----------
00170     // To perform collated comparisons, method
00171     //
00172     //   CompareC(TDesC&, TInt aMaxLevel, const TCollationMethod*)
00173     //
00174     // is used. The aMaxLevel parameter can be used to perform specific rules:
00175     //
00176     // 0 - only test the character identity; accents and case are ignored
00177     // 1 - test the character identity and accents; case is ignored
00178     // 2 - test the character identity, accents and case
00179     // 3 - test the Unicode value as well as the character identity,
00180     //     accents and case.
00181     //
00182     _LIT(KCollatedCmpMsg,"\nCompareC()...\n");
00183     output.Append( KCollatedCmpMsg );
00184 
00185     // To perform case insensitive comparison, level 1 is used
00186     RenderResultFormatted( KString3().CompareC(KString4,1,NULL),
00187         output, KRenderDefault ); // 0 : equal
00188 
00189     // note that when using collated comparison as case sensitive manner
00190     // the string "ab" is less than "AB" while using standars Compare()
00191     // the "ab" is greater than "AB"!!!
00192     RenderResultFormatted( KString3().CompareC(KString4,2,NULL),
00193         output, KRenderDefault );
00194     RenderResultFormatted( KString3().Compare(KString4),
00195         output, KRenderDefault );
00196 
00197     // ----------
00198     // The natural comparison ignores white space differences.
00199     // This and other specific behaviour can be changed by modifying
00200     // the standard behaviour.
00201     TCollationMethod stdMethod = *Mem::CollationMethodByIndex(0); // std impl
00202 
00203     // dont ignore punctuation and spaces
00204     TCollationMethod strictMethod = stdMethod; // copy std impl
00205     strictMethod.iFlags |= TCollationMethod::EIgnoreNone; // modify copy
00206 
00207     // use level 1 (case ignored) in both cases to compare. Then use
00208     // standard method and modified one. Examplerun:
00209     //
00210     //     _LIT(KString5, "a   b");
00211     //     _LIT(KString6, "A B");
00212     //
00213     //    KString5.CompareC(KString6,1,&stdMethod)=0
00214     //    KString5.CompareC(KString6,1,&strictMethod)=-2
00215     //
00216     _LIT(KString5, "a   b");
00217     _LIT(KString6, "A B");
00218     RenderResultFormatted( KString5().CompareC(KString6,1,&stdMethod),
00219         output, KRenderDefault );
00220     RenderResultFormatted( KString5().CompareC(KString6,1,&strictMethod),
00221         output, KRenderDefault );
00222 
00223     iViewer->UpdateView();
00224     }
00230 void CDescriptorExamples::ModifyingMethodsL()
00231     {
00232     // this is used to point to correct position in view buffer
00233     TPtr output( iViewer->GetViewBuffer() );
00234     _LIT(KModifyingMethods, "ModifyingMethods");
00235     RenderHeader( KModifyingMethods, output );
00236     TBuf<10> buf10;
00237 
00238     // ---------- Setting and viewing characteristics ----------
00239     _LIT(KCharacteristicsMsg, "\nCharacteristics of buffer after initialization:\n");
00240     output.Append( KCharacteristicsMsg );
00241     RenderResult( buf10.MaxLength() ); // max number of chars
00242     RenderResult( buf10.MaxSize() ); // max number of bytes
00243     RenderResult( buf10.Length() ); // number of chars
00244     RenderResult( buf10.Size() ); // number of bytes
00245 
00246     _LIT(KHello, "Hello");
00247     TBuf<20> example(KHello);
00248     RenderVariableFormatted( example, output, KRenderCharacteristics );
00249 
00250     // first init the data in buffer with asterix
00251     _LIT(KFillMsg, "\nLet's fill the buffer\n");
00252     output.Append( KFillMsg );
00253     buf10.Fill( '*', buf10.MaxLength() );
00254     RenderVariableString( buf10 ); // max number of bytes
00255     RenderResult( buf10.Length() ); // number of chars
00256     RenderResult( buf10.Size() ); // number of bytes
00257 
00258     _LIT(KLengthManipulateMsg, "\nNow manipilate the length:\n");
00259     output.Append( KLengthManipulateMsg );
00260     // set the current length to three. The data that exist in the
00261     // buffer is as is
00262     ExecuteAndRenderVariable( buf10.SetLength(3), buf10 );
00263     ExecuteAndRenderVariable( buf10.SetMax(), buf10 );
00264     ExecuteAndRenderVariable( buf10.Zero(), buf10 );
00265 
00266     // ---------- Copying ----------
00267     _LIT(KCopyMsg, "\nCopying...\n");
00268     output.Append( KCopyMsg );
00269 
00270     _LIT8(KNarrowHello, "hello");
00271     TPtrC8 narrowHello( KNarrowHello );
00272     RenderVariableString( narrowHello );
00273 
00274     _LIT16(KWideHello, "unIc heLLo");
00275     TPtrC16 wideHello( KWideHello );
00276     RenderVariableString( wideHello );
00277     // copy contents of 8 bit descriptor to 16 bit descriptor
00278     ExecuteAndRenderVariable( buf10.Copy(narrowHello), buf10 );
00279     // copy contents of 16 bit descriptor to 16 bit descriptor
00280     ExecuteAndRenderVariable( buf10.Copy(wideHello), buf10 );
00281     // copy contents of 16 bit descriptor to 16 bit descriptor
00282     // and capitalize the content
00283     ExecuteAndRenderVariable( buf10.CopyCP(wideHello), buf10 );
00284     // copy and set to lower case
00285     ExecuteAndRenderVariable( buf10.CopyLC(wideHello), buf10 );
00286     // copy and set to Uppper case
00287     ExecuteAndRenderVariable( buf10.CopyUC(wideHello), buf10 );
00288 
00289     // ---------- Repeating ----------
00290     _LIT(KRepeatMsg, "\nRepeating...\n");
00291     output.Append( KRepeatMsg );
00292 
00293     _LIT(KaBc, "aBc");
00294     TPtrC abc( KaBc );
00295     RenderVariableString( abc );
00296     // write abs twise to the descriptor
00297     ExecuteAndRenderVariable( buf10.Repeat((TText*)abc.Ptr(),2), buf10 );
00298     // write abs until to the maximum size
00299     ExecuteAndRenderVariable( buf10.Repeat(abc), buf10 );
00300 
00301     // ---------- Justifying ----------
00302     _LIT(KJustifyMsg, "\nJustifying...\n");
00303     output.Append( KJustifyMsg );
00304     RenderVariableString( abc );
00305     // show aligning of string "abs" to our buffer
00306     ExecuteAndRenderVariable( buf10.Justify(abc,8,ELeft,'_'), buf10 );
00307     ExecuteAndRenderVariable( buf10.Justify(abc,8,ECenter,'*'), buf10 );
00308     ExecuteAndRenderVariable( buf10.Justify(abc,10,ERight,'.'), buf10 );
00309 
00310     // ---------- Rendering numbers ----------
00311     _LIT(KRenderingMsg, "\nRendering numbers...\n");
00312     output.Append( KRenderingMsg );
00313     // decimal number
00314     ExecuteAndRenderVariable( buf10.Num(12345), buf10 );
00315     // hexadecimal number. characters in lover case
00316     ExecuteAndRenderVariable( buf10.Num(65300,EHex), buf10 );
00317     // binary number
00318     ExecuteAndRenderVariable( buf10.Num(55,EBinary), buf10 );
00319     // hexadecimal number. characters in upper case
00320     ExecuteAndRenderVariable( buf10.NumUC(65300,EHex), buf10 );
00321     // fixed width decimal number
00322     ExecuteAndRenderVariable( buf10.NumFixedWidth(25,EDecimal,6), buf10 );
00323     // fixed width hexadecimal number
00324     ExecuteAndRenderVariable( buf10.NumFixedWidth(0xf05,EHex,6), buf10 );
00325     // fixed width binary number
00326     ExecuteAndRenderVariable( buf10.NumFixedWidth(3,EBinary,8), buf10 );
00327     // fixed width hexadecimal number, upper case
00328     ExecuteAndRenderVariable( buf10.NumFixedWidthUC(0xf05,EHex,6), buf10 );
00329 
00330     // declare 64 bit number that has now only lower 32 bits set
00331     TInt64 longNum(0x12345678);
00332     ExecuteAndRenderVariable( buf10.Num(longNum,EHex), buf10 );
00333     // multiply the result so that it needs more than 32 bits and show results.
00334     // The result 123456780 > FFFFFFFF that is the maximum size 32 bit
00335     // integer can store
00336     ExecuteAndRenderVariable( buf10.Num(longNum*16,EHex), buf10 );
00337 
00338     TRealFormat realFormatter(8);
00339     ExecuteAndRenderVariable( buf10.Num(3.5,realFormatter), buf10 );
00340     ExecuteAndRenderVariable( buf10.Num(10.0/3,realFormatter), buf10 );
00341     ExecuteAndRenderVariable( buf10.Num(123.0*1000*1000*1000,realFormatter),
00342                               buf10 );
00343 
00344     // ---------- Formatting ----------
00345     _LIT(KFormattingMsg, "\nFormatting data types\n");
00346     output.Append( KFormattingMsg );
00347     TBuf<64> buf;
00348     RenderVariableFormatted( buf, output, KRenderCharacteristics );
00349 
00350     // binary format using %b
00351     _LIT(KBinary, "binary=%b");
00352     ExecuteAndRenderVariableFormatted(
00353         buf.Format( KBinary, 33),
00354         buf,output, KRenderDefault );
00355 
00356     // integer format using %d
00357     _LIT(KDecimal, "decimal=%d");
00358     ExecuteAndRenderVariableFormatted(
00359         buf.Format( KDecimal, 33),
00360         buf,output, KRenderDefault );
00361 
00362     // hexadecimal format using %x
00363     _LIT(KHexaDecimal, "hexadecimal=%x");
00364     ExecuteAndRenderVariableFormatted(
00365         buf.Format( KHexaDecimal, 33),
00366         buf,output, KRenderDefault );
00367 
00368     // real as exponent format
00369     _LIT(KRealExp, "real (exp)=%e");
00370     ExecuteAndRenderVariableFormatted(
00371         buf.Format( KRealExp, 33.43),
00372         buf,output, KRenderDefault );
00373 
00374     // real as fixed format
00375     _LIT(KRealFixed, "real (fixed)=%f");
00376     ExecuteAndRenderVariableFormatted(
00377         buf.Format( KRealFixed, 33.43),
00378         buf,output, KRenderDefault );
00379 
00380     // pointer to descriptor
00381     _LIT(Khellostring, "hello");
00382     _LIT(KString, "str='%S'");
00383     _LIT(KString1, "str='%s'");
00384     TPtrC str = Khellostring();
00385     RenderVariableString( str );
00386     ExecuteAndRenderVariableFormatted(
00387         buf.Format( KString, &str),
00388         buf,output, KRenderDefault );
00389 
00390     // pointer to null terminated character array
00391     ExecuteAndRenderVariableFormatted(
00392         buf.Format( KString1, str.Ptr()),
00393         buf,output, KRenderDefault );
00394 
00395     // ---------- Formatting align and padding ----------
00396     _LIT(KalignpadMsg, "\nFormatting align and padding\n");
00397     output.Append( KalignpadMsg );
00398 
00399     // padding width: 10, default pad character, default alignment
00400     _LIT(KString2, "binary=%10b");
00401     ExecuteAndRenderVariableFormatted(
00402         buf.Format( KString2, 33),
00403         buf,output, KRenderDefault );
00404 
00405     // padding width: 10, '0' as pad character, default alignment
00406     _LIT(KString3, "binary=%010b");
00407     ExecuteAndRenderVariableFormatted(
00408         buf.Format( KString3, 33),
00409         buf,output, KRenderDefault );
00410 
00411     // padding width: 8, '0' as pad character, default alignment
00412     _LIT(KString4, "binary=%08d");
00413     ExecuteAndRenderVariableFormatted(
00414         buf.Format( KString4, 12345),
00415         buf,output, KRenderDefault );
00416 
00417     RenderVariableString( str );
00418     _LIT(KString5, "str='%20S'");
00419     // padding width: 20, default pad character, default alignment
00420     ExecuteAndRenderVariableFormatted(
00421         buf.Format( KString5, &str),
00422         buf,output, KRenderDefault );
00423 
00424     // padding width: 20, '0' as pad character, default alignment
00425     _LIT(KString6, "str='%020S'");
00426     ExecuteAndRenderVariableFormatted(
00427         buf.Format( KString6, &str),
00428         buf,output, KRenderDefault );
00429 
00430     // padding width: 20, default (space) as pad character, center alignment
00431     _LIT(KString7, "str='%=20S'");
00432     ExecuteAndRenderVariableFormatted(
00433         buf.Format( KString7, &str),
00434         buf,output, KRenderDefault );
00435 
00436     // padding width: 20, default (space) as pad character, left alignment
00437     _LIT(KSTring8, "str='%-20S'");
00438     ExecuteAndRenderVariableFormatted(
00439         buf.Format( KSTring8, &str),
00440         buf,output, KRenderDefault );
00441 
00442     // padding width: 20, default (space) as pad character, right alignment
00443     _LIT(KString9, "str='%+20S'");
00444     ExecuteAndRenderVariableFormatted(
00445         buf.Format( KString9, &str),
00446         buf,output, KRenderDefault );
00447 
00448     // padding width: 20, custom pad character, center alignment
00449     _LIT(KString10, "str='%=*20S'");
00450     ExecuteAndRenderVariableFormatted(
00451         buf.Format( KString10, '.', &str),
00452         buf,output, KRenderDefault );
00453 
00454     // ---------- Modifying content in other means ----------
00455     _LIT(KOtherModificationMsg, "\nOther modifications...\n");
00456     output.Append( KOtherModificationMsg );
00457     _LIT(KStrabcd, "abcd");
00458     buf.Copy(KStrabcd);
00459     RenderVariableString( buf );
00460 
00461     // insert a string to middle of buffer
00462     _LIT(KString11, "____");
00463     ExecuteAndRenderVariableFormatted(
00464         buf.Insert( 2, KString11 ),
00465         buf, output, KRenderDefault );
00466 
00467     // replaces a portion with given string
00468     _LIT(KString12, "****");
00469     ExecuteAndRenderVariableFormatted(
00470         buf.Replace( 3, 2, KString12 ),
00471         buf, output, KRenderDefault );
00472 
00473     // _LIT(KStrAA, "AA");
00474     // replaces but since portion size is 0, this is same
00475     // as insert( 1, KStrAA );
00476     _LIT(KStrAA, "AA");
00477     ExecuteAndRenderVariableFormatted(
00478         buf.Replace( 1, 0, KStrAA ),
00479         buf, output, KRenderDefault );
00480 
00481     // replaces a portion but since no data empty, this is same
00482     // as delete( 1, 9 );
00483     _LIT(KStrblank, "");
00484     ExecuteAndRenderVariableFormatted(
00485         buf.Replace( 2, 9, KStrblank ),
00486         buf, output, KRenderDefault );
00487 
00488     // delete from index 1, 2 characters
00489     _LIT(KStrHello, "Hello!");
00490     buf.Copy( KStrHello);
00491     RenderVariableString(buf);
00492     ExecuteAndRenderVariableFormatted(
00493         buf.Delete( 1, 2 ),
00494         buf, output, KRenderDefault );
00495 
00496     // ---------- Trimming ----------
00497     _LIT(KTrimMsg, "\nTrimming. The buf in each case is\n");
00498     _LIT(KStrHelloWorld, "    Hello   World!  ");
00499     output.Append( KTrimMsg );
00500     buf.Copy( KStrHelloWorld );
00501     RenderVariableString(buf);
00502 
00503     // trim from left
00504     ExecuteAndRenderVariableFormatted(
00505         buf.TrimLeft(),
00506         buf, output, KRenderDefault );
00507 
00508     // trim from right
00509     buf.Copy( KStrHelloWorld );
00510     ExecuteAndRenderVariableFormatted(
00511         buf.TrimRight(),
00512         buf, output, KRenderDefault );
00513 
00514     // trim from left & right
00515     buf.Copy( KStrHelloWorld );
00516     ExecuteAndRenderVariableFormatted(
00517         buf.Trim(),
00518         buf, output, KRenderDefault );
00519 
00520     // trim from left & right & from middle
00521     buf.Copy( KStrHelloWorld );
00522     ExecuteAndRenderVariableFormatted(
00523         buf.TrimAll(),
00524         buf, output, KRenderDefault );
00525 
00526     // ---------- Filling ----------
00527     _LIT(KFillingMsg, "\nFilling...\n");
00528     output.Append( KFillingMsg );
00529     buf.Copy( KStrabcd );
00530     RenderVariableString(buf);
00531     // fill chars from zero index to current length with given char
00532     ExecuteAndRenderVariableFormatted(
00533         buf.Fill('*'),
00534         buf, output, KRenderCharacteristics );
00535 
00536     // fill chars from zero index to 11 with given char
00537     ExecuteAndRenderVariableFormatted(
00538         buf.Fill('.', 12),
00539         buf, output, KRenderCharacteristics );
00540 
00541     // fill chars from zero index to 6 with binary zero
00542     ExecuteAndRenderVariableFormatted(
00543         buf.FillZ(6),
00544         buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00545 
00546     // change content of buffer (so length changes)
00547     _LIT(KHey, "Hey!");
00548     ExecuteAndRenderVariableFormatted(
00549         buf.Copy( KHey ),
00550         buf, output, KRenderDefault);
00551     // fill chars from zero index to current length binary zero
00552     ExecuteAndRenderVariableFormatted(
00553         buf.FillZ(),
00554         buf, output, KRenderCharacteristics | KRenderContentAsBinary);
00555 
00556     // ---------- Filling ----------
00557     _LIT(KTxtConvertMsg, "\nText conversions...\n");
00558     _LIT(KHelloWorld, "Hello World");
00559     output.Append( KTxtConvertMsg );
00560     buf.Copy( KHelloWorld );
00561     RenderVariableString(buf);
00562     ExecuteAndRenderVariableFormatted(
00563         buf.LowerCase(),
00564         buf, output, KRenderDefault);
00565 
00566     ExecuteAndRenderVariableFormatted(
00567         buf.UpperCase(),
00568         buf, output, KRenderDefault);
00569 
00570     ExecuteAndRenderVariableFormatted(
00571         buf.Capitalize(),
00572         buf, output, KRenderDefault);
00573 
00574     // ---------- Swapping ----------
00575     _LIT(KSwapMsg, "\nSwapping...\n");
00576     output.Append( KSwapMsg );
00577     _LIT(Kbuf1, "buf1!");
00578     _LIT(Kbuf2, "buf2**");
00579     TBuf<20> buf1( Kbuf1 );
00580     TBuf<20> buf2( Kbuf2 );
00581     RenderVariableString( buf1 );
00582     RenderVariableString( buf2 );
00583     _LIT(KSwapResult, "After buf1.Swap(buf2)\n");
00584     output.Append( KSwapResult );
00585     buf1.Swap(buf2);
00586     RenderVariableString( buf1 );
00587     RenderVariableString( buf2 );
00588 
00589     // ---------- AppendXXX ----------
00590     // AppendXXX() methods are similar like other methods in descriptor API.
00591     // They just append to the end of the descriptor buffer instead of
00592     // replacing the current content. That's why appending methods are not
00593     // described here.
00594 
00595     iViewer->UpdateView();
00596     }
00602 void CDescriptorExamples::CharacterConversionsL()
00603     {
00604 {
00605     // Create session to the file server
00606     RFs fsSession;
00607     User::LeaveIfError( fsSession.Connect() );
00608     CleanupClosePushL(fsSession);
00609 
00610     // Create character converter that can convert between
00611     // SMS encoding and UCS-2
00612     CCnvCharacterSetConverter *converter;
00613     converter = CCnvCharacterSetConverter::NewLC();
00614     converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierSms7Bit,
00615                                          fsSession );
00616 
00617     // Declare descriptor containing a string encoded with
00618     // 7-bit SMS encoding.
00619     const TUint8 SMSSourceBytes[] =
00620         {
00621         54,      // character '6'  (takes one byte)
00622         2,       // character '$'  (takes one byte)
00623         27, 61,  // character '~'  (takes two bytes)
00624         53,      // character '5'  (takes one byte)
00625         27, 101, // euro character (takes two bytes)
00626         };
00627     TPtrC8 SMSEncodedStr(SMSSourceBytes,sizeof(SMSSourceBytes));
00628 
00629     // Convert the SMS encoded message to the UCS-2 encoding
00630     TInt state = CCnvCharacterSetConverter::KStateDefault;
00631     TBuf<64> UCS2Buf;
00632     converter->ConvertToUnicode(UCS2Buf, SMSEncodedStr, state);
00633 
00634     // remove objects from the cleanup stack
00635     CleanupStack::PopAndDestroy(2); // converter, fsSession
00636 }
00637     // this is used to point to correct position in view buffer
00638     TPtr output( iViewer->GetViewBuffer() );
00639     CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* sets;
00640     RFs fsSession;
00641     CCnvCharacterSetConverter *converter;
00642 
00643     // ---------- List available converters ----------
00644     // This example lists all character sets converters available in the
00645     // system.
00646     _LIT(KConvertersList, "CharacterConversions: List converters");
00647     RenderHeader( KConvertersList, output );
00648 
00649     // open connection to file server and push the instance to the cleanup
00650     // stack
00651     User::LeaveIfError(fsSession.Connect());
00652     CleanupClosePushL(fsSession);
00653     // get list of character sets and push it to cleanup stack
00654     sets = CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableL(fsSession);
00655     CleanupStack::PushL(sets);
00656     // show names
00657     _LIT(KList, "\nList of available character converters:\n");
00658     output.Append( KList );
00659     _LIT(KName, "  %S\n");
00660     for( int i=sets->Count()-1; i>=0; i-- )
00661     {
00662         TPtrC name = sets->At(i).Name();
00663         output.AppendFormat( KName, &name );
00664     }
00665 
00666     // ---------- Convert from 7-bit SMS to Unicode ----------
00667     // This example converts SMS to Unicode string.
00668     //
00669     // SMS character set consist of characters whose value is specified with 7
00670     // bits. So 128 characters are available in 7-bit SMS character set (default
00671     // alphabet). However, character set is extended so that if a character in
00672     // message is ESC (0x1B) the next character is treated as extended character
00673     // outside the default alphabet. So 7-bit SMS encoding can represent 255
00674     // different characters when using extended behaviour.
00675     //
00676     // The default alphabet is specified in ETSI "GSM 03.38". However, to
00677     // quickly view standard characters and extended ones, refer to
00678     // (http://www.ozeki.hu/index.phtml?ow_page_number=137) or to
00679     // (http://www.csoft.co.uk/character_sets/gsm.htm) or google with search
00680     // string "GSM character set" or "SMS character set".
00681     //
00682     // GSM 03.38 specification specifies that SMS message can be encoded with
00683     // four character encodings. This example codes the string with default
00684     // 7-bit SMS alphabet. SMS can consist of maximum of 160 characters. Packing
00685     // mechanism specified in GSM 03.38 makes it possible to pack 160 7-bit
00686     // characters to 140 bytes.
00687     //
00688     // Symbian provices character conversion for SMS 7-bit SMS data. However, it
00689     // doesn't use packed format specified in GSM 03.38. Instead, it treats the
00690     // binary array (of 8 bit numbers) to store 7 bit SMS characters.
00691     //
00692     // Lets assume that we have a SMS that consist of string "6$~5e" (where the
00693     // 'e' is meant to be an euro sign). The SMS and Unicode character codes for
00694     // the characters are:
00695     //
00696     // char | SMS value (7bit) | Unicode value (16 bit)
00697     // ------------------------------------------------
00698     // '6'  |  54              |   54
00699     // '$'  |   2              |   36
00700     // '~'  |  27              |  126 // Extended character coded
00701     //      |  61              |      // with two 7 bit values: ESC+code
00702     // '5'  |  53              |   53
00703     // 'e'  |  27              | 8364 // "Euro sign": Extended character
00704     //      | 101              |      // coded with two 7 bit values: ESC+code
00705     //
00706     const TUint8 SMSSourceBytes[] =
00707         {
00708         54,2,27,61,53,27,101
00709         };
00710 
00711     // create converter that converts character data between 7-bit GSM data and
00712     // Unicode
00713     converter = CCnvCharacterSetConverter::NewLC();
00714     converter->PrepareToConvertToOrFromL( KCharacterSetIdentifierSms7Bit,
00715                                           fsSession );
00716 
00717     // declare output buffer and declare a 8 bit descriptor to describe our
00718     // 7-bit SMS data.
00719     TInt state = CCnvCharacterSetConverter::KStateDefault;
00720     TBuf16<10> unicodeConverted;
00721     TPtrC8 SMSSource( SMSSourceBytes, sizeof( SMSSourceBytes ) );
00722 
00723     // convert the 7-bit SMS data to Unicode characters and store results to
00724     // variable "unicodeConverted"
00725     converter->ConvertToUnicode( unicodeConverted, SMSSource, state );
00726 
00727     // show unicode string converted from SMS ("6$~5€");
00728     _LIT(KMsg1, "CharacterConversions: SMS to Unicode");
00729     RenderHeader( KMsg1, output );
00730     _LIT(KMsg2, "SMS data consist of 7 bit character values (where euro and tilde char consist of two numbers):\n");
00731     output.Append( KMsg2 );
00732     RenderVariableBinary( SMSSource );
00733     _LIT(KMsg3, "\nWhile unicode uses 16 bits to store each character:\n");
00734     output.Append( KMsg3 );
00735     RenderVariableBinary( unicodeConverted );
00736     RenderVariableString( unicodeConverted );
00737 
00738     // ---------- Unicode To SMS ----------
00739     // Declare unicode string that consist "normal" and special characters
00740     _LIT(KMsg4, "CharacterConversions: Unicode to SMS");
00741     RenderHeader( KMsg4, output );
00742     
00743     //nice characters but can't be compiled with GCCE ;)
00744     _LIT(KUnicodeSrc, "Some nice chars: \x00F6,\x00E4,\x004F,\x00C4 \x20AC");
00745     TBuf<64> unicodeSource( KUnicodeSrc );
00746     
00747     //ö == 00F6 
00748     //Ö == 004F
00749     //ä == 00E4
00750     //Ä == 00C4
00751     
00752     RenderVariableString( unicodeSource ); 
00753     
00754     RenderVariableBinary( unicodeSource );
00755 
00756     // lets convert the unicode to 7 bit SMS
00757     TBuf8<128> convertedToSMS;
00758     converter->ConvertFromUnicode( convertedToSMS, unicodeSource );
00759     RenderVariableBinary( convertedToSMS );
00760     RenderVariableString( convertedToSMS );
00761 
00762     _LIT(KMsg5, "\nEuro character is espaced in SMS encoding so number of 'chars' differ:\n");
00763     output.Append( KMsg5 );
00764     RenderResult( unicodeSource.Length() );
00765     RenderResult( convertedToSMS.Length() );
00766 
00767     _LIT(KMsg6, "\nUnicode consist of 16 bit chars while SMS consist of 8 bit ones. Number of bytes in encodings:\n");
00768     output.Append( KMsg6 );
00769     RenderResult( unicodeSource.Size() );
00770     RenderResult( convertedToSMS.Size() );
00771 
00772     iViewer->UpdateView();
00773     CleanupStack::PopAndDestroy(3); // fsSession, sets, converter
00774     }
00780 void CDescriptorExamples::LexicalAnalysis()
00781     {
00782     TPtr output( iViewer->GetViewBuffer() );
00783     _LIT(KLexicalAnalysis, "LexicalAnalysis");
00784     RenderHeader( KLexicalAnalysis, output );
00785 
00786     // TLex provides methods for parsin text. Its usage is quite well
00787     // documented in symbian documentation "Using TLex". However, converting
00788     // strings to numbers is not explained. So this example explains them.
00789     TLex lex;
00790 
00791     _LIT(KNum1, "-254 is number");
00792     TInt num1;
00793     lex.Assign( KNum1 );
00794     RenderVariableString(lex.Remainder());
00795     // parse integer number
00796     ExecuteAndRenderVariableFormatted(
00797         lex.Val(num1),
00798         num1, output, KRenderDefault );
00799 
00800     _LIT(KNum2, "2ff is hex number");
00801     TUint32 num2;
00802     lex.Assign( KNum2 );
00803 
00804     RenderVariableString(lex.Remainder());
00805     // parse hex number
00806     ExecuteAndRenderVariableFormatted(
00807         lex.Val(num2, EHex),
00808         num2, output, KRenderDefault );
00809 
00810     // parse float
00811     _LIT(KNum3, "234.678 is real number");
00812     TReal32 num3;
00813     lex.Assign( KNum3 );
00814     RenderVariableString(lex.Remainder());
00815     // parses the value as 234.677993774414!
00816     lex.Val(num3, '.');
00817     _LIT(KNum3Value, "lex.Val(num3, '.') -> num3=");
00818     output.Append( KNum3Value );
00819     TRealFormat realFormat;
00820     output.AppendNum( num3, realFormat );
00821 
00822     iViewer->UpdateView();
00823     }

Generated by  doxygen 1.6.2