? typescript Index: common/shared.c =================================================================== RCS file: /home/freeciv/CVS/freeciv/common/shared.c,v retrieving revision 1.56 diff -u -r1.56 shared.c --- shared.c 2000/08/14 12:42:05 1.56 +++ shared.c 2000/08/15 03:36:09 @@ -26,6 +26,12 @@ #ifdef HAVE_PWD_H #include #endif +#ifdef ENABLE_NLS +#include /* For CHAR_MAX. */ +#ifdef HAVE_NL_LANGINFO +#include +#endif +#endif #include "astring.h" #include "fcintl.h" @@ -46,6 +52,12 @@ "~/.freeciv" #endif +#ifdef ENABLE_NLS +static char grouping[32]; /* _Not_ a string... */ +static char *thousands_sep; /* ...but this is... */ +static char *negative_sign; /* ...as is this. */ +#endif + /************************************************************************** FIXME: This is an inelegant, English-specific kludge that fails to work correctly even in the limited case of English, e.g. "An hour". @@ -164,31 +176,17 @@ /*************************************************************** Returns a statically allocated string containing a nicely-formatted version of the given number. - FIXME: This is not internationalised at all. ***************************************************************/ -#define SPLITS 3 -#define N_DIGS 20 char *int_to_text(int nr) { - char tmpbuf[N_DIGS+1]; - static char buf[N_DIGS+((N_DIGS-1)/SPLITS)+1]; - char *to=buf; - char *from=tmpbuf; - my_snprintf(tmpbuf, sizeof(tmpbuf), "%d", nr); - while (*from) { - *to++ = *from++; - if (strlen(from)%SPLITS==0 && *from) - *to++=','; - } - *to=0; - return buf; -#if 0 - /* FIXME: Some code which may help when i18n'd using localeconv(). */ - int neg; - static char buf[1 + (2 * N_DIGS - 1) + 1]; + static char buf[64]; /* Note that we'll be filling this in right to + left. */ char *ptr; - int inx; - + int neg, i; +#ifdef ENABLE_NLS + char *grp = grouping; +#endif + if (nr == 0) { return "0"; } @@ -198,25 +196,45 @@ } ptr = &(buf[sizeof(buf) - 1]); - *ptr-- = '\0'; + *ptr = '\0'; - inx = 0; + i = 0; while (nr != 0) { int dig = nr % 10; - *ptr-- = '0' + dig; + *(--ptr) = '0' + dig; nr /= 10; - inx++; - if ((inx % SPLITS == 0) && (nr != 0)) { - *ptr-- = ','; + i++; + + if (nr != 0 +#ifndef ENABLE_NLS + && i == 3 +#else + && i == *grp +#endif + ) { + i = 0; +#ifndef ENABLE_NLS + *(--ptr) = ','; +#else + ptr -= strlen(thousands_sep); + memcpy(ptr, thousands_sep, strlen(thousands_sep)); + grp++; +#endif } } if (neg) { - *ptr-- = '-'; +#ifndef ENABLE_NLS + *(--ptr) = '-'; +#else + ptr -= strlen(negative_sign); + memcpy (ptr, negative_sign, strlen(negative_sign)); +#endif } - return ptr + 1; -#endif + assert(ptr >= buf); + + return ptr; } /*************************************************************** @@ -695,9 +713,55 @@ void init_nls(void) { #ifdef ENABLE_NLS + char *s_grouping; + int i; + setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain(PACKAGE); + +#ifndef HAVE_NL_LANGINFO + { + struct lconv *lc = localeconv(); + + thousands_sep = mystrdup(lc->thousands_sep); + s_grouping = lc->grouping; + negative_sign = lc->negative_sign; + } +#else + thousands_sep = mystrdup(nl_langinfo(THOUSANDS_SEP)); + s_grouping = nl_langinfo(GROUPING); + negative_sign = nl_langinfo(NEGATIVE_SIGN); +#endif + + if (*negative_sign) { + negative_sign = mystrdup(negative_sign); + } else { + negative_sign = "-"; + } + + if (s_grouping[0] == 0) { + /* So we don't read grouping[-1] below. */ + grouping[0] = CHAR_MAX; + return; + } + + for (i = 0; i < sizeof (grouping) - 1; i++) { + grouping[i] = s_grouping[i]; + if (grouping[i] == CHAR_MAX) + break; + if (grouping[i] == 0) { + /* Zero means to repeat the present number indefinitely. */ + char g = grouping[i - 1]; + + for ( ; i < sizeof (grouping) - 1; i++) { + grouping[i] = g; + } + break; + } + } + + grouping[sizeof(grouping) - 1] = CHAR_MAX; /* Just in case. */ #endif }