Commit d3e747b3 authored by Ronan's avatar Ronan

Merge branch 'feature/default_encoding' into 'master'

Re added the default encoding value to allow platform not to use the system's locale

See merge request !1
parents 7a8fb9c0 6c3a2d61
Pipeline #412 passed with stage
in 0 seconds
/*
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 Affero General Public License as
published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* charconv.h
* Copyright (C) 2010-2018 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef BCTBX_CHARCONV_H
......@@ -25,10 +26,24 @@
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.
*
* @param[in] str string to convert
* @param[in] str string to convert
*
* @return a pointer to a null-terminated string containing the converted string. This buffer must then be freed
* by caller. NULL on failure.
......@@ -38,7 +53,7 @@ BCTBX_PUBLIC char *bctbx_locale_to_utf8 (const char *str);
/**
* @brief Convert the given string from UTF8 to system locale.
*
* @param[in] str string to convert
* @param[in] str string to convert
*
* @return a pointer to a null-terminated string containing the converted string. This buffer must then be freed
* by caller. NULL on failure.
......@@ -48,8 +63,8 @@ BCTBX_PUBLIC char *bctbx_utf8_to_locale (const char *str);
/**
* @brief Convert the given string.
*
* @param[in] str string to convert
* @param[in] encoding charset of the string
* @param[in] str string to convert
* @param[in] encoding charset of the string
*
* @return a pointer to a null-terminated string containing the converted string. This buffer must then be freed
* by caller. NULL on failure.
......
......@@ -31,6 +31,7 @@ set(BCTOOLBOX_C_SOURCE_FILES
set(BCTOOLBOX_CXX_SOURCE_FILES
containers/map.cc
conversion/charconv_encoding.cc
crypto/ecc.cc
utils/regex.cc
utils/exception.cc
......@@ -75,6 +76,7 @@ if(ENABLE_STATIC)
add_library(bctoolbox-static STATIC ${BCTOOLBOX_SOURCE_FILES} ${BCTOOLBOX_HEADER_FILES})
target_link_libraries(bctoolbox-static INTERFACE ${CMAKE_THREAD_LIBS_INIT})
if(APPLE)
target_link_libraries(bctoolbox-static INTERFACE "-framework Foundation" "-framework AVFoundation")
target_include_directories(bctoolbox-static PRIVATE ${ICONV_INCLUDE_DIRS})
target_link_libraries(bctoolbox-static INTERFACE ${ICONV_LIBRARIES})
endif()
......@@ -117,6 +119,7 @@ if(ENABLE_SHARED)
)
endif()
if(APPLE)
target_link_libraries(bctoolbox PRIVATE "-framework Foundation" "-framework AVFoundation")
target_include_directories(bctoolbox PRIVATE ${ICONV_INCLUDE_DIRS})
target_link_libraries(bctoolbox PRIVATE ${ICONV_LIBRARIES})
endif()
......
/*
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/>.
*/
* charconv.cc
* Copyright (C) 2010-2018 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
......@@ -26,9 +27,9 @@
#include <locale.h>
#include <string.h>
#include "bctoolbox/charconv.h"
#include "bctoolbox/logging.h"
#include "bctoolbox/port.h"
#include "bctoolbox/charconv.h"
static char *convert_from_to (const char *str, const char *from, const char *to) {
if (!from || !to)
......@@ -43,15 +44,10 @@ static char *convert_from_to (const char *str, const char *from, const char *to)
size_t out_left = in_left + in_left/10; // leave a marge of 10%
iconv_t cd;
#ifdef __APPLE__
const char* r_from = strcasecmp("locale", from) == 0 ? "" : from;
const char* r_to = strcasecmp("locale", to) == 0 ? "" : to;
#else
setlocale(LC_CTYPE, "");
const char* r_from = strcasecmp("locale", from) == 0 ? nl_langinfo(CODESET) : from;
const char* r_to = strcasecmp("locale", to) == 0 ? nl_langinfo(CODESET) : to;
#endif
if (strcasecmp(r_from, r_to) == 0) {
return bctbx_strdup(str);
......@@ -80,22 +76,34 @@ static char *convert_from_to (const char *str, const char *from, const char *to)
if (ret == (size_t)-1 && errno != E2BIG) {
bctbx_error("Error while converting a string from '%s' to '%s': %s", from, to, strerror(errno));
bctbx_free(ptr);
return NULL;
return bctbx_strdup(str);
}
} else {
bctbx_error("Unable to open iconv content descriptor from '%s' to '%s': %s", from, to, strerror(errno));
return NULL;
return bctbx_strdup(str);
}
return ptr;
}
char *bctbx_locale_to_utf8 (const char *str) {
return convert_from_to(str, "locale", "UTF-8");
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");
}
char *bctbx_utf8_to_locale (const char *str) {
return convert_from_to(str, "UTF-8", "locale");
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);
}
char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
......
/*
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/>.
*/
* charconv_android.cc
* Copyright (C) 2010-2018 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "bctoolbox/charconv.h"
#include "bctoolbox/logging.h"
#include "bctoolbox/port.h"
#include "bctoolbox/charconv.h"
char *bctbx_locale_to_utf8 (const char *str) {
// TODO remove this part when the NDK will contain a usable iconv
......
/*
* charconv_encoding.cc
* Copyright (C) 2010-2018 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __APPLE__
#include "TargetConditionals.h"
#endif
#include "bctoolbox/charconv.h"
#include "bctoolbox/logging.h"
#include "bctoolbox/port.h"
namespace {
std::string defaultEncoding = "";
}
void bctbx_set_default_encoding (const char *encoding) {
defaultEncoding = 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
}
/*
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/>.
*/
* charconv_windows.cc
* Copyright (C) 2010-2018 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
......@@ -23,9 +24,9 @@
#include <algorithm>
#include <unordered_map>
#include "bctoolbox/charconv.h"
#include "bctoolbox/logging.h"
#include "bctoolbox/port.h"
#include "bctoolbox/charconv.h"
static std::unordered_map <std::string, UINT> windowsCharset {
{ "LOCALE", CP_ACP },
......@@ -158,11 +159,21 @@ static char *convertFromTo (const char *str, const char *from, const char *to) {
}
char *bctbx_locale_to_utf8 (const char *str) {
return convertFromTo(str, "locale", "UTF-8");
const char *defaultEncoding = bctbx_get_default_encoding();
if (!strcmp(defaultEncoding, "UTF-8"))
return bctbx_strdup(str);
return convertFromTo(str, defaultEncoding, "UTF-8");
}
char *bctbx_utf8_to_locale (const char *str) {
return convertFromTo(str, "UTF-8", "locale");
const char *defaultEncoding = bctbx_get_default_encoding();
if (!strcmp(defaultEncoding, "UTF-8"))
return bctbx_strdup(str);
return convertFromTo(str, "UTF-8", defaultEncoding);
}
char *bctbx_convert_any_to_utf8 (const char *str, const char *encoding) {
......
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