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
11 #include <rtthread.h>
12 #if defined(RT_KLIBC_USING_LIBC_VSSCANF) || \
13 defined(RT_KLIBC_USING_LIBC_VSNPRINTF)
14 #include <stdio.h>
15 #endif
16
17 /**
18 * @brief This function will fill a formatted string to buffer.
19 *
20 * @param buf is the buffer to save formatted string.
21 *
22 * @param size is the size of buffer.
23 *
24 * @param fmt is the format parameters.
25 *
26 * @return The number of characters actually written to buffer.
27 */
rt_snprintf(char * buf,rt_size_t size,const char * fmt,...)28 int rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
29 {
30 rt_int32_t n = 0;
31 va_list args;
32
33 va_start(args, fmt);
34 n = rt_vsnprintf(buf, size, fmt, args);
35 va_end(args);
36
37 return n;
38 }
39 RTM_EXPORT(rt_snprintf);
40
41 /**
42 * @brief This function will fill a formatted string to buffer.
43 *
44 * @param buf is the buffer to save formatted string.
45 *
46 * @param format is the format parameters.
47 *
48 * @param arg_ptr is a list of variable parameters.
49 *
50 * @return The number of characters actually written to buffer.
51 */
rt_vsprintf(char * buf,const char * format,va_list arg_ptr)52 int rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
53 {
54 return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
55 }
56 RTM_EXPORT(rt_vsprintf);
57
58 /**
59 * @brief This function will fill a formatted string to buffer
60 *
61 * @param buf the buffer to save formatted string.
62 *
63 * @param format is the format parameters.
64 *
65 * @return The number of characters actually written to buffer.
66 */
rt_sprintf(char * buf,const char * format,...)67 int rt_sprintf(char *buf, const char *format, ...)
68 {
69 rt_int32_t n = 0;
70 va_list arg_ptr;
71
72 va_start(arg_ptr, format);
73 n = rt_vsprintf(buf, format, arg_ptr);
74 va_end(arg_ptr);
75
76 return n;
77 }
78 RTM_EXPORT(rt_sprintf);
79
80 #ifdef RT_KLIBC_USING_LIBC_VSNPRINTF
rt_vsnprintf(char * buf,rt_size_t size,const char * fmt,va_list args)81 int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args)
82 {
83 return vsnprintf(buf, size, fmt, args);
84 }
85 #endif /* RT_KLIBC_USING_LIBC_VSNPRINTF */
86 RTM_EXPORT(rt_vsnprintf);
87
88 #ifdef RT_KLIBC_USING_LIBC_VSSCANF
rt_vsscanf(const char * buffer,const char * format,va_list ap)89 int rt_vsscanf(const char *buffer, const char *format, va_list ap)
90 {
91 return vsscanf(buffer, format, ap);
92 }
93 #endif /* RT_KLIBC_USING_LIBC_VSSCANF */
94 RTM_EXPORT(rt_vsscanf);
95
96 /**
97 * @brief This function parses a formatted string from the input string.
98 *
99 * @param str the input string to be parsed.
100 *
101 * @param format the format string that specifies how to interpret the input.
102 *
103 * @return The number of input items successfully matched and assigned.
104 */
rt_sscanf(const char * str,const char * format,...)105 int rt_sscanf(const char *str, const char *format, ...)
106 {
107 va_list ap;
108 int rv;
109
110 va_start(ap, format);
111 rv = rt_vsscanf(str, format, ap);
112 va_end(ap);
113
114 return rv;
115 }
116 RTM_EXPORT(rt_sscanf);
117