1 // Copyright (C) 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************* 5 * 6 * Copyright (C) 2009-2015, International Business Machines 7 * Corporation and others. All Rights Reserved. 8 * 9 ******************************************************************************* 10 * file name: udatpg.cpp 11 * encoding: US-ASCII 12 * tab size: 8 (not used) 13 * indentation:4 14 * 15 * created on: 2007jul30 16 * created by: Markus W. Scherer 17 */ 18 19 #include "unicode/utypes.h" 20 21 #if !UCONFIG_NO_FORMATTING 22 23 #include "unicode/udatpg.h" 24 #include "unicode/uenum.h" 25 #include "unicode/strenum.h" 26 #include "unicode/dtptngen.h" 27 #include "ustrenum.h" 28 29 U_NAMESPACE_USE 30 31 U_CAPI UDateTimePatternGenerator * U_EXPORT2 32 udatpg_open(const char *locale, UErrorCode *pErrorCode) { 33 if(locale==NULL) { 34 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(*pErrorCode); 35 } else { 36 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(Locale(locale), *pErrorCode); 37 } 38 } 39 40 U_CAPI UDateTimePatternGenerator * U_EXPORT2 41 udatpg_openEmpty(UErrorCode *pErrorCode) { 42 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createEmptyInstance(*pErrorCode); 43 } 44 45 U_CAPI void U_EXPORT2 46 udatpg_close(UDateTimePatternGenerator *dtpg) { 47 delete (DateTimePatternGenerator *)dtpg; 48 } 49 50 U_CAPI UDateTimePatternGenerator * U_EXPORT2 51 udatpg_clone(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { 52 if(U_FAILURE(*pErrorCode)) { 53 return NULL; 54 } 55 return (UDateTimePatternGenerator *)(((const DateTimePatternGenerator *)dtpg)->clone()); 56 } 57 58 U_CAPI int32_t U_EXPORT2 59 udatpg_getBestPattern(UDateTimePatternGenerator *dtpg, 60 const UChar *skeleton, int32_t length, 61 UChar *bestPattern, int32_t capacity, 62 UErrorCode *pErrorCode) { 63 return udatpg_getBestPatternWithOptions(dtpg, skeleton, length, 64 UDATPG_MATCH_NO_OPTIONS, 65 bestPattern, capacity, pErrorCode); 66 } 67 68 U_CAPI int32_t U_EXPORT2 69 udatpg_getBestPatternWithOptions(UDateTimePatternGenerator *dtpg, 70 const UChar *skeleton, int32_t length, 71 UDateTimePatternMatchOptions options, 72 UChar *bestPattern, int32_t capacity, 73 UErrorCode *pErrorCode) { 74 if(U_FAILURE(*pErrorCode)) { 75 return 0; 76 } 77 if(skeleton==NULL && length!=0) { 78 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 79 return 0; 80 } 81 UnicodeString skeletonString((UBool)(length<0), skeleton, length); 82 UnicodeString result=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeletonString, options, *pErrorCode); 83 return result.extract(bestPattern, capacity, *pErrorCode); 84 } 85 86 U_CAPI int32_t U_EXPORT2 87 udatpg_getSkeleton(UDateTimePatternGenerator * /* dtpg */, 88 const UChar *pattern, int32_t length, 89 UChar *skeleton, int32_t capacity, 90 UErrorCode *pErrorCode) { 91 if(U_FAILURE(*pErrorCode)) { 92 return 0; 93 } 94 if(pattern==NULL && length!=0) { 95 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 96 return 0; 97 } 98 UnicodeString patternString((UBool)(length<0), pattern, length); 99 UnicodeString result=DateTimePatternGenerator::staticGetSkeleton( 100 patternString, *pErrorCode); 101 return result.extract(skeleton, capacity, *pErrorCode); 102 } 103 104 U_CAPI int32_t U_EXPORT2 105 udatpg_getBaseSkeleton(UDateTimePatternGenerator * /* dtpg */, 106 const UChar *pattern, int32_t length, 107 UChar *skeleton, int32_t capacity, 108 UErrorCode *pErrorCode) { 109 if(U_FAILURE(*pErrorCode)) { 110 return 0; 111 } 112 if(pattern==NULL && length!=0) { 113 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 114 return 0; 115 } 116 UnicodeString patternString((UBool)(length<0), pattern, length); 117 UnicodeString result=DateTimePatternGenerator::staticGetBaseSkeleton( 118 patternString, *pErrorCode); 119 return result.extract(skeleton, capacity, *pErrorCode); 120 } 121 122 U_CAPI UDateTimePatternConflict U_EXPORT2 123 udatpg_addPattern(UDateTimePatternGenerator *dtpg, 124 const UChar *pattern, int32_t patternLength, 125 UBool override, 126 UChar *conflictingPattern, int32_t capacity, int32_t *pLength, 127 UErrorCode *pErrorCode) { 128 if(U_FAILURE(*pErrorCode)) { 129 return UDATPG_NO_CONFLICT; 130 } 131 if(pattern==NULL && patternLength!=0) { 132 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 133 return UDATPG_NO_CONFLICT; 134 } 135 UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength); 136 UnicodeString conflictingPatternString; 137 UDateTimePatternConflict result=((DateTimePatternGenerator *)dtpg)-> 138 addPattern(patternString, override, conflictingPatternString, *pErrorCode); 139 int32_t length=conflictingPatternString.extract(conflictingPattern, capacity, *pErrorCode); 140 if(pLength!=NULL) { 141 *pLength=length; 142 } 143 return result; 144 } 145 146 U_CAPI void U_EXPORT2 147 udatpg_setAppendItemFormat(UDateTimePatternGenerator *dtpg, 148 UDateTimePatternField field, 149 const UChar *value, int32_t length) { 150 UnicodeString valueString((UBool)(length<0), value, length); 151 ((DateTimePatternGenerator *)dtpg)->setAppendItemFormat(field, valueString); 152 } 153 154 U_CAPI const UChar * U_EXPORT2 155 udatpg_getAppendItemFormat(const UDateTimePatternGenerator *dtpg, 156 UDateTimePatternField field, 157 int32_t *pLength) { 158 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemFormat(field); 159 if(pLength!=NULL) { 160 *pLength=result.length(); 161 } 162 return result.getBuffer(); 163 } 164 165 U_CAPI void U_EXPORT2 166 udatpg_setAppendItemName(UDateTimePatternGenerator *dtpg, 167 UDateTimePatternField field, 168 const UChar *value, int32_t length) { 169 UnicodeString valueString((UBool)(length<0), value, length); 170 ((DateTimePatternGenerator *)dtpg)->setAppendItemName(field, valueString); 171 } 172 173 U_CAPI const UChar * U_EXPORT2 174 udatpg_getAppendItemName(const UDateTimePatternGenerator *dtpg, 175 UDateTimePatternField field, 176 int32_t *pLength) { 177 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemName(field); 178 if(pLength!=NULL) { 179 *pLength=result.length(); 180 } 181 return result.getBuffer(); 182 } 183 184 U_CAPI void U_EXPORT2 185 udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg, 186 const UChar *dtFormat, int32_t length) { 187 UnicodeString dtFormatString((UBool)(length<0), dtFormat, length); 188 ((DateTimePatternGenerator *)dtpg)->setDateTimeFormat(dtFormatString); 189 } 190 191 U_CAPI const UChar * U_EXPORT2 192 udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg, 193 int32_t *pLength) { 194 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDateTimeFormat(); 195 if(pLength!=NULL) { 196 *pLength=result.length(); 197 } 198 return result.getBuffer(); 199 } 200 201 U_CAPI void U_EXPORT2 202 udatpg_setDecimal(UDateTimePatternGenerator *dtpg, 203 const UChar *decimal, int32_t length) { 204 UnicodeString decimalString((UBool)(length<0), decimal, length); 205 ((DateTimePatternGenerator *)dtpg)->setDecimal(decimalString); 206 } 207 208 U_CAPI const UChar * U_EXPORT2 209 udatpg_getDecimal(const UDateTimePatternGenerator *dtpg, 210 int32_t *pLength) { 211 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDecimal(); 212 if(pLength!=NULL) { 213 *pLength=result.length(); 214 } 215 return result.getBuffer(); 216 } 217 218 U_CAPI int32_t U_EXPORT2 219 udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg, 220 const UChar *pattern, int32_t patternLength, 221 const UChar *skeleton, int32_t skeletonLength, 222 UChar *dest, int32_t destCapacity, 223 UErrorCode *pErrorCode) { 224 return udatpg_replaceFieldTypesWithOptions(dtpg, pattern, patternLength, skeleton, skeletonLength, 225 UDATPG_MATCH_NO_OPTIONS, 226 dest, destCapacity, pErrorCode); 227 } 228 229 U_CAPI int32_t U_EXPORT2 230 udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator *dtpg, 231 const UChar *pattern, int32_t patternLength, 232 const UChar *skeleton, int32_t skeletonLength, 233 UDateTimePatternMatchOptions options, 234 UChar *dest, int32_t destCapacity, 235 UErrorCode *pErrorCode) { 236 if(U_FAILURE(*pErrorCode)) { 237 return 0; 238 } 239 if((pattern==NULL && patternLength!=0) || (skeleton==NULL && skeletonLength!=0)) { 240 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 241 return 0; 242 } 243 UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength); 244 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength); 245 UnicodeString result=((DateTimePatternGenerator *)dtpg)->replaceFieldTypes(patternString, skeletonString, options, *pErrorCode); 246 return result.extract(dest, destCapacity, *pErrorCode); 247 } 248 249 U_CAPI UEnumeration * U_EXPORT2 250 udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { 251 return uenum_openFromStringEnumeration( 252 ((DateTimePatternGenerator *)dtpg)->getSkeletons(*pErrorCode), 253 pErrorCode); 254 } 255 256 U_CAPI UEnumeration * U_EXPORT2 257 udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { 258 return uenum_openFromStringEnumeration( 259 ((DateTimePatternGenerator *)dtpg)->getBaseSkeletons(*pErrorCode), 260 pErrorCode); 261 } 262 263 U_CAPI const UChar * U_EXPORT2 264 udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg, 265 const UChar *skeleton, int32_t skeletonLength, 266 int32_t *pLength) { 267 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength); 268 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getPatternForSkeleton(skeletonString); 269 if(pLength!=NULL) { 270 *pLength=result.length(); 271 } 272 return result.getBuffer(); 273 } 274 275 #endif 276