C++字符转换

最近在使用c++开发,系统使用的TChar字符,有时候要用到char字符,经常需要把Tchar和char进行互相转换。

 

#pragma once

#include "shlwapi.h"
#include "atlstr.h"

class C_StringConvertor
{
public:
    C_StringConvertor();
    virtual ~C_StringConvertor();

    void Initialize();

    void SetMultiByte(LPCSTR lpszMultiByte);
    void SetWideChars(LPCWSTR lpszWideChars);
    void SetUnicodeString(LPCTSTR lpszString);

    LPCSTR GetMultiByte();
    LPCWSTR GetWideChars();
    LPCTSTR GetUnicodeString();

protected:
    LPSTR m_lpMultiByte;
    LPWSTR m_lpWideChars;

protected:
    void ToMultiByte();
    void ToWideChars();
};

inline C_StringConvertor::C_StringConvertor()
{
    m_lpMultiByte = NULL;
    m_lpWideChars = NULL;
}

inline C_StringConvertor::~C_StringConvertor()
{
    Initialize();
}

inline void C_StringConvertor::Initialize()
{
    if (m_lpMultiByte)
    {
        ::LocalFree((HLOCAL)(m_lpMultiByte));
        m_lpMultiByte = NULL;
    }

    if (m_lpWideChars)
    {
        ::LocalFree((HLOCAL)(m_lpWideChars));
        m_lpWideChars = NULL;
    }
}

inline void C_StringConvertor::SetMultiByte(LPCSTR lpszMultiByte)
{
    if (m_lpMultiByte)
    {
        ::LocalFree((HLOCAL)(m_lpMultiByte));
        m_lpMultiByte = NULL;
    }

    if (lpszMultiByte)
    {
        m_lpMultiByte = ::StrDupA(lpszMultiByte);
        ToWideChars();
    }
}

inline void C_StringConvertor::SetWideChars(LPCWSTR lpszWideChars)
{
    if (m_lpWideChars)
    {
        ::LocalFree((HLOCAL)(m_lpWideChars));
        m_lpWideChars = NULL;
    }

    if (lpszWideChars)
    {
        m_lpWideChars = ::StrDupW(lpszWideChars);
        ToMultiByte();
    }
}

inline void C_StringConvertor::SetUnicodeString(LPCTSTR lpszString)
{
#ifdef UNICODE
    SetWideChars(lpszString);
#else
    SetMultiByte(lpszString);
#endif
}

inline LPCSTR C_StringConvertor::GetMultiByte()
{
    return m_lpMultiByte;
}

inline LPCWSTR C_StringConvertor::GetWideChars()
{
    return m_lpWideChars;
}

inline LPCTSTR C_StringConvertor::GetUnicodeString()
{
#ifdef UNICODE
    return GetWideChars();
#else
    return GetMultiByte();
#endif
}

inline void C_StringConvertor::ToMultiByte()
{
    int nLength = 0;
    int nBufferSize = 0;

    if (m_lpMultiByte)
    {
        ::LocalFree((HLOCAL)(m_lpMultiByte));
        m_lpMultiByte = NULL;
    }

    if (m_lpWideChars != NULL)
    {
        nLength = ::WideCharToMultiByte(CP_ACP, NULL, m_lpWideChars, -1, NULL, 0, NULL, FALSE);
        nBufferSize = ((nLength + 1) * sizeof(CHAR));
        m_lpMultiByte = (LPSTR)(::LocalAlloc(LPTR, nBufferSize));

        if (m_lpMultiByte)
        {
            ::WideCharToMultiByte (CP_ACP, NULL, m_lpWideChars, -1, m_lpMultiByte, nLength, NULL, FALSE);
        }
    }
}

inline void C_StringConvertor::ToWideChars()
{
    int nLength = 0;
    int nBufferSize = 0;

    if (m_lpWideChars)
    {
        ::LocalFree((HLOCAL)(m_lpWideChars));
        m_lpWideChars = NULL;
    }

    if (m_lpMultiByte != NULL)
    {
        nLength = ::MultiByteToWideChar(CP_ACP,0, m_lpMultiByte, -1, NULL,0);
        nBufferSize = ((nLength + 1) * sizeof(WCHAR));
        m_lpWideChars = (LPWSTR)(::LocalAlloc(LPTR, nBufferSize));

        if (m_lpWideChars)
        {
            ::MultiByteToWideChar(CP_ACP, 0, m_lpMultiByte, -1, m_lpWideChars, nLength);
        }
    }
}

inline BOOL CheckDirectoryString(LPCTSTR lpszString)
{
    TCHAR chDriverName = 0;
    CString strDirectory = _T("");

    strDirectory = lpszString;
    strDirectory.Trim();

    if (strDirectory.IsEmpty())
    {
        return FALSE;
    }

    strDirectory.MakeLower();
    chDriverName = strDirectory.GetAt(0);

    if ((chDriverName < _T('a')) || chDriverName > (_T('z')))
    {
        return FALSE;
    }

    if (strDirectory.GetLength() == 2)
    {
        if (strDirectory.GetAt(1) != _T(':'))
        {
            return FALSE;
        }

        return TRUE;
    }

    if (strDirectory.Find(_T(":\\")) != 1)
    {
        return FALSE;
    }

    if (strDirectory.Find(_T("\\\\")) >= 0)
    {
        return FALSE;
    }

    CString strCheck = _T("/*\"<>|?");
    if (strDirectory.FindOneOf(strCheck) >= 0)
    {
        return FALSE;
    }

    return TRUE;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值