uscanf.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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) 1998-2014, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File uscanf.c
  12. *
  13. * Modification History:
  14. *
  15. * Date Name Description
  16. * 12/02/98 stephen Creation.
  17. * 03/13/99 stephen Modified for new C API.
  18. ******************************************************************************
  19. */
  20. #include "unicode/utypes.h"
  21. #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_CONVERSION
  22. #include "unicode/putil.h"
  23. #include "unicode/ustdio.h"
  24. #include "unicode/ustring.h"
  25. #include "uscanf.h"
  26. #include "ufile.h"
  27. #include "ufmt_cmn.h"
  28. #include "cmemory.h"
  29. #include "cstring.h"
  30. U_CAPI int32_t U_EXPORT2
  31. u_fscanf(UFILE *f,
  32. const char *patternSpecification,
  33. ... )
  34. {
  35. va_list ap;
  36. int32_t converted;
  37. va_start(ap, patternSpecification);
  38. converted = u_vfscanf(f, patternSpecification, ap);
  39. va_end(ap);
  40. return converted;
  41. }
  42. U_CAPI int32_t U_EXPORT2
  43. u_fscanf_u(UFILE *f,
  44. const char16_t *patternSpecification,
  45. ... )
  46. {
  47. va_list ap;
  48. int32_t converted;
  49. va_start(ap, patternSpecification);
  50. converted = u_vfscanf_u(f, patternSpecification, ap);
  51. va_end(ap);
  52. return converted;
  53. }
  54. U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
  55. u_vfscanf(UFILE *f,
  56. const char *patternSpecification,
  57. va_list ap)
  58. {
  59. int32_t converted;
  60. char16_t *pattern;
  61. char16_t patBuffer[UFMT_DEFAULT_BUFFER_SIZE];
  62. int32_t size = (int32_t)uprv_strlen(patternSpecification) + 1;
  63. /* convert from the default codepage to Unicode */
  64. if (size >= MAX_UCHAR_BUFFER_SIZE(patBuffer)) {
  65. pattern = (char16_t *)uprv_malloc(size * sizeof(char16_t));
  66. if (pattern == nullptr) {
  67. return 0;
  68. }
  69. }
  70. else {
  71. pattern = patBuffer;
  72. }
  73. u_charsToUChars(patternSpecification, pattern, size);
  74. /* do the work */
  75. converted = u_vfscanf_u(f, pattern, ap);
  76. /* clean up */
  77. if (pattern != patBuffer) {
  78. uprv_free(pattern);
  79. }
  80. return converted;
  81. }
  82. U_CAPI int32_t U_EXPORT2 /* U_CAPI ... U_EXPORT2 added by Peter Kirk 17 Nov 2001 */
  83. u_vfscanf_u(UFILE *f,
  84. const char16_t *patternSpecification,
  85. va_list ap)
  86. {
  87. return u_scanf_parse(f, patternSpecification, ap);
  88. }
  89. #endif /* #if !UCONFIG_NO_FORMATTING */