LMMS
Loading...
Searching...
No Matches
pretty-format.c File Reference
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <rtosc/rtosc.h>
#include <rtosc/pretty-format.h>

Functions

static int asnprintf (char *str, size_t size, const char *format,...)
static int as_escaped_char (int c, int chr)
static void break_string (char **buffer, size_t bs, int wrt, int *cols_used)
size_t rtosc_print_arg_val (const rtosc_arg_val_t *arg, char *buffer, size_t bs, const rtosc_print_options *opt, int *cols_used)
size_t rtosc_print_arg_vals (const rtosc_arg_val_t *args, size_t n, char *buffer, size_t bs, const rtosc_print_options *opt, int cols_used)
size_t rtosc_print_message (const char *address, const rtosc_arg_val_t *args, size_t n, char *buffer, size_t bs, const rtosc_print_options *opt, int cols_used)
static void skip_while (const char **s, int(*property)(int))
static int skip_fmt (const char **src, const char *fmt)
static int skip_fmt_null (const char **src, const char *fmt)
static const char * try_fmt (const char *src, int exp, const char *fmt, char *typesrc, char type)
static const char * scanf_fmtstr (const char *src, char *type)
static const char * scanf_fmtstr_scan (const char *src, char *bytes8, char *type)
static size_t skip_numeric (const char **src, char *type)
static char get_escaped_char (char c, int chr)
static const char * end_of_printed_string (const char *src)
static const char * skip_word (const char *exp, const char **str)
static const char * skip_identifier (const char *str)
const char * rtosc_skip_next_printed_arg (const char *src)
int rtosc_count_printed_arg_vals (const char *src)
int rtosc_count_printed_arg_vals_of_msg (const char *msg)
const char * parse_identifier (const char *src, rtosc_arg_val_t *arg, char *buffer_for_strings, size_t *bufsize)
 Tries to parse an identifier at src and stores it in arg.
size_t rtosc_scan_arg_val (const char *src, rtosc_arg_val_t *arg, char *buffer_for_strings, size_t *bufsize)
size_t rtosc_scan_arg_vals (const char *src, rtosc_arg_val_t *args, size_t n, char *buffer_for_strings, size_t bufsize)
size_t rtosc_scan_message (const char *src, char *address, size_t adrsize, rtosc_arg_val_t *args, size_t n, char *buffer_for_strings, size_t bufsize)

Variables

static const rtosc_print_options default_print_options = ((rtosc_print_options) { true, 2, " ", 80})

Function Documentation

◆ as_escaped_char()

int as_escaped_char ( int c,
int chr )
static

Return the char that represents the escape sequence

Parameters
cThe escape sequence, e.g. '
'
chrTrue if the character appears in a single character (vs in a string)
Returns
The character describing the escape sequence, e.g. 'n'; -1 if none exists

◆ asnprintf()

int asnprintf ( char * str,
size_t size,
const char * format,
... )
static

Call snprintf and assert() that it did fit into the buffer

◆ break_string()

void break_string ( char ** buffer,
size_t bs,
int wrt,
int * cols_used )
static

◆ end_of_printed_string()

const char * end_of_printed_string ( const char * src)
static

Called inside a string at src, skips until after the closing quote

◆ get_escaped_char()

char get_escaped_char ( char c,
int chr )
static

Return the escape sequence that's generated with the given character

Parameters
cThe character, e.g. 'n'
chrTrue if the character appears in a single character (vs in a string)
Returns
The escape sequence generated by the character, e.g. '
'; 0 if none exists

◆ parse_identifier()

const char * parse_identifier ( const char * src,
rtosc_arg_val_t * arg,
char * buffer_for_strings,
size_t * bufsize )

Tries to parse an identifier at src and stores it in arg.

◆ rtosc_count_printed_arg_vals()

int rtosc_count_printed_arg_vals ( const char * src)

Count arguments that would be scanned and do a complete syntax check

This function should be run before rtosc_scan_arg_vals() in order to know the number of argument values. Also, rtosc_scan_arg_vals() does no complete syntax check.

Parameters
srcThe string to scan from
Returns
The number of arguments that can be scanned (>=0), or if the nth arg (range 1...) can not be scanned, -n

◆ rtosc_count_printed_arg_vals_of_msg()

int rtosc_count_printed_arg_vals_of_msg ( const char * msg)

Count arguments of a message that would be scanned and do a complete syntax check

Parameters
msgThe message to scan from
Returns
-1 if the address could not be scanned, INT_MIN if the whole string is whitespace, otherwise
See also
rtosc_count_printed_arg_vals

◆ rtosc_print_arg_val()

size_t rtosc_print_arg_val ( const rtosc_arg_val_t * arg,
char * buffer,
size_t buffersize,
const rtosc_print_options * opt,
int * cols_used )

Pretty-print rtosct_arg_val_t structure into buffer

Parameters
argPointer to the structure that shall be printed
bufferThe buffer to write to
buffersizeThe maximum size to write to, includin a trailing 0 byte
optPrinter options, NULL for default options
cols_usedHow many columns have been used for writing in this line (will be updated by this function)
Returns
The number of bytes written, excluding the null byte

◆ rtosc_print_arg_vals()

size_t rtosc_print_arg_vals ( const rtosc_arg_val_t * args,
size_t n,
char * buffer,
size_t bs,
const rtosc_print_options * opt,
int cols_used )

Pretty-print rtosct_arg_val_t array into buffer

See also
rtosc_print_message
Warning
in case of possible line breaks (almost always), buffer[-1] must be accessible and be whitespace (since it can be converted to a newline)

◆ rtosc_print_message()

size_t rtosc_print_message ( const char * address,
const rtosc_arg_val_t * args,
size_t n,
char * buffer,
size_t bs,
const rtosc_print_options * opt,
int cols_used )

Pretty-print OSC message into string buffer

A newline will be appended.

Parameters
addressOSC pattern to send message to
argsThe array to print
nNumber of args from the array that should be printed
bufferThe buffer to write to
bsThe maximum size to write to, includin a trailing 0 byte
optPrinter options, NULL for default options
cols_usedHow many columns have been used for writing in this line
Returns
The number of bytes written, excluding the null byte

◆ rtosc_scan_arg_val()

size_t rtosc_scan_arg_val ( const char * src,
rtosc_arg_val_t * arg,
char * buffer_for_strings,
size_t * bufsize )

Scan one argument value from a string

This function does no complete syntaxcheck. Call rtosc_count_printed_arg_vals() before.

Parameters
srcThe string
argPointer to where to store the argument value; must be allocated
buffer_for_stringsA buffer with enough space for scanned strings and blobs
bufsizeSize of buffer_for_strings , will be shrinked to the bufferbytes left after the scan
Returns
The number of bytes scanned

◆ rtosc_scan_arg_vals()

size_t rtosc_scan_arg_vals ( const char * src,
rtosc_arg_val_t * args,
size_t n,
char * buffer_for_strings,
size_t bufsize )

Scan a fixed number of argument values from a string

This function does no complete syntaxcheck. Call rtosc_count_printed_arg_vals() before. This will also give you the n parameter.

See also
rtosc_scan_message

◆ rtosc_scan_message()

size_t rtosc_scan_message ( const char * src,
char * address,
size_t adrsize,
rtosc_arg_val_t * args,
size_t n,
char * buffer_for_strings,
size_t bufsize )

Scan an OSC message from a string

This function does no complete syntaxcheck. Call rtosc_count_printed_arg_vals() before. This will also give you the n parameter. Preceding and trailing whitespace will be consumed.

Parameters
srcThe string
addressA buffer where the port address will be written
adrsizeSize of buffer address
argsPointer to an array of argument values; the output will be written here
nThe amount of argument values to scan
buffer_for_stringsA buffer with enough space for scanned strings and blobs
bufsizeSize of buffer_for_strings
Returns
The number of bytes scanned

◆ rtosc_skip_next_printed_arg()

const char * rtosc_skip_next_printed_arg ( const char * src)

Skip characters from a string until one argument value would have been scanned

Parameters
srcThe string
Returns
The first character after that argument value

◆ scanf_fmtstr()

const char * scanf_fmtstr ( const char * src,
char * type )
static

Return the right format string for skipping the next numeric value

This can be used to find out how to sscanf() ints, floats and doubles. If the string needs to be read, too, see scanf_fmtstr_scan() below.

Parameters
srcThe beginning of the numeric value to scan
typeIf non-NULL, the corresponding rtosc argument character (h,i,f,d) will be written here
Returns
The format string to use or NULL

◆ scanf_fmtstr_scan()

const char * scanf_fmtstr_scan ( const char * src,
char * bytes8,
char * type )
static

Return the right format string for reading the next numeric value

◆ skip_fmt()

int skip_fmt ( const char ** src,
const char * fmt )
static

Parse the string pointed to by src conforming to the format string @param src Pointer to the input string

Parameters
fmtFormat string for sscanf(). Must suppress all assignments, except the last one, which must be "%n" and be at the string's end.
Returns
The number of bytes skipped from the string pointed to by src

◆ skip_fmt_null()

int skip_fmt_null ( const char ** src,
const char * fmt )
static

Behave like skip_fmt() , but set *src to NULL if the string didn't match

See also
skip_fmt

◆ skip_identifier()

const char * skip_identifier ( const char * str)
static

Tries to skip the next identifier beginning at str

Returns
The position after the identifier, or NULL if there's no identifier at str .

◆ skip_numeric()

size_t skip_numeric ( const char ** src,
char * type )
static

Skip the next numeric at src

◆ skip_while()

void skip_while ( const char ** s,
int(* property )(int) )
static

◆ skip_word()

const char * skip_word ( const char * exp,
const char ** str )
static

Skips string exp at the current string pointed to by str, but only if it's a separated word, i.e. if there's a char after the string, it mus be a slash or whitespace.

Returns
The position after the word, or NULL if the word was not present at str .

◆ try_fmt()

const char * try_fmt ( const char * src,
int exp,
const char * fmt,
char * typesrc,
char type )
static

Helper function for scanf_fmtstr()

Variable Documentation

◆ default_print_options

const rtosc_print_options default_print_options = ((rtosc_print_options) { true, 2, " ", 80})
static