2 ******************************************************************************
4 * Copyright (C) 1997-2016, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
9 * FILE NAME : putil.c (previously putil.cpp and ptypes.cpp)
11 * Date Name Description
12 * 04/14/97 aliu Creation.
13 * 04/24/97 aliu Added getDefaultDataDirectory() and
14 * getDefaultLocaleID().
15 * 04/28/97 aliu Rewritten to assume Unix and apply general methods
16 * for assumed case. Non-UNIX platforms must be
17 * special-cased. Rewrote numeric methods dealing
18 * with NaN and Infinity to be platform independent
19 * over all IEEE 754 platforms.
20 * 05/13/97 aliu Restored sign of timezone
21 * (semantics are hours West of GMT)
22 * 06/16/98 erm Added IEEE_754 stuff, cleaned up isInfinite, isNan,
24 * 07/22/98 stephen Added remainder, max, min, trunc
25 * 08/13/98 stephen Added isNegativeInfinity, isPositiveInfinity
26 * 08/24/98 stephen Added longBitsFromDouble
27 * 09/08/98 stephen Minor changes for Mac Port
28 * 03/02/99 stephen Removed openFile(). Added AS400 support.
30 * 04/15/99 stephen Converted to C.
31 * 06/28/99 stephen Removed mutex locking in u_isBigEndian().
32 * 08/04/99 jeffrey R. Added OS/2 changes
33 * 11/15/99 helena Integrated S/390 IEEE support.
34 * 04/26/01 Barry N. OS/400 support for uprv_getDefaultLocaleID
35 * 08/15/01 Steven H. OS/400 support for uprv_getDefaultCodepage
36 * 01/03/08 Steven L. Fake Time Support
37 ******************************************************************************
40 // Defines _XOPEN_SOURCE for access to POSIX functions.
41 // Must be before any other #includes.
42 #include "uposixdefs.h"
44 /* include ICU headers */
45 #include "unicode/utypes.h"
46 #include "unicode/putil.h"
47 #include "unicode/ustring.h"
57 /* Include standard headers. */
65 #ifndef U_COMMON_IMPLEMENTATION
66 #error U_COMMON_IMPLEMENTATION not set - must be set for all ICU source files in common/ - see http://userguide.icu-project.org/howtouseicu
70 /* include system headers */
71 #if U_PLATFORM_USES_ONLY_WIN32_API
73 * TODO: U_PLATFORM_USES_ONLY_WIN32_API includes MinGW.
74 * Should Cygwin be included as well (U_PLATFORM_HAS_WIN32_API)
75 * to use native APIs as much as possible?
77 # define WIN32_LEAN_AND_MEAN
85 #elif U_PLATFORM == U_PF_OS400
87 # include <qusec.h> /* error code structure */
88 # include <qusrjobi.h>
89 # include <qliept.h> /* EPT_CALL macro - this include must be after all other "QSYSINCs" */
90 # include <mih/testptr.h> /* For uprv_maximumPtr */
91 #elif U_PLATFORM == U_PF_OS390
92 # include "unicode/ucnv.h" /* Needed for UCNV_SWAP_LFNL_OPTION_STRING */
93 #elif U_PLATFORM_IS_DARWIN_BASED || U_PLATFORM_IS_LINUX_BASED || U_PLATFORM == U_PF_BSD || U_PLATFORM == U_PF_SOLARIS
96 # if U_PLATFORM == U_PF_SOLARIS
101 #elif U_PLATFORM == U_PF_QNX
102 # include <sys/neutrino.h>
105 #if (U_PF_MINGW <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN) && defined(__STRICT_ANSI__)
106 /* tzset isn't defined in strict ANSI on Cygwin and MinGW. */
107 #undef __STRICT_ANSI__
111 * Cygwin with GCC requires inclusion of time.h after the above disabling strict asci mode statement.
115 #if !U_PLATFORM_USES_ONLY_WIN32_API
116 #include <sys/time.h>
120 * Only include langinfo.h if we have a way to get the codeset. If we later
121 * depend on more feature, we can test on U_HAVE_NL_LANGINFO.
125 #if U_HAVE_NL_LANGINFO_CODESET
126 #include <langinfo.h>
130 * Simple things (presence of functions, etc) should just go in configure.in and be added to
131 * icucfg.h via autoheader.
133 #if U_PLATFORM_IMPLEMENTS_POSIX
134 # if U_PLATFORM == U_PF_OS400
135 # define HAVE_DLFCN_H 0
136 # define HAVE_DLOPEN 0
138 # ifndef HAVE_DLFCN_H
139 # define HAVE_DLFCN_H 1
142 # define HAVE_DLOPEN 1
145 # ifndef HAVE_GETTIMEOFDAY
146 # define HAVE_GETTIMEOFDAY 1
149 # define HAVE_DLFCN_H 0
150 # define HAVE_DLOPEN 0
151 # define HAVE_GETTIMEOFDAY 0
156 /* Define the extension for data files, again... */
157 #define DATA_TYPE "dat"
159 /* Leave this copyright notice here! */
160 static const char copyright
[] = U_COPYRIGHT_STRING
;
162 /* floating point implementations ------------------------------------------- */
164 /* We return QNAN rather than SNAN*/
165 #define SIGN 0x80000000U
167 /* Make it easy to define certain types of constants */
169 int64_t i64
; /* This must be defined first in order to allow the initialization to work. This is a C89 feature. */
171 } BitPatternConversion
;
172 static const BitPatternConversion gNan
= { (int64_t) INT64_C(0x7FF8000000000000) };
173 static const BitPatternConversion gInf
= { (int64_t) INT64_C(0x7FF0000000000000) };
175 /*---------------------------------------------------------------------------
177 Our general strategy is to assume we're on a POSIX platform. Platforms which
178 are non-POSIX must declare themselves so. The default POSIX implementation
179 will sometimes work for non-POSIX platforms as well (e.g., the NaN-related
181 ---------------------------------------------------------------------------*/
183 #if U_PLATFORM_USES_ONLY_WIN32_API || U_PLATFORM == U_PF_OS400
184 # undef U_POSIX_LOCALE
186 # define U_POSIX_LOCALE 1
190 WARNING! u_topNBytesOfDouble and u_bottomNBytesOfDouble
191 can't be properly optimized by the gcc compiler sometimes (i.e. gcc 3.2).
195 u_topNBytesOfDouble(double* d
, int n
)
200 return (char*)(d
+ 1) - n
;
205 u_bottomNBytesOfDouble(double* d
, int n
)
208 return (char*)(d
+ 1) - n
;
213 #endif /* !IEEE_754 */
217 u_signBit(double d
) {
220 hiByte
= *(uint8_t *)&d
;
222 hiByte
= *(((uint8_t *)&d
) + sizeof(double) - 1);
224 return (hiByte
& 0x80) != 0;
230 #if defined (U_DEBUG_FAKETIME)
231 /* Override the clock to test things without having to move the system clock.
232 * Assumes POSIX gettimeofday() will function
234 UDate fakeClock_t0
= 0; /** Time to start the clock from **/
235 UDate fakeClock_dt
= 0; /** Offset (fake time - real time) **/
236 UBool fakeClock_set
= FALSE
; /** True if fake clock has spun up **/
237 static UMutex fakeClockMutex
= U_MUTEX_INTIALIZER
;
239 static UDate
getUTCtime_real() {
240 struct timeval posixTime
;
241 gettimeofday(&posixTime
, NULL
);
242 return (UDate
)(((int64_t)posixTime
.tv_sec
* U_MILLIS_PER_SECOND
) + (posixTime
.tv_usec
/1000));
245 static UDate
getUTCtime_fake() {
246 umtx_lock(&fakeClockMutex
);
248 UDate real
= getUTCtime_real();
249 const char *fake_start
= getenv("U_FAKETIME_START");
250 if((fake_start
!=NULL
) && (fake_start
[0]!=0)) {
251 sscanf(fake_start
,"%lf",&fakeClock_t0
);
252 fakeClock_dt
= fakeClock_t0
- real
;
253 fprintf(stderr
,"U_DEBUG_FAKETIME was set at compile time, so the ICU clock will start at a preset value\n"
254 "env variable U_FAKETIME_START=%.0f (%s) for an offset of %.0f ms from the current time %.0f\n",
255 fakeClock_t0
, fake_start
, fakeClock_dt
, real
);
258 fprintf(stderr
,"U_DEBUG_FAKETIME was set at compile time, but U_FAKETIME_START was not set.\n"
259 "Set U_FAKETIME_START to the number of milliseconds since 1/1/1970 to set the ICU clock.\n");
261 fakeClock_set
= TRUE
;
263 umtx_unlock(&fakeClockMutex
);
265 return getUTCtime_real() + fakeClock_dt
;
269 #if U_PLATFORM_USES_ONLY_WIN32_API
273 } FileTimeConversion
; /* This is like a ULARGE_INTEGER */
275 /* Number of 100 nanoseconds from 1/1/1601 to 1/1/1970 */
276 #define EPOCH_BIAS INT64_C(116444736000000000)
277 #define HECTONANOSECOND_PER_MILLISECOND 10000
281 /*---------------------------------------------------------------------------
282 Universal Implementations
283 These are designed to work on all platforms. Try these, and if they
284 don't work on your platform, then special case your platform with new
286 ---------------------------------------------------------------------------*/
288 U_CAPI UDate U_EXPORT2
291 #if defined(U_DEBUG_FAKETIME)
292 return getUTCtime_fake(); /* Hook for overriding the clock */
294 return uprv_getRawUTCtime();
298 /* Return UTC (GMT) time measured in milliseconds since 0:00 on 1/1/70.*/
299 U_CAPI UDate U_EXPORT2
302 #if U_PLATFORM_USES_ONLY_WIN32_API
304 FileTimeConversion winTime
;
305 GetSystemTimeAsFileTime(&winTime
.fileTime
);
306 return (UDate
)((winTime
.int64
- EPOCH_BIAS
) / HECTONANOSECOND_PER_MILLISECOND
);
309 #if HAVE_GETTIMEOFDAY
310 struct timeval posixTime
;
311 gettimeofday(&posixTime
, NULL
);
312 return (UDate
)(((int64_t)posixTime
.tv_sec
* U_MILLIS_PER_SECOND
) + (posixTime
.tv_usec
/1000));
316 return (UDate
)epochtime
* U_MILLIS_PER_SECOND
;
322 /*-----------------------------------------------------------------------------
324 These methods detect and return NaN and infinity values for doubles
325 conforming to IEEE 754. Platforms which support this standard include X86,
326 Mac 680x0, Mac PowerPC, AIX RS/6000, and most others.
327 If this doesn't work on your platform, you have non-IEEE floating-point, and
328 will need to code your own versions. A naive implementation is to return 0.0
329 for getNaN and getInfinity, and false for isNaN and isInfinite.
330 ---------------------------------------------------------------------------*/
332 U_CAPI UBool U_EXPORT2
333 uprv_isNaN(double number
)
336 BitPatternConversion convertedNumber
;
337 convertedNumber
.d64
= number
;
338 /* Infinity is 0x7FF0000000000000U. Anything greater than that is a NaN */
339 return (UBool
)((convertedNumber
.i64
& U_INT64_MAX
) > gInf
.i64
);
341 #elif U_PLATFORM == U_PF_OS390
342 uint32_t highBits
= *(uint32_t*)u_topNBytesOfDouble(&number
,
344 uint32_t lowBits
= *(uint32_t*)u_bottomNBytesOfDouble(&number
,
347 return ((highBits
& 0x7F080000L
) == 0x7F080000L
) &&
348 (lowBits
== 0x00000000L
);
351 /* If your platform doesn't support IEEE 754 but *does* have an NaN value,*/
352 /* you'll need to replace this default implementation with what's correct*/
353 /* for your platform.*/
354 return number
!= number
;
358 U_CAPI UBool U_EXPORT2
359 uprv_isInfinite(double number
)
362 BitPatternConversion convertedNumber
;
363 convertedNumber
.d64
= number
;
364 /* Infinity is exactly 0x7FF0000000000000U. */
365 return (UBool
)((convertedNumber
.i64
& U_INT64_MAX
) == gInf
.i64
);
366 #elif U_PLATFORM == U_PF_OS390
367 uint32_t highBits
= *(uint32_t*)u_topNBytesOfDouble(&number
,
369 uint32_t lowBits
= *(uint32_t*)u_bottomNBytesOfDouble(&number
,
372 return ((highBits
& ~SIGN
) == 0x70FF0000L
) && (lowBits
== 0x00000000L
);
375 /* If your platform doesn't support IEEE 754 but *does* have an infinity*/
376 /* value, you'll need to replace this default implementation with what's*/
377 /* correct for your platform.*/
378 return number
== (2.0 * number
);
382 U_CAPI UBool U_EXPORT2
383 uprv_isPositiveInfinity(double number
)
385 #if IEEE_754 || U_PLATFORM == U_PF_OS390
386 return (UBool
)(number
> 0 && uprv_isInfinite(number
));
388 return uprv_isInfinite(number
);
392 U_CAPI UBool U_EXPORT2
393 uprv_isNegativeInfinity(double number
)
395 #if IEEE_754 || U_PLATFORM == U_PF_OS390
396 return (UBool
)(number
< 0 && uprv_isInfinite(number
));
399 uint32_t highBits
= *(uint32_t*)u_topNBytesOfDouble(&number
,
401 return((highBits
& SIGN
) && uprv_isInfinite(number
));
406 U_CAPI
double U_EXPORT2
409 #if IEEE_754 || U_PLATFORM == U_PF_OS390
412 /* If your platform doesn't support IEEE 754 but *does* have an NaN value,*/
413 /* you'll need to replace this default implementation with what's correct*/
414 /* for your platform.*/
419 U_CAPI
double U_EXPORT2
422 #if IEEE_754 || U_PLATFORM == U_PF_OS390
425 /* If your platform doesn't support IEEE 754 but *does* have an infinity*/
426 /* value, you'll need to replace this default implementation with what's*/
427 /* correct for your platform.*/
432 U_CAPI
double U_EXPORT2
438 U_CAPI
double U_EXPORT2
444 U_CAPI
double U_EXPORT2
447 return uprv_floor(x
+ 0.5);
450 U_CAPI
double U_EXPORT2
456 U_CAPI
double U_EXPORT2
457 uprv_modf(double x
, double* y
)
462 U_CAPI
double U_EXPORT2
463 uprv_fmod(double x
, double y
)
468 U_CAPI
double U_EXPORT2
469 uprv_pow(double x
, double y
)
471 /* This is declared as "double pow(double x, double y)" */
475 U_CAPI
double U_EXPORT2
476 uprv_pow10(int32_t x
)
478 return pow(10.0, (double)x
);
481 U_CAPI
double U_EXPORT2
482 uprv_fmax(double x
, double y
)
485 /* first handle NaN*/
486 if(uprv_isNaN(x
) || uprv_isNaN(y
))
487 return uprv_getNaN();
489 /* check for -0 and 0*/
490 if(x
== 0.0 && y
== 0.0 && u_signBit(x
))
495 /* this should work for all flt point w/o NaN and Inf special cases */
496 return (x
> y
? x
: y
);
499 U_CAPI
double U_EXPORT2
500 uprv_fmin(double x
, double y
)
503 /* first handle NaN*/
504 if(uprv_isNaN(x
) || uprv_isNaN(y
))
505 return uprv_getNaN();
507 /* check for -0 and 0*/
508 if(x
== 0.0 && y
== 0.0 && u_signBit(y
))
513 /* this should work for all flt point w/o NaN and Inf special cases */
514 return (x
> y
? y
: x
);
518 * Truncates the given double.
519 * trunc(3.3) = 3.0, trunc (-3.3) = -3.0
520 * This is different than calling floor() or ceil():
521 * floor(3.3) = 3, floor(-3.3) = -4
522 * ceil(3.3) = 4, ceil(-3.3) = -3
524 U_CAPI
double U_EXPORT2
528 /* handle error cases*/
530 return uprv_getNaN();
531 if(uprv_isInfinite(d
))
532 return uprv_getInfinity();
534 if(u_signBit(d
)) /* Signbit() picks up -0.0; d<0 does not. */
540 return d
>= 0 ? floor(d
) : ceil(d
);
546 * Return the largest positive number that can be represented by an integer
547 * type of arbitrary bit length.
549 U_CAPI
double U_EXPORT2
550 uprv_maxMantissa(void)
552 return pow(2.0, DBL_MANT_DIG
+ 1.0) - 1.0;
555 U_CAPI
double U_EXPORT2
561 U_CAPI
void * U_EXPORT2
562 uprv_maximumPtr(void * base
)
564 #if U_PLATFORM == U_PF_OS400
566 * With the provided function we should never be out of range of a given segment
567 * (a traditional/typical segment that is). Our segments have 5 bytes for the
568 * id and 3 bytes for the offset. The key is that the casting takes care of
569 * only retrieving the offset portion minus x1000. Hence, the smallest offset
570 * seen in a program is x001000 and when casted to an int would be 0.
571 * That's why we can only add 0xffefff. Otherwise, we would exceed the segment.
573 * Currently, 16MB is the current addressing limitation on i5/OS if the activation is
574 * non-TERASPACE. If it is TERASPACE it is 2GB - 4k(header information).
575 * This function determines the activation based on the pointer that is passed in and
576 * calculates the appropriate maximum available size for
577 * each pointer type (TERASPACE and non-TERASPACE)
579 * Unlike other operating systems, the pointer model isn't determined at
580 * compile time on i5/OS.
582 if ((base
!= NULL
) && (_TESTPTR(base
, _C_TERASPACE_CHECK
))) {
583 /* if it is a TERASPACE pointer the max is 2GB - 4k */
584 return ((void *)(((char *)base
)-((uint32_t)(base
))+((uint32_t)0x7fffefff)));
586 /* otherwise 16MB since NULL ptr is not checkable or the ptr is not TERASPACE */
587 return ((void *)(((char *)base
)-((uint32_t)(base
))+((uint32_t)0xffefff)));
590 return U_MAX_PTR(base
);
594 /*---------------------------------------------------------------------------
595 Platform-specific Implementations
596 Try these, and if they don't work on your platform, then special case your
597 platform with new implementations.
598 ---------------------------------------------------------------------------*/
600 /* Generic time zone layer -------------------------------------------------- */
602 /* Time zone utilities */
603 U_CAPI
void U_EXPORT2
609 /* no initialization*/
613 U_CAPI
int32_t U_EXPORT2
624 uprv_memcpy( &tmrec
, localtime(&t
), sizeof(tmrec
) );
625 #if U_PLATFORM != U_PF_IPHONE
626 UBool dst_checked
= (tmrec
.tm_isdst
!= 0); /* daylight savings time is checked*/
628 t1
= mktime(&tmrec
); /* local time in seconds*/
629 uprv_memcpy( &tmrec
, gmtime(&t
), sizeof(tmrec
) );
630 t2
= mktime(&tmrec
); /* GMT (or UTC) in seconds*/
633 #if U_PLATFORM != U_PF_IPHONE
634 /* imitate NT behaviour, which returns same timezone offset to GMT for
636 This does not work on all platforms. For instance, on glibc on Linux
637 and on Mac OS 10.5, tdiff calculated above remains the same
638 regardless of whether DST is in effect or not. iOS is another
639 platform where this does not work. Linux + glibc and Mac OS 10.5
640 have U_TIMEZONE defined so that this code is not reached.
649 /* Note that U_TZNAME does *not* have to be tzname, but if it is,
650 some platforms need to have it declared here. */
652 #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))
653 /* RS6000 and others reject char **tzname. */
654 extern U_IMPORT
char *U_TZNAME
[];
657 #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)
658 /* These platforms are likely to use Olson timezone IDs. */
659 #define CHECK_LOCALTIME_LINK 1
660 #if U_PLATFORM_IS_DARWIN_BASED
662 #define TZZONEINFO (TZDIR "/")
663 #elif U_PLATFORM == U_PF_SOLARIS
664 #define TZDEFAULT "/etc/localtime"
665 #define TZZONEINFO "/usr/share/lib/zoneinfo/"
666 #define TZZONEINFO2 "../usr/share/lib/zoneinfo/"
667 #define TZ_ENV_CHECK "localtime"
669 #define TZDEFAULT "/etc/localtime"
670 #define TZZONEINFO "/usr/share/zoneinfo/"
673 #define TZFILE_SKIP "posixrules" /* tz file to skip when searching. */
674 /* Some Linux distributions have 'localtime' in /usr/share/zoneinfo
675 symlinked to /etc/localtime, which makes searchForTZFile return
676 'localtime' when it's the first match. */
677 #define TZFILE_SKIP2 "localtime"
678 #define SEARCH_TZFILE
679 #include <dirent.h> /* Needed to search through system timezone files */
681 static char gTimeZoneBuffer
[PATH_MAX
];
682 static char *gTimeZoneBufferPtr
= NULL
;
685 #if !U_PLATFORM_USES_ONLY_WIN32_API
686 #define isNonDigit(ch) (ch < '0' || '9' < ch)
687 static UBool
isValidOlsonID(const char *id
) {
690 /* Determine if this is something like Iceland (Olson ID)
691 or AST4ADT (non-Olson ID) */
692 while (id
[idx
] && isNonDigit(id
[idx
]) && id
[idx
] != ',') {
696 /* If we went through the whole string, then it might be okay.
697 The timezone is sometimes set to "CST-7CDT", "CST6CDT5,J129,J131/19:30",
698 "GRNLNDST3GRNLNDDT" or similar, so we cannot use it.
699 The rest of the time it could be an Olson ID. George */
700 return (UBool
)(id
[idx
] == 0
701 || uprv_strcmp(id
, "PST8PDT") == 0
702 || uprv_strcmp(id
, "MST7MDT") == 0
703 || uprv_strcmp(id
, "CST6CDT") == 0
704 || uprv_strcmp(id
, "EST5EDT") == 0);
707 /* On some Unix-like OS, 'posix' subdirectory in
708 /usr/share/zoneinfo replicates the top-level contents. 'right'
709 subdirectory has the same set of files, but individual files
710 are different from those in the top-level directory or 'posix'
711 because 'right' has files for TAI (Int'l Atomic Time) while 'posix'
713 When the first match for /etc/localtime is in either of them
714 (usually in posix because 'right' has different file contents),
715 or TZ environment variable points to one of them, createTimeZone
716 fails because, say, 'posix/America/New_York' is not an Olson
717 timezone id ('America/New_York' is). So, we have to skip
718 'posix/' and 'right/' at the beginning. */
719 static void skipZoneIDPrefix(const char** id
) {
720 if (uprv_strncmp(*id
, "posix/", 6) == 0
721 || uprv_strncmp(*id
, "right/", 6) == 0)
728 #if defined(U_TZNAME) && !U_PLATFORM_USES_ONLY_WIN32_API
730 #define CONVERT_HOURS_TO_SECONDS(offset) (int32_t)(offset*3600)
731 typedef struct OffsetZoneMapping
{
732 int32_t offsetSeconds
;
733 int32_t daylightType
; /* 0=U_DAYLIGHT_NONE, 1=daylight in June-U_DAYLIGHT_JUNE, 2=daylight in December=U_DAYLIGHT_DECEMBER*/
739 enum { U_DAYLIGHT_NONE
=0,U_DAYLIGHT_JUNE
=1,U_DAYLIGHT_DECEMBER
=2 };
742 This list tries to disambiguate a set of abbreviated timezone IDs and offsets
743 and maps it to an Olson ID.
744 Before adding anything to this list, take a look at
745 icu/source/tools/tzcode/tz.alias
746 Sometimes no daylight savings (0) is important to define due to aliases.
747 This list can be tested with icu/source/test/compat/tzone.pl
748 More values could be added to daylightType to increase precision.
750 static const struct OffsetZoneMapping OFFSET_ZONE_MAPPINGS
[] = {
751 {-45900, 2, "CHAST", "CHADT", "Pacific/Chatham"},
752 {-43200, 1, "PETT", "PETST", "Asia/Kamchatka"},
753 {-43200, 2, "NZST", "NZDT", "Pacific/Auckland"},
754 {-43200, 1, "ANAT", "ANAST", "Asia/Anadyr"},
755 {-39600, 1, "MAGT", "MAGST", "Asia/Magadan"},
756 {-37800, 2, "LHST", "LHST", "Australia/Lord_Howe"},
757 {-36000, 2, "EST", "EST", "Australia/Sydney"},
758 {-36000, 1, "SAKT", "SAKST", "Asia/Sakhalin"},
759 {-36000, 1, "VLAT", "VLAST", "Asia/Vladivostok"},
760 {-34200, 2, "CST", "CST", "Australia/South"},
761 {-32400, 1, "YAKT", "YAKST", "Asia/Yakutsk"},
762 {-32400, 1, "CHOT", "CHOST", "Asia/Choibalsan"},
763 {-31500, 2, "CWST", "CWST", "Australia/Eucla"},
764 {-28800, 1, "IRKT", "IRKST", "Asia/Irkutsk"},
765 {-28800, 1, "ULAT", "ULAST", "Asia/Ulaanbaatar"},
766 {-28800, 2, "WST", "WST", "Australia/West"},
767 {-25200, 1, "HOVT", "HOVST", "Asia/Hovd"},
768 {-25200, 1, "KRAT", "KRAST", "Asia/Krasnoyarsk"},
769 {-21600, 1, "NOVT", "NOVST", "Asia/Novosibirsk"},
770 {-21600, 1, "OMST", "OMSST", "Asia/Omsk"},
771 {-18000, 1, "YEKT", "YEKST", "Asia/Yekaterinburg"},
772 {-14400, 1, "SAMT", "SAMST", "Europe/Samara"},
773 {-14400, 1, "AMT", "AMST", "Asia/Yerevan"},
774 {-14400, 1, "AZT", "AZST", "Asia/Baku"},
775 {-10800, 1, "AST", "ADT", "Asia/Baghdad"},
776 {-10800, 1, "MSK", "MSD", "Europe/Moscow"},
777 {-10800, 1, "VOLT", "VOLST", "Europe/Volgograd"},
778 {-7200, 0, "EET", "CEST", "Africa/Tripoli"},
779 {-7200, 1, "EET", "EEST", "Europe/Athens"}, /* Conflicts with Africa/Cairo */
780 {-7200, 1, "IST", "IDT", "Asia/Jerusalem"},
781 {-3600, 0, "CET", "WEST", "Africa/Algiers"},
782 {-3600, 2, "WAT", "WAST", "Africa/Windhoek"},
783 {0, 1, "GMT", "IST", "Europe/Dublin"},
784 {0, 1, "GMT", "BST", "Europe/London"},
785 {0, 0, "WET", "WEST", "Africa/Casablanca"},
786 {0, 0, "WET", "WET", "Africa/El_Aaiun"},
787 {3600, 1, "AZOT", "AZOST", "Atlantic/Azores"},
788 {3600, 1, "EGT", "EGST", "America/Scoresbysund"},
789 {10800, 1, "PMST", "PMDT", "America/Miquelon"},
790 {10800, 2, "UYT", "UYST", "America/Montevideo"},
791 {10800, 1, "WGT", "WGST", "America/Godthab"},
792 {10800, 2, "BRT", "BRST", "Brazil/East"},
793 {12600, 1, "NST", "NDT", "America/St_Johns"},
794 {14400, 1, "AST", "ADT", "Canada/Atlantic"},
795 {14400, 2, "AMT", "AMST", "America/Cuiaba"},
796 {14400, 2, "CLT", "CLST", "Chile/Continental"},
797 {14400, 2, "FKT", "FKST", "Atlantic/Stanley"},
798 {14400, 2, "PYT", "PYST", "America/Asuncion"},
799 {18000, 1, "CST", "CDT", "America/Havana"},
800 {18000, 1, "EST", "EDT", "US/Eastern"}, /* Conflicts with America/Grand_Turk */
801 {21600, 2, "EAST", "EASST", "Chile/EasterIsland"},
802 {21600, 0, "CST", "MDT", "Canada/Saskatchewan"},
803 {21600, 0, "CST", "CDT", "America/Guatemala"},
804 {21600, 1, "CST", "CDT", "US/Central"}, /* Conflicts with Mexico/General */
805 {25200, 1, "MST", "MDT", "US/Mountain"}, /* Conflicts with Mexico/BajaSur */
806 {28800, 0, "PST", "PST", "Pacific/Pitcairn"},
807 {28800, 1, "PST", "PDT", "US/Pacific"}, /* Conflicts with Mexico/BajaNorte */
808 {32400, 1, "AKST", "AKDT", "US/Alaska"},
809 {36000, 1, "HAST", "HADT", "US/Aleutian"}
812 /*#define DEBUG_TZNAME*/
814 static const char* remapShortTimeZone(const char *stdID
, const char *dstID
, int32_t daylightType
, int32_t offset
)
818 fprintf(stderr
, "TZ=%s std=%s dst=%s daylight=%d offset=%d\n", getenv("TZ"), stdID
, dstID
, daylightType
, offset
);
820 for (idx
= 0; idx
< UPRV_LENGTHOF(OFFSET_ZONE_MAPPINGS
); idx
++)
822 if (offset
== OFFSET_ZONE_MAPPINGS
[idx
].offsetSeconds
823 && daylightType
== OFFSET_ZONE_MAPPINGS
[idx
].daylightType
824 && strcmp(OFFSET_ZONE_MAPPINGS
[idx
].stdID
, stdID
) == 0
825 && strcmp(OFFSET_ZONE_MAPPINGS
[idx
].dstID
, dstID
) == 0)
827 return OFFSET_ZONE_MAPPINGS
[idx
].olsonID
;
835 #define MAX_PATH_SIZE PATH_MAX /* Set the limit for the size of the path. */
836 #define MAX_READ_SIZE 512
838 typedef struct DefaultTZInfo
{
839 char* defaultTZBuffer
;
840 int64_t defaultTZFileSize
;
841 FILE* defaultTZFilePtr
;
842 UBool defaultTZstatus
;
843 int32_t defaultTZPosition
;
847 * This method compares the two files given to see if they are a match.
848 * It is currently use to compare two TZ files.
850 static UBool
compareBinaryFiles(const char* defaultTZFileName
, const char* TZFileName
, DefaultTZInfo
* tzInfo
) {
853 int64_t sizeFileLeft
;
854 int32_t sizeFileRead
;
855 int32_t sizeFileToRead
;
856 char bufferFile
[MAX_READ_SIZE
];
859 if (tzInfo
->defaultTZFilePtr
== NULL
) {
860 tzInfo
->defaultTZFilePtr
= fopen(defaultTZFileName
, "r");
862 file
= fopen(TZFileName
, "r");
864 tzInfo
->defaultTZPosition
= 0; /* reset position to begin search */
866 if (file
!= NULL
&& tzInfo
->defaultTZFilePtr
!= NULL
) {
867 /* First check that the file size are equal. */
868 if (tzInfo
->defaultTZFileSize
== 0) {
869 fseek(tzInfo
->defaultTZFilePtr
, 0, SEEK_END
);
870 tzInfo
->defaultTZFileSize
= ftell(tzInfo
->defaultTZFilePtr
);
872 fseek(file
, 0, SEEK_END
);
873 sizeFile
= ftell(file
);
874 sizeFileLeft
= sizeFile
;
876 if (sizeFile
!= tzInfo
->defaultTZFileSize
) {
879 /* Store the data from the files in seperate buffers and
880 * compare each byte to determine equality.
882 if (tzInfo
->defaultTZBuffer
== NULL
) {
883 rewind(tzInfo
->defaultTZFilePtr
);
884 tzInfo
->defaultTZBuffer
= (char*)uprv_malloc(sizeof(char) * tzInfo
->defaultTZFileSize
);
885 sizeFileRead
= fread(tzInfo
->defaultTZBuffer
, 1, tzInfo
->defaultTZFileSize
, tzInfo
->defaultTZFilePtr
);
888 while(sizeFileLeft
> 0) {
889 uprv_memset(bufferFile
, 0, MAX_READ_SIZE
);
890 sizeFileToRead
= sizeFileLeft
< MAX_READ_SIZE
? sizeFileLeft
: MAX_READ_SIZE
;
892 sizeFileRead
= fread(bufferFile
, 1, sizeFileToRead
, file
);
893 if (memcmp(tzInfo
->defaultTZBuffer
+ tzInfo
->defaultTZPosition
, bufferFile
, sizeFileRead
) != 0) {
897 sizeFileLeft
-= sizeFileRead
;
898 tzInfo
->defaultTZPosition
+= sizeFileRead
;
912 * This method recursively traverses the directory given for a matching TZ file and returns the first match.
914 /* dirent also lists two entries: "." and ".." that we can safely ignore. */
917 static char SEARCH_TZFILE_RESULT
[MAX_PATH_SIZE
] = "";
918 static char* searchForTZFile(const char* path
, DefaultTZInfo
* tzInfo
) {
919 char curpath
[MAX_PATH_SIZE
];
920 DIR* dirp
= opendir(path
);
922 struct dirent
* dirEntry
= NULL
;
929 /* Save the current path */
930 uprv_memset(curpath
, 0, MAX_PATH_SIZE
);
931 uprv_strcpy(curpath
, path
);
933 /* Check each entry in the directory. */
934 while((dirEntry
= readdir(dirp
)) != NULL
) {
935 const char* dirName
= dirEntry
->d_name
;
936 if (uprv_strcmp(dirName
, SKIP1
) != 0 && uprv_strcmp(dirName
, SKIP2
) != 0) {
937 /* Create a newpath with the new entry to test each entry in the directory. */
938 char newpath
[MAX_PATH_SIZE
];
939 uprv_strcpy(newpath
, curpath
);
940 uprv_strcat(newpath
, dirName
);
942 if ((subDirp
= opendir(newpath
)) != NULL
) {
943 /* If this new path is a directory, make a recursive call with the newpath. */
945 uprv_strcat(newpath
, "/");
946 result
= searchForTZFile(newpath
, tzInfo
);
948 Have to get out here. Otherwise, we'd keep looking
949 and return the first match in the top-level directory
950 if there's a match in the top-level. If not, this function
951 would return NULL and set gTimeZoneBufferPtr to NULL in initDefault().
952 It worked without this in most cases because we have a fallback of calling
953 localtime_r to figure out the default timezone.
957 } else if (uprv_strcmp(TZFILE_SKIP
, dirName
) != 0 && uprv_strcmp(TZFILE_SKIP2
, dirName
) != 0) {
958 if(compareBinaryFiles(TZDEFAULT
, newpath
, tzInfo
)) {
959 const char* zoneid
= newpath
+ (sizeof(TZZONEINFO
)) - 1;
960 skipZoneIDPrefix(&zoneid
);
961 uprv_strcpy(SEARCH_TZFILE_RESULT
, zoneid
);
962 result
= SEARCH_TZFILE_RESULT
;
963 /* Get out after the first one found. */
973 U_CAPI
const char* U_EXPORT2
976 const char *tzid
= NULL
;
977 #if U_PLATFORM_USES_ONLY_WIN32_API
978 tzid
= uprv_detectWindowsTimeZone();
985 /*#if U_PLATFORM_IS_DARWIN_BASED
988 tzid = getenv("TZFILE");
994 /* This code can be temporarily disabled to test tzname resolution later on. */
997 if (tzid
!= NULL
&& isValidOlsonID(tzid
)
998 #if U_PLATFORM == U_PF_SOLARIS
999 /* When TZ equals localtime on Solaris, check the /etc/localtime file. */
1000 && uprv_strcmp(tzid
, TZ_ENV_CHECK
) != 0
1003 /* The colon forces tzset() to treat the remainder as zoneinfo path */
1004 if (tzid
[0] == ':') {
1007 /* This might be a good Olson ID. */
1008 skipZoneIDPrefix(&tzid
);
1011 /* else U_TZNAME will give a better result. */
1014 #if defined(CHECK_LOCALTIME_LINK) && !defined(DEBUG_SKIP_LOCALTIME_LINK)
1015 /* Caller must handle threading issues */
1016 if (gTimeZoneBufferPtr
== NULL
) {
1018 This is a trick to look at the name of the link to get the Olson ID
1019 because the tzfile contents is underspecified.
1020 This isn't guaranteed to work because it may not be a symlink.
1022 int32_t ret
= (int32_t)readlink(TZDEFAULT
, gTimeZoneBuffer
, sizeof(gTimeZoneBuffer
));
1024 int32_t tzZoneInfoLen
= uprv_strlen(TZZONEINFO
);
1025 gTimeZoneBuffer
[ret
] = 0;
1026 if (uprv_strncmp(gTimeZoneBuffer
, TZZONEINFO
, tzZoneInfoLen
) == 0
1027 && isValidOlsonID(gTimeZoneBuffer
+ tzZoneInfoLen
))
1029 return (gTimeZoneBufferPtr
= gTimeZoneBuffer
+ tzZoneInfoLen
);
1031 #if U_PLATFORM == U_PF_SOLARIS
1034 tzZoneInfoLen
= uprv_strlen(TZZONEINFO2
);
1035 if (uprv_strncmp(gTimeZoneBuffer
, TZZONEINFO2
, tzZoneInfoLen
) == 0
1036 && isValidOlsonID(gTimeZoneBuffer
+ tzZoneInfoLen
))
1038 return (gTimeZoneBufferPtr
= gTimeZoneBuffer
+ tzZoneInfoLen
);
1043 #if defined(SEARCH_TZFILE)
1044 DefaultTZInfo
* tzInfo
= (DefaultTZInfo
*)uprv_malloc(sizeof(DefaultTZInfo
));
1045 if (tzInfo
!= NULL
) {
1046 tzInfo
->defaultTZBuffer
= NULL
;
1047 tzInfo
->defaultTZFileSize
= 0;
1048 tzInfo
->defaultTZFilePtr
= NULL
;
1049 tzInfo
->defaultTZstatus
= FALSE
;
1050 tzInfo
->defaultTZPosition
= 0;
1052 gTimeZoneBufferPtr
= searchForTZFile(TZZONEINFO
, tzInfo
);
1054 /* Free previously allocated memory */
1055 if (tzInfo
->defaultTZBuffer
!= NULL
) {
1056 uprv_free(tzInfo
->defaultTZBuffer
);
1058 if (tzInfo
->defaultTZFilePtr
!= NULL
) {
1059 fclose(tzInfo
->defaultTZFilePtr
);
1064 if (gTimeZoneBufferPtr
!= NULL
&& isValidOlsonID(gTimeZoneBufferPtr
)) {
1065 return gTimeZoneBufferPtr
;
1071 return gTimeZoneBufferPtr
;
1077 #if U_PLATFORM_USES_ONLY_WIN32_API
1078 /* The return value is free'd in timezone.cpp on Windows because
1079 * the other code path returns a pointer to a heap location. */
1080 return uprv_strdup(U_TZNAME
[n
]);
1083 U_TZNAME is usually a non-unique abbreviation, which isn't normally usable.
1084 So we remap the abbreviation to an olson ID.
1086 Since Windows exposes a little more timezone information,
1087 we normally don't use this code on Windows because
1088 uprv_detectWindowsTimeZone should have already given the correct answer.
1091 struct tm juneSol
, decemberSol
;
1093 static const time_t juneSolstice
=1182478260; /*2007-06-21 18:11 UT*/
1094 static const time_t decemberSolstice
=1198332540; /*2007-12-22 06:09 UT*/
1096 /* This probing will tell us when daylight savings occurs. */
1097 localtime_r(&juneSolstice
, &juneSol
);
1098 localtime_r(&decemberSolstice
, &decemberSol
);
1099 if(decemberSol
.tm_isdst
> 0) {
1100 daylightType
= U_DAYLIGHT_DECEMBER
;
1101 } else if(juneSol
.tm_isdst
> 0) {
1102 daylightType
= U_DAYLIGHT_JUNE
;
1104 daylightType
= U_DAYLIGHT_NONE
;
1106 tzid
= remapShortTimeZone(U_TZNAME
[0], U_TZNAME
[1], daylightType
, uprv_timezone());
1118 /* Get and set the ICU data directory --------------------------------------- */
1120 static icu::UInitOnce gDataDirInitOnce
= U_INITONCE_INITIALIZER
;
1121 static char *gDataDirectory
= NULL
;
1123 UInitOnce gTimeZoneFilesInitOnce
= U_INITONCE_INITIALIZER
;
1124 static CharString
*gTimeZoneFilesDirectory
= NULL
;
1126 #if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API
1127 static char *gCorrectedPOSIXLocale
= NULL
; /* Heap allocated */
1130 static UBool U_CALLCONV
putil_cleanup(void)
1132 if (gDataDirectory
&& *gDataDirectory
) {
1133 uprv_free(gDataDirectory
);
1135 gDataDirectory
= NULL
;
1136 gDataDirInitOnce
.reset();
1138 delete gTimeZoneFilesDirectory
;
1139 gTimeZoneFilesDirectory
= NULL
;
1140 gTimeZoneFilesInitOnce
.reset();
1142 #if U_POSIX_LOCALE || U_PLATFORM_USES_ONLY_WIN32_API
1143 if (gCorrectedPOSIXLocale
) {
1144 uprv_free(gCorrectedPOSIXLocale
);
1145 gCorrectedPOSIXLocale
= NULL
;
1152 * Set the data directory.
1153 * Make a copy of the passed string, and set the global data dir to point to it.
1155 U_CAPI
void U_EXPORT2
1156 u_setDataDirectory(const char *directory
) {
1160 if(directory
==NULL
|| *directory
==0) {
1161 /* A small optimization to prevent the malloc and copy when the
1162 shared library is used, and this is a way to make sure that NULL
1165 newDataDir
= (char *)"";
1168 length
=(int32_t)uprv_strlen(directory
);
1169 newDataDir
= (char *)uprv_malloc(length
+ 2);
1170 /* Exit out if newDataDir could not be created. */
1171 if (newDataDir
== NULL
) {
1174 uprv_strcpy(newDataDir
, directory
);
1176 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
1179 while(p
= uprv_strchr(newDataDir
, U_FILE_ALT_SEP_CHAR
)) {
1180 *p
= U_FILE_SEP_CHAR
;
1186 if (gDataDirectory
&& *gDataDirectory
) {
1187 uprv_free(gDataDirectory
);
1189 gDataDirectory
= newDataDir
;
1190 ucln_common_registerCleanup(UCLN_COMMON_PUTIL
, putil_cleanup
);
1193 U_CAPI UBool U_EXPORT2
1194 uprv_pathIsAbsolute(const char *path
)
1196 if(!path
|| !*path
) {
1200 if(*path
== U_FILE_SEP_CHAR
) {
1204 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
1205 if(*path
== U_FILE_ALT_SEP_CHAR
) {
1210 #if U_PLATFORM_USES_ONLY_WIN32_API
1211 if( (((path
[0] >= 'A') && (path
[0] <= 'Z')) ||
1212 ((path
[0] >= 'a') && (path
[0] <= 'z'))) &&
1221 /* Temporary backup setting of ICU_DATA_DIR_PREFIX_ENV_VAR
1222 until some client wrapper makefiles are updated */
1223 #if U_PLATFORM_IS_DARWIN_BASED && TARGET_IPHONE_SIMULATOR
1224 # if !defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
1225 # define ICU_DATA_DIR_PREFIX_ENV_VAR "IPHONE_SIMULATOR_ROOT"
1229 static void U_CALLCONV
dataDirectoryInitFn() {
1230 /* If we already have the directory, then return immediately. Will happen if user called
1231 * u_setDataDirectory().
1233 if (gDataDirectory
) {
1237 const char *path
= NULL
;
1238 #if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
1239 char datadir_path_buffer
[PATH_MAX
];
1243 When ICU_NO_USER_DATA_OVERRIDE is defined, users aren't allowed to
1244 override ICU's data with the ICU_DATA environment variable. This prevents
1245 problems where multiple custom copies of ICU's specific version of data
1246 are installed on a system. Either the application must define the data
1247 directory with u_setDataDirectory, define ICU_DATA_DIR when compiling
1248 ICU, set the data with udata_setCommonData or trust that all of the
1249 required data is contained in ICU's data library that contains
1250 the entry point defined by U_ICUDATA_ENTRY_POINT.
1252 There may also be some platforms where environment variables
1255 # if !defined(ICU_NO_USER_DATA_OVERRIDE) && !UCONFIG_NO_FILE_IO
1256 /* First try to get the environment variable */
1257 path
=getenv("ICU_DATA");
1260 /* ICU_DATA_DIR may be set as a compile option.
1261 * U_ICU_DATA_DEFAULT_DIR is provided and is set by ICU at compile time
1262 * and is used only when data is built in archive mode eliminating the need
1263 * for ICU_DATA_DIR to be set. U_ICU_DATA_DEFAULT_DIR is set to the installation
1264 * directory of the data dat file. Users should use ICU_DATA_DIR if they want to
1265 * set their own path.
1267 #if defined(ICU_DATA_DIR) || defined(U_ICU_DATA_DEFAULT_DIR)
1268 if(path
==NULL
|| *path
==0) {
1269 # if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
1270 const char *prefix
= getenv(ICU_DATA_DIR_PREFIX_ENV_VAR
);
1272 # ifdef ICU_DATA_DIR
1275 path
=U_ICU_DATA_DEFAULT_DIR
;
1277 # if defined(ICU_DATA_DIR_PREFIX_ENV_VAR)
1278 if (prefix
!= NULL
) {
1279 snprintf(datadir_path_buffer
, PATH_MAX
, "%s%s", prefix
, path
);
1280 path
=datadir_path_buffer
;
1287 /* It looks really bad, set it to something. */
1291 u_setDataDirectory(path
);
1295 U_CAPI
const char * U_EXPORT2
1296 u_getDataDirectory(void) {
1297 umtx_initOnce(gDataDirInitOnce
, &dataDirectoryInitFn
);
1298 return gDataDirectory
;
1301 static void setTimeZoneFilesDir(const char *path
, UErrorCode
&status
) {
1302 if (U_FAILURE(status
)) {
1305 gTimeZoneFilesDirectory
->clear();
1306 gTimeZoneFilesDirectory
->append(path
, status
);
1307 #if (U_FILE_SEP_CHAR != U_FILE_ALT_SEP_CHAR)
1308 char *p
= gTimeZoneFilesDirectory
->data();
1309 while (p
= uprv_strchr(p
, U_FILE_ALT_SEP_CHAR
)) {
1310 *p
= U_FILE_SEP_CHAR
;
1315 #if U_PLATFORM_IMPLEMENTS_POSIX
1316 #include <sys/stat.h>
1317 #if defined(U_TIMEZONE_FILES_DIR)
1318 const char tzdirbuf
[] = U_TIMEZONE_FILES_DIR
;
1319 enum { kTzfilenamebufLen
= UPRV_LENGTHOF(tzdirbuf
) + 24 }; // extra room for "/icutz44l.dat" or "/zoneinfo64.res"
1323 #define TO_STRING(x) TO_STRING_2(x)
1324 #define TO_STRING_2(x) #x
1326 static void U_CALLCONV
TimeZoneDataDirInitFn(UErrorCode
&status
) {
1327 U_ASSERT(gTimeZoneFilesDirectory
== NULL
);
1328 ucln_common_registerCleanup(UCLN_COMMON_PUTIL
, putil_cleanup
);
1329 gTimeZoneFilesDirectory
= new CharString();
1330 if (gTimeZoneFilesDirectory
== NULL
) {
1331 status
= U_MEMORY_ALLOCATION_ERROR
;
1334 const char *dir
= getenv("ICU_TIMEZONE_FILES_DIR");
1335 UBool usingUTzFilesDir
= FALSE
;
1336 #if defined(U_TIMEZONE_FILES_DIR)
1338 // dir = TO_STRING(U_TIMEZONE_FILES_DIR);
1339 // Not sure why the above was done for this path only;
1340 // it preserves unwanted quotes.
1342 usingUTzFilesDir
= TRUE
;
1345 #if U_PLATFORM_IMPLEMENTS_POSIX
1348 if (stat(dir
, &buf
) != 0) {
1351 #if defined(U_TIMEZONE_FILES_DIR)
1352 else if (usingUTzFilesDir
) {
1353 char tzfilenamebuf
[kTzfilenamebufLen
];
1354 uprv_strcpy(tzfilenamebuf
, tzdirbuf
);
1355 uprv_strcat(tzfilenamebuf
, U_FILE_SEP_STRING
);
1356 #if defined(U_TIMEZONE_PACKAGE)
1357 uprv_strcat(tzfilenamebuf
, U_TIMEZONE_PACKAGE
);
1358 uprv_strcat(tzfilenamebuf
, ".dat");
1360 uprv_strcat(tzfilenamebuf
, "zoneinfo64.res");
1362 if (stat(tzfilenamebuf
, &buf
) != 0) {
1366 #endif /* defined(U_TIMEZONE_FILES_DIR) */
1368 #endif /* U_PLATFORM_IMPLEMENTS_POSIX */
1372 setTimeZoneFilesDir(dir
, status
);
1376 U_CAPI
const char * U_EXPORT2
1377 u_getTimeZoneFilesDirectory(UErrorCode
*status
) {
1378 umtx_initOnce(gTimeZoneFilesInitOnce
, &TimeZoneDataDirInitFn
, *status
);
1379 return U_SUCCESS(*status
) ? gTimeZoneFilesDirectory
->data() : "";
1382 U_CAPI
void U_EXPORT2
1383 u_setTimeZoneFilesDirectory(const char *path
, UErrorCode
*status
) {
1384 umtx_initOnce(gTimeZoneFilesInitOnce
, &TimeZoneDataDirInitFn
, *status
);
1385 setTimeZoneFilesDir(path
, *status
);
1387 // Note: this function does some extra churn, first setting based on the
1388 // environment, then immediately replacing with the value passed in.
1389 // The logic is simpler that way, and performance shouldn't be an issue.
1394 /* A helper function used by uprv_getPOSIXIDForDefaultLocale and
1395 * uprv_getPOSIXIDForDefaultCodepage. Returns the posix locale id for
1396 * LC_CTYPE and LC_MESSAGES. It doesn't support other locale categories.
1398 static const char *uprv_getPOSIXIDForCategory(int category
)
1400 const char* posixID
= NULL
;
1401 if (category
== LC_MESSAGES
|| category
== LC_CTYPE
) {
1403 * On Solaris two different calls to setlocale can result in
1404 * different values. Only get this value once.
1406 * We must check this first because an application can set this.
1408 * LC_ALL can't be used because it's platform dependent. The LANG
1409 * environment variable seems to affect LC_CTYPE variable by default.
1410 * Here is what setlocale(LC_ALL, NULL) can return.
1411 * HPUX can return 'C C C C C C C'
1412 * Solaris can return /en_US/C/C/C/C/C on the second try.
1413 * Linux can return LC_CTYPE=C;LC_NUMERIC=C;...
1415 * The default codepage detection also needs to use LC_CTYPE.
1417 * Do not call setlocale(LC_*, "")! Using an empty string instead
1418 * of NULL, will modify the libc behavior.
1420 posixID
= setlocale(category
, NULL
);
1422 || (uprv_strcmp("C", posixID
) == 0)
1423 || (uprv_strcmp("POSIX", posixID
) == 0))
1425 /* Maybe we got some garbage. Try something more reasonable */
1426 posixID
= getenv("LC_ALL");
1427 /* Solaris speaks POSIX - See IEEE Std 1003.1-2008
1428 * This is needed to properly handle empty env. variables
1430 #if U_PLATFORM == U_PF_SOLARIS
1431 if ((posixID
== 0) || (posixID
[0] == '\0')) {
1432 posixID
= getenv(category
== LC_MESSAGES
? "LC_MESSAGES" : "LC_CTYPE");
1433 if ((posixID
== 0) || (posixID
[0] == '\0')) {
1436 posixID
= getenv(category
== LC_MESSAGES
? "LC_MESSAGES" : "LC_CTYPE");
1439 posixID
= getenv("LANG");
1445 || (uprv_strcmp("C", posixID
) == 0)
1446 || (uprv_strcmp("POSIX", posixID
) == 0))
1448 /* Nothing worked. Give it a nice POSIX default value. */
1449 posixID
= "en_US_POSIX";
1454 /* Return just the POSIX id for the default locale, whatever happens to be in
1455 * it. It gets the value from LC_MESSAGES and indirectly from LC_ALL and LANG.
1457 static const char *uprv_getPOSIXIDForDefaultLocale(void)
1459 static const char* posixID
= NULL
;
1461 posixID
= uprv_getPOSIXIDForCategory(LC_MESSAGES
);
1466 #if !U_CHARSET_IS_UTF8
1467 /* Return just the POSIX id for the default codepage, whatever happens to be in
1468 * it. It gets the value from LC_CTYPE and indirectly from LC_ALL and LANG.
1470 static const char *uprv_getPOSIXIDForDefaultCodepage(void)
1472 static const char* posixID
= NULL
;
1474 posixID
= uprv_getPOSIXIDForCategory(LC_CTYPE
);
1481 /* NOTE: The caller should handle thread safety */
1482 U_CAPI
const char* U_EXPORT2
1483 uprv_getDefaultLocaleID()
1487 Note that: (a '!' means the ID is improper somehow)
1488 LC_ALL ----> default_loc codepage
1489 --------------------------------------------------------
1494 ab_CD.EF@GH ab_CD_GH EF
1496 Some 'improper' ways to do the same as above:
1497 ! ab_CD@GH.EF ab_CD_GH EF
1498 ! ab_CD.EF@GH.IJ ab_CD_GH EF
1499 ! ab_CD@ZZ.EF@GH.IJ ab_CD_GH EF
1504 The variant cannot have dots in it.
1505 The 'rightmost' variant (@xxx) wins.
1506 The leftmost codepage (.xxx) wins.
1508 char *correctedPOSIXLocale
= 0;
1509 const char* posixID
= uprv_getPOSIXIDForDefaultLocale();
1514 /* Format: (no spaces)
1515 ll [ _CC ] [ . MM ] [ @ VV]
1517 l = lang, C = ctry, M = charmap, V = variant
1520 if (gCorrectedPOSIXLocale
!= NULL
) {
1521 return gCorrectedPOSIXLocale
;
1524 if ((p
= uprv_strchr(posixID
, '.')) != NULL
) {
1525 /* assume new locale can't be larger than old one? */
1526 correctedPOSIXLocale
= static_cast<char *>(uprv_malloc(uprv_strlen(posixID
)+1));
1527 /* Exit on memory allocation error. */
1528 if (correctedPOSIXLocale
== NULL
) {
1531 uprv_strncpy(correctedPOSIXLocale
, posixID
, p
-posixID
);
1532 correctedPOSIXLocale
[p
-posixID
] = 0;
1534 /* do not copy after the @ */
1535 if ((p
= uprv_strchr(correctedPOSIXLocale
, '@')) != NULL
) {
1536 correctedPOSIXLocale
[p
-correctedPOSIXLocale
] = 0;
1540 /* Note that we scan the *uncorrected* ID. */
1541 if ((p
= uprv_strrchr(posixID
, '@')) != NULL
) {
1542 if (correctedPOSIXLocale
== NULL
) {
1543 correctedPOSIXLocale
= static_cast<char *>(uprv_malloc(uprv_strlen(posixID
)+1));
1544 /* Exit on memory allocation error. */
1545 if (correctedPOSIXLocale
== NULL
) {
1548 uprv_strncpy(correctedPOSIXLocale
, posixID
, p
-posixID
);
1549 correctedPOSIXLocale
[p
-posixID
] = 0;
1553 /* Take care of any special cases here.. */
1554 if (!uprv_strcmp(p
, "nynorsk")) {
1556 /* Don't worry about no__NY. In practice, it won't appear. */
1559 if (uprv_strchr(correctedPOSIXLocale
,'_') == NULL
) {
1560 uprv_strcat(correctedPOSIXLocale
, "__"); /* aa@b -> aa__b */
1563 uprv_strcat(correctedPOSIXLocale
, "_"); /* aa_CC@b -> aa_CC_b */
1566 if ((q
= uprv_strchr(p
, '.')) != NULL
) {
1567 /* How big will the resulting string be? */
1568 len
= (int32_t)(uprv_strlen(correctedPOSIXLocale
) + (q
-p
));
1569 uprv_strncat(correctedPOSIXLocale
, p
, q
-p
);
1570 correctedPOSIXLocale
[len
] = 0;
1573 /* Anything following the @ sign */
1574 uprv_strcat(correctedPOSIXLocale
, p
);
1577 /* Should there be a map from 'no@nynorsk' -> no_NO_NY here?
1578 * How about 'russian' -> 'ru'?
1579 * Many of the other locales using ISO codes will be handled by the
1580 * canonicalization functions in uloc_getDefault.
1584 /* Was a correction made? */
1585 if (correctedPOSIXLocale
!= NULL
) {
1586 posixID
= correctedPOSIXLocale
;
1589 /* copy it, just in case the original pointer goes away. See j2395 */
1590 correctedPOSIXLocale
= (char *)uprv_malloc(uprv_strlen(posixID
) + 1);
1591 /* Exit on memory allocation error. */
1592 if (correctedPOSIXLocale
== NULL
) {
1595 posixID
= uprv_strcpy(correctedPOSIXLocale
, posixID
);
1598 if (gCorrectedPOSIXLocale
== NULL
) {
1599 gCorrectedPOSIXLocale
= correctedPOSIXLocale
;
1600 ucln_common_registerCleanup(UCLN_COMMON_PUTIL
, putil_cleanup
);
1601 correctedPOSIXLocale
= NULL
;
1604 if (correctedPOSIXLocale
!= NULL
) { /* Was already set - clean up. */
1605 uprv_free(correctedPOSIXLocale
);
1610 #elif U_PLATFORM_USES_ONLY_WIN32_API
1611 #define POSIX_LOCALE_CAPACITY 64
1612 UErrorCode status
= U_ZERO_ERROR
;
1613 char *correctedPOSIXLocale
= 0;
1615 if (gCorrectedPOSIXLocale
!= NULL
) {
1616 return gCorrectedPOSIXLocale
;
1619 LCID id
= GetThreadLocale();
1620 correctedPOSIXLocale
= static_cast<char *>(uprv_malloc(POSIX_LOCALE_CAPACITY
+ 1));
1621 if (correctedPOSIXLocale
) {
1622 int32_t posixLen
= uprv_convertToPosix(id
, correctedPOSIXLocale
, POSIX_LOCALE_CAPACITY
, &status
);
1623 if (U_SUCCESS(status
)) {
1624 *(correctedPOSIXLocale
+ posixLen
) = 0;
1625 gCorrectedPOSIXLocale
= correctedPOSIXLocale
;
1626 ucln_common_registerCleanup(UCLN_COMMON_PUTIL
, putil_cleanup
);
1628 uprv_free(correctedPOSIXLocale
);
1632 if (gCorrectedPOSIXLocale
== NULL
) {
1635 return gCorrectedPOSIXLocale
;
1637 #elif U_PLATFORM == U_PF_OS400
1638 /* locales are process scoped and are by definition thread safe */
1639 static char correctedLocale
[64];
1640 const char *localeID
= getenv("LC_ALL");
1643 if (localeID
== NULL
)
1644 localeID
= getenv("LANG");
1645 if (localeID
== NULL
)
1646 localeID
= setlocale(LC_ALL
, NULL
);
1647 /* Make sure we have something... */
1648 if (localeID
== NULL
)
1649 return "en_US_POSIX";
1651 /* Extract the locale name from the path. */
1652 if((p
= uprv_strrchr(localeID
, '/')) != NULL
)
1654 /* Increment p to start of locale name. */
1659 /* Copy to work location. */
1660 uprv_strcpy(correctedLocale
, localeID
);
1662 /* Strip off the '.locale' extension. */
1663 if((p
= uprv_strchr(correctedLocale
, '.')) != NULL
) {
1667 /* Upper case the locale name. */
1668 T_CString_toUpperCase(correctedLocale
);
1670 /* See if we are using the POSIX locale. Any of the
1671 * following are equivalent and use the same QLGPGCMA
1673 * QLGPGCMA2 means UCS2
1674 * QLGPGCMA_4 means UTF-32
1675 * QLGPGCMA_8 means UTF-8
1677 if ((uprv_strcmp("C", correctedLocale
) == 0) ||
1678 (uprv_strcmp("POSIX", correctedLocale
) == 0) ||
1679 (uprv_strncmp("QLGPGCMA", correctedLocale
, 8) == 0))
1681 uprv_strcpy(correctedLocale
, "en_US_POSIX");
1687 /* Lower case the lang portion. */
1688 for(p
= correctedLocale
; *p
!= 0 && *p
!= '_'; p
++)
1690 *p
= uprv_tolower(*p
);
1693 /* Adjust for Euro. After '_E' add 'URO'. */
1694 LocaleLen
= uprv_strlen(correctedLocale
);
1695 if (correctedLocale
[LocaleLen
- 2] == '_' &&
1696 correctedLocale
[LocaleLen
- 1] == 'E')
1698 uprv_strcat(correctedLocale
, "URO");
1701 /* If using Lotus-based locale then convert to
1702 * equivalent non Lotus.
1704 else if (correctedLocale
[LocaleLen
- 2] == '_' &&
1705 correctedLocale
[LocaleLen
- 1] == 'L')
1707 correctedLocale
[LocaleLen
- 2] = 0;
1710 /* There are separate simplified and traditional
1711 * locales called zh_HK_S and zh_HK_T.
1713 else if (uprv_strncmp(correctedLocale
, "zh_HK", 5) == 0)
1715 uprv_strcpy(correctedLocale
, "zh_HK");
1718 /* A special zh_CN_GBK locale...
1720 else if (uprv_strcmp(correctedLocale
, "zh_CN_GBK") == 0)
1722 uprv_strcpy(correctedLocale
, "zh_CN");
1727 return correctedLocale
;
1732 #if !U_CHARSET_IS_UTF8
1735 Due to various platform differences, one platform may specify a charset,
1736 when they really mean a different charset. Remap the names so that they are
1737 compatible with ICU. Only conflicting/ambiguous aliases should be resolved
1738 here. Before adding anything to this function, please consider adding unique
1739 names to the ICU alias table in the data directory.
1742 remapPlatformDependentCodepage(const char *locale
, const char *name
) {
1743 if (locale
!= NULL
&& *locale
== 0) {
1744 /* Make sure that an empty locale is handled the same way. */
1750 #if U_PLATFORM == U_PF_AIX
1751 if (uprv_strcmp(name
, "IBM-943") == 0) {
1752 /* Use the ASCII compatible ibm-943 */
1755 else if (uprv_strcmp(name
, "IBM-1252") == 0) {
1756 /* Use the windows-1252 that contains the Euro */
1759 #elif U_PLATFORM == U_PF_SOLARIS
1760 if (locale
!= NULL
&& uprv_strcmp(name
, "EUC") == 0) {
1761 /* Solaris underspecifies the "EUC" name. */
1762 if (uprv_strcmp(locale
, "zh_CN") == 0) {
1765 else if (uprv_strcmp(locale
, "zh_TW") == 0) {
1768 else if (uprv_strcmp(locale
, "ko_KR") == 0) {
1772 else if (uprv_strcmp(name
, "eucJP") == 0) {
1774 ibm-954 is the best match.
1775 ibm-33722 is the default for eucJP (similar to Windows).
1779 else if (uprv_strcmp(name
, "646") == 0) {
1781 * The default codepage given by Solaris is 646 but the C library routines treat it as if it was
1782 * ISO-8859-1 instead of US-ASCII(646).
1784 name
= "ISO-8859-1";
1786 #elif U_PLATFORM_IS_DARWIN_BASED
1787 if (locale
== NULL
&& *name
== 0) {
1789 No locale was specified, and an empty name was passed in.
1790 This usually indicates that nl_langinfo didn't return valid information.
1791 Mac OS X uses UTF-8 by default (especially the locale data and console).
1795 else if (uprv_strcmp(name
, "CP949") == 0) {
1796 /* Remap CP949 to a similar codepage to avoid issues with backslash and won symbol. */
1799 else if (locale
!= NULL
&& uprv_strcmp(locale
, "en_US_POSIX") != 0 && uprv_strcmp(name
, "US-ASCII") == 0) {
1801 * For non C/POSIX locale, default the code page to UTF-8 instead of US-ASCII.
1805 #elif U_PLATFORM == U_PF_BSD
1806 if (uprv_strcmp(name
, "CP949") == 0) {
1807 /* Remap CP949 to a similar codepage to avoid issues with backslash and won symbol. */
1810 #elif U_PLATFORM == U_PF_HPUX
1811 if (locale
!= NULL
&& uprv_strcmp(locale
, "zh_HK") == 0 && uprv_strcmp(name
, "big5") == 0) {
1812 /* HP decided to extend big5 as hkbig5 even though it's not compatible :-( */
1813 /* zh_TW.big5 is not the same charset as zh_HK.big5! */
1816 else if (uprv_strcmp(name
, "eucJP") == 0) {
1818 ibm-1350 is the best match, but unavailable.
1819 ibm-954 is mostly a superset of ibm-1350.
1820 ibm-33722 is the default for eucJP (similar to Windows).
1824 #elif U_PLATFORM == U_PF_LINUX
1825 if (locale
!= NULL
&& uprv_strcmp(name
, "euc") == 0) {
1826 /* Linux underspecifies the "EUC" name. */
1827 if (uprv_strcmp(locale
, "korean") == 0) {
1830 else if (uprv_strcmp(locale
, "japanese") == 0) {
1831 /* See comment below about eucJP */
1835 else if (uprv_strcmp(name
, "eucjp") == 0) {
1837 ibm-1350 is the best match, but unavailable.
1838 ibm-954 is mostly a superset of ibm-1350.
1839 ibm-33722 is the default for eucJP (similar to Windows).
1843 else if (locale
!= NULL
&& uprv_strcmp(locale
, "en_US_POSIX") != 0 &&
1844 (uprv_strcmp(name
, "ANSI_X3.4-1968") == 0 || uprv_strcmp(name
, "US-ASCII") == 0)) {
1846 * For non C/POSIX locale, default the code page to UTF-8 instead of US-ASCII.
1851 * Linux returns ANSI_X3.4-1968 for C/POSIX, but the call site takes care of
1852 * it by falling back to 'US-ASCII' when NULL is returned from this
1853 * function. So, we don't have to worry about it here.
1856 /* return NULL when "" is passed in */
1864 getCodepageFromPOSIXID(const char *localeName
, char * buffer
, int32_t buffCapacity
)
1866 char localeBuf
[100];
1867 const char *name
= NULL
;
1868 char *variant
= NULL
;
1870 if (localeName
!= NULL
&& (name
= (uprv_strchr(localeName
, '.'))) != NULL
) {
1871 size_t localeCapacity
= uprv_min(sizeof(localeBuf
), (name
-localeName
)+1);
1872 uprv_strncpy(localeBuf
, localeName
, localeCapacity
);
1873 localeBuf
[localeCapacity
-1] = 0; /* ensure NULL termination */
1874 name
= uprv_strncpy(buffer
, name
+1, buffCapacity
);
1875 buffer
[buffCapacity
-1] = 0; /* ensure NULL termination */
1876 if ((variant
= const_cast<char *>(uprv_strchr(name
, '@'))) != NULL
) {
1879 name
= remapPlatformDependentCodepage(localeBuf
, name
);
1886 int_getDefaultCodepage()
1888 #if U_PLATFORM == U_PF_OS400
1889 uint32_t ccsid
= 37; /* Default to ibm-37 */
1890 static char codepage
[64];
1891 Qwc_JOBI0400_t jobinfo
;
1892 Qus_EC_t error
= { sizeof(Qus_EC_t
) }; /* SPI error code */
1894 EPT_CALL(QUSRJOBI
)(&jobinfo
, sizeof(jobinfo
), "JOBI0400",
1897 if (error
.Bytes_Available
== 0) {
1898 if (jobinfo
.Coded_Char_Set_ID
!= 0xFFFF) {
1899 ccsid
= (uint32_t)jobinfo
.Coded_Char_Set_ID
;
1901 else if (jobinfo
.Default_Coded_Char_Set_Id
!= 0xFFFF) {
1902 ccsid
= (uint32_t)jobinfo
.Default_Coded_Char_Set_Id
;
1904 /* else use the default */
1906 sprintf(codepage
,"ibm-%d", ccsid
);
1909 #elif U_PLATFORM == U_PF_OS390
1910 static char codepage
[64];
1912 strncpy(codepage
, nl_langinfo(CODESET
),63-strlen(UCNV_SWAP_LFNL_OPTION_STRING
));
1913 strcat(codepage
,UCNV_SWAP_LFNL_OPTION_STRING
);
1914 codepage
[63] = 0; /* NULL terminate */
1918 #elif U_PLATFORM_USES_ONLY_WIN32_API
1919 static char codepage
[64];
1920 sprintf(codepage
, "windows-%d", GetACP());
1923 #elif U_POSIX_LOCALE
1924 static char codesetName
[100];
1925 const char *localeName
= NULL
;
1926 const char *name
= NULL
;
1928 localeName
= uprv_getPOSIXIDForDefaultCodepage();
1929 uprv_memset(codesetName
, 0, sizeof(codesetName
));
1930 /* On Solaris nl_langinfo returns C locale values unless setlocale
1931 * was called earlier.
1933 #if (U_HAVE_NL_LANGINFO_CODESET && U_PLATFORM != U_PF_SOLARIS)
1934 /* When available, check nl_langinfo first because it usually gives more
1935 useful names. It depends on LC_CTYPE.
1936 nl_langinfo may use the same buffer as setlocale. */
1938 const char *codeset
= nl_langinfo(U_NL_LANGINFO_CODESET
);
1939 #if U_PLATFORM_IS_DARWIN_BASED || U_PLATFORM_IS_LINUX_BASED
1941 * On Linux and MacOSX, ensure that default codepage for non C/POSIX locale is UTF-8
1944 if (uprv_strcmp(localeName
, "en_US_POSIX") != 0) {
1945 codeset
= remapPlatformDependentCodepage(localeName
, codeset
);
1949 codeset
= remapPlatformDependentCodepage(NULL
, codeset
);
1952 if (codeset
!= NULL
) {
1953 uprv_strncpy(codesetName
, codeset
, sizeof(codesetName
));
1954 codesetName
[sizeof(codesetName
)-1] = 0;
1960 /* Use setlocale in a nice way, and then check some environment variables.
1961 Maybe the application used setlocale already.
1963 uprv_memset(codesetName
, 0, sizeof(codesetName
));
1964 name
= getCodepageFromPOSIXID(localeName
, codesetName
, sizeof(codesetName
));
1966 /* if we can find the codeset name from setlocale, return that. */
1970 if (*codesetName
== 0)
1972 /* Everything failed. Return US ASCII (ISO 646). */
1973 (void)uprv_strcpy(codesetName
, "US-ASCII");
1982 U_CAPI
const char* U_EXPORT2
1983 uprv_getDefaultCodepage()
1985 static char const *name
= NULL
;
1988 name
= int_getDefaultCodepage();
1993 #endif /* !U_CHARSET_IS_UTF8 */
1996 /* end of platform-specific implementation -------------- */
1998 /* version handling --------------------------------------------------------- */
2000 U_CAPI
void U_EXPORT2
2001 u_versionFromString(UVersionInfo versionArray
, const char *versionString
) {
2005 if(versionArray
==NULL
) {
2009 if(versionString
!=NULL
) {
2011 versionArray
[part
]=(uint8_t)uprv_strtoul(versionString
, &end
, 10);
2012 if(end
==versionString
|| ++part
==U_MAX_VERSION_LENGTH
|| *end
!=U_VERSION_DELIMITER
) {
2015 versionString
=end
+1;
2019 while(part
<U_MAX_VERSION_LENGTH
) {
2020 versionArray
[part
++]=0;
2024 U_CAPI
void U_EXPORT2
2025 u_versionFromUString(UVersionInfo versionArray
, const UChar
*versionString
) {
2026 if(versionArray
!=NULL
&& versionString
!=NULL
) {
2027 char versionChars
[U_MAX_VERSION_STRING_LENGTH
+1];
2028 int32_t len
= u_strlen(versionString
);
2029 if(len
>U_MAX_VERSION_STRING_LENGTH
) {
2030 len
= U_MAX_VERSION_STRING_LENGTH
;
2032 u_UCharsToChars(versionString
, versionChars
, len
);
2033 versionChars
[len
]=0;
2034 u_versionFromString(versionArray
, versionChars
);
2038 U_CAPI
void U_EXPORT2
2039 u_versionToString(const UVersionInfo versionArray
, char *versionString
) {
2040 uint16_t count
, part
;
2043 if(versionString
==NULL
) {
2047 if(versionArray
==NULL
) {
2052 /* count how many fields need to be written */
2053 for(count
=4; count
>0 && versionArray
[count
-1]==0; --count
) {
2060 /* write the first part */
2061 /* write the decimal field value */
2062 field
=versionArray
[0];
2064 *versionString
++=(char)('0'+field
/100);
2068 *versionString
++=(char)('0'+field
/10);
2071 *versionString
++=(char)('0'+field
);
2073 /* write the following parts */
2074 for(part
=1; part
<count
; ++part
) {
2075 /* write a dot first */
2076 *versionString
++=U_VERSION_DELIMITER
;
2078 /* write the decimal field value */
2079 field
=versionArray
[part
];
2081 *versionString
++=(char)('0'+field
/100);
2085 *versionString
++=(char)('0'+field
/10);
2088 *versionString
++=(char)('0'+field
);
2095 U_CAPI
void U_EXPORT2
2096 u_getVersion(UVersionInfo versionArray
) {
2097 (void)copyright
; // Suppress unused variable warning from clang.
2098 u_versionFromString(versionArray
, U_ICU_VERSION
);
2102 * icucfg.h dependent code
2107 #if HAVE_DLOPEN && !U_PLATFORM_USES_ONLY_WIN32_API
2119 U_INTERNAL
void * U_EXPORT2
2120 uprv_dl_open(const char *libName
, UErrorCode
*status
) {
2122 if(U_FAILURE(*status
)) return ret
;
2123 ret
= dlopen(libName
, RTLD_NOW
|RTLD_GLOBAL
);
2125 #ifdef U_TRACE_DYLOAD
2126 printf("dlerror on dlopen(%s): %s\n", libName
, dlerror());
2128 *status
= U_MISSING_RESOURCE_ERROR
;
2133 U_INTERNAL
void U_EXPORT2
2134 uprv_dl_close(void *lib
, UErrorCode
*status
) {
2135 if(U_FAILURE(*status
)) return;
2139 U_INTERNAL UVoidFunction
* U_EXPORT2
2140 uprv_dlsym_func(void *lib
, const char* sym
, UErrorCode
*status
) {
2146 if(U_FAILURE(*status
)) return uret
.fp
;
2147 uret
.vp
= dlsym(lib
, sym
);
2148 if(uret
.vp
== NULL
) {
2149 #ifdef U_TRACE_DYLOAD
2150 printf("dlerror on dlsym(%p,%s): %s\n", lib
,sym
, dlerror());
2152 *status
= U_MISSING_RESOURCE_ERROR
;
2159 /* null (nonexistent) implementation. */
2161 U_INTERNAL
void * U_EXPORT2
2162 uprv_dl_open(const char *libName
, UErrorCode
*status
) {
2163 if(U_FAILURE(*status
)) return NULL
;
2164 *status
= U_UNSUPPORTED_ERROR
;
2168 U_INTERNAL
void U_EXPORT2
2169 uprv_dl_close(void *lib
, UErrorCode
*status
) {
2170 if(U_FAILURE(*status
)) return;
2171 *status
= U_UNSUPPORTED_ERROR
;
2176 U_INTERNAL UVoidFunction
* U_EXPORT2
2177 uprv_dlsym_func(void *lib
, const char* sym
, UErrorCode
*status
) {
2178 if(U_SUCCESS(*status
)) {
2179 *status
= U_UNSUPPORTED_ERROR
;
2181 return (UVoidFunction
*)NULL
;
2188 #elif U_PLATFORM_USES_ONLY_WIN32_API
2190 U_INTERNAL
void * U_EXPORT2
2191 uprv_dl_open(const char *libName
, UErrorCode
*status
) {
2194 if(U_FAILURE(*status
)) return NULL
;
2196 lib
= LoadLibraryA(libName
);
2199 *status
= U_MISSING_RESOURCE_ERROR
;
2205 U_INTERNAL
void U_EXPORT2
2206 uprv_dl_close(void *lib
, UErrorCode
*status
) {
2207 HMODULE handle
= (HMODULE
)lib
;
2208 if(U_FAILURE(*status
)) return;
2210 FreeLibrary(handle
);
2216 U_INTERNAL UVoidFunction
* U_EXPORT2
2217 uprv_dlsym_func(void *lib
, const char* sym
, UErrorCode
*status
) {
2218 HMODULE handle
= (HMODULE
)lib
;
2219 UVoidFunction
* addr
= NULL
;
2221 if(U_FAILURE(*status
) || lib
==NULL
) return NULL
;
2223 addr
= (UVoidFunction
*)GetProcAddress(handle
, sym
);
2226 DWORD lastError
= GetLastError();
2227 if(lastError
== ERROR_PROC_NOT_FOUND
) {
2228 *status
= U_MISSING_RESOURCE_ERROR
;
2230 *status
= U_UNSUPPORTED_ERROR
; /* other unknown error. */
2240 /* No dynamic loading set. */
2242 U_INTERNAL
void * U_EXPORT2
2243 uprv_dl_open(const char *libName
, UErrorCode
*status
) {
2245 if(U_FAILURE(*status
)) return NULL
;
2246 *status
= U_UNSUPPORTED_ERROR
;
2250 U_INTERNAL
void U_EXPORT2
2251 uprv_dl_close(void *lib
, UErrorCode
*status
) {
2253 if(U_FAILURE(*status
)) return;
2254 *status
= U_UNSUPPORTED_ERROR
;
2259 U_INTERNAL UVoidFunction
* U_EXPORT2
2260 uprv_dlsym_func(void *lib
, const char* sym
, UErrorCode
*status
) {
2263 if(U_SUCCESS(*status
)) {
2264 *status
= U_UNSUPPORTED_ERROR
;
2266 return (UVoidFunction
*)NULL
;
2269 #endif /* U_ENABLE_DYLOAD */
2272 * Hey, Emacs, please set the following:
2275 * indent-tabs-mode: nil