omadrm/drmplugins/drmrohandler/src/StringResourceReader.cpp
branchRCL_3
changeset 26 1221b68b8a5f
parent 25 50c53e893c3f
child 27 1481bf457703
equal deleted inserted replaced
25:50c53e893c3f 26:1221b68b8a5f
     1 /*
       
     2 * Copyright (c) 2002 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: 
       
    15 *     Implementation of CStringResourceReader
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "StringResourceReader.h"
       
    24 #include <f32file.h>
       
    25 #include <barsread.h>
       
    26 #include <bautils.h>
       
    27 
       
    28 
       
    29 // ============================= LOCAL FUNCTIONS ===============================
       
    30 
       
    31 //#define _DRM_TESTING
       
    32 
       
    33 #ifdef _DRM_TESTING
       
    34 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs );
       
    35 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName );
       
    36 LOCAL_C void CreateLogL();
       
    37 LOCAL_C void WriteL( const TDesC8& aText );
       
    38 LOCAL_C void WriteL( const TDesC& aText );
       
    39 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr );
       
    40 LOCAL_C void WriteCurrentTimeL();
       
    41 #endif
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // Testing stuff
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 
       
    48 #ifdef _DRM_TESTING
       
    49 LOCAL_C void WriteLogL( const TDesC8& text , RFs &aFs )
       
    50     {
       
    51     _LIT( KLogFile , "c:\\StringResourceReader.txt" );
       
    52     WriteFileL( text , aFs , KLogFile );
       
    53     }
       
    54 
       
    55 LOCAL_C void WriteFileL( const TDesC8& text , RFs &aFs , const TDesC& aName )
       
    56     {
       
    57     RFile file;
       
    58     TInt size;
       
    59     User::LeaveIfError( file.Open( aFs, aName , EFileWrite ) );
       
    60     CleanupClosePushL( file );
       
    61     User::LeaveIfError( file.Size( size ) );
       
    62     User::LeaveIfError( file.Write( size, text ) );
       
    63     CleanupStack::PopAndDestroy(); //file
       
    64     }
       
    65 
       
    66 LOCAL_C void CreateLogL()
       
    67     {
       
    68     RFs fs;
       
    69     User::LeaveIfError(fs.Connect());
       
    70     CleanupClosePushL(fs);
       
    71     RFile file;
       
    72     User::LeaveIfError( file.Replace( fs , _L("c:\\StringResourceReader.txt") , EFileWrite ) );
       
    73     file.Close();
       
    74     CleanupStack::PopAndDestroy(); //fs
       
    75     }
       
    76 
       
    77 LOCAL_C void WriteL( const TDesC& aText )
       
    78     {
       
    79     RFs fs;
       
    80     User::LeaveIfError( fs.Connect() );
       
    81     CleanupClosePushL(fs);
       
    82     HBufC8* text = HBufC8::NewLC(1000);
       
    83     TPtr8 textptr(text->Des() );
       
    84     textptr.Append( aText );
       
    85     textptr.Append( _L8("\r\n") );
       
    86     WriteLogL(textptr , fs);
       
    87     CleanupStack::PopAndDestroy(text);
       
    88     CleanupStack::PopAndDestroy(); //fs
       
    89     WriteCurrentTimeL();
       
    90     }
       
    91 
       
    92 LOCAL_C void WriteL( const TDesC8& aText )
       
    93     {
       
    94     RFs fs;
       
    95     User::LeaveIfError( fs.Connect() );
       
    96     CleanupClosePushL(fs);
       
    97     HBufC8* text = HBufC8::NewLC(1000);
       
    98     TPtr8 textptr(text->Des() );
       
    99     textptr.Append( aText );
       
   100     textptr.Append( _L8("\r\n") );
       
   101     WriteLogL(textptr , fs);
       
   102     CleanupStack::PopAndDestroy(text);
       
   103     CleanupStack::PopAndDestroy(); //fs
       
   104     WriteCurrentTimeL();
       
   105     }
       
   106 
       
   107 LOCAL_C void WriteL( const TDesC8& aText , TInt aErr )
       
   108     {
       
   109     _LIT8(KErr,": %d");
       
   110     HBufC8* text = HBufC8::NewLC(1000+20);
       
   111     TBuf8<20> num;
       
   112     TPtr8 textptr(text->Des());
       
   113     textptr.Append(aText);
       
   114     num.Format(KErr(),aErr);
       
   115     textptr.Append(num);
       
   116     WriteL(textptr);
       
   117     CleanupStack::PopAndDestroy(text);
       
   118     }
       
   119 
       
   120 LOCAL_C void WriteCurrentTimeL()
       
   121     {
       
   122     RFs fs;
       
   123     User::LeaveIfError( fs.Connect() );
       
   124     CleanupClosePushL(fs);
       
   125     HBufC8* text = HBufC8::NewLC(100);
       
   126     TPtr8 textptr(text->Des() );
       
   127 // Date and Time display
       
   128     TTime time;
       
   129     time.HomeTime();
       
   130     TBuf<256> dateString;
       
   131     _LIT(KDate,"%*E%*D%X%*N%*Y %1 %2 '%3");
       
   132     time.FormatL(dateString,KDate);
       
   133     textptr.Append(_L( "\r\n\t\tData:\t" ) );
       
   134     textptr.Append( dateString );
       
   135     _LIT(KTime,"%-B%:0%J%:1%T%:2%S%:3%+B");
       
   136     time.FormatL(dateString,KTime);
       
   137     textptr.Append(_L( "\r\n\t\tTime:\t" ) );
       
   138     textptr.Append( dateString );
       
   139     textptr.Append(_L( "\r\n" ) );
       
   140     textptr.Append(_L( "\r\n" ) );
       
   141     WriteLogL(textptr , fs);
       
   142     CleanupStack::PopAndDestroy(text);
       
   143     CleanupStack::PopAndDestroy(); //fs
       
   144     }
       
   145 #endif
       
   146 
       
   147 // ================= MEMBER FUNCTIONS =======================
       
   148 
       
   149 
       
   150 // ---------------------------------------------------------
       
   151 // CStringResourceReader::CStringResourceReader
       
   152 // ---------------------------------------------------------
       
   153 //
       
   154 CStringResourceReader::CStringResourceReader
       
   155     ( RFs& aFs, const TDesC& aRscFileWithPathAndDrive )
       
   156 :   CBase(), iFs( aFs ), iInitialized( EFalse )
       
   157     {
       
   158 #ifdef _DRM_TESTING
       
   159 	TRAPD(r,CreateLogL());   
       
   160 	TRAP(r,WriteL(_L8("CStringResourceReader")));
       
   161 #endif 
       
   162     iRscFileName.Copy( aRscFileWithPathAndDrive );
       
   163     }
       
   164 
       
   165 // ---------------------------------------------------------
       
   166 // CStringResourceReader::~CStringResourceReader
       
   167 // ---------------------------------------------------------
       
   168 //
       
   169 CStringResourceReader::~CStringResourceReader()
       
   170     {
       
   171     iResourceFile.Close();
       
   172 #ifdef _DRM_TESTING
       
   173 	TRAPD(r,WriteL(_L8("~CStringResourceReader")));
       
   174 #endif 
       
   175     }
       
   176 
       
   177 // ---------------------------------------------------------
       
   178 // CStringResourceReader::AllocReadResourceL
       
   179 // ---------------------------------------------------------
       
   180 //
       
   181 HBufC* CStringResourceReader::AllocReadResourceL( TInt aResId )
       
   182     {
       
   183 #ifdef _DRM_TESTING
       
   184 	TRAPD(r,WriteL(_L8("AllocReadResourceL"),aResId));
       
   185 #endif 
       
   186 
       
   187     InitializeL();
       
   188 
       
   189 #ifdef _DRM_TESTING
       
   190 	TRAP(r,WriteL(_L8("AllocReadResourceL-InitializeL")));
       
   191 #endif 
       
   192 
       
   193     HBufC8* buf8 = iResourceFile.AllocReadLC( aResId );
       
   194 
       
   195 #ifdef _DRM_TESTING
       
   196 	TRAP(r,WriteL(_L8("AllocReadResourceL-iResourceFile.AllocReadLC")));
       
   197 #endif     
       
   198     
       
   199 #ifdef _UNICODE
       
   200     const TPtrC buf( (const TUint16*)buf8->Ptr(), buf8->Size()/2 );
       
   201 #else
       
   202     const TPtrC buf( buf8->Ptr(), buf8->Size() );
       
   203 #endif
       
   204     HBufC* retBuf = buf.AllocL();
       
   205     CleanupStack::PopAndDestroy( buf8 );
       
   206 
       
   207 #ifdef _DRM_TESTING
       
   208 	TRAP(r,WriteL(_L8("AllocReadResourceL-End")));
       
   209 #endif 
       
   210     return retBuf;
       
   211     }
       
   212 
       
   213 // ---------------------------------------------------------
       
   214 // CStringResourceReader::InitializeL
       
   215 // ---------------------------------------------------------
       
   216 //
       
   217 void CStringResourceReader::InitializeL()
       
   218     {
       
   219 #ifdef _DRM_TESTING
       
   220 	TRAPD(r,WriteL(_L8("InitializeL")));
       
   221 #endif 
       
   222     if ( !iInitialized )
       
   223         {
       
   224         TFileName resourceFileName( iRscFileName );
       
   225 #ifdef _DRM_TESTING
       
   226 	TRAP(r,WriteL(_L8("InitializeL-NearestLanguageFile")));
       
   227 #endif 
       
   228 
       
   229 #ifdef _DRM_TESTING
       
   230 	TRAP(r,WriteL(resourceFileName));
       
   231 #endif 
       
   232         BaflUtils::NearestLanguageFile( iFs, resourceFileName );
       
   233 #ifdef _DRM_TESTING
       
   234 	TRAP(r,WriteL(_L8("InitializeL-NearestLanguageFile-End")));
       
   235 #endif 
       
   236 
       
   237         iResourceFile.OpenL( iFs, resourceFileName );
       
   238 #ifdef _DRM_TESTING
       
   239 	TRAP(r,WriteL(_L8("InitializeL-OpenL-End")));
       
   240 #endif 
       
   241 
       
   242         iResourceFile.ConfirmSignatureL( iResourceFile.SignatureL() );
       
   243 #ifdef _DRM_TESTING
       
   244 	TRAP(r,WriteL(_L8("InitializeL-ConfirmSignatureL-End")));
       
   245 #endif 
       
   246         iInitialized = ETrue;
       
   247         }
       
   248 #ifdef _DRM_TESTING
       
   249 	TRAP(r,WriteL(_L8("InitializeL-End")));
       
   250 #endif 
       
   251     }
       
   252 
       
   253 // End of file.