sl@0: // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include "filestreambuf.h" sl@0: sl@0: namespace PCStore sl@0: { sl@0: /** sl@0: Constructs a file stream buffer object with the specified file name and open mode. sl@0: sl@0: Opens the file in specified read/write mode. sl@0: sl@0: @param aFileName The name of the file which the file stream buffer will open and access. sl@0: @param aMode The mode in which the file would be opened. sl@0: @exception TStoreException::EFileOpenError Error occurs in file opening. sl@0: */ sl@0: CFileStreamBuf::CFileStreamBuf(const char* aFileName, TFileMode aMode) sl@0: :iFile(NULL), iStreamOpened(false) sl@0: { sl@0: if(aMode == EWriteFile) sl@0: { sl@0: iFile=fopen(aFileName,"wb"); sl@0: } sl@0: else sl@0: { sl@0: iFile=fopen(aFileName,"rb"); sl@0: } sl@0: if(iFile == NULL) sl@0: { sl@0: throw TStoreException(TStoreException::EFileOpenError); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Destructor. sl@0: Closes the file. sl@0: */ sl@0: CFileStreamBuf::~CFileStreamBuf() sl@0: { sl@0: fclose(iFile); sl@0: } sl@0: sl@0: /** sl@0: Flags that a stream with the specified stream id is opened. sl@0: sl@0: Checks whether a stream has been opened. If not, moves the file pointer to the position sl@0: which is represented by the given stream id, and sets the stream open flag to TRUE. If a sl@0: stream has been opened, throws an exception. sl@0: sl@0: This function is called by the constructor of CStoreReadStream and CStoreWriteStream sl@0: to flag the new stream is opened. sl@0: sl@0: @param aStreamId The stream id of the open stream. sl@0: @exception TStoreException::EStreamExclusionError Another stream is still opened. sl@0: @exception TStoreException::EFileSeekError Error occurs when seeking within the file sl@0: due to an incorrect stream id or other reason. sl@0: */ sl@0: void CFileStreamBuf::StreamOpen(TStreamId aStreamId) sl@0: { sl@0: //check the opened stream sl@0: if(iStreamOpened) sl@0: { sl@0: throw TStoreException(TStoreException::EStreamExclusionError); sl@0: } sl@0: // set pointer to the correct position sl@0: TInt ret = fseek(iFile,aStreamId.Value(),SEEK_SET); sl@0: if(ret) sl@0: { sl@0: throw TStoreException(TStoreException::EFileSeekError); sl@0: } sl@0: sl@0: iStreamOpened = true; sl@0: } sl@0: sl@0: /** sl@0: Flags that the opened stream is closed. sl@0: sl@0: This function is called by the destructor of the CStoreReadStream and CStoreWriteStream sl@0: to indicate the stream is being closed. sl@0: */ sl@0: void CFileStreamBuf::StreamClose() sl@0: { sl@0: iStreamOpened = false; sl@0: } sl@0: sl@0: /** sl@0: Gets a new stream id which will be used to create a write stream. sl@0: sl@0: The new stream id is the end position of the store file. sl@0: sl@0: Moves the read/write pointer to the end to the file to get the stream id. sl@0: sl@0: @return The new stream id. sl@0: @exception TStoreException::EStreamExclusionError Another stream is still opened. sl@0: @exception TStoreException::EFileSeekError Error occurs when the file pointer is moved to sl@0: the end of file. sl@0: */ sl@0: TStreamId CFileStreamBuf::GetNewStreamId() sl@0: { sl@0: //check the opened stream sl@0: if(iStreamOpened) sl@0: { sl@0: throw TStoreException(TStoreException::EStreamExclusionError); sl@0: } sl@0: // move pointer to the end of the file sl@0: TInt ret = fseek(iFile,0,SEEK_END); sl@0: if(ret) sl@0: { sl@0: throw TStoreException(TStoreException::EFileSeekError); sl@0: } sl@0: sl@0: TUint size = ftell(iFile); sl@0: TStreamId id = size; sl@0: return id; sl@0: } sl@0: sl@0: /** sl@0: Reads the specified length of 8-bit data to the supplied buffer from the file. sl@0: sl@0: Assert aLength must not be negative and aPtr must not be NULL. sl@0: sl@0: @param aPtr The pointer to the buffer to receive the read data. sl@0: @param aLength The length of the data to read. sl@0: @exception TStoreException::EFileReadError Error occurs in data reading. sl@0: */ sl@0: void CFileStreamBuf::Read(const TUint8* aPtr, TInt32 aLength) sl@0: { sl@0: assert(aLength >= 0 && aPtr != NULL); sl@0: if (aLength > 0) sl@0: { sl@0: size_t numRead; sl@0: numRead = fread(reinterpret_cast(const_cast(aPtr)),\ sl@0: sizeof(TUint8), aLength, iFile); sl@0: if(numRead < static_cast(aLength)) sl@0: { sl@0: throw TStoreException(TStoreException::EFileReadError); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Writes the specified length of 8-bit data from the supplied buffer to the file. sl@0: sl@0: Assert aLength must not be negative and aPtr must not be NULL. sl@0: sl@0: @param aPtr The pointer to the buffer holding the written data. sl@0: @param aLength The length of the data to write. sl@0: @exception TStoreException::EFileWriteError Error occurs in data writing. sl@0: */ sl@0: void CFileStreamBuf::Write(const TUint8* aPtr, TInt32 aLength) sl@0: { sl@0: assert(aLength >= 0 && aPtr != NULL); sl@0: if (aLength > 0) sl@0: { sl@0: size_t numWritten ; sl@0: numWritten = fwrite(reinterpret_cast(const_cast(aPtr)),\ sl@0: sizeof(TUint8), aLength, iFile); sl@0: if(numWritten < static_cast(aLength)) sl@0: { sl@0: throw TStoreException(TStoreException::EFileWriteError); sl@0: } sl@0: } sl@0: } sl@0: }