utf8_helper.cpp
author Volker Birk <vb@pep-project.org>
Thu, 16 Apr 2015 20:43:31 +0200
changeset 0 0aa9866d751c
child 10 15fd256d4adb
permissions -rw-r--r--
initial commit
     1 #include "stdafx.h"
     2 #include "utf8_helper.h"
     3 
     4 using namespace std;
     5 
     6 string utf8_string(wstring wstr, NORM_FORM norm) {
     7 	if (wstr.size() == 0)
     8 		return string();
     9 
    10 	wstring _wstr_normalized;
    11 
    12 	if (norm == NormalizationOther)
    13 		_wstr_normalized = wstr;
    14 	else {
    15 		int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
    16 		assert(size > 0);
    17 		if (size > 0) {
    18 			wchar_t *buf = new wchar_t[size];
    19 			NormalizeString(norm, wstr.c_str(), -1, buf, size);
    20 			_wstr_normalized = buf;
    21 			delete[] buf;
    22 		}
    23 		else
    24 			throw out_of_range("input wstring is not valid while normalizing.");
    25 	}
    26 	string result;
    27 
    28 	int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
    29 	assert(size);
    30 	if (size) {
    31 		char *buf = new char[size];
    32 		WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
    33 		result = buf;
    34 		delete[] buf;
    35 	}
    36 	else
    37 		throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
    38 
    39 	return result;
    40 }
    41 
    42 string utf8_string(BSTR bstr, NORM_FORM norm) {
    43 	return utf8_string((wstring)(wchar_t *)_bstr_t(bstr, true), norm);
    44 }
    45 
    46 wstring utf16_string(string str) {
    47 	if (str.size() == 0)
    48 		return wstring();
    49 
    50 	wstring result;
    51 
    52 	int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
    53 	assert(size);
    54 	if (size) {
    55 		wchar_t * buf = new wchar_t[size];
    56 		MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
    57 		result = buf;
    58 		delete[] buf;
    59 	}
    60 	else
    61 		throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
    62 
    63 	return result;
    64 }
    65 
    66 _bstr_t utf16_bstr(string str) {
    67 	return _bstr_t(utf16_string(str).c_str());
    68 }