2 *******************************************************************************
4 * Copyright (C) 2009, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: udatpg.cpp
10 * tab size: 8 (not used)
13 * created on: 2007jul30
14 * created by: Markus W. Scherer
17 #include "unicode/utypes.h"
19 #if !UCONFIG_NO_FORMATTING
21 #include "unicode/udatpg.h"
22 #include "unicode/uenum.h"
23 #include "unicode/strenum.h"
24 #include "unicode/dtptngen.h"
29 U_DRAFT UDateTimePatternGenerator
* U_EXPORT2
30 udatpg_open(const char *locale
, UErrorCode
*pErrorCode
) {
32 return (UDateTimePatternGenerator
*)DateTimePatternGenerator::createInstance(*pErrorCode
);
34 return (UDateTimePatternGenerator
*)DateTimePatternGenerator::createInstance(Locale(locale
), *pErrorCode
);
38 U_DRAFT UDateTimePatternGenerator
* U_EXPORT2
39 udatpg_openEmpty(UErrorCode
*pErrorCode
) {
40 return (UDateTimePatternGenerator
*)DateTimePatternGenerator::createEmptyInstance(*pErrorCode
);
43 U_DRAFT
void U_EXPORT2
44 udatpg_close(UDateTimePatternGenerator
*dtpg
) {
45 delete (DateTimePatternGenerator
*)dtpg
;
48 U_DRAFT UDateTimePatternGenerator
* U_EXPORT2
49 udatpg_clone(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
) {
50 if(U_FAILURE(*pErrorCode
)) {
53 return (UDateTimePatternGenerator
*)(((const DateTimePatternGenerator
*)dtpg
)->clone());
56 U_DRAFT
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
);
66 U_DRAFT
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
)) {
75 if(skeleton
==NULL
&& length
!=0) {
76 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
79 UnicodeString
skeletonString((UBool
)(length
<0), skeleton
, length
);
80 UnicodeString result
=((DateTimePatternGenerator
*)dtpg
)->getBestPattern(skeletonString
, options
, *pErrorCode
);
81 return result
.extract(bestPattern
, capacity
, *pErrorCode
);
84 U_DRAFT
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
)) {
92 if(pattern
==NULL
&& length
!=0) {
93 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
96 UnicodeString
patternString((UBool
)(length
<0), pattern
, length
);
97 UnicodeString result
=((DateTimePatternGenerator
*)dtpg
)->getSkeleton(patternString
, *pErrorCode
);
98 return result
.extract(skeleton
, capacity
, *pErrorCode
);
101 U_DRAFT
int32_t U_EXPORT2
102 udatpg_getBaseSkeleton(UDateTimePatternGenerator
*dtpg
,
103 const UChar
*pattern
, int32_t length
,
104 UChar
*skeleton
, int32_t capacity
,
105 UErrorCode
*pErrorCode
) {
106 if(U_FAILURE(*pErrorCode
)) {
109 if(pattern
==NULL
&& length
!=0) {
110 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
113 UnicodeString
patternString((UBool
)(length
<0), pattern
, length
);
114 UnicodeString result
=((DateTimePatternGenerator
*)dtpg
)->getBaseSkeleton(patternString
, *pErrorCode
);
115 return result
.extract(skeleton
, capacity
, *pErrorCode
);
118 U_DRAFT UDateTimePatternConflict U_EXPORT2
119 udatpg_addPattern(UDateTimePatternGenerator
*dtpg
,
120 const UChar
*pattern
, int32_t patternLength
,
122 UChar
*conflictingPattern
, int32_t capacity
, int32_t *pLength
,
123 UErrorCode
*pErrorCode
) {
124 if(U_FAILURE(*pErrorCode
)) {
125 return UDATPG_NO_CONFLICT
;
127 if(pattern
==NULL
&& patternLength
!=0) {
128 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
129 return UDATPG_NO_CONFLICT
;
131 UnicodeString
patternString((UBool
)(patternLength
<0), pattern
, patternLength
);
132 UnicodeString conflictingPatternString
;
133 UDateTimePatternConflict result
=((DateTimePatternGenerator
*)dtpg
)->
134 addPattern(patternString
, override
, conflictingPatternString
, *pErrorCode
);
135 int32_t length
=conflictingPatternString
.extract(conflictingPattern
, capacity
, *pErrorCode
);
142 U_DRAFT
void U_EXPORT2
143 udatpg_setAppendItemFormat(UDateTimePatternGenerator
*dtpg
,
144 UDateTimePatternField field
,
145 const UChar
*value
, int32_t length
) {
146 UnicodeString
valueString((UBool
)(length
<0), value
, length
);
147 ((DateTimePatternGenerator
*)dtpg
)->setAppendItemFormat(field
, valueString
);
150 U_DRAFT
const UChar
* U_EXPORT2
151 udatpg_getAppendItemFormat(const UDateTimePatternGenerator
*dtpg
,
152 UDateTimePatternField field
,
154 const UnicodeString
&result
=((const DateTimePatternGenerator
*)dtpg
)->getAppendItemFormat(field
);
156 *pLength
=result
.length();
158 return result
.getBuffer();
161 U_DRAFT
void U_EXPORT2
162 udatpg_setAppendItemName(UDateTimePatternGenerator
*dtpg
,
163 UDateTimePatternField field
,
164 const UChar
*value
, int32_t length
) {
165 UnicodeString
valueString((UBool
)(length
<0), value
, length
);
166 ((DateTimePatternGenerator
*)dtpg
)->setAppendItemName(field
, valueString
);
169 U_DRAFT
const UChar
* U_EXPORT2
170 udatpg_getAppendItemName(const UDateTimePatternGenerator
*dtpg
,
171 UDateTimePatternField field
,
173 const UnicodeString
&result
=((const DateTimePatternGenerator
*)dtpg
)->getAppendItemName(field
);
175 *pLength
=result
.length();
177 return result
.getBuffer();
180 U_DRAFT
void U_EXPORT2
181 udatpg_setDateTimeFormat(const UDateTimePatternGenerator
*dtpg
,
182 const UChar
*dtFormat
, int32_t length
) {
183 UnicodeString
dtFormatString((UBool
)(length
<0), dtFormat
, length
);
184 ((DateTimePatternGenerator
*)dtpg
)->setDateTimeFormat(dtFormatString
);
187 U_DRAFT
const UChar
* U_EXPORT2
188 udatpg_getDateTimeFormat(const UDateTimePatternGenerator
*dtpg
,
190 const UnicodeString
&result
=((const DateTimePatternGenerator
*)dtpg
)->getDateTimeFormat();
192 *pLength
=result
.length();
194 return result
.getBuffer();
197 U_DRAFT
void U_EXPORT2
198 udatpg_setDecimal(UDateTimePatternGenerator
*dtpg
,
199 const UChar
*decimal
, int32_t length
) {
200 UnicodeString
decimalString((UBool
)(length
<0), decimal
, length
);
201 ((DateTimePatternGenerator
*)dtpg
)->setDecimal(decimalString
);
204 U_DRAFT
const UChar
* U_EXPORT2
205 udatpg_getDecimal(const UDateTimePatternGenerator
*dtpg
,
207 const UnicodeString
&result
=((const DateTimePatternGenerator
*)dtpg
)->getDecimal();
209 *pLength
=result
.length();
211 return result
.getBuffer();
214 U_DRAFT
int32_t U_EXPORT2
215 udatpg_replaceFieldTypes(UDateTimePatternGenerator
*dtpg
,
216 const UChar
*pattern
, int32_t patternLength
,
217 const UChar
*skeleton
, int32_t skeletonLength
,
218 UChar
*dest
, int32_t destCapacity
,
219 UErrorCode
*pErrorCode
) {
220 return udatpg_replaceFieldTypesWithOptions(dtpg
, pattern
, patternLength
, skeleton
, skeletonLength
,
221 UDATPG_MATCH_NO_OPTIONS
,
222 dest
, destCapacity
, pErrorCode
);
225 U_DRAFT
int32_t U_EXPORT2
226 udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator
*dtpg
,
227 const UChar
*pattern
, int32_t patternLength
,
228 const UChar
*skeleton
, int32_t skeletonLength
,
229 UDateTimePatternMatchOptions options
,
230 UChar
*dest
, int32_t destCapacity
,
231 UErrorCode
*pErrorCode
) {
232 if(U_FAILURE(*pErrorCode
)) {
235 if((pattern
==NULL
&& patternLength
!=0) || (skeleton
==NULL
&& skeletonLength
!=0)) {
236 *pErrorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
239 UnicodeString
patternString((UBool
)(patternLength
<0), pattern
, patternLength
);
240 UnicodeString
skeletonString((UBool
)(skeletonLength
<0), skeleton
, skeletonLength
);
241 UnicodeString result
=((DateTimePatternGenerator
*)dtpg
)->replaceFieldTypes(patternString
, skeletonString
, options
, *pErrorCode
);
242 return result
.extract(dest
, destCapacity
, *pErrorCode
);
245 U_DRAFT UEnumeration
* U_EXPORT2
246 udatpg_openSkeletons(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
) {
247 return uenum_openFromStringEnumeration(
248 ((DateTimePatternGenerator
*)dtpg
)->getSkeletons(*pErrorCode
),
252 U_DRAFT UEnumeration
* U_EXPORT2
253 udatpg_openBaseSkeletons(const UDateTimePatternGenerator
*dtpg
, UErrorCode
*pErrorCode
) {
254 return uenum_openFromStringEnumeration(
255 ((DateTimePatternGenerator
*)dtpg
)->getBaseSkeletons(*pErrorCode
),
259 U_DRAFT
const UChar
* U_EXPORT2
260 udatpg_getPatternForSkeleton(const UDateTimePatternGenerator
*dtpg
,
261 const UChar
*skeleton
, int32_t skeletonLength
,
263 UnicodeString
skeletonString((UBool
)(skeletonLength
<0), skeleton
, skeletonLength
);
264 const UnicodeString
&result
=((const DateTimePatternGenerator
*)dtpg
)->getPatternForSkeleton(skeletonString
);
266 *pLength
=result
.length();
268 return result
.getBuffer();