kstdio.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-03-10 Meco Man the first version
  9. */
  10. #include <rtthread.h>
  11. /**
  12. * @brief This function will fill a formatted string to buffer.
  13. *
  14. * @param buf is the buffer to save formatted string.
  15. *
  16. * @param size is the size of buffer.
  17. *
  18. * @param fmt is the format parameters.
  19. *
  20. * @return The number of characters actually written to buffer.
  21. */
  22. int rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  23. {
  24. rt_int32_t n = 0;
  25. va_list args;
  26. va_start(args, fmt);
  27. n = rt_vsnprintf(buf, size, fmt, args);
  28. va_end(args);
  29. return n;
  30. }
  31. RTM_EXPORT(rt_snprintf);
  32. /**
  33. * @brief This function will fill a formatted string to buffer.
  34. *
  35. * @param buf is the buffer to save formatted string.
  36. *
  37. * @param format is the format parameters.
  38. *
  39. * @param arg_ptr is a list of variable parameters.
  40. *
  41. * @return The number of characters actually written to buffer.
  42. */
  43. int rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  44. {
  45. return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
  46. }
  47. RTM_EXPORT(rt_vsprintf);
  48. /**
  49. * @brief This function will fill a formatted string to buffer
  50. *
  51. * @param buf the buffer to save formatted string.
  52. *
  53. * @param format is the format parameters.
  54. *
  55. * @return The number of characters actually written to buffer.
  56. */
  57. int rt_sprintf(char *buf, const char *format, ...)
  58. {
  59. rt_int32_t n = 0;
  60. va_list arg_ptr;
  61. va_start(arg_ptr, format);
  62. n = rt_vsprintf(buf, format, arg_ptr);
  63. va_end(arg_ptr);
  64. return n;
  65. }
  66. RTM_EXPORT(rt_sprintf);
  67. /**
  68. * @brief This function parses a formatted string from the input string.
  69. *
  70. * @param str the input string to be parsed.
  71. *
  72. * @param format the format string that specifies how to interpret the input.
  73. *
  74. * @return The number of input items successfully matched and assigned.
  75. */
  76. int rt_sscanf(const char *str, const char *format, ...)
  77. {
  78. va_list ap;
  79. int rv;
  80. va_start(ap, format);
  81. rv = rt_vsscanf(str, format, ap);
  82. va_end(ap);
  83. return rv;
  84. }
  85. RTM_EXPORT(rt_sscanf);