#include "StdAfx.h"
#include "Attribute_CArrayCSTRING.h"
#include <exception>
using namespace std;
// Construction/Destruction
CAttribute_CArrayCSTRING::CAttribute_CArrayCSTRING(CObjectList* pStringList, int index)
: CAttribute_Generic(index)
{
if ( !pStringList )
{
AllocateArray();
}
else
{
try
{
value = NULL;
value = new CObjectList(10);
if ( !value )
throw;
for( UINT32 iString = 0; iString < pStringList->GetObjectCount(); iString++ )
{
TCHAR* pCurString = NULL;
pCurString = (TCHAR*) pStringList->GetObjectByIdx(iString);
if ( pCurString )
{
TCHAR* pNewString = NULL;
size_t unSize = _tcslen(pCurString) + 1;
pNewString = new TCHAR[unSize];
if ( pNewString )
{
_tcscpy(pNewString,pCurString);
value->AddObject((POINTER) pNewString);
}
}
}
m_size = (size_t) value->GetBinStringTotalSize();
}
catch(...)
{
value = NULL;
m_size = 0;
}
}
value_tmp = NULL;
value_bin = NULL;
ClassName = _T("CAttribute_CArrayCSTRING");
Kind = IDC_ATTR_ARRAY_STRING;
}
CAttribute_CArrayCSTRING::~CAttribute_CArrayCSTRING()
{
FreeArray();
if ( value_bin )
{
delete [] value_bin;
}
}
void CAttribute_CArrayCSTRING::AllocateArray()
{
try
{
value = NULL;
value = new CObjectList(10);
if ( !value )
throw;
// init the strings
TCHAR* pObj;
for ( UINT32 unObj = 0; unObj < value->GetObjectCount(); unObj++ )
{
pObj = NULL;
pObj = (TCHAR*) new TCHAR[2];
if (pObj)
{
_tcscpy(pObj,IDC_NULL_TEXT);
*(pObj+1) = L'\0';
value->AddObject((POINTER) pObj);
}
}
m_size = (size_t) value->GetBinStringTotalSize();
}
catch (...)
{
m_size = 0;
value = NULL;
value_tmp = NULL; // not used at the moment
}
}
void CAttribute_CArrayCSTRING::FreeArray()
{
try
{
if ( NULL != value )
{
for (unsigned int i=0; i<value->GetObjectCount(); i++)
{
TCHAR* pString = NULL;
pString = (TCHAR*) value->GetObjectByIdx(i);
try { delete [] pString; } catch(...) {/* do nothing */}
}
value->EraseAllEntries();
delete value;
value = NULL;
m_size = 0;
}
}
catch (...)
{
m_size = 0;
value = NULL;
value_tmp = NULL;
}
}
// This method does nothing for this object
void CAttribute_CArrayCSTRING::SetSize(size_t& size)
{
}
void CAttribute_CArrayCSTRING::GetSize(size_t& size)
{
if (value)
{
size = m_size;
}
else
{
size = 0;
}
}
bool CAttribute_CArrayCSTRING::IsEmpty(bool ori)
{
bool bRes = true;
if (value && (value->GetObjectCount()>0))
bRes = false;
// not used at the moment value_tmp
return bRes;
}
// Safe access methods
void CAttribute_CArrayCSTRING::GetAt(CString& szDummy, size_t index)
{
szDummy = IDC_NULL_TEXT;
if (value)
{
if ( (0<=index) && (index<value->GetObjectCount()) )
{
TCHAR* pString = NULL;
pString = (TCHAR*) value->GetObjectByIdx((unsigned int) index);
if (pString)
{
szDummy = pString;
}
}
}
}
void CAttribute_CArrayCSTRING::SetAt(CString& szDummy, size_t index)
{
if (value)
{
TCHAR* pString = NULL;
int nLen = szDummy.GetLength();
pString = new TCHAR[nLen+1];
if (pString)
{
_tcscpy(pString,szDummy.GetBuffer());
szDummy.ReleaseBuffer();
// remove old one
if ( (0<=index) && (index<value->GetObjectCount()) )
{
TCHAR* pStringToRem = NULL;
pStringToRem = (TCHAR*) value->GetObjectByIdx((unsigned int) index);
if ( pStringToRem )
{
try { delete [] pStringToRem; } catch(...) { /* do nothing */ }
}
value->DeleteObjectIdx((unsigned int) index);
value->InsertObject((POINTER) pString,(unsigned int) index);
}
else // could not find it, add it anyway at the end
{
value->AddObject((POINTER) pString);
}
// get new size
m_size = (size_t) value->GetBinStringTotalSize();
}
}
}
// CopyTo, CopyFrom
void CAttribute_CArrayCSTRING::CopyTo(CObjectList* pStringListTo)
{
if( !pStringListTo )
{
pStringListTo = new CObjectList(1);
if ( !pStringListTo )
throw;
}
if (value)
{
for ( unsigned int i = 0; i < ((unsigned int) value->GetObjectCount()); i++ )
{
try
{
TCHAR* pString = NULL;
pString = (TCHAR*) value->GetObjectByIdx(i);
if ( pString )
{
size_t nLen = _tcslen(pString);
TCHAR* pNewString = NULL;
pNewString = new TCHAR[nLen+1];
if ( pNewString )
{
_tcscpy(pNewString,pString);
*(pNewString+nLen) = L'\0';
pStringListTo->AddObject((POINTER) pNewString);
}
}
}
catch ( exception &e )
{
//out_of_range
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::CopyTo - exception: probably out of bounds"));
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::CopyTo - exception: what = %s"),e.what());
break;
}
catch (...)
{
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::CopyTo - exception: probably out of bounds"));
break;
}
}
}
}
void CAttribute_CArrayCSTRING::CopyFrom(CObjectList* pStringListFrom)
{
if( !pStringListFrom )
return;
if (!value)
{
value = new CObjectList(1);
if(!value)
throw;
}
else
{
FreeArray();
value = new CObjectList(1);
if(!value)
throw;
}
if (value)
{
for ( unsigned int i = 0; i < pStringListFrom->GetObjectCount(); i++ )
{
try
{
TCHAR* pString = NULL;
pString = (TCHAR*) pStringListFrom->GetObjectByIdx(i);
if (pString)
{
size_t nLen = _tcslen(pString);
TCHAR* pNewString = NULL;
pNewString = new TCHAR[nLen+1];
if ( pNewString )
{
_tcscpy(pNewString,pString);
*(pNewString+nLen) = L'\0';
value->AddObject((POINTER) pNewString);
}
}
}
catch ( exception &e )
{
//out_of_range
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::CopyTo - exception: probably out of bounds"));
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::CopyTo - exception: what = %s"),e.what());
}
catch (...)
{
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::CopyTo - exception: probably out of bounds"));
}
m_size = (size_t) value->GetBinStringTotalSize();
}
}
}
// Normal attribute methods
void CAttribute_CArrayCSTRING::Get_Value_Str(CString& szDummy, size_t index)
{
GetAt(szDummy,index);
}
void CAttribute_CArrayCSTRING::Get_Value_Str_tmp(CString& szDummy, size_t index)
{
szDummy = IDC_NULL_TEXT;
return;
}
void CAttribute_CArrayCSTRING::SetAttributeCast(CString& szDummy, size_t index)
{
if ( value )
{
SetAt(szDummy,index);
}
}
void CAttribute_CArrayCSTRING::SetTmpAttributeCast(CString& szDummy, size_t index)
{
return;
}
void CAttribute_CArrayCSTRING::Empty_Value(bool ori)
{
if (value && (value->GetObjectCount()>0))
{
FreeArray();
}
}
// concatenate strings without last null terminator
// strings are separated by the escape sequence
void CAttribute_CArrayCSTRING::GetAttributeBinData(BIN_KIND_TYPE& bType,BIN_KIND_SIZE& bSize,void*& pAttribute)
{
TCHAR* szTotalBinString = NULL;
try
{
bType = BIN_KIND_ARRAY_STRING;
bSize = (BIN_KIND_SIZE) value->GetBinStringTotalSize(); // binary size
// calculate total size adding escape sequences
#ifdef UNICODE
bSize += (BIN_KIND_SIZE) (value->GetObjectCount() * 2 * ESCAPE_SEQUENCE_SIZE);
#else
bSize += (BIN_KIND_SIZE) (value->GetObjectCount() * ESCAPE_SEQUENCE_SIZE);
#endif
szTotalBinString = new TCHAR[bSize+1];
_tcscpy(szTotalBinString,IDC_NULL_TEXT);
if (szTotalBinString)
{
TCHAR* pszString;
for( UINT32 unObj = 0; unObj < value->GetObjectCount(); unObj++)
{
pszString = NULL;
pszString = (TCHAR*) value->GetObjectByIdx(unObj);
_tcscat(szTotalBinString,pszString);
_tcscat(szTotalBinString,ESCAPE_SEQUENCE);
}
if ( value_bin )
{
try { delete [] value_bin; } catch (...) { /* do nothing */ }
}
value_bin = szTotalBinString;
pAttribute = (void*) value_bin;
}
else
{
bType = BIN_KIND_NULL;
bSize = 0;
pAttribute = NULL;
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::GetAttributeBinData - error: could not get pointer value."));
}
}
catch (...)
{
bType = BIN_KIND_NULL;
bSize = 0;
pAttribute = NULL;
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::GetAttributeBinData - error: could not get pointer value."));
}
}
// strings are separated by the escape sequence without null terminator
void CAttribute_CArrayCSTRING::SetAttributeBinData(BIN_KIND_TYPE bType,BIN_KIND_SIZE bSize,void* pAttribute)
{
if ( bType != BIN_KIND_ARRAY_STRING )
return;
TCHAR* pTotalBinString = NULL;
try
{
#ifdef UNICODE
bSize = bSize/2; // binary size
#endif
pTotalBinString = new TCHAR[bSize+1];
if ( pTotalBinString )
{
pTotalBinString[bSize] = L'\0';
_tcsncpy(pTotalBinString,(TCHAR*) pAttribute, bSize);
if ( _tcslen(pTotalBinString) )
{
// set the elements
BIN_KIND_SIZE bEndSize = 0, bTotEndSize = 0;
TCHAR* pFirst = pTotalBinString;
TCHAR* pNext = NULL;
TCHAR* pElement = NULL;
while ( NULL != (pNext = _tcsstr(pFirst,ESCAPE_SEQUENCE)) )
{
bEndSize = (BIN_KIND_SIZE) (pNext - pFirst);
pElement = NULL;
pElement = new TCHAR[bEndSize+1];
if ( pElement )
{
pElement[bEndSize] = L'\0';
_tcsncpy(pElement,pFirst,bEndSize);
value->AddObject((POINTER) pElement);
}
bTotEndSize += bEndSize + ESCAPE_SEQUENCE_SIZE;
if ( (bTotEndSize+1) >= bSize )
break;
else
pFirst = pNext + ESCAPE_SEQUENCE_SIZE;
}
}
try { delete [] pTotalBinString; } catch (...) { /* do nothing */ }
}
}
catch (...)
{
LTRACE(LOG_TRLVL_ERROR,_T("CAttribute_CArrayCSTRING::SetAttributeBinData - error: could not set value."));
}
}