mapnavproviderrefapp/src/mnrputils.cpp
branchRCL_3
changeset 17 1fc85118c3ae
parent 16 8173571d354e
child 18 870918037e16
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  MnrpUtils class implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <e32math.h>
       
    21 #include <lbsposition.h>
       
    22 
       
    23 #include "mnrputils.h"
       
    24 
       
    25 _LIT( KNan, "<n/a>" );
       
    26 _LIT( KNeg, "<neg>" );
       
    27 _LIT( KOutOfBounds, "<inv>" );
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // -----------------------------------------------------------------------------
       
    33 //
       
    34 EXPORT_C void MnrpUtils::CoordinateToString(
       
    35     TReal aCoord,
       
    36     TDes& aStr,
       
    37     TBool aFull,
       
    38     TBool aMillis )
       
    39     {
       
    40     const TChar KDegreeMark = 0xB0;
       
    41     const TChar KMinuteMark = 0x27;
       
    42     const TChar KSecondMark = 0x22;
       
    43     const TChar KMilliSecondMark = 0x2E;
       
    44     const TChar KMinus = '-';
       
    45     const TChar KPlus = '+';
       
    46 
       
    47     _LIT( KCoordFormatFull, "%c%d%c%02d%c%02d%c" );
       
    48     _LIT( KCoordFormatPartMseconds, "%c%03d" );
       
    49 
       
    50     if ( Math::IsNaN( aCoord ) )
       
    51         {
       
    52         aStr.Copy( KNan );
       
    53         }
       
    54     else if ( Abs( aCoord ) > 360 )
       
    55         {
       
    56         aStr.Copy( KOutOfBounds );
       
    57         }
       
    58     else
       
    59         {
       
    60         TBool negative = aCoord < 0;
       
    61         aCoord = Abs( aCoord );
       
    62 
       
    63         TInt degrees = TInt( aCoord );
       
    64         aCoord = ( aCoord - degrees ) * 60;
       
    65         TInt minutes = TInt( aCoord );
       
    66         aCoord = ( aCoord - minutes ) * 60;
       
    67         TInt seconds = TInt( aCoord );
       
    68         aCoord = ( aCoord - seconds ) * 100;
       
    69         TInt mseconds = TInt( aCoord );
       
    70 
       
    71         TChar sign = negative ? KMinus : KPlus;
       
    72 
       
    73         if ( aFull )
       
    74             {
       
    75             aStr.Format( KCoordFormatFull,
       
    76                             TUint( sign ), degrees, TUint( KDegreeMark ),
       
    77                             minutes, TUint( KMinuteMark ),
       
    78                             seconds, TUint( KSecondMark ) );
       
    79             if ( aMillis )
       
    80                 {
       
    81                 aStr.AppendFormat( KCoordFormatPartMseconds, TUint( KMilliSecondMark ), mseconds );
       
    82                 }
       
    83             }
       
    84         else
       
    85             {
       
    86             aStr.Zero();
       
    87             if ( degrees )
       
    88                 {
       
    89                 aStr.AppendNum( degrees );
       
    90                 aStr.Append( KDegreeMark );
       
    91                 }
       
    92             if ( minutes )
       
    93                 {
       
    94                 aStr.AppendNum( minutes );
       
    95                 aStr.Append( KMinuteMark );
       
    96                 }
       
    97             if ( seconds )
       
    98                 {
       
    99                 aStr.AppendNum( seconds );
       
   100                 aStr.Append( KSecondMark );
       
   101                 }
       
   102             if ( mseconds && aMillis )
       
   103                 {
       
   104                 if ( !seconds )
       
   105                     {
       
   106                     aStr.AppendNum( 0 );
       
   107                     aStr.Append( KSecondMark );
       
   108                     }
       
   109                 aStr.AppendFormat( KCoordFormatPartMseconds, TUint( KMilliSecondMark ), mseconds );
       
   110                 }
       
   111 
       
   112             if ( !aStr.Length() )
       
   113                 {
       
   114                 aStr.AppendNum( degrees );
       
   115                 aStr.Append( KDegreeMark );
       
   116                 }
       
   117             }
       
   118         }
       
   119     }
       
   120 
       
   121 // -----------------------------------------------------------------------------
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 EXPORT_C void MnrpUtils::DistanceToString( TReal aDistance, TDes& aStr )
       
   125     {
       
   126     _LIT( KSmall, "< 1 m" );
       
   127     _LIT( KMeters, "%d m" );
       
   128     _LIT( KKiloMeters, "%.1f Km" );
       
   129     _LIT( KThousandsKiloMeters, "%.1f tKm" );
       
   130 
       
   131     const TInt KMeter = 1;
       
   132     const TInt KKilometer = KMeter * 1000;
       
   133     const TInt KThousandKilometer = KKilometer * 1000;
       
   134 
       
   135     if ( Math::IsNaN( aDistance ) )
       
   136         {
       
   137         aStr.Copy( KNan );
       
   138         }
       
   139     else if ( aDistance < 0 )
       
   140         {
       
   141         aStr.Copy( KNeg );
       
   142         }
       
   143     else if ( aDistance < KMeter )
       
   144         {
       
   145         aStr.Copy( KSmall );
       
   146         }
       
   147     else if ( aDistance < KKilometer )
       
   148         {
       
   149         aStr.Format( KMeters, TInt( aDistance ) );
       
   150         }
       
   151     else if ( aDistance < KThousandKilometer )
       
   152         {
       
   153         aStr.Format( KKiloMeters, aDistance / KKilometer );
       
   154         }
       
   155     else
       
   156         {
       
   157         aStr.Format( KThousandsKiloMeters, aDistance / KThousandKilometer );
       
   158         }
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // -----------------------------------------------------------------------------
       
   163 //
       
   164 EXPORT_C void MnrpUtils::SpeedToString( TReal aSpeed, TDes& aStr )
       
   165     {
       
   166     _LIT( KPerSecond, "/s" );
       
   167 
       
   168     if ( Math::IsNaN( aSpeed ) )
       
   169         {
       
   170         aStr.Copy( KNan );
       
   171         }
       
   172     else if ( aSpeed < 0 )
       
   173         {
       
   174         aStr.Copy( KNeg );
       
   175         }
       
   176     else
       
   177         {
       
   178         DistanceToString( aSpeed, aStr );
       
   179         aStr.Append( KPerSecond );
       
   180         }
       
   181     }
       
   182     
       
   183 // -----------------------------------------------------------------------------
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 EXPORT_C void MnrpUtils::AngleToString( TReal aAngle, TDes& aStr )
       
   187     {
       
   188     const TChar KMinus = '-';
       
   189     const TChar KPlus = '+';
       
   190     _LIT( KAngleFormat, "%S%c%.1f\xB0 /  %.1f\xB0" );
       
   191     
       
   192     _LIT( KNorth, "N" );
       
   193     _LIT( KSouth, "S" );
       
   194     _LIT( KWest, "W" );
       
   195     _LIT( KEast, "E" );
       
   196 
       
   197     TPtrC side;
       
   198     TReal angle;
       
   199 
       
   200     if ( Math::IsNaN( aAngle ) )
       
   201         {
       
   202         aStr.Copy( KNan );
       
   203         }
       
   204     else if ( aAngle > 360 )
       
   205         {
       
   206         aStr.Copy( KOutOfBounds );
       
   207         }
       
   208     else
       
   209         {
       
   210         if ( aAngle > 315 )
       
   211             {
       
   212             side.Set( KNorth );
       
   213             angle = aAngle - 360;
       
   214             }
       
   215         else if ( aAngle > 225 )
       
   216             {
       
   217             side.Set( KWest );
       
   218             angle = aAngle - 270;
       
   219             }
       
   220         else if ( aAngle > 135 )
       
   221             {
       
   222             side.Set( KSouth );
       
   223             angle = aAngle - 180;
       
   224             }
       
   225         else if ( aAngle > 45 )
       
   226             {
       
   227             side.Set( KEast );
       
   228             angle = aAngle - 90;
       
   229             }
       
   230         else // aAngle <= 45
       
   231             {
       
   232             side.Set( KNorth );
       
   233             angle = aAngle;
       
   234             }
       
   235 
       
   236         TChar sign = ( angle >= 0 ) ? KPlus : KMinus;
       
   237         aStr.Format( KAngleFormat, &side, TUint( sign ), Abs( angle ), aAngle );
       
   238         }
       
   239     }
       
   240 
       
   241 // -----------------------------------------------------------------------------
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 EXPORT_C HBufC* MnrpUtils::PrintableStringLC( const TDesC& aStr )
       
   245     {
       
   246     HBufC* buf = aStr.AllocLC();
       
   247     TPtr string( buf->Des() );
       
   248     for ( TInt i = 0; i < string.Length(); i++ )
       
   249         {
       
   250         if ( !TChar( string[i] ).IsPrint() )
       
   251             {
       
   252             string[i] = ' ';
       
   253             }
       
   254         }
       
   255     return buf;
       
   256     }