Commit ff023282 authored by Mickaël Turnel's avatar Mickaël Turnel

Add a default encoding information for character conversions

By default android and iOS are set with UTF-8 and others with the system's locale
parent 06cd2b31
......@@ -23,6 +23,20 @@
extern "C" {
#endif
/**
* @brief Set the default encoding for the application.
*
* @param[in] encoding default encoding, "locale" to set it to the system's locale
*/
BCTBX_PUBLIC void bctbx_set_default_encoding (const char *encoding);
/**
* @brief Return the default encoding for the application.
*
* @return a pointer to a null-terminated string containing the default encoding.
*/
BCTBX_PUBLIC const char *bctbx_get_default_encoding ();
/**
* @brief Convert the given string from system locale to UTF8.
*
......
......@@ -32,6 +32,7 @@ set(BCTOOLBOX_C_SOURCE_FILES
set(BCTOOLBOX_CXX_SOURCE_FILES
containers/map.cc
conversion/charconv.cc
crypto/ecc.cc
utils/regex.cc
)
......
/*
bctoolbox
Copyright (C) 2016 Belledonne Communications SARL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
#include "bctoolbox/logging.h"
#include "bctoolbox/port.h"
#include "bctoolbox/charconv.h"
namespace {
std::string defaultEncoding = "";
}
void bctbx_set_default_encoding (const char *encoding) {
defaultEncoding = std::string(encoding);
}
const char *bctbx_get_default_encoding () {
if (!defaultEncoding.empty())
return defaultEncoding.c_str();
#if defined(__ANDROID__) || TARGET_OS_IPHONE
return "UTF-8";
#else
return "locale";
#endif
}
......@@ -24,17 +24,17 @@
#include "bctoolbox/port.h"
#include "bctoolbox/charconv.h"
extern "C" char *bctbx_locale_to_utf8 (const char *str) {
char *bctbx_locale_to_utf8 (const char *str) {
// TODO remove this part when the NDK will contain a usable iconv
return bctbx_strdup(str);
}
extern "C" char *bctbx_utf8_to_locale (const char *str) {
char *bctbx_utf8_to_locale (const char *str) {
// TODO remove this part when the NDK will contain a usable iconv
return bctbx_strdup(str);
}
extern "C" char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
if (!encoding)
return NULL;
......
......@@ -27,21 +27,61 @@
#import "bctoolbox/port.h"
#import "bctoolbox/charconv.h"
extern "C" char *bctbx_locale_to_utf8 (const char *str) {
NSString *string = [[NSString alloc] initWithCString:str encoding:[NSString defaultCStringEncoding]];
static NSStringEncoding getDefaultStringEncoding (const char *encoding) {
const char *defaultEncoding = encoding;
if (defaultEncoding == NULL)
defaultEncoding = bctbx_get_default_encoding();
if (!strcmp(defaultEncoding, "UTF-8"))
return [NSString NSUTF8StringEncoding];
if (!strcmp(defaultEncoding, "locale")) {
return [NSString defaultCStringEncoding];
} else {
NSString *encodingString = [[NSString alloc] initWithCString:defaultEncoding encoding:[NSString defaultCStringEncoding]];
CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) encodingString);
if (stringEncoding == kCFStringEncodingInvalidId) {
bctbx_error("Error invalid ID '%s': unknown charset", defaultEncoding);
return [NSString defaultCStringEncoding];
}
return CFStringConvertEncodingToNSStringEncoding(stringEncoding);
}
}
char *bctbx_locale_to_utf8 (const char *str) {
NSStringEncoding encoding = getDefaultStringEncoding(NULL);
if (encoding == NSUTF8StringEncoding)
return bctbx_strdup(str);
NSString *string = [[NSString alloc] initWithCString:str encoding:encoding];
return bctbx_strdup([string UTF8String]);
}
extern "C" char *bctbx_utf8_to_locale (const char *str) {
char *bctbx_utf8_to_locale (const char *str) {
NSStringEncoding encoding = getDefaultStringEncoding(NULL);
if (encoding == NSUTF8StringEncoding)
return bctbx_strdup(str);
NSString *string = [[NSString alloc] initWithUTF8String:str];
return bctbx_strdup([string cStringUsingEncoding:[NSString defaultCStringEncoding]]);
return bctbx_strdup([string cStringUsingEncoding:encoding]);
}
extern "C" char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
if (!encoding)
return NULL;
if (!strcmp(encoding, "UTF-8"))
return bctbx_strdup(str);
NSString *encodingString = [[NSString alloc] initWithCString:encoding encoding:[NSString defaultCStringEncoding]];
CFStringEncoding stringEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef) encodingString);
if (stringEncoding == kCFStringEncodingInvalidId) {
bctbx_error("Error while converting a string from '%s' to 'UTF-8': unknown charset", encoding);
......
......@@ -85,14 +85,24 @@ static char *convert_from_to (const char *str, const char *from, const char *to)
return ptr;
}
extern "C" char *bctbx_locale_to_utf8 (const char *str) {
return convert_from_to(str, "locale", "UTF-8");
char *bctbx_locale_to_utf8 (const char *str) {
const char *default_encoding = bctbx_get_default_encoding();
if (!strcmp(default_encoding, "UTF-8"))
return bctbx_strdup(str);
return convert_from_to(str, default_encoding, "UTF-8");
}
extern "C" char *bctbx_utf8_to_locale (const char *str) {
return convert_from_to(str, "UTF-8", "locale");
char *bctbx_utf8_to_locale (const char *str) {
const char *default_encoding = bctbx_get_default_encoding();
if (!strcmp(default_encoding, "UTF-8"))
return bctbx_strdup(str);
return convert_from_to(str, "UTF-8", default_encoding);
}
extern "C" char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
return convert_from_to(str, encoding, "UTF-8");
}
......@@ -113,7 +113,7 @@ static std::string stringToUpper (const std::string &str) {
return result;
}
static char *convert_from_to (const char *str, const char *from, const char *to) {
static char *convertFromTo (const char *str, const char *from, const char *to) {
if (!from || !to)
return NULL;
......@@ -157,14 +157,24 @@ static char *convert_from_to (const char *str, const char *from, const char *to)
return convertedStr;
}
extern "C" char *bctbx_locale_to_utf8 (const char *str) {
return convert_from_to(str, "locale", "UTF-8");
char *bctbx_locale_to_utf8 (const char *str) {
const char *defaultEncoding = bctbx_get_default_encoding();
if (!strcmp(defaultEncoding, "UTF-8"))
return bctbx_strdup(str);
return convertFromTo(str, defaultEncoding, "UTF-8");
}
extern "C" char *bctbx_utf8_to_locale (const char *str) {
return convert_from_to(str, "UTF-8", "locale");
char *bctbx_utf8_to_locale (const char *str) {
const char *defaultEncoding = bctbx_get_default_encoding();
if (!strcmp(defaultEncoding, "UTF-8"))
return bctbx_strdup(str);
return convertFromTo(str, "UTF-8", defaultEncoding);
}
extern "C" char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
return convert_from_to(str, encoding, "UTF-8");
char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
return convertFromTo(str, encoding, "UTF-8");
}
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