10.5. std — Standard functions

Source code: src/text/std.h, src/text/std.c

Test code: tst/text/std/main.c

Test coverage: src/text/std.c


Functions

int std_module_init(void)

Initialize the std module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

ssize_t std_sprintf(char *dst_p, far_string_t fmt_p, ...)

Format and write data to destination buffer. The buffer must be big enough to fit the formatted string. The output is null terminated.

A format specifier has this format:

%[flags][width][length]specifier

where

  • flags: 0 or -
  • width: 0..127
  • length: l for long or nothing
  • specifier: c, s, S, d, i, u, x or f

The S specifier expects a far string (far_string_t) argument. Other specifiers have their usual definition.

Return
Length of the string written to the destination buffer, not including the null termination, or negative error code.
Parameters
  • dst_p: Destination buffer. The formatted string is written to this buffer.
  • fmt_p: Format string.
  • ...: Variable arguments list.

ssize_t std_snprintf(char *dst_p, size_t size, far_string_t fmt_p, ...)

Format and write data to given buffer. The output is null terminated.

Return
Length of the string written to the destination buffer, not including the null termination, -ENOMEM if the string does not fit in the destination buffer, or other negative error code.
Parameters
  • dst_p: Destination buffer. The formatted string is written to this buffer.
  • size: Size of the destination buffer.
  • fmt_p: Format string.
  • ...: Variable arguments list.

ssize_t std_vsprintf(char *dst_p, far_string_t fmt_p, va_list *ap_p)

Format and write data to given buffer. The output is null terminated.

Return
Length of the string written to the destination buffer, not including the null termination, or negative error code.
Parameters
  • dst_p: Destination buffer. The formatted string is written to this buffer.
  • fmt_p: Format string.
  • ap_p: Variable arguments list.

ssize_t std_vsnprintf(char *dst_p, size_t size, far_string_t fmt_p, va_list *ap_p)

Format and write data to given buffer. The output is null terminated.

Return
Length of the string written to the destination buffer, not including the null termination, -ENOMEM if the string does not fit in the destination buffer, or other negative error code.
Parameters
  • dst_p: Destination buffer. The formatted string is written to this buffer.
  • size: Size of the destination buffer.
  • fmt_p: Format string.
  • ap_p: Variable arguments list.

ssize_t std_printf(far_string_t fmt_p, ...)

Format and print data to standard output. The output is not null terminated.

See std_sprintf() for the the format string specification.

Return
Number of characters written to standard output, or negative error code.
Parameters
  • fmt_p: Format string.
  • ...: Variable arguments list.

ssize_t std_vprintf(far_string_t fmt_p, va_list *ap_p)

Format and print data to standard output. The output is not null terminated.

See std_sprintf() for the the format string specification.

Return
Number of characters written to standard output, or negative error code.
Parameters
  • fmt_p: Format string.
  • ap_p: Variable arguments list.

ssize_t std_fprintf(void *chan_p, far_string_t fmt_p, ...)

Format and print data to given channel. The output is not null terminated.

See std_sprintf() for the the format string specification.

Return
Number of characters written to given channel, or negative error code.
Parameters
  • chan_p: Output channel.
  • fmt_p: Format string.
  • ...: Variable arguments list.

ssize_t std_vfprintf(void *chan_p, far_string_t fmt_p, va_list *ap_p)

Format and print data to given channel. The output is not null terminated.

See std_sprintf() for the the format string specification.

Return
Number of characters written to given channel, or negative error code.
Parameters
  • chan_p: Output channel.
  • fmt_p: Format string.
  • ...: Variable arguments list.

ssize_t std_printf_isr(far_string_t fmt_p, ...)

Format and print data to standard output from interrupt context or with the system lock taken. The output is not null terminated.

See std_sprintf() for the the format string specification.

Return
Number of characters written to standard output, or negative error code.
Parameters
  • fmt_p: Format string.
  • ...: Variable arguments list.

ssize_t std_fprintf_isr(void *chan_p, far_string_t fmt_p, ...)

Format and print data to given channel from interrupt context or with the system lock taken. The output is not null terminated.

See std_sprintf() for the the format string specification.

Return
Number of characters written to given channel, or negative error code.
Parameters
  • chan_p: Output channel.
  • fmt_p: Format string.
  • ...: Variable arguments list.

const char *std_strtolb(const char *str_p, long *value_p, int base)

Convert given string to an integer in given base.

Return
Pointer to the next byte, or NULL if no value was found.
Parameters
  • str_p: Integer string.
  • value_p: Parsed integer.
  • base: The base of the value to parse. One of 16, 10, 8 or 2, or 0 to select base based on the string prefix. Supported string prefixes are “0x” for hexadecimal numbers, “0” for octal numbers and “0b” for binary numbers.

const char *std_strtol(const char *str_p, long *value_p)

Convert given string to an integer with base selection based on the string prefix.

Return
Pointer to the next byte, or NULL if no value was found.
Parameters
  • str_p: Integer string.
  • value_p: Parsed integer.

const char *std_strtod(const char *str_p, double *value_p)

Convert given string to a double.

Return
Pointer to the next byte, or NULL if no value vas found.
Parameters
  • str_p: Double string.
  • value_p: Parsed value.

const char *std_strtodfp(const char *str_p, long *value_p, int precision)

Convert string to decimal fixed point number with given precision.

Return
Pointer to the next byte, or NULL on failure.
Parameters
  • str_p: Double string.
  • value_p: Decimal fixed point number of given precision.
  • precision: Number precision, or decimal places.

int std_strcpy(char *dst_p, far_string_t src_p)

Copy string from far memory to memory.

Return
String length or negative error code.
Parameters
  • dst_p: Normal memory string.
  • src_p: Far memory string.

int std_strcmp(const char *str_p, far_string_t fstr_p)

Compare a string with a far string.

Return
zero(0) if match, otherwise the difference of the mismatched characters
Parameters
  • str_p: Normal memory string.
  • fstr_p: Far memory string.

int std_strcmp_f(far_string_t fstr0_p, far_string_t fstr1_p)

Compare two far strings.

Return
zero(0) if match, otherwise the difference of the mismatched characters.
Parameters
  • fstr0_p: Far memory string.
  • fstr1_p: Far memory string.

int std_strncmp(far_string_t fstr_p, const char *str_p, size_t size)

Compare at most size bytes of one far string and one string.

Return
zero(0) if match, otherwise the difference of the mismatched characters.
Parameters
  • fstr_p: Far memory string.
  • str_p: String.
  • size: Compare at most size number of bytes.

int std_strncmp_f(far_string_t fstr0_p, far_string_t fstr1_p, size_t size)

Compare at most size bytes of two far strings.

Return
zero(0) if match, otherwise the difference of the mismatched characters.
Parameters
  • fstr0_p: Far memory string.
  • fstr1_p: Far memory string.
  • size: Compare at most size number of bytes.

int std_strlen(far_string_t fstr_p)

Get the length in bytes of given far string, not including null termination.

Return
String length in number of bytes (not including the null termination).
Parameters
  • fstr_p: Far memory string.

char *std_strip(char *str_p, const char *strip_p)

Strip leading and trailing characters from a string. The characters to strip are given by strip_p.

Return
Pointer to the stripped string.
Parameters
  • str_p: String to strip characters from.
  • strip_p: Characters to strip or NULL for whitespace characters. Must be null-terminated.

ssize_t std_hexdump(void *chan_p, const void *buf_p, size_t size)

Write a hex dump of given data to given channel.

Return
Number of characters written to given channel, or negative error code.
Parameters
  • chan_p: Channel to write the hexdump to.
  • buf_p: Buffer to dump.
  • size: Size of buffer.