#include "unicode/utypes.h"
#if !UCONFIG_NO_FORMATTING
#include "unicode/dtfmtsym.h"
#include "unicode/smpdtfmt.h"
#include "ucln_in.h"
#include "mutex.h"
#include "cmemory.h"
#include "cstring.h"
#include "locbased.h"
#include "gregoimp.h"
#define PATTERN_CHARS_LEN 24
static const UChar gPatternChars[] = {
0x47, 0x79, 0x4D, 0x64, 0x6B, 0x48, 0x6D, 0x73, 0x53, 0x45,
0x44, 0x46, 0x77, 0x57, 0x61, 0x68, 0x4B, 0x7A, 0x59, 0x65,
0x75, 0x67, 0x41, 0x5A, 0
};
static const UChar gLastResortMonthNames[13][3] =
{
{0x0030, 0x0031, 0x0000},
{0x0030, 0x0032, 0x0000},
{0x0030, 0x0033, 0x0000},
{0x0030, 0x0034, 0x0000},
{0x0030, 0x0035, 0x0000},
{0x0030, 0x0036, 0x0000},
{0x0030, 0x0037, 0x0000},
{0x0030, 0x0038, 0x0000},
{0x0030, 0x0039, 0x0000},
{0x0031, 0x0030, 0x0000},
{0x0031, 0x0031, 0x0000},
{0x0031, 0x0032, 0x0000},
{0x0031, 0x0033, 0x0000}
};
static const UChar gLastResortDayNames[8][2] =
{
{0x0000, 0x0000},
{0x0031, 0x0000},
{0x0032, 0x0000},
{0x0033, 0x0000},
{0x0034, 0x0000},
{0x0035, 0x0000},
{0x0036, 0x0000},
{0x0037, 0x0000}
};
static const UChar gLastResortAmPmMarkers[2][3] =
{
{0x0041, 0x004D, 0x0000},
{0x0050, 0x004D, 0x0000}
};
static const UChar gLastResortEras[2][3] =
{
{0x0042, 0x0043, 0x0000},
{0x0041, 0x0044, 0x0000}
};
static const UChar gLastResortZoneStrings[5][4] =
{
{0x0047, 0x004D, 0x0054, 0x0000},
{0x0047, 0x004D, 0x0054, 0x0000},
{0x0047, 0x004D, 0x0054, 0x0000},
{0x0047, 0x004D, 0x0054, 0x0000},
{0x0047, 0x004D, 0x0054, 0x0000}
};
U_NAMESPACE_BEGIN
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(DateFormatSymbols)
const char gErasTag[]="eras";
const char gAbbreviatedTag[] = "abbreviated";
const char gMonthNamesTag[]="monthNames";
const char gDayNamesTag[]="dayNames";
const char gNamesWideTag[]="wide";
const char gNamesAbbrTag[]="abbreviated";
const char gAmPmMarkersTag[]="AmPmMarkers";
const char gZoneStringsTag[]="zoneStrings";
const char gLocalPatternCharsTag[]="localPatternChars";
static inline UnicodeString* newUnicodeStringArray(size_t count) {
return new UnicodeString[count ? count : 1];
}
DateFormatSymbols::DateFormatSymbols(const Locale& locale,
UErrorCode& status)
: UObject()
{
initializeData(locale, NULL, status);
}
DateFormatSymbols::DateFormatSymbols(UErrorCode& status)
: UObject()
{
initializeData(Locale::getDefault(), NULL, status, TRUE);
}
DateFormatSymbols::DateFormatSymbols(const Locale& locale,
const char *type,
UErrorCode& status)
: UObject()
{
initializeData(locale, type, status);
}
DateFormatSymbols::DateFormatSymbols(const char *type, UErrorCode& status)
: UObject()
{
initializeData(Locale::getDefault(), type, status, TRUE);
}
DateFormatSymbols::DateFormatSymbols(const DateFormatSymbols& other)
: UObject(other)
{
copyData(other);
}
void
DateFormatSymbols::assignArray(UnicodeString*& dstArray,
int32_t& dstCount,
const UnicodeString* srcArray,
int32_t srcCount)
{
dstCount = srcCount;
dstArray = newUnicodeStringArray(srcCount);
if(dstArray != NULL) {
int32_t i;
for(i=0; i<srcCount; ++i) {
dstArray[i].fastCopyFrom(srcArray[i]);
}
}
}
void
DateFormatSymbols::createZoneStrings(const UnicodeString *const * otherStrings)
{
int32_t row, col;
fZoneStrings = (UnicodeString **)uprv_malloc(fZoneStringsRowCount * sizeof(UnicodeString *));
for (row=0; row<fZoneStringsRowCount; ++row)
{
fZoneStrings[row] = newUnicodeStringArray(fZoneStringsColCount);
for (col=0; col<fZoneStringsColCount; ++col) {
fZoneStrings[row][col].fastCopyFrom(otherStrings[row][col]);
}
}
}
void
DateFormatSymbols::copyData(const DateFormatSymbols& other) {
assignArray(fEras, fErasCount, other.fEras, other.fErasCount);
assignArray(fMonths, fMonthsCount, other.fMonths, other.fMonthsCount);
assignArray(fShortMonths, fShortMonthsCount, other.fShortMonths, other.fShortMonthsCount);
assignArray(fWeekdays, fWeekdaysCount, other.fWeekdays, other.fWeekdaysCount);
assignArray(fShortWeekdays, fShortWeekdaysCount, other.fShortWeekdays, other.fShortWeekdaysCount);
assignArray(fAmPms, fAmPmsCount, other.fAmPms, other.fAmPmsCount);
fZoneStringsRowCount = other.fZoneStringsRowCount;
fZoneStringsColCount = other.fZoneStringsColCount;
createZoneStrings((const UnicodeString**)other.fZoneStrings);
fLocalPatternChars.fastCopyFrom(other.fLocalPatternChars);
}
DateFormatSymbols& DateFormatSymbols::operator=(const DateFormatSymbols& other)
{
dispose();
copyData(other);
return *this;
}
DateFormatSymbols::~DateFormatSymbols()
{
dispose();
}
void DateFormatSymbols::dispose()
{
if (fEras) delete[] fEras;
if (fMonths) delete[] fMonths;
if (fShortMonths) delete[] fShortMonths;
if (fWeekdays) delete[] fWeekdays;
if (fShortWeekdays) delete[] fShortWeekdays;
if (fAmPms) delete[] fAmPms;
disposeZoneStrings();
}
void DateFormatSymbols::disposeZoneStrings()
{
if (fZoneStrings) {
for (int32_t row=0; row<fZoneStringsRowCount; ++row)
delete[] fZoneStrings[row];
uprv_free(fZoneStrings);
}
}
UBool
DateFormatSymbols::arrayCompare(const UnicodeString* array1,
const UnicodeString* array2,
int32_t count)
{
if (array1 == array2) return TRUE;
while (count>0)
{
--count;
if (array1[count] != array2[count]) return FALSE;
}
return TRUE;
}
UBool
DateFormatSymbols::operator==(const DateFormatSymbols& other) const
{
if (this == &other) {
return TRUE;
}
if (fErasCount == other.fErasCount &&
fMonthsCount == other.fMonthsCount &&
fShortMonthsCount == other.fShortMonthsCount &&
fWeekdaysCount == other.fWeekdaysCount &&
fShortWeekdaysCount == other.fShortWeekdaysCount &&
fAmPmsCount == other.fAmPmsCount &&
fZoneStringsRowCount == other.fZoneStringsRowCount &&
fZoneStringsColCount == other.fZoneStringsColCount)
{
if (arrayCompare(fEras, other.fEras, fErasCount) &&
arrayCompare(fMonths, other.fMonths, fMonthsCount) &&
arrayCompare(fShortMonths, other.fShortMonths, fShortMonthsCount) &&
arrayCompare(fWeekdays, other.fWeekdays, fWeekdaysCount) &&
arrayCompare(fShortWeekdays, other.fShortWeekdays, fShortWeekdaysCount) &&
arrayCompare(fAmPms, other.fAmPms, fAmPmsCount))
{
if (fZoneStrings == other.fZoneStrings) return TRUE;
for (int32_t row=0; row<fZoneStringsRowCount; ++row)
{
if (!arrayCompare(fZoneStrings[row], other.fZoneStrings[row], fZoneStringsColCount))
return FALSE;
}
return TRUE;
}
}
return FALSE;
}
const UnicodeString*
DateFormatSymbols::getEras(int32_t &count) const
{
count = fErasCount;
return fEras;
}
const UnicodeString*
DateFormatSymbols::getMonths(int32_t &count) const
{
count = fMonthsCount;
return fMonths;
}
const UnicodeString*
DateFormatSymbols::getShortMonths(int32_t &count) const
{
count = fShortMonthsCount;
return fShortMonths;
}
const UnicodeString*
DateFormatSymbols::getWeekdays(int32_t &count) const
{
count = fWeekdaysCount;
return fWeekdays;
}
const UnicodeString*
DateFormatSymbols::getShortWeekdays(int32_t &count) const
{
count = fShortWeekdaysCount;
return fShortWeekdays;
}
const UnicodeString*
DateFormatSymbols::getAmPmStrings(int32_t &count) const
{
count = fAmPmsCount;
return fAmPms;
}
void
DateFormatSymbols::setEras(const UnicodeString* erasArray, int32_t count)
{
if (fEras) delete[] fEras;
fEras = newUnicodeStringArray(count);
uprv_arrayCopy(erasArray,fEras, count);
fErasCount = count;
}
void
DateFormatSymbols::setMonths(const UnicodeString* monthsArray, int32_t count)
{
if (fMonths) delete[] fMonths;
fMonths = newUnicodeStringArray(count);
uprv_arrayCopy( monthsArray,fMonths,count);
fMonthsCount = count;
}
void
DateFormatSymbols::setShortMonths(const UnicodeString* shortMonthsArray, int32_t count)
{
if (fShortMonths) delete[] fShortMonths;
fShortMonths = newUnicodeStringArray(count);
uprv_arrayCopy(shortMonthsArray,fShortMonths, count);
fShortMonthsCount = count;
}
void DateFormatSymbols::setWeekdays(const UnicodeString* weekdaysArray, int32_t count)
{
if (fWeekdays) delete[] fWeekdays;
fWeekdays = newUnicodeStringArray(count);
uprv_arrayCopy(weekdaysArray,fWeekdays,count);
fWeekdaysCount = count;
}
void
DateFormatSymbols::setShortWeekdays(const UnicodeString* shortWeekdaysArray, int32_t count)
{
if (fShortWeekdays) delete[] fShortWeekdays;
fShortWeekdays = newUnicodeStringArray(count);
uprv_arrayCopy( shortWeekdaysArray,fShortWeekdays,count);
fShortWeekdaysCount = count;
}
void
DateFormatSymbols::setAmPmStrings(const UnicodeString* amPmsArray, int32_t count)
{
if (fAmPms) delete[] fAmPms;
fAmPms = newUnicodeStringArray(count);
uprv_arrayCopy(amPmsArray,fAmPms,count);
fAmPmsCount = count;
}
const UnicodeString**
DateFormatSymbols::getZoneStrings(int32_t& rowCount, int32_t& columnCount) const
{
rowCount = fZoneStringsRowCount;
columnCount = fZoneStringsColCount;
return (const UnicodeString**)fZoneStrings; }
void
DateFormatSymbols::setZoneStrings(const UnicodeString* const *strings, int32_t rowCount, int32_t columnCount)
{
disposeZoneStrings();
fZoneStringsRowCount = rowCount;
fZoneStringsColCount = columnCount;
createZoneStrings((const UnicodeString**)strings);
}
const UChar * U_EXPORT2
DateFormatSymbols::getPatternUChars(void)
{
return gPatternChars;
}
UnicodeString&
DateFormatSymbols::getLocalPatternChars(UnicodeString& result) const
{
return result.fastCopyFrom(fLocalPatternChars);
}
void
DateFormatSymbols::setLocalPatternChars(const UnicodeString& newLocalPatternChars)
{
fLocalPatternChars = newLocalPatternChars;
}
void
DateFormatSymbols::initField(UnicodeString **field, int32_t& length, const UResourceBundle *data, UErrorCode &status) {
if (U_SUCCESS(status)) {
int32_t strLen = 0;
length = ures_getSize(data);
*field = newUnicodeStringArray(length);
if (*field) {
for(int32_t i = 0; i<length; i++) {
const UChar *resStr = ures_getStringByIndex(data, i, &strLen, &status);
(*(field)+i)->setTo(TRUE, resStr, strLen);
}
}
else {
length = 0;
status = U_MEMORY_ALLOCATION_ERROR;
}
}
}
void
DateFormatSymbols::initField(UnicodeString **field, int32_t& length, const UChar *data, LastResortSize numStr, LastResortSize strLen, UErrorCode &status) {
if (U_SUCCESS(status)) {
length = numStr;
*field = newUnicodeStringArray((size_t)numStr);
if (*field) {
for(int32_t i = 0; i<length; i++) {
(*(field)+i)->setTo(TRUE, data+(i*((int32_t)strLen)), -1);
}
}
else {
length = 0;
status = U_MEMORY_ALLOCATION_ERROR;
}
}
}
void
DateFormatSymbols::initializeData(const Locale& locale, const char *type, UErrorCode& status, UBool useLastResortData)
{
int32_t i;
int32_t len = 0;
const UChar *resStr;
fEras = NULL;
fErasCount = 0;
fMonths = NULL;
fMonthsCount=0;
fShortMonths = NULL;
fShortMonthsCount=0;
fWeekdays = NULL;
fWeekdaysCount=0;
fShortWeekdays = NULL;
fShortWeekdaysCount=0;
fAmPms = NULL;
fAmPmsCount=0;
fZoneStringsRowCount = 0;
fZoneStringsColCount = 0;
fZoneStrings = NULL;
if (U_FAILURE(status)) return;
CalendarData calData(locale, type, status);
UResourceBundle *nonCalendarData = ures_open((char*)0, locale.getName(), &status);
UResourceBundle *erasMain = calData.getByKey(gErasTag, status);
UResourceBundle *eras = ures_getByKeyWithFallback(erasMain, gAbbreviatedTag, NULL, &status);
UResourceBundle *lsweekdaysData = NULL; UResourceBundle *weekdaysData = NULL; UResourceBundle *zoneArray = ures_getByKey(nonCalendarData, gZoneStringsTag, NULL, &status);
UResourceBundle *zoneRow = ures_getByIndex(zoneArray, (int32_t)0, NULL, &status);
U_LOCALE_BASED(locBased, *this);
if (U_FAILURE(status))
{
if (useLastResortData)
{
status = U_USING_FALLBACK_WARNING;
initField(&fEras, fErasCount, (const UChar *)gLastResortEras, kEraNum, kEraLen, status);
initField(&fMonths, fMonthsCount, (const UChar *)gLastResortMonthNames, kMonthNum, kMonthLen, status);
initField(&fShortMonths, fShortMonthsCount, (const UChar *)gLastResortMonthNames, kMonthNum, kMonthLen, status);
initField(&fWeekdays, fWeekdaysCount, (const UChar *)gLastResortDayNames, kDayNum, kDayLen, status);
initField(&fShortWeekdays, fShortWeekdaysCount, (const UChar *)gLastResortDayNames, kDayNum, kDayLen, status);
initField(&fAmPms, fAmPmsCount, (const UChar *)gLastResortAmPmMarkers, kAmPmNum, kAmPmLen, status);
fZoneStrings = (UnicodeString **)uprv_malloc(sizeof(UnicodeString *));
if (fZoneStrings == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
}
fZoneStringsRowCount = 1;
initField(fZoneStrings, fZoneStringsColCount, (const UChar *)gLastResortZoneStrings, kZoneNum, kZoneLen, status);
fLocalPatternChars = gPatternChars;
}
goto cleanup;
}
locBased.setLocaleIDs(ures_getLocaleByType(eras, ULOC_VALID_LOCALE, &status),
ures_getLocaleByType(eras, ULOC_ACTUAL_LOCALE, &status));
initField(&fEras, fErasCount, eras, status);
initField(&fMonths, fMonthsCount, calData.getByKey2(gMonthNamesTag, gNamesWideTag, status), status);
initField(&fShortMonths, fShortMonthsCount, calData.getByKey2(gMonthNamesTag, gNamesAbbrTag, status), status);
initField(&fAmPms, fAmPmsCount, calData.getByKey(gAmPmMarkersTag, status), status);
resStr = ures_getStringByKey(nonCalendarData, gLocalPatternCharsTag, &len, &status);
fLocalPatternChars.setTo(TRUE, resStr, len);
if (len < PATTERN_CHARS_LEN) {
fLocalPatternChars.append(UnicodeString(TRUE, &gPatternChars[len], PATTERN_CHARS_LEN-len));
}
fZoneStringsRowCount = ures_getSize(zoneArray);
fZoneStringsColCount = ures_getSize(zoneRow);
fZoneStrings = (UnicodeString **)uprv_malloc(fZoneStringsRowCount * sizeof(UnicodeString *));
if (fZoneStrings == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
goto cleanup;
}
for(i = 0; i<fZoneStringsRowCount; i++) {
*(fZoneStrings+i) = newUnicodeStringArray(fZoneStringsColCount);
if ((*(fZoneStrings+i)) == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
goto cleanup;
}
zoneRow = ures_getByIndex(zoneArray, i, zoneRow, &status);
for(int32_t j = 0; j<fZoneStringsColCount; j++) {
resStr = ures_getStringByIndex(zoneRow, j, &len, &status);
fZoneStrings[i][j].setTo(TRUE, resStr, len);
}
}
weekdaysData = calData.getByKey2(gDayNamesTag, gNamesWideTag, status);
fWeekdaysCount = ures_getSize(weekdaysData);
fWeekdays = new UnicodeString[fWeekdaysCount+1];
if (fWeekdays == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
goto cleanup;
}
for(i = 0; i<fWeekdaysCount; i++) {
resStr = ures_getStringByIndex(weekdaysData, i, &len, &status);
fWeekdays[i+1].setTo(TRUE, resStr, len);
}
fWeekdaysCount++;
lsweekdaysData = calData.getByKey2(gDayNamesTag, gNamesAbbrTag, status);
fShortWeekdaysCount = ures_getSize(lsweekdaysData);
fShortWeekdays = new UnicodeString[fShortWeekdaysCount+1];
if (fShortWeekdays == 0) {
status = U_MEMORY_ALLOCATION_ERROR;
goto cleanup;
}
for(i = 0; i<fShortWeekdaysCount; i++) {
resStr = ures_getStringByIndex(lsweekdaysData, i, &len, &status);
fShortWeekdays[i+1].setTo(TRUE, resStr, len);
}
fShortWeekdaysCount++;
cleanup:
ures_close(eras);
ures_close(zoneRow);
ures_close(zoneArray);
ures_close(nonCalendarData);
}
int32_t DateFormatSymbols::getZoneIndex(const UnicodeString& ID) const
{
int32_t result = _getZoneIndex(ID);
if (result >= 0) {
return result;
}
int32_t n = TimeZone::countEquivalentIDs(ID);
if (n > 1) {
int32_t i;
for (i=0; i<n; ++i) {
UnicodeString equivID = TimeZone::getEquivalentID(ID, i);
if (equivID != ID) {
int32_t equivResult = _getZoneIndex(equivID);
if (equivResult >= 0) {
return equivResult;
}
}
}
}
return -1;
}
int32_t DateFormatSymbols::_getZoneIndex(const UnicodeString& ID) const
{
for(int32_t index = 0; index < fZoneStringsRowCount; index++) {
if (0 == ID.caseCompare(fZoneStrings[index][0], 0)) {
return index;
}
}
return -1;
}
Locale
DateFormatSymbols::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
U_LOCALE_BASED(locBased, *this);
return locBased.getLocale(type, status);
}
U_NAMESPACE_END
#endif