to std::string in locale and some other fixes
fluxgen fluxgen
2 files changed,
84 insertions(+),
89 deletions(-)
M
src/i18n.cc
→
src/i18n.cc
@@ -22,69 +22,76 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// 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 <X11/Xlocale.h> -#ifdef STDC_HEADERS -# include <stdlib.h> -# include <string.h> -# include <stdio.h> +#ifdef STDC_HEADERS +# include <stdlib.h> +# include <string.h> +# include <stdio.h> #endif // STDC_HEADERS -#ifdef HAVE_LOCALE_H -# include <locale.h> +#ifdef HAVE_LOCALE_H +# include <locale.h> #endif // HAVE_LOCALE_H +#include <iostream> +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\""<<endl; #endif // HAVE_SETLOCALE - locale = "C"; - mb = 0; -#ifdef HAVE_SETLOCALE - } else if (! strcmp(locale, "C") || ! strcmp(locale, "POSIX")) { - mb = 0; - } else { - mb = 1; - // truncate any encoding off the end of the locale - char *l = strchr(locale, '.'); - if (l) *l = '\0'; - } + m_locale = "C"; +#ifdef HAVE_SETLOCALE + } else { + // MB_CUR_MAX returns the size of a char in the current locale + if (MB_CUR_MAX > 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 @@ }
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); + + string catalog_filename = LOCALEPATH; + catalog_filename += '/'; + catalog_filename += m_locale; + catalog_filename += '/'; + catalog_filename += catalog; -# ifdef MCLoadBySet - catalog_fd = catopen(catalog_filename, MCLoadBySet); -# else // !MCLoadBySet - catalog_fd = catopen(catalog_filename, NL_CAT_LOCALE); -# endif // MCLoadBySet +#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 (catalog_fd == (nl_catd) -1) - fprintf(stderr, "failed to open catalog, using default messages\n"); + if (m_catalog_fd == (nl_catd)-1) + cerr<<"Warning: Failed to open catalog, using default messages."<<endl; #else // !HAVE_CATOPEN - catalog_fd = (nl_catd) -1; - catalog_filename = (char *) 0; + + m_catalog_fd = (nl_catd)-1; #endif // HAVE_CATOPEN } -const char *I18n::getMessage(int set, int msg, const char *s) { -#if defined(NLS) && defined(HAVE_CATGETS) - if (catalog_fd != (nl_catd) -1) - return (const char *) catgets(catalog_fd, set, msg, s); - else +const char *I18n::getMessage(int set_number, int message_number, const char *default_message) { + +#if defined(NLS) && defined(HAVE_CATGETS) + if (m_catalog_fd != (nl_catd)-1) + return (const char *) catgets(m_catalog_fd, set_number, message_number, default_message); + else #endif - return s; + return default_message; }
M
src/i18n.hh
→
src/i18n.hh
@@ -22,14 +22,12 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// $Id: i18n.hh,v 1.6 2002/03/18 15:46:42 fluxgen Exp $ +// $Id: i18n.hh,v 1.7 2002/04/04 00:21:48 fluxgen Exp $ #ifndef I18N_HH #define I18N_HH -#ifdef NLS -# include "../nls/blackbox-nls.hh" -#endif // NLS +#include "../nls/blackbox-nls.hh" #ifdef HAVE_LOCALE_H # include <locale.h>@@ -46,34 +44,26 @@ #ifdef __CYGWIN32__
# include "nl_types_cygnus.h" #endif -class I18n { -private: - char *locale, *catalog_filename; - int mb; - nl_catd catalog_fd; - +#include <string> -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