📄 opwavehxdataf.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: opwavehxdataf.cpp,v 1.1.32.3 2004/07/09 01:44:17 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** */#include "hlxclib/sys/types.h"#include "hlxclib/sys/stat.h"//#include <unistd.h>#include "hlxclib/fcntl.h"#include "hlxclib/errno.h"#include "hxtypes.h"#include "hxcom.h"#include "hxresult.h"#include "ihxpckts.h"#include "hxbuffer.h"#include "debug.h"#include "opwavehxdataf.h"#include "op_fs.h"///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::QueryInterface// Purpose:// Implement this to export the interfaces supported by your// object.//STDMETHODIMPOpenwaveHXDataFile::QueryInterface(REFIID riid, void** ppvObj){ if (IsEqualIID(riid, IID_IHXDataFile)) { AddRef(); *ppvObj = (IHXDataFile*)this; return HXR_OK; } *ppvObj = NULL; return HXR_NOINTERFACE;} ///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::AddRef// Purpose:// Everyone usually implements this the same... feel free to use// this implementation.//STDMETHODIMP_(ULONG32)OpenwaveHXDataFile::AddRef(){ return InterlockedIncrement(&m_lRefCount);} ///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Release// Purpose:// Everyone usually implements this the same... feel free to use// this implementation.//STDMETHODIMP_(ULONG32)OpenwaveHXDataFile::Release(){ if (InterlockedDecrement(&m_lRefCount) > 0) { return m_lRefCount; } delete this; return 0;} OpenwaveHXDataFile::OpenwaveHXDataFile(IUnknown** ppCommonObj) : m_ulPos(0), m_lRefCount(0), m_LastError(0), m_pFileName(0), m_Fd(-1), m_Flags(0), m_Begin(0), m_BufSize(0), m_BufFill(0), m_Offset(0), m_FileOffset(0), m_FlushSize(0), m_pBuf(0), m_Dirty(0){ m_pFileName = new CHXBuffer; m_pFileName->AddRef(); m_BufSize = GetPageSize();}OpenwaveHXDataFile::~OpenwaveHXDataFile(){ Close(); FreeBuf(); HX_RELEASE(m_pFileName);} ///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Bind:// Purpose:// Cache the file name for opening.// If file already open, close it.//STDMETHODIMP_(void)OpenwaveHXDataFile::Bind(const char* pFileName){ if (m_Fd >= 0) Close(); m_pFileName->Set((BYTE *)pFileName, ::strlen(pFileName)+1);}///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Create// Purpose:// Creates a datafile using the specified mode// uOpenMode - File Open mode - HX_FILEFLAG_READ/HX_FILEFLAG_WRITE/HX_FILEFLAG_BINARY//STDMETHODIMPOpenwaveHXDataFile::Create(UINT16 uOpenMode){ return HXR_NOTIMPL;}///////////////////////////////////////////////////////////////////////////// Method:// HXBufferedDatatFile::Open// Purpose:// Open for buffered i/o. If file is already open, ignore this// open and return HXR_OK status. Flags can have 3 possible values// // HX_FILEFLAG_READ: Open file read only//// HX_FILEFLAG_WRITE: Open file for writing. If file does not// exists, it is created.//// HX_FILEFLAG_READ|HX_FILEFLAG_WRITE: // Open file for both read and writing.//// Note:// HX_FILEFLAG_WRITE implies both reads and writes to the file. It// also implies the file can be randomly updated any where in the// file as well as appended onto the end. If the file exists,// then the file contents are buffered in before updating. As a// result, writing implies both system reads and writes.//// If open succeeds, returns HXR_OK, otherwise HXR_FAIL.//STDMETHODIMPOpenwaveHXDataFile::Open(UINT16 flags){ // flags don't match close and reopen if (m_Fd >= 0 && flags != m_Flags) { Close(); } int status = HXR_OK; if (m_Fd < 0) { OpFsFlags oflags = 0; m_Flags = flags; m_LastError = 0; if (m_Flags & HX_FILEFLAG_WRITE) { oflags = (kOpFsFlagCreate|kOpFsFlagRdwr); if (!(m_Flags & HX_FILEFLAG_NOTRUNC)) { oflags |= kOpFsFlagRdwr; } } else if (m_Flags & HX_FILEFLAG_READ) oflags = kOpFsFlagRdOnly; else return HXR_FAIL; if ((m_Fd = //::open((const char*) m_pFileName->GetBuffer(), oflags, 0644)) < 0) OpFsOpen( (const char*) m_pFileName->GetBuffer(), oflags, 0644 )) == kOpFsErrAny) { status = HXR_FAIL; m_LastError = errno; } else // open ok: initialize { if (m_pBuf == 0) AllocBuf(); m_ulPos = 0; m_Begin = 0; m_BufFill = 0; m_Offset = 0; m_FileOffset = 0; m_FlushSize = 0; m_LastError = 0; m_Dirty = 0; } } return status;}///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Close:// Purpose:// Flush the file buffer (if open of writing and close the file.// Note that file is not closed when there are multiple references// to this object.//STDMETHODIMPOpenwaveHXDataFile::Close(){ int status = HXR_OK; if (m_Fd >= 0 && m_lRefCount <= 1) { if (m_Flags & HX_FILEFLAG_WRITE) FlushBuf(); //if (::close(m_Fd) == -1) if (OpFsClose( m_Fd ) != kOpFsErrOk ) { status = HXR_FAIL; m_LastError = errno; } m_Fd = -1; } return status;}///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Name// Purpose:// Name returns the currently bound file name in FileName.// and returns TRUE, if the a name has been bound. Otherwise// FALSE is returned.//STDMETHODIMP_(BOOL)OpenwaveHXDataFile::Name(REF(IHXBuffer*) pFileName){ if (m_pFileName && m_pFileName->GetSize()) { pFileName = m_pFileName; pFileName->AddRef(); return TRUE; } return FALSE;}///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::IsOpen()// Purpose:// IsOpen returns TRUE if file is open. Otherwise FALSE.//BOOLOpenwaveHXDataFile::IsOpen(){ return (m_Fd >= 0 ? TRUE : FALSE);}///////////////////////////////////////////////////////////////////////////// Method// OpenwaveHXDataFile::Seek:// Purpose:// Just move the seek offset according to the value of whense.// This only sets the local idea of the current offset, no system// level seek is performed. If seek is ok, the current value of// seek offset is returned. Otherwise -1 is returned.//// XXXBH: 24-Mar-99// Note that most of callers expect HXR_OK as return value, not// the current offset. So the above comment is a noop. Also// the offset is passed in as an unsigned, it should be signed to// allow seeks in both directions. Right now we do the ugly cast.//STDMETHODIMPOpenwaveHXDataFile::Seek(ULONG32 offset, UINT16 whense){ m_LastError = 0; if (m_Fd > 0) { if (OpFsSeek( m_Fd, (OpFsSize)offset, (U8CPU)whense) == kOpFsErrAny) { m_LastError = errno; return HXR_INVALID_FILE; } return HXR_OK; } return HXR_INVALID_FILE;}///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Tell()// Purpose:// Tell returns the current (logical) file position in the file//STDMETHODIMP_(ULONG32)OpenwaveHXDataFile::Tell(){ INT32 offset = -1; if (m_Fd > 0) { m_LastError = kOpFsErrOk; // so we do this instead.... if ((offset = (INT32)OpFsSeek( m_Fd, 0, kOpFsSeekCur)) == kOpFsErrAny) { m_LastError = kOpFsErrAny; } } return (ULONG32)offset;}///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Read:// Purpose// Read reads up to count bytes of data into buf.// returns the number of bytes read, EOF, or -1 if the read failed //STDMETHODIMP_(ULONG32)OpenwaveHXDataFile::Read(REF(IHXBuffer *) pBuf, ULONG32 count){ UINT32 ncnt = 0; m_LastError = 0; pBuf = new CHXBuffer(); pBuf->AddRef(); pBuf->SetSize(count); if (m_Fd > 0) { if ((int)(ncnt = OpFsRead(m_Fd, (void *)pBuf->GetBuffer(), count)) == kOpFsErrAny) { m_LastError = kOpFsErrAny; pBuf->Release(); pBuf = NULL; /* * XXX PM Have to return 0 here because it is unsigned * return value. */ return 0; } else { m_ulPos += ncnt; } if (ncnt < count) { pBuf->SetSize(ncnt); } } return (ULONG32)ncnt;}///////////////////////////////////////////////////////////////////////////// Method:// OpenwaveHXDataFile::Write:// Purpose:// Write the requested number of bytes. The number of bytes// written are return. If errors occur, -1 is returned. The// LastError() member function returns the value of errno from// the last system request.//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -