⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 convert.h

📁 本人收集整理的一份c/c++跨平台网络库
💻 H
字号:
#ifndef UTILS_BASE_CONVERT_H_#define UTILS_BASE_CONVERT_H_#ifdef WIN32#include <string>#include <windows.h>#include <malloc.h>#ifndef NO_ATL#include <atlstr.h>#endif // NO_ATL#include "basictypes.h"class Utf8 {public:#ifndef NO_ATL	explicit Utf8(const CString & str) {    *this = str;  }#else	explicit Utf8(const wchar_t *str) {	*this = str;	}#endif  explicit Utf8() {}#ifndef NO_ATL  inline Utf8& operator =(const CString & str) {    // TODO: deal with errors    int len8 = WideCharToMultiByte(CP_UTF8, 0, str.GetString(), str.GetLength(),                                   NULL, 0, NULL, NULL);    char * ns = static_cast<char*>(_alloca(len8));    WideCharToMultiByte(CP_UTF8, 0, str.GetString(), str.GetLength(),                        ns, len8, NULL, NULL);    str_.assign(ns, len8);    return *this;  }#elseinline Utf8& operator =(const wchar_t *str) {    // TODO: deal with errors    int len8 = WideCharToMultiByte(CP_UTF8, 0, str, (int)wcslen(str),                                   NULL, 0, NULL, NULL);    char * ns = static_cast<char*>(_alloca(len8));    WideCharToMultiByte(CP_UTF8, 0, str, (int)wcslen(str),                        ns, len8, NULL, NULL);    str_.assign(ns, len8);    return *this;  }#endif // NO_ATL  inline operator const std::string & () const {    return str_;  }  inline const char * AsSz() const {    return str_.c_str();  }  // Deprecated  inline const std::string & AsString() const {    return str_;  }  // Deprecated  inline int Len8() const {    return (int)str_.length();  }private:  DISALLOW_EVIL_CONSTRUCTORS(Utf8);  std::string str_;};class Utf16 {public:  explicit Utf16(const std::string & str) {    // TODO: deal with errors    int len16 = MultiByteToWideChar(CP_UTF8, 0, str.data(), -1,                                    NULL, 0);#ifndef NO_ATL    wchar_t * ws = cstr_.GetBuffer(len16);	MultiByteToWideChar(CP_UTF8, 0, str.data(), str.length(), ws, len16);    cstr_.ReleaseBuffer(len16);#else	str_ = new wchar_t[len16];	MultiByteToWideChar(CP_UTF8, 0, str.data(), -1, str_, len16);#endif  }#ifndef NO_ATL  inline operator const CString & () const {    return cstr_;  }    // Deprecated  inline const CString & AsCString() const {    return cstr_;  }  // Deprecated  inline int Len16() const {    return cstr_.GetLength();  }  inline const wchar_t * AsWz() const {    return cstr_.GetString();  }#else  ~Utf16() {    delete[] str_;  }  inline const wchar_t * AsWz() const {    return str_;  }#endifprivate:  DISALLOW_EVIL_CONSTRUCTORS(Utf16);#ifndef NO_ATL  CString cstr_;#else  wchar_t *str_;#endif};#endif //WIN32#endif  // UTILS_BASE_CONVERT_H_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -