michael@0: /* michael@0: ******************************************************************************* michael@0: * michael@0: * Copyright (C) 2009-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ******************************************************************************* michael@0: * file name: udatpg.cpp michael@0: * encoding: US-ASCII michael@0: * tab size: 8 (not used) michael@0: * indentation:4 michael@0: * michael@0: * created on: 2007jul30 michael@0: * created by: Markus W. Scherer michael@0: */ michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/udatpg.h" michael@0: #include "unicode/uenum.h" michael@0: #include "unicode/strenum.h" michael@0: #include "unicode/dtptngen.h" michael@0: #include "ustrenum.h" michael@0: michael@0: U_NAMESPACE_USE michael@0: michael@0: U_CAPI UDateTimePatternGenerator * U_EXPORT2 michael@0: udatpg_open(const char *locale, UErrorCode *pErrorCode) { michael@0: if(locale==NULL) { michael@0: return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(*pErrorCode); michael@0: } else { michael@0: return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(Locale(locale), *pErrorCode); michael@0: } michael@0: } michael@0: michael@0: U_CAPI UDateTimePatternGenerator * U_EXPORT2 michael@0: udatpg_openEmpty(UErrorCode *pErrorCode) { michael@0: return (UDateTimePatternGenerator *)DateTimePatternGenerator::createEmptyInstance(*pErrorCode); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udatpg_close(UDateTimePatternGenerator *dtpg) { michael@0: delete (DateTimePatternGenerator *)dtpg; michael@0: } michael@0: michael@0: U_CAPI UDateTimePatternGenerator * U_EXPORT2 michael@0: udatpg_clone(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return NULL; michael@0: } michael@0: return (UDateTimePatternGenerator *)(((const DateTimePatternGenerator *)dtpg)->clone()); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udatpg_getBestPattern(UDateTimePatternGenerator *dtpg, michael@0: const UChar *skeleton, int32_t length, michael@0: UChar *bestPattern, int32_t capacity, michael@0: UErrorCode *pErrorCode) { michael@0: return udatpg_getBestPatternWithOptions(dtpg, skeleton, length, michael@0: UDATPG_MATCH_NO_OPTIONS, michael@0: bestPattern, capacity, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udatpg_getBestPatternWithOptions(UDateTimePatternGenerator *dtpg, michael@0: const UChar *skeleton, int32_t length, michael@0: UDateTimePatternMatchOptions options, michael@0: UChar *bestPattern, int32_t capacity, michael@0: UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(skeleton==NULL && length!=0) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: UnicodeString skeletonString((UBool)(length<0), skeleton, length); michael@0: UnicodeString result=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeletonString, options, *pErrorCode); michael@0: return result.extract(bestPattern, capacity, *pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udatpg_getSkeleton(UDateTimePatternGenerator *dtpg, michael@0: const UChar *pattern, int32_t length, michael@0: UChar *skeleton, int32_t capacity, michael@0: UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(pattern==NULL && length!=0) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: UnicodeString patternString((UBool)(length<0), pattern, length); michael@0: UnicodeString result=((DateTimePatternGenerator *)dtpg)->getSkeleton(patternString, *pErrorCode); michael@0: return result.extract(skeleton, capacity, *pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udatpg_getBaseSkeleton(UDateTimePatternGenerator *dtpg, michael@0: const UChar *pattern, int32_t length, michael@0: UChar *skeleton, int32_t capacity, michael@0: UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if(pattern==NULL && length!=0) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: UnicodeString patternString((UBool)(length<0), pattern, length); michael@0: UnicodeString result=((DateTimePatternGenerator *)dtpg)->getBaseSkeleton(patternString, *pErrorCode); michael@0: return result.extract(skeleton, capacity, *pErrorCode); michael@0: } michael@0: michael@0: U_CAPI UDateTimePatternConflict U_EXPORT2 michael@0: udatpg_addPattern(UDateTimePatternGenerator *dtpg, michael@0: const UChar *pattern, int32_t patternLength, michael@0: UBool override, michael@0: UChar *conflictingPattern, int32_t capacity, int32_t *pLength, michael@0: UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return UDATPG_NO_CONFLICT; michael@0: } michael@0: if(pattern==NULL && patternLength!=0) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return UDATPG_NO_CONFLICT; michael@0: } michael@0: UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength); michael@0: UnicodeString conflictingPatternString; michael@0: UDateTimePatternConflict result=((DateTimePatternGenerator *)dtpg)-> michael@0: addPattern(patternString, override, conflictingPatternString, *pErrorCode); michael@0: int32_t length=conflictingPatternString.extract(conflictingPattern, capacity, *pErrorCode); michael@0: if(pLength!=NULL) { michael@0: *pLength=length; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udatpg_setAppendItemFormat(UDateTimePatternGenerator *dtpg, michael@0: UDateTimePatternField field, michael@0: const UChar *value, int32_t length) { michael@0: UnicodeString valueString((UBool)(length<0), value, length); michael@0: ((DateTimePatternGenerator *)dtpg)->setAppendItemFormat(field, valueString); michael@0: } michael@0: michael@0: U_CAPI const UChar * U_EXPORT2 michael@0: udatpg_getAppendItemFormat(const UDateTimePatternGenerator *dtpg, michael@0: UDateTimePatternField field, michael@0: int32_t *pLength) { michael@0: const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemFormat(field); michael@0: if(pLength!=NULL) { michael@0: *pLength=result.length(); michael@0: } michael@0: return result.getBuffer(); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udatpg_setAppendItemName(UDateTimePatternGenerator *dtpg, michael@0: UDateTimePatternField field, michael@0: const UChar *value, int32_t length) { michael@0: UnicodeString valueString((UBool)(length<0), value, length); michael@0: ((DateTimePatternGenerator *)dtpg)->setAppendItemName(field, valueString); michael@0: } michael@0: michael@0: U_CAPI const UChar * U_EXPORT2 michael@0: udatpg_getAppendItemName(const UDateTimePatternGenerator *dtpg, michael@0: UDateTimePatternField field, michael@0: int32_t *pLength) { michael@0: const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemName(field); michael@0: if(pLength!=NULL) { michael@0: *pLength=result.length(); michael@0: } michael@0: return result.getBuffer(); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg, michael@0: const UChar *dtFormat, int32_t length) { michael@0: UnicodeString dtFormatString((UBool)(length<0), dtFormat, length); michael@0: ((DateTimePatternGenerator *)dtpg)->setDateTimeFormat(dtFormatString); michael@0: } michael@0: michael@0: U_CAPI const UChar * U_EXPORT2 michael@0: udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg, michael@0: int32_t *pLength) { michael@0: const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDateTimeFormat(); michael@0: if(pLength!=NULL) { michael@0: *pLength=result.length(); michael@0: } michael@0: return result.getBuffer(); michael@0: } michael@0: michael@0: U_CAPI void U_EXPORT2 michael@0: udatpg_setDecimal(UDateTimePatternGenerator *dtpg, michael@0: const UChar *decimal, int32_t length) { michael@0: UnicodeString decimalString((UBool)(length<0), decimal, length); michael@0: ((DateTimePatternGenerator *)dtpg)->setDecimal(decimalString); michael@0: } michael@0: michael@0: U_CAPI const UChar * U_EXPORT2 michael@0: udatpg_getDecimal(const UDateTimePatternGenerator *dtpg, michael@0: int32_t *pLength) { michael@0: const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDecimal(); michael@0: if(pLength!=NULL) { michael@0: *pLength=result.length(); michael@0: } michael@0: return result.getBuffer(); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg, michael@0: const UChar *pattern, int32_t patternLength, michael@0: const UChar *skeleton, int32_t skeletonLength, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) { michael@0: return udatpg_replaceFieldTypesWithOptions(dtpg, pattern, patternLength, skeleton, skeletonLength, michael@0: UDATPG_MATCH_NO_OPTIONS, michael@0: dest, destCapacity, pErrorCode); michael@0: } michael@0: michael@0: U_CAPI int32_t U_EXPORT2 michael@0: udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator *dtpg, michael@0: const UChar *pattern, int32_t patternLength, michael@0: const UChar *skeleton, int32_t skeletonLength, michael@0: UDateTimePatternMatchOptions options, michael@0: UChar *dest, int32_t destCapacity, michael@0: UErrorCode *pErrorCode) { michael@0: if(U_FAILURE(*pErrorCode)) { michael@0: return 0; michael@0: } michael@0: if((pattern==NULL && patternLength!=0) || (skeleton==NULL && skeletonLength!=0)) { michael@0: *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; michael@0: return 0; michael@0: } michael@0: UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength); michael@0: UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength); michael@0: UnicodeString result=((DateTimePatternGenerator *)dtpg)->replaceFieldTypes(patternString, skeletonString, options, *pErrorCode); michael@0: return result.extract(dest, destCapacity, *pErrorCode); michael@0: } michael@0: michael@0: U_CAPI UEnumeration * U_EXPORT2 michael@0: udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { michael@0: return uenum_openFromStringEnumeration( michael@0: ((DateTimePatternGenerator *)dtpg)->getSkeletons(*pErrorCode), michael@0: pErrorCode); michael@0: } michael@0: michael@0: U_CAPI UEnumeration * U_EXPORT2 michael@0: udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { michael@0: return uenum_openFromStringEnumeration( michael@0: ((DateTimePatternGenerator *)dtpg)->getBaseSkeletons(*pErrorCode), michael@0: pErrorCode); michael@0: } michael@0: michael@0: U_CAPI const UChar * U_EXPORT2 michael@0: udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg, michael@0: const UChar *skeleton, int32_t skeletonLength, michael@0: int32_t *pLength) { michael@0: UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength); michael@0: const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getPatternForSkeleton(skeletonString); michael@0: if(pLength!=NULL) { michael@0: *pLength=result.length(); michael@0: } michael@0: return result.getBuffer(); michael@0: } michael@0: michael@0: #endif