libraries/iosrv/server/file.cpp
changeset 0 7f656887cf89
child 78 b3ffff030d5c
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // file.cpp
       
     2 // 
       
     3 // Copyright (c) 2006 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "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 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include "file.h"
       
    14 #include "log.h"
       
    15 #include <fshell/ltkutils.h>
       
    16 
       
    17 CIoFile* CIoFile::NewLC(RFs& aFs, const TDesC& aName, RIoFile::TMode aMode)
       
    18 	{
       
    19 	CIoFile* self = new(ELeave) CIoFile();
       
    20 	CleanupClosePushL(*self);
       
    21 	self->ConstructL(aFs, aName, aMode);
       
    22 	return self;
       
    23 	}
       
    24 
       
    25 CIoFile::~CIoFile()
       
    26 	{
       
    27 	iFile.Close();
       
    28 	iTempReadBuf.Close();
       
    29 	}
       
    30 
       
    31 TBool CIoFile::IsType(RIoHandle::TType aType) const
       
    32 	{
       
    33 	return ((aType == RIoHandle::EEndPoint) || (aType == RIoHandle::EFile));
       
    34 	}
       
    35 
       
    36 void CIoFile::IorepReadL(MIoReader& aReader)
       
    37 	{
       
    38 	TInt fileSize;
       
    39 	User::LeaveIfError(iFile.Size(fileSize));
       
    40 	if (iPos < fileSize)
       
    41 		{
       
    42 		TDes& readBuf = aReader.IorReadBuf();
       
    43 		iTempReadBuf.Zero();
       
    44 		HBufC8* narrowBuf = HBufC8::NewLC(readBuf.MaxLength());
       
    45 		TPtr8 narrowBufPtr(narrowBuf->Des());
       
    46 		User::LeaveIfError(iFile.Read(narrowBufPtr, readBuf.MaxLength()));
       
    47 		iPos += narrowBuf->Length();
       
    48 		iTempReadBuf.AppendUtf8L(narrowBufPtr);
       
    49 		if (iPos >= fileSize)
       
    50 			{
       
    51 			iTempReadBuf.FinalizeUtf8();
       
    52 			}
       
    53 		readBuf.Copy(iTempReadBuf);
       
    54 		aReader.IorDataBuffered(readBuf.Length());
       
    55 		CleanupStack::PopAndDestroy(narrowBuf);
       
    56 		if (iPos >= fileSize)
       
    57 			{
       
    58 			aReader.IorReadComplete(KErrNone);
       
    59 			}
       
    60 		}
       
    61 	else
       
    62 		{
       
    63 		aReader.IorReadComplete(KErrEof);
       
    64 		}
       
    65 	}
       
    66 
       
    67 void CIoFile::IowepWriteL(MIoWriter& aWriter)
       
    68 	{
       
    69 	HBufC* buf = HBufC::NewLC(aWriter.IowWriteLength());
       
    70 	TPtr bufPtr(buf->Des());
       
    71 	aWriter.IowWrite(bufPtr);
       
    72 
       
    73 	// Convert to UTF-8
       
    74 	HBufC8* narrowBuf = LtkUtils::Utf8L(*buf);
       
    75 	TInt err = iFile.Write(*narrowBuf);
       
    76 	delete narrowBuf;
       
    77 	aWriter.IowComplete(err);
       
    78 	CleanupStack::PopAndDestroy(buf);
       
    79 	}
       
    80 
       
    81 void CIoFile::IowepWriteCancel(MIoWriter&)
       
    82 	{
       
    83 	}
       
    84 
       
    85 CIoFile::CIoFile()
       
    86 	{
       
    87 	}
       
    88 
       
    89 void CIoFile::ConstructL(RFs& aFs, const TDesC& aName, RIoFile::TMode aMode)
       
    90 	{
       
    91 	LOG(CIoLog::Printf(_L("Attempting to create file 0x%08x (\"%S\")"), this, &aName));
       
    92 	switch (aMode)
       
    93 		{
       
    94 		case RIoFile::ERead:
       
    95 			{
       
    96 			User::LeaveIfError(iFile.Open(aFs, aName, EFileRead));
       
    97 			break;
       
    98 			}
       
    99 		case RIoFile::EOverwrite:
       
   100 			{
       
   101 			User::LeaveIfError(iFile.Replace(aFs, aName, EFileWrite));
       
   102 			break;
       
   103 			}
       
   104 		case RIoFile::EAppend:
       
   105 			{
       
   106 			TInt err = iFile.Open(aFs, aName, EFileWrite);
       
   107 			if (err == KErrNotFound)
       
   108 				{
       
   109 				err = iFile.Create(aFs, aName, EFileWrite);
       
   110 				}
       
   111 			User::LeaveIfError(err);
       
   112 			TInt pos = 0;
       
   113 			User::LeaveIfError(iFile.Seek(ESeekEnd, pos));
       
   114 			break;
       
   115 			}
       
   116 		default:
       
   117 			{
       
   118 			User::Leave(KErrNotSupported);
       
   119 			}
       
   120 		}
       
   121 	}
       
   122