From 8b22f580a20cbe2950d148fc5b556e891ef896fd Mon Sep 17 00:00:00 2001 From: martii Date: Sat, 9 Nov 2013 16:28:41 +0100 Subject: [PATCH] system/localize: change memory allocation, no user-visible changes Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/9f563d720953909ff97c6870bf22d8b1f67e14ae Author: martii Date: 2013-11-09 (Sat, 09 Nov 2013) ------------------ No further description and justification available within origin commit message! ------------------ This commit was generated by Migit --- src/system/localize.cpp | 86 ++++++++++++++++++++++------------------- src/system/localize.h | 3 ++ 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/src/system/localize.cpp b/src/system/localize.cpp index 220f0b3d4..61c67b8af 100644 --- a/src/system/localize.cpp +++ b/src/system/localize.cpp @@ -43,6 +43,9 @@ #include #include #include +#include +#include +#include //static const char * iso639filename = "/usr/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() { 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 *)]; - for (unsigned int i = 0; i < (sizeof(locale_real_names)/sizeof(const char *)); i++) - defaultData[i] = (char *)locale_real_names[i]; + memcpy(localeData, locale_real_names, sizeof(locale_real_names)); + memcpy(defaultData, locale_real_names, sizeof(locale_real_names)); + defaultDataMem = localeDataMem = NULL; loadLocale(DEFAULT_LOCALE, true); } 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; - - 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; + + if (localeDataMem) + ::free(localeDataMem); + if (defaultDataMem) + ::free(defaultDataMem); } const char * path[2] = { CONFIGDIR "/locale/", DATADIR "/neutrino/locale/"}; CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const locale, bool asdefault) { - unsigned int i; - FILE * fd; + FILE * fd = NULL; char ** loadData = asdefault ? defaultData : localeData; + char **mem = asdefault ? &defaultDataMem : &localeDataMem; + if(!asdefault && !strcmp(locale, DEFAULT_LOCALE)) { - for (unsigned j = 0; j < (sizeof(locale_real_names)/sizeof(const char *)); j++) { - if (loadData[j] != locale_real_names[j] && loadData[j] != defaultData[j]) - free(loadData[j]); - loadData[j] = defaultData[j]; + if (*mem) { + free(*mem); + *mem = NULL; } + memcpy(loadData, defaultData, sizeof(locale_real_names)); 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]; filename += locale; filename += ".locale"; + ::stat(filename.c_str(), &st); fd = fopen(filename.c_str(), "r"); if (fd) break; } - if (i == 2) + if (!fd) { perror("cannot read locale"); return NO_SUCH_LOCALE; } - if(!asdefault) { - for (unsigned j = 0; j < (sizeof(locale_real_names)/sizeof(const char *)); j++) { - if (loadData[j] != locale_real_names[j] && loadData[j] != defaultData[j]) - { - free(loadData[j]); - } - loadData[j] = (char *)locale_real_names[j]; - } + if (*mem) { + free (*mem); + *mem = NULL; } + 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; size_t len = 0; - i = 1; - while(!feof(fd)) { if(getline(&buf, &len, fd)!=-1) @@ -192,13 +195,19 @@ CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const l } } while ( ( pos != -1 ) ); + unsigned int i; for(i = 1; i < sizeof(locale_real_names)/sizeof(const char *); i++) { //printf("[%s] [%s]\n", buf,locale_real_names[i]); if(!strcmp(buf,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 printf("[%s.locale] dup entry: %s\n", locale, locale_real_names[i]); break; @@ -212,6 +221,9 @@ CLocaleManager::loadLocale_ret_t CLocaleManager::loadLocale(const char * const l fclose(fd); if(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++) 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]; } - return ( - (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; + return UNICODE_FONT; } const char * CLocaleManager::getText(const neutrino_locale_t keyName) const diff --git a/src/system/localize.h b/src/system/localize.h index ad8bedb81..b12b430fd 100644 --- a/src/system/localize.h +++ b/src/system/localize.h @@ -51,6 +51,9 @@ class CLocaleManager private: char * * localeData; char * * defaultData; + + char * localeDataMem; + char * defaultDataMem; public: enum loadLocale_ret_t