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