mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-01-19 12:08:11 +00:00
fix(utils): avoid memory leak in utf8 conversions
This commit is contained in:
parent
03c581bee7
commit
af99144376
2 changed files with 17 additions and 6 deletions
|
|
@ -99,6 +99,10 @@ namespace Utils {
|
|||
// Return a buffer allocated with new.
|
||||
LINPHONE_PUBLIC char *utf8ToChar (uint32_t ic);
|
||||
|
||||
LINPHONE_PUBLIC inline std::string cStringToCppString (const char *str) {
|
||||
return str ? str : "";
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
LINPHONE_PUBLIC const T &getEmptyConstRefObject () {
|
||||
static const T object;
|
||||
|
|
|
|||
|
|
@ -187,19 +187,26 @@ long Utils::getTmAsTimeT (const tm &time) {
|
|||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// TODO: Improve perf!!! Avoid c <--> cpp string conversions.
|
||||
string Utils::localeToUtf8 (const string &str) {
|
||||
char *ret = bctbx_locale_to_utf8(str.c_str());
|
||||
return string(ret != NULL ? ret : "");
|
||||
char *cStr = bctbx_locale_to_utf8(str.c_str());
|
||||
string utf8Str = cStringToCppString(cStr);
|
||||
bctbx_free(cStr);
|
||||
return utf8Str;
|
||||
}
|
||||
|
||||
string Utils::utf8ToLocale (const string &str) {
|
||||
char *ret = bctbx_utf8_to_locale(str.c_str());
|
||||
return string(ret != NULL ? ret : "");
|
||||
char *cStr = bctbx_utf8_to_locale(str.c_str());
|
||||
string localeStr = cStringToCppString(cStr);
|
||||
bctbx_free(cStr);
|
||||
return localeStr;
|
||||
}
|
||||
|
||||
string Utils::convertString (const string &str, const string &from, const string &to) {
|
||||
char *ret = bctbx_convert_from_to(str.c_str(), from.c_str(), to.c_str());
|
||||
return string(ret != NULL ? ret : "");
|
||||
char *cStr = bctbx_convert_from_to(str.c_str(), from.c_str(), to.c_str());
|
||||
string convertedStr = cStringToCppString(cStr);
|
||||
bctbx_free(cStr);
|
||||
return convertedStr;
|
||||
}
|
||||
|
||||
LINPHONE_END_NAMESPACE
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue