/******************************************************************** file name : const2buffer.h author : Clark/陈泽丹 created : 2011-1-8 purpose : *********************************************************************/ #pragma once #include <tchar.h> namespace Clark { class const2buffer { public: static const2buffer c2b(const TCHAR* _pText) throw(const TCHAR*) {//结果用值传递比用局部,引用, 指针,静态传出结果的作法更具安全意义 try{ return const2buffer(_pText); } catch(const TCHAR* pError){ throw pError; } } explicit const2buffer(const TCHAR* _pText) throw(const TCHAR*): m_pText(NULL) { init(_pText); } const2buffer(const const2buffer& other) throw(const TCHAR*):m_pText(NULL) {//有内存操作,防止class objThis = other时出现浅拷贝问题 init(other.m_pText); } const2buffer& operator = (const const2buffer& other) {//有内存操作,防止objThis = other时出现浅拷贝问题 init(other.m_pText); return *this; } ~const2buffer() { uninit(); } operator TCHAR*() { return m_pText; } private: void init(const TCHAR* _pText) { uninit(); int iLen = _tcslen(_pText); m_pText = new TCHAR[iLen+1]; if( NULL == m_pText) { TCHAR _tcsError[64]; _stprintf(_tcsError,_T("Failed in const2buffer::new TCHAR[%d]\n"),iLen+1); throw _tcsError; } _tcscpy(m_pText,_pText); m_pText[iLen] = '\0'; } void uninit() { if( NULL != m_pText) { delete[] m_pText; m_pText = NULL; } } TCHAR* m_pText; }; class a2w { public: explicit a2w():buffer(0){} explicit a2w(const char* str) throw(const TCHAR*):buffer(0) { try { init(str); } catch(const TCHAR* pError){ throw pError; } } a2w(a2w& other) throw(const TCHAR*):buffer(0) { try { *this = other; } catch(const TCHAR* pError){ throw pError; } } void init(const char* str) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != str) { int nLen = ::MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0); buffer = new wchar_t[nLen+1]; if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in a2w -> init -> new wchar_t[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(wchar_t)); ::MultiByteToWideChar(CP_ACP,0,str,-1,buffer,nLen); buffer[nLen] = 0; } } ~a2w() { delete[] buffer; } a2w& operator=(a2w& other) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != other.buffer) { int nLen = wcslen(other.buffer); buffer = new wchar_t[nLen+1]; if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in a2w::init -> new wchar_t[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(wchar_t)); wcscpy(buffer,other.buffer); buffer[nLen] = 0; } return *this; } operator const wchar_t*() { return buffer; } private: wchar_t* buffer; }; class w2a { public: explicit w2a():buffer(0){} explicit w2a(const wchar_t* str) throw(const TCHAR*):buffer(0) { try { init(str); } catch(const TCHAR* pError){ throw pError; } } w2a(w2a& other) throw(const TCHAR*):buffer(0) { try { *this = other; } catch(const TCHAR* pError){ throw pError; } } void init(const wchar_t* str) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != str) { int nLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL); buffer = new char[nLen+1]; if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in w2a::init -> new char[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(char)); ::WideCharToMultiByte (CP_ACP, 0,str, -1,buffer , nLen, NULL,NULL); buffer[nLen] = 0; } } ~w2a() { delete[] buffer; } w2a& operator=(w2a& other) throw(const TCHAR*) { if( NULL != buffer) delete[] buffer; if(NULL != other.buffer) { int nLen = strlen(other.buffer); buffer = new char[nLen+1]; if( NULL == buffer) { buffer = NULL; TCHAR _tcsError[256]; _stprintf(_tcsError,_T("Failed in w2a::init -> new char[%d]\n"),nLen+1); throw _tcsError; } memset(buffer,0,(nLen+1)*sizeof(char)); strcpy(buffer,other.buffer); buffer[nLen] = 0; } return *this; } operator const char*() { return buffer; } private: char *buffer; }; }