📄 opwavehxdataf.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 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
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (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.
*
* 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.
//
STDMETHODIMP
OpenwaveHXDataFile::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
//
STDMETHODIMP
OpenwaveHXDataFile::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.
//
STDMETHODIMP
OpenwaveHXDataFile::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.
//
STDMETHODIMP
OpenwaveHXDataFile::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.
//
BOOL
OpenwaveHXDataFile::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.
//
STDMETHODIMP
OpenwaveHXDataFile::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.
//
STDMETHODIMP_(ULONG32)
OpenwaveHXDataFile::Write(REF(IHXBuffer *) pBuf)
{
if (m_Fd >= 0)
{
pBuf->AddRef();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -