Commit 253378d1 authored by Mickaël Turnel's avatar Mickaël Turnel

Add conversion mechanism in Content so it is always UTF-8

parent e3736438
......@@ -107,6 +107,10 @@ namespace Utils {
LINPHONE_PUBLIC std::tm getTimeTAsTm (time_t time);
LINPHONE_PUBLIC time_t getTmAsTimeT (const std::tm &time);
LINPHONE_PUBLIC std::string localeToUtf8 (const std::string &str);
LINPHONE_PUBLIC std::string utf8ToLocale (const std::string &str);
LINPHONE_PUBLIC std::string convertString (const std::string &str, const std::string &from, const std::string &to);
}
LINPHONE_END_NAMESPACE
......
......@@ -19,6 +19,7 @@
// TODO: Remove me later.
#include "linphone/core.h"
#include "linphone/utils/utils.h"
#include "content-p.h"
#include "content-type.h"
......@@ -117,6 +118,11 @@ const vector<char> &Content::getBody () const {
}
string Content::getBodyAsString () const {
L_D();
return Utils::utf8ToLocale(string(d->body.begin(), d->body.end()));
}
string Content::getBodyAsUtf8String () const {
L_D();
return string(d->body.begin(), d->body.end());
}
......@@ -133,7 +139,8 @@ void Content::setBody (vector<char> &&body) {
void Content::setBody (const string &body) {
L_D();
d->body = vector<char>(body.cbegin(), body.cend());
string toUtf8 = Utils::localeToUtf8(body);
d->body = vector<char>(toUtf8.cbegin(), toUtf8.cend());
}
void Content::setBody (const void *buffer, size_t size) {
......@@ -142,6 +149,11 @@ void Content::setBody (const void *buffer, size_t size) {
d->body = vector<char>(start, start + size);
}
void Content::setBodyFromUtf8 (const string &body) {
L_D();
d->body = vector<char>(body.cbegin(), body.cend());
}
size_t Content::getSize () const {
L_D();
return d->body.size();
......
......@@ -55,11 +55,13 @@ public:
const std::vector<char> &getBody () const;
std::string getBodyAsString () const;
std::string getBodyAsUtf8String () const;
void setBody (const std::vector<char> &body);
void setBody (std::vector<char> &&body);
void setBody (const std::string &body);
void setBody (const void *buffer, size_t size);
void setBodyFromUtf8 (const std::string &body);
size_t getSize () const;
......
......@@ -22,6 +22,7 @@
#include <sstream>
#include <bctoolbox/port.h>
#include <bctoolbox/charconv.h>
#include "linphone/utils/utils.h"
......@@ -184,4 +185,21 @@ long Utils::getTmAsTimeT (const tm &time) {
return timegm(&const_cast<tm &>(time));
}
// -----------------------------------------------------------------------------
string Utils::localeToUtf8 (const string &str) {
char *ret = bctbx_locale_to_utf8(str.c_str());
return string(ret != NULL ? ret : "");
}
string Utils::utf8ToLocale (const string &str) {
char *ret = bctbx_utf8_to_locale(str.c_str());
return string(ret != NULL ? ret : "");
}
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 : "");
}
LINPHONE_END_NAMESPACE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment