udatpg.cpp (13236B)
1 // © 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: UTF-8 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==nullptr) { 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 nullptr; 54 } 55 return (UDateTimePatternGenerator *)(((const DateTimePatternGenerator *)dtpg)->clone()); 56 } 57 58 U_CAPI int32_t U_EXPORT2 59 udatpg_getBestPattern(UDateTimePatternGenerator *dtpg, 60 const char16_t *skeleton, int32_t length, 61 char16_t *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 char16_t *skeleton, int32_t length, 71 UDateTimePatternMatchOptions options, 72 char16_t *bestPattern, int32_t capacity, 73 UErrorCode *pErrorCode) { 74 if(U_FAILURE(*pErrorCode)) { 75 return 0; 76 } 77 if(skeleton==nullptr && length!=0) { 78 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 79 return 0; 80 } 81 UnicodeString skeletonString(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 char16_t *pattern, int32_t length, 89 char16_t *skeleton, int32_t capacity, 90 UErrorCode *pErrorCode) { 91 if(U_FAILURE(*pErrorCode)) { 92 return 0; 93 } 94 if(pattern==nullptr && length!=0) { 95 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 96 return 0; 97 } 98 UnicodeString patternString(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 char16_t *pattern, int32_t length, 107 char16_t *skeleton, int32_t capacity, 108 UErrorCode *pErrorCode) { 109 if(U_FAILURE(*pErrorCode)) { 110 return 0; 111 } 112 if(pattern==nullptr && length!=0) { 113 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 114 return 0; 115 } 116 UnicodeString patternString(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 char16_t *pattern, int32_t patternLength, 125 UBool override, 126 char16_t *conflictingPattern, int32_t capacity, int32_t *pLength, 127 UErrorCode *pErrorCode) { 128 if(U_FAILURE(*pErrorCode)) { 129 return UDATPG_NO_CONFLICT; 130 } 131 if(pattern==nullptr && patternLength!=0) { 132 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 133 return UDATPG_NO_CONFLICT; 134 } 135 UnicodeString patternString(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!=nullptr) { 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 char16_t *value, int32_t length) { 150 UnicodeString valueString(length < 0, value, length); 151 ((DateTimePatternGenerator *)dtpg)->setAppendItemFormat(field, valueString); 152 } 153 154 U_CAPI const char16_t * 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!=nullptr) { 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 char16_t *value, int32_t length) { 169 UnicodeString valueString(length < 0, value, length); 170 ((DateTimePatternGenerator *)dtpg)->setAppendItemName(field, valueString); 171 } 172 173 U_CAPI const char16_t * 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!=nullptr) { 179 *pLength=result.length(); 180 } 181 return result.getBuffer(); 182 } 183 184 U_CAPI int32_t U_EXPORT2 185 udatpg_getFieldDisplayName(const UDateTimePatternGenerator *dtpg, 186 UDateTimePatternField field, 187 UDateTimePGDisplayWidth width, 188 char16_t *fieldName, int32_t capacity, 189 UErrorCode *pErrorCode) { 190 if (U_FAILURE(*pErrorCode)) 191 return -1; 192 if (fieldName == nullptr ? capacity != 0 : capacity < 0) { 193 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; 194 return -1; 195 } 196 UnicodeString result = ((const DateTimePatternGenerator *)dtpg)->getFieldDisplayName(field,width); 197 if (fieldName == nullptr) { 198 return result.length(); 199 } 200 return result.extract(fieldName, capacity, *pErrorCode); 201 } 202 203 U_CAPI void U_EXPORT2 204 udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg, 205 const char16_t *dtFormat, int32_t length) { 206 UnicodeString dtFormatString(length < 0, dtFormat, length); 207 ((DateTimePatternGenerator *)dtpg)->setDateTimeFormat(dtFormatString); 208 } 209 210 U_CAPI const char16_t * U_EXPORT2 211 udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg, 212 int32_t *pLength) { 213 UErrorCode status = U_ZERO_ERROR; 214 return udatpg_getDateTimeFormatForStyle(dtpg, UDAT_MEDIUM, pLength, &status); 215 } 216 217 U_CAPI void U_EXPORT2 218 udatpg_setDateTimeFormatForStyle(UDateTimePatternGenerator *udtpg, 219 UDateFormatStyle style, 220 const char16_t *dateTimeFormat, int32_t length, 221 UErrorCode *pErrorCode) { 222 if (U_FAILURE(*pErrorCode)) { 223 return; 224 } else if (dateTimeFormat==nullptr) { 225 *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR; 226 return; 227 } 228 DateTimePatternGenerator *dtpg = reinterpret_cast<DateTimePatternGenerator *>(udtpg); 229 UnicodeString dtFormatString(length < 0, dateTimeFormat, length); 230 dtpg->setDateTimeFormat(style, dtFormatString, *pErrorCode); 231 } 232 233 U_CAPI const char16_t* U_EXPORT2 234 udatpg_getDateTimeFormatForStyle(const UDateTimePatternGenerator *udtpg, 235 UDateFormatStyle style, int32_t *pLength, 236 UErrorCode *pErrorCode) { 237 static const char16_t emptyString[] = { (char16_t)0 }; 238 if (U_FAILURE(*pErrorCode)) { 239 if (pLength !=nullptr) { 240 *pLength = 0; 241 } 242 return emptyString; 243 } 244 const DateTimePatternGenerator *dtpg = reinterpret_cast<const DateTimePatternGenerator *>(udtpg); 245 const UnicodeString &result = dtpg->getDateTimeFormat(style, *pErrorCode); 246 if (pLength != nullptr) { 247 *pLength=result.length(); 248 } 249 // Note: The UnicodeString for the dateTimeFormat string in the DateTimePatternGenerator 250 // was NUL-terminated what it was set, to avoid doing it here which could re-allocate 251 // the buffe and affect and cont references to the string or its buffer. 252 return result.getBuffer(); 253 } 254 255 U_CAPI void U_EXPORT2 256 udatpg_setDecimal(UDateTimePatternGenerator *dtpg, 257 const char16_t *decimal, int32_t length) { 258 UnicodeString decimalString(length < 0, decimal, length); 259 ((DateTimePatternGenerator *)dtpg)->setDecimal(decimalString); 260 } 261 262 U_CAPI const char16_t * U_EXPORT2 263 udatpg_getDecimal(const UDateTimePatternGenerator *dtpg, 264 int32_t *pLength) { 265 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDecimal(); 266 if(pLength!=nullptr) { 267 *pLength=result.length(); 268 } 269 return result.getBuffer(); 270 } 271 272 U_CAPI int32_t U_EXPORT2 273 udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg, 274 const char16_t *pattern, int32_t patternLength, 275 const char16_t *skeleton, int32_t skeletonLength, 276 char16_t *dest, int32_t destCapacity, 277 UErrorCode *pErrorCode) { 278 return udatpg_replaceFieldTypesWithOptions(dtpg, pattern, patternLength, skeleton, skeletonLength, 279 UDATPG_MATCH_NO_OPTIONS, 280 dest, destCapacity, pErrorCode); 281 } 282 283 U_CAPI int32_t U_EXPORT2 284 udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator *dtpg, 285 const char16_t *pattern, int32_t patternLength, 286 const char16_t *skeleton, int32_t skeletonLength, 287 UDateTimePatternMatchOptions options, 288 char16_t *dest, int32_t destCapacity, 289 UErrorCode *pErrorCode) { 290 if(U_FAILURE(*pErrorCode)) { 291 return 0; 292 } 293 if((pattern==nullptr && patternLength!=0) || (skeleton==nullptr && skeletonLength!=0)) { 294 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; 295 return 0; 296 } 297 UnicodeString patternString(patternLength < 0, pattern, patternLength); 298 UnicodeString skeletonString(skeletonLength < 0, skeleton, skeletonLength); 299 UnicodeString result=((DateTimePatternGenerator *)dtpg)->replaceFieldTypes(patternString, skeletonString, options, *pErrorCode); 300 return result.extract(dest, destCapacity, *pErrorCode); 301 } 302 303 U_CAPI UEnumeration * U_EXPORT2 304 udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { 305 return uenum_openFromStringEnumeration( 306 ((DateTimePatternGenerator *)dtpg)->getSkeletons(*pErrorCode), 307 pErrorCode); 308 } 309 310 U_CAPI UEnumeration * U_EXPORT2 311 udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) { 312 return uenum_openFromStringEnumeration( 313 ((DateTimePatternGenerator *)dtpg)->getBaseSkeletons(*pErrorCode), 314 pErrorCode); 315 } 316 317 U_CAPI const char16_t * U_EXPORT2 318 udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg, 319 const char16_t *skeleton, int32_t skeletonLength, 320 int32_t *pLength) { 321 UnicodeString skeletonString(skeletonLength < 0, skeleton, skeletonLength); 322 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getPatternForSkeleton(skeletonString); 323 if(pLength!=nullptr) { 324 *pLength=result.length(); 325 } 326 return result.getBuffer(); 327 } 328 329 U_CAPI UDateFormatHourCycle U_EXPORT2 330 udatpg_getDefaultHourCycle(const UDateTimePatternGenerator *dtpg, UErrorCode* pErrorCode) { 331 return ((const DateTimePatternGenerator *)dtpg)->getDefaultHourCycle(*pErrorCode); 332 } 333 334 #endif