From 16b40d64c22a2fe0d0cf178f473d07146dddc81b Mon Sep 17 00:00:00 2001 From: fluxgen Date: Thu, 4 Apr 2002 00:21:48 +0000 Subject: to std::string in locale and some other fixes --- src/i18n.cc | 133 +++++++++++++++++++++++++++++++----------------------------- src/i18n.hh | 44 ++++++++------------ 2 files changed, 86 insertions(+), 91 deletions(-) diff --git a/src/i18n.cc b/src/i18n.cc index 96641eb..85c170e 100644 --- a/src/i18n.cc +++ b/src/i18n.cc @@ -22,69 +22,76 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: i18n.cc,v 1.2 2002/01/09 14:11:20 fluxgen Exp $ +// $Id: i18n.cc,v 1.3 2002/04/04 00:21:48 fluxgen Exp $ -// stupid macros needed to access some functions in version 2 of the GNU C -// library -#ifndef _GNU_SOURCE -#define _GNU_SOURCE +//usr GNU extensions +#ifndef _GNU_SOURCE +#define _GNU_SOURCE #endif // _GNU_SOURCE -#ifdef HAVE_CONFIG_H -# include "../config.h" +#ifdef HAVE_CONFIG_H +#include "../config.h" #endif // HAVE_CONFIG_H #include "i18n.hh" #include -#ifdef STDC_HEADERS -# include -# include -# include +#ifdef STDC_HEADERS +# include +# include +# include #endif // STDC_HEADERS -#ifdef HAVE_LOCALE_H -# include +#ifdef HAVE_LOCALE_H +# include #endif // HAVE_LOCALE_H +#include +using std::cerr; +using std::endl; +using std::string; void NLSInit(const char *catalog) { - I18n *i18n = I18n::instance(); - i18n->openCatalog(catalog); + I18n *i18n = I18n::instance(); + i18n->openCatalog(catalog); } -I18n::I18n(void) { -#ifdef HAVE_SETLOCALE - locale = setlocale(LC_ALL, ""); - if (! locale) { - fprintf(stderr, "failed to set locale, reverting to \"C\"\n"); +I18n::I18n():m_multibyte(false), m_catalog_fd(0) { +#ifdef HAVE_SETLOCALE + //make sure we don't get 0 to m_locale string + char *temp = setlocale(LC_ALL, ""); + m_locale = ( temp ? temp : ""); + if (m_locale.size() == 0) { + cerr<<"Warning: Failed to set locale, reverting to \"C\""< 1) + m_multibyte = true; + + // truncate any encoding off the end of the locale + + string::size_type index = m_locale.find('@'); + if (index != string::npos) + m_locale.erase(index); //erase all characters starting at index + + index = m_locale.find('.'); + if (index != string::npos) + m_locale.erase(index); //erase all characters starting at index + } #endif // HAVE_SETLOCALE - - catalog_filename = (char *) 0; - catalog_fd = (nl_catd) -1; } -I18n::~I18n(void) { - delete catalog_filename; +I18n::~I18n() { #if defined(NLS) && defined(HAVE_CATCLOSE) - if (catalog_fd != (nl_catd) -1) - catclose(catalog_fd); + if (m_catalog_fd != (nl_catd)-1) + catclose(m_catalog_fd); #endif // HAVE_CATCLOSE } @@ -95,37 +102,35 @@ I18n *I18n::instance() { void I18n::openCatalog(const char *catalog) { #if defined(NLS) && defined(HAVE_CATOPEN) - int lp = strlen(LOCALEPATH), lc = strlen(locale), - ct = strlen(catalog), len = lp + lc + ct + 3; - catalog_filename = new char[len]; - - strncpy(catalog_filename, LOCALEPATH, lp); - *(catalog_filename + lp) = '/'; - strncpy(catalog_filename + lp + 1, locale, lc); - *(catalog_filename + lp + lc + 1) = '/'; - strncpy(catalog_filename + lp + lc + 2, catalog, ct + 1); - -# ifdef MCLoadBySet - catalog_fd = catopen(catalog_filename, MCLoadBySet); -# else // !MCLoadBySet - catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE); -# endif // MCLoadBySet - - if (catalog_fd == (nl_catd) -1) - fprintf(stderr, "failed to open catalog, using default messages\n"); + + string catalog_filename = LOCALEPATH; + catalog_filename += '/'; + catalog_filename += m_locale; + catalog_filename += '/'; + catalog_filename += catalog; + +#ifdef MCLoadBySet + m_catalog_fd = catopen(catalog_filename.c_str(), MCLoadBySet); +#else // !MCLoadBySet + m_catalog_fd = catopen(catalog_filename.c_str(), NL_CAT_LOCALE); +#endif // MCLoadBySet + + if (m_catalog_fd == (nl_catd)-1) + cerr<<"Warning: Failed to open catalog, using default messages."< @@ -46,34 +44,26 @@ extern "C" { # include "nl_types_cygnus.h" #endif -class I18n { -private: - char *locale, *catalog_filename; - int mb; - nl_catd catalog_fd; +#include - -protected: - I18n(void); - +class I18n { public: - //so old compilators dont complain - ~I18n(void); - static I18n *instance(); - inline const char *getLocale(void) const { return locale; } - inline const char *getCatalogFilename(void) const { return catalog_filename; } - - inline const int multibyte(void) const { return mb; } + inline const char *getLocale(void) const { return m_locale.c_str(); } + inline bool multibyte(void) const { return m_multibyte; } + inline const nl_catd &getCatalogFd(void) const { return m_catalog_fd; } - inline const nl_catd &getCatalogFd(void) const { return catalog_fd; } - - const char *getMessage(int, int, const char * = 0); + const char *getMessage(int set_number, int message_number, const char *default_messsage = 0); void openCatalog(const char *); -}; +private: + I18n(); + ~I18n(); + std::string m_locale; + bool m_multibyte; + nl_catd m_catalog_fd; +}; -//extern I18n *i18n; -extern void NLSInit(const char *); +void NLSInit(const char *); -#endif // __i18n_h +#endif // I18N_HH -- cgit v0.11.2