putil.cpp revision 8de051c3d18a56cc126f0f44e368495a52f9148c
1b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
2b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru******************************************************************************
3b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*
48de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert*   Copyright (C) 1997-2015, International Business Machines
5b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   Corporation and others.  All Rights Reserved.
6b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*
7b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru******************************************************************************
8b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*
9b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*  FILE NAME : putil.c (previously putil.cpp and ptypes.cpp)
10b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*
11b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   Date        Name        Description
12b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   04/14/97    aliu        Creation.
13b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   04/24/97    aliu        Added getDefaultDataDirectory() and
14b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                            getDefaultLocaleID().
15b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   04/28/97    aliu        Rewritten to assume Unix and apply general methods
16b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                            for assumed case.  Non-UNIX platforms must be
17b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                            special-cased.  Rewrote numeric methods dealing
18b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                            with NaN and Infinity to be platform independent
19b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                             over all IEEE 754 platforms.
20b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   05/13/97    aliu        Restored sign of timezone
21b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                            (semantics are hours West of GMT)
22b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   06/16/98    erm         Added IEEE_754 stuff, cleaned up isInfinite, isNan,
23b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                             nextDouble..
24b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   07/22/98    stephen     Added remainder, max, min, trunc
25b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   08/13/98    stephen     Added isNegativeInfinity, isPositiveInfinity
26b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   08/24/98    stephen     Added longBitsFromDouble
27b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   09/08/98    stephen     Minor changes for Mac Port
28b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   03/02/99    stephen     Removed openFile().  Added AS400 support.
29b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*                            Fixed EBCDIC tables
30b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   04/15/99    stephen     Converted to C.
31b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   06/28/99    stephen     Removed mutex locking in u_isBigEndian().
32b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   08/04/99    jeffrey R.  Added OS/2 changes
33b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   11/15/99    helena      Integrated S/390 IEEE support.
34b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   04/26/01    Barry N.    OS/400 support for uprv_getDefaultLocaleID
35b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*   08/15/01    Steven H.   OS/400 support for uprv_getDefaultCodepage
3685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho*   01/03/08    Steven L.   Fake Time Support
37b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru******************************************************************************
38b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*/
39b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
40103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius// Defines _XOPEN_SOURCE for access to POSIX functions.
41103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius// Must be before any other #includes.
42103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#include "uposixdefs.h"
43b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
44b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* include ICU headers */
45b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "unicode/utypes.h"
46b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "unicode/putil.h"
47b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "unicode/ustring.h"
48b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "putilimp.h"
49b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "uassert.h"
50b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "umutex.h"
51b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "cmemory.h"
52b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "cstring.h"
53b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "locmap.h"
54b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include "ucln_cmn.h"
55f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#include "charstr.h"
56b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
57b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Include standard headers. */
58b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <stdio.h>
59b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <stdlib.h>
60b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <string.h>
61b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <math.h>
62b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <locale.h>
63b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <float.h>
64b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
65103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#ifndef U_COMMON_IMPLEMENTATION
66103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#error U_COMMON_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see http://userguide.icu-project.org/howtouseicu
67103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#endif
68103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius
69103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius
70b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* include system headers */
71103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_USES_ONLY_WIN32_API
72103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius    /*
73103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius     * TODO: U_PLATFORM_USES_ONLY_WIN32_API includes MinGW.
74103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius     * Should Cygwin be included as well (U_PLATFORM_HAS_WIN32_API)
75103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius     * to use native APIs as much as possible?
76103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius     */
77b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   define WIN32_LEAN_AND_MEAN
78b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   define VC_EXTRALEAN
79b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   define NOUSER
80b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   define NOSERVICE
81b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   define NOIME
82b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   define NOMCX
83b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   include <windows.h>
84b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   include "wintz.h"
85103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_OS400
86b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   include <float.h>
87b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   include <qusec.h>       /* error code structure */
88b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   include <qusrjobi.h>
89b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   include <qliept.h>      /* EPT_CALL macro  - this include must be after all other "QSYSINCs" */
90b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   include <mih/testptr.h> /* For uprv_maximumPtr */
91103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_OS390
92b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#   include "unicode/ucnv.h"   /* Needed for UCNV_SWAP_LFNL_OPTION_STRING */
938393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#elif U_PLATFORM_IS_DARWIN_BASED || U_PLATFORM_IS_LINUX_BASED || U_PLATFORM == U_PF_BSD || U_PLATFORM == U_PF_SOLARIS
94b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#   include <limits.h>
95b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#   include <unistd.h>
968393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#   if U_PLATFORM == U_PF_SOLARIS
978393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#       ifndef _XPG4_2
988393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#           define _XPG4_2
998393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#       endif
1008393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#   endif
101103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_QNX
102b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#   include <sys/neutrino.h>
10350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
10450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
105103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if (U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(__STRICT_ANSI__)
106b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho/* tzset isn't defined in strict ANSI on Cygwin and MinGW. */
107b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#undef __STRICT_ANSI__
108b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#endif
109b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho
110b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho/*
111b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho * Cygwin with GCC requires inclusion of time.h after the above disabling strict asci mode statement.
112b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho */
113b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#include <time.h>
11450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
115103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if !U_PLATFORM_USES_ONLY_WIN32_API
11685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include <sys/time.h>
117b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
118b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
119b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
120b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * Only include langinfo.h if we have a way to get the codeset. If we later
121b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * depend on more feature, we can test on U_HAVE_NL_LANGINFO.
122b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru *
123b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru */
124b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
125b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if U_HAVE_NL_LANGINFO_CODESET
126b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <langinfo.h>
127b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
128b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
12950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho/**
13050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho * Simple things (presence of functions, etc) should just go in configure.in and be added to
13150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho * icucfg.h via autoheader.
13250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho */
133103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_IMPLEMENTS_POSIX
134103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   if U_PLATFORM == U_PF_OS400
135103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#    define HAVE_DLFCN_H 0
136103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#    define HAVE_DLOPEN 0
137103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   else
138103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   ifndef HAVE_DLFCN_H
139103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#    define HAVE_DLFCN_H 1
140103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   endif
141103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   ifndef HAVE_DLOPEN
142103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#    define HAVE_DLOPEN 1
143103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   endif
144103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   endif
145103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   ifndef HAVE_GETTIMEOFDAY
146103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#    define HAVE_GETTIMEOFDAY 1
147103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   endif
148103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#else
149103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   define HAVE_DLFCN_H 0
150103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   define HAVE_DLOPEN 0
151103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#   define HAVE_GETTIMEOFDAY 0
15250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
15350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
154f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusU_NAMESPACE_USE
155103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius
156b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Define the extension for data files, again... */
157b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define DATA_TYPE "dat"
158b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
159b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Leave this copyright notice here! */
160b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic const char copyright[] = U_COPYRIGHT_STRING;
161b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
162b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* floating point implementations ------------------------------------------- */
163b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
164b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* We return QNAN rather than SNAN*/
165b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define SIGN 0x80000000U
166b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
167b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Make it easy to define certain types of constants */
168b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querutypedef union {
169b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int64_t i64; /* This must be defined first in order to allow the initialization to work. This is a C89 feature. */
170b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    double d64;
171b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru} BitPatternConversion;
172b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic const BitPatternConversion gNan = { (int64_t) INT64_C(0x7FF8000000000000) };
173b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic const BitPatternConversion gInf = { (int64_t) INT64_C(0x7FF0000000000000) };
174b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
175b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*---------------------------------------------------------------------------
176b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  Platform utilities
177b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  Our general strategy is to assume we're on a POSIX platform.  Platforms which
178b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  are non-POSIX must declare themselves so.  The default POSIX implementation
179b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  will sometimes work for non-POSIX platforms as well (e.g., the NaN-related
180b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  functions).
181b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  ---------------------------------------------------------------------------*/
182b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
183f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#if U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_OS400
184b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   undef U_POSIX_LOCALE
185b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
186b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   define U_POSIX_LOCALE    1
187b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
188b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
189b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
190b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    WARNING! u_topNBytesOfDouble and u_bottomNBytesOfDouble
191b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    can't be properly optimized by the gcc compiler sometimes (i.e. gcc 3.2).
192b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*/
193b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if !IEEE_754
194b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic char*
195b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruu_topNBytesOfDouble(double* d, int n)
196b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
197b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if U_IS_BIG_ENDIAN
198b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (char*)d;
199b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
200b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (char*)(d + 1) - n;
201b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
202b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
203b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
204b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic char*
205b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruu_bottomNBytesOfDouble(double* d, int n)
206b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
207b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if U_IS_BIG_ENDIAN
208b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (char*)(d + 1) - n;
209b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
210b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (char*)d;
211b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
212b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
21327f654740f2a26ad62a5c155af9199af9e69b889claireho#endif   /* !IEEE_754 */
21427f654740f2a26ad62a5c155af9199af9e69b889claireho
21527f654740f2a26ad62a5c155af9199af9e69b889claireho#if IEEE_754
21627f654740f2a26ad62a5c155af9199af9e69b889clairehostatic UBool
21727f654740f2a26ad62a5c155af9199af9e69b889clairehou_signBit(double d) {
21827f654740f2a26ad62a5c155af9199af9e69b889claireho    uint8_t hiByte;
21927f654740f2a26ad62a5c155af9199af9e69b889claireho#if U_IS_BIG_ENDIAN
22027f654740f2a26ad62a5c155af9199af9e69b889claireho    hiByte = *(uint8_t *)&d;
22127f654740f2a26ad62a5c155af9199af9e69b889claireho#else
22227f654740f2a26ad62a5c155af9199af9e69b889claireho    hiByte = *(((uint8_t *)&d) + sizeof(double) - 1);
22327f654740f2a26ad62a5c155af9199af9e69b889claireho#endif
22427f654740f2a26ad62a5c155af9199af9e69b889claireho    return (hiByte & 0x80) != 0;
22527f654740f2a26ad62a5c155af9199af9e69b889claireho}
22627f654740f2a26ad62a5c155af9199af9e69b889claireho#endif
22727f654740f2a26ad62a5c155af9199af9e69b889claireho
22827f654740f2a26ad62a5c155af9199af9e69b889claireho
229b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
23085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if defined (U_DEBUG_FAKETIME)
23185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/* Override the clock to test things without having to move the system clock.
23285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * Assumes POSIX gettimeofday() will function
23385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
23485bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUDate fakeClock_t0 = 0; /** Time to start the clock from **/
23585bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUDate fakeClock_dt = 0; /** Offset (fake time - real time) **/
23685bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoUBool fakeClock_set = FALSE; /** True if fake clock has spun up **/
23754dcd9b6a06071f647dac967e9e267abb9410720Craig Corneliusstatic UMutex fakeClockMutex = U_MUTEX_INTIALIZER;
23885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
23985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic UDate getUTCtime_real() {
24085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    struct timeval posixTime;
24185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    gettimeofday(&posixTime, NULL);
24285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return (UDate)(((int64_t)posixTime.tv_sec * U_MILLIS_PER_SECOND) + (posixTime.tv_usec/1000));
24385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
24485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
24585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic UDate getUTCtime_fake() {
24685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    umtx_lock(&fakeClockMutex);
24785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if(!fakeClock_set) {
24885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        UDate real = getUTCtime_real();
24985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        const char *fake_start = getenv("U_FAKETIME_START");
25027f654740f2a26ad62a5c155af9199af9e69b889claireho        if((fake_start!=NULL) && (fake_start[0]!=0)) {
25185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            sscanf(fake_start,"%lf",&fakeClock_t0);
25227f654740f2a26ad62a5c155af9199af9e69b889claireho            fakeClock_dt = fakeClock_t0 - real;
25327f654740f2a26ad62a5c155af9199af9e69b889claireho            fprintf(stderr,"U_DEBUG_FAKETIME was set at compile time, so the ICU clock will start at a preset value\n"
25427f654740f2a26ad62a5c155af9199af9e69b889claireho                    "env variable U_FAKETIME_START=%.0f (%s) for an offset of %.0f ms from the current time %.0f\n",
25527f654740f2a26ad62a5c155af9199af9e69b889claireho                    fakeClock_t0, fake_start, fakeClock_dt, real);
25627f654740f2a26ad62a5c155af9199af9e69b889claireho        } else {
25727f654740f2a26ad62a5c155af9199af9e69b889claireho          fakeClock_dt = 0;
25827f654740f2a26ad62a5c155af9199af9e69b889claireho            fprintf(stderr,"U_DEBUG_FAKETIME was set at compile time, but U_FAKETIME_START was not set.\n"
25927f654740f2a26ad62a5c155af9199af9e69b889claireho                    "Set U_FAKETIME_START to the number of milliseconds since 1/1/1970 to set the ICU clock.\n");
26085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
26185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        fakeClock_set = TRUE;
26285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
26385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    umtx_unlock(&fakeClockMutex);
26485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
26585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return getUTCtime_real() + fakeClock_dt;
26685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
26785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
26885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
269103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_USES_ONLY_WIN32_API
270b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querutypedef union {
271b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int64_t int64;
272b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    FILETIME fileTime;
273b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru} FileTimeConversion;   /* This is like a ULARGE_INTEGER */
274b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
275b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Number of 100 nanoseconds from 1/1/1601 to 1/1/1970 */
276b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define EPOCH_BIAS  INT64_C(116444736000000000)
277b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define HECTONANOSECOND_PER_MILLISECOND   10000
278b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
279b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
280b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
281b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*---------------------------------------------------------------------------
282b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  Universal Implementations
283b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  These are designed to work on all platforms.  Try these, and if they
284b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  don't work on your platform, then special case your platform with new
285b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  implementations.
286b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru---------------------------------------------------------------------------*/
287b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
288b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI UDate U_EXPORT2
289b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_getUTCtime()
290b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
29185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if defined(U_DEBUG_FAKETIME)
29285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return getUTCtime_fake(); /* Hook for overriding the clock */
29327f654740f2a26ad62a5c155af9199af9e69b889claireho#else
29427f654740f2a26ad62a5c155af9199af9e69b889claireho    return uprv_getRawUTCtime();
29527f654740f2a26ad62a5c155af9199af9e69b889claireho#endif
29627f654740f2a26ad62a5c155af9199af9e69b889claireho}
29727f654740f2a26ad62a5c155af9199af9e69b889claireho
29827f654740f2a26ad62a5c155af9199af9e69b889claireho/* Return UTC (GMT) time measured in milliseconds since 0:00 on 1/1/70.*/
29927f654740f2a26ad62a5c155af9199af9e69b889clairehoU_CAPI UDate U_EXPORT2
30027f654740f2a26ad62a5c155af9199af9e69b889clairehouprv_getRawUTCtime()
30127f654740f2a26ad62a5c155af9199af9e69b889claireho{
302f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#if U_PLATFORM_USES_ONLY_WIN32_API
303b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
304b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    FileTimeConversion winTime;
305b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    GetSystemTimeAsFileTime(&winTime.fileTime);
306b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UDate)((winTime.int64 - EPOCH_BIAS) / HECTONANOSECOND_PER_MILLISECOND);
307b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
30850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
309103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if HAVE_GETTIMEOFDAY
310b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    struct timeval posixTime;
311b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    gettimeofday(&posixTime, NULL);
312b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UDate)(((int64_t)posixTime.tv_sec * U_MILLIS_PER_SECOND) + (posixTime.tv_usec/1000));
31350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#else
314b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    time_t epochtime;
315b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    time(&epochtime);
316b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UDate)epochtime * U_MILLIS_PER_SECOND;
317b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
31850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
31950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
320b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
321b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
322b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*-----------------------------------------------------------------------------
323b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  IEEE 754
324b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  These methods detect and return NaN and infinity values for doubles
325b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  conforming to IEEE 754.  Platforms which support this standard include X86,
326b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  Mac 680x0, Mac PowerPC, AIX RS/6000, and most others.
327b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  If this doesn't work on your platform, you have non-IEEE floating-point, and
328b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  will need to code your own versions.  A naive implementation is to return 0.0
329b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  for getNaN and getInfinity, and false for isNaN and isInfinite.
330b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  ---------------------------------------------------------------------------*/
331b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
332b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI UBool U_EXPORT2
333b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_isNaN(double number)
334b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
335b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if IEEE_754
336b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    BitPatternConversion convertedNumber;
337b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    convertedNumber.d64 = number;
338b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Infinity is 0x7FF0000000000000U. Anything greater than that is a NaN */
339b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UBool)((convertedNumber.i64 & U_INT64_MAX) > gInf.i64);
340b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
341103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_OS390
342b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint32_t highBits = *(uint32_t*)u_topNBytesOfDouble(&number,
343b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                        sizeof(uint32_t));
344b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint32_t lowBits  = *(uint32_t*)u_bottomNBytesOfDouble(&number,
345b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                        sizeof(uint32_t));
346b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
347b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return ((highBits & 0x7F080000L) == 0x7F080000L) &&
348b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru      (lowBits == 0x00000000L);
349b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
350b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
351b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* If your platform doesn't support IEEE 754 but *does* have an NaN value,*/
352b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* you'll need to replace this default implementation with what's correct*/
353b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* for your platform.*/
354b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return number != number;
355b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
356b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
357b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
358b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI UBool U_EXPORT2
359b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_isInfinite(double number)
360b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
361b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if IEEE_754
362b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    BitPatternConversion convertedNumber;
363b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    convertedNumber.d64 = number;
364b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Infinity is exactly 0x7FF0000000000000U. */
365b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UBool)((convertedNumber.i64 & U_INT64_MAX) == gInf.i64);
366103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_OS390
367b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint32_t highBits = *(uint32_t*)u_topNBytesOfDouble(&number,
368b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                        sizeof(uint32_t));
369b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint32_t lowBits  = *(uint32_t*)u_bottomNBytesOfDouble(&number,
370b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                        sizeof(uint32_t));
371b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
372b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return ((highBits  & ~SIGN) == 0x70FF0000L) && (lowBits == 0x00000000L);
373b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
374b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
375b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* If your platform doesn't support IEEE 754 but *does* have an infinity*/
376b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* value, you'll need to replace this default implementation with what's*/
377b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* correct for your platform.*/
378b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return number == (2.0 * number);
379b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
380b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
381b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
382b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI UBool U_EXPORT2
383b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_isPositiveInfinity(double number)
384b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
385103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if IEEE_754 || U_PLATFORM == U_PF_OS390
386b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UBool)(number > 0 && uprv_isInfinite(number));
387b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
388b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return uprv_isInfinite(number);
389b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
390b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
391b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
392b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI UBool U_EXPORT2
393b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_isNegativeInfinity(double number)
394b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
395103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if IEEE_754 || U_PLATFORM == U_PF_OS390
396b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UBool)(number < 0 && uprv_isInfinite(number));
397b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
398b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
399b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint32_t highBits = *(uint32_t*)u_topNBytesOfDouble(&number,
400b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                        sizeof(uint32_t));
401b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return((highBits & SIGN) && uprv_isInfinite(number));
402b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
403b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
404b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
405b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
406b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
407b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_getNaN()
408b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
409103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if IEEE_754 || U_PLATFORM == U_PF_OS390
410b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return gNan.d64;
411b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
412b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* If your platform doesn't support IEEE 754 but *does* have an NaN value,*/
413b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* you'll need to replace this default implementation with what's correct*/
414b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* for your platform.*/
415b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return 0.0;
416b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
417b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
418b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
419b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
420b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_getInfinity()
421b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
422103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if IEEE_754 || U_PLATFORM == U_PF_OS390
423b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return gInf.d64;
424b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
425b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* If your platform doesn't support IEEE 754 but *does* have an infinity*/
426b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* value, you'll need to replace this default implementation with what's*/
427b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* correct for your platform.*/
428b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return 0.0;
429b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
430b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
431b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
432b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
433b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_floor(double x)
434b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
435b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return floor(x);
436b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
437b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
438b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
439b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_ceil(double x)
440b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
441b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return ceil(x);
442b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
443b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
444b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
445b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_round(double x)
446b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
447b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return uprv_floor(x + 0.5);
448b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
449b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
450b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
451b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_fabs(double x)
452b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
453b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return fabs(x);
454b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
455b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
456b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
457b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_modf(double x, double* y)
458b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
459b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return modf(x, y);
460b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
461b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
462b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
463b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_fmod(double x, double y)
464b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
465b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return fmod(x, y);
466b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
467b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
468b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
469b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_pow(double x, double y)
470b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
471b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* This is declared as "double pow(double x, double y)" */
472b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return pow(x, y);
473b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
474b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
475b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
476b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_pow10(int32_t x)
477b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
478b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return pow(10.0, (double)x);
479b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
480b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
481b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
482b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_fmax(double x, double y)
483b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
484b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if IEEE_754
485b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* first handle NaN*/
486b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(uprv_isNaN(x) || uprv_isNaN(y))
487b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return uprv_getNaN();
488b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
489b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* check for -0 and 0*/
49027f654740f2a26ad62a5c155af9199af9e69b889claireho    if(x == 0.0 && y == 0.0 && u_signBit(x))
491b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return y;
492b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
493b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
494b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
49527f654740f2a26ad62a5c155af9199af9e69b889claireho    /* this should work for all flt point w/o NaN and Inf special cases */
496b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (x > y ? x : y);
497b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
498b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
499b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
500b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_fmin(double x, double y)
501b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
502b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if IEEE_754
503b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* first handle NaN*/
504b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(uprv_isNaN(x) || uprv_isNaN(y))
505b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return uprv_getNaN();
506b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
507b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* check for -0 and 0*/
50827f654740f2a26ad62a5c155af9199af9e69b889claireho    if(x == 0.0 && y == 0.0 && u_signBit(y))
509b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return y;
510b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
511b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
512b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
513b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* this should work for all flt point w/o NaN and Inf special cases */
514b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (x > y ? y : x);
515b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
516b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
517b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/**
518b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * Truncates the given double.
519b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * trunc(3.3) = 3.0, trunc (-3.3) = -3.0
520b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * This is different than calling floor() or ceil():
521b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * floor(3.3) = 3, floor(-3.3) = -4
522b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * ceil(3.3) = 4, ceil(-3.3) = -3
523b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru */
524b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
525b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_trunc(double d)
526b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
527b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if IEEE_754
528b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* handle error cases*/
529b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(uprv_isNaN(d))
530b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return uprv_getNaN();
531b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(uprv_isInfinite(d))
532b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return uprv_getInfinity();
533b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
53427f654740f2a26ad62a5c155af9199af9e69b889claireho    if(u_signBit(d))    /* Signbit() picks up -0.0;  d<0 does not. */
535b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return ceil(d);
536b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else
537b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return floor(d);
538b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
539b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
540b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return d >= 0 ? floor(d) : ceil(d);
541b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
542b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
543b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
544b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
545b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/**
546b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * Return the largest positive number that can be represented by an integer
547b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * type of arbitrary bit length.
548b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru */
549b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
550b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_maxMantissa(void)
551b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
552b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return pow(2.0, DBL_MANT_DIG + 1.0) - 1.0;
553b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
554b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
555b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI double U_EXPORT2
556b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_log(double d)
557b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
558b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return log(d);
559b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
560b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
561b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI void * U_EXPORT2
562b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_maximumPtr(void * base)
563b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
564103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM == U_PF_OS400
565b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /*
56685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * With the provided function we should never be out of range of a given segment
567b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * (a traditional/typical segment that is).  Our segments have 5 bytes for the
568b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * id and 3 bytes for the offset.  The key is that the casting takes care of
569b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * only retrieving the offset portion minus x1000.  Hence, the smallest offset
570b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * seen in a program is x001000 and when casted to an int would be 0.
571b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * That's why we can only add 0xffefff.  Otherwise, we would exceed the segment.
572b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     *
57385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * Currently, 16MB is the current addressing limitation on i5/OS if the activation is
574b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * non-TERASPACE.  If it is TERASPACE it is 2GB - 4k(header information).
57585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * This function determines the activation based on the pointer that is passed in and
57685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho     * calculates the appropriate maximum available size for
577b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * each pointer type (TERASPACE and non-TERASPACE)
578b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     *
579b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * Unlike other operating systems, the pointer model isn't determined at
580b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     * compile time on i5/OS.
581b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     */
582b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if ((base != NULL) && (_TESTPTR(base, _C_TERASPACE_CHECK))) {
583b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* if it is a TERASPACE pointer the max is 2GB - 4k */
584b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return ((void *)(((char *)base)-((uint32_t)(base))+((uint32_t)0x7fffefff)));
585b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
586b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* otherwise 16MB since NULL ptr is not checkable or the ptr is not TERASPACE */
587b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return ((void *)(((char *)base)-((uint32_t)(base))+((uint32_t)0xffefff)));
588b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
589b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
59085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return U_MAX_PTR(base);
591b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
592b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
593b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
594b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*---------------------------------------------------------------------------
595b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  Platform-specific Implementations
596b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  Try these, and if they don't work on your platform, then special case your
597b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  platform with new implementations.
598b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  ---------------------------------------------------------------------------*/
599b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
600b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Generic time zone layer -------------------------------------------------- */
601b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
602b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Time zone utilities */
603b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI void U_EXPORT2
604b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_tzset()
605b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
606b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho#if defined(U_TZSET)
607b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    U_TZSET();
608b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
609b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* no initialization*/
610b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
611b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
612b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
613b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI int32_t U_EXPORT2
614b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_timezone()
615b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
616b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#ifdef U_TIMEZONE
617b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return U_TIMEZONE;
618b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
619b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    time_t t, t1, t2;
620b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    struct tm tmrec;
621b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int32_t tdiff = 0;
622b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
623b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    time(&t);
624b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uprv_memcpy( &tmrec, localtime(&t), sizeof(tmrec) );
62559d709d503bab6e2b61931737e662dd293b40578ccornelius#if U_PLATFORM != U_PF_IPHONE
62659d709d503bab6e2b61931737e662dd293b40578ccornelius    UBool dst_checked = (tmrec.tm_isdst != 0); /* daylight savings time is checked*/
62759d709d503bab6e2b61931737e662dd293b40578ccornelius#endif
628b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    t1 = mktime(&tmrec);                 /* local time in seconds*/
629b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uprv_memcpy( &tmrec, gmtime(&t), sizeof(tmrec) );
630b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    t2 = mktime(&tmrec);                 /* GMT (or UTC) in seconds*/
631b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    tdiff = t2 - t1;
63259d709d503bab6e2b61931737e662dd293b40578ccornelius
63359d709d503bab6e2b61931737e662dd293b40578ccornelius#if U_PLATFORM != U_PF_IPHONE
634b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* imitate NT behaviour, which returns same timezone offset to GMT for
63554dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius       winter and summer.
63654dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius       This does not work on all platforms. For instance, on glibc on Linux
63754dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius       and on Mac OS 10.5, tdiff calculated above remains the same
63859d709d503bab6e2b61931737e662dd293b40578ccornelius       regardless of whether DST is in effect or not. iOS is another
63959d709d503bab6e2b61931737e662dd293b40578ccornelius       platform where this does not work. Linux + glibc and Mac OS 10.5
64059d709d503bab6e2b61931737e662dd293b40578ccornelius       have U_TIMEZONE defined so that this code is not reached.
64159d709d503bab6e2b61931737e662dd293b40578ccornelius    */
642b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (dst_checked)
643b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        tdiff += 3600;
64459d709d503bab6e2b61931737e662dd293b40578ccornelius#endif
645b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return tdiff;
646b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
647b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
648b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
649b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Note that U_TZNAME does *not* have to be tzname, but if it is,
65085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho   some platforms need to have it declared here. */
651b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
652103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#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))
653b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* RS6000 and others reject char **tzname.  */
654b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruextern U_IMPORT char *U_TZNAME[];
655b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
656b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
65759d709d503bab6e2b61931737e662dd293b40578ccornelius#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)
658b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* These platforms are likely to use Olson timezone IDs. */
659b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define CHECK_LOCALTIME_LINK 1
660103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_IS_DARWIN_BASED
661b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#include <tzfile.h>
662b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define TZZONEINFO      (TZDIR "/")
6638393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#elif U_PLATFORM == U_PF_SOLARIS
6648393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#define TZDEFAULT       "/etc/localtime"
6658393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#define TZZONEINFO      "/usr/share/lib/zoneinfo/"
66659d709d503bab6e2b61931737e662dd293b40578ccornelius#define TZZONEINFO2     "../usr/share/lib/zoneinfo/"
6678393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#define TZ_ENV_CHECK    "localtime"
66885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#else
66985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define TZDEFAULT       "/etc/localtime"
67085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define TZZONEINFO      "/usr/share/zoneinfo/"
67185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
67285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if U_HAVE_DIRENT_H
67350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#define TZFILE_SKIP     "posixrules" /* tz file to skip when searching. */
67427f654740f2a26ad62a5c155af9199af9e69b889claireho/* Some Linux distributions have 'localtime' in /usr/share/zoneinfo
67527f654740f2a26ad62a5c155af9199af9e69b889claireho   symlinked to /etc/localtime, which makes searchForTZFile return
67627f654740f2a26ad62a5c155af9199af9e69b889claireho   'localtime' when it's the first match. */
67727f654740f2a26ad62a5c155af9199af9e69b889claireho#define TZFILE_SKIP2    "localtime"
67885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define SEARCH_TZFILE
67985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#include <dirent.h>  /* Needed to search through system timezone files */
680b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
681b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic char gTimeZoneBuffer[PATH_MAX];
682b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic char *gTimeZoneBufferPtr = NULL;
683b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
684b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
685103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if !U_PLATFORM_USES_ONLY_WIN32_API
686b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define isNonDigit(ch) (ch < '0' || '9' < ch)
687b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic UBool isValidOlsonID(const char *id) {
688b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int32_t idx = 0;
689b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
690b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Determine if this is something like Iceland (Olson ID)
691b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    or AST4ADT (non-Olson ID) */
692b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    while (id[idx] && isNonDigit(id[idx]) && id[idx] != ',') {
693b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        idx++;
694b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
695b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
696b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* If we went through the whole string, then it might be okay.
697b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    The timezone is sometimes set to "CST-7CDT", "CST6CDT5,J129,J131/19:30",
698b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    "GRNLNDST3GRNLNDDT" or similar, so we cannot use it.
699b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    The rest of the time it could be an Olson ID. George */
700b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return (UBool)(id[idx] == 0
701b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        || uprv_strcmp(id, "PST8PDT") == 0
702b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        || uprv_strcmp(id, "MST7MDT") == 0
703b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        || uprv_strcmp(id, "CST6CDT") == 0
704b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        || uprv_strcmp(id, "EST5EDT") == 0);
705b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
70627f654740f2a26ad62a5c155af9199af9e69b889claireho
70727f654740f2a26ad62a5c155af9199af9e69b889claireho/* On some Unix-like OS, 'posix' subdirectory in
70827f654740f2a26ad62a5c155af9199af9e69b889claireho   /usr/share/zoneinfo replicates the top-level contents. 'right'
70927f654740f2a26ad62a5c155af9199af9e69b889claireho   subdirectory has the same set of files, but individual files
71027f654740f2a26ad62a5c155af9199af9e69b889claireho   are different from those in the top-level directory or 'posix'
71127f654740f2a26ad62a5c155af9199af9e69b889claireho   because 'right' has files for TAI (Int'l Atomic Time) while 'posix'
71227f654740f2a26ad62a5c155af9199af9e69b889claireho   has files for UTC.
71327f654740f2a26ad62a5c155af9199af9e69b889claireho   When the first match for /etc/localtime is in either of them
71427f654740f2a26ad62a5c155af9199af9e69b889claireho   (usually in posix because 'right' has different file contents),
71527f654740f2a26ad62a5c155af9199af9e69b889claireho   or TZ environment variable points to one of them, createTimeZone
71627f654740f2a26ad62a5c155af9199af9e69b889claireho   fails because, say, 'posix/America/New_York' is not an Olson
71727f654740f2a26ad62a5c155af9199af9e69b889claireho   timezone id ('America/New_York' is). So, we have to skip
71827f654740f2a26ad62a5c155af9199af9e69b889claireho   'posix/' and 'right/' at the beginning. */
71927f654740f2a26ad62a5c155af9199af9e69b889clairehostatic void skipZoneIDPrefix(const char** id) {
72027f654740f2a26ad62a5c155af9199af9e69b889claireho    if (uprv_strncmp(*id, "posix/", 6) == 0
72127f654740f2a26ad62a5c155af9199af9e69b889claireho        || uprv_strncmp(*id, "right/", 6) == 0)
72227f654740f2a26ad62a5c155af9199af9e69b889claireho    {
72327f654740f2a26ad62a5c155af9199af9e69b889claireho        *id += 6;
72427f654740f2a26ad62a5c155af9199af9e69b889claireho    }
72527f654740f2a26ad62a5c155af9199af9e69b889claireho}
726b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
727b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
728103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if defined(U_TZNAME) && !U_PLATFORM_USES_ONLY_WIN32_API
729b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
730b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#define CONVERT_HOURS_TO_SECONDS(offset) (int32_t)(offset*3600)
731b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querutypedef struct OffsetZoneMapping {
732b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int32_t offsetSeconds;
733103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius    int32_t daylightType; /* 0=U_DAYLIGHT_NONE, 1=daylight in June-U_DAYLIGHT_JUNE, 2=daylight in December=U_DAYLIGHT_DECEMBER*/
734b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *stdID;
735b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *dstID;
736b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *olsonID;
737b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru} OffsetZoneMapping;
738b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
739103e9ffba2cba345d0078eb8b8db33249f81840aCraig Corneliusenum { U_DAYLIGHT_NONE=0,U_DAYLIGHT_JUNE=1,U_DAYLIGHT_DECEMBER=2 };
740103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius
741b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
742b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruThis list tries to disambiguate a set of abbreviated timezone IDs and offsets
743b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruand maps it to an Olson ID.
744b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruBefore adding anything to this list, take a look at
745b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruicu/source/tools/tzcode/tz.alias
746b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruSometimes no daylight savings (0) is important to define due to aliases.
747b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruThis list can be tested with icu/source/test/compat/tzone.pl
748b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruMore values could be added to daylightType to increase precision.
749b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*/
750b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic const struct OffsetZoneMapping OFFSET_ZONE_MAPPINGS[] = {
751b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-45900, 2, "CHAST", "CHADT", "Pacific/Chatham"},
752b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-43200, 1, "PETT", "PETST", "Asia/Kamchatka"},
753b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-43200, 2, "NZST", "NZDT", "Pacific/Auckland"},
754b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-43200, 1, "ANAT", "ANAST", "Asia/Anadyr"},
755b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-39600, 1, "MAGT", "MAGST", "Asia/Magadan"},
756b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-37800, 2, "LHST", "LHST", "Australia/Lord_Howe"},
757b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-36000, 2, "EST", "EST", "Australia/Sydney"},
758b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-36000, 1, "SAKT", "SAKST", "Asia/Sakhalin"},
759b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-36000, 1, "VLAT", "VLAST", "Asia/Vladivostok"},
760b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-34200, 2, "CST", "CST", "Australia/South"},
761b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-32400, 1, "YAKT", "YAKST", "Asia/Yakutsk"},
762b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-32400, 1, "CHOT", "CHOST", "Asia/Choibalsan"},
763b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-31500, 2, "CWST", "CWST", "Australia/Eucla"},
764b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-28800, 1, "IRKT", "IRKST", "Asia/Irkutsk"},
765b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-28800, 1, "ULAT", "ULAST", "Asia/Ulaanbaatar"},
766b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-28800, 2, "WST", "WST", "Australia/West"},
767b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-25200, 1, "HOVT", "HOVST", "Asia/Hovd"},
768b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-25200, 1, "KRAT", "KRAST", "Asia/Krasnoyarsk"},
769b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-21600, 1, "NOVT", "NOVST", "Asia/Novosibirsk"},
770b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-21600, 1, "OMST", "OMSST", "Asia/Omsk"},
771b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-18000, 1, "YEKT", "YEKST", "Asia/Yekaterinburg"},
772b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-14400, 1, "SAMT", "SAMST", "Europe/Samara"},
773b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-14400, 1, "AMT", "AMST", "Asia/Yerevan"},
774b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-14400, 1, "AZT", "AZST", "Asia/Baku"},
775b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-10800, 1, "AST", "ADT", "Asia/Baghdad"},
776b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-10800, 1, "MSK", "MSD", "Europe/Moscow"},
777b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-10800, 1, "VOLT", "VOLST", "Europe/Volgograd"},
778b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-7200, 0, "EET", "CEST", "Africa/Tripoli"},
779b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-7200, 1, "EET", "EEST", "Europe/Athens"}, /* Conflicts with Africa/Cairo */
780b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-7200, 1, "IST", "IDT", "Asia/Jerusalem"},
781b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-3600, 0, "CET", "WEST", "Africa/Algiers"},
782b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {-3600, 2, "WAT", "WAST", "Africa/Windhoek"},
783b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {0, 1, "GMT", "IST", "Europe/Dublin"},
784b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {0, 1, "GMT", "BST", "Europe/London"},
785b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {0, 0, "WET", "WEST", "Africa/Casablanca"},
786b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {0, 0, "WET", "WET", "Africa/El_Aaiun"},
787b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {3600, 1, "AZOT", "AZOST", "Atlantic/Azores"},
788b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {3600, 1, "EGT", "EGST", "America/Scoresbysund"},
789b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {10800, 1, "PMST", "PMDT", "America/Miquelon"},
790b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {10800, 2, "UYT", "UYST", "America/Montevideo"},
791b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {10800, 1, "WGT", "WGST", "America/Godthab"},
792b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {10800, 2, "BRT", "BRST", "Brazil/East"},
793b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {12600, 1, "NST", "NDT", "America/St_Johns"},
794b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {14400, 1, "AST", "ADT", "Canada/Atlantic"},
795b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {14400, 2, "AMT", "AMST", "America/Cuiaba"},
796b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {14400, 2, "CLT", "CLST", "Chile/Continental"},
797b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {14400, 2, "FKT", "FKST", "Atlantic/Stanley"},
798b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {14400, 2, "PYT", "PYST", "America/Asuncion"},
799b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {18000, 1, "CST", "CDT", "America/Havana"},
800b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {18000, 1, "EST", "EDT", "US/Eastern"}, /* Conflicts with America/Grand_Turk */
801b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {21600, 2, "EAST", "EASST", "Chile/EasterIsland"},
802b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {21600, 0, "CST", "MDT", "Canada/Saskatchewan"},
803b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {21600, 0, "CST", "CDT", "America/Guatemala"},
804b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {21600, 1, "CST", "CDT", "US/Central"}, /* Conflicts with Mexico/General */
805b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {25200, 1, "MST", "MDT", "US/Mountain"}, /* Conflicts with Mexico/BajaSur */
806b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {28800, 0, "PST", "PST", "Pacific/Pitcairn"},
807b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {28800, 1, "PST", "PDT", "US/Pacific"}, /* Conflicts with Mexico/BajaNorte */
808b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {32400, 1, "AKST", "AKDT", "US/Alaska"},
809b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {36000, 1, "HAST", "HADT", "US/Aleutian"}
810b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru};
811b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
812b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*#define DEBUG_TZNAME*/
813b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
814b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic const char* remapShortTimeZone(const char *stdID, const char *dstID, int32_t daylightType, int32_t offset)
815b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
816b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int32_t idx;
817b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#ifdef DEBUG_TZNAME
818b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    fprintf(stderr, "TZ=%s std=%s dst=%s daylight=%d offset=%d\n", getenv("TZ"), stdID, dstID, daylightType, offset);
819b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
820f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    for (idx = 0; idx < UPRV_LENGTHOF(OFFSET_ZONE_MAPPINGS); idx++)
821b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {
822b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (offset == OFFSET_ZONE_MAPPINGS[idx].offsetSeconds
823b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            && daylightType == OFFSET_ZONE_MAPPINGS[idx].daylightType
824b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            && strcmp(OFFSET_ZONE_MAPPINGS[idx].stdID, stdID) == 0
825b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            && strcmp(OFFSET_ZONE_MAPPINGS[idx].dstID, dstID) == 0)
826b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
827b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            return OFFSET_ZONE_MAPPINGS[idx].olsonID;
828b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
829b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
830b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return NULL;
831b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
832b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
833b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
83485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#ifdef SEARCH_TZFILE
83585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define MAX_PATH_SIZE PATH_MAX /* Set the limit for the size of the path. */
83685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define MAX_READ_SIZE 512
83785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
83885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hotypedef struct DefaultTZInfo {
83985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    char* defaultTZBuffer;
84085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int64_t defaultTZFileSize;
84185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    FILE* defaultTZFilePtr;
84285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UBool defaultTZstatus;
84385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t defaultTZPosition;
84485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho} DefaultTZInfo;
84585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
84685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/*
84785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * This method compares the two files given to see if they are a match.
84885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * It is currently use to compare two TZ files.
84985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
85085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic UBool compareBinaryFiles(const char* defaultTZFileName, const char* TZFileName, DefaultTZInfo* tzInfo) {
85185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    FILE* file;
85285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int64_t sizeFile;
85385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int64_t sizeFileLeft;
85485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t sizeFileRead;
85585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    int32_t sizeFileToRead;
85685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    char bufferFile[MAX_READ_SIZE];
85785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    UBool result = TRUE;
85885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
85985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (tzInfo->defaultTZFilePtr == NULL) {
86085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        tzInfo->defaultTZFilePtr = fopen(defaultTZFileName, "r");
86185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
86285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    file = fopen(TZFileName, "r");
86385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
86485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    tzInfo->defaultTZPosition = 0; /* reset position to begin search */
86585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
86685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (file != NULL && tzInfo->defaultTZFilePtr != NULL) {
86785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* First check that the file size are equal. */
86885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (tzInfo->defaultTZFileSize == 0) {
86985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            fseek(tzInfo->defaultTZFilePtr, 0, SEEK_END);
87085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            tzInfo->defaultTZFileSize = ftell(tzInfo->defaultTZFilePtr);
87185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
87285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        fseek(file, 0, SEEK_END);
87385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        sizeFile = ftell(file);
87485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        sizeFileLeft = sizeFile;
87585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
87685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (sizeFile != tzInfo->defaultTZFileSize) {
87785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            result = FALSE;
87885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        } else {
87985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            /* Store the data from the files in seperate buffers and
88085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho             * compare each byte to determine equality.
88185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho             */
88285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (tzInfo->defaultTZBuffer == NULL) {
88385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                rewind(tzInfo->defaultTZFilePtr);
88485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                tzInfo->defaultTZBuffer = (char*)uprv_malloc(sizeof(char) * tzInfo->defaultTZFileSize);
8851b7d32f919554dda9c193b32188251337bc756f1Fredrik Roubert                sizeFileRead = fread(tzInfo->defaultTZBuffer, 1, tzInfo->defaultTZFileSize, tzInfo->defaultTZFilePtr);
88685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
88785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            rewind(file);
88885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            while(sizeFileLeft > 0) {
88985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                uprv_memset(bufferFile, 0, MAX_READ_SIZE);
89085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                sizeFileToRead = sizeFileLeft < MAX_READ_SIZE ? sizeFileLeft : MAX_READ_SIZE;
89185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
89285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                sizeFileRead = fread(bufferFile, 1, sizeFileToRead, file);
89385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                if (memcmp(tzInfo->defaultTZBuffer + tzInfo->defaultTZPosition, bufferFile, sizeFileRead) != 0) {
89485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    result = FALSE;
89585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    break;
89685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                }
89785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                sizeFileLeft -= sizeFileRead;
89885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                tzInfo->defaultTZPosition += sizeFileRead;
89985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
90085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
90185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    } else {
90285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        result = FALSE;
90385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
90485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
90585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (file != NULL) {
90685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        fclose(file);
90785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
90885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
90985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return result;
91085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
91185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/*
91285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho * This method recursively traverses the directory given for a matching TZ file and returns the first match.
91385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho */
91485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho/* dirent also lists two entries: "." and ".." that we can safely ignore. */
91585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define SKIP1 "."
91685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define SKIP2 ".."
91785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic char SEARCH_TZFILE_RESULT[MAX_PATH_SIZE] = "";
91885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic char* searchForTZFile(const char* path, DefaultTZInfo* tzInfo) {
91950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    char curpath[MAX_PATH_SIZE];
92085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    DIR* dirp = opendir(path);
92185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    DIR* subDirp = NULL;
92285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    struct dirent* dirEntry = NULL;
92385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
92485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    char* result = NULL;
92550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    if (dirp == NULL) {
92650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        return result;
92750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    }
92885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
92985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    /* Save the current path */
93085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    uprv_memset(curpath, 0, MAX_PATH_SIZE);
93185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    uprv_strcpy(curpath, path);
93285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
93385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    /* Check each entry in the directory. */
93485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    while((dirEntry = readdir(dirp)) != NULL) {
93527f654740f2a26ad62a5c155af9199af9e69b889claireho        const char* dirName = dirEntry->d_name;
93627f654740f2a26ad62a5c155af9199af9e69b889claireho        if (uprv_strcmp(dirName, SKIP1) != 0 && uprv_strcmp(dirName, SKIP2) != 0) {
93785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            /* Create a newpath with the new entry to test each entry in the directory. */
93885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            char newpath[MAX_PATH_SIZE];
93985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            uprv_strcpy(newpath, curpath);
94027f654740f2a26ad62a5c155af9199af9e69b889claireho            uprv_strcat(newpath, dirName);
94185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
94285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if ((subDirp = opendir(newpath)) != NULL) {
94385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                /* If this new path is a directory, make a recursive call with the newpath. */
94485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                closedir(subDirp);
94585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                uprv_strcat(newpath, "/");
94685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                result = searchForTZFile(newpath, tzInfo);
94727f654740f2a26ad62a5c155af9199af9e69b889claireho                /*
94827f654740f2a26ad62a5c155af9199af9e69b889claireho                 Have to get out here. Otherwise, we'd keep looking
94927f654740f2a26ad62a5c155af9199af9e69b889claireho                 and return the first match in the top-level directory
95027f654740f2a26ad62a5c155af9199af9e69b889claireho                 if there's a match in the top-level. If not, this function
95127f654740f2a26ad62a5c155af9199af9e69b889claireho                 would return NULL and set gTimeZoneBufferPtr to NULL in initDefault().
95227f654740f2a26ad62a5c155af9199af9e69b889claireho                 It worked without this in most cases because we have a fallback of calling
95327f654740f2a26ad62a5c155af9199af9e69b889claireho                 localtime_r to figure out the default timezone.
95427f654740f2a26ad62a5c155af9199af9e69b889claireho                */
95527f654740f2a26ad62a5c155af9199af9e69b889claireho                if (result != NULL)
95627f654740f2a26ad62a5c155af9199af9e69b889claireho                    break;
95727f654740f2a26ad62a5c155af9199af9e69b889claireho            } else if (uprv_strcmp(TZFILE_SKIP, dirName) != 0 && uprv_strcmp(TZFILE_SKIP2, dirName) != 0) {
95885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                if(compareBinaryFiles(TZDEFAULT, newpath, tzInfo)) {
95927f654740f2a26ad62a5c155af9199af9e69b889claireho                    const char* zoneid = newpath + (sizeof(TZZONEINFO)) - 1;
96027f654740f2a26ad62a5c155af9199af9e69b889claireho                    skipZoneIDPrefix(&zoneid);
96127f654740f2a26ad62a5c155af9199af9e69b889claireho                    uprv_strcpy(SEARCH_TZFILE_RESULT, zoneid);
96285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    result = SEARCH_TZFILE_RESULT;
96385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    /* Get out after the first one found. */
96485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    break;
96585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                }
96685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
96785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
96885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
96985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    closedir(dirp);
97085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return result;
97185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
97285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
973b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI const char* U_EXPORT2
974b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_tzname(int n)
975b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
976b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *tzid = NULL;
977103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_USES_ONLY_WIN32_API
978b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    tzid = uprv_detectWindowsTimeZone();
979b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
980b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (tzid != NULL) {
981b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return tzid;
982b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
983b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
984b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
985103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius/*#if U_PLATFORM_IS_DARWIN_BASED
986b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int ret;
987b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
988b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    tzid = getenv("TZFILE");
989b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (tzid != NULL) {
990b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return tzid;
991b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
992b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif*/
993b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
994b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* This code can be temporarily disabled to test tzname resolution later on. */
995b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#ifndef DEBUG_TZNAME
996b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    tzid = getenv("TZ");
9978393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    if (tzid != NULL && isValidOlsonID(tzid)
9988393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#if U_PLATFORM == U_PF_SOLARIS
9998393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    /* When TZ equals localtime on Solaris, check the /etc/localtime file. */
10008393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius        && uprv_strcmp(tzid, TZ_ENV_CHECK) != 0
10018393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius#endif
10028393335b955da7340c9f19b1b4b2d6c0c2c04be7Craig Cornelius    ) {
10038de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert        /* The colon forces tzset() to treat the remainder as zoneinfo path */
10048de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert        if (tzid[0] == ':') {
10058de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert            tzid++;
10068de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert        }
1007b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* This might be a good Olson ID. */
100827f654740f2a26ad62a5c155af9199af9e69b889claireho        skipZoneIDPrefix(&tzid);
1009b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return tzid;
1010b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1011b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* else U_TZNAME will give a better result. */
1012b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1013b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1014103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if defined(CHECK_LOCALTIME_LINK) && !defined(DEBUG_SKIP_LOCALTIME_LINK)
1015b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Caller must handle threading issues */
1016b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (gTimeZoneBufferPtr == NULL) {
1017b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /*
1018b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        This is a trick to look at the name of the link to get the Olson ID
1019b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        because the tzfile contents is underspecified.
1020b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        This isn't guaranteed to work because it may not be a symlink.
1021b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
1022b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        int32_t ret = (int32_t)readlink(TZDEFAULT, gTimeZoneBuffer, sizeof(gTimeZoneBuffer));
1023b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (0 < ret) {
1024b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            int32_t tzZoneInfoLen = uprv_strlen(TZZONEINFO);
1025b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            gTimeZoneBuffer[ret] = 0;
1026b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            if (uprv_strncmp(gTimeZoneBuffer, TZZONEINFO, tzZoneInfoLen) == 0
1027b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                && isValidOlsonID(gTimeZoneBuffer + tzZoneInfoLen))
1028b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            {
1029b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                return (gTimeZoneBufferPtr = gTimeZoneBuffer + tzZoneInfoLen);
1030b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            }
103159d709d503bab6e2b61931737e662dd293b40578ccornelius#if U_PLATFORM == U_PF_SOLARIS
103259d709d503bab6e2b61931737e662dd293b40578ccornelius            else
103359d709d503bab6e2b61931737e662dd293b40578ccornelius            {
103459d709d503bab6e2b61931737e662dd293b40578ccornelius                tzZoneInfoLen = uprv_strlen(TZZONEINFO2);
103559d709d503bab6e2b61931737e662dd293b40578ccornelius                if (uprv_strncmp(gTimeZoneBuffer, TZZONEINFO2, tzZoneInfoLen) == 0
103659d709d503bab6e2b61931737e662dd293b40578ccornelius                                && isValidOlsonID(gTimeZoneBuffer + tzZoneInfoLen))
103759d709d503bab6e2b61931737e662dd293b40578ccornelius                {
103859d709d503bab6e2b61931737e662dd293b40578ccornelius                    return (gTimeZoneBufferPtr = gTimeZoneBuffer + tzZoneInfoLen);
103959d709d503bab6e2b61931737e662dd293b40578ccornelius                }
104059d709d503bab6e2b61931737e662dd293b40578ccornelius            }
104159d709d503bab6e2b61931737e662dd293b40578ccornelius#endif
104285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        } else {
104385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if defined(SEARCH_TZFILE)
104485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            DefaultTZInfo* tzInfo = (DefaultTZInfo*)uprv_malloc(sizeof(DefaultTZInfo));
104585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (tzInfo != NULL) {
104685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                tzInfo->defaultTZBuffer = NULL;
104785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                tzInfo->defaultTZFileSize = 0;
104885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                tzInfo->defaultTZFilePtr = NULL;
104985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                tzInfo->defaultTZstatus = FALSE;
105085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                tzInfo->defaultTZPosition = 0;
105185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
105285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                gTimeZoneBufferPtr = searchForTZFile(TZZONEINFO, tzInfo);
105385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
105485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                /* Free previously allocated memory */
105585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                if (tzInfo->defaultTZBuffer != NULL) {
105685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    uprv_free(tzInfo->defaultTZBuffer);
105785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                }
105885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                if (tzInfo->defaultTZFilePtr != NULL) {
105985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                    fclose(tzInfo->defaultTZFilePtr);
106085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                }
106185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                uprv_free(tzInfo);
106285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
106385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
106485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (gTimeZoneBufferPtr != NULL && isValidOlsonID(gTimeZoneBufferPtr)) {
106585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                return gTimeZoneBufferPtr;
106685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
106785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif
1068b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1069b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1070b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else {
1071b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return gTimeZoneBufferPtr;
1072b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1073b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1074b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1075b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1076b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#ifdef U_TZNAME
1077103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_USES_ONLY_WIN32_API
107850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    /* The return value is free'd in timezone.cpp on Windows because
107950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho     * the other code path returns a pointer to a heap location. */
108050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    return uprv_strdup(U_TZNAME[n]);
108150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#else
1082b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /*
1083b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    U_TZNAME is usually a non-unique abbreviation, which isn't normally usable.
1084b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    So we remap the abbreviation to an olson ID.
1085b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1086b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    Since Windows exposes a little more timezone information,
1087b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    we normally don't use this code on Windows because
1088b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uprv_detectWindowsTimeZone should have already given the correct answer.
1089b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    */
1090b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {
1091b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        struct tm juneSol, decemberSol;
1092b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        int daylightType;
1093b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        static const time_t juneSolstice=1182478260; /*2007-06-21 18:11 UT*/
1094b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        static const time_t decemberSolstice=1198332540; /*2007-12-22 06:09 UT*/
1095b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1096b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* This probing will tell us when daylight savings occurs.  */
1097b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        localtime_r(&juneSolstice, &juneSol);
1098b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        localtime_r(&decemberSolstice, &decemberSol);
1099103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius        if(decemberSol.tm_isdst > 0) {
1100103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius          daylightType = U_DAYLIGHT_DECEMBER;
1101103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius        } else if(juneSol.tm_isdst > 0) {
1102103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius          daylightType = U_DAYLIGHT_JUNE;
1103103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius        } else {
1104103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius          daylightType = U_DAYLIGHT_NONE;
1105103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius        }
1106b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        tzid = remapShortTimeZone(U_TZNAME[0], U_TZNAME[1], daylightType, uprv_timezone());
1107b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (tzid != NULL) {
1108b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            return tzid;
1109b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1110b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1111b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return U_TZNAME[n];
111250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
1113b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
1114b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return "";
1115b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1116b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1117b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1118b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* Get and set the ICU data directory --------------------------------------- */
1119b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1120f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstatic icu::UInitOnce gDataDirInitOnce = U_INITONCE_INITIALIZER;
1121b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic char *gDataDirectory = NULL;
1122f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
1123f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusUInitOnce gTimeZoneFilesInitOnce = U_INITONCE_INITIALIZER;
1124f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstatic CharString *gTimeZoneFilesDirectory = NULL;
1125f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
112659d709d503bab6e2b61931737e662dd293b40578ccornelius#if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API
1127b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru static char *gCorrectedPOSIXLocale = NULL; /* Heap allocated */
1128b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1129b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1130b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic UBool U_CALLCONV putil_cleanup(void)
1131b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
1132b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (gDataDirectory && *gDataDirectory) {
1133b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        uprv_free(gDataDirectory);
1134b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1135b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    gDataDirectory = NULL;
1136f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    gDataDirInitOnce.reset();
1137f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
1138f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    delete gTimeZoneFilesDirectory;
1139f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    gTimeZoneFilesDirectory = NULL;
1140f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    gTimeZoneFilesInitOnce.reset();
1141f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
114259d709d503bab6e2b61931737e662dd293b40578ccornelius#if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API
1143b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (gCorrectedPOSIXLocale) {
1144b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        uprv_free(gCorrectedPOSIXLocale);
1145b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        gCorrectedPOSIXLocale = NULL;
1146b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1147b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1148b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return TRUE;
1149b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1150b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1151b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
1152b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * Set the data directory.
1153b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru *    Make a copy of the passed string, and set the global data dir to point to it.
1154b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru */
1155b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI void U_EXPORT2
1156b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruu_setDataDirectory(const char *directory) {
1157b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    char *newDataDir;
1158b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int32_t length;
1159b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1160b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(directory==NULL || *directory==0) {
1161b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* A small optimization to prevent the malloc and copy when the
1162b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        shared library is used, and this is a way to make sure that NULL
1163b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        is never returned.
1164b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
1165b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        newDataDir = (char *)"";
1166b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1167b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else {
1168b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        length=(int32_t)uprv_strlen(directory);
1169b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        newDataDir = (char *)uprv_malloc(length + 2);
117085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* Exit out if newDataDir could not be created. */
117185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (newDataDir == NULL) {
117285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            return;
117385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
1174b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        uprv_strcpy(newDataDir, directory);
1175b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1176b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
1177b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
1178b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            char *p;
1179b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            while(p = uprv_strchr(newDataDir, U_FILE_ALT_SEP_CHAR)) {
1180b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                *p = U_FILE_SEP_CHAR;
1181b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            }
1182b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1183b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1184b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1185b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1186b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (gDataDirectory && *gDataDirectory) {
1187b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        uprv_free(gDataDirectory);
1188b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1189b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    gDataDirectory = newDataDir;
1190b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup);
1191b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1192b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1193b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI UBool U_EXPORT2
119485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Houprv_pathIsAbsolute(const char *path)
1195b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
119685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho  if(!path || !*path) {
119785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    return FALSE;
1198b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  }
1199b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1200b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  if(*path == U_FILE_SEP_CHAR) {
1201b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return TRUE;
1202b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  }
1203b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1204b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
1205b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  if(*path == U_FILE_ALT_SEP_CHAR) {
1206b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return TRUE;
1207b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  }
1208b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1209b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1210103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_USES_ONLY_WIN32_API
1211b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  if( (((path[0] >= 'A') && (path[0] <= 'Z')) ||
1212b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru       ((path[0] >= 'a') && (path[0] <= 'z'))) &&
1213b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru      path[1] == ':' ) {
1214b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return TRUE;
1215b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  }
1216b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1217b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1218b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  return FALSE;
1219b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1220b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
122127f654740f2a26ad62a5c155af9199af9e69b889claireho/* Temporary backup setting of ICU_DATA_DIR_PREFIX_ENV_VAR
122227f654740f2a26ad62a5c155af9199af9e69b889claireho   until some client wrapper makefiles are updated */
1223103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_IS_DARWIN_BASED && TARGET_IPHONE_SIMULATOR
122427f654740f2a26ad62a5c155af9199af9e69b889claireho# if !defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
122527f654740f2a26ad62a5c155af9199af9e69b889claireho#  define ICU_DATA_DIR_PREFIX_ENV_VAR "IPHONE_SIMULATOR_ROOT"
122627f654740f2a26ad62a5c155af9199af9e69b889claireho# endif
122727f654740f2a26ad62a5c155af9199af9e69b889claireho#endif
122827f654740f2a26ad62a5c155af9199af9e69b889claireho
1229f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstatic void U_CALLCONV dataDirectoryInitFn() {
1230f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    /* If we already have the directory, then return immediately. Will happen if user called
1231f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius     * u_setDataDirectory().
1232f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius     */
1233f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    if (gDataDirectory) {
1234f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        return;
1235f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    }
1236f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
1237b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *path = NULL;
123802fdd983fede27bf25bddfbe3b5deb20813df5efElliott Hughes#if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
123950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    char datadir_path_buffer[PATH_MAX];
124050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
1241b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1242b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /*
1243b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    When ICU_NO_USER_DATA_OVERRIDE is defined, users aren't allowed to
1244b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    override ICU's data with the ICU_DATA environment variable. This prevents
1245b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    problems where multiple custom copies of ICU's specific version of data
1246b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    are installed on a system. Either the application must define the data
1247b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    directory with u_setDataDirectory, define ICU_DATA_DIR when compiling
1248b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    ICU, set the data with udata_setCommonData or trust that all of the
1249b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    required data is contained in ICU's data library that contains
1250b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    the entry point defined by U_ICUDATA_ENTRY_POINT.
1251b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1252b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    There may also be some platforms where environment variables
1253b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    are not allowed.
1254b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    */
1255b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   if !defined(ICU_NO_USER_DATA_OVERRIDE) && !UCONFIG_NO_FILE_IO
1256b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* First try to get the environment variable */
1257b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    path=getenv("ICU_DATA");
1258b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#   endif
1259b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
126027f654740f2a26ad62a5c155af9199af9e69b889claireho    /* ICU_DATA_DIR may be set as a compile option.
126127f654740f2a26ad62a5c155af9199af9e69b889claireho     * U_ICU_DATA_DEFAULT_DIR is provided and is set by ICU at compile time
126227f654740f2a26ad62a5c155af9199af9e69b889claireho     * and is used only when data is built in archive mode eliminating the need
126327f654740f2a26ad62a5c155af9199af9e69b889claireho     * for ICU_DATA_DIR to be set. U_ICU_DATA_DEFAULT_DIR is set to the installation
126427f654740f2a26ad62a5c155af9199af9e69b889claireho     * directory of the data dat file. Users should use ICU_DATA_DIR if they want to
126527f654740f2a26ad62a5c155af9199af9e69b889claireho     * set their own path.
126627f654740f2a26ad62a5c155af9199af9e69b889claireho     */
126727f654740f2a26ad62a5c155af9199af9e69b889claireho#if defined(ICU_DATA_DIR) || defined(U_ICU_DATA_DEFAULT_DIR)
1268b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(path==NULL || *path==0) {
126927f654740f2a26ad62a5c155af9199af9e69b889claireho# if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
127002fdd983fede27bf25bddfbe3b5deb20813df5efElliott Hughes        const char *prefix = getenv(ICU_DATA_DIR_PREFIX_ENV_VAR);
127127f654740f2a26ad62a5c155af9199af9e69b889claireho# endif
127227f654740f2a26ad62a5c155af9199af9e69b889claireho# ifdef ICU_DATA_DIR
127327f654740f2a26ad62a5c155af9199af9e69b889claireho        path=ICU_DATA_DIR;
127427f654740f2a26ad62a5c155af9199af9e69b889claireho# else
127527f654740f2a26ad62a5c155af9199af9e69b889claireho        path=U_ICU_DATA_DEFAULT_DIR;
127627f654740f2a26ad62a5c155af9199af9e69b889claireho# endif
127727f654740f2a26ad62a5c155af9199af9e69b889claireho# if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
127802fdd983fede27bf25bddfbe3b5deb20813df5efElliott Hughes        if (prefix != NULL) {
127927f654740f2a26ad62a5c155af9199af9e69b889claireho            snprintf(datadir_path_buffer, PATH_MAX, "%s%s", prefix, path);
128050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho            path=datadir_path_buffer;
128150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        }
128227f654740f2a26ad62a5c155af9199af9e69b889claireho# endif
1283b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
128427f654740f2a26ad62a5c155af9199af9e69b889claireho#endif
1285b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1286b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(path==NULL) {
1287b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* It looks really bad, set it to something. */
1288b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        path = "";
1289b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1290b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1291b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    u_setDataDirectory(path);
1292f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    return;
1293f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius}
1294f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
1295f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusU_CAPI const char * U_EXPORT2
1296f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusu_getDataDirectory(void) {
1297f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    umtx_initOnce(gDataDirInitOnce, &dataDirectoryInitFn);
1298b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return gDataDirectory;
1299b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1300b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1301f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstatic void setTimeZoneFilesDir(const char *path, UErrorCode &status) {
1302f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    if (U_FAILURE(status)) {
1303f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        return;
1304f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    }
1305f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    gTimeZoneFilesDirectory->clear();
1306f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    gTimeZoneFilesDirectory->append(path, status);
1307f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
1308f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    char *p = gTimeZoneFilesDirectory->data();
1309f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    while (p = uprv_strchr(p, U_FILE_ALT_SEP_CHAR)) {
1310f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        *p = U_FILE_SEP_CHAR;
1311f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    }
1312f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif
1313f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius}
1314b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1315f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#define TO_STRING(x) TO_STRING_2(x)
1316f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#define TO_STRING_2(x) #x
1317b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1318f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusstatic void U_CALLCONV TimeZoneDataDirInitFn(UErrorCode &status) {
1319f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    U_ASSERT(gTimeZoneFilesDirectory == NULL);
1320f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup);
1321f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    gTimeZoneFilesDirectory = new CharString();
1322f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    if (gTimeZoneFilesDirectory == NULL) {
1323f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        status = U_MEMORY_ALLOCATION_ERROR;
1324f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        return;
1325f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    }
1326f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    const char *dir = getenv("ICU_TIMEZONE_FILES_DIR");
1327f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#if defined(U_TIMEZONE_FILES_DIR)
1328f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    if (dir == NULL) {
1329f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        dir = TO_STRING(U_TIMEZONE_FILES_DIR);
1330f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    }
1331f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius#endif
1332f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    if (dir == NULL) {
1333f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius        dir = "";
1334f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    }
1335f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    setTimeZoneFilesDir(dir, status);
1336f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius}
1337b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1338b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1339f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusU_CAPI const char * U_EXPORT2
1340f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusu_getTimeZoneFilesDirectory(UErrorCode *status) {
1341f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    umtx_initOnce(gTimeZoneFilesInitOnce, &TimeZoneDataDirInitFn, *status);
1342f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    return U_SUCCESS(*status) ? gTimeZoneFilesDirectory->data() : "";
1343f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius}
1344f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
1345f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusU_CAPI void U_EXPORT2
1346f9878a236aa0d9662d8e40cafdaf2e04cd615835ccorneliusu_setTimeZoneFilesDirectory(const char *path, UErrorCode *status) {
1347f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    umtx_initOnce(gTimeZoneFilesInitOnce, &TimeZoneDataDirInitFn, *status);
1348f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    setTimeZoneFilesDir(path, *status);
1349f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius
1350f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    // Note: this function does some extra churn, first setting based on the
1351f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    //       environment, then immediately replacing with the value passed in.
1352f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius    //       The logic is simpler that way, and performance shouldn't be an issue.
1353f9878a236aa0d9662d8e40cafdaf2e04cd615835ccornelius}
1354b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1355b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1356b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if U_POSIX_LOCALE
135727f654740f2a26ad62a5c155af9199af9e69b889claireho/* A helper function used by uprv_getPOSIXIDForDefaultLocale and
135827f654740f2a26ad62a5c155af9199af9e69b889claireho * uprv_getPOSIXIDForDefaultCodepage. Returns the posix locale id for
135927f654740f2a26ad62a5c155af9199af9e69b889claireho * LC_CTYPE and LC_MESSAGES. It doesn't support other locale categories.
136027f654740f2a26ad62a5c155af9199af9e69b889claireho */
136127f654740f2a26ad62a5c155af9199af9e69b889clairehostatic const char *uprv_getPOSIXIDForCategory(int category)
1362b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
136327f654740f2a26ad62a5c155af9199af9e69b889claireho    const char* posixID = NULL;
136427f654740f2a26ad62a5c155af9199af9e69b889claireho    if (category == LC_MESSAGES || category == LC_CTYPE) {
1365b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /*
136685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        * On Solaris two different calls to setlocale can result in
1367b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * different values. Only get this value once.
1368b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *
1369b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * We must check this first because an application can set this.
1370b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *
1371b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * LC_ALL can't be used because it's platform dependent. The LANG
1372b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * environment variable seems to affect LC_CTYPE variable by default.
1373b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * Here is what setlocale(LC_ALL, NULL) can return.
1374b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * HPUX can return 'C C C C C C C'
1375b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * Solaris can return /en_US/C/C/C/C/C on the second try.
1376b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * Linux can return LC_CTYPE=C;LC_NUMERIC=C;...
1377b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *
1378b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * The default codepage detection also needs to use LC_CTYPE.
137985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        *
1380b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * Do not call setlocale(LC_*, "")! Using an empty string instead
1381b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        * of NULL, will modify the libc behavior.
1382b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
138327f654740f2a26ad62a5c155af9199af9e69b889claireho        posixID = setlocale(category, NULL);
1384b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if ((posixID == 0)
1385b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            || (uprv_strcmp("C", posixID) == 0)
1386b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            || (uprv_strcmp("POSIX", posixID) == 0))
1387b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
1388b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            /* Maybe we got some garbage.  Try something more reasonable */
1389b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            posixID = getenv("LC_ALL");
13908de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert            /* Solaris speaks POSIX -  See IEEE Std 1003.1-2008
13918de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert             * This is needed to properly handle empty env. variables
13928de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert             */
13938de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert#if U_PLATFORM == U_PF_SOLARIS
13948de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert            if ((posixID == 0) || (posixID[0] == '\0')) {
13958de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert                posixID = getenv(category == LC_MESSAGES ? "LC_MESSAGES" : "LC_CTYPE");
13968de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert                if ((posixID == 0) || (posixID[0] == '\0')) {
13978de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert#else
1398b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            if (posixID == 0) {
139927f654740f2a26ad62a5c155af9199af9e69b889claireho                posixID = getenv(category == LC_MESSAGES ? "LC_MESSAGES" : "LC_CTYPE");
1400b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                if (posixID == 0) {
14018de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert#endif
1402b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                    posixID = getenv("LANG");
1403b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                }
1404b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            }
1405b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
140627f654740f2a26ad62a5c155af9199af9e69b889claireho    }
140727f654740f2a26ad62a5c155af9199af9e69b889claireho    if ((posixID==0)
140827f654740f2a26ad62a5c155af9199af9e69b889claireho        || (uprv_strcmp("C", posixID) == 0)
140927f654740f2a26ad62a5c155af9199af9e69b889claireho        || (uprv_strcmp("POSIX", posixID) == 0))
141027f654740f2a26ad62a5c155af9199af9e69b889claireho    {
141127f654740f2a26ad62a5c155af9199af9e69b889claireho        /* Nothing worked.  Give it a nice POSIX default value. */
141227f654740f2a26ad62a5c155af9199af9e69b889claireho        posixID = "en_US_POSIX";
141327f654740f2a26ad62a5c155af9199af9e69b889claireho    }
141427f654740f2a26ad62a5c155af9199af9e69b889claireho    return posixID;
141527f654740f2a26ad62a5c155af9199af9e69b889claireho}
1416b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
141727f654740f2a26ad62a5c155af9199af9e69b889claireho/* Return just the POSIX id for the default locale, whatever happens to be in
141827f654740f2a26ad62a5c155af9199af9e69b889claireho * it. It gets the value from LC_MESSAGES and indirectly from LC_ALL and LANG.
141927f654740f2a26ad62a5c155af9199af9e69b889claireho */
142027f654740f2a26ad62a5c155af9199af9e69b889clairehostatic const char *uprv_getPOSIXIDForDefaultLocale(void)
142127f654740f2a26ad62a5c155af9199af9e69b889claireho{
142227f654740f2a26ad62a5c155af9199af9e69b889claireho    static const char* posixID = NULL;
142327f654740f2a26ad62a5c155af9199af9e69b889claireho    if (posixID == 0) {
142427f654740f2a26ad62a5c155af9199af9e69b889claireho        posixID = uprv_getPOSIXIDForCategory(LC_MESSAGES);
1425b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
142627f654740f2a26ad62a5c155af9199af9e69b889claireho    return posixID;
142727f654740f2a26ad62a5c155af9199af9e69b889claireho}
1428b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
142954dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius#if !U_CHARSET_IS_UTF8
143027f654740f2a26ad62a5c155af9199af9e69b889claireho/* Return just the POSIX id for the default codepage, whatever happens to be in
143127f654740f2a26ad62a5c155af9199af9e69b889claireho * it. It gets the value from LC_CTYPE and indirectly from LC_ALL and LANG.
143227f654740f2a26ad62a5c155af9199af9e69b889claireho */
143327f654740f2a26ad62a5c155af9199af9e69b889clairehostatic const char *uprv_getPOSIXIDForDefaultCodepage(void)
143427f654740f2a26ad62a5c155af9199af9e69b889claireho{
143527f654740f2a26ad62a5c155af9199af9e69b889claireho    static const char* posixID = NULL;
143627f654740f2a26ad62a5c155af9199af9e69b889claireho    if (posixID == 0) {
143727f654740f2a26ad62a5c155af9199af9e69b889claireho        posixID = uprv_getPOSIXIDForCategory(LC_CTYPE);
143827f654740f2a26ad62a5c155af9199af9e69b889claireho    }
1439b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return posixID;
1440b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1441b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
144254dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius#endif
1443b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1444b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* NOTE: The caller should handle thread safety */
1445b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI const char* U_EXPORT2
1446b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_getDefaultLocaleID()
1447b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
1448b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if U_POSIX_LOCALE
1449b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
1450b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  Note that:  (a '!' means the ID is improper somehow)
1451b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     LC_ALL  ---->     default_loc          codepage
1452b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru--------------------------------------------------------
1453b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     ab.CD             ab                   CD
1454b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     ab@CD             ab__CD               -
1455b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     ab@CD.EF          ab__CD               EF
1456b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1457b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     ab_CD.EF@GH       ab_CD_GH             EF
1458b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1459b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruSome 'improper' ways to do the same as above:
1460b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  !  ab_CD@GH.EF       ab_CD_GH             EF
1461b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  !  ab_CD.EF@GH.IJ    ab_CD_GH             EF
1462b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru  !  ab_CD@ZZ.EF@GH.IJ ab_CD_GH             EF
1463b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1464b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     _CD@GH            _CD_GH               -
1465b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru     _CD.EF@GH         _CD_GH               EF
1466b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1467b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruThe variant cannot have dots in it.
1468b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruThe 'rightmost' variant (@xxx) wins.
1469b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruThe leftmost codepage (.xxx) wins.
1470b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*/
1471b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    char *correctedPOSIXLocale = 0;
147227f654740f2a26ad62a5c155af9199af9e69b889claireho    const char* posixID = uprv_getPOSIXIDForDefaultLocale();
1473b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *p;
1474b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *q;
1475b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    int32_t len;
1476b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1477b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Format: (no spaces)
1478b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    ll [ _CC ] [ . MM ] [ @ VV]
1479b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1480b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru      l = lang, C = ctry, M = charmap, V = variant
1481b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    */
1482b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1483b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (gCorrectedPOSIXLocale != NULL) {
148485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        return gCorrectedPOSIXLocale;
1485b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1486b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1487b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if ((p = uprv_strchr(posixID, '.')) != NULL) {
1488b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* assume new locale can't be larger than old one? */
148954dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius        correctedPOSIXLocale = static_cast<char *>(uprv_malloc(uprv_strlen(posixID)+1));
149085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* Exit on memory allocation error. */
149185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (correctedPOSIXLocale == NULL) {
149285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            return NULL;
149385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
1494b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        uprv_strncpy(correctedPOSIXLocale, posixID, p-posixID);
1495b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        correctedPOSIXLocale[p-posixID] = 0;
1496b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1497b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* do not copy after the @ */
1498b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if ((p = uprv_strchr(correctedPOSIXLocale, '@')) != NULL) {
1499b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            correctedPOSIXLocale[p-correctedPOSIXLocale] = 0;
1500b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1501b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1502b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1503b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Note that we scan the *uncorrected* ID. */
1504b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if ((p = uprv_strrchr(posixID, '@')) != NULL) {
1505b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (correctedPOSIXLocale == NULL) {
150654dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius            correctedPOSIXLocale = static_cast<char *>(uprv_malloc(uprv_strlen(posixID)+1));
150785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            /* Exit on memory allocation error. */
150885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            if (correctedPOSIXLocale == NULL) {
150985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho                return NULL;
151085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            }
1511b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strncpy(correctedPOSIXLocale, posixID, p-posixID);
1512b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            correctedPOSIXLocale[p-posixID] = 0;
1513b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1514b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        p++;
1515b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1516b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Take care of any special cases here.. */
1517b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (!uprv_strcmp(p, "nynorsk")) {
1518b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            p = "NY";
1519b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            /* Don't worry about no__NY. In practice, it won't appear. */
1520b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1521b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1522b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (uprv_strchr(correctedPOSIXLocale,'_') == NULL) {
1523b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strcat(correctedPOSIXLocale, "__"); /* aa@b -> aa__b */
1524b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1525b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else {
1526b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strcat(correctedPOSIXLocale, "_"); /* aa_CC@b -> aa_CC_b */
1527b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1528b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1529b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if ((q = uprv_strchr(p, '.')) != NULL) {
1530b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            /* How big will the resulting string be? */
1531b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            len = (int32_t)(uprv_strlen(correctedPOSIXLocale) + (q-p));
1532b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strncat(correctedPOSIXLocale, p, q-p);
1533b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            correctedPOSIXLocale[len] = 0;
1534b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1535b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else {
1536b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            /* Anything following the @ sign */
1537b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strcat(correctedPOSIXLocale, p);
1538b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1539b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1540b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Should there be a map from 'no@nynorsk' -> no_NO_NY here?
1541b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         * How about 'russian' -> 'ru'?
1542b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         * Many of the other locales using ISO codes will be handled by the
1543b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         * canonicalization functions in uloc_getDefault.
1544b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         */
1545b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1546b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1547b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Was a correction made? */
1548b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (correctedPOSIXLocale != NULL) {
1549b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        posixID = correctedPOSIXLocale;
1550b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1551b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else {
1552b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* copy it, just in case the original pointer goes away.  See j2395 */
1553b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        correctedPOSIXLocale = (char *)uprv_malloc(uprv_strlen(posixID) + 1);
155485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* Exit on memory allocation error. */
155585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if (correctedPOSIXLocale == NULL) {
155685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            return NULL;
155785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
1558b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        posixID = uprv_strcpy(correctedPOSIXLocale, posixID);
1559b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1560b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1561b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (gCorrectedPOSIXLocale == NULL) {
1562b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        gCorrectedPOSIXLocale = correctedPOSIXLocale;
1563b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup);
1564b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        correctedPOSIXLocale = NULL;
1565b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1566b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1567b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (correctedPOSIXLocale != NULL) {  /* Was already set - clean up. */
156885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        uprv_free(correctedPOSIXLocale);
1569b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1570b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1571b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return posixID;
1572b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1573103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM_USES_ONLY_WIN32_API
157459d709d503bab6e2b61931737e662dd293b40578ccornelius#define POSIX_LOCALE_CAPACITY 64
1575b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    UErrorCode status = U_ZERO_ERROR;
157659d709d503bab6e2b61931737e662dd293b40578ccornelius    char *correctedPOSIXLocale = 0;
157759d709d503bab6e2b61931737e662dd293b40578ccornelius
157859d709d503bab6e2b61931737e662dd293b40578ccornelius    if (gCorrectedPOSIXLocale != NULL) {
157959d709d503bab6e2b61931737e662dd293b40578ccornelius        return gCorrectedPOSIXLocale;
158059d709d503bab6e2b61931737e662dd293b40578ccornelius    }
158159d709d503bab6e2b61931737e662dd293b40578ccornelius
1582b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    LCID id = GetThreadLocale();
158359d709d503bab6e2b61931737e662dd293b40578ccornelius    correctedPOSIXLocale = static_cast<char *>(uprv_malloc(POSIX_LOCALE_CAPACITY + 1));
158459d709d503bab6e2b61931737e662dd293b40578ccornelius    if (correctedPOSIXLocale) {
158559d709d503bab6e2b61931737e662dd293b40578ccornelius        int32_t posixLen = uprv_convertToPosix(id, correctedPOSIXLocale, POSIX_LOCALE_CAPACITY, &status);
158659d709d503bab6e2b61931737e662dd293b40578ccornelius        if (U_SUCCESS(status)) {
158759d709d503bab6e2b61931737e662dd293b40578ccornelius            *(correctedPOSIXLocale + posixLen) = 0;
158859d709d503bab6e2b61931737e662dd293b40578ccornelius            gCorrectedPOSIXLocale = correctedPOSIXLocale;
158959d709d503bab6e2b61931737e662dd293b40578ccornelius            ucln_common_registerCleanup(UCLN_COMMON_PUTIL, putil_cleanup);
159059d709d503bab6e2b61931737e662dd293b40578ccornelius        } else {
159159d709d503bab6e2b61931737e662dd293b40578ccornelius            uprv_free(correctedPOSIXLocale);
159259d709d503bab6e2b61931737e662dd293b40578ccornelius        }
159359d709d503bab6e2b61931737e662dd293b40578ccornelius    }
1594b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
159559d709d503bab6e2b61931737e662dd293b40578ccornelius    if (gCorrectedPOSIXLocale == NULL) {
159659d709d503bab6e2b61931737e662dd293b40578ccornelius        return "en_US";
1597b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
159859d709d503bab6e2b61931737e662dd293b40578ccornelius    return gCorrectedPOSIXLocale;
1599b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1600103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_OS400
1601b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* locales are process scoped and are by definition thread safe */
1602b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    static char correctedLocale[64];
1603b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const  char *localeID = getenv("LC_ALL");
1604b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru           char *p;
1605b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1606b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (localeID == NULL)
1607b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        localeID = getenv("LANG");
1608b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (localeID == NULL)
1609b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        localeID = setlocale(LC_ALL, NULL);
1610b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Make sure we have something... */
1611b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (localeID == NULL)
1612b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return "en_US_POSIX";
1613b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1614b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Extract the locale name from the path. */
1615b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if((p = uprv_strrchr(localeID, '/')) != NULL)
1616b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {
1617b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Increment p to start of locale name. */
1618b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        p++;
1619b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        localeID = p;
1620b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1621b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1622b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Copy to work location. */
1623b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uprv_strcpy(correctedLocale, localeID);
1624b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1625b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Strip off the '.locale' extension. */
1626b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if((p = uprv_strchr(correctedLocale, '.')) != NULL) {
1627b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *p = 0;
1628b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1629b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1630b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* Upper case the locale name. */
1631b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    T_CString_toUpperCase(correctedLocale);
1632b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1633b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* See if we are using the POSIX locale.  Any of the
1634b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    * following are equivalent and use the same QLGPGCMA
1635b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    * (POSIX) locale.
1636b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    * QLGPGCMA2 means UCS2
1637b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    * QLGPGCMA_4 means UTF-32
1638b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    * QLGPGCMA_8 means UTF-8
1639b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    */
1640b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if ((uprv_strcmp("C", correctedLocale) == 0) ||
1641b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        (uprv_strcmp("POSIX", correctedLocale) == 0) ||
1642b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        (uprv_strncmp("QLGPGCMA", correctedLocale, 8) == 0))
1643b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {
1644b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        uprv_strcpy(correctedLocale, "en_US_POSIX");
1645b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1646b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else
1647b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {
1648b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        int16_t LocaleLen;
1649b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1650b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Lower case the lang portion. */
1651b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        for(p = correctedLocale; *p != 0 && *p != '_'; p++)
1652b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
1653b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            *p = uprv_tolower(*p);
1654b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1655b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1656b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Adjust for Euro.  After '_E' add 'URO'. */
1657b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        LocaleLen = uprv_strlen(correctedLocale);
1658b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (correctedLocale[LocaleLen - 2] == '_' &&
1659b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            correctedLocale[LocaleLen - 1] == 'E')
1660b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
1661b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strcat(correctedLocale, "URO");
1662b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1663b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1664b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* If using Lotus-based locale then convert to
1665b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         * equivalent non Lotus.
1666b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         */
1667b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else if (correctedLocale[LocaleLen - 2] == '_' &&
1668b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            correctedLocale[LocaleLen - 1] == 'L')
1669b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
1670b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            correctedLocale[LocaleLen - 2] = 0;
1671b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1672b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1673b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* There are separate simplified and traditional
1674b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         * locales called zh_HK_S and zh_HK_T.
1675b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru         */
1676b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else if (uprv_strncmp(correctedLocale, "zh_HK", 5) == 0)
1677b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
1678b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strcpy(correctedLocale, "zh_HK");
1679b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1680b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1681b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* A special zh_CN_GBK locale...
1682b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
1683b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else if (uprv_strcmp(correctedLocale, "zh_CN_GBK") == 0)
1684b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        {
1685b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strcpy(correctedLocale, "zh_CN");
1686b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1687b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1688b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1689b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1690b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return correctedLocale;
1691b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1692b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1693b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1694b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
169585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if !U_CHARSET_IS_UTF8
1696b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#if U_POSIX_LOCALE
1697b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
1698b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruDue to various platform differences, one platform may specify a charset,
1699b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruwhen they really mean a different charset. Remap the names so that they are
1700b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querucompatible with ICU. Only conflicting/ambiguous aliases should be resolved
1701b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruhere. Before adding anything to this function, please consider adding unique
1702b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querunames to the ICU alias table in the data directory.
1703b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru*/
1704b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Querustatic const char*
1705b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruremapPlatformDependentCodepage(const char *locale, const char *name) {
1706b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (locale != NULL && *locale == 0) {
1707b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Make sure that an empty locale is handled the same way. */
1708b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        locale = NULL;
1709b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1710b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (name == NULL) {
1711b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return NULL;
1712b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1713103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM == U_PF_AIX
1714b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (uprv_strcmp(name, "IBM-943") == 0) {
1715b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Use the ASCII compatible ibm-943 */
1716b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = "Shift-JIS";
1717b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1718b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else if (uprv_strcmp(name, "IBM-1252") == 0) {
1719b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Use the windows-1252 that contains the Euro */
1720b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = "IBM-5348";
1721b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1722103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_SOLARIS
1723b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (locale != NULL && uprv_strcmp(name, "EUC") == 0) {
1724b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Solaris underspecifies the "EUC" name. */
1725b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (uprv_strcmp(locale, "zh_CN") == 0) {
1726b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            name = "EUC-CN";
1727b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1728b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else if (uprv_strcmp(locale, "zh_TW") == 0) {
1729b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            name = "EUC-TW";
1730b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1731b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else if (uprv_strcmp(locale, "ko_KR") == 0) {
1732b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            name = "EUC-KR";
1733b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1734b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1735b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else if (uprv_strcmp(name, "eucJP") == 0) {
1736b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /*
1737b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-954 is the best match.
1738b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-33722 is the default for eucJP (similar to Windows).
1739b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
1740b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = "eucjis";
1741b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
174285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    else if (uprv_strcmp(name, "646") == 0) {
174385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /*
174485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho         * The default codepage given by Solaris is 646 but the C library routines treat it as if it was
174585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho         * ISO-8859-1 instead of US-ASCII(646).
174685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho         */
174785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        name = "ISO-8859-1";
174885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
1749103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM_IS_DARWIN_BASED
1750b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (locale == NULL && *name == 0) {
1751b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /*
1752b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        No locale was specified, and an empty name was passed in.
1753b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        This usually indicates that nl_langinfo didn't return valid information.
1754b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        Mac OS X uses UTF-8 by default (especially the locale data and console).
1755b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
1756b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = "UTF-8";
1757b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
175885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    else if (uprv_strcmp(name, "CP949") == 0) {
175985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* Remap CP949 to a similar codepage to avoid issues with backslash and won symbol. */
176085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        name = "EUC-KR";
176185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
176250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    else if (locale != NULL && uprv_strcmp(locale, "en_US_POSIX") != 0 && uprv_strcmp(name, "US-ASCII") == 0) {
176350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        /*
176450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho         * For non C/POSIX locale, default the code page to UTF-8 instead of US-ASCII.
176550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho         */
176650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        name = "UTF-8";
176750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    }
1768103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_BSD
176985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (uprv_strcmp(name, "CP949") == 0) {
177085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* Remap CP949 to a similar codepage to avoid issues with backslash and won symbol. */
177185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        name = "EUC-KR";
177285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
1773103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_HPUX
177485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if (locale != NULL && uprv_strcmp(locale, "zh_HK") == 0 && uprv_strcmp(name, "big5") == 0) {
177585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* HP decided to extend big5 as hkbig5 even though it's not compatible :-( */
177685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        /* zh_TW.big5 is not the same charset as zh_HK.big5! */
177785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        name = "hkbig5";
177885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
177985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    else if (uprv_strcmp(name, "eucJP") == 0) {
1780b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /*
1781b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-1350 is the best match, but unavailable.
1782b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-954 is mostly a superset of ibm-1350.
1783b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-33722 is the default for eucJP (similar to Windows).
1784b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
1785b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = "eucjis";
1786b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1787103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_LINUX
1788b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (locale != NULL && uprv_strcmp(name, "euc") == 0) {
1789b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Linux underspecifies the "EUC" name. */
1790b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (uprv_strcmp(locale, "korean") == 0) {
1791b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            name = "EUC-KR";
1792b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1793b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else if (uprv_strcmp(locale, "japanese") == 0) {
1794b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            /* See comment below about eucJP */
1795b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            name = "eucjis";
1796b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1797b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1798b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    else if (uprv_strcmp(name, "eucjp") == 0) {
1799b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /*
1800b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-1350 is the best match, but unavailable.
1801b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-954 is mostly a superset of ibm-1350.
1802b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        ibm-33722 is the default for eucJP (similar to Windows).
1803b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        */
1804b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = "eucjis";
1805b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
180650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    else if (locale != NULL && uprv_strcmp(locale, "en_US_POSIX") != 0 &&
180750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho            (uprv_strcmp(name, "ANSI_X3.4-1968") == 0 || uprv_strcmp(name, "US-ASCII") == 0)) {
180850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        /*
180950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho         * For non C/POSIX locale, default the code page to UTF-8 instead of US-ASCII.
181050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho         */
181150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        name = "UTF-8";
181250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    }
181327f654740f2a26ad62a5c155af9199af9e69b889claireho    /*
181427f654740f2a26ad62a5c155af9199af9e69b889claireho     * Linux returns ANSI_X3.4-1968 for C/POSIX, but the call site takes care of
181527f654740f2a26ad62a5c155af9199af9e69b889claireho     * it by falling back to 'US-ASCII' when NULL is returned from this
181627f654740f2a26ad62a5c155af9199af9e69b889claireho     * function. So, we don't have to worry about it here.
181727f654740f2a26ad62a5c155af9199af9e69b889claireho     */
1818b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1819b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* return NULL when "" is passed in */
1820b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (*name == 0) {
1821b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = NULL;
1822b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1823b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return name;
1824b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1825b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
182685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic const char*
1827b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QuerugetCodepageFromPOSIXID(const char *localeName, char * buffer, int32_t buffCapacity)
1828b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
1829b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    char localeBuf[100];
1830b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *name = NULL;
1831b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    char *variant = NULL;
1832b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1833b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (localeName != NULL && (name = (uprv_strchr(localeName, '.'))) != NULL) {
1834b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        size_t localeCapacity = uprv_min(sizeof(localeBuf), (name-localeName)+1);
1835b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        uprv_strncpy(localeBuf, localeName, localeCapacity);
1836b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        localeBuf[localeCapacity-1] = 0; /* ensure NULL termination */
1837b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = uprv_strncpy(buffer, name+1, buffCapacity);
1838b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        buffer[buffCapacity-1] = 0; /* ensure NULL termination */
1839103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius        if ((variant = const_cast<char *>(uprv_strchr(name, '@'))) != NULL) {
1840b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            *variant = 0;
1841b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1842b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = remapPlatformDependentCodepage(localeBuf, name);
1843b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1844b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return name;
1845b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1846b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1847b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
184885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hostatic const char*
1849b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruint_getDefaultCodepage()
1850b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
1851103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM == U_PF_OS400
1852b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint32_t ccsid = 37; /* Default to ibm-37 */
1853b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    static char codepage[64];
1854b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    Qwc_JOBI0400_t jobinfo;
1855b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    Qus_EC_t error = { sizeof(Qus_EC_t) }; /* SPI error code */
1856b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1857b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    EPT_CALL(QUSRJOBI)(&jobinfo, sizeof(jobinfo), "JOBI0400",
1858b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        "*                         ", "                ", &error);
1859b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1860b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (error.Bytes_Available == 0) {
1861b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (jobinfo.Coded_Char_Set_ID != 0xFFFF) {
1862b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            ccsid = (uint32_t)jobinfo.Coded_Char_Set_ID;
1863b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1864b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        else if (jobinfo.Default_Coded_Char_Set_Id != 0xFFFF) {
1865b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            ccsid = (uint32_t)jobinfo.Default_Coded_Char_Set_Id;
1866b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1867b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* else use the default */
1868b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1869b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    sprintf(codepage,"ibm-%d", ccsid);
1870b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return codepage;
1871b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1872103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM == U_PF_OS390
1873b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    static char codepage[64];
187485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
187585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    strncpy(codepage, nl_langinfo(CODESET),63-strlen(UCNV_SWAP_LFNL_OPTION_STRING));
187685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    strcat(codepage,UCNV_SWAP_LFNL_OPTION_STRING);
1877b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    codepage[63] = 0; /* NULL terminate */
187885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
1879b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return codepage;
1880b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1881103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#elif U_PLATFORM_USES_ONLY_WIN32_API
1882b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    static char codepage[64];
1883b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    sprintf(codepage, "windows-%d", GetACP());
1884b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return codepage;
1885b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1886b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#elif U_POSIX_LOCALE
1887b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    static char codesetName[100];
1888b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *localeName = NULL;
1889b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    const char *name = NULL;
1890b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
189127f654740f2a26ad62a5c155af9199af9e69b889claireho    localeName = uprv_getPOSIXIDForDefaultCodepage();
1892b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uprv_memset(codesetName, 0, sizeof(codesetName));
18938de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert    /* On Solaris nl_langinfo returns C locale values unless setlocale
18948de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert     * was called earlier.
18958de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert     */
18968de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert#if (U_HAVE_NL_LANGINFO_CODESET && U_PLATFORM != U_PF_SOLARIS)
189727f654740f2a26ad62a5c155af9199af9e69b889claireho    /* When available, check nl_langinfo first because it usually gives more
189827f654740f2a26ad62a5c155af9199af9e69b889claireho       useful names. It depends on LC_CTYPE.
1899b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru       nl_langinfo may use the same buffer as setlocale. */
1900b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {
1901b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        const char *codeset = nl_langinfo(U_NL_LANGINFO_CODESET);
1902103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if U_PLATFORM_IS_DARWIN_BASED || U_PLATFORM_IS_LINUX_BASED
190350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        /*
190450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho         * On Linux and MacOSX, ensure that default codepage for non C/POSIX locale is UTF-8
190550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho         * instead of ASCII.
190650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho         */
190750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        if (uprv_strcmp(localeName, "en_US_POSIX") != 0) {
190850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho            codeset = remapPlatformDependentCodepage(localeName, codeset);
190950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        } else
191050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
191150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        {
191250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho            codeset = remapPlatformDependentCodepage(NULL, codeset);
191350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        }
191450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
1915b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if (codeset != NULL) {
1916b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            uprv_strncpy(codesetName, codeset, sizeof(codesetName));
1917b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            codesetName[sizeof(codesetName)-1] = 0;
1918b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            return codesetName;
1919b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1920b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1921b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1922b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
192327f654740f2a26ad62a5c155af9199af9e69b889claireho    /* Use setlocale in a nice way, and then check some environment variables.
192427f654740f2a26ad62a5c155af9199af9e69b889claireho       Maybe the application used setlocale already.
192527f654740f2a26ad62a5c155af9199af9e69b889claireho    */
192627f654740f2a26ad62a5c155af9199af9e69b889claireho    uprv_memset(codesetName, 0, sizeof(codesetName));
192727f654740f2a26ad62a5c155af9199af9e69b889claireho    name = getCodepageFromPOSIXID(localeName, codesetName, sizeof(codesetName));
192827f654740f2a26ad62a5c155af9199af9e69b889claireho    if (name) {
192927f654740f2a26ad62a5c155af9199af9e69b889claireho        /* if we can find the codeset name from setlocale, return that. */
193027f654740f2a26ad62a5c155af9199af9e69b889claireho        return name;
193127f654740f2a26ad62a5c155af9199af9e69b889claireho    }
193227f654740f2a26ad62a5c155af9199af9e69b889claireho
1933b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (*codesetName == 0)
1934b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    {
1935b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* Everything failed. Return US ASCII (ISO 646). */
1936b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        (void)uprv_strcpy(codesetName, "US-ASCII");
1937b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1938b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return codesetName;
1939b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#else
1940b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return "US-ASCII";
1941b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru#endif
1942b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1943b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1944b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1945b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI const char*  U_EXPORT2
1946b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruuprv_getDefaultCodepage()
1947b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru{
1948b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    static char const  *name = NULL;
1949b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    umtx_lock(NULL);
1950b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if (name == NULL) {
1951b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        name = int_getDefaultCodepage();
1952b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1953b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    umtx_unlock(NULL);
1954b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    return name;
1955b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
195685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif  /* !U_CHARSET_IS_UTF8 */
1957b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1958b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1959b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* end of platform-specific implementation -------------- */
1960b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1961b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/* version handling --------------------------------------------------------- */
1962b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1963b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI void U_EXPORT2
1964b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruu_versionFromString(UVersionInfo versionArray, const char *versionString) {
1965b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    char *end;
1966b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint16_t part=0;
1967b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1968b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(versionArray==NULL) {
1969b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return;
1970b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1971b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1972b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(versionString!=NULL) {
1973b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        for(;;) {
1974b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            versionArray[part]=(uint8_t)uprv_strtoul(versionString, &end, 10);
1975b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            if(end==versionString || ++part==U_MAX_VERSION_LENGTH || *end!=U_VERSION_DELIMITER) {
1976b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru                break;
1977b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            }
1978b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            versionString=end+1;
1979b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
1980b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1981b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1982b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    while(part<U_MAX_VERSION_LENGTH) {
1983b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        versionArray[part++]=0;
1984b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
1985b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
1986b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
1987b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI void U_EXPORT2
198885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hou_versionFromUString(UVersionInfo versionArray, const UChar *versionString) {
198985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    if(versionArray!=NULL && versionString!=NULL) {
199085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        char versionChars[U_MAX_VERSION_STRING_LENGTH+1];
199185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        int32_t len = u_strlen(versionString);
199285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        if(len>U_MAX_VERSION_STRING_LENGTH) {
199385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho            len = U_MAX_VERSION_STRING_LENGTH;
199485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        }
199585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        u_UCharsToChars(versionString, versionChars, len);
199650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho        versionChars[len]=0;
199785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho        u_versionFromString(versionArray, versionChars);
199885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho    }
199985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho}
200085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho
200185bf2e2fbc60a9f938064abc8127d61da7d19882Claire HoU_CAPI void U_EXPORT2
2002103e9ffba2cba345d0078eb8b8db33249f81840aCraig Corneliusu_versionToString(const UVersionInfo versionArray, char *versionString) {
2003b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint16_t count, part;
2004b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    uint8_t field;
2005b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2006b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(versionString==NULL) {
2007b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return;
2008b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
2009b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2010b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(versionArray==NULL) {
2011b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        versionString[0]=0;
2012b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        return;
2013b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
2014b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2015b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* count how many fields need to be written */
2016b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    for(count=4; count>0 && versionArray[count-1]==0; --count) {
2017b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
2018b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2019b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(count <= 1) {
2020b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        count = 2;
2021b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
2022b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2023b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* write the first part */
2024b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* write the decimal field value */
2025b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    field=versionArray[0];
2026b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(field>=100) {
2027b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *versionString++=(char)('0'+field/100);
2028b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        field%=100;
2029b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
2030b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    if(field>=10) {
2031b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *versionString++=(char)('0'+field/10);
2032b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        field%=10;
2033b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
2034b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    *versionString++=(char)('0'+field);
2035b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2036b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* write the following parts */
2037b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    for(part=1; part<count; ++part) {
2038b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* write a dot first */
2039b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *versionString++=U_VERSION_DELIMITER;
2040b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2041b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        /* write the decimal field value */
2042b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        field=versionArray[part];
2043b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if(field>=100) {
2044b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            *versionString++=(char)('0'+field/100);
2045b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            field%=100;
2046b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
2047b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        if(field>=10) {
2048b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            *versionString++=(char)('0'+field/10);
2049b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru            field%=10;
2050b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        }
2051b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru        *versionString++=(char)('0'+field);
2052b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    }
2053b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2054b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    /* NUL-terminate */
2055b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    *versionString=0;
2056b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
2057b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
2058b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste QueruU_CAPI void U_EXPORT2
2059b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queruu_getVersion(UVersionInfo versionArray) {
2060fceb39872958b9fa2505e63f8b8699a9e0f882f4ccornelius    (void)copyright;   // Suppress unused variable warning from clang.
2061b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru    u_versionFromString(versionArray, U_ICU_VERSION);
2062b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru}
2063b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru
206450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho/**
206550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho * icucfg.h dependent code
206650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho */
206750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
206850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#if U_ENABLE_DYLOAD
206950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
207054dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius#if HAVE_DLOPEN && !U_PLATFORM_USES_ONLY_WIN32_API
207150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
2072103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#if HAVE_DLFCN_H
207350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
207450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#ifdef __MVS__
207550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#ifndef __SUSV3
207650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#define __SUSV3 1
207750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
207850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
207950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#include <dlfcn.h>
208050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
208150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
208250294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void * U_EXPORT2
208350294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_open(const char *libName, UErrorCode *status) {
208427f654740f2a26ad62a5c155af9199af9e69b889claireho  void *ret = NULL;
208527f654740f2a26ad62a5c155af9199af9e69b889claireho  if(U_FAILURE(*status)) return ret;
208627f654740f2a26ad62a5c155af9199af9e69b889claireho  ret =  dlopen(libName, RTLD_NOW|RTLD_GLOBAL);
208727f654740f2a26ad62a5c155af9199af9e69b889claireho  if(ret==NULL) {
2088103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#ifdef U_TRACE_DYLOAD
2089103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius    printf("dlerror on dlopen(%s): %s\n", libName, dlerror());
209027f654740f2a26ad62a5c155af9199af9e69b889claireho#endif
209127f654740f2a26ad62a5c155af9199af9e69b889claireho    *status = U_MISSING_RESOURCE_ERROR;
209227f654740f2a26ad62a5c155af9199af9e69b889claireho  }
209327f654740f2a26ad62a5c155af9199af9e69b889claireho  return ret;
209450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
209550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
209650294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void U_EXPORT2
209750294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_close(void *lib, UErrorCode *status) {
209827f654740f2a26ad62a5c155af9199af9e69b889claireho  if(U_FAILURE(*status)) return;
209927f654740f2a26ad62a5c155af9199af9e69b889claireho  dlclose(lib);
210050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
210150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
2102b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehoU_INTERNAL UVoidFunction* U_EXPORT2
2103b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehouprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
2104b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  union {
2105103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius      UVoidFunction *fp;
2106103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius      void *vp;
2107103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius  } uret;
2108103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius  uret.fp = NULL;
2109103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius  if(U_FAILURE(*status)) return uret.fp;
2110103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius  uret.vp = dlsym(lib, sym);
2111103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius  if(uret.vp == NULL) {
2112103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#ifdef U_TRACE_DYLOAD
2113103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius    printf("dlerror on dlsym(%p,%s): %s\n", lib,sym, dlerror());
2114103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius#endif
211527f654740f2a26ad62a5c155af9199af9e69b889claireho    *status = U_MISSING_RESOURCE_ERROR;
211627f654740f2a26ad62a5c155af9199af9e69b889claireho  }
2117103e9ffba2cba345d0078eb8b8db33249f81840aCraig Cornelius  return uret.fp;
211850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
211950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
212050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#else
212150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
212250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho/* null (nonexistent) implementation. */
212350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
212450294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void * U_EXPORT2
212550294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_open(const char *libName, UErrorCode *status) {
212627f654740f2a26ad62a5c155af9199af9e69b889claireho  if(U_FAILURE(*status)) return NULL;
212727f654740f2a26ad62a5c155af9199af9e69b889claireho  *status = U_UNSUPPORTED_ERROR;
212827f654740f2a26ad62a5c155af9199af9e69b889claireho  return NULL;
212950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
213050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
213150294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void U_EXPORT2
213250294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_close(void *lib, UErrorCode *status) {
213327f654740f2a26ad62a5c155af9199af9e69b889claireho  if(U_FAILURE(*status)) return;
213427f654740f2a26ad62a5c155af9199af9e69b889claireho  *status = U_UNSUPPORTED_ERROR;
213527f654740f2a26ad62a5c155af9199af9e69b889claireho  return;
213650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
213750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
213850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
2139b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehoU_INTERNAL UVoidFunction* U_EXPORT2
2140b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehouprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
2141b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  if(U_SUCCESS(*status)) {
2142b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho    *status = U_UNSUPPORTED_ERROR;
2143b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  }
2144b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  return (UVoidFunction*)NULL;
214550294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
214650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
214750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
214850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
214950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif
215050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
215154dcd9b6a06071f647dac967e9e267abb9410720Craig Cornelius#elif U_PLATFORM_USES_ONLY_WIN32_API
215250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
215350294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void * U_EXPORT2
215450294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_open(const char *libName, UErrorCode *status) {
215527f654740f2a26ad62a5c155af9199af9e69b889claireho  HMODULE lib = NULL;
215627f654740f2a26ad62a5c155af9199af9e69b889claireho
215727f654740f2a26ad62a5c155af9199af9e69b889claireho  if(U_FAILURE(*status)) return NULL;
215827f654740f2a26ad62a5c155af9199af9e69b889claireho
2159b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  lib = LoadLibraryA(libName);
216027f654740f2a26ad62a5c155af9199af9e69b889claireho
216127f654740f2a26ad62a5c155af9199af9e69b889claireho  if(lib==NULL) {
216227f654740f2a26ad62a5c155af9199af9e69b889claireho    *status = U_MISSING_RESOURCE_ERROR;
216327f654740f2a26ad62a5c155af9199af9e69b889claireho  }
216427f654740f2a26ad62a5c155af9199af9e69b889claireho
216527f654740f2a26ad62a5c155af9199af9e69b889claireho  return (void*)lib;
216650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
216750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
216850294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void U_EXPORT2
216950294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_close(void *lib, UErrorCode *status) {
217027f654740f2a26ad62a5c155af9199af9e69b889claireho  HMODULE handle = (HMODULE)lib;
217127f654740f2a26ad62a5c155af9199af9e69b889claireho  if(U_FAILURE(*status)) return;
217227f654740f2a26ad62a5c155af9199af9e69b889claireho
217327f654740f2a26ad62a5c155af9199af9e69b889claireho  FreeLibrary(handle);
217427f654740f2a26ad62a5c155af9199af9e69b889claireho
217527f654740f2a26ad62a5c155af9199af9e69b889claireho  return;
217650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
217750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
217850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
2179b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehoU_INTERNAL UVoidFunction* U_EXPORT2
2180b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehouprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
218127f654740f2a26ad62a5c155af9199af9e69b889claireho  HMODULE handle = (HMODULE)lib;
2182b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  UVoidFunction* addr = NULL;
218327f654740f2a26ad62a5c155af9199af9e69b889claireho
218427f654740f2a26ad62a5c155af9199af9e69b889claireho  if(U_FAILURE(*status) || lib==NULL) return NULL;
218527f654740f2a26ad62a5c155af9199af9e69b889claireho
2186b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  addr = (UVoidFunction*)GetProcAddress(handle, sym);
218727f654740f2a26ad62a5c155af9199af9e69b889claireho
218827f654740f2a26ad62a5c155af9199af9e69b889claireho  if(addr==NULL) {
218927f654740f2a26ad62a5c155af9199af9e69b889claireho    DWORD lastError = GetLastError();
219027f654740f2a26ad62a5c155af9199af9e69b889claireho    if(lastError == ERROR_PROC_NOT_FOUND) {
219127f654740f2a26ad62a5c155af9199af9e69b889claireho      *status = U_MISSING_RESOURCE_ERROR;
219227f654740f2a26ad62a5c155af9199af9e69b889claireho    } else {
219327f654740f2a26ad62a5c155af9199af9e69b889claireho      *status = U_UNSUPPORTED_ERROR; /* other unknown error. */
219427f654740f2a26ad62a5c155af9199af9e69b889claireho    }
219527f654740f2a26ad62a5c155af9199af9e69b889claireho  }
219627f654740f2a26ad62a5c155af9199af9e69b889claireho
219727f654740f2a26ad62a5c155af9199af9e69b889claireho  return addr;
219850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
219950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
220050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
220150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#else
220250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
220350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho/* No dynamic loading set. */
220450294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
220550294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void * U_EXPORT2
220650294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_open(const char *libName, UErrorCode *status) {
22078de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert    (void)libName;
220850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    if(U_FAILURE(*status)) return NULL;
220950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    *status = U_UNSUPPORTED_ERROR;
221050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    return NULL;
221150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
221250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
221350294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehoU_INTERNAL void U_EXPORT2
221450294ead5e5d23f5bbfed76e00e6b510bd41eee1clairehouprv_dl_close(void *lib, UErrorCode *status) {
22158de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert    (void)lib;
221650294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    if(U_FAILURE(*status)) return;
221750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    *status = U_UNSUPPORTED_ERROR;
221850294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    return;
221950294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
222050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
222150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
2222b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehoU_INTERNAL UVoidFunction* U_EXPORT2
2223b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2clairehouprv_dlsym_func(void *lib, const char* sym, UErrorCode *status) {
22248de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert  (void)lib;
22258de051c3d18a56cc126f0f44e368495a52f9148cFredrik Roubert  (void)sym;
2226b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  if(U_SUCCESS(*status)) {
222750294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho    *status = U_UNSUPPORTED_ERROR;
2228b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  }
2229b26ce3a7367e4ed2ee7ddddcdc3f3d3377a455c2claireho  return (UVoidFunction*)NULL;
223050294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho}
223150294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
223250294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho#endif /* U_ENABLE_DYLOAD */
223350294ead5e5d23f5bbfed76e00e6b510bd41eee1claireho
2234b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru/*
2235b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * Hey, Emacs, please set the following:
2236b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru *
2237b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * Local Variables:
2238b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * indent-tabs-mode: nil
2239b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru * End:
2240b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru *
2241b13da9df870a61b11249bf741347908dbea0edd8Jean-Baptiste Queru */
2242