/*============================================================================ * * File Name: OleCharConv.h * * Class COleCharConv * * Description: Straight forward shim class - converts char * to wchar_t * * (or OLECHAR) and so on * * Author: Roger Hughes * * Date: 28/12/98 * * *==============================================================================*/ #if !defined(AFX_OLECHARCONV_H__4991FB57_9E4F_11D2_ADEE_000000000000__INCLUDED_) #define AFX_OLECHARCONV_H__4991FB57_9E4F_11D2_ADEE_000000000000__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include #include class COleCharConv { public: COleCharConv(); COleCharConv(const wchar_t * pOleStr); COleCharConv(const char * psz); virtual ~COleCharConv(); operator wchar_t *(); operator const char *(); private: char * m_allocPsz; // an allocated string wchar_t *m_allocPwc; // allocated wide char }; //=== Inline stuff ========================================================================== //--------------------------------------------------------------------------- // Method: ctors //--------------------------------------------------------------------------- _inline COleCharConv::COleCharConv() : m_allocPsz(NULL), m_allocPwc(NULL) { } _inline COleCharConv::COleCharConv(const wchar_t * pOleStr) : m_allocPsz(NULL), m_allocPwc(NULL) { if(pOleStr) { int len = wcslen(pOleStr); m_allocPwc = (wchar_t *)malloc(sizeof(wchar_t)* (len+5)); if(m_allocPwc) { wcscpy(m_allocPwc,pOleStr); } m_allocPsz = (char *)malloc(len+5); if(m_allocPsz) { int i = wcstombs(m_allocPsz,pOleStr,len); if(i != -1) // check for errors { *(m_allocPsz + len) = '\0'; } } } } _inline COleCharConv::COleCharConv(const char * psz) : m_allocPsz(NULL), m_allocPwc(NULL) { // here we convert a passed in char * to a wchar_t * if(psz) { int len = strlen(psz); m_allocPwc = (wchar_t *)malloc(sizeof(wchar_t) * (len+5)); if(m_allocPwc) { int i = mbstowcs(m_allocPwc,psz,len); if(i != -1) // check for errors { *(m_allocPwc + len) = OLECHAR('\0'); } } m_allocPsz = (char *)malloc(len+5); if(m_allocPsz) strcpy(m_allocPsz,psz); } } //--------------------------------------------------------------------------- // Method: dtors //--------------------------------------------------------------------------- _inline COleCharConv::~COleCharConv() { if(m_allocPsz) free(m_allocPsz); if(m_allocPwc) free(m_allocPwc); } //--------------------------------------------------------------------------- // Method: operator overloads //--------------------------------------------------------------------------- _inline COleCharConv::operator wchar_t *() { return m_allocPwc; } //--------------------------------------------------------------------------- // Method: operator overloads //--------------------------------------------------------------------------- _inline COleCharConv::operator const char *() { return m_allocPsz; } #endif // !defined(AFX_OLECHARCONV_H__4991FB57_9E4F_11D2_ADEE_000000000000__INCLUDED_) /*============================================================================ * End of File: OleCharConv.h ==============================================================================*/