system/localize: change memory allocation, no user-visible changes

Origin commit data
------------------
Commit: 9f563d7209
Author: martii <m4rtii@gmx.de>
Date: 2013-11-09 (Sat, 09 Nov 2013)
This commit is contained in:
martii
2013-11-09 16:28:41 +01:00
committed by vanhofen
parent 24eaf93c32
commit 29f0d5f0e7
2 changed files with 49 additions and 40 deletions

View File

@@ -43,6 +43,9 @@
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
//static const char * iso639filename = "/usr/share/iso-codes/iso-639.tab"; //static const char * iso639filename = "/usr/share/iso-codes/iso-639.tab";
static const char * iso639filename = "/share/iso-codes/iso-639.tab"; static const char * iso639filename = "/share/iso-codes/iso-639.tab";
@@ -86,80 +89,80 @@ const char * getISO639Description(const char * const iso)
CLocaleManager::CLocaleManager() CLocaleManager::CLocaleManager()
{ {
localeData = new char * [sizeof(locale_real_names)/sizeof(const char *)]; localeData = new char * [sizeof(locale_real_names)/sizeof(const char *)];
for (unsigned int i = 0; i < (sizeof(locale_real_names)/sizeof(const char *)); i++)
localeData[i] = (char *)locale_real_names[i];
defaultData = new char * [sizeof(locale_real_names)/sizeof(const char *)]; defaultData = new char * [sizeof(locale_real_names)/sizeof(const char *)];
for (unsigned int i = 0; i < (sizeof(locale_real_names)/sizeof(const char *)); i++) memcpy(localeData, locale_real_names, sizeof(locale_real_names));
defaultData[i] = (char *)locale_real_names[i]; memcpy(defaultData, locale_real_names, sizeof(locale_real_names));
defaultDataMem = localeDataMem = NULL;
loadLocale(DEFAULT_LOCALE, true); loadLocale(DEFAULT_LOCALE, true);
} }
CLocaleManager::~CLocaleManager() CLocaleManager::~CLocaleManager()
{ {
for (unsigned j = 0; j < (sizeof(locale_real_names)/sizeof(const char *)); j++)
if (localeData[j] != locale_real_names[j] && localeData[j] != defaultData[j])
::free(localeData[j]);
delete[] localeData; delete[] localeData;
for (unsigned j = 0; j < (sizeof(locale_real_names)/sizeof(const char *)); j++)
if (defaultData[j] != locale_real_names[j])
::free(defaultData[j]);
delete[] defaultData; delete[] defaultData;
if (localeDataMem)
::free(localeDataMem);
if (defaultDataMem)
::free(defaultDataMem);
} }
const char * path[2] = { CONFIGDIR "/locale/", DATADIR "/neutrino/locale/"}; const char * path[2] = { CONFIGDIR "/locale/", DATADIR "/neutrino/locale/"};
CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const locale, bool asdefault) CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const locale, bool asdefault)
{ {
unsigned int i; FILE * fd = NULL;
FILE * fd;
char ** loadData = asdefault ? defaultData : localeData; char ** loadData = asdefault ? defaultData : localeData;
char **mem = asdefault ? &defaultDataMem : &localeDataMem;
if(!asdefault && !strcmp(locale, DEFAULT_LOCALE)) { if(!asdefault && !strcmp(locale, DEFAULT_LOCALE)) {
for (unsigned j = 0; j < (sizeof(locale_real_names)/sizeof(const char *)); j++) { if (*mem) {
if (loadData[j] != locale_real_names[j] && loadData[j] != defaultData[j]) free(*mem);
free(loadData[j]); *mem = NULL;
loadData[j] = defaultData[j];
} }
memcpy(loadData, defaultData, sizeof(locale_real_names));
return UNICODE_FONT; return UNICODE_FONT;
} }
for (i = 0; i < 2; i++) struct stat st;
for (unsigned int i = 0; i < 2; i++)
{ {
std::string filename = path[i]; std::string filename = path[i];
filename += locale; filename += locale;
filename += ".locale"; filename += ".locale";
::stat(filename.c_str(), &st);
fd = fopen(filename.c_str(), "r"); fd = fopen(filename.c_str(), "r");
if (fd) if (fd)
break; break;
} }
if (i == 2) if (!fd)
{ {
perror("cannot read locale"); perror("cannot read locale");
return NO_SUCH_LOCALE; return NO_SUCH_LOCALE;
} }
if(!asdefault) { if (*mem) {
for (unsigned j = 0; j < (sizeof(locale_real_names)/sizeof(const char *)); j++) { free (*mem);
if (loadData[j] != locale_real_names[j] && loadData[j] != defaultData[j]) *mem = NULL;
{
free(loadData[j]);
}
loadData[j] = (char *)locale_real_names[j];
}
} }
memcpy(loadData, locale_real_names, sizeof(locale_real_names));
*mem = (char *) malloc(st.st_size);
if (!*mem)
{
perror("loadLocale");
return NO_SUCH_LOCALE;
}
char *memp = *mem;
char *buf=NULL; char *buf=NULL;
size_t len = 0; size_t len = 0;
i = 1;
while(!feof(fd)) while(!feof(fd))
{ {
if(getline(&buf, &len, fd)!=-1) if(getline(&buf, &len, fd)!=-1)
@@ -192,13 +195,19 @@ CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const l
} }
} while ( ( pos != -1 ) ); } while ( ( pos != -1 ) );
unsigned int i;
for(i = 1; i < sizeof(locale_real_names)/sizeof(const char *); i++) for(i = 1; i < sizeof(locale_real_names)/sizeof(const char *); i++)
{ {
//printf("[%s] [%s]\n", buf,locale_real_names[i]); //printf("[%s] [%s]\n", buf,locale_real_names[i]);
if(!strcmp(buf,locale_real_names[i])) if(!strcmp(buf,locale_real_names[i]))
{ {
if(loadData[i] == locale_real_names[i]) if(loadData[i] == locale_real_names[i])
loadData[i] = strdup(text.c_str()); {
loadData[i] = memp;
size_t l = text.length() + 1;
memcpy(memp, text.c_str(), l);
memp += l;
}
else else
printf("[%s.locale] dup entry: %s\n", locale, locale_real_names[i]); printf("[%s.locale] dup entry: %s\n", locale, locale_real_names[i]);
break; break;
@@ -212,6 +221,9 @@ CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const l
fclose(fd); fclose(fd);
if(buf) if(buf)
free(buf); free(buf);
char *_mem = (char *) realloc(*mem, memp - *mem);
if (_mem) // I see no reason for realloc to fail here, but anyways ...
*mem = _mem;
for (unsigned j = 1; j < (sizeof(locale_real_names)/sizeof(const char *)); j++) for (unsigned j = 1; j < (sizeof(locale_real_names)/sizeof(const char *)); j++)
if (loadData[j] == locale_real_names[j]) if (loadData[j] == locale_real_names[j])
@@ -221,13 +233,7 @@ CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const l
loadData[j] = defaultData[j]; loadData[j] = defaultData[j];
} }
return ( return UNICODE_FONT;
(strcmp(locale, "bosanski") == 0) ||
(strcmp(locale, "ellinika") == 0) ||
(strcmp(locale, "russkij") == 0) ||
(strcmp(locale, "utf8") == 0)
/* utf8.locale is a generic name that can be used for new locales which need characters outside the ISO-8859-1 character set */
) ? UNICODE_FONT : ISO_8859_1_FONT;
} }
const char * CLocaleManager::getText(const neutrino_locale_t keyName) const const char * CLocaleManager::getText(const neutrino_locale_t keyName) const

View File

@@ -52,6 +52,9 @@ class CLocaleManager
char * * localeData; char * * localeData;
char * * defaultData; char * * defaultData;
char * localeDataMem;
char * defaultDataMem;
public: public:
enum loadLocale_ret_t enum loadLocale_ret_t
{ {