16f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
26f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org******************************************************************************
36f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*
46f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   Copyright (C) 1997-2013, International Business Machines
56f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   Corporation and others.  All Rights Reserved.
66f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*
76f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org******************************************************************************
86f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*
96f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*  FILE NAME : putil.c (previously putil.cpp and ptypes.cpp)
106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*
116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   Date        Name        Description
126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   04/14/97    aliu        Creation.
136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   04/24/97    aliu        Added getDefaultDataDirectory() and
146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                            getDefaultLocaleID().
156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   04/28/97    aliu        Rewritten to assume Unix and apply general methods
166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                            for assumed case.  Non-UNIX platforms must be
176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                            special-cased.  Rewrote numeric methods dealing
186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                            with NaN and Infinity to be platform independent
196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                             over all IEEE 754 platforms.
206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   05/13/97    aliu        Restored sign of timezone
216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                            (semantics are hours West of GMT)
226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   06/16/98    erm         Added IEEE_754 stuff, cleaned up isInfinite, isNan,
236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                             nextDouble..
246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   07/22/98    stephen     Added remainder, max, min, trunc
256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   08/13/98    stephen     Added isNegativeInfinity, isPositiveInfinity
266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   08/24/98    stephen     Added longBitsFromDouble
276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   09/08/98    stephen     Minor changes for Mac Port
286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   03/02/99    stephen     Removed openFile().  Added AS400 support.
296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*                            Fixed EBCDIC tables
306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   04/15/99    stephen     Converted to C.
316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   06/28/99    stephen     Removed mutex locking in u_isBigEndian().
326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   08/04/99    jeffrey R.  Added OS/2 changes
336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   11/15/99    helena      Integrated S/390 IEEE support.
346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   04/26/01    Barry N.    OS/400 support for uprv_getDefaultLocaleID
356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   08/15/01    Steven H.   OS/400 support for uprv_getDefaultCodepage
366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*   01/03/08    Steven L.   Fake Time Support
376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org******************************************************************************
386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*/
396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Defines _XOPEN_SOURCE for access to POSIX functions.
416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org// Must be before any other #includes.
426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "uposixdefs.h"
436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* include ICU headers */
456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/utypes.h"
466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/putil.h"
476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "unicode/ustring.h"
486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "putilimp.h"
496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "uassert.h"
506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "umutex.h"
516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "cmemory.h"
526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "cstring.h"
536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "locmap.h"
546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include "ucln_cmn.h"
556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Include standard headers. */
576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <stdio.h>
586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <stdlib.h>
596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <string.h>
606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <math.h>
616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <locale.h>
626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <float.h>
636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifndef U_COMMON_IMPLEMENTATION
656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#error U_COMMON_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see http://userguide.icu-project.org/howtouseicu
666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* include system headers */
706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_USES_ONLY_WIN32_API
716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*
726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * TODO: U_PLATFORM_USES_ONLY_WIN32_API includes MinGW.
736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * Should Cygwin be included as well (U_PLATFORM_HAS_WIN32_API)
746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * to use native APIs as much as possible?
756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define WIN32_LEAN_AND_MEAN
776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define VC_EXTRALEAN
786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define NOUSER
796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define NOSERVICE
806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define NOIME
816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define NOMCX
826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <windows.h>
836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include "wintz.h"
846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_OS400
856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <float.h>
866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <qusec.h>       /* error code structure */
876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <qusrjobi.h>
886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <qliept.h>      /* EPT_CALL macro  - this include must be after all other "QSYSINCs" */
896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <mih/testptr.h> /* For uprv_maximumPtr */
906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_CLASSIC_MACOS
916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <Files.h>
926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <IntlResources.h>
936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <Script.h>
946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <Folders.h>
956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <MacTypes.h>
966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <TextUtils.h>
976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define ICU_NO_USER_DATA_OVERRIDE 1
986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_OS390
996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include "unicode/ucnv.h"   /* Needed for UCNV_SWAP_LFNL_OPTION_STRING */
1006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM_IS_DARWIN_BASED || U_PLATFORM_IS_LINUX_BASED || U_PLATFORM == U_PF_BSD || U_PLATFORM == U_PF_SOLARIS
1016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <limits.h>
1026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <unistd.h>
1036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   if U_PLATFORM == U_PF_SOLARIS
1046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#       ifndef _XPG4_2
1056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#           define _XPG4_2
1066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#       endif
1076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   endif
1086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_QNX
1096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   include <sys/neutrino.h>
1106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
1116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if (U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(__STRICT_ANSI__)
1136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* tzset isn't defined in strict ANSI on Cygwin and MinGW. */
1146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#undef __STRICT_ANSI__
1156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
1166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
1186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Cygwin with GCC requires inclusion of time.h after the above disabling strict asci mode statement.
1196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
1206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <time.h>
1216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !U_PLATFORM_USES_ONLY_WIN32_API
1236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <sys/time.h>
1246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
1256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
1276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Only include langinfo.h if we have a way to get the codeset. If we later
1286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * depend on more feature, we can test on U_HAVE_NL_LANGINFO.
1296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *
1306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
1316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_HAVE_NL_LANGINFO_CODESET
1336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <langinfo.h>
1346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
1356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
1376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Simple things (presence of functions, etc) should just go in configure.in and be added to
1386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * icucfg.h via autoheader.
1396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
1406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_IMPLEMENTS_POSIX
1416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   if U_PLATFORM == U_PF_OS400
1426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#    define HAVE_DLFCN_H 0
1436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#    define HAVE_DLOPEN 0
1446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   else
1456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   ifndef HAVE_DLFCN_H
1466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#    define HAVE_DLFCN_H 1
1476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   endif
1486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   ifndef HAVE_DLOPEN
1496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#    define HAVE_DLOPEN 1
1506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   endif
1516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   endif
1526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   ifndef HAVE_GETTIMEOFDAY
1536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#    define HAVE_GETTIMEOFDAY 1
1546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   endif
1556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
1566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define HAVE_DLFCN_H 0
1576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define HAVE_DLOPEN 0
1586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define HAVE_GETTIMEOFDAY 0
1596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
1606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
1626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Define the extension for data files, again... */
1646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define DATA_TYPE "dat"
1656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Leave this copyright notice here! */
1676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char copyright[] = U_COPYRIGHT_STRING;
1686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* floating point implementations ------------------------------------------- */
1706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* We return QNAN rather than SNAN*/
1726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define SIGN 0x80000000U
1736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Make it easy to define certain types of constants */
1756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgtypedef union {
1766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int64_t i64; /* This must be defined first in order to allow the initialization to work. This is a C89 feature. */
1776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    double d64;
1786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org} BitPatternConversion;
1796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const BitPatternConversion gNan = { (int64_t) INT64_C(0x7FF8000000000000) };
1806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const BitPatternConversion gInf = { (int64_t) INT64_C(0x7FF0000000000000) };
1816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*---------------------------------------------------------------------------
1836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  Platform utilities
1846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  Our general strategy is to assume we're on a POSIX platform.  Platforms which
1856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  are non-POSIX must declare themselves so.  The default POSIX implementation
1866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  will sometimes work for non-POSIX platforms as well (e.g., the NaN-related
1876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  functions).
1886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ---------------------------------------------------------------------------*/
1896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_CLASSIC_MACOS || U_PLATFORM == U_PF_OS400
1916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   undef U_POSIX_LOCALE
1926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
1936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   define U_POSIX_LOCALE    1
1946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
1956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
1966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
1976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    WARNING! u_topNBytesOfDouble and u_bottomNBytesOfDouble
1986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    can't be properly optimized by the gcc compiler sometimes (i.e. gcc 3.2).
1996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*/
2006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !IEEE_754
2016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char*
2026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_topNBytesOfDouble(double* d, int n)
2036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
2046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_IS_BIG_ENDIAN
2056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (char*)d;
2066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
2076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (char*)(d + 1) - n;
2086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
2106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char*
2126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_bottomNBytesOfDouble(double* d, int n)
2136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
2146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_IS_BIG_ENDIAN
2156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (char*)(d + 1) - n;
2166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
2176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (char*)d;
2186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
2206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif   /* !IEEE_754 */
2216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754
2236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UBool
2246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_signBit(double d) {
2256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint8_t hiByte;
2266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_IS_BIG_ENDIAN
2276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    hiByte = *(uint8_t *)&d;
2286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
2296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    hiByte = *(((uint8_t *)&d) + sizeof(double) - 1);
2306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (hiByte & 0x80) != 0;
2326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
2336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined (U_DEBUG_FAKETIME)
2386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Override the clock to test things without having to move the system clock.
2396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Assumes POSIX gettimeofday() will function
2406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
2416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgUDate fakeClock_t0 = 0; /** Time to start the clock from **/
2426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgUDate fakeClock_dt = 0; /** Offset (fake time - real time) **/
2436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgUBool fakeClock_set = FALSE; /** True if fake clock has spun up **/
2446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UMutex fakeClockMutex = U_MUTEX_INTIALIZER;
2456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UDate getUTCtime_real() {
2476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    struct timeval posixTime;
2486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    gettimeofday(&posixTime, NULL);
2496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UDate)(((int64_t)posixTime.tv_sec * U_MILLIS_PER_SECOND) + (posixTime.tv_usec/1000));
2506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
2516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UDate getUTCtime_fake() {
2536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    umtx_lock(&fakeClockMutex);
2546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(!fakeClock_set) {
2556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        UDate real = getUTCtime_real();
2566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        const char *fake_start = getenv("U_FAKETIME_START");
2576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if((fake_start!=NULL) && (fake_start[0]!=0)) {
2586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            sscanf(fake_start,"%lf",&fakeClock_t0);
2596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fakeClock_dt = fakeClock_t0 - real;
2606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fprintf(stderr,"U_DEBUG_FAKETIME was set at compile time, so the ICU clock will start at a preset value\n"
2616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "env variable U_FAKETIME_START=%.0f (%s) for an offset of %.0f ms from the current time %.0f\n",
2626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    fakeClock_t0, fake_start, fakeClock_dt, real);
2636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
2646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          fakeClock_dt = 0;
2656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fprintf(stderr,"U_DEBUG_FAKETIME was set at compile time, but U_FAKETIME_START was not set.\n"
2666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    "Set U_FAKETIME_START to the number of milliseconds since 1/1/1970 to set the ICU clock.\n");
2676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
2686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        fakeClock_set = TRUE;
2696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
2706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    umtx_unlock(&fakeClockMutex);
2716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return getUTCtime_real() + fakeClock_dt;
2736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
2746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_USES_ONLY_WIN32_API
2776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgtypedef union {
2786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int64_t int64;
2796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    FILETIME fileTime;
2806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org} FileTimeConversion;   /* This is like a ULARGE_INTEGER */
2816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Number of 100 nanoseconds from 1/1/1601 to 1/1/1970 */
2836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define EPOCH_BIAS  INT64_C(116444736000000000)
2846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define HECTONANOSECOND_PER_MILLISECOND   10000
2856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
2876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*---------------------------------------------------------------------------
2896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  Universal Implementations
2906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  These are designed to work on all platforms.  Try these, and if they
2916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  don't work on your platform, then special case your platform with new
2926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  implementations.
2936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org---------------------------------------------------------------------------*/
2946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
2956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI UDate U_EXPORT2
2966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_getUTCtime()
2976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
2986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(U_DEBUG_FAKETIME)
2996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return getUTCtime_fake(); /* Hook for overriding the clock */
3006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
3016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return uprv_getRawUTCtime();
3026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
3036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
3046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Return UTC (GMT) time measured in milliseconds since 0:00 on 1/1/70.*/
3066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI UDate U_EXPORT2
3076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_getRawUTCtime()
3086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
3096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM == U_PF_CLASSIC_MACOS
3106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    time_t t, t1, t2;
3116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    struct tm tmrec;
3126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_memset( &tmrec, 0, sizeof(tmrec) );
3146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tmrec.tm_year = 70;
3156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tmrec.tm_mon = 0;
3166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tmrec.tm_mday = 1;
3176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    t1 = mktime(&tmrec);    /* seconds of 1/1/1970*/
3186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    time(&t);
3206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_memcpy( &tmrec, gmtime(&t), sizeof(tmrec) );
3216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    t2 = mktime(&tmrec);    /* seconds of current GMT*/
3226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UDate)(t2 - t1) * U_MILLIS_PER_SECOND;         /* GMT (or UTC) in seconds since 1970*/
3236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM_USES_ONLY_WIN32_API
3246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    FileTimeConversion winTime;
3266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    GetSystemTimeAsFileTime(&winTime.fileTime);
3276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UDate)((winTime.int64 - EPOCH_BIAS) / HECTONANOSECOND_PER_MILLISECOND);
3286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
3296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if HAVE_GETTIMEOFDAY
3316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    struct timeval posixTime;
3326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    gettimeofday(&posixTime, NULL);
3336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UDate)(((int64_t)posixTime.tv_sec * U_MILLIS_PER_SECOND) + (posixTime.tv_usec/1000));
3346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
3356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    time_t epochtime;
3366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    time(&epochtime);
3376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UDate)epochtime * U_MILLIS_PER_SECOND;
3386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
3396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
3416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
3426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*-----------------------------------------------------------------------------
3446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  IEEE 754
3456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  These methods detect and return NaN and infinity values for doubles
3466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  conforming to IEEE 754.  Platforms which support this standard include X86,
3476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  Mac 680x0, Mac PowerPC, AIX RS/6000, and most others.
3486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  If this doesn't work on your platform, you have non-IEEE floating-point, and
3496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  will need to code your own versions.  A naive implementation is to return 0.0
3506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  for getNaN and getInfinity, and false for isNaN and isInfinite.
3516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ---------------------------------------------------------------------------*/
3526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI UBool U_EXPORT2
3546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_isNaN(double number)
3556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
3566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754
3576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    BitPatternConversion convertedNumber;
3586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    convertedNumber.d64 = number;
3596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Infinity is 0x7FF0000000000000U. Anything greater than that is a NaN */
3606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UBool)((convertedNumber.i64 & U_INT64_MAX) > gInf.i64);
3616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_OS390
3636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint32_t highBits = *(uint32_t*)u_topNBytesOfDouble(&number,
3646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                        sizeof(uint32_t));
3656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint32_t lowBits  = *(uint32_t*)u_bottomNBytesOfDouble(&number,
3666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                        sizeof(uint32_t));
3676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return ((highBits & 0x7F080000L) == 0x7F080000L) &&
3696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      (lowBits == 0x00000000L);
3706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
3726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* If your platform doesn't support IEEE 754 but *does* have an NaN value,*/
3736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* you'll need to replace this default implementation with what's correct*/
3746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* for your platform.*/
3756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return number != number;
3766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
3776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
3786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI UBool U_EXPORT2
3806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_isInfinite(double number)
3816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
3826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754
3836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    BitPatternConversion convertedNumber;
3846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    convertedNumber.d64 = number;
3856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Infinity is exactly 0x7FF0000000000000U. */
3866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UBool)((convertedNumber.i64 & U_INT64_MAX) == gInf.i64);
3876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_OS390
3886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint32_t highBits = *(uint32_t*)u_topNBytesOfDouble(&number,
3896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                        sizeof(uint32_t));
3906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint32_t lowBits  = *(uint32_t*)u_bottomNBytesOfDouble(&number,
3916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                        sizeof(uint32_t));
3926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return ((highBits  & ~SIGN) == 0x70FF0000L) && (lowBits == 0x00000000L);
3946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
3956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
3966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* If your platform doesn't support IEEE 754 but *does* have an infinity*/
3976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* value, you'll need to replace this default implementation with what's*/
3986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* correct for your platform.*/
3996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return number == (2.0 * number);
4006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
4016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI UBool U_EXPORT2
4046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_isPositiveInfinity(double number)
4056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754 || U_PLATFORM == U_PF_OS390
4076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UBool)(number > 0 && uprv_isInfinite(number));
4086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
4096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return uprv_isInfinite(number);
4106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
4116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI UBool U_EXPORT2
4146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_isNegativeInfinity(double number)
4156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754 || U_PLATFORM == U_PF_OS390
4176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UBool)(number < 0 && uprv_isInfinite(number));
4186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
4206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint32_t highBits = *(uint32_t*)u_topNBytesOfDouble(&number,
4216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                        sizeof(uint32_t));
4226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return((highBits & SIGN) && uprv_isInfinite(number));
4236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
4256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_getNaN()
4296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754 || U_PLATFORM == U_PF_OS390
4316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return gNan.d64;
4326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
4336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* If your platform doesn't support IEEE 754 but *does* have an NaN value,*/
4346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* you'll need to replace this default implementation with what's correct*/
4356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* for your platform.*/
4366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return 0.0;
4376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
4386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_getInfinity()
4426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754 || U_PLATFORM == U_PF_OS390
4446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return gInf.d64;
4456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
4466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* If your platform doesn't support IEEE 754 but *does* have an infinity*/
4476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* value, you'll need to replace this default implementation with what's*/
4486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* correct for your platform.*/
4496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return 0.0;
4506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
4516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_floor(double x)
4556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return floor(x);
4576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_ceil(double x)
4616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return ceil(x);
4636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_round(double x)
4676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return uprv_floor(x + 0.5);
4696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_fabs(double x)
4736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return fabs(x);
4756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_modf(double x, double* y)
4796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return modf(x, y);
4816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_fmod(double x, double y)
4856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return fmod(x, y);
4876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_pow(double x, double y)
4916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* This is declared as "double pow(double x, double y)" */
4936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return pow(x, y);
4946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
4956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
4966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
4976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_pow10(int32_t x)
4986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
4996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return pow(10.0, (double)x);
5006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
5036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_fmax(double x, double y)
5046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
5056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754
5066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* first handle NaN*/
5076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(uprv_isNaN(x) || uprv_isNaN(y))
5086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return uprv_getNaN();
5096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* check for -0 and 0*/
5116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(x == 0.0 && y == 0.0 && u_signBit(x))
5126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return y;
5136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
5156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* this should work for all flt point w/o NaN and Inf special cases */
5176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (x > y ? x : y);
5186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
5216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_fmin(double x, double y)
5226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
5236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754
5246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* first handle NaN*/
5256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(uprv_isNaN(x) || uprv_isNaN(y))
5266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return uprv_getNaN();
5276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* check for -0 and 0*/
5296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(x == 0.0 && y == 0.0 && u_signBit(y))
5306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return y;
5316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
5336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* this should work for all flt point w/o NaN and Inf special cases */
5356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (x > y ? y : x);
5366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
5396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Truncates the given double.
5406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * trunc(3.3) = 3.0, trunc (-3.3) = -3.0
5416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * This is different than calling floor() or ceil():
5426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * floor(3.3) = 3, floor(-3.3) = -4
5436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * ceil(3.3) = 4, ceil(-3.3) = -3
5446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
5456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
5466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_trunc(double d)
5476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
5486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if IEEE_754
5496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* handle error cases*/
5506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(uprv_isNaN(d))
5516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return uprv_getNaN();
5526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(uprv_isInfinite(d))
5536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return uprv_getInfinity();
5546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(u_signBit(d))    /* Signbit() picks up -0.0;  d<0 does not. */
5566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return ceil(d);
5576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else
5586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return floor(d);
5596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
5616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return d >= 0 ? floor(d) : ceil(d);
5626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
5646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
5676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Return the largest positive number that can be represented by an integer
5686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * type of arbitrary bit length.
5696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
5706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
5716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_maxMantissa(void)
5726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
5736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return pow(2.0, DBL_MANT_DIG + 1.0) - 1.0;
5746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI double U_EXPORT2
5776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_log(double d)
5786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
5796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return log(d);
5806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
5816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
5826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI void * U_EXPORT2
5836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_maximumPtr(void * base)
5846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
5856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM == U_PF_OS400
5866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*
5876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * With the provided function we should never be out of range of a given segment
5886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * (a traditional/typical segment that is).  Our segments have 5 bytes for the
5896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * id and 3 bytes for the offset.  The key is that the casting takes care of
5906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * only retrieving the offset portion minus x1000.  Hence, the smallest offset
5916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * seen in a program is x001000 and when casted to an int would be 0.
5926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * That's why we can only add 0xffefff.  Otherwise, we would exceed the segment.
5936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *
5946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * Currently, 16MB is the current addressing limitation on i5/OS if the activation is
5956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * non-TERASPACE.  If it is TERASPACE it is 2GB - 4k(header information).
5966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * This function determines the activation based on the pointer that is passed in and
5976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * calculates the appropriate maximum available size for
5986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * each pointer type (TERASPACE and non-TERASPACE)
5996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     *
6006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * Unlike other operating systems, the pointer model isn't determined at
6016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * compile time on i5/OS.
6026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
6036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if ((base != NULL) && (_TESTPTR(base, _C_TERASPACE_CHECK))) {
6046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* if it is a TERASPACE pointer the max is 2GB - 4k */
6056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return ((void *)(((char *)base)-((uint32_t)(base))+((uint32_t)0x7fffefff)));
6066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
6076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* otherwise 16MB since NULL ptr is not checkable or the ptr is not TERASPACE */
6086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return ((void *)(((char *)base)-((uint32_t)(base))+((uint32_t)0xffefff)));
6096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
6116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return U_MAX_PTR(base);
6126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*---------------------------------------------------------------------------
6166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  Platform-specific Implementations
6176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  Try these, and if they don't work on your platform, then special case your
6186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  platform with new implementations.
6196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ---------------------------------------------------------------------------*/
6206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Generic time zone layer -------------------------------------------------- */
6226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Time zone utilities */
6246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI void U_EXPORT2
6256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_tzset()
6266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
6276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(U_TZSET)
6286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    U_TZSET();
6296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
6306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* no initialization*/
6316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI int32_t U_EXPORT2
6356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_timezone()
6366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
6376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef U_TIMEZONE
6386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return U_TIMEZONE;
6396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
6406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    time_t t, t1, t2;
6416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    struct tm tmrec;
6426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t tdiff = 0;
6436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    time(&t);
6456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_memcpy( &tmrec, localtime(&t), sizeof(tmrec) );
6466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM != U_PF_IPHONE
6476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UBool dst_checked = (tmrec.tm_isdst != 0); /* daylight savings time is checked*/
6486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    t1 = mktime(&tmrec);                 /* local time in seconds*/
6506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_memcpy( &tmrec, gmtime(&t), sizeof(tmrec) );
6516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    t2 = mktime(&tmrec);                 /* GMT (or UTC) in seconds*/
6526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tdiff = t2 - t1;
6536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM != U_PF_IPHONE
6556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* imitate NT behaviour, which returns same timezone offset to GMT for
6566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       winter and summer.
6576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       This does not work on all platforms. For instance, on glibc on Linux
6586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       and on Mac OS 10.5, tdiff calculated above remains the same
6596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       regardless of whether DST is in effect or not. iOS is another
6606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       platform where this does not work. Linux + glibc and Mac OS 10.5
6616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       have U_TIMEZONE defined so that this code is not reached.
6626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
6636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (dst_checked)
6646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        tdiff += 3600;
6656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return tdiff;
6676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
6696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Note that U_TZNAME does *not* have to be tzname, but if it is,
6716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   some platforms need to have it declared here. */
6726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(U_TZNAME) && (U_PLATFORM == U_PF_IRIX || U_PLATFORM_IS_DARWIN_BASED || (U_PLATFORM == U_PF_CYGWIN && !U_PLATFORM_USES_ONLY_WIN32_API))
6746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* RS6000 and others reject char **tzname.  */
6756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgextern U_IMPORT char *U_TZNAME[];
6766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
6786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !UCONFIG_NO_FILE_IO && ((U_PLATFORM_IS_DARWIN_BASED && (U_PLATFORM != U_PF_IPHONE || defined(U_TIMEZONE))) || U_PLATFORM_IS_LINUX_BASED || U_PLATFORM == U_PF_BSD || U_PLATFORM == U_PF_SOLARIS)
6796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* These platforms are likely to use Olson timezone IDs. */
6806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define CHECK_LOCALTIME_LINK 1
6816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_IS_DARWIN_BASED
6826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <tzfile.h>
6836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZZONEINFO      (TZDIR "/")
6846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_SOLARIS
6856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZDEFAULT       "/etc/localtime"
6866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZZONEINFO      "/usr/share/lib/zoneinfo/"
6876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZZONEINFO2     "../usr/share/lib/zoneinfo/"
6886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZ_ENV_CHECK    "localtime"
6896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
6906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZDEFAULT       "/etc/localtime"
6916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZZONEINFO      "/usr/share/zoneinfo/"
6926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
6936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_HAVE_DIRENT_H
6946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZFILE_SKIP     "posixrules" /* tz file to skip when searching. */
6956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Some Linux distributions have 'localtime' in /usr/share/zoneinfo
6966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   symlinked to /etc/localtime, which makes searchForTZFile return
6976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   'localtime' when it's the first match. */
6986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define TZFILE_SKIP2    "localtime"
6996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define SEARCH_TZFILE
7006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <dirent.h>  /* Needed to search through system timezone files */
7016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
7026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char gTimeZoneBuffer[PATH_MAX];
7036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char *gTimeZoneBufferPtr = NULL;
7046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
7056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !U_PLATFORM_USES_ONLY_WIN32_API
7076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define isNonDigit(ch) (ch < '0' || '9' < ch)
7086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UBool isValidOlsonID(const char *id) {
7096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t idx = 0;
7106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Determine if this is something like Iceland (Olson ID)
7126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    or AST4ADT (non-Olson ID) */
7136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    while (id[idx] && isNonDigit(id[idx]) && id[idx] != ',') {
7146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        idx++;
7156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
7166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* If we went through the whole string, then it might be okay.
7186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    The timezone is sometimes set to "CST-7CDT", "CST6CDT5,J129,J131/19:30",
7196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    "GRNLNDST3GRNLNDDT" or similar, so we cannot use it.
7206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    The rest of the time it could be an Olson ID. George */
7216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return (UBool)(id[idx] == 0
7226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        || uprv_strcmp(id, "PST8PDT") == 0
7236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        || uprv_strcmp(id, "MST7MDT") == 0
7246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        || uprv_strcmp(id, "CST6CDT") == 0
7256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        || uprv_strcmp(id, "EST5EDT") == 0);
7266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
7276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* On some Unix-like OS, 'posix' subdirectory in
7296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   /usr/share/zoneinfo replicates the top-level contents. 'right'
7306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   subdirectory has the same set of files, but individual files
7316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   are different from those in the top-level directory or 'posix'
7326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   because 'right' has files for TAI (Int'l Atomic Time) while 'posix'
7336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   has files for UTC.
7346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   When the first match for /etc/localtime is in either of them
7356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   (usually in posix because 'right' has different file contents),
7366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   or TZ environment variable points to one of them, createTimeZone
7376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   fails because, say, 'posix/America/New_York' is not an Olson
7386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   timezone id ('America/New_York' is). So, we have to skip
7396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   'posix/' and 'right/' at the beginning. */
7406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic void skipZoneIDPrefix(const char** id) {
7416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (uprv_strncmp(*id, "posix/", 6) == 0
7426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        || uprv_strncmp(*id, "right/", 6) == 0)
7436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
7446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *id += 6;
7456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
7466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
7476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
7486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(U_TZNAME) && !U_PLATFORM_USES_ONLY_WIN32_API
7506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define CONVERT_HOURS_TO_SECONDS(offset) (int32_t)(offset*3600)
7526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgtypedef struct OffsetZoneMapping {
7536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t offsetSeconds;
7546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t daylightType; /* 0=U_DAYLIGHT_NONE, 1=daylight in June-U_DAYLIGHT_JUNE, 2=daylight in December=U_DAYLIGHT_DECEMBER*/
7556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *stdID;
7566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *dstID;
7576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *olsonID;
7586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org} OffsetZoneMapping;
7596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgenum { U_DAYLIGHT_NONE=0,U_DAYLIGHT_JUNE=1,U_DAYLIGHT_DECEMBER=2 };
7616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
7626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
7636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgThis list tries to disambiguate a set of abbreviated timezone IDs and offsets
7646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.organd maps it to an Olson ID.
7656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgBefore adding anything to this list, take a look at
7666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgicu/source/tools/tzcode/tz.alias
7676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgSometimes no daylight savings (0) is important to define due to aliases.
7686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgThis list can be tested with icu/source/test/compat/tzone.pl
7696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgMore values could be added to daylightType to increase precision.
7706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*/
7716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const struct OffsetZoneMapping OFFSET_ZONE_MAPPINGS[] = {
7726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-45900, 2, "CHAST", "CHADT", "Pacific/Chatham"},
7736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-43200, 1, "PETT", "PETST", "Asia/Kamchatka"},
7746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-43200, 2, "NZST", "NZDT", "Pacific/Auckland"},
7756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-43200, 1, "ANAT", "ANAST", "Asia/Anadyr"},
7766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-39600, 1, "MAGT", "MAGST", "Asia/Magadan"},
7776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-37800, 2, "LHST", "LHST", "Australia/Lord_Howe"},
7786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-36000, 2, "EST", "EST", "Australia/Sydney"},
7796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-36000, 1, "SAKT", "SAKST", "Asia/Sakhalin"},
7806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-36000, 1, "VLAT", "VLAST", "Asia/Vladivostok"},
7816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-34200, 2, "CST", "CST", "Australia/South"},
7826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-32400, 1, "YAKT", "YAKST", "Asia/Yakutsk"},
7836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-32400, 1, "CHOT", "CHOST", "Asia/Choibalsan"},
7846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-31500, 2, "CWST", "CWST", "Australia/Eucla"},
7856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-28800, 1, "IRKT", "IRKST", "Asia/Irkutsk"},
7866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-28800, 1, "ULAT", "ULAST", "Asia/Ulaanbaatar"},
7876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-28800, 2, "WST", "WST", "Australia/West"},
7886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-25200, 1, "HOVT", "HOVST", "Asia/Hovd"},
7896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-25200, 1, "KRAT", "KRAST", "Asia/Krasnoyarsk"},
7906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-21600, 1, "NOVT", "NOVST", "Asia/Novosibirsk"},
7916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-21600, 1, "OMST", "OMSST", "Asia/Omsk"},
7926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-18000, 1, "YEKT", "YEKST", "Asia/Yekaterinburg"},
7936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-14400, 1, "SAMT", "SAMST", "Europe/Samara"},
7946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-14400, 1, "AMT", "AMST", "Asia/Yerevan"},
7956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-14400, 1, "AZT", "AZST", "Asia/Baku"},
7966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-10800, 1, "AST", "ADT", "Asia/Baghdad"},
7976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-10800, 1, "MSK", "MSD", "Europe/Moscow"},
7986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-10800, 1, "VOLT", "VOLST", "Europe/Volgograd"},
7996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-7200, 0, "EET", "CEST", "Africa/Tripoli"},
8006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-7200, 1, "EET", "EEST", "Europe/Athens"}, /* Conflicts with Africa/Cairo */
8016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-7200, 1, "IST", "IDT", "Asia/Jerusalem"},
8026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-3600, 0, "CET", "WEST", "Africa/Algiers"},
8036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {-3600, 2, "WAT", "WAST", "Africa/Windhoek"},
8046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {0, 1, "GMT", "IST", "Europe/Dublin"},
8056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {0, 1, "GMT", "BST", "Europe/London"},
8066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {0, 0, "WET", "WEST", "Africa/Casablanca"},
8076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {0, 0, "WET", "WET", "Africa/El_Aaiun"},
8086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {3600, 1, "AZOT", "AZOST", "Atlantic/Azores"},
8096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {3600, 1, "EGT", "EGST", "America/Scoresbysund"},
8106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {10800, 1, "PMST", "PMDT", "America/Miquelon"},
8116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {10800, 2, "UYT", "UYST", "America/Montevideo"},
8126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {10800, 1, "WGT", "WGST", "America/Godthab"},
8136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {10800, 2, "BRT", "BRST", "Brazil/East"},
8146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {12600, 1, "NST", "NDT", "America/St_Johns"},
8156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {14400, 1, "AST", "ADT", "Canada/Atlantic"},
8166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {14400, 2, "AMT", "AMST", "America/Cuiaba"},
8176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {14400, 2, "CLT", "CLST", "Chile/Continental"},
8186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {14400, 2, "FKT", "FKST", "Atlantic/Stanley"},
8196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {14400, 2, "PYT", "PYST", "America/Asuncion"},
8206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {18000, 1, "CST", "CDT", "America/Havana"},
8216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {18000, 1, "EST", "EDT", "US/Eastern"}, /* Conflicts with America/Grand_Turk */
8226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {21600, 2, "EAST", "EASST", "Chile/EasterIsland"},
8236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {21600, 0, "CST", "MDT", "Canada/Saskatchewan"},
8246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {21600, 0, "CST", "CDT", "America/Guatemala"},
8256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {21600, 1, "CST", "CDT", "US/Central"}, /* Conflicts with Mexico/General */
8266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {25200, 1, "MST", "MDT", "US/Mountain"}, /* Conflicts with Mexico/BajaSur */
8276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {28800, 0, "PST", "PST", "Pacific/Pitcairn"},
8286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {28800, 1, "PST", "PDT", "US/Pacific"}, /* Conflicts with Mexico/BajaNorte */
8296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {32400, 1, "AKST", "AKDT", "US/Alaska"},
8306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {36000, 1, "HAST", "HADT", "US/Aleutian"}
8316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org};
8326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*#define DEBUG_TZNAME*/
8346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char* remapShortTimeZone(const char *stdID, const char *dstID, int32_t daylightType, int32_t offset)
8366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
8376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t idx;
8386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef DEBUG_TZNAME
8396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    fprintf(stderr, "TZ=%s std=%s dst=%s daylight=%d offset=%d\n", getenv("TZ"), stdID, dstID, daylightType, offset);
8406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
8416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    for (idx = 0; idx < LENGTHOF(OFFSET_ZONE_MAPPINGS); idx++)
8426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
8436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (offset == OFFSET_ZONE_MAPPINGS[idx].offsetSeconds
8446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            && daylightType == OFFSET_ZONE_MAPPINGS[idx].daylightType
8456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            && strcmp(OFFSET_ZONE_MAPPINGS[idx].stdID, stdID) == 0
8466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            && strcmp(OFFSET_ZONE_MAPPINGS[idx].dstID, dstID) == 0)
8476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
8486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return OFFSET_ZONE_MAPPINGS[idx].olsonID;
8496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
8506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
8516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return NULL;
8526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
8536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
8546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef SEARCH_TZFILE
8566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define MAX_PATH_SIZE PATH_MAX /* Set the limit for the size of the path. */
8576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define MAX_READ_SIZE 512
8586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgtypedef struct DefaultTZInfo {
8606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char* defaultTZBuffer;
8616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int64_t defaultTZFileSize;
8626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    FILE* defaultTZFilePtr;
8636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UBool defaultTZstatus;
8646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t defaultTZPosition;
8656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org} DefaultTZInfo;
8666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
8686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * This method compares the two files given to see if they are a match.
8696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * It is currently use to compare two TZ files.
8706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
8716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UBool compareBinaryFiles(const char* defaultTZFileName, const char* TZFileName, DefaultTZInfo* tzInfo) {
8726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    FILE* file;
8736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int64_t sizeFile;
8746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int64_t sizeFileLeft;
8756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t sizeFileRead;
8766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t sizeFileToRead;
8776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char bufferFile[MAX_READ_SIZE];
8786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UBool result = TRUE;
8796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (tzInfo->defaultTZFilePtr == NULL) {
8816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        tzInfo->defaultTZFilePtr = fopen(defaultTZFileName, "r");
8826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
8836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    file = fopen(TZFileName, "r");
8846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tzInfo->defaultTZPosition = 0; /* reset position to begin search */
8866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (file != NULL && tzInfo->defaultTZFilePtr != NULL) {
8886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* First check that the file size are equal. */
8896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (tzInfo->defaultTZFileSize == 0) {
8906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            fseek(tzInfo->defaultTZFilePtr, 0, SEEK_END);
8916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            tzInfo->defaultTZFileSize = ftell(tzInfo->defaultTZFilePtr);
8926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
8936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        fseek(file, 0, SEEK_END);
8946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        sizeFile = ftell(file);
8956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        sizeFileLeft = sizeFile;
8966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
8976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (sizeFile != tzInfo->defaultTZFileSize) {
8986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            result = FALSE;
8996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
9006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* Store the data from the files in seperate buffers and
9016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             * compare each byte to determine equality.
9026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             */
9036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (tzInfo->defaultTZBuffer == NULL) {
9046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                rewind(tzInfo->defaultTZFilePtr);
9056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzInfo->defaultTZBuffer = (char*)uprv_malloc(sizeof(char) * tzInfo->defaultTZFileSize);
9066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                sizeFileRead = fread(tzInfo->defaultTZBuffer, 1, tzInfo->defaultTZFileSize, tzInfo->defaultTZFilePtr);
9076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
9086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            rewind(file);
9096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            while(sizeFileLeft > 0) {
9106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                uprv_memset(bufferFile, 0, MAX_READ_SIZE);
9116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                sizeFileToRead = sizeFileLeft < MAX_READ_SIZE ? sizeFileLeft : MAX_READ_SIZE;
9126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
9136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                sizeFileRead = fread(bufferFile, 1, sizeFileToRead, file);
9146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if (memcmp(tzInfo->defaultTZBuffer + tzInfo->defaultTZPosition, bufferFile, sizeFileRead) != 0) {
9156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    result = FALSE;
9166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    break;
9176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                }
9186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                sizeFileLeft -= sizeFileRead;
9196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzInfo->defaultTZPosition += sizeFileRead;
9206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
9216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
9226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    } else {
9236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        result = FALSE;
9246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
9256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
9266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (file != NULL) {
9276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        fclose(file);
9286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
9296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
9306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return result;
9316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
9326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
9336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * This method recursively traverses the directory given for a matching TZ file and returns the first match.
9346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
9356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* dirent also lists two entries: "." and ".." that we can safely ignore. */
9366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define SKIP1 "."
9376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define SKIP2 ".."
9386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char SEARCH_TZFILE_RESULT[MAX_PATH_SIZE] = "";
9396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char* searchForTZFile(const char* path, DefaultTZInfo* tzInfo) {
9406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char curpath[MAX_PATH_SIZE];
9416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    DIR* dirp = opendir(path);
9426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    DIR* subDirp = NULL;
9436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    struct dirent* dirEntry = NULL;
9446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
9456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char* result = NULL;
9466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (dirp == NULL) {
9476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return result;
9486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
9496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
9506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Save the current path */
9516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_memset(curpath, 0, MAX_PATH_SIZE);
9526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_strcpy(curpath, path);
9536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
9546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Check each entry in the directory. */
9556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    while((dirEntry = readdir(dirp)) != NULL) {
9566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        const char* dirName = dirEntry->d_name;
9576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (uprv_strcmp(dirName, SKIP1) != 0 && uprv_strcmp(dirName, SKIP2) != 0) {
9586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* Create a newpath with the new entry to test each entry in the directory. */
9596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            char newpath[MAX_PATH_SIZE];
9606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcpy(newpath, curpath);
9616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcat(newpath, dirName);
9626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
9636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if ((subDirp = opendir(newpath)) != NULL) {
9646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                /* If this new path is a directory, make a recursive call with the newpath. */
9656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                closedir(subDirp);
9666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                uprv_strcat(newpath, "/");
9676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                result = searchForTZFile(newpath, tzInfo);
9686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                /*
9696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                 Have to get out here. Otherwise, we'd keep looking
9706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                 and return the first match in the top-level directory
9716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                 if there's a match in the top-level. If not, this function
9726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                 would return NULL and set gTimeZoneBufferPtr to NULL in initDefault().
9736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                 It worked without this in most cases because we have a fallback of calling
9746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                 localtime_r to figure out the default timezone.
9756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                */
9766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if (result != NULL)
9776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    break;
9786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            } else if (uprv_strcmp(TZFILE_SKIP, dirName) != 0 && uprv_strcmp(TZFILE_SKIP2, dirName) != 0) {
9796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if(compareBinaryFiles(TZDEFAULT, newpath, tzInfo)) {
9806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    const char* zoneid = newpath + (sizeof(TZZONEINFO)) - 1;
9816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    skipZoneIDPrefix(&zoneid);
9826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    uprv_strcpy(SEARCH_TZFILE_RESULT, zoneid);
9836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    result = SEARCH_TZFILE_RESULT;
9846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    /* Get out after the first one found. */
9856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    break;
9866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                }
9876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
9886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
9896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
9906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    closedir(dirp);
9916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return result;
9926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
9936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
9946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI const char* U_EXPORT2
9956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_tzname(int n)
9966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
9976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *tzid = NULL;
9986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_USES_ONLY_WIN32_API
9996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tzid = uprv_detectWindowsTimeZone();
10006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (tzid != NULL) {
10026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return tzid;
10036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
10046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
10056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*#if U_PLATFORM_IS_DARWIN_BASED
10076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int ret;
10086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tzid = getenv("TZFILE");
10106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (tzid != NULL) {
10116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return tzid;
10126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
10136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif*/
10146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* This code can be temporarily disabled to test tzname resolution later on. */
10166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifndef DEBUG_TZNAME
10176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    tzid = getenv("TZ");
10186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (tzid != NULL && isValidOlsonID(tzid)
10196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM == U_PF_SOLARIS
10206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* When TZ equals localtime on Solaris, check the /etc/localtime file. */
10216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        && uprv_strcmp(tzid, TZ_ENV_CHECK) != 0
10226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
10236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ) {
10246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* This might be a good Olson ID. */
10256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        skipZoneIDPrefix(&tzid);
10266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return tzid;
10276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
10286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* else U_TZNAME will give a better result. */
10296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
10306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(CHECK_LOCALTIME_LINK) && !defined(DEBUG_SKIP_LOCALTIME_LINK)
10326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Caller must handle threading issues */
10336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gTimeZoneBufferPtr == NULL) {
10346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
10356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        This is a trick to look at the name of the link to get the Olson ID
10366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        because the tzfile contents is underspecified.
10376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        This isn't guaranteed to work because it may not be a symlink.
10386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
10396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        int32_t ret = (int32_t)readlink(TZDEFAULT, gTimeZoneBuffer, sizeof(gTimeZoneBuffer));
10406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (0 < ret) {
10416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            int32_t tzZoneInfoLen = uprv_strlen(TZZONEINFO);
10426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            gTimeZoneBuffer[ret] = 0;
10436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (uprv_strncmp(gTimeZoneBuffer, TZZONEINFO, tzZoneInfoLen) == 0
10446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                && isValidOlsonID(gTimeZoneBuffer + tzZoneInfoLen))
10456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            {
10466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                return (gTimeZoneBufferPtr = gTimeZoneBuffer + tzZoneInfoLen);
10476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
10486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM == U_PF_SOLARIS
10496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            else
10506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            {
10516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzZoneInfoLen = uprv_strlen(TZZONEINFO2);
10526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if (uprv_strncmp(gTimeZoneBuffer, TZZONEINFO2, tzZoneInfoLen) == 0
10536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                                && isValidOlsonID(gTimeZoneBuffer + tzZoneInfoLen))
10546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                {
10556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    return (gTimeZoneBufferPtr = gTimeZoneBuffer + tzZoneInfoLen);
10566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                }
10576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
10586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
10596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
10606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(SEARCH_TZFILE)
10616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            DefaultTZInfo* tzInfo = (DefaultTZInfo*)uprv_malloc(sizeof(DefaultTZInfo));
10626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (tzInfo != NULL) {
10636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzInfo->defaultTZBuffer = NULL;
10646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzInfo->defaultTZFileSize = 0;
10656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzInfo->defaultTZFilePtr = NULL;
10666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzInfo->defaultTZstatus = FALSE;
10676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                tzInfo->defaultTZPosition = 0;
10686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                gTimeZoneBufferPtr = searchForTZFile(TZZONEINFO, tzInfo);
10706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                /* Free previously allocated memory */
10726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if (tzInfo->defaultTZBuffer != NULL) {
10736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    uprv_free(tzInfo->defaultTZBuffer);
10746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                }
10756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if (tzInfo->defaultTZFilePtr != NULL) {
10766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    fclose(tzInfo->defaultTZFilePtr);
10776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                }
10786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                uprv_free(tzInfo);
10796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
10806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (gTimeZoneBufferPtr != NULL && isValidOlsonID(gTimeZoneBufferPtr)) {
10826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                return gTimeZoneBufferPtr;
10836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
10846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
10856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
10866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
10876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else {
10886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return gTimeZoneBufferPtr;
10896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
10906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
10916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
10926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
10936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef U_TZNAME
10946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_USES_ONLY_WIN32_API
10956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* The return value is free'd in timezone.cpp on Windows because
10966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * the other code path returns a pointer to a heap location. */
10976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return uprv_strdup(U_TZNAME[n]);
10986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
10996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*
11006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    U_TZNAME is usually a non-unique abbreviation, which isn't normally usable.
11016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    So we remap the abbreviation to an olson ID.
11026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    Since Windows exposes a little more timezone information,
11046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    we normally don't use this code on Windows because
11056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_detectWindowsTimeZone should have already given the correct answer.
11066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
11076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
11086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        struct tm juneSol, decemberSol;
11096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        int daylightType;
11106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        static const time_t juneSolstice=1182478260; /*2007-06-21 18:11 UT*/
11116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        static const time_t decemberSolstice=1198332540; /*2007-12-22 06:09 UT*/
11126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* This probing will tell us when daylight savings occurs.  */
11146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        localtime_r(&juneSolstice, &juneSol);
11156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        localtime_r(&decemberSolstice, &decemberSol);
11166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(decemberSol.tm_isdst > 0) {
11176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          daylightType = U_DAYLIGHT_DECEMBER;
11186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else if(juneSol.tm_isdst > 0) {
11196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          daylightType = U_DAYLIGHT_JUNE;
11206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
11216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org          daylightType = U_DAYLIGHT_NONE;
11226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
11236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        tzid = remapShortTimeZone(U_TZNAME[0], U_TZNAME[1], daylightType, uprv_timezone());
11246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (tzid != NULL) {
11256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return tzid;
11266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
11276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
11286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return U_TZNAME[n];
11296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
11306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
11316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return "";
11326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
11336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
11346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Get and set the ICU data directory --------------------------------------- */
11366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic char *gDataDirectory = NULL;
11386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API
11396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org static char *gCorrectedPOSIXLocale = NULL; /* Heap allocated */
11406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
11416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic UBool U_CALLCONV putil_cleanup(void)
11436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
11446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gDataDirectory && *gDataDirectory) {
11456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_free(gDataDirectory);
11466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
11476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    gDataDirectory = NULL;
11486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API
11496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gCorrectedPOSIXLocale) {
11506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_free(gCorrectedPOSIXLocale);
11516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        gCorrectedPOSIXLocale = NULL;
11526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
11536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
11546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return TRUE;
11556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
11566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
11586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Set the data directory.
11596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *    Make a copy of the passed string, and set the global data dir to point to it.
11606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
11616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI void U_EXPORT2
11626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_setDataDirectory(const char *directory) {
11636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *newDataDir;
11646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t length;
11656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(directory==NULL || *directory==0) {
11676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* A small optimization to prevent the malloc and copy when the
11686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        shared library is used, and this is a way to make sure that NULL
11696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        is never returned.
11706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
11716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        newDataDir = (char *)"";
11726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
11736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else {
11746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        length=(int32_t)uprv_strlen(directory);
11756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        newDataDir = (char *)uprv_malloc(length + 2);
11766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Exit out if newDataDir could not be created. */
11776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (newDataDir == NULL) {
11786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return;
11796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
11806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_strcpy(newDataDir, directory);
11816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
11836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
11846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            char *p;
11856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            while(p = uprv_strchr(newDataDir, U_FILE_ALT_SEP_CHAR)) {
11866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                *p = U_FILE_SEP_CHAR;
11876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
11886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
11896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
11906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
11916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gDataDirectory && *gDataDirectory) {
11936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_free(gDataDirectory);
11946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
11956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    gDataDirectory = newDataDir;
11966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup);
11976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
11986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
11996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI UBool U_EXPORT2
12006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_pathIsAbsolute(const char *path)
12016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
12026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(!path || !*path) {
12036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return FALSE;
12046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
12056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(*path == U_FILE_SEP_CHAR) {
12076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return TRUE;
12086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
12096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
12116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(*path == U_FILE_ALT_SEP_CHAR) {
12126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return TRUE;
12136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
12146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
12156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_USES_ONLY_WIN32_API
12176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if( (((path[0] >= 'A') && (path[0] <= 'Z')) ||
12186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       ((path[0] >= 'a') && (path[0] <= 'z'))) &&
12196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      path[1] == ':' ) {
12206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return TRUE;
12216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
12226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
12236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return FALSE;
12256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
12266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Temporary backup setting of ICU_DATA_DIR_PREFIX_ENV_VAR
12286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   until some client wrapper makefiles are updated */
12296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_IS_DARWIN_BASED && TARGET_IPHONE_SIMULATOR
12306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# if !defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
12316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#  define ICU_DATA_DIR_PREFIX_ENV_VAR "IPHONE_SIMULATOR_ROOT"
12326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# endif
12336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
12346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI const char * U_EXPORT2
12366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_getDataDirectory(void) {
12376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *path = NULL;
12386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
12396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char datadir_path_buffer[PATH_MAX];
12406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
12416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* if we have the directory, then return it immediately */
12436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(gDataDirectory) {
12446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return gDataDirectory;
12456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
12466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*
12486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    When ICU_NO_USER_DATA_OVERRIDE is defined, users aren't allowed to
12496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    override ICU's data with the ICU_DATA environment variable. This prevents
12506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    problems where multiple custom copies of ICU's specific version of data
12516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    are installed on a system. Either the application must define the data
12526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    directory with u_setDataDirectory, define ICU_DATA_DIR when compiling
12536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ICU, set the data with udata_setCommonData or trust that all of the
12546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    required data is contained in ICU's data library that contains
12556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    the entry point defined by U_ICUDATA_ENTRY_POINT.
12566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    There may also be some platforms where environment variables
12586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    are not allowed.
12596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
12606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   if !defined(ICU_NO_USER_DATA_OVERRIDE) && !UCONFIG_NO_FILE_IO
12616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* First try to get the environment variable */
12626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    path=getenv("ICU_DATA");
12636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#   endif
12646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* ICU_DATA_DIR may be set as a compile option.
12666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * U_ICU_DATA_DEFAULT_DIR is provided and is set by ICU at compile time
12676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * and is used only when data is built in archive mode eliminating the need
12686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * for ICU_DATA_DIR to be set. U_ICU_DATA_DEFAULT_DIR is set to the installation
12696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * directory of the data dat file. Users should use ICU_DATA_DIR if they want to
12706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * set their own path.
12716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
12726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if defined(ICU_DATA_DIR) || defined(U_ICU_DATA_DEFAULT_DIR)
12736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(path==NULL || *path==0) {
12746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
12756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        const char *prefix = getenv(ICU_DATA_DIR_PREFIX_ENV_VAR);
12766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# endif
12776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# ifdef ICU_DATA_DIR
12786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        path=ICU_DATA_DIR;
12796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# else
12806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        path=U_ICU_DATA_DEFAULT_DIR;
12816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# endif
12826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
12836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (prefix != NULL) {
12846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            snprintf(datadir_path_buffer, PATH_MAX, "%s%s", prefix, path);
12856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            path=datadir_path_buffer;
12866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
12876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org# endif
12886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
12896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
12906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(path==NULL) {
12926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* It looks really bad, set it to something. */
12936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        path = "";
12946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
12956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
12966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_setDataDirectory(path);
12976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return gDataDirectory;
12986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
12996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Macintosh-specific locale information ------------------------------------ */
13056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM == U_PF_CLASSIC_MACOS
13066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgtypedef struct {
13086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t script;
13096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t region;
13106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t lang;
13116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t date_region;
13126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char* posixID;
13136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org} mac_lc_rec;
13146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Todo: This will be updated with a newer version from www.unicode.org web
13166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org   page when it's available.*/
13176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define MAC_LC_MAGIC_NUMBER -5
13186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define MAC_LC_INIT_NUMBER -9
13196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
13206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const mac_lc_rec mac_lc_recs[] = {
13216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 0, "en_US",
13226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* United States*/
13236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 1, "fr_FR",
13246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* France*/
13256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 2, "en_GB",
13266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Great Britain*/
13276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 3, "de_DE",
13286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Germany*/
13296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 4, "it_IT",
13306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Italy*/
13316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 5, "nl_NL",
13326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Metherlands*/
13336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 6, "fr_BE",
13346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* French for Belgium or Lxembourg*/
13356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 7, "sv_SE",
13366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Sweden*/
13376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 9, "da_DK",
13386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Denmark*/
13396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 10, "pt_PT",
13406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Portugal*/
13416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 11, "fr_CA",
13426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* French Canada*/
13436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 13, "is_IS",
13446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Israel*/
13456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 14, "ja_JP",
13466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Japan*/
13476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 15, "en_AU",
13486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Australia*/
13496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 16, "ar_AE",
13506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* the Arabic world (?)*/
13516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 17, "fi_FI",
13526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Finland*/
13536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 18, "fr_CH",
13546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* French for Switzerland*/
13556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 19, "de_CH",
13566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* German for Switzerland*/
13576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 20, "el_GR",
13586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Greece*/
13596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 21, "is_IS",
13606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Iceland ===*/
13616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 22, "",*/
13626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Malta ===*/
13636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 23, "",*/
13646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Cyprus ===*/
13656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 24, "tr_TR",
13666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Turkey ===*/
13676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 25, "sh_YU",
13686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Croatian system for Yugoslavia*/
13696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 33, "",*/
13706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Hindi system for India*/
13716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 34, "",*/
13726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Pakistan*/
13736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 41, "lt_LT",
13746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Lithuania*/
13756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 42, "pl_PL",
13766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Poland*/
13776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 43, "hu_HU",
13786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Hungary*/
13796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 44, "et_EE",
13806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Estonia*/
13816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 45, "lv_LV",
13826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Latvia*/
13836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 46, "",*/
13846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Lapland  [Ask Rich for the data. HS]*/
13856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 47, "",*/
13866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Faeroe Islands*/
13876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 48, "fa_IR",
13886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Iran*/
13896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 49, "ru_RU",
13906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Russia*/
13916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 50, "en_IE",
13926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Ireland*/
13936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 51, "ko_KR",
13946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Korea*/
13956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 52, "zh_CN",
13966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* People's Republic of China*/
13976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 53, "zh_TW",
13986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Taiwan*/
13996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, 54, "th_TH",
14006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Thailand*/
14016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
14026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* fallback is en_US*/
14036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER, MAC_LC_MAGIC_NUMBER,
14046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    MAC_LC_MAGIC_NUMBER, "en_US"
14056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org};
14066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
14076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
14086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
14096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_POSIX_LOCALE
14106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* A helper function used by uprv_getPOSIXIDForDefaultLocale and
14116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * uprv_getPOSIXIDForDefaultCodepage. Returns the posix locale id for
14126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * LC_CTYPE and LC_MESSAGES. It doesn't support other locale categories.
14136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
14146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char *uprv_getPOSIXIDForCategory(int category)
14156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
14166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char* posixID = NULL;
14176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (category == LC_MESSAGES || category == LC_CTYPE) {
14186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
14196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * On Solaris two different calls to setlocale can result in
14206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * different values. Only get this value once.
14216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *
14226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * We must check this first because an application can set this.
14236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *
14246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * LC_ALL can't be used because it's platform dependent. The LANG
14256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * environment variable seems to affect LC_CTYPE variable by default.
14266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * Here is what setlocale(LC_ALL, NULL) can return.
14276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * HPUX can return 'C C C C C C C'
14286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * Solaris can return /en_US/C/C/C/C/C on the second try.
14296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * Linux can return LC_CTYPE=C;LC_NUMERIC=C;...
14306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *
14316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * The default codepage detection also needs to use LC_CTYPE.
14326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *
14336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * Do not call setlocale(LC_*, "")! Using an empty string instead
14346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        * of NULL, will modify the libc behavior.
14356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
14366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        posixID = setlocale(category, NULL);
14376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if ((posixID == 0)
14386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            || (uprv_strcmp("C", posixID) == 0)
14396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            || (uprv_strcmp("POSIX", posixID) == 0))
14406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
14416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* Maybe we got some garbage.  Try something more reasonable */
14426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            posixID = getenv("LC_ALL");
14436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (posixID == 0) {
14446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                posixID = getenv(category == LC_MESSAGES ? "LC_MESSAGES" : "LC_CTYPE");
14456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                if (posixID == 0) {
14466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                    posixID = getenv("LANG");
14476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                }
14486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
14496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
14506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
14516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if ((posixID==0)
14526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        || (uprv_strcmp("C", posixID) == 0)
14536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        || (uprv_strcmp("POSIX", posixID) == 0))
14546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
14556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Nothing worked.  Give it a nice POSIX default value. */
14566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        posixID = "en_US_POSIX";
14576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
14586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return posixID;
14596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
14606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
14616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Return just the POSIX id for the default locale, whatever happens to be in
14626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * it. It gets the value from LC_MESSAGES and indirectly from LC_ALL and LANG.
14636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
14646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char *uprv_getPOSIXIDForDefaultLocale(void)
14656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
14666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static const char* posixID = NULL;
14676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (posixID == 0) {
14686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        posixID = uprv_getPOSIXIDForCategory(LC_MESSAGES);
14696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
14706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return posixID;
14716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
14726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
14736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !U_CHARSET_IS_UTF8
14746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* Return just the POSIX id for the default codepage, whatever happens to be in
14756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * it. It gets the value from LC_CTYPE and indirectly from LC_ALL and LANG.
14766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
14776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char *uprv_getPOSIXIDForDefaultCodepage(void)
14786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
14796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static const char* posixID = NULL;
14806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (posixID == 0) {
14816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        posixID = uprv_getPOSIXIDForCategory(LC_CTYPE);
14826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
14836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return posixID;
14846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
14856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
14866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
14876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
14886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* NOTE: The caller should handle thread safety */
14896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI const char* U_EXPORT2
14906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_getDefaultLocaleID()
14916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
14926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_POSIX_LOCALE
14936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
14946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  Note that:  (a '!' means the ID is improper somehow)
14956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     LC_ALL  ---->     default_loc          codepage
14966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org--------------------------------------------------------
14976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     ab.CD             ab                   CD
14986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     ab@CD             ab__CD               -
14996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     ab@CD.EF          ab__CD               EF
15006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     ab_CD.EF@GH       ab_CD_GH             EF
15026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgSome 'improper' ways to do the same as above:
15046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  !  ab_CD@GH.EF       ab_CD_GH             EF
15056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  !  ab_CD.EF@GH.IJ    ab_CD_GH             EF
15066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  !  ab_CD@ZZ.EF@GH.IJ ab_CD_GH             EF
15076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     _CD@GH            _CD_GH               -
15096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     _CD.EF@GH         _CD_GH               EF
15106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgThe variant cannot have dots in it.
15126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgThe 'rightmost' variant (@xxx) wins.
15136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgThe leftmost codepage (.xxx) wins.
15146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*/
15156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *correctedPOSIXLocale = 0;
15166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char* posixID = uprv_getPOSIXIDForDefaultLocale();
15176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *p;
15186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *q;
15196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t len;
15206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Format: (no spaces)
15226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ll [ _CC ] [ . MM ] [ @ VV]
15236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      l = lang, C = ctry, M = charmap, V = variant
15256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
15266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gCorrectedPOSIXLocale != NULL) {
15286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return gCorrectedPOSIXLocale;
15296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
15306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if ((p = uprv_strchr(posixID, '.')) != NULL) {
15326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* assume new locale can't be larger than old one? */
15336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        correctedPOSIXLocale = static_cast<char *>(uprv_malloc(uprv_strlen(posixID)+1));
15346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Exit on memory allocation error. */
15356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (correctedPOSIXLocale == NULL) {
15366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return NULL;
15376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_strncpy(correctedPOSIXLocale, posixID, p-posixID);
15396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        correctedPOSIXLocale[p-posixID] = 0;
15406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* do not copy after the @ */
15426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if ((p = uprv_strchr(correctedPOSIXLocale, '@')) != NULL) {
15436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            correctedPOSIXLocale[p-correctedPOSIXLocale] = 0;
15446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
15466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Note that we scan the *uncorrected* ID. */
15486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if ((p = uprv_strrchr(posixID, '@')) != NULL) {
15496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (correctedPOSIXLocale == NULL) {
15506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            correctedPOSIXLocale = static_cast<char *>(uprv_malloc(uprv_strlen(posixID)+1));
15516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* Exit on memory allocation error. */
15526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if (correctedPOSIXLocale == NULL) {
15536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                return NULL;
15546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
15556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strncpy(correctedPOSIXLocale, posixID, p-posixID);
15566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            correctedPOSIXLocale[p-posixID] = 0;
15576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        p++;
15596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Take care of any special cases here.. */
15616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (!uprv_strcmp(p, "nynorsk")) {
15626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            p = "NY";
15636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* Don't worry about no__NY. In practice, it won't appear. */
15646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (uprv_strchr(correctedPOSIXLocale,'_') == NULL) {
15676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcat(correctedPOSIXLocale, "__"); /* aa@b -> aa__b */
15686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else {
15706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcat(correctedPOSIXLocale, "_"); /* aa_CC@b -> aa_CC_b */
15716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if ((q = uprv_strchr(p, '.')) != NULL) {
15746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* How big will the resulting string be? */
15756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            len = (int32_t)(uprv_strlen(correctedPOSIXLocale) + (q-p));
15766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strncat(correctedPOSIXLocale, p, q-p);
15776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            correctedPOSIXLocale[len] = 0;
15786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else {
15806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* Anything following the @ sign */
15816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcat(correctedPOSIXLocale, p);
15826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
15836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Should there be a map from 'no@nynorsk' -> no_NO_NY here?
15856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * How about 'russian' -> 'ru'?
15866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * Many of the other locales using ISO codes will be handled by the
15876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * canonicalization functions in uloc_getDefault.
15886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
15896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
15906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
15916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Was a correction made? */
15926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (correctedPOSIXLocale != NULL) {
15936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        posixID = correctedPOSIXLocale;
15946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
15956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else {
15966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* copy it, just in case the original pointer goes away.  See j2395 */
15976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        correctedPOSIXLocale = (char *)uprv_malloc(uprv_strlen(posixID) + 1);
15986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Exit on memory allocation error. */
15996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (correctedPOSIXLocale == NULL) {
16006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return NULL;
16016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
16026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        posixID = uprv_strcpy(correctedPOSIXLocale, posixID);
16036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
16046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gCorrectedPOSIXLocale == NULL) {
16066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        gCorrectedPOSIXLocale = correctedPOSIXLocale;
16076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup);
16086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        correctedPOSIXLocale = NULL;
16096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
16106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (correctedPOSIXLocale != NULL) {  /* Was already set - clean up. */
16126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_free(correctedPOSIXLocale);
16136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
16146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return posixID;
16166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM_USES_ONLY_WIN32_API
16186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define POSIX_LOCALE_CAPACITY 64
16196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    UErrorCode status = U_ZERO_ERROR;
16206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *correctedPOSIXLocale = 0;
16216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gCorrectedPOSIXLocale != NULL) {
16236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return gCorrectedPOSIXLocale;
16246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
16256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    LCID id = GetThreadLocale();
16276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    correctedPOSIXLocale = static_cast<char *>(uprv_malloc(POSIX_LOCALE_CAPACITY + 1));
16286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (correctedPOSIXLocale) {
16296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        int32_t posixLen = uprv_convertToPosix(id, correctedPOSIXLocale, POSIX_LOCALE_CAPACITY, &status);
16306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (U_SUCCESS(status)) {
16316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            *(correctedPOSIXLocale + posixLen) = 0;
16326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            gCorrectedPOSIXLocale = correctedPOSIXLocale;
16336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup);
16346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else {
16356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_free(correctedPOSIXLocale);
16366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
16376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
16386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (gCorrectedPOSIXLocale == NULL) {
16406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return "en_US";
16416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
16426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return gCorrectedPOSIXLocale;
16436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_CLASSIC_MACOS
16456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t script = MAC_LC_INIT_NUMBER;
16466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* = IntlScript(); or GetScriptManagerVariable(smSysScript);*/
16476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t region = MAC_LC_INIT_NUMBER;
16486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* = GetScriptManagerVariable(smRegionCode);*/
16496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t lang = MAC_LC_INIT_NUMBER;
16506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* = GetScriptManagerVariable(smScriptLang);*/
16516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t date_region = MAC_LC_INIT_NUMBER;
16526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char* posixID = 0;
16536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t count = sizeof(mac_lc_recs) / sizeof(mac_lc_rec);
16546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    int32_t i;
16556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    Intl1Hndl ih;
16566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    ih = (Intl1Hndl) GetIntlResource(1);
16586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (ih)
16596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        date_region = ((uint16_t)(*ih)->intl1Vers) >> 8;
16606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    for (i = 0; i < count; i++) {
16626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (   ((mac_lc_recs[i].script == MAC_LC_MAGIC_NUMBER)
16636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             || (mac_lc_recs[i].script == script))
16646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            && ((mac_lc_recs[i].region == MAC_LC_MAGIC_NUMBER)
16656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             || (mac_lc_recs[i].region == region))
16666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            && ((mac_lc_recs[i].lang == MAC_LC_MAGIC_NUMBER)
16676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             || (mac_lc_recs[i].lang == lang))
16686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            && ((mac_lc_recs[i].date_region == MAC_LC_MAGIC_NUMBER)
16696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org             || (mac_lc_recs[i].date_region == date_region))
16706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            )
16716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
16726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            posixID = mac_lc_recs[i].posixID;
16736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            break;
16746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
16756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
16766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return posixID;
16786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_OS400
16806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* locales are process scoped and are by definition thread safe */
16816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static char correctedLocale[64];
16826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const  char *localeID = getenv("LC_ALL");
16836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org           char *p;
16846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (localeID == NULL)
16866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        localeID = getenv("LANG");
16876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (localeID == NULL)
16886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        localeID = setlocale(LC_ALL, NULL);
16896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Make sure we have something... */
16906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (localeID == NULL)
16916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return "en_US_POSIX";
16926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
16936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Extract the locale name from the path. */
16946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if((p = uprv_strrchr(localeID, '/')) != NULL)
16956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
16966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Increment p to start of locale name. */
16976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        p++;
16986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        localeID = p;
16996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
17006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Copy to work location. */
17026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_strcpy(correctedLocale, localeID);
17036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Strip off the '.locale' extension. */
17056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if((p = uprv_strchr(correctedLocale, '.')) != NULL) {
17066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *p = 0;
17076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
17086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Upper case the locale name. */
17106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    T_CString_toUpperCase(correctedLocale);
17116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* See if we are using the POSIX locale.  Any of the
17136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    * following are equivalent and use the same QLGPGCMA
17146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    * (POSIX) locale.
17156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    * QLGPGCMA2 means UCS2
17166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    * QLGPGCMA_4 means UTF-32
17176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    * QLGPGCMA_8 means UTF-8
17186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
17196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if ((uprv_strcmp("C", correctedLocale) == 0) ||
17206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        (uprv_strcmp("POSIX", correctedLocale) == 0) ||
17216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        (uprv_strncmp("QLGPGCMA", correctedLocale, 8) == 0))
17226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
17236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_strcpy(correctedLocale, "en_US_POSIX");
17246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
17256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else
17266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
17276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        int16_t LocaleLen;
17286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Lower case the lang portion. */
17306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        for(p = correctedLocale; *p != 0 && *p != '_'; p++)
17316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
17326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            *p = uprv_tolower(*p);
17336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
17346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Adjust for Euro.  After '_E' add 'URO'. */
17366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        LocaleLen = uprv_strlen(correctedLocale);
17376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (correctedLocale[LocaleLen - 2] == '_' &&
17386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            correctedLocale[LocaleLen - 1] == 'E')
17396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
17406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcat(correctedLocale, "URO");
17416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
17426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* If using Lotus-based locale then convert to
17446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * equivalent non Lotus.
17456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
17466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else if (correctedLocale[LocaleLen - 2] == '_' &&
17476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            correctedLocale[LocaleLen - 1] == 'L')
17486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
17496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            correctedLocale[LocaleLen - 2] = 0;
17506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
17516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* There are separate simplified and traditional
17536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * locales called zh_HK_S and zh_HK_T.
17546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
17556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else if (uprv_strncmp(correctedLocale, "zh_HK", 5) == 0)
17566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
17576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcpy(correctedLocale, "zh_HK");
17586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
17596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* A special zh_CN_GBK locale...
17616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
17626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else if (uprv_strcmp(correctedLocale, "zh_CN_GBK") == 0)
17636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
17646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strcpy(correctedLocale, "zh_CN");
17656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
17666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
17686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return correctedLocale;
17706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
17716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
17736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
17746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if !U_CHARSET_IS_UTF8
17756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_POSIX_LOCALE
17766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
17776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgDue to various platform differences, one platform may specify a charset,
17786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgwhen they really mean a different charset. Remap the names so that they are
17796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgcompatible with ICU. Only conflicting/ambiguous aliases should be resolved
17806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orghere. Before adding anything to this function, please consider adding unique
17816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgnames to the ICU alias table in the data directory.
17826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org*/
17836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char*
17846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgremapPlatformDependentCodepage(const char *locale, const char *name) {
17856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (locale != NULL && *locale == 0) {
17866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Make sure that an empty locale is handled the same way. */
17876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        locale = NULL;
17886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
17896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (name == NULL) {
17906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return NULL;
17916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
17926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM == U_PF_AIX
17936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (uprv_strcmp(name, "IBM-943") == 0) {
17946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Use the ASCII compatible ibm-943 */
17956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "Shift-JIS";
17966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
17976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (uprv_strcmp(name, "IBM-1252") == 0) {
17986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Use the windows-1252 that contains the Euro */
17996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "IBM-5348";
18006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_SOLARIS
18026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (locale != NULL && uprv_strcmp(name, "EUC") == 0) {
18036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Solaris underspecifies the "EUC" name. */
18046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (uprv_strcmp(locale, "zh_CN") == 0) {
18056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            name = "EUC-CN";
18066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
18076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else if (uprv_strcmp(locale, "zh_TW") == 0) {
18086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            name = "EUC-TW";
18096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
18106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else if (uprv_strcmp(locale, "ko_KR") == 0) {
18116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            name = "EUC-KR";
18126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
18136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (uprv_strcmp(name, "eucJP") == 0) {
18156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
18166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-954 is the best match.
18176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-33722 is the default for eucJP (similar to Windows).
18186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
18196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "eucjis";
18206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (uprv_strcmp(name, "646") == 0) {
18226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
18236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * The default codepage given by Solaris is 646 but the C library routines treat it as if it was
18246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * ISO-8859-1 instead of US-ASCII(646).
18256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
18266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "ISO-8859-1";
18276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM_IS_DARWIN_BASED
18296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (locale == NULL && *name == 0) {
18306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
18316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        No locale was specified, and an empty name was passed in.
18326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        This usually indicates that nl_langinfo didn't return valid information.
18336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        Mac OS X uses UTF-8 by default (especially the locale data and console).
18346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
18356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "UTF-8";
18366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (uprv_strcmp(name, "CP949") == 0) {
18386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Remap CP949 to a similar codepage to avoid issues with backslash and won symbol. */
18396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "EUC-KR";
18406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (locale != NULL && uprv_strcmp(locale, "en_US_POSIX") != 0 && uprv_strcmp(name, "US-ASCII") == 0) {
18426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
18436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * For non C/POSIX locale, default the code page to UTF-8 instead of US-ASCII.
18446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
18456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "UTF-8";
18466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_BSD
18486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (uprv_strcmp(name, "CP949") == 0) {
18496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Remap CP949 to a similar codepage to avoid issues with backslash and won symbol. */
18506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "EUC-KR";
18516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_HPUX
18536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (locale != NULL && uprv_strcmp(locale, "zh_HK") == 0 && uprv_strcmp(name, "big5") == 0) {
18546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* HP decided to extend big5 as hkbig5 even though it's not compatible :-( */
18556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* zh_TW.big5 is not the same charset as zh_HK.big5! */
18566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "hkbig5";
18576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (uprv_strcmp(name, "eucJP") == 0) {
18596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
18606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-1350 is the best match, but unavailable.
18616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-954 is mostly a superset of ibm-1350.
18626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-33722 is the default for eucJP (similar to Windows).
18636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
18646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "eucjis";
18656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_LINUX
18676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (locale != NULL && uprv_strcmp(name, "euc") == 0) {
18686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Linux underspecifies the "EUC" name. */
18696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (uprv_strcmp(locale, "korean") == 0) {
18706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            name = "EUC-KR";
18716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
18726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else if (uprv_strcmp(locale, "japanese") == 0) {
18736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            /* See comment below about eucJP */
18746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            name = "eucjis";
18756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
18766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (uprv_strcmp(name, "eucjp") == 0) {
18786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
18796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-1350 is the best match, but unavailable.
18806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-954 is mostly a superset of ibm-1350.
18816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        ibm-33722 is the default for eucJP (similar to Windows).
18826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        */
18836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "eucjis";
18846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    else if (locale != NULL && uprv_strcmp(locale, "en_US_POSIX") != 0 &&
18866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            (uprv_strcmp(name, "ANSI_X3.4-1968") == 0 || uprv_strcmp(name, "US-ASCII") == 0)) {
18876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
18886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * For non C/POSIX locale, default the code page to UTF-8 instead of US-ASCII.
18896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
18906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = "UTF-8";
18916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
18926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /*
18936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * Linux returns ANSI_X3.4-1968 for C/POSIX, but the call site takes care of
18946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * it by falling back to 'US-ASCII' when NULL is returned from this
18956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     * function. So, we don't have to worry about it here.
18966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org     */
18976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
18986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* return NULL when "" is passed in */
18996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (*name == 0) {
19006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = NULL;
19016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
19026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return name;
19036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
19046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char*
19066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orggetCodepageFromPOSIXID(const char *localeName, char * buffer, int32_t buffCapacity)
19076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
19086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char localeBuf[100];
19096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *name = NULL;
19106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *variant = NULL;
19116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (localeName != NULL && (name = (uprv_strchr(localeName, '.'))) != NULL) {
19136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        size_t localeCapacity = uprv_min(sizeof(localeBuf), (name-localeName)+1);
19146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        uprv_strncpy(localeBuf, localeName, localeCapacity);
19156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        localeBuf[localeCapacity-1] = 0; /* ensure NULL termination */
19166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = uprv_strncpy(buffer, name+1, buffCapacity);
19176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        buffer[buffCapacity-1] = 0; /* ensure NULL termination */
19186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if ((variant = const_cast<char *>(uprv_strchr(name, '@'))) != NULL) {
19196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            *variant = 0;
19206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
19216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = remapPlatformDependentCodepage(localeBuf, name);
19226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
19236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return name;
19246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
19256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
19266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgstatic const char*
19286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgint_getDefaultCodepage()
19296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
19306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM == U_PF_OS400
19316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint32_t ccsid = 37; /* Default to ibm-37 */
19326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static char codepage[64];
19336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    Qwc_JOBI0400_t jobinfo;
19346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    Qus_EC_t error = { sizeof(Qus_EC_t) }; /* SPI error code */
19356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    EPT_CALL(QUSRJOBI)(&jobinfo, sizeof(jobinfo), "JOBI0400",
19376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        "*                         ", "                ", &error);
19386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (error.Bytes_Available == 0) {
19406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (jobinfo.Coded_Char_Set_ID != 0xFFFF) {
19416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ccsid = (uint32_t)jobinfo.Coded_Char_Set_ID;
19426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
19436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        else if (jobinfo.Default_Coded_Char_Set_Id != 0xFFFF) {
19446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            ccsid = (uint32_t)jobinfo.Default_Coded_Char_Set_Id;
19456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
19466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* else use the default */
19476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
19486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    sprintf(codepage,"ibm-%d", ccsid);
19496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return codepage;
19506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_OS390
19526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static char codepage[64];
19536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    strncpy(codepage, nl_langinfo(CODESET),63-strlen(UCNV_SWAP_LFNL_OPTION_STRING));
19556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    strcat(codepage,UCNV_SWAP_LFNL_OPTION_STRING);
19566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    codepage[63] = 0; /* NULL terminate */
19576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return codepage;
19596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM == U_PF_CLASSIC_MACOS
19616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return "macintosh"; /* TODO: Macintosh Roman. There must be a better way. fixme! */
19626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM_USES_ONLY_WIN32_API
19646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static char codepage[64];
19656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    sprintf(codepage, "windows-%d", GetACP());
19666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return codepage;
19676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_POSIX_LOCALE
19696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static char codesetName[100];
19706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *localeName = NULL;
19716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    const char *name = NULL;
19726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    localeName = uprv_getPOSIXIDForDefaultCodepage();
19746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_memset(codesetName, 0, sizeof(codesetName));
19756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_HAVE_NL_LANGINFO_CODESET
19766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* When available, check nl_langinfo first because it usually gives more
19776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       useful names. It depends on LC_CTYPE.
19786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       nl_langinfo may use the same buffer as setlocale. */
19796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
19806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        const char *codeset = nl_langinfo(U_NL_LANGINFO_CODESET);
19816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_PLATFORM_IS_DARWIN_BASED || U_PLATFORM_IS_LINUX_BASED
19826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /*
19836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * On Linux and MacOSX, ensure that default codepage for non C/POSIX locale is UTF-8
19846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         * instead of ASCII.
19856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org         */
19866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (uprv_strcmp(localeName, "en_US_POSIX") != 0) {
19876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            codeset = remapPlatformDependentCodepage(localeName, codeset);
19886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        } else
19896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
19906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        {
19916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            codeset = remapPlatformDependentCodepage(NULL, codeset);
19926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
19936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
19946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if (codeset != NULL) {
19956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            uprv_strncpy(codesetName, codeset, sizeof(codesetName));
19966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            codesetName[sizeof(codesetName)-1] = 0;
19976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            return codesetName;
19986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
19996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
20016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* Use setlocale in a nice way, and then check some environment variables.
20036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org       Maybe the application used setlocale already.
20046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    */
20056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uprv_memset(codesetName, 0, sizeof(codesetName));
20066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    name = getCodepageFromPOSIXID(localeName, codesetName, sizeof(codesetName));
20076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (name) {
20086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* if we can find the codeset name from setlocale, return that. */
20096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return name;
20106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (*codesetName == 0)
20136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    {
20146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* Everything failed. Return US ASCII (ISO 646). */
20156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        (void)uprv_strcpy(codesetName, "US-ASCII");
20166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return codesetName;
20186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
20196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return "US-ASCII";
20206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
20216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
20226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI const char*  U_EXPORT2
20256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_getDefaultCodepage()
20266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org{
20276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    static char const  *name = NULL;
20286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    umtx_lock(NULL);
20296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if (name == NULL) {
20306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        name = int_getDefaultCodepage();
20316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    umtx_unlock(NULL);
20336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return name;
20346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
20356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif  /* !U_CHARSET_IS_UTF8 */
20366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* end of platform-specific implementation -------------- */
20396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* version handling --------------------------------------------------------- */
20416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI void U_EXPORT2
20436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_versionFromString(UVersionInfo versionArray, const char *versionString) {
20446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    char *end;
20456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint16_t part=0;
20466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(versionArray==NULL) {
20486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return;
20496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(versionString!=NULL) {
20526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        for(;;) {
20536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            versionArray[part]=(uint8_t)uprv_strtoul(versionString, &end, 10);
20546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            if(end==versionString || ++part==U_MAX_VERSION_LENGTH || *end!=U_VERSION_DELIMITER) {
20556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org                break;
20566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            }
20576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            versionString=end+1;
20586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
20596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    while(part<U_MAX_VERSION_LENGTH) {
20626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        versionArray[part++]=0;
20636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
20656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI void U_EXPORT2
20676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_versionFromUString(UVersionInfo versionArray, const UChar *versionString) {
20686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(versionArray!=NULL && versionString!=NULL) {
20696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        char versionChars[U_MAX_VERSION_STRING_LENGTH+1];
20706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        int32_t len = u_strlen(versionString);
20716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(len>U_MAX_VERSION_STRING_LENGTH) {
20726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            len = U_MAX_VERSION_STRING_LENGTH;
20736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
20746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        u_UCharsToChars(versionString, versionChars, len);
20756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        versionChars[len]=0;
20766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        u_versionFromString(versionArray, versionChars);
20776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
20796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI void U_EXPORT2
20816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_versionToString(const UVersionInfo versionArray, char *versionString) {
20826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint16_t count, part;
20836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    uint8_t field;
20846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(versionString==NULL) {
20866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return;
20876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(versionArray==NULL) {
20906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        versionString[0]=0;
20916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        return;
20926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* count how many fields need to be written */
20956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    for(count=4; count>0 && versionArray[count-1]==0; --count) {
20966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
20976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
20986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(count <= 1) {
20996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        count = 2;
21006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
21016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* write the first part */
21036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* write the decimal field value */
21046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    field=versionArray[0];
21056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(field>=100) {
21066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *versionString++=(char)('0'+field/100);
21076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        field%=100;
21086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
21096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(field>=10) {
21106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *versionString++=(char)('0'+field/10);
21116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        field%=10;
21126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
21136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *versionString++=(char)('0'+field);
21146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* write the following parts */
21166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    for(part=1; part<count; ++part) {
21176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* write a dot first */
21186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *versionString++=U_VERSION_DELIMITER;
21196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        /* write the decimal field value */
21216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        field=versionArray[part];
21226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(field>=100) {
21236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            *versionString++=(char)('0'+field/100);
21246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            field%=100;
21256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
21266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        if(field>=10) {
21276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            *versionString++=(char)('0'+field/10);
21286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org            field%=10;
21296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        }
21306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org        *versionString++=(char)('0'+field);
21316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
21326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    /* NUL-terminate */
21346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *versionString=0;
21356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
21366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_CAPI void U_EXPORT2
21386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgu_getVersion(UVersionInfo versionArray) {
21396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    u_versionFromString(versionArray, U_ICU_VERSION);
21406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
21416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/**
21436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * icucfg.h dependent code
21446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
21456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if U_ENABLE_DYLOAD
21476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if HAVE_DLOPEN && !U_PLATFORM_USES_ONLY_WIN32_API
21496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#if HAVE_DLFCN_H
21516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef __MVS__
21536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifndef __SUSV3
21546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#define __SUSV3 1
21556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
21566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
21576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#include <dlfcn.h>
21586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
21596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void * U_EXPORT2
21616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_open(const char *libName, UErrorCode *status) {
21626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  void *ret = NULL;
21636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status)) return ret;
21646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  ret =  dlopen(libName, RTLD_NOW|RTLD_GLOBAL);
21656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(ret==NULL) {
21666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef U_TRACE_DYLOAD
21676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    printf("dlerror on dlopen(%s): %s\n", libName, dlerror());
21686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
21696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *status = U_MISSING_RESOURCE_ERROR;
21706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
21716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return ret;
21726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
21736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void U_EXPORT2
21756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_close(void *lib, UErrorCode *status) {
21766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status)) return;
21776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  dlclose(lib);
21786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
21796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL UVoidFunction* U_EXPORT2
21816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
21826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  union {
21836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      UVoidFunction *fp;
21846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      void *vp;
21856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  } uret;
21866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  uret.fp = NULL;
21876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status)) return uret.fp;
21886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  uret.vp = dlsym(lib, sym);
21896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(uret.vp == NULL) {
21906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#ifdef U_TRACE_DYLOAD
21916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    printf("dlerror on dlsym(%p,%s): %s\n", lib,sym, dlerror());
21926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
21936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *status = U_MISSING_RESOURCE_ERROR;
21946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
21956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return uret.fp;
21966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
21976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
21986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
21996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* null (nonexistent) implementation. */
22016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void * U_EXPORT2
22036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_open(const char *libName, UErrorCode *status) {
22046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status)) return NULL;
22056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  *status = U_UNSUPPORTED_ERROR;
22066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return NULL;
22076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void U_EXPORT2
22106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_close(void *lib, UErrorCode *status) {
22116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status)) return;
22126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  *status = U_UNSUPPORTED_ERROR;
22136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return;
22146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22166f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22176f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL UVoidFunction* U_EXPORT2
22186f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
22196f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_SUCCESS(*status)) {
22206f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *status = U_UNSUPPORTED_ERROR;
22216f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
22226f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return (UVoidFunction*)NULL;
22236f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22246f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22256f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22266f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22276f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif
22286f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22296f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#elif U_PLATFORM_USES_ONLY_WIN32_API
22306f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22316f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void * U_EXPORT2
22326f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_open(const char *libName, UErrorCode *status) {
22336f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  HMODULE lib = NULL;
22346f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22356f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status)) return NULL;
22366f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22376f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  lib = LoadLibraryA(libName);
22386f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22396f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(lib==NULL) {
22406f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *status = U_MISSING_RESOURCE_ERROR;
22416f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
22426f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22436f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return (void*)lib;
22446f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22456f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22466f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void U_EXPORT2
22476f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_close(void *lib, UErrorCode *status) {
22486f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  HMODULE handle = (HMODULE)lib;
22496f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status)) return;
22506f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22516f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  FreeLibrary(handle);
22526f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22536f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return;
22546f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22556f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22566f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22576f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL UVoidFunction* U_EXPORT2
22586f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
22596f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  HMODULE handle = (HMODULE)lib;
22606f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  UVoidFunction* addr = NULL;
22616f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22626f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_FAILURE(*status) || lib==NULL) return NULL;
22636f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22646f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  addr = (UVoidFunction*)GetProcAddress(handle, sym);
22656f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22666f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(addr==NULL) {
22676f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    DWORD lastError = GetLastError();
22686f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(lastError == ERROR_PROC_NOT_FOUND) {
22696f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      *status = U_MISSING_RESOURCE_ERROR;
22706f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    } else {
22716f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org      *status = U_UNSUPPORTED_ERROR; /* other unknown error. */
22726f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    }
22736f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
22746f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22756f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return addr;
22766f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22776f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22786f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22796f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#else
22806f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22816f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/* No dynamic loading set. */
22826f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22836f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void * U_EXPORT2
22846f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_open(const char *libName, UErrorCode *status) {
22856f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(U_FAILURE(*status)) return NULL;
22866f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *status = U_UNSUPPORTED_ERROR;
22876f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return NULL;
22886f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22896f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22906f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL void U_EXPORT2
22916f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dl_close(void *lib, UErrorCode *status) {
22926f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    if(U_FAILURE(*status)) return;
22936f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *status = U_UNSUPPORTED_ERROR;
22946f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    return;
22956f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
22966f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22976f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
22986f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orgU_INTERNAL UVoidFunction* U_EXPORT2
22996f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.orguprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
23006f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  if(U_SUCCESS(*status)) {
23016f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org    *status = U_UNSUPPORTED_ERROR;
23026f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  }
23036f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org  return (UVoidFunction*)NULL;
23046f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org}
23056f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
23066f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org#endif /* U_ENABLE_DYLOAD */
23076f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org
23086f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org/*
23096f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Hey, Emacs, please set the following:
23106f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *
23116f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * Local Variables:
23126f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * indent-tabs-mode: nil
23136f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org * End:
23146f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org *
23156f31ac30b9092fd02a8c97e5216cf53f3e4fae4jshin@chromium.org */
2316