Ignore:
Timestamp:
Nov 1, 2014, 3:02:00 AM (11 years ago)
Author:
Alex Taylor
Message:

Add fallback logic for locale resolution in Sys2FormatNumber, should work on
most systems now (if not ideally).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rxutilex/trunk/rxutilex.c

    r21 r22  
    6060#define INCL_RXFUNC
    6161#include <rexxsaa.h>
     62#include <nl_types.h>
     63#include <langinfo.h>
    6264
    6365#pragma import( DosGetPrty, "DosGetPrty", "DOSCALL1", 9 )
     
    7375#define SZ_LIBRARY_NAME         "RXUTILEX"  // Name of this library
    7476#define SZ_ERROR_NAME           "SYS2ERR"   // REXX variable used to store error codes
    75 #define SZ_VERSION              "0.1.0"     // Current version of this library
     77#define SZ_VERSION              "0.1.1"     // Current version of this library
    7678
    7779// Maximum string lengths...
     
    923925{
    924926    CHAR  achNumber[ US_NUMSTR_MAXZ ];  // Formatted output string
    925     PSZ    pszTZ,                       // Pointer to TZ environment var
    926            pszSetTZ;
     927    PSZ   pszSep = NULL;
    927928    float fVal;
    928929    int   iVal;
     
    932933    if ( argc < 1  || ( !RXVALIDSTRING(argv[0]) )) return ( 40 );
    933934
    934 
    935     /* These next 4 lines really shouldn't be necessary, but without them
    936      * getenv() and (apparently) tzset() may see the value of TZ as NULL
    937      * if the environment variable was changed in the REXX script.
    938      */
    939     DosScanEnv("TZ", &pszTZ );
    940     pszSetTZ = (PSZ) malloc( strlen( pszTZ ) + 5 );
    941     if ( pszSetTZ ) {
    942         sprintf( pszSetTZ, "TZ=%s", pszTZ );
    943         putenv( pszSetTZ );
    944     }
    945 
    946     // Use the locale and timezone settings from the environment
    947     tzset();
    948     setlocale( LC_ALL, "");
     935    // Use the locale settings from the environment
     936    pszSep = nl_langinfo( THOUSEP );
     937    if ( !pszSep || !strlen(pszSep) ) {
     938        /* If the current locale isn't known to the C runtime, use a common
     939         * known locale for the same language, if possible.
     940         */
     941        PSZ pszLang, p;
     942        if (( DosScanEnv("LANG", &pszLang ) == NO_ERROR ) &&
     943            pszLang && ( strlen( pszLang ) >= 2 ))
     944        {
     945            p = strdup( pszLang );
     946            if ( !strnicmp( p, "en_us", 2 )) setlocale( LC_NUMERIC, "EN_US");
     947            else if ( !strnicmp( p, "en_uk", 2 )) setlocale( LC_NUMERIC, "EN_GB");
     948            else if ( !strnicmp( p, "de", 2 )) setlocale( LC_NUMERIC, "DE_DE");
     949            else if ( !strnicmp( p, "es", 2 )) setlocale( LC_NUMERIC, "ES_ES");
     950            else if ( !strnicmp( p, "fr", 2 )) setlocale( LC_NUMERIC, "FR_FR");
     951            else if ( !strnicmp( p, "it", 2 )) setlocale( LC_NUMERIC, "IT_IT");
     952            else if ( !strnicmp( p, "ja", 2 )) setlocale( LC_NUMERIC, "JA_JP");
     953/*
     954            else if ( !strnicmp( p, "ar", 2 )) setlocale( LC_NUMERIC, "ar_AA");
     955            else if ( !strnicmp( p, "be", 2 )) setlocale( LC_NUMERIC, "be_BY");
     956            else if ( !strnicmp( p, "bg", 2 )) setlocale( LC_NUMERIC, "bg_BG");
     957            else if ( !strnicmp( p, "be", 2 )) setlocale( LC_NUMERIC, "be_BY");
     958            else if ( !strnicmp( p, "ca", 2 )) setlocale( LC_NUMERIC, "ca_ES");
     959            else if ( !strnicmp( p, "cs", 2 )) setlocale( LC_NUMERIC, "cs_CZ");
     960            else if ( !strnicmp( p, "da", 2 )) setlocale( LC_NUMERIC, "da_DK");
     961            else if ( !strnicmp( p, "de", 2 )) setlocale( LC_NUMERIC, "de_DE");
     962            else if ( !strnicmp( p, "el", 2 )) setlocale( LC_NUMERIC, "el_GR");
     963            else if ( !strnicmp( p, "es", 2 )) setlocale( LC_NUMERIC, "es_ES");
     964            else if ( !strnicmp( p, "fi", 2 )) setlocale( LC_NUMERIC, "fi_FI");
     965            else if ( !strnicmp( p, "fr", 2 )) setlocale( LC_NUMERIC, "fr_FR");
     966            else if ( !strnicmp( p, "hr", 2 )) setlocale( LC_NUMERIC, "hr_HR");
     967            else if ( !strnicmp( p, "hu", 2 )) setlocale( LC_NUMERIC, "hu_HU");
     968            else if ( !strnicmp( p, "is", 2 )) setlocale( LC_NUMERIC, "is_IS");
     969            else if ( !strnicmp( p, "it", 2 )) setlocale( LC_NUMERIC, "it_IT");
     970            else if ( !strnicmp( p, "iw", 2 )) setlocale( LC_NUMERIC, "iw_IL");
     971            else if ( !strnicmp( p, "ja", 2 )) setlocale( LC_NUMERIC, "ja_JP");
     972            else if ( !strnicmp( p, "ko", 2 )) setlocale( LC_NUMERIC, "ko_KR");
     973            else if ( !strnicmp( p, "mk", 2 )) setlocale( LC_NUMERIC, "mk_MK");
     974            else if ( !strnicmp( p, "nl", 2 )) setlocale( LC_NUMERIC, "nl_NL");
     975            else if ( !strnicmp( p, "no", 2 )) setlocale( LC_NUMERIC, "no_NO");
     976            else if ( !strnicmp( p, "pl", 2 )) setlocale( LC_NUMERIC, "pl_PL");
     977            else if ( !strnicmp( p, "pt", 2 )) setlocale( LC_NUMERIC, "pt_PT");
     978            else if ( !strnicmp( p, "ro", 2 )) setlocale( LC_NUMERIC, "ro_RO");
     979            else if ( !strnicmp( p, "ru", 2 )) setlocale( LC_NUMERIC, "ru_RU");
     980            else if ( !strnicmp( p, "sh", 2 )) setlocale( LC_NUMERIC, "sh_SP");
     981            else if ( !strnicmp( p, "sk", 2 )) setlocale( LC_NUMERIC, "sk_SK");
     982            else if ( !strnicmp( p, "sl", 2 )) setlocale( LC_NUMERIC, "sl_SI");
     983            else if ( !strnicmp( p, "sq", 2 )) setlocale( LC_NUMERIC, "sq_AL");
     984            else if ( !strnicmp( p, "sv", 2 )) setlocale( LC_NUMERIC, "sv_SE");
     985            else if ( !strnicmp( p, "th", 2 )) setlocale( LC_NUMERIC, "th_TH");
     986            else if ( !strnicmp( p, "tr", 2 )) setlocale( LC_NUMERIC, "tr_TR");
     987            else if ( !strnicmp( p, "uk", 2 )) setlocale( LC_NUMERIC, "uk_UA");
     988            else if ( !strnicmp( p, "zh", 2 )) setlocale( LC_NUMERIC, "zh_TW");
     989*/
     990            else setlocale( LC_NUMERIC, "EN_US");
     991            free(p);
     992        }
     993        else setlocale( LC_NUMERIC, "en_us");
     994    }
     995    else setlocale( LC_NUMERIC, "");
    949996
    950997    // Check for a decimal place and treat as float or integer accordingly
     
    9681015    MAKERXSTRING( *prsResult, achNumber, strlen( achNumber ));
    9691016
    970     if ( pszSetTZ ) free( pszSetTZ );
    9711017    return ( 0 );
    9721018}
Note: See TracChangeset for help on using the changeset viewer.