From 5327fe3e7066230cf69ee66179495dd42e067b30 Mon Sep 17 00:00:00 2001 From: lzwdgc Date: Fri, 18 Sep 2020 22:26:10 +0300 Subject: [PATCH] Move string conversion functions into common.cpp. --- src/common/buffer.cpp | 29 -------------------------- src/common/common.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 29 deletions(-) create mode 100644 src/common/common.cpp diff --git a/src/common/buffer.cpp b/src/common/buffer.cpp index c1140fb..536acd1 100644 --- a/src/common/buffer.cpp +++ b/src/common/buffer.cpp @@ -22,8 +22,6 @@ #include -#include - const int build_version = 0 //#include ; @@ -193,30 +191,3 @@ const std::vector &buffer::buf() const throw std::logic_error("buffer: not initialized"); return *buf_; } - -std::string str2utf8(const std::string &codepage_str, int cp) -{ - auto utf16_str = str2utf16(codepage_str, cp); - int utf8_size = WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), - utf16_str.length(), nullptr, 0, - nullptr, nullptr); - std::string utf8_str(utf8_size, '\0'); - WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), - utf16_str.length(), &utf8_str[0], utf8_size, - nullptr, nullptr); - return utf8_str; -} - -std::wstring str2utf16(const std::string &codepage_str, int cp) -{ - int size; - std::wstring utf16_str; - - size = MultiByteToWideChar(cp, MB_PRECOMPOSED, codepage_str.c_str(), - codepage_str.length(), nullptr, 0); - utf16_str = std::wstring(size, '\0'); - MultiByteToWideChar(cp, MB_PRECOMPOSED, codepage_str.c_str(), - codepage_str.length(), &utf16_str[0], size); - - return utf16_str; -} diff --git a/src/common/common.cpp b/src/common/common.cpp new file mode 100644 index 0000000..36aef04 --- /dev/null +++ b/src/common/common.cpp @@ -0,0 +1,48 @@ +/* + * AIM tools + * Copyright (C) 2015 lzwdgc + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "common.h" + +#include + +std::string str2utf8(const std::string &codepage_str, int cp) +{ + auto utf16_str = str2utf16(codepage_str, cp); + int utf8_size = WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), + utf16_str.length(), nullptr, 0, + nullptr, nullptr); + std::string utf8_str(utf8_size, '\0'); + WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), + utf16_str.length(), &utf8_str[0], utf8_size, + nullptr, nullptr); + return utf8_str; +} + +std::wstring str2utf16(const std::string &codepage_str, int cp) +{ + int size; + std::wstring utf16_str; + + size = MultiByteToWideChar(cp, MB_PRECOMPOSED, codepage_str.c_str(), + codepage_str.length(), nullptr, 0); + utf16_str = std::wstring(size, '\0'); + MultiByteToWideChar(cp, MB_PRECOMPOSED, codepage_str.c_str(), + codepage_str.length(), &utf16_str[0], size); + + return utf16_str; +}