?? strcast.h
字號:
#ifndef __DNC_STRCAST_H__
#define __DNC_STRCAST_H__
//##############################################################################
//The dnc Library
//Copyright (c) 2005 Dreamsoft 趙純華
//定義字符串轉換器,把你所要的東西轉換成字符串
//##############################################################################
//
//Last update: 2005-11-9
#ifndef __DNC_DEFINE_H__
#include "define.h"
#endif
#ifndef __DNC_UTF8_H__
#include "utf8.h"
#endif
#include <wchar.h>
#ifdef __GNUC__
#define utf8(s) dnc::string_cast<dnc::uchar>(s).str()
#else
#define utf8(s) dnc::string_cast<dnc::uchar>(L##s).str()
#endif
namespace dnc{
//------------------------------------------------------------------------------
//相同類型的轉換
//------------------------------------------------------------------------------
#ifdef DNC_OLDVC
template<class TDescType>
class string_cast{
enum{t_nBufferLength = 128};
#else
template<class TDescType,int t_nBufferLength = 128 >
class string_cast{
#endif
public:
typedef TDescType SrcType;
typedef TDescType DescType;
public:
string_cast( SrcType* psz ):
m_psz( psz )
{
m_length = -1;
}
~string_cast() throw(){
}
inline operator DescType* () const throw(){
return const_cast<DescType*>(m_psz);
}
inline DescType* c_str() const throw(){
return const_cast<DescType*>(m_psz);
}
inline std::basic_string<TDescType> str() const{
return std::basic_string<TDescType>(m_psz,size());
}
inline size_t size() const{
if(m_length == -1){
m_length = getLength(TDescType());
}
return m_length;
}
public:
size_t m_length;
DescType* m_psz;
private:
inline size_t getLength(char) const{
return strlen(m_psz);
}
inline size_t getLength(wchar_t) const{
return wcslen(m_psz);
}
string_cast( const string_cast& ) throw();
string_cast& operator=( const string_cast& ) throw();
};
//------------------------------------------------------------------------------
//to wchar_t*
//------------------------------------------------------------------------------
#ifdef DNC_OLDVC
template<>
class string_cast<wchar>{
enum{t_nBufferLength = 128};
#else
template<int t_nBufferLength>
class string_cast<wchar,t_nBufferLength>{
#endif
public:
typedef char SrcType;
typedef wchar_t DescType;
public:
//char* to wchar_t*
explicit string_cast(castr psz,size_t count = -1) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(psz,count == -1 ? strlen(psz) : count);
}
explicit string_cast(astr psz,size_t count = -1) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(psz,count == -1 ? strlen(psz) : count);
}
//uchar* to wchar_t*
explicit string_cast(custr psz,size_t count = -1) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(psz,count == -1 ? strlen((char*)psz) : count);
}
explicit string_cast(ustr psz,size_t count = -1) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(psz,count == -1 ? strlen((char*)psz) : count);
}
explicit string_cast(const std::string &sz,size_t count = -1) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init((custr)sz.c_str(),count == -1 ? sz.size() : count);
}
explicit string_cast(const dnc::utf8_const_iterator &it ,size_t count = -1) :
m_psz(m_szBuffer),
m_isSameType(false)
{
init((custr)(const char*)it,count == -1 ? strlen(it) : count);
}
//wchar_t* to wchar_t*
explicit string_cast(const wchar_t* psz ) :
m_psz((DescType*)psz),
m_isSameType(true),
m_length(-1)
{
}
explicit string_cast(wchar_t* psz ) :
m_psz((DescType*)psz),
m_isSameType(true),
m_length(-1)
{
}
////int to wchar_t*
//explicit string_cast(NullType val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(4)
//{
// wcscpy(m_psz,L"null");
//}
//explicit string_cast(bool val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(char val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,L"%i",val);
//}
//explicit string_cast(unsigned char val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(short val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(unsigned short val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(long val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(unsigned long val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(int val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(unsigned int val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(long long val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
//explicit string_cast(unsigned long long val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%i",val);
//}
////double to wchar_t*
//explicit string_cast(double val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%f",val);
//}
////float to wchar_t*
//explicit string_cast(float val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// sprintf(m_psz,t_nBufferLength,L"%f",val);
//}
////Variant to wchar_t*
//explicit string_cast(const Variant &val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// const dlc_variant& v=(const dlc_variant&)val;
// switch(getVariantTypeId(v)){
// case vt_string:
// init(v.strval);
// break;
// default:
// //std::string str;
// //getVariant(val,str);
// //m_length = str.size();
// //size_t len = m_length+1;
// //if(len > t_nBufferLength){
// // m_psz = new wchar_t[len];
// //}
// //wcscpy(m_psz,str.c_str());
// assert("not implementation" && 0);
// }
//}
//template<class TSrc>
//explicit string_cast(const TSrc &val) :
// m_psz( m_szBuffer ),
// m_isSameType(false),
// m_length(-1)
//{
// StringT<DescType> str;
// val.toString(str);
// if(str.size() >= t_nBufferLength){
// m_psz = new DescType[str.size()+1];
// }
// m_length = str.size();
// memcpy(m_psz,str.c_str(),m_length+1);
//}
~string_cast() throw(){
if( m_psz != m_szBuffer && !m_isSameType){
delete [] m_psz;
}
}
inline operator DescType* () const throw(){
return const_cast<DescType*>(m_psz);
}
inline DescType* c_str() const throw(){
return const_cast<DescType*>(m_psz);
}
inline size_t size() const{
if(m_length == -1){
const_cast<string_cast*>(this)->m_length = (size_t)wcslen(m_psz);
}
return m_length;
}
private:
size_t m_length;
DescType* m_psz;
bool m_isSameType;
DescType m_szBuffer[t_nBufferLength];
private:
void init(custr psz,size_t count) {
if (psz == NULL){
m_psz = NULL;
return;
}
if( count >= t_nBufferLength ){
m_psz = new wchar_t[count+1];
}
m_length = UTF8ToUNICODE(psz,count,m_psz,count);
m_psz[m_length] = 0;
}
void init(castr psz,size_t count) {
if (psz == NULL){
m_psz = NULL;
return;
}
if( count >= t_nBufferLength ){
m_psz = new wchar_t[count+1];
}
m_length = ANSIToUNICODE(psz,count,m_psz,count);
m_psz[m_length] = 0;
}
explicit string_cast( const string_cast& ) throw();
string_cast& operator=( const string_cast& ) throw();
};
//------------------------------------------------------------------------------
//to char*
//------------------------------------------------------------------------------
#ifdef DNC_OLDVC
template<>
class string_cast<achar>{
enum{t_nBufferLength = 128};
#else
template<int t_nBufferLength>
class string_cast<achar,t_nBufferLength>{
#endif
public:
typedef wchar SrcType;
typedef achar DescType;
public:
//wchar_t* to char*
explicit string_cast(cwstr psz,size_t count = -1) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(psz,count == -1 ? wcslen(psz) : count);
}
explicit string_cast(wstr psz,size_t count = -1) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(psz,count == -1 ? wcslen(psz) : count);
}
template<class Char,class Traits,class Allocator>
explicit string_cast(const std::basic_string<Char,Traits,Allocator> &str,size_t count) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(str.data(),count);
}
template<class Char,class Traits,class Allocator>
explicit string_cast(const std::basic_string<Char,Traits,Allocator> &str) :
m_psz( m_szBuffer ),
m_isSameType(false)
{
init(str.data(),str.size());
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -