intl/icu/source/i18n/udatpg.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial