applayerprotocols/httptransportfw/Test/T_HttpIntegration/CDumpFile.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // $Header$
       
    15 // CCommandDO.cpp
       
    16 // This module implements the CDmpfile (dump output file) class.
       
    17 // rev:	mjdavey, symbian@mjdss.com, July 2002
       
    18 // for:	Typhoon (7.0s) & JetStream (8.0)
       
    19 // Include Files  
       
    20 // 
       
    21 //
       
    22 
       
    23 #include "CDumpFile.h"                    // This module
       
    24 
       
    25 //-----------------------------------------------------------------------------
       
    26 
       
    27 CDmpfile* CDmpfile::New( )
       
    28 //Return Values:  Pointer to a new Dmpfile class object, NULL if fails.
       
    29 {
       
    30 CDmpfile* self = NULL;
       
    31 TRAPD(error,self=NewL());
       
    32 return ( error == KErrNone ? self : NULL );
       
    33 }
       
    34 
       
    35 //-----------------------------------------------------------------------------
       
    36 
       
    37 CDmpfile* CDmpfile::NewL( )
       
    38 {
       
    39 CDmpfile* self = NewLC();
       
    40 CleanupStack::Pop();
       
    41 return self; 
       
    42 }
       
    43 
       
    44 //-----------------------------------------------------------------------------
       
    45 
       
    46 CDmpfile* CDmpfile::NewLC(  )
       
    47 {
       
    48 CDmpfile* self = new (ELeave) CDmpfile();
       
    49 CleanupStack::PushL(self);
       
    50 self->ConstructL();
       
    51 return self;
       
    52 }
       
    53 
       
    54 //-----------------------------------------------------------------------------
       
    55 
       
    56 CDmpfile::~CDmpfile( )
       
    57 {
       
    58 Close();
       
    59 }
       
    60 
       
    61 //-----------------------------------------------------------------------------
       
    62 
       
    63 void CDmpfile::ConstructL( )
       
    64 {
       
    65 iIsOpen  = EFalse;
       
    66 iDoFlush = EFalse;
       
    67 iError   = KErrNone;
       
    68 iFileName.Zero();
       
    69 }
       
    70 
       
    71 //-----------------------------------------------------------------------------
       
    72 
       
    73 TInt CDmpfile::Open( const TDesC& aName )
       
    74 //	Open existing file without replacing it. If it does not already exist,
       
    75 //	create it.
       
    76 {
       
    77 Close();
       
    78 iError = iParse.Set( aName, NULL, NULL );
       
    79 if ( iError == KErrNone )
       
    80 	{
       
    81 	iFileName.Copy( iParse.FullName() );
       
    82 	iError = DoOpen();
       
    83 	}
       
    84 
       
    85 return iError;
       
    86 }
       
    87 
       
    88 //-----------------------------------------------------------------------------
       
    89 
       
    90 TInt CDmpfile::Open( const TDesC& aPath, const TDesC& aName )
       
    91 {
       
    92 Close();
       
    93 iError = iParse.Set( aName, NULL, &aPath );
       
    94 if ( iError == KErrNone )
       
    95 	{
       
    96 	iFileName.Copy( iParse.FullName() );
       
    97 	iError = DoOpen();
       
    98 	}
       
    99 return iError;
       
   100 }
       
   101 
       
   102 //-----------------------------------------------------------------------------
       
   103 
       
   104 TInt CDmpfile::Open()
       
   105 {
       
   106 Close();
       
   107 return ( iError = DoOpen() );
       
   108 }
       
   109 
       
   110 //-----------------------------------------------------------------------------
       
   111 
       
   112 TInt CDmpfile::Replace( const TDesC& aName )
       
   113 //	Open existing file replacing its contents. If it does not already exist,
       
   114 //	create it.
       
   115 {
       
   116 Close();
       
   117 iError = iParse.Set( aName, NULL, NULL );
       
   118 if (iError == KErrNone )
       
   119 	{
       
   120 	iFileName.Copy( iParse.FullName() );
       
   121 	iError = DoReplace();
       
   122 	}
       
   123 return iError;
       
   124 }
       
   125 
       
   126 //-----------------------------------------------------------------------------
       
   127 
       
   128 TInt CDmpfile::Replace( const TDesC& aPath, const TDesC& aName )
       
   129 {
       
   130 Close();
       
   131 iError = iParse.Set( aName, NULL, &aPath );
       
   132 if ( iError == KErrNone )
       
   133 	{
       
   134 	iFileName.Copy( iParse.FullName() );
       
   135 	iError = DoReplace();
       
   136 	}
       
   137 return iError;
       
   138 }
       
   139 
       
   140 //-----------------------------------------------------------------------------
       
   141 
       
   142 TInt CDmpfile::Replace()
       
   143 {
       
   144 Close();
       
   145 return ( iError = DoReplace() );
       
   146 }
       
   147 
       
   148 //-----------------------------------------------------------------------------
       
   149 
       
   150 void CDmpfile::Close( )
       
   151 //	Close file iff is open.
       
   152 {
       
   153 if ( iIsOpen )
       
   154 	{
       
   155 	iFile.Close();
       
   156 	iFs.Close();
       
   157 	iIsOpen = EFalse;
       
   158 	}
       
   159 }
       
   160 
       
   161 //-----------------------------------------------------------------------------
       
   162 
       
   163 TInt CDmpfile::Write( const TDesC8& aDes )
       
   164 {
       
   165 iError = iFile.Write( aDes );
       
   166 if ( iError == KErrNone && iDoFlush ) 
       
   167 	iError = iFile.Flush();
       
   168 return iError;
       
   169 }
       
   170 
       
   171 //-----------------------------------------------------------------------------
       
   172 
       
   173 TInt CDmpfile::Write( const TDesC8& aDes, TInt aLength )
       
   174 {
       
   175 iError = iFile.Write( aDes, aLength );
       
   176 if ( iError == KErrNone && iDoFlush ) 
       
   177 	iError = iFile.Flush();
       
   178 return iError;
       
   179 }
       
   180 
       
   181 //-----------------------------------------------------------------------------
       
   182 
       
   183 TInt CDmpfile::Flush( )
       
   184 {
       
   185 return ( iError = iFile.Flush() );
       
   186 }
       
   187 
       
   188 //-----------------------------------------------------------------------------
       
   189 
       
   190 void CDmpfile::SetFlush( TBool doFlush )
       
   191 {
       
   192 iDoFlush = doFlush;
       
   193 }
       
   194 
       
   195 //-----------------------------------------------------------------------------
       
   196 
       
   197 TInt CDmpfile::Size( )
       
   198 //	Get file size. Negative KErr* if fails.
       
   199 {
       
   200 TInt size;
       
   201 iError = iFile.Size( size );
       
   202 return ( iError == KErrNone ? size : iError );
       
   203 }
       
   204 
       
   205 //-----------------------------------------------------------------------------
       
   206 
       
   207 TBool CDmpfile::IsOpen( )
       
   208 {
       
   209 return iIsOpen;
       
   210 }
       
   211 
       
   212 //-----------------------------------------------------------------------------
       
   213 
       
   214 TPtrC CDmpfile::FileName( ) const
       
   215 {
       
   216 TPtrC filename( iFileName );
       
   217 return filename;
       
   218 }
       
   219 
       
   220 //-----------------------------------------------------------------------------
       
   221 
       
   222 TInt CDmpfile::Error( )
       
   223 {
       
   224 return iError;
       
   225 }
       
   226 
       
   227 //-----------------------------------------------------------------------------
       
   228 
       
   229 TInt CDmpfile::DoOpen( )
       
   230 {
       
   231 TInt ret = iFs.Connect();
       
   232 if ( ret != KErrNone ) 
       
   233 	return ret;
       
   234 
       
   235 ret = iFile.Open( iFs, iFileName, EFileWrite );
       
   236 if ( ret == KErrNotFound ) 
       
   237 	ret = iFile.Create( iFs, iFileName, EFileWrite );
       
   238 if ( ret != KErrNone )
       
   239 	{
       
   240 	iFs.Close();
       
   241 	return ret;
       
   242 	}
       
   243 
       
   244 TInt filepos;
       
   245 ret = iFile.Seek( ESeekEnd, filepos );
       
   246 if ( ret != KErrNone )
       
   247 	{
       
   248 	iFs.Close();
       
   249 	iFile.Close();
       
   250 	return ret;
       
   251 	}
       
   252 
       
   253 iIsOpen = ETrue;
       
   254 return ret;
       
   255 }
       
   256 
       
   257 //-----------------------------------------------------------------------------
       
   258 
       
   259 TInt CDmpfile::DoReplace( )
       
   260 {
       
   261 TInt ret = iFs.Connect();
       
   262 if ( ret != KErrNone ) 
       
   263 	return ret;
       
   264 
       
   265 ret = iFile.Replace( iFs, iFileName, EFileWrite );
       
   266 if ( ret != KErrNone )
       
   267 	{
       
   268 	iFs.Close();
       
   269 	return ret;
       
   270 	}
       
   271 
       
   272 TInt filepos;
       
   273 ret = iFile.Seek( ESeekEnd, filepos );
       
   274 if ( ret != KErrNone )
       
   275 	{
       
   276 	iFs.Close();
       
   277 	iFile.Close();
       
   278 	return ret;
       
   279 	}
       
   280 
       
   281 iIsOpen = ETrue;
       
   282 return ret;
       
   283 }
       
   284 
       
   285 //-----------------------------------------------------------------------------
       
   286 //  End of File  
       
   287 //-----------------------------------------------------------------------------
       
   288