Revision: 8638 https://osdn.net/projects/ttssh2/scm/svn/commits/8638 Author: zmatsuo Date: 2020-03-28 00:47:22 +0900 (Sat, 28 Mar 2020) Log Message: ----------- codeconvにwcクラスを追加 - wchar_t 文字列を保持するクラス Modified Paths: -------------- branches/unicode_macro_2/teraterm/common/codeconv.cpp branches/unicode_macro_2/teraterm/common/codeconv.h -------------- next part -------------- Modified: branches/unicode_macro_2/teraterm/common/codeconv.cpp =================================================================== --- branches/unicode_macro_2/teraterm/common/codeconv.cpp 2020-03-27 15:47:12 UTC (rev 8637) +++ branches/unicode_macro_2/teraterm/common/codeconv.cpp 2020-03-27 15:47:22 UTC (rev 8638) @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018-2019 TeraTerm Project + * Copyright (C) 2018-2020 TeraTerm Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -1269,3 +1269,139 @@ obj.tstr_ = NULL; } } + +////////////////////////////////////////////////////////////////////////////// + +wc::wc() +{ + tstr_ = NULL; +} + +wc::wc(const char *strA) +{ + tstr_ = NULL; + assign(strA, CP_ACP); +} + +wc::wc(const char *strA, int code_page) +{ + tstr_ = NULL; + assign(strA, code_page); +} + +wc::wc(const wchar_t *strW) +{ + tstr_ = NULL; + assign(strW); +} + +wc::wc(const wc &obj) +{ + tstr_ = NULL; + copy(obj); +} + +#if defined(MOVE_CONSTRUCTOR_ENABLE) +wc::wc(wc &&obj) noexcept +{ + tstr_ = NULL; + move(obj); +} +#endif + + +wc::~wc() +{ + if (tstr_ != NULL) { + free(tstr_); + } +} + +wc& wc::operator=(const char *strA) +{ + assign(strA, CP_ACP); + return *this; +} + +wc& wc::operator=(const wchar_t *strW) +{ + assign(strW); + return *this; +} + +wc &wc::operator=(const wc &obj) +{ + copy(obj); + return *this; +} + +#if defined(MOVE_CONSTRUCTOR_ENABLE) +wc& wc::operator=(wc &&obj) noexcept +{ + move(obj); + return *this; +} +#endif + +wc wc::fromUtf8(const char *strU8) +{ + wchar_t *strW = _MultiByteToWideChar(strU8, 0, CP_UTF8, NULL); + wc _wc = strW; + free(strW); + return _wc; +} + +// void\x82Ȃ\xB5\x82\xAA\x88\xEA\x94ʓI\x82Ǝv\x82\xED\x82\xEA\x82邪\x81A +// VS2005\x82Ń\x8A\x83\x93\x83N\x83G\x83\x89\x81[\x82\xAA\x8Fo\x82Ă\xB5\x82܂\xA4\x82\xBD\x82\xDF void \x92lj\xC1 +wc::operator const wchar_t *(void) const +{ + return cstr(); +} + +const wchar_t *wc::cstr() const +{ + if (tstr_ == NULL) { + return L""; + } + return tstr_; +} + +void wc::assign(const char *strA, int code_page) +{ + if (tstr_ != NULL) { + free(tstr_); + } + wchar_t *strW = _MultiByteToWideChar(strA, 0, code_page, NULL); + if (strW != NULL) { + tstr_ = strW; + } else { + tstr_ = NULL; + } +} + +void wc::assign(const wchar_t *strW) +{ + if (tstr_ != NULL) { + free(tstr_); + } + tstr_ = _wcsdup(strW); +} + +void wc::copy(const wc &obj) +{ + if (tstr_ != NULL) { + free(tstr_); + } + tstr_ = _wcsdup(obj.tstr_); +} + +void wc::move(wc &obj) +{ + if (this != &obj) { + if (tstr_ != NULL) { + free(tstr_); + } + tstr_ = obj.tstr_; + obj.tstr_ = NULL; + } +} Modified: branches/unicode_macro_2/teraterm/common/codeconv.h =================================================================== --- branches/unicode_macro_2/teraterm/common/codeconv.h 2020-03-27 15:47:12 UTC (rev 8637) +++ branches/unicode_macro_2/teraterm/common/codeconv.h 2020-03-27 15:47:22 UTC (rev 8638) @@ -150,4 +150,35 @@ void copy(const tc &obj); void move(tc &obj); }; + +class wc +{ +public: + wc(); + wc(const char *strA); + wc(const char *strA, int code_page); + wc(const wchar_t *strW); + wc(const wc &obj); +#if defined(MOVE_CONSTRUCTOR_ENABLE) + wc(wc &&obj) noexcept; #endif + ~wc(); + wc& operator=(const char *strA); + wc& operator=(const wchar_t *strW); + wc& operator=(const wc &obj); +#if defined(MOVE_CONSTRUCTOR_ENABLE) + wc& operator=(wc &&obj) noexcept; +#endif + static wc fromUtf8(const char *strU8); + operator const wchar_t *() const; + const wchar_t *cstr() const; + operator const char *() const; +// const char *cstr() const; +private: + wchar_t *tstr_; + void assign(const char *strA, int code_page); + void assign(const wchar_t *strW); + void copy(const wc &obj); + void move(wc &obj); +}; +#endif