sscanf.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) 2000-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File sscanf.c
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 02/08/00 george Creation. Copied from uscanf.c
  17. ******************************************************************************
  18. */
  19. #include "unicode/utypes.h"
  20. #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
  21. #include "unicode/putil.h"
  22. #include "unicode/ustdio.h"
  23. #include "unicode/ustring.h"
  24. #include "uscanf.h"
  25. #include "ufile.h"
  26. #include "ufmt_cmn.h"
  27. #include "cmemory.h"
  28. #include "cstring.h"
  29. U_CAPI int32_t U_EXPORT2
  30. u_sscanf(const char16_t *buffer,
  31. const char *patternSpecification,
  32. ... )
  33. {
  34. va_list ap;
  35. int32_t converted;
  36. va_start(ap, patternSpecification);
  37. converted = u_vsscanf(buffer, patternSpecification, ap);
  38. va_end(ap);
  39. return converted;
  40. }
  41. U_CAPI int32_t U_EXPORT2
  42. u_sscanf_u(const char16_t *buffer,
  43. const char16_t *patternSpecification,
  44. ... )
  45. {
  46. va_list ap;
  47. int32_t converted;
  48. va_start(ap, patternSpecification);
  49. converted = u_vsscanf_u(buffer, patternSpecification, ap);
  50. va_end(ap);
  51. return converted;
  52. }
  53. U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
  54. u_vsscanf(const char16_t *buffer,
  55. const char *patternSpecification,
  56. va_list ap)
  57. {
  58. int32_t converted;
  59. char16_t *pattern;
  60. char16_t patBuffer[UFMT_DEFAULT_BUFFER_SIZE];
  61. int32_t size = (int32_t)uprv_strlen(patternSpecification) + 1;
  62. /* convert from the default codepage to Unicode */
  63. if (size >= MAX_UCHAR_BUFFER_SIZE(patBuffer)) {
  64. pattern = (char16_t *)uprv_malloc(size * sizeof(char16_t));
  65. if (pattern == nullptr) {
  66. return 0;
  67. }
  68. }
  69. else {
  70. pattern = patBuffer;
  71. }
  72. u_charsToUChars(patternSpecification, pattern, size);
  73. /* do the work */
  74. converted = u_vsscanf_u(buffer, pattern, ap);
  75. /* clean up */
  76. if (pattern != patBuffer) {
  77. uprv_free(pattern);
  78. }
  79. return converted;
  80. }
  81. U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
  82. u_vsscanf_u(const char16_t *buffer,
  83. const char16_t *patternSpecification,
  84. va_list ap)
  85. {
  86. int32_t converted;
  87. UFILE inStr;
  88. inStr.fConverter = nullptr;
  89. inStr.fFile = nullptr;
  90. inStr.fOwnFile = false;
  91. #if !UCONFIG_NO_TRANSLITERATION
  92. inStr.fTranslit = nullptr;
  93. #endif
  94. inStr.fUCBuffer[0] = 0;
  95. inStr.str.fBuffer = (char16_t *)buffer;
  96. inStr.str.fPos = (char16_t *)buffer;
  97. inStr.str.fLimit = buffer + u_strlen(buffer);
  98. if (u_locbund_init(&inStr.str.fBundle, "en_US_POSIX") == nullptr) {
  99. return 0;
  100. }
  101. converted = u_scanf_parse(&inStr, patternSpecification, ap);
  102. u_locbund_close(&inStr.str.fBundle);
  103. /* return # of items converted */
  104. return converted;
  105. }
  106. #endif /* #if !UCONFIG_NO_FORMATTING */