1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/persistentdata/persistentstorage/store/pcstore/src/filestreambuf.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,179 @@
1.4 +// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +
1.20 +#include <assert.h>
1.21 +#include <pcstore/storeexception.h>
1.22 +#include "filestreambuf.h"
1.23 +
1.24 +namespace PCStore
1.25 +{
1.26 +/**
1.27 +Constructs a file stream buffer object with the specified file name and open mode.
1.28 +
1.29 +Opens the file in specified read/write mode.
1.30 +
1.31 +@param aFileName The name of the file which the file stream buffer will open and access.
1.32 +@param aMode The mode in which the file would be opened.
1.33 +@exception TStoreException::EFileOpenError Error occurs in file opening.
1.34 +*/
1.35 +CFileStreamBuf::CFileStreamBuf(const char* aFileName, TFileMode aMode)
1.36 + :iFile(NULL), iStreamOpened(false)
1.37 + {
1.38 + if(aMode == EWriteFile)
1.39 + {
1.40 + iFile=fopen(aFileName,"wb");
1.41 + }
1.42 + else
1.43 + {
1.44 + iFile=fopen(aFileName,"rb");
1.45 + }
1.46 + if(iFile == NULL)
1.47 + {
1.48 + throw TStoreException(TStoreException::EFileOpenError);
1.49 + }
1.50 + }
1.51 +
1.52 +/**
1.53 +Destructor.
1.54 +Closes the file.
1.55 +*/
1.56 +CFileStreamBuf::~CFileStreamBuf()
1.57 + {
1.58 + fclose(iFile);
1.59 + }
1.60 +
1.61 +/**
1.62 +Flags that a stream with the specified stream id is opened.
1.63 +
1.64 +Checks whether a stream has been opened. If not, moves the file pointer to the position
1.65 +which is represented by the given stream id, and sets the stream open flag to TRUE. If a
1.66 +stream has been opened, throws an exception.
1.67 +
1.68 +This function is called by the constructor of CStoreReadStream and CStoreWriteStream
1.69 +to flag the new stream is opened.
1.70 +
1.71 +@param aStreamId The stream id of the open stream.
1.72 +@exception TStoreException::EStreamExclusionError Another stream is still opened.
1.73 +@exception TStoreException::EFileSeekError Error occurs when seeking within the file
1.74 +due to an incorrect stream id or other reason.
1.75 +*/
1.76 +void CFileStreamBuf::StreamOpen(TStreamId aStreamId)
1.77 + {
1.78 + //check the opened stream
1.79 + if(iStreamOpened)
1.80 + {
1.81 + throw TStoreException(TStoreException::EStreamExclusionError);
1.82 + }
1.83 + // set pointer to the correct position
1.84 + TInt ret = fseek(iFile,aStreamId.Value(),SEEK_SET);
1.85 + if(ret)
1.86 + {
1.87 + throw TStoreException(TStoreException::EFileSeekError);
1.88 + }
1.89 +
1.90 + iStreamOpened = true;
1.91 + }
1.92 +
1.93 +/**
1.94 +Flags that the opened stream is closed.
1.95 +
1.96 +This function is called by the destructor of the CStoreReadStream and CStoreWriteStream
1.97 +to indicate the stream is being closed.
1.98 +*/
1.99 +void CFileStreamBuf::StreamClose()
1.100 + {
1.101 + iStreamOpened = false;
1.102 + }
1.103 +
1.104 +/**
1.105 +Gets a new stream id which will be used to create a write stream.
1.106 +
1.107 +The new stream id is the end position of the store file.
1.108 +
1.109 +Moves the read/write pointer to the end to the file to get the stream id.
1.110 +
1.111 +@return The new stream id.
1.112 +@exception TStoreException::EStreamExclusionError Another stream is still opened.
1.113 +@exception TStoreException::EFileSeekError Error occurs when the file pointer is moved to
1.114 +the end of file.
1.115 +*/
1.116 +TStreamId CFileStreamBuf::GetNewStreamId()
1.117 + {
1.118 + //check the opened stream
1.119 + if(iStreamOpened)
1.120 + {
1.121 + throw TStoreException(TStoreException::EStreamExclusionError);
1.122 + }
1.123 + // move pointer to the end of the file
1.124 + TInt ret = fseek(iFile,0,SEEK_END);
1.125 + if(ret)
1.126 + {
1.127 + throw TStoreException(TStoreException::EFileSeekError);
1.128 + }
1.129 +
1.130 + TUint size = ftell(iFile);
1.131 + TStreamId id = size;
1.132 + return id;
1.133 + }
1.134 +
1.135 +/**
1.136 +Reads the specified length of 8-bit data to the supplied buffer from the file.
1.137 +
1.138 +Assert aLength must not be negative and aPtr must not be NULL.
1.139 +
1.140 +@param aPtr The pointer to the buffer to receive the read data.
1.141 +@param aLength The length of the data to read.
1.142 +@exception TStoreException::EFileReadError Error occurs in data reading.
1.143 +*/
1.144 +void CFileStreamBuf::Read(const TUint8* aPtr, TInt32 aLength)
1.145 + {
1.146 + assert(aLength >= 0 && aPtr != NULL);
1.147 + if (aLength > 0)
1.148 + {
1.149 + size_t numRead;
1.150 + numRead = fread(reinterpret_cast<void*>(const_cast<TUint8*>(aPtr)),\
1.151 + sizeof(TUint8), aLength, iFile);
1.152 + if(numRead < static_cast<TUint32>(aLength))
1.153 + {
1.154 + throw TStoreException(TStoreException::EFileReadError);
1.155 + }
1.156 + }
1.157 + }
1.158 +
1.159 +/**
1.160 +Writes the specified length of 8-bit data from the supplied buffer to the file.
1.161 +
1.162 +Assert aLength must not be negative and aPtr must not be NULL.
1.163 +
1.164 +@param aPtr The pointer to the buffer holding the written data.
1.165 +@param aLength The length of the data to write.
1.166 +@exception TStoreException::EFileWriteError Error occurs in data writing.
1.167 +*/
1.168 +void CFileStreamBuf::Write(const TUint8* aPtr, TInt32 aLength)
1.169 + {
1.170 + assert(aLength >= 0 && aPtr != NULL);
1.171 + if (aLength > 0)
1.172 + {
1.173 + size_t numWritten ;
1.174 + numWritten = fwrite(reinterpret_cast<void*>(const_cast<TUint8*>(aPtr)),\
1.175 + sizeof(TUint8), aLength, iFile);
1.176 + if(numWritten < static_cast<TUint32>(aLength))
1.177 + {
1.178 + throw TStoreException(TStoreException::EFileWriteError);
1.179 + }
1.180 + }
1.181 + }
1.182 +}