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