|
// MyDataObject.cpp : Implementation of CMyDataObject
#include "stdafx.h"
#include "SimpleDataObject.h"
#include "MyDataObject.h"
#include <comdef.h> // for _bstr_t
/////////////////////////////////////////////////////////////////////////////
// CMyDataObject
CMyDataObject::CMyDataObject()
: m_numCols(0),
m_numRows(0),
m_allocatedRows(0),
m_rowSize(0),
m_columnSize(10 + 1),
m_pData(0)
{
}
CMyDataObject::~CMyDataObject()
{
for (size_t i = 0; i < m_numCols; i++)
{
free(m_columnNames[i]);
}
delete [] m_pData;
}
HRESULT CMyDataObject::SetColumnSize(
/* [in] */ long columnSize)
{
if (m_pData != 0)
{
return E_UNEXPECTED; // too late!
}
m_columnSize = columnSize + 1; // add one for null term
return S_OK;
}
HRESULT CMyDataObject::AddColumn(
/* [in] */ BSTR columnName,
/* [out, retval] */ long *index)
{
if (m_pData != 0)
{
return E_UNEXPECTED; // too late!
}
if (!index)
{
return E_POINTER;
}
// add the column to the column map,
_bstr_t bstr(columnName);
m_columnNames.Add(strdup((char*)bstr));
*index = m_numCols++;
return S_OK;
}
HRESULT CMyDataObject::GetColumnName(
/* [in] */ long index,
/* [out, retval] */ BSTR *columnName)
{
if (!CheckColumnValid(index))
{
return E_INVALIDARG;
}
if (!columnName)
{
return E_POINTER;
}
char *pColumnName = m_columnNames[index - 1];
CComBSTR bstr(pColumnName);
*columnName = bstr.Detach();
return S_OK;
}
HRESULT CMyDataObject::AddRow(
/* [out, retval] */ long *index)
{
if (m_numCols == 0)
{
return E_UNEXPECTED; // To early!
}
if (!index)
{
return E_POINTER;
}
if (m_allocatedRows == 0)
{
// First allocation
m_rowSize = m_numCols * m_columnSize;
}
*index = m_numRows;
if (m_pData == 0)
{
// First row to be added.
m_numRows++;
m_allocatedRows++;
m_pData = CreateTable();
}
else if (m_numRows == m_allocatedRows)
{
// need to expand the row buffer
char *pOldData = m_pData;
m_numRows++;
m_allocatedRows++;
m_pData = CreateTable();
memcpy(m_pData, pOldData, (m_numRows -1) * m_rowSize);
delete [] pOldData;
}
else
{
m_numRows++;
memset(GetAt(m_numRows, 1), 0, m_rowSize);
}
return S_OK;
}
HRESULT CMyDataObject::RemoveRow(
/* [in] */ long index)
{
if (!CheckRowValid(index))
{
return E_INVALIDARG;
}
// remove a row from somewhere in the table...
// all rows below shuffle up one
// array is not made smaller...
if ((size_t)index != m_numRows) // if it's not the last row...
{
char *pThisRow = GetAt(index, 1);
char *pNextRow = GetAt(index + 1, 1);
memmove(pThisRow, pNextRow, (m_numRows - index) * m_rowSize);
}
m_numRows--;
return S_OK;
}
HRESULT CMyDataObject::Depth(
/* [out, retval] */ long *depth)
{
if (!depth)
{
return E_POINTER;
}
*depth = m_numRows;
return S_OK;
}
HRESULT CMyDataObject::Width(
/* [out, retval] */ long *width)
{
if (!width)
{
return E_POINTER;
}
*width = m_numCols;
return S_OK;
}
HRESULT CMyDataObject::SetAt(
/* [in] */ long rowIndex,
/* [in] */ long columnIndex,
/* [in] */ BSTR value)
{
if (!CheckRowAndColumnValid(rowIndex, columnIndex))
{
return E_INVALIDARG;
}
_bstr_t bstr(value);
if (bstr.length() >= m_columnSize)
{
return E_INVALIDARG;
}
char *pData = GetAt(rowIndex, columnIndex);
if (bstr.length() > 0)
{
memcpy(pData, (char*)bstr, bstr.length());
}
*(pData + bstr.length()) = '\0';
return S_OK;
}
HRESULT CMyDataObject::GetAt(
/* [in] */ long rowIndex,
/* [in] */ long columnIndex,
/* [out, retval] */ BSTR *value)
{
if (!CheckRowAndColumnValid(rowIndex, columnIndex))
{
return E_INVALIDARG;
}
if (!value)
{
return E_POINTER;
}
char *pData = GetAt(rowIndex, columnIndex);
// convert to BSTR
CComBSTR bstr(pData);
*value = bstr.Detach();
return S_OK;
}
char *CMyDataObject::GetAt(const size_t row, const size_t col)
{
ATLASSERT(CheckRowAndColumnValid(row, col));
return &m_pData[((row -1) * m_rowSize) + ((col -1)* m_columnSize)];
}
char *CMyDataObject::CreateTable()
{
char *pTable = new char[(m_allocatedRows + 1) * m_rowSize];
memset(pTable, 0, (m_allocatedRows + 1) * m_rowSize);
return pTable;
}
bool CMyDataObject::CheckRowAndColumnValid(
const size_t row,
const size_t column)
{
return CheckRowValid(row) && CheckColumnValid(column);
}
bool CMyDataObject::CheckRowValid(const size_t row)
{
bool ok = true;
if (row < 1 || row > m_numRows)
{
ok = false;
}
return ok;
}
bool CMyDataObject::CheckColumnValid(const size_t column)
{
bool ok = true;
if (column < 1 ||
column > m_numCols)
{
ok = false;
}
return ok;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Len has been programming for over 30 years, having first started with a
Sinclair ZX-80. Now he runs his own consulting company,
JetByte Limited and has a technical blog
here.
JetByte provides contract programming and consultancy services. We can provide experience in COM, Corba, C++, Windows NT and UNIX. Our speciality is the design and implementation of systems but we are happy to work with you throughout the entire project life-cycle. We are happy to quote for fixed price work, or, if required, can work for an hourly rate.
We are based in London, England, but, thanks to the Internet, we can work 'virtually' anywhere...
Please note that many of the articles here may have updated code available on Len's
blog and that the IOCP socket server framework is also available in a licensed, much improved and fully supported version, see
here for details.