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
29U_NAMESPACE_USE
30
31U_CAPI UDateTimePatternGenerator * U_EXPORT2
32udatpg_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
40U_CAPI UDateTimePatternGenerator * U_EXPORT2
41udatpg_openEmpty(UErrorCode *pErrorCode) {
42 return (UDateTimePatternGenerator *)DateTimePatternGenerator::createEmptyInstance(*pErrorCode);
43}
44
45U_CAPI void U_EXPORT2
46udatpg_close(UDateTimePatternGenerator *dtpg) {
47 delete (DateTimePatternGenerator *)dtpg;
48}
49
50U_CAPI UDateTimePatternGenerator * U_EXPORT2
51udatpg_clone(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
52 if(U_FAILURE(*pErrorCode)) {
53 return NULL;
54 }
55 return (UDateTimePatternGenerator *)(((const DateTimePatternGenerator *)dtpg)->clone());
56}
57
58U_CAPI int32_t U_EXPORT2
59udatpg_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
68U_CAPI int32_t U_EXPORT2
69udatpg_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
86U_CAPI int32_t U_EXPORT2
87udatpg_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
104U_CAPI int32_t U_EXPORT2
105udatpg_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
122U_CAPI UDateTimePatternConflict U_EXPORT2
123udatpg_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
146U_CAPI void U_EXPORT2
147udatpg_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
154U_CAPI const UChar * U_EXPORT2
155udatpg_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
165U_CAPI void U_EXPORT2
166udatpg_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
173U_CAPI const UChar * U_EXPORT2
174udatpg_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
184U_CAPI int32_t U_EXPORT2
185udatpg_getFieldDisplayName(const UDateTimePatternGenerator *dtpg,
186 UDateTimePatternField field,
187 UDateTimePGDisplayWidth width,
188 UChar *fieldName, int32_t capacity,
189 UErrorCode *pErrorCode) {
190 if (U_FAILURE(*pErrorCode))
191 return -1;
192 if (fieldName == NULL ? 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 == NULL) {
198 return result.length();
199 }
200 return result.extract(fieldName, capacity, *pErrorCode);
201}
202
203U_CAPI void U_EXPORT2
204udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg,
205 const UChar *dtFormat, int32_t length) {
206 UnicodeString dtFormatString((UBool)(length<0), dtFormat, length);
207 ((DateTimePatternGenerator *)dtpg)->setDateTimeFormat(dtFormatString);
208}
209
210U_CAPI const UChar * U_EXPORT2
211udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg,
212 int32_t *pLength) {
213 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDateTimeFormat();
214 if(pLength!=NULL) {
215 *pLength=result.length();
216 }
217 return result.getBuffer();
218}
219
220U_CAPI void U_EXPORT2
221udatpg_setDecimal(UDateTimePatternGenerator *dtpg,
222 const UChar *decimal, int32_t length) {
223 UnicodeString decimalString((UBool)(length<0), decimal, length);
224 ((DateTimePatternGenerator *)dtpg)->setDecimal(decimalString);
225}
226
227U_CAPI const UChar * U_EXPORT2
228udatpg_getDecimal(const UDateTimePatternGenerator *dtpg,
229 int32_t *pLength) {
230 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDecimal();
231 if(pLength!=NULL) {
232 *pLength=result.length();
233 }
234 return result.getBuffer();
235}
236
237U_CAPI int32_t U_EXPORT2
238udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg,
239 const UChar *pattern, int32_t patternLength,
240 const UChar *skeleton, int32_t skeletonLength,
241 UChar *dest, int32_t destCapacity,
242 UErrorCode *pErrorCode) {
243 return udatpg_replaceFieldTypesWithOptions(dtpg, pattern, patternLength, skeleton, skeletonLength,
244 UDATPG_MATCH_NO_OPTIONS,
245 dest, destCapacity, pErrorCode);
246}
247
248U_CAPI int32_t U_EXPORT2
249udatpg_replaceFieldTypesWithOptions(UDateTimePatternGenerator *dtpg,
250 const UChar *pattern, int32_t patternLength,
251 const UChar *skeleton, int32_t skeletonLength,
252 UDateTimePatternMatchOptions options,
253 UChar *dest, int32_t destCapacity,
254 UErrorCode *pErrorCode) {
255 if(U_FAILURE(*pErrorCode)) {
256 return 0;
257 }
258 if((pattern==NULL && patternLength!=0) || (skeleton==NULL && skeletonLength!=0)) {
259 *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
260 return 0;
261 }
262 UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength);
263 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
264 UnicodeString result=((DateTimePatternGenerator *)dtpg)->replaceFieldTypes(patternString, skeletonString, options, *pErrorCode);
265 return result.extract(dest, destCapacity, *pErrorCode);
266}
267
268U_CAPI UEnumeration * U_EXPORT2
269udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
270 return uenum_openFromStringEnumeration(
271 ((DateTimePatternGenerator *)dtpg)->getSkeletons(*pErrorCode),
272 pErrorCode);
273}
274
275U_CAPI UEnumeration * U_EXPORT2
276udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
277 return uenum_openFromStringEnumeration(
278 ((DateTimePatternGenerator *)dtpg)->getBaseSkeletons(*pErrorCode),
279 pErrorCode);
280}
281
282U_CAPI const UChar * U_EXPORT2
283udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg,
284 const UChar *skeleton, int32_t skeletonLength,
285 int32_t *pLength) {
286 UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
287 const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getPatternForSkeleton(skeletonString);
288 if(pLength!=NULL) {
289 *pLength=result.length();
290 }
291 return result.getBuffer();
292}
293
294#endif
295