Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/intl/icu/source/i18n/udatpg.cpp
Line
Count
Source (jump to first uncovered line)
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
0
udatpg_open(const char *locale, UErrorCode *pErrorCode) {
33
0
    if(locale==NULL) {
34
0
        return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(*pErrorCode);
35
0
    } else {
36
0
        return (UDateTimePatternGenerator *)DateTimePatternGenerator::createInstance(Locale(locale), *pErrorCode);
37
0
    }
38
0
}
39
40
U_CAPI UDateTimePatternGenerator * U_EXPORT2
41
0
udatpg_openEmpty(UErrorCode *pErrorCode) {
42
0
    return (UDateTimePatternGenerator *)DateTimePatternGenerator::createEmptyInstance(*pErrorCode);
43
0
}
44
45
U_CAPI void U_EXPORT2
46
0
udatpg_close(UDateTimePatternGenerator *dtpg) {
47
0
    delete (DateTimePatternGenerator *)dtpg;
48
0
}
49
50
U_CAPI UDateTimePatternGenerator * U_EXPORT2
51
0
udatpg_clone(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
52
0
    if(U_FAILURE(*pErrorCode)) {
53
0
        return NULL;
54
0
    }
55
0
    return (UDateTimePatternGenerator *)(((const DateTimePatternGenerator *)dtpg)->clone());
56
0
}
57
58
U_CAPI int32_t U_EXPORT2
59
udatpg_getBestPattern(UDateTimePatternGenerator *dtpg,
60
                      const UChar *skeleton, int32_t length,
61
                      UChar *bestPattern, int32_t capacity,
62
0
                      UErrorCode *pErrorCode) {
63
0
    return udatpg_getBestPatternWithOptions(dtpg, skeleton, length,
64
0
                                            UDATPG_MATCH_NO_OPTIONS,
65
0
                                            bestPattern, capacity, pErrorCode);
66
0
}
67
68
U_CAPI int32_t U_EXPORT2
69
udatpg_getBestPatternWithOptions(UDateTimePatternGenerator *dtpg,
70
                                 const UChar *skeleton, int32_t length,
71
                                 UDateTimePatternMatchOptions options,
72
                                 UChar *bestPattern, int32_t capacity,
73
0
                                 UErrorCode *pErrorCode) {
74
0
    if(U_FAILURE(*pErrorCode)) {
75
0
        return 0;
76
0
    }
77
0
    if(skeleton==NULL && length!=0) {
78
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
79
0
        return 0;
80
0
    }
81
0
    UnicodeString skeletonString((UBool)(length<0), skeleton, length);
82
0
    UnicodeString result=((DateTimePatternGenerator *)dtpg)->getBestPattern(skeletonString, options, *pErrorCode);
83
0
    return result.extract(bestPattern, capacity, *pErrorCode);
84
0
}
85
86
U_CAPI int32_t U_EXPORT2
87
udatpg_getSkeleton(UDateTimePatternGenerator * /* dtpg */,
88
                   const UChar *pattern, int32_t length,
89
                   UChar *skeleton, int32_t capacity,
90
0
                   UErrorCode *pErrorCode) {
91
0
    if(U_FAILURE(*pErrorCode)) {
92
0
        return 0;
93
0
    }
94
0
    if(pattern==NULL && length!=0) {
95
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
96
0
        return 0;
97
0
    }
98
0
    UnicodeString patternString((UBool)(length<0), pattern, length);
99
0
    UnicodeString result=DateTimePatternGenerator::staticGetSkeleton(
100
0
            patternString, *pErrorCode);
101
0
    return result.extract(skeleton, capacity, *pErrorCode);
102
0
}
103
104
U_CAPI int32_t U_EXPORT2
105
udatpg_getBaseSkeleton(UDateTimePatternGenerator * /* dtpg */,
106
                       const UChar *pattern, int32_t length,
107
                       UChar *skeleton, int32_t capacity,
108
0
                       UErrorCode *pErrorCode) {
109
0
    if(U_FAILURE(*pErrorCode)) {
110
0
        return 0;
111
0
    }
112
0
    if(pattern==NULL && length!=0) {
113
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
114
0
        return 0;
115
0
    }
116
0
    UnicodeString patternString((UBool)(length<0), pattern, length);
117
0
    UnicodeString result=DateTimePatternGenerator::staticGetBaseSkeleton(
118
0
            patternString, *pErrorCode);
119
0
    return result.extract(skeleton, capacity, *pErrorCode);
120
0
}
121
122
U_CAPI UDateTimePatternConflict U_EXPORT2
123
udatpg_addPattern(UDateTimePatternGenerator *dtpg,
124
                  const UChar *pattern, int32_t patternLength,
125
                  UBool override,
126
                  UChar *conflictingPattern, int32_t capacity, int32_t *pLength,
127
0
                  UErrorCode *pErrorCode) {
128
0
    if(U_FAILURE(*pErrorCode)) {
129
0
        return UDATPG_NO_CONFLICT;
130
0
    }
131
0
    if(pattern==NULL && patternLength!=0) {
132
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
133
0
        return UDATPG_NO_CONFLICT;
134
0
    }
135
0
    UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength);
136
0
    UnicodeString conflictingPatternString;
137
0
    UDateTimePatternConflict result=((DateTimePatternGenerator *)dtpg)->
138
0
            addPattern(patternString, override, conflictingPatternString, *pErrorCode);
139
0
    int32_t length=conflictingPatternString.extract(conflictingPattern, capacity, *pErrorCode);
140
0
    if(pLength!=NULL) {
141
0
        *pLength=length;
142
0
    }
143
0
    return result;
144
0
}
145
146
U_CAPI void U_EXPORT2
147
udatpg_setAppendItemFormat(UDateTimePatternGenerator *dtpg,
148
                           UDateTimePatternField field,
149
0
                           const UChar *value, int32_t length) {
150
0
    UnicodeString valueString((UBool)(length<0), value, length);
151
0
    ((DateTimePatternGenerator *)dtpg)->setAppendItemFormat(field, valueString);
152
0
}
153
154
U_CAPI const UChar * U_EXPORT2
155
udatpg_getAppendItemFormat(const UDateTimePatternGenerator *dtpg,
156
                           UDateTimePatternField field,
157
0
                           int32_t *pLength) {
158
0
    const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemFormat(field);
159
0
    if(pLength!=NULL) {
160
0
        *pLength=result.length();
161
0
    }
162
0
    return result.getBuffer();
163
0
}
164
165
U_CAPI void U_EXPORT2
166
udatpg_setAppendItemName(UDateTimePatternGenerator *dtpg,
167
                         UDateTimePatternField field,
168
0
                         const UChar *value, int32_t length) {
169
0
    UnicodeString valueString((UBool)(length<0), value, length);
170
0
    ((DateTimePatternGenerator *)dtpg)->setAppendItemName(field, valueString);
171
0
}
172
173
U_CAPI const UChar * U_EXPORT2
174
udatpg_getAppendItemName(const UDateTimePatternGenerator *dtpg,
175
                         UDateTimePatternField field,
176
0
                         int32_t *pLength) {
177
0
    const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getAppendItemName(field);
178
0
    if(pLength!=NULL) {
179
0
        *pLength=result.length();
180
0
    }
181
0
    return result.getBuffer();
182
0
}
183
184
U_CAPI int32_t U_EXPORT2
185
udatpg_getFieldDisplayName(const UDateTimePatternGenerator *dtpg,
186
                           UDateTimePatternField field,
187
                           UDateTimePGDisplayWidth width,
188
                           UChar *fieldName, int32_t capacity,
189
0
                           UErrorCode *pErrorCode) {
190
0
    if (U_FAILURE(*pErrorCode))
191
0
        return -1;
192
0
    if (fieldName == NULL ? capacity != 0 : capacity < 0) {
193
0
        *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
194
0
        return -1;
195
0
    }
196
0
    UnicodeString result = ((const DateTimePatternGenerator *)dtpg)->getFieldDisplayName(field,width);
197
0
    if (fieldName == NULL) {
198
0
        return result.length();
199
0
    }
200
0
    return result.extract(fieldName, capacity, *pErrorCode);
201
0
}
202
203
U_CAPI void U_EXPORT2
204
udatpg_setDateTimeFormat(const UDateTimePatternGenerator *dtpg,
205
0
                         const UChar *dtFormat, int32_t length) {
206
0
    UnicodeString dtFormatString((UBool)(length<0), dtFormat, length);
207
0
    ((DateTimePatternGenerator *)dtpg)->setDateTimeFormat(dtFormatString);
208
0
}
209
210
U_CAPI const UChar * U_EXPORT2
211
udatpg_getDateTimeFormat(const UDateTimePatternGenerator *dtpg,
212
0
                         int32_t *pLength) {
213
0
    const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDateTimeFormat();
214
0
    if(pLength!=NULL) {
215
0
        *pLength=result.length();
216
0
    }
217
0
    return result.getBuffer();
218
0
}
219
220
U_CAPI void U_EXPORT2
221
udatpg_setDecimal(UDateTimePatternGenerator *dtpg,
222
0
                  const UChar *decimal, int32_t length) {
223
0
    UnicodeString decimalString((UBool)(length<0), decimal, length);
224
0
    ((DateTimePatternGenerator *)dtpg)->setDecimal(decimalString);
225
0
}
226
227
U_CAPI const UChar * U_EXPORT2
228
udatpg_getDecimal(const UDateTimePatternGenerator *dtpg,
229
0
                  int32_t *pLength) {
230
0
    const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getDecimal();
231
0
    if(pLength!=NULL) {
232
0
        *pLength=result.length();
233
0
    }
234
0
    return result.getBuffer();
235
0
}
236
237
U_CAPI int32_t U_EXPORT2
238
udatpg_replaceFieldTypes(UDateTimePatternGenerator *dtpg,
239
                         const UChar *pattern, int32_t patternLength,
240
                         const UChar *skeleton, int32_t skeletonLength,
241
                         UChar *dest, int32_t destCapacity,
242
0
                         UErrorCode *pErrorCode) {
243
0
    return udatpg_replaceFieldTypesWithOptions(dtpg, pattern, patternLength, skeleton, skeletonLength,
244
0
                                               UDATPG_MATCH_NO_OPTIONS,
245
0
                                               dest, destCapacity, pErrorCode);
246
0
}
247
248
U_CAPI int32_t U_EXPORT2
249
udatpg_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
0
                                    UErrorCode *pErrorCode) {
255
0
    if(U_FAILURE(*pErrorCode)) {
256
0
        return 0;
257
0
    }
258
0
    if((pattern==NULL && patternLength!=0) || (skeleton==NULL && skeletonLength!=0)) {
259
0
        *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
260
0
        return 0;
261
0
    }
262
0
    UnicodeString patternString((UBool)(patternLength<0), pattern, patternLength);
263
0
    UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
264
0
    UnicodeString result=((DateTimePatternGenerator *)dtpg)->replaceFieldTypes(patternString, skeletonString, options, *pErrorCode);
265
0
    return result.extract(dest, destCapacity, *pErrorCode);
266
0
}
267
268
U_CAPI UEnumeration * U_EXPORT2
269
0
udatpg_openSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
270
0
    return uenum_openFromStringEnumeration(
271
0
                ((DateTimePatternGenerator *)dtpg)->getSkeletons(*pErrorCode),
272
0
                pErrorCode);
273
0
}
274
275
U_CAPI UEnumeration * U_EXPORT2
276
0
udatpg_openBaseSkeletons(const UDateTimePatternGenerator *dtpg, UErrorCode *pErrorCode) {
277
0
    return uenum_openFromStringEnumeration(
278
0
                ((DateTimePatternGenerator *)dtpg)->getBaseSkeletons(*pErrorCode),
279
0
                pErrorCode);
280
0
}
281
282
U_CAPI const UChar * U_EXPORT2
283
udatpg_getPatternForSkeleton(const UDateTimePatternGenerator *dtpg,
284
                             const UChar *skeleton, int32_t skeletonLength,
285
0
                             int32_t *pLength) {
286
0
    UnicodeString skeletonString((UBool)(skeletonLength<0), skeleton, skeletonLength);
287
0
    const UnicodeString &result=((const DateTimePatternGenerator *)dtpg)->getPatternForSkeleton(skeletonString);
288
0
    if(pLength!=NULL) {
289
0
        *pLength=result.length();
290
0
    }
291
0
    return result.getBuffer();
292
0
}
293
294
#endif