(file) Return to MyString.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / WMIMapper / WMIProvider

File: [Pegasus] / pegasus / src / WMIMapper / WMIProvider / MyString.cpp (download)
Revision: 1.1, Tue Nov 19 21:32:49 2002 UTC (21 years, 7 months ago) by kumpf
Branch: MAIN
CVS Tags: pep_88, mday-merge-start, mday-merge-pegasus/src/Pegasus/Server, mday-merge-pegasus/src/Pegasus/Common, RELEASE_2_2_1-snapshot, RELEASE_2_2_0_0-release, RELEASE_2_2_0-root, RELEASE_2_2_0-branch, RELEASE_2_2-root, PEGASUS_FC_VERSION_2_2, LOCAL_ASSOCPROV-ROOT, LOCAL_ASSOCPROV-BRANCH
HP-[BJH] Added WMIMapper code at pegasus/src/WMIMapper

//%/////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
// The Open Group, Tivoli Systems
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//==============================================================================
//
// Author: Barbara Packard (barbara_packard@hp.com)
//
// Modified By:
//
//%/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include <Pegasus/Common/Config.h>
//#include <stdio.h>
//#include <stdarg.h>
#include "MyString.h"

PEGASUS_NAMESPACE_BEGIN

/////////////////////////////////////////////////////////////////////////////
// CMyString

void CMyString::Init() {
    m_pszData = NULL;
    m_nStrLen = 0;
	m_nBufLen = 0;
	m_nStatus = S_OK;
}

/////////////////////////////////////////////////////////////////////////////
// Construction/Destruction

CMyString::CMyString()	{
	Init();
}

CMyString::CMyString(const CMyString& strSrc)	{
	Init();
	*this = strSrc.m_pszData;
}

CMyString::CMyString(const unsigned char* psz)	{
	Init();
	*this = (LPCSTR)psz;
}


CMyString::CMyString(LPCTSTR psz)	{
	Init();
	*this = psz;
}

#ifdef _UNICODE
CMyString::CMyString(LPCSTR lpsz)	{
	Init();

    int nLen = ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, NULL, 0);

	AllocBuffer(nLen);

    ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, m_pszData, m_nBufLen);
	m_nStrLen = _tcslen(m_pszData);
}
#endif

CMyString::~CMyString()	{
//	free any pointers
	DeallocBuffer();
}

/////////////////////////////////////////////////////////////////////////////
// Helpers

BOOL CMyString::AllocBuffer(int nLen)	{
// always allocate one extra character for '\0' termination

	if (m_nBufLen > nLen)	{
		return TRUE;
	}

	Init();

	if (nLen > 0)	{
		m_pszData = (LPTSTR)new BYTE[(nLen+1)*sizeof(TCHAR)];
	}

	if (m_pszData)	{
		m_nBufLen = nLen+1;
		memset((void*)m_pszData, '\0', (nLen+1)*sizeof(TCHAR));
	}

	else	{
		m_nStatus = E_OUTOFMEMORY;
		return FALSE;
	}
	
	return TRUE;
}

void CMyString::DeallocBuffer()	{

	if (m_pszData)	{
		delete [] m_pszData;
	}

	Init();
}

void CMyString::AllocBeforeWrite(int nLen)	{

	if (m_pszData && (m_nBufLen > nLen))	{
		memset((void*)m_pszData, '\0', m_nBufLen);
		return;		
	}

	else	{
		DeallocBuffer();
		AllocBuffer(nLen);
	}
}

void CMyString::AssignCopy( int nLen, LPCTSTR lpsz)	{
	AllocBeforeWrite(nLen);

	if (m_pszData)	{
		memcpy(m_pszData, lpsz, nLen*sizeof(TCHAR));
		m_pszData[nLen] = '\0';
		m_nStrLen = nLen;
	}
}


/////////////////////////////////////////////////////////////////////////////
// Overloaded operators

const CMyString& CMyString::operator=(const CMyString& stringSrc)	{
	*this = stringSrc.m_pszData;
	return *this;
}

#ifdef _UNICODE
const CMyString& CMyString::operator=(LPCSTR lpsz)	{
    int nLen = ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, NULL, 0);

	AllocBeforeWrite(nLen);

    if (m_pszData)	{
		::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, m_pszData, m_nBufLen);
		m_nStrLen = _tcslen(m_pszData);
	}

	return *this;
}


#else	//!_UNICODE

/***@
const CMyString& CMyString::operator=(LPCTSTR lpsz)	{
    int nLen = ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, NULL, 0);

	AllocBeforeWrite(nLen);

    if (m_pszData)	{
		::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, lpsz, -1, m_pszData, m_nBufLen);
		m_nStrLen = _tcslen(lpsz);
	}

	return *this;
}
@***/
const CMyString& CMyString::operator=(LPCWSTR lpsz)	{

    if (lpsz && *lpsz)  {
	    int nLen = ::WideCharToMultiByte(CP_ACP, 0, lpsz, -1, NULL, 0, NULL, NULL);

        AllocBeforeWrite(nLen);

        if (m_pszData)  {
            ::WideCharToMultiByte(CP_ACP, 0, lpsz, -1, m_pszData, m_nBufLen, NULL, NULL);
            m_nStrLen = _tcslen(m_pszData);
        }
    }
    else    {
        Empty();
    }
	
	return *this;
}


#endif

const CMyString& CMyString::operator=(BSTR bStr)	{
    _bstr_t bstr(bStr, TRUE);
//    _bstr_t bstr(bStr, FALSE);
//    CComBSTR bstr(bStr);
    *this = (LPCWSTR)bstr;
	return *this;
}

const CMyString& CMyString::operator=(LPCTSTR lpsz)	{
	int nLen = (lpsz != NULL) ? _tcslen(lpsz) : 0;
	AssignCopy( nLen, lpsz);
	return *this;
}

const CMyString& CMyString::operator=(const unsigned char* psz)	{
	*this = (LPCSTR)psz;
	return *this;
}


const CMyString& CMyString::operator=(String sStr)	{
	CString str = sStr.getCString();
	*this = (const char*)str;
	return *this;
}

CMyString::operator LPCTSTR() const	{
	return m_pszData;
}


/////////////////////////////////////////////////////////////////////////////
// string concatenation

void CMyString::ConcatCopy(	int nLen1, LPCTSTR lpszSrc1, 
							int nLen2, LPCTSTR lpszSrc2)	{
// concatenate two sources
// assumes 'this' is a new CMyString
	int nLen = nLen1 + nLen2;

	if (nLen != 0)	{
		AllocBuffer(nLen);
		memcpy(m_pszData, lpszSrc1, nLen1*sizeof(TCHAR));
		memcpy(m_pszData+nLen1, lpszSrc2, nLen2*sizeof(TCHAR));
		m_nStrLen = nLen;
	}
}

void CMyString::ConcatInPlace(int nSrcLen, LPCTSTR lpszSrc)	{
	if (nSrcLen == 0)
		return;

	int nLen = m_nStrLen + nSrcLen;

	if (nLen < m_nBufLen)	{
	// have enough room, just add it
		memcpy(m_pszData+m_nStrLen, lpszSrc, nSrcLen*sizeof(TCHAR));
		m_nStrLen = nLen;
		m_pszData[nLen] = '\0';
	}

	else	{	// buffer is too small - reallocate.
		LPTSTR pszOld = m_pszData;
		ConcatCopy(m_nStrLen, m_pszData, nSrcLen, lpszSrc);

		if (pszOld != NULL)
			delete [] pszOld;
	}
}

const CMyString& CMyString::operator+=(const CMyString& stringSrc)	{

	ConcatInPlace(stringSrc.m_nStrLen, stringSrc.m_pszData);
	return *this;
}

#ifdef _UNICODE
const CMyString& CMyString::operator+=(LPCTSTR lpsz)	{

	if (lpsz != NULL)	{
		ConcatInPlace(_tcslen(lpsz), lpsz);
	}
	return *this;
}

#else	//!_UNICODE
const CMyString& CMyString::operator+=(LPCTSTR lpsz)	{

	if (lpsz != NULL)	{
		CMyString myStr = lpsz;
		ConcatInPlace(_tcslen(lpsz), myStr.m_pszData);
	}
	return *this;
}

#endif

/////////////////////////////////////////////////////////////////////////////
// Operations

LPTSTR CMyString::Copy()	{
	LPTSTR lpsz = new TCHAR[m_nStrLen+1];

	if (lpsz)	{
	_tcscpy(lpsz, m_pszData);
	}
	else	{
		m_nStatus = (m_nStatus == S_OK) ? E_OUTOFMEMORY : m_nStatus;
	}

	return lpsz;
}


BSTR CMyString::Bstr()	{

//    CComBSTR cb(2*m_nStrLen+2);
//	cb = (LPCSTR)m_pszData;
	CComBSTR cb = (LPCSTR)m_pszData;
    BSTR bstr = cb.Copy();
//	_bstr_t cb(m_pszData);
//    BSTR bstr = cb.copy();
//	LPOLESTR sz = L(m_pszData);
//	BSTR bstr = SysAllocString(sz);
	return bstr;
}

BOOL CMyString::Compare(BSTR bStr)	{
	_bstr_t sText1(m_pszData);
	_bstr_t sText2(bStr);
	return (sText1==sText2);
}

BOOL CMyString::Compare(LPCTSTR szStr)	{
	_bstr_t sText1(m_pszData);
	_bstr_t sText2(szStr);
	return (sText1==sText2);
}

/////////////////////////////////////////////////////////////////////////////
// Search for a character
//	lifted from CString
//		returns 0 based index of first instance else -1
//
int CMyString::Find(TCHAR ch) const
{
	return Find(ch, 0);
}

int CMyString::Find(TCHAR ch, int nStart) const
{
	if (nStart >= m_nStrLen)	{
		return -1;
	}

	// find first single character
	LPTSTR lpsz = _tcschr(m_pszData + nStart, (_TUCHAR)ch);

	return (NULL == lpsz) ? -1 : (int)(lpsz - m_pszData);
}

/////////////////////////////////////////////////////////////////////////////
// Windows extensions to strings
//	lifted from CString
/*
BOOL CMyString::LoadString(UINT nID)
{
	// This is all done in char so it will work in Win95 too

	// try fixed buffer first (to avoid wasting space in the heap)
	char szTemp[BUF_MAX];
	int nLen = ::LoadStringA(_Module.m_hInstResource, nID, szTemp, sizeof(szTemp));
	if (sizeof(szTemp) - nLen > CHAR_FUDGE)
	{
		*this = szTemp;
		return nLen > 0;
	}

	// try buffer size of 512, then larger size until entire string is retrieved
	int nSize = BUF_MAX;
	LPSTR lpbuf = NULL;
	do
	{
		nSize += BUF_MAX;

		if (lpbuf) delete [] lpbuf;

		lpbuf = new char[nSize];
		if (lpbuf == NULL)	{
			nLen = 0;
			m_nStatus = (m_nStatus == S_OK) ? E_OUTOFMEMORY : m_nStatus;
			break;
		}

		nLen = ::LoadStringA(_Module.m_hInstResource, nID, lpbuf, nSize);
	} while (nSize - nLen <= CHAR_FUDGE);

	if (lpbuf)	{
		*this = lpbuf;
		delete [] lpbuf;
	}

	return nLen > 0;
}
*/

/////////////////////////////////////////////////////////////////////////////
//	method to format data buffer in printable hex format "xx xx xx "
//	lifted from snmp++
//	len - number of bytes to format
void CMyString::GetPrintableHex(int len, const unsigned char* data)	{
	LPTSTR lpsz = new TCHAR[3*len+1];	//2 chars for each byte plus space between
	LPTSTR ptr = lpsz;
	const unsigned char* bytes = data;
	int i;

	if (lpsz)	{
		for (i=0; i<len; i++)	{
			_stprintf(ptr, _T("%2.2X "), *bytes);
			bytes++;
			ptr +=3;
		}
		*ptr = '\0';
	}
	else	{
		m_nStatus = (m_nStatus == S_OK) ? E_OUTOFMEMORY : m_nStatus;
	}

	if (lpsz)	{
		*this = lpsz;
		delete[] lpsz;
	}
}


/////////////////////////////////////////////////////////////////////////////
//	methods to format data buffer as in sprintf.
//		NOTE:  iSize is size of output buffer in TCHAR format. Only iSize
//		characters will be copied.
//		uses _vsntprintf
//		returns number of characters in formatted string

//	protected method which calls _vsntprintf
int CMyString::FormatV(LPCTSTR lpszFormat, int iSize, va_list argList)
{
	int		iNumChars, iLen;
	LPTSTR	lpBuf = NULL;

	iLen = iSize - CHAR_FUDGE;

	lpBuf = new TCHAR[iSize];

	iNumChars = _vsntprintf(lpBuf, iLen, lpszFormat, argList);
	
	if (0 <= iNumChars)	{
		*this = lpBuf;
	}
	
	delete [] lpBuf;

	return iNumChars;
}

// uses sprintf format
int CMyString::Format(LPCTSTR lpszFormat, int iSize, ...)
{
	int iNumChars;
	va_list	argList;

	va_start(argList, iSize);
	iNumChars = FormatV(lpszFormat, iSize, argList);
	va_end(argList);

	return iNumChars;
}

/*
// uses resource ID
int CMyString::Format(UINT nID, int iSize, ...)
{
	int iNumChars;
	CMyString s;
	va_list argList;

	s.LoadString(nID);

	va_start(argList, iSize);
	iNumChars = FormatV(s, iSize, argList);
	va_end(argList);

	return iNumChars;
}
*/

PEGASUS_NAMESPACE_END


No CVS admin address has been configured
Powered by
ViewCVS 0.9.2