Standard I/O (stdio)#
- group stdio
Freestanding stdio: the printf family, string routines, and character I/O, suitable for bare-metal applications.
For an overview, see Standard I/O (STDIO).
rcsw/stdio/printf.h#
Wrapper over the eyalroz/printf library, providing the RCSW stdio_printf() family of functions.
This header adapts the upstream eyalroz/printf API to the RCSW naming convention and type system. Application code should include this header rather than the upstream <printf.h> directly.
Functions
-
int stdio_printf(const char *format, ...)#
An implementation of the C standard’s printf().
Note
Emits characters through stdio_putchar(), which calls the function named by
RCSW_CONFIG_STDIO_PUTCHAR. That is what the platform or BSP provides, if it overrides the default.- Parameters:
format – A string specifying the format of the output, with %-marked specifiers of how to interpret additional arguments.
... – Additional arguments, one for each %-specifier in
format.
- Returns:
The number of characters written, not counting the terminating NUL.
-
int stdio_vprintf(const char *format, va_list arg)#
Same as stdio_printf(), but accepts a va_list directly.
- Parameters:
format – A format string.
arg – A va_list of arguments corresponding to
format.
- Returns:
The number of characters written, not counting the terminating NUL.
-
int stdio_sprintf(char *s, const char *format, ...)#
An implementation of the C standard’s sprintf().
Warning
Does not bounds-check
s. Prefer stdio_snprintf().- Parameters:
s – Destination buffer. Must be large enough to hold the formatted output including the terminating NUL.
format – A format string.
... – Additional arguments, one for each %-specifier in
format.
- Returns:
The number of characters written into
s, not counting the terminating NUL.
-
int stdio_vsprintf(char *s, const char *format, va_list arg)#
Same as stdio_sprintf(), but accepts a va_list directly.
- Parameters:
s – Destination buffer.
format – A format string.
arg – A va_list of arguments corresponding to
format.
- Returns:
The number of characters written into
s, not counting the terminating NUL.
-
int stdio_snprintf(char *s, size_t n, const char *format, ...)#
An implementation of the C standard’s snprintf().
- Parameters:
s – Destination buffer, or NULL to count characters only.
n – Maximum number of characters to write, including the terminating NUL.
format – A format string.
... – Additional arguments, one for each %-specifier in
format.
- Returns:
The number of characters that COULD have been written (excluding the terminating NUL). A value >=
nindicates truncation.
-
int stdio_vsnprintf(char *s, size_t count, const char *format, va_list arg)#
Same as stdio_snprintf(), but accepts a va_list directly.
- Parameters:
s – Destination buffer, or NULL to count characters only.
count – Maximum number of characters to write, including terminating NUL.
format – A format string.
arg – A va_list of arguments corresponding to
format.
- Returns:
The number of characters that COULD have been written. A value >=
countindicates truncation.
-
int stdio_usfprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, ...)#
printf() with a caller-supplied output function (USF = User Specified Function).
An alternative to stdio_printf() in which the output function is specified dynamically rather than using stdio_putchar().
- Parameters:
out – Output function called once per character. Receives the character and
extra_arg.extra_arg – Opaque argument forwarded to
outon every call.format – A format string.
... – Additional arguments, one for each %-specifier in
format.
- Returns:
The number of characters for which
outwas invoked, not counting the terminating NUL.
-
int stdio_vusfprintf(void (*out)(char c, void *extra_arg), void *extra_arg, const char *format, va_list arg)#
Same as stdio_usfprintf(), but accepts a va_list directly.
- Parameters:
out – Output function called once per character.
extra_arg – Opaque argument forwarded to
outon every call.format – A format string.
arg – A va_list of arguments corresponding to
format.
- Returns:
The number of characters for which
outwas invoked, not counting the terminating NUL.
rcsw/stdio/stdio.h#
A small stdio library.
Mostly routines that support printf(). Mainly for bare-metal environments with no OS or stdlib, e.g. bootstraps.
Defines
-
RCSW_STDIO_ITOAD_BUFSIZE#
Buffer size sufficient for any stdio_itoad() result, including the terminating NUL (sign + 10 digits + NUL).
-
RCSW_STDIO_ITOAX_BUFSIZE#
Buffer size sufficient for any stdio_itoax() result, including the “0x” prefix and the terminating NUL.
Functions
-
size_t stdio_puts(const char *s)#
Write a string to stdout.
Unlike libc’s
puts(), no newline is appended.- Parameters:
s – The string to write.
- Returns:
The number of bytes written.
-
int stdio_putchar(int c)#
Write a character to stdout.
Calls RCSW_CONFIG_STDIO_PUTCHAR.
- Parameters:
c – The char to write.
- Returns:
What RCSW_CONFIG_STDIO_PUTCHAR returns:
c, orEOFon error for libc’sputchar.
-
int stdio_getchar(void)#
Get a character from stdin.
Calls RCSW_CONFIG_STDIO_GETCHAR.
- Returns:
The character received.
-
int stdio_atoi(const char *s, int base)#
Convert a string to an integer.
Convert a string representing an integer in the specified base (2-16). Leading spaces (not other whitespace) are skipped, then an optional ‘+’ or ‘-’. For base 16 an optional 0x/0X prefix is skipped. Conversion stops at the first character that is not a digit in
base.- Parameters:
s – The string to convert
base – The base the string is in (10, 16, etc.)
- Returns:
The converted result. Values outside the range of int wrap.
-
char *stdio_itoad(int32_t n, char *s, size_t len)#
Convert a 32-bit integer into a decimal string.
Negative numbers are prefixed with ‘-’, positive numbers with ‘+’, and 0 has no sign.
- Parameters:
n – The number to convert.
s – The buffer to fill.
len – Size of
sin bytes. RCSW_STDIO_ITOAD_BUFSIZE is always sufficient.
- Returns:
s, or NULL ifsis NULL orlenis too small for the result (errno = ENOSPC);sis not modified in that case.
-
char *stdio_itoax(uint32_t i, char *s, size_t len, bool add_0x)#
Convert a 32-bit unsigned integer into a lowercase hexadecimal string, without leading zeros.
- Parameters:
i – The number to convert.
s – The buffer to fill.
len – Size of
sin bytes. RCSW_STDIO_ITOAX_BUFSIZE is always sufficient.add_0x – Should “0x” be added to the front of the string?
- Returns:
s, or NULL ifsis NULL orlenis too small for the result (errno = ENOSPC);sis not modified in that case.
rcsw/stdio/string.h#
Implementation of stdio string routines.
Character classification
ASCII-only, locale-independent tests on a single character. Each returns 1 or 0. Values outside 0-127 (including negative char values) are never in any class.
-
static inline int stdio_isprintable(int c)#
Printable character (space through tilde).
-
static inline int stdio_isspace(int c)#
A space. Tabs and newlines do not count.
-
static inline int stdio_islower(int c)#
Lowercase letter.
-
static inline int stdio_isupper(int c)#
Uppercase letter.
-
static inline int stdio_isdigit(int c)#
Decimal digit.
-
static inline int stdio_ishex(int c)#
Hexadecimal digit, either case.
-
static inline int stdio_isalpha(int c)#
Letter, either case.
Functions
-
void *stdio_memcpy(void *dest, const void *src, size_t n)#
Copy memory.
This routine copies n bytes from the the memory pointed to by src to the memory pointed to by dest. The memory areas must NOT overlap.
- Parameters:
dest – Destination of copy.
src – Source of copy.
n – # of bytes to copy.
- Returns:
Memory pointed to by dest.
-
void *stdio_memset(void *dest, int c, size_t n)#
Set memory.
Fills the first
nbytes of the memory area pointed to bydestwith the constant bytec.- Parameters:
dest – Memory to fill.
c – The byte to use.
n – # of bytes to fill.
- Returns:
Memory pointed to by dest.
-
int stdio_toupper(int c)#
Convert a char to upper case.
- Parameters:
c – The character to convert.
- Returns:
The converted char.
-
int stdio_tolower(int c)#
Convert a char to lower case.
- Parameters:
c – The character to convert.
- Returns:
The converted char.
-
size_t stdio_strlen(const char *s)#
Get the length of a string.
- Parameters:
s – The string
- Returns:
The length of the string.
-
size_t stdio_strnlen(const char *s, size_t maxsize)#
Get the length of a string, limited by
maxsize.- Parameters:
s – The string
maxsize – The maximum string length to consider
- Returns:
The length of the string.
-
static inline void stdio_strrev(char *const s, size_t len)#
Reverse a string of known length in place.
O(
len); no strlen() is needed because the length is given. The terminating NUL (if any) is not moved. Equivalent to utils_arr8_reverse() on the string’s bytes.- Parameters:
s – The string to reverse.
len – Length of the string to reverse.
-
const char *stdio_strstr(const char *haystack, const char *needle)#
Search a string for another string.
This routine searches the haystack for the first occurrence of the needle. Both strings must be NUL-terminated; the needle’s terminating NUL is not part of the match. If needle occurs more than once, the first occurrence is returned.
- Parameters:
haystack – The string to search IN.
needle – The string to search for.
- Returns:
The first occurrence of the needle, or NULL if the substring was not found.
-
const char *stdio_strchr(const char *haystack, char needle)#
Search a string for a character.
This routine searches the haystack for the first occurrence of the needle. The character must be a byte; that is, multi-byte characters are not supported. The haystack must be null terminated. If more than one occurrence of needle is found in the haystack, a pointer to the first one will be returned.
- Parameters:
haystack – The string to search IN.
needle – The char to search for.
- Returns:
Pointer to the first occurrence of the needle, or NULL if the char was not found.
-
char *stdio_strncpy(char *dest, const char *src, size_t n)#
Copy one string over another.
This routine copies one string over top of another until the null byte is found or until n bytes have been written, whichever comes first. As per the standard library strncpy(), if strlen(haystack) < n, then the remaining bytes in dest are filled with zeroes.
- Parameters:
dest – Copy destination.
src – Copy source.
n – Max # of bytes to copy.
- Returns:
Pointer to dest.
-
char *stdio_strcpy(char *dest, const char *src)#
Copy one string over another.
This routine copies one string over top of another until the NULL byte.
The src/dest strings must NOT overlap. The null byte of src is also copied into dest. If either
srcordestis NULL, nothing is done.- Parameters:
dest – Copy destination.
src – Copy source.
- Returns:
Pointer to dest, unless
destis NULL, then NULL.
-
int stdio_strncmp(const char *s1, const char *s2, size_t len)#
Compare two strings for equality.
This routine compares two strings alphabetically, char by char, to a maximum of len chars.
- Parameters:
s1 – String #1.
s2 – String #2.
len – # of bytes to compare.
- Returns:
<,=,> 0 depending if s1 is found to be <,=,> s2
-
int stdio_strcmp(const char *s1, const char *s2)#
Compare two strings for equality.
This routine compares two strings alphabetically, char by char, until the NULL byte is encountered.
- Parameters:
s1 – String #1.
s2 – String #2.
- Returns:
<,=,> 0 depending if s1 is found to be <,=,> s2
-
char *stdio_strrep(const char *original, const char *pattern, const char *replacement, char *new_str)#
Replace a occurrences of one substring within another string with a new substring.
Nothing is allocated:
new_strmust be large enough to hold the string formed by replacing all occurrences ofpatternwithreplacement, plus the NUL; otherwise the behavior is undefined.- Parameters:
original – The string to replacement stuff in.
pattern – The substring to replace.
replacement – The substring to replace occurrences of pattern with.
new_str – Where to write the updated string. Must not overlap the other arguments.
- Returns:
new_str, or NULL if an argument is NULL.