testexecfw/statsrv/device/source/statapi/src/filedump.cpp
changeset 0 3e07fef1e154
equal deleted inserted replaced
-1:000000000000 0:3e07fef1e154
       
     1 /*
       
     2 * Copyright (c) 2005-2009 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 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "filedump.h"
       
    21 
       
    22 //////////////////////////////////////////////////////////////////////////
       
    23 // Open the output file
       
    24 //////////////////////////////////////////////////////////////////////////
       
    25 GLDEF_C TInt 
       
    26 FileDump::Init(RFs &fsSession, TPtrC16 filename, CConsoleBase* console)
       
    27 {
       
    28 	/*
       
    29 	// To determine the date and create a string.
       
    30 	static const TInt defLength = 256;
       
    31 	int exception = KErrNone;
       
    32 	TBuf<defLength> fileDateName( _L("") );
       
    33 	TTime date;
       
    34 	date.HomeTime();	
       
    35 	TRAP( exception, date.FormatL(fileDateName, (_L("%F%Y%M%D"))) );
       
    36 	*/
       
    37 
       
    38 	/*
       
    39 	// To either open an existing file or create a new file.
       
    40 	TFileMode shareMode = static_cast<TFileMode>(EFileShareAny|EFileStream|EFileWrite);
       
    41 
       
    42 	TInt err = file.Open( fsSession, fileDateName, shareMode );
       
    43 	if ( err == KErrNotFound )
       
    44 	    {
       
    45 	    err = file.Create( fsSession, fileDateName, shareMode );
       
    46 	    }
       
    47 	*/
       
    48 
       
    49    	if (file.Replace(fsSession, filename, EFileShareAny|EFileStream|EFileWrite) == KErrNone)
       
    50 	{
       
    51 		pConsole = console;
       
    52 		bInitialised = ETrue;
       
    53 		return ETrue;
       
    54 	}
       
    55 
       
    56 	return EFalse;
       
    57 }
       
    58 
       
    59 GLDEF_C void
       
    60 FileDump::Msg(TPtrC16 text, ...)
       
    61 {
       
    62 	int exception = KErrNone;
       
    63 
       
    64 	if (bInitialised && text.Length())
       
    65 	{
       
    66 		// create date/time string for filename
       
    67 		TBuf<10> datestring( _L("") );
       
    68 		TTime DateAndTime;
       
    69 		DateAndTime.HomeTime();	
       
    70 		TRAP( exception, DateAndTime.FormatL(datestring, (_L("%H:%T:%S "))) );
       
    71 		if( exception == KErrNone ) {
       
    72 			buf.Zero();
       
    73 			buf.Append(datestring);
       
    74 			buf.ZeroTerminate();
       
    75 			WriteBuffer16ToFile(buf, file);
       
    76 		}
       
    77 
       
    78 		VA_LIST list;
       
    79 		VA_START(list, text);
       
    80 
       
    81 		buf.Zero();
       
    82 		buf.FormatList(text, list);
       
    83 		buf.Append(_L("\r\n"));
       
    84 		buf.ZeroTerminate();
       
    85 
       
    86 		if (pConsole)
       
    87 			pConsole->Printf(buf);
       
    88 
       
    89 		WriteBuffer16ToFile(buf, file);
       
    90 
       
    91 		VA_END(list);
       
    92 	}
       
    93 }
       
    94 
       
    95 //////////////////////////////////////////////////////////////////////////
       
    96 // Close the file
       
    97 //////////////////////////////////////////////////////////////////////////
       
    98 GLDEF_C void 
       
    99 FileDump::CloseFile()
       
   100 {
       
   101 	if( bInitialised ) {
       
   102 		file.Close();
       
   103 	}
       
   104 	bInitialised = EFalse;
       
   105 }
       
   106 
       
   107 
       
   108 GLDEF_C TBool
       
   109 FileDump::WriteBuffer16ToFile(TDesC16 &buffer, RFile &handle)
       
   110 {
       
   111 	// keep buffer small to reduce memory usage
       
   112 	TBuf8<5> outputBuffer;
       
   113 	TPtrC16 ptr;
       
   114 	TInt consumed;
       
   115 	TInt remainder = 0;
       
   116 
       
   117 	TBool valid = EFalse;
       
   118 
       
   119 	if (buffer.Length())
       
   120 	{
       
   121 		ptr.Set(buffer);
       
   122 		do
       
   123 		{
       
   124 			// get something to write
       
   125 			consumed = Min(outputBuffer.MaxLength(), ptr.Length());
       
   126 
       
   127 			// write it
       
   128 			outputBuffer.Copy(ptr.Left(consumed));
       
   129 			if (handle.Write(outputBuffer) != KErrNone)
       
   130 				return EFalse;
       
   131 
       
   132 			handle.Flush();
       
   133 
       
   134 			// get the next chunk
       
   135 			remainder = ptr.Length() - consumed;
       
   136 			if (remainder > 0)
       
   137 				ptr.Set(ptr.Right(remainder));
       
   138 
       
   139 		}while (remainder > 0);
       
   140 
       
   141 		valid = ETrue;
       
   142 	}
       
   143 
       
   144 	return valid;
       
   145 }
       
   146 
       
   147 GLDEF_C TBool
       
   148 FileDump::IsInitialised() const
       
   149 {
       
   150 	return bInitialised;
       
   151 }