examples/ForumNokia/DescriptorExample/src/StringRenderer.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 #include "StringRenderer.h"
00006 
00007 // -----------------------------------------------------------------------------
00008 // Append() documented in StringRenderer.h.
00009 // -----------------------------------------------------------------------------
00010 void Append( const TDesC8 &aSrc, TDes16 &aDst )
00011     {
00012     for( TInt i=0; i < aSrc.Length(); i++ )
00013         {
00014         aDst.Append(aSrc[i]);
00015         }
00016     }
00017 
00018 void Append( const TDesC16 &aSrc, TDes16 &aDst )
00019     {
00020     aDst.Append( aSrc );
00021     }
00022 
00023 // -----------------------------------------------------------------------------
00024 // RenderAsNumbers() documented in StringRenderer.h.
00025 // -----------------------------------------------------------------------------
00026 template <class TDESC>
00027 LOCAL_C void RenderAsNumbersTemplated( const TDESC &anArray, TDes16 &aOutput )
00028     {
00029     aOutput.Append('{');
00030     for( TInt i=0; i < anArray.Length(); i++ )
00031         {
00032         if( i>0 )
00033             {
00034             aOutput.Append(',');
00035             }
00036         aOutput.AppendNum( (TInt) anArray[i] );
00037         }
00038     aOutput.Append('}');
00039     }
00040 
00041 void RenderAsNumbers( const TDesC8 &anArray, TDes16 &aOutput )
00042 {
00043     RenderAsNumbersTemplated(anArray, aOutput);
00044 }
00045 
00046 void RenderAsNumbers( const TDesC16 &anArray, TDes16 &aOutput )
00047 {
00048     RenderAsNumbersTemplated(anArray, aOutput);
00049 }
00050 
00051 // -----------------------------------------------------------------------------
00052 // Renders contents of 8 or 16 bit descriptor either as numbers or characters.
00053 // The example result is "Hello" when aRenderFormat == KRenderDefault and
00054 // {72,101,108,108,111} when aRenderFormat == KRenderContentAsBinary
00055 // -----------------------------------------------------------------------------
00056 template <class DESC>
00057 LOCAL_C void RenderDescriptorContent( const DESC &aVariable,
00058                                       TDes16 &aOutput,
00059                                       TInt aRenderFormat=KRenderDefault )
00060     {
00061     if( aRenderFormat & KRenderContentAsBinary )
00062         { // render as numbers
00063         RenderAsNumbers( aVariable, aOutput );
00064         }
00065     else
00066         { // render as string
00067         aOutput.Append('"');
00068         Append(aVariable, aOutput);
00069         aOutput.Append('"');
00070         }
00071     }
00072 
00073 // -----------------------------------------------------------------------------
00074 // RenderObject() documented in StringRenderer.h.
00075 // -----------------------------------------------------------------------------
00076 _LIT( KTDesCCharacteristicsFormat, "(len=%d)" );
00077 _LIT( KTDesCharacteristicsFormat,  "(len=%d, maxlen=%d)" );
00078 
00079 void RenderObject(const TDesC8 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00080     {
00081     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00082     if( aRenderFormat & KRenderCharacteristics )
00083         {
00084         aOutput.Append(' ');
00085         aOutput.AppendFormat( KTDesCCharacteristicsFormat, aVariable.Length() );
00086         }
00087     }
00088 
00089 void RenderObject(const TDes8 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00090     {
00091     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00092     if( aRenderFormat & KRenderCharacteristics )
00093         {
00094         aOutput.Append(' ');
00095         aOutput.AppendFormat( KTDesCharacteristicsFormat, aVariable.Length(),
00096             aVariable.MaxLength() );
00097         }
00098     }
00099 
00100 void RenderObject(const TDesC16 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00101     {
00102     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00103     if( aRenderFormat & KRenderCharacteristics )
00104         {
00105         aOutput.Append(' ');
00106         aOutput.AppendFormat( KTDesCCharacteristicsFormat, aVariable.Length() );
00107         }
00108     }
00109 
00110 void RenderObject(const TDes16 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00111     {
00112     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00113     if( aRenderFormat & KRenderCharacteristics )
00114         {
00115         aOutput.Append(' ');
00116         aOutput.AppendFormat( KTDesCharacteristicsFormat,
00117                               aVariable.Length(),
00118                               aVariable.MaxLength() );
00119         }
00120     }
00121 
00122 void RenderObject(const TInt &aVariable,
00123                   TDes16 &aOutput,
00124                   TInt /*aRenderFormat*/ )
00125     {
00126     aOutput.AppendNum( aVariable );
00127     }
00128 
00129 // -----------------------------------------------------------------------------
00130 // RenderHeader() documented in StringRenderer.h.
00131 // -----------------------------------------------------------------------------
00132 void RenderHeader(const TDesC &aHeader, TDes &aOutput)
00133     {
00134     _LIT( KFormat, "\n--- %S ---\n" );
00135     TInt spaceAvailable = aOutput.MaxLength() - aOutput.Length();
00136     if( spaceAvailable >=
00137             aHeader.Length() +
00138             KFormat().Length()-2 ) // "%S" is replaced with header text
00139         {
00140         aOutput.AppendFormat( KFormat, &aHeader );
00141         }
00142     }

Generated by  doxygen 1.6.2