Browse Source

moving into cpp file

COM-121
Volker Birk 7 years ago
parent
commit
23bf1d77f5
9 changed files with 319 additions and 272 deletions
  1. +2
    -42
      CpEpEngine.cpp
  2. +1
    -1
      CpEpEngine.h
  3. +1
    -0
      TextMessage.cpp
  4. +1
    -0
      pEpCOMServerAdapter.vcxproj
  5. +3
    -0
      pEpCOMServerAdapter.vcxproj.filters
  6. +163
    -0
      pEp_identity_helper.cpp
  7. +32
    -172
      pEp_identity_helper.h
  8. +104
    -53
      utf8_helper.cpp
  9. +12
    -4
      utf8_helper.h

+ 2
- 42
CpEpEngine.cpp View File

@ -3,6 +3,8 @@
#include "stdafx.h"
#include "CpEpEngine.h"
using namespace std;
using namespace pEp::utility;
// CpEpEngine
@ -23,48 +25,6 @@ STDMETHODIMP CpEpEngine::InterfaceSupportsErrorInfo(REFIID riid)
#define FAIL(msg) error(msg)
using namespace std;
static CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist)
{
CComSafeArray<BSTR> sa_string_list;
int n = 0;
for (const ::stringlist_t *k = stringlist; k != NULL; k = k->next) {
if (k->value) {
HRESULT _result = sa_string_list.Add(utf16_bstr(k->value).Detach(), false);
assert(_result == S_OK);
if (_result == E_OUTOFMEMORY)
throw std::bad_alloc();
++n;
}
}
return sa_string_list;
}
static ::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
{
CComSafeArray<BSTR> sa(safearray);
int n_strings = 0;
::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
assert(_stringlist);
if (_stringlist == NULL)
throw std::bad_alloc();
n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
::stringlist_t *k = _stringlist;
for (int i = 0, j = sa.GetLowerBound(); i<n_strings; ++i, ++j) {
k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
assert(k);
if (k == NULL) {
::free_stringlist(_stringlist);
throw std::bad_alloc();
}
}
return _stringlist;
}
// CpEpEngine
STDMETHODIMP CpEpEngine::log(BSTR title, BSTR entity, BSTR description, BSTR comment)


+ 1
- 1
CpEpEngine.h View File

@ -18,7 +18,7 @@
using namespace ATL;
using namespace utility;
using namespace pEp::utility;
// CpEpEngine


+ 1
- 0
TextMessage.cpp View File

@ -3,6 +3,7 @@
#include "stdafx.h"
#include "TextMessage.h"
using namespace pEp::utility;
// CTextMessage


+ 1
- 0
pEpCOMServerAdapter.vcxproj View File

@ -123,6 +123,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
</PrecompiledHeader>
</ClCompile>
<ClCompile Include="pEp_identity_helper.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>


+ 3
- 0
pEpCOMServerAdapter.vcxproj.filters View File

@ -40,6 +40,9 @@
<ClCompile Include="TextMessage.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="pEp_identity_helper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="stdafx.h">


+ 163
- 0
pEp_identity_helper.cpp View File

@ -0,0 +1,163 @@
#include "stdafx.h"
#include "pEp_identity_helper.h"
namespace pEp {
namespace utility {
pEp_identity_cpp::pEp_identity_cpp(const ::pEp_identity *_ident)
: me(false)
{
if (_ident->address)
address = _ident->address;
if (_ident->fpr)
fpr = _ident->fpr;
if (_ident->user_id)
user_id = _ident->user_id;
if (_ident->username)
username = _ident->username;
comm_type = (pEp_comm_type) _ident->comm_type;
lang = _ident->lang;
}
pEp_identity_cpp::pEp_identity_cpp(const pEp_identity_s *_ident)
: me(false)
{
if (_ident->address)
address = utf8_string(_ident->address);
if (_ident->fpr)
fpr = utf8_string(_ident->fpr);
if (_ident->user_id)
user_id = utf8_string(_ident->user_id);
if (_ident->username)
username = utf8_string(_ident->username);
comm_type = _ident->comm_type;
if (_ident->lang)
lang = utf8_string(_ident->lang);
}
pEp_identity * pEp_identity_cpp::to_pEp_identity()
{
::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
assert(_ident);
if (_ident == NULL)
throw bad_alloc();
_ident->comm_type = (::PEP_comm_type) this->comm_type;
_ident->me = this->me;
assert(this->lang.size() == 0 || this->lang.size() == 2);
if (this->lang.size()) {
_ident->lang[0] = this->lang[0];
_ident->lang[1] = this->lang[1];
}
return _ident;
}
pEp_identity_s * pEp_identity_cpp::to_pEp_identity_s()
{
pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
assert(_ident);
if (_ident == NULL)
throw bad_alloc();
_ident->address = utf16_bstr(this->address).Detach();
_ident->comm_type = this->comm_type;
_ident->fpr = utf16_bstr(this->fpr).Detach();
_ident->lang = utf16_bstr(this->lang).Detach();
_ident->username = utf16_bstr(this->username).Detach();
_ident->user_id = utf16_bstr(this->user_id).Detach();
return _ident;
}
void free_pEp_identity_s(pEp_identity_s *ident)
{
if (ident) {
SysFreeString(ident->address);
SysFreeString(ident->fpr);
SysFreeString(ident->lang);
SysFreeString(ident->username);
SysFreeString(ident->user_id);
free(ident);
}
}
void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
{
assert(ident_s);
::memset(ident_s, 0, sizeof(pEp_identity_s));
if (ident) {
if (ident->address)
ident_s->address = utf16_bstr(ident->address).Detach();
if (ident->fpr)
ident_s->fpr = utf16_bstr(ident->fpr).Detach();
if (ident->user_id)
ident_s->user_id = utf16_bstr(ident->user_id).Detach();
if (ident->username)
ident_s->username = utf16_bstr(ident->username).Detach();
ident_s->comm_type = (pEp_comm_type) ident->comm_type;
if (ident->lang)
ident_s->lang = utf16_bstr(ident->lang).Detach();
}
}
::pEp_identity *new_identity(const pEp_identity_s * ident)
{
::pEp_identity *_ident;
string _address;
string _fpr;
string _user_id;
string _username;
if (ident->address)
_address = utf8_string(ident->address);
if (ident->fpr) {
_fpr = utf8_string(ident->fpr);
for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
if (*p >= 'A' && *p <= 'Z')
continue;
if (*p >= '0' && *p <= '9')
continue;
throw invalid_argument("invalid hex digits in fingerprint");
}
}
if (ident->user_id)
_user_id = utf8_string(ident->user_id);
if (ident->username)
_username = utf8_string(ident->username);
_ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
assert(_ident);
if (_ident == NULL)
throw bad_alloc();
_ident->comm_type = (PEP_comm_type) ident->comm_type;
if (ident->lang) {
string _lang = utf8_string(ident->lang);
if (_lang.length() != 0) {
if (_lang.length() != 2) {
::free_identity(_ident);
throw invalid_argument("invalid language code");
}
if (_lang[0] < 'a' || _lang[0] > 'z') {
::free_identity(_ident);
throw invalid_argument("invalid language code");
}
if (_lang[1] < 'a' || _lang[1] > 'z') {
::free_identity(_ident);
throw invalid_argument("invalid language code");
}
_ident->lang[0] = _lang[0];
_ident->lang[1] = _lang[1];
}
}
return _ident;
}
}
}

+ 32
- 172
pEp_identity_helper.h View File

@ -6,178 +6,38 @@
using namespace std;
struct pEp_identity_cpp {
string address;
string fpr;
string user_id;
string username;
pEp_comm_type comm_type;
string lang;
bool me;
namespace pEp {
namespace utility {
struct pEp_identity_cpp {
string address;
string fpr;
string user_id;
string username;
pEp_comm_type comm_type;
string lang;
bool me;
pEp_identity_cpp(
string _address = string(),
string _fpr = string(),
string _user_id = string(),
string _username = string(),
pEp_comm_type _comm_type = pEp_ct_unknown,
string _lang = string()
) : address(_address), fpr(_fpr), user_id(_user_id), username(_username), comm_type(_comm_type), lang(_lang), me(false)
{ }
pEp_identity_cpp(const ::pEp_identity *_ident);
pEp_identity_cpp(const pEp_identity_s *_ident);
pEp_identity * to_pEp_identity();
pEp_identity_s * to_pEp_identity_s();
};
void free_pEp_identity_s(pEp_identity_s *ident);
void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident);
::pEp_identity *new_identity(const pEp_identity_s * ident);
pEp_identity_cpp(
string _address = string(),
string _fpr = string(),
string _user_id = string(),
string _username = string(),
pEp_comm_type _comm_type = pEp_ct_unknown,
string _lang = string()
) : address(_address), fpr(_fpr), user_id(_user_id), username(_username), comm_type(_comm_type), lang(_lang), me(false)
{ }
pEp_identity_cpp(const ::pEp_identity *_ident)
: me(false)
{
if (_ident->address)
address = _ident->address;
if (_ident->fpr)
fpr = _ident->fpr;
if (_ident->user_id)
user_id = _ident->user_id;
if (_ident->username)
username = _ident->username;
comm_type = (pEp_comm_type) _ident->comm_type;
lang = _ident->lang;
}
pEp_identity_cpp(const pEp_identity_s *_ident)
: me(false)
{
if (_ident->address)
address = utf8_string(_ident->address);
if (_ident->fpr)
fpr = utf8_string(_ident->fpr);
if (_ident->user_id)
user_id = utf8_string(_ident->user_id);
if (_ident->username)
username = utf8_string(_ident->username);
comm_type = _ident->comm_type;
if (_ident->lang)
lang = utf8_string(_ident->lang);
}
pEp_identity * to_pEp_identity()
{
::pEp_identity *_ident = ::new_identity(this->address.c_str(), this->fpr.c_str(), this->user_id.c_str(), this->username.c_str());
assert(_ident);
if (_ident == NULL)
throw bad_alloc();
_ident->comm_type = (::PEP_comm_type) this->comm_type;
_ident->me = this->me;
assert(this->lang.size() == 0 || this->lang.size() == 2);
if (this->lang.size()) {
_ident->lang[0] = this->lang[0];
_ident->lang[1] = this->lang[1];
}
return _ident;
}
pEp_identity_s * to_pEp_identity_s()
{
pEp_identity_s *_ident = (pEp_identity_s *) calloc(1, sizeof(pEp_identity_s));
assert(_ident);
if (_ident == NULL)
throw bad_alloc();
_ident->address = utf16_bstr(this->address).Detach();
_ident->comm_type = this->comm_type;
_ident->fpr = utf16_bstr(this->fpr).Detach();
_ident->lang = utf16_bstr(this->lang).Detach();
_ident->username = utf16_bstr(this->username).Detach();
_ident->user_id = utf16_bstr(this->user_id).Detach();
return _ident;
}
};
static void free_pEp_identity_s(pEp_identity_s *ident)
{
if (ident) {
SysFreeString(ident->address);
SysFreeString(ident->fpr);
SysFreeString(ident->lang);
SysFreeString(ident->username);
SysFreeString(ident->user_id);
free(ident);
}
}
static void copy_identity(pEp_identity_s * ident_s, const pEp_identity * ident)
{
assert(ident_s);
::memset(ident_s, 0, sizeof(pEp_identity_s));
if (ident) {
if (ident->address)
ident_s->address = utf16_bstr(ident->address).Detach();
if (ident->fpr)
ident_s->fpr = utf16_bstr(ident->fpr).Detach();
if (ident->user_id)
ident_s->user_id = utf16_bstr(ident->user_id).Detach();
if (ident->username)
ident_s->username = utf16_bstr(ident->username).Detach();
ident_s->comm_type = (pEp_comm_type) ident->comm_type;
if (ident->lang)
ident_s->lang = utf16_bstr(ident->lang).Detach();
}
}
static ::pEp_identity *new_identity(const pEp_identity_s * ident)
{
::pEp_identity *_ident;
string _address;
string _fpr;
string _user_id;
string _username;
if (ident->address)
_address = utf8_string(ident->address);
if (ident->fpr) {
_fpr = utf8_string(ident->fpr);
for (auto p = _fpr.begin(); p != _fpr.end(); ++p) {
if (*p >= 'A' && *p <= 'Z')
continue;
if (*p >= '0' && *p <= '9')
continue;
throw invalid_argument("invalid hex digits in fingerprint");
}
}
if (ident->user_id)
_user_id = utf8_string(ident->user_id);
if (ident->username)
_username = utf8_string(ident->username);
_ident = ::new_identity(_address.c_str(), _fpr.c_str(), _user_id.c_str(), _username.c_str());
assert(_ident);
if (_ident == NULL)
throw bad_alloc();
_ident->comm_type = (PEP_comm_type) ident->comm_type;
if (ident->lang) {
string _lang = utf8_string(ident->lang);
if (_lang.length() != 0) {
if (_lang.length() != 2) {
::free_identity(_ident);
throw invalid_argument("invalid language code");
}
if (_lang[0] < 'a' || _lang[0] > 'z') {
::free_identity(_ident);
throw invalid_argument("invalid language code");
}
if (_lang[1] < 'a' || _lang[1] > 'z') {
::free_identity(_ident);
throw invalid_argument("invalid language code");
}
_ident->lang[0] = _lang[0];
_ident->lang[1] = _lang[1];
}
}
return _ident;
}

+ 104
- 53
utf8_helper.cpp View File

@ -1,68 +1,119 @@
#include "stdafx.h"
#include "utf8_helper.h"
using namespace ATL;
using namespace std;
string utf8_string(wstring wstr, NORM_FORM norm) {
if (wstr.size() == 0)
return string();
namespace pEp {
namespace utility {
wstring _wstr_normalized;
string utf8_string(wstring wstr, NORM_FORM norm)
{
if (wstr.size() == 0)
return string();
if (norm == NormalizationOther)
_wstr_normalized = wstr;
else {
int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
assert(size > 0);
if (size > 0) {
wchar_t *buf = new wchar_t[size];
NormalizeString(norm, wstr.c_str(), -1, buf, size);
_wstr_normalized = buf;
delete[] buf;
}
else
throw out_of_range("input wstring is not valid while normalizing.");
}
string result;
wstring _wstr_normalized;
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
assert(size);
if (size) {
char *buf = new char[size];
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
result = buf;
delete[] buf;
}
else
throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
if (norm == NormalizationOther)
_wstr_normalized = wstr;
else {
int size = NormalizeString(norm, wstr.c_str(), -1, NULL, 0);
assert(size > 0);
if (size > 0) {
wchar_t *buf = new wchar_t[size];
NormalizeString(norm, wstr.c_str(), -1, buf, size);
_wstr_normalized = buf;
delete[] buf;
}
else
throw out_of_range("input wstring is not valid while normalizing.");
}
string result;
return result;
}
int size = WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, NULL, 0, NULL, NULL);
assert(size);
if (size) {
char *buf = new char[size];
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, _wstr_normalized.c_str(), -1, buf, size, NULL, NULL);
result = buf;
delete[] buf;
}
else
throw out_of_range("input wstring is not valid while converting UTF-16 to UTF-8.");
string utf8_string(BSTR bstr, NORM_FORM norm) {
return utf8_string((wstring)(wchar_t *)_bstr_t(bstr, true), norm);
}
return result;
}
wstring utf16_string(string str) {
if (str.size() == 0)
return wstring();
string utf8_string(BSTR bstr, NORM_FORM norm)
{
return utf8_string((wstring) (wchar_t *) _bstr_t(bstr, true), norm);
}
wstring result;
wstring utf16_string(string str)
{
if (str.size() == 0)
return wstring();
int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
assert(size);
if (size) {
wchar_t * buf = new wchar_t[size];
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
result = buf;
delete[] buf;
}
else
throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
wstring result;
return result;
}
int size = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, NULL, 0);
assert(size);
if (size) {
wchar_t * buf = new wchar_t[size];
MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str.c_str(), -1, buf, size);
result = buf;
delete[] buf;
}
else
throw out_of_range("input string is not valid while converting UTF-8 to UTF-16.");
_bstr_t utf16_bstr(string str) {
return _bstr_t(utf16_string(str).c_str());
}
return result;
}
_bstr_t utf16_bstr(string str)
{
return _bstr_t(utf16_string(str).c_str());
}
CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist)
{
CComSafeArray<BSTR> sa_string_list;
int n = 0;
for (const ::stringlist_t *k = stringlist; k != NULL; k = k->next) {
if (k->value) {
HRESULT _result = sa_string_list.Add(utf16_bstr(k->value).Detach(), false);
assert(_result == S_OK);
if (_result == E_OUTOFMEMORY)
throw std::bad_alloc();
++n;
}
}
return sa_string_list;
}
::stringlist_t * new_stringlist(const SAFEARRAY * safearray)
{
CComSafeArray<BSTR> sa(safearray);
int n_strings = 0;
::stringlist_t *_stringlist = ::new_stringlist((const char *) NULL);
assert(_stringlist);
if (_stringlist == NULL)
throw std::bad_alloc();
n_strings = sa.GetUpperBound() - sa.GetLowerBound() + 1;
::stringlist_t *k = _stringlist;
for (int i = 0, j = sa.GetLowerBound(); i < n_strings; ++i, ++j) {
k = ::stringlist_add(k, utf8_string(sa.GetAt(j)).c_str());
assert(k);
if (k == NULL) {
::free_stringlist(_stringlist);
throw std::bad_alloc();
}
}
return _stringlist;
}
}
}

+ 12
- 4
utf8_helper.h View File

@ -1,6 +1,14 @@
#pragma once
std::string utf8_string(std::wstring wstr, NORM_FORM norm = NormalizationC);
std::string utf8_string(BSTR bstr, NORM_FORM norm = NormalizationC);
std::wstring utf16_string(std::string str);
_bstr_t utf16_bstr(std::string str);
namespace pEp {
namespace utility {
std::string utf8_string(std::wstring wstr, NORM_FORM norm = NormalizationC);
std::string utf8_string(BSTR bstr, NORM_FORM norm = NormalizationC);
std::wstring utf16_string(std::string str);
_bstr_t utf16_bstr(std::string str);
ATL::CComSafeArray<BSTR> string_array(const ::stringlist_t *stringlist);
::stringlist_t * new_stringlist(const SAFEARRAY * safearray);
}
}

Loading…
Cancel
Save