COMMON: Encoding refactoring

This commit is contained in:
Jaromir Wysoglad 2019-08-19 18:22:50 +02:00 committed by Filippos Karapetis
parent 6dba0bbfd4
commit f8ac40af7c
2 changed files with 6 additions and 5 deletions

View file

@ -24,7 +24,7 @@
#include "common/textconsole.h" #include "common/textconsole.h"
#include "common/system.h" #include "common/system.h"
#include "common/translation.h" #include "common/translation.h"
#include <cerrno> #include <errno.h>
namespace Common { namespace Common {
@ -246,22 +246,22 @@ char *Encoding::convertTransManMapping(const char *to, const char *from, const c
String currentCharset = TransMan.getCurrentCharset(); String currentCharset = TransMan.getCurrentCharset();
if (currentCharset.equalsIgnoreCase(from)) { if (currentCharset.equalsIgnoreCase(from)) {
// We can use the transMan mapping directly // We can use the transMan mapping directly
uint32 *partialResult = (uint32 *) calloc(sizeof(uint32), (strlen(string) + 1)); uint32 *partialResult = (uint32 *) calloc(sizeof(uint32), (length + 1));
if (!partialResult) { if (!partialResult) {
warning("Couldn't allocate memory for encoding conversion"); warning("Couldn't allocate memory for encoding conversion");
return nullptr; return nullptr;
} }
const uint32 *mapping = TransMan.getCharsetMapping(); const uint32 *mapping = TransMan.getCharsetMapping();
if (mapping == 0) { if (mapping == 0) {
for(unsigned i = 0; i < strlen(string); i++) { for(unsigned i = 0; i < length; i++) {
partialResult[i] = string[i]; partialResult[i] = string[i];
} }
} else { } else {
for(unsigned i = 0; i < strlen(string); i++) { for(unsigned i = 0; i < length; i++) {
partialResult[i] = mapping[(unsigned char) string[i]] & 0x7FFFFFFF; partialResult[i] = mapping[(unsigned char) string[i]] & 0x7FFFFFFF;
} }
} }
char *finalResult = convert(to, "UTF-32", (char *) partialResult, strlen(string) * 4); char *finalResult = convert(to, "UTF-32", (char *) partialResult, length * 4);
free(partialResult); free(partialResult);
return finalResult; return finalResult;
} else if (currentCharset.equalsIgnoreCase(to) && String(from).hasPrefixIgnoreCase("utf-32")) { } else if (currentCharset.equalsIgnoreCase(to) && String(from).hasPrefixIgnoreCase("utf-32")) {

View file

@ -22,6 +22,7 @@
#ifndef COMMON_ENCODING_H #ifndef COMMON_ENCODING_H
#define COMMON_ENCODING_H #define COMMON_ENCODING_H
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
#include "config.h" #include "config.h"
#endif // HAVE_CONFIG_H #endif // HAVE_CONFIG_H