udatpg.cpp 10 KB

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