examples/ForumNokia/DescriptorExample/src/StringRenderer.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 "StringRenderer.h"
00032 
00033 // -----------------------------------------------------------------------------
00034 // Append() documented in StringRenderer.h.
00035 // -----------------------------------------------------------------------------
00036 void Append( const TDesC8 &aSrc, TDes16 &aDst )
00037     {
00038     for( TInt i=0; i < aSrc.Length(); i++ )
00039         {
00040         aDst.Append(aSrc[i]);
00041         }
00042     }
00043 
00044 void Append( const TDesC16 &aSrc, TDes16 &aDst )
00045     {
00046     aDst.Append( aSrc );
00047     }
00048 
00049 // -----------------------------------------------------------------------------
00050 // RenderAsNumbers() documented in StringRenderer.h.
00051 // -----------------------------------------------------------------------------
00052 template <class TDESC>
00053 LOCAL_C void RenderAsNumbersTemplated( const TDESC &anArray, TDes16 &aOutput )
00054     {
00055     aOutput.Append('{');
00056     for( TInt i=0; i < anArray.Length(); i++ )
00057         {
00058         if( i>0 )
00059             {
00060             aOutput.Append(',');
00061             }
00062         aOutput.AppendNum( (TInt) anArray[i] );
00063         }
00064     aOutput.Append('}');
00065     }
00066 
00067 void RenderAsNumbers( const TDesC8 &anArray, TDes16 &aOutput )
00068 {
00069     RenderAsNumbersTemplated(anArray, aOutput);
00070 }
00071 
00072 void RenderAsNumbers( const TDesC16 &anArray, TDes16 &aOutput )
00073 {
00074     RenderAsNumbersTemplated(anArray, aOutput);
00075 }
00076 
00077 // -----------------------------------------------------------------------------
00078 // Renders contents of 8 or 16 bit descriptor either as numbers or characters.
00079 // The example result is "Hello" when aRenderFormat == KRenderDefault and
00080 // {72,101,108,108,111} when aRenderFormat == KRenderContentAsBinary
00081 // -----------------------------------------------------------------------------
00082 template <class DESC>
00083 LOCAL_C void RenderDescriptorContent( const DESC &aVariable,
00084                                       TDes16 &aOutput,
00085                                       TInt aRenderFormat=KRenderDefault )
00086     {
00087     if( aRenderFormat & KRenderContentAsBinary )
00088         { // render as numbers
00089         RenderAsNumbers( aVariable, aOutput );
00090         }
00091     else
00092         { // render as string
00093         aOutput.Append('"');
00094         Append(aVariable, aOutput);
00095         aOutput.Append('"');
00096         }
00097     }
00098 
00099 // -----------------------------------------------------------------------------
00100 // RenderObject() documented in StringRenderer.h.
00101 // -----------------------------------------------------------------------------
00102 _LIT( KTDesCCharacteristicsFormat, "(len=%d)" );
00103 _LIT( KTDesCharacteristicsFormat,  "(len=%d, maxlen=%d)" );
00104 
00105 void RenderObject(const TDesC8 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00106     {
00107     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00108     if( aRenderFormat & KRenderCharacteristics )
00109         {
00110         aOutput.Append(' ');
00111         aOutput.AppendFormat( KTDesCCharacteristicsFormat, aVariable.Length() );
00112         }
00113     }
00114 
00115 void RenderObject(const TDes8 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00116     {
00117     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00118     if( aRenderFormat & KRenderCharacteristics )
00119         {
00120         aOutput.Append(' ');
00121         aOutput.AppendFormat( KTDesCharacteristicsFormat, aVariable.Length(),
00122             aVariable.MaxLength() );
00123         }
00124     }
00125 
00126 void RenderObject(const TDesC16 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00127     {
00128     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00129     if( aRenderFormat & KRenderCharacteristics )
00130         {
00131         aOutput.Append(' ');
00132         aOutput.AppendFormat( KTDesCCharacteristicsFormat, aVariable.Length() );
00133         }
00134     }
00135 
00136 void RenderObject(const TDes16 &aVariable, TDes16 &aOutput, TInt aRenderFormat)
00137     {
00138     RenderDescriptorContent( aVariable, aOutput, aRenderFormat );
00139     if( aRenderFormat & KRenderCharacteristics )
00140         {
00141         aOutput.Append(' ');
00142         aOutput.AppendFormat( KTDesCharacteristicsFormat,
00143                               aVariable.Length(),
00144                               aVariable.MaxLength() );
00145         }
00146     }
00147 
00148 void RenderObject(const TInt &aVariable,
00149                   TDes16 &aOutput,
00150                   TInt /*aRenderFormat*/ )
00151     {
00152     aOutput.AppendNum( aVariable );
00153     }
00154 
00155 // -----------------------------------------------------------------------------
00156 // RenderHeader() documented in StringRenderer.h.
00157 // -----------------------------------------------------------------------------
00158 void RenderHeader(const TDesC &aHeader, TDes &aOutput)
00159     {
00160     _LIT( KFormat, "\n--- %S ---\n" );
00161     TInt spaceAvailable = aOutput.MaxLength() - aOutput.Length();
00162     if( spaceAvailable >=
00163             aHeader.Length() +
00164             KFormat().Length()-2 ) // "%S" is replaced with header text
00165         {
00166         aOutput.AppendFormat( KFormat, &aHeader );
00167         }
00168     }

Generated by  doxygen 1.6.2