libraries/iosrv/server/file.cpp
author Tom Sutcliffe <thomas.sutcliffe@accenture.com>
Thu, 26 Aug 2010 00:49:35 +0100
changeset 37 534b01198c2d
parent 0 7f656887cf89
child 78 b3ffff030d5c
permissions -rw-r--r--
Added ENotifyKeypresses and ECaptureCtrlC flags to CCommandBase. Commands can now get keypresses and handle ctrl-C via callbacks instead of having to implement custom active objects. As part of this extended the CCommandBase extension interface to MCommandExtensionsV2 for the new virtual functions KeyPressed(TUint aKeyCode, TUint aModifiers) and CtrlCPressed(). sudo now cleans up correctly by using ECaptureCtrlC.

// file.cpp
// 
// Copyright (c) 2006 - 2010 Accenture. All rights reserved.
// This component and the accompanying materials are made available
// under the terms of the "Eclipse Public License v1.0"
// which accompanies this distribution, and is available
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
// 
// Initial Contributors:
// Accenture - Initial contribution
//

#include "file.h"
#include "log.h"
#include <fshell/ltkutils.h>

CIoFile* CIoFile::NewLC(RFs& aFs, const TDesC& aName, RIoFile::TMode aMode)
	{
	CIoFile* self = new(ELeave) CIoFile();
	CleanupClosePushL(*self);
	self->ConstructL(aFs, aName, aMode);
	return self;
	}

CIoFile::~CIoFile()
	{
	iFile.Close();
	iTempReadBuf.Close();
	}

TBool CIoFile::IsType(RIoHandle::TType aType) const
	{
	return ((aType == RIoHandle::EEndPoint) || (aType == RIoHandle::EFile));
	}

void CIoFile::IorepReadL(MIoReader& aReader)
	{
	TInt fileSize;
	User::LeaveIfError(iFile.Size(fileSize));
	if (iPos < fileSize)
		{
		TDes& readBuf = aReader.IorReadBuf();
		iTempReadBuf.Zero();
		HBufC8* narrowBuf = HBufC8::NewLC(readBuf.MaxLength());
		TPtr8 narrowBufPtr(narrowBuf->Des());
		User::LeaveIfError(iFile.Read(narrowBufPtr, readBuf.MaxLength()));
		iPos += narrowBuf->Length();
		iTempReadBuf.AppendUtf8L(narrowBufPtr);
		if (iPos >= fileSize)
			{
			iTempReadBuf.FinalizeUtf8();
			}
		readBuf.Copy(iTempReadBuf);
		aReader.IorDataBuffered(readBuf.Length());
		CleanupStack::PopAndDestroy(narrowBuf);
		if (iPos >= fileSize)
			{
			aReader.IorReadComplete(KErrNone);
			}
		}
	else
		{
		aReader.IorReadComplete(KErrEof);
		}
	}

void CIoFile::IowepWriteL(MIoWriter& aWriter)
	{
	HBufC* buf = HBufC::NewLC(aWriter.IowWriteLength());
	TPtr bufPtr(buf->Des());
	aWriter.IowWrite(bufPtr);

	// Convert to UTF-8
	HBufC8* narrowBuf = LtkUtils::Utf8L(*buf);
	TInt err = iFile.Write(*narrowBuf);
	delete narrowBuf;
	aWriter.IowComplete(err);
	CleanupStack::PopAndDestroy(buf);
	}

void CIoFile::IowepWriteCancel(MIoWriter&)
	{
	}

CIoFile::CIoFile()
	{
	}

void CIoFile::ConstructL(RFs& aFs, const TDesC& aName, RIoFile::TMode aMode)
	{
	LOG(CIoLog::Printf(_L("Attempting to create file 0x%08x (\"%S\")"), this, &aName));
	switch (aMode)
		{
		case RIoFile::ERead:
			{
			User::LeaveIfError(iFile.Open(aFs, aName, EFileRead));
			break;
			}
		case RIoFile::EOverwrite:
			{
			User::LeaveIfError(iFile.Replace(aFs, aName, EFileWrite));
			break;
			}
		case RIoFile::EAppend:
			{
			TInt err = iFile.Open(aFs, aName, EFileWrite);
			if (err == KErrNotFound)
				{
				err = iFile.Create(aFs, aName, EFileWrite);
				}
			User::LeaveIfError(err);
			TInt pos = 0;
			User::LeaveIfError(iFile.Seek(ESeekEnd, pos));
			break;
			}
		default:
			{
			User::Leave(KErrNotSupported);
			}
		}
	}