sys/fmt: Added submodule fmt_table

print_col_u32_dec() / print_col_s32_dec() can be used to print an
uint32_t / int32_t as a column of the given width
This commit is contained in:
Marian Buschsieweke 2019-06-13 14:52:59 +02:00
parent 3e61cc5b5e
commit 26d73116f6
No known key found for this signature in database
GPG Key ID: 61F64C6599B1539F
5 changed files with 141 additions and 0 deletions

View File

@ -12,6 +12,7 @@ PSEUDOMODULES += cpu_check_address
PSEUDOMODULES += ecc_%
PSEUDOMODULES += emb6_router
PSEUDOMODULES += event_%
PSEUDOMODULES += fmt_%
PSEUDOMODULES += gnrc_ipv6_default
PSEUDOMODULES += gnrc_ipv6_router
PSEUDOMODULES += gnrc_ipv6_router_default

View File

@ -2,6 +2,10 @@ ifneq (,$(filter eepreg,$(USEMODULE)))
FEATURES_REQUIRED += periph_eeprom
endif
ifneq (,$(filter fmt_table,$(USEMODULE)))
USEMODULE += fmt
endif
ifneq (,$(filter i2c_scan,$(USEMODULE)))
FEATURES_REQUIRED += periph_i2c
endif

View File

@ -1 +1,7 @@
# exclude submodule sources from *.c wildcard source selection
SRC := $(filter-out table.c,$(wildcard *.c))
# enable submodules
SUBMODULES := 1
include $(RIOTBASE)/Makefile.base

74
sys/fmt/table.c Normal file
View File

@ -0,0 +1,74 @@
/*
* Copyright 2019 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup sys_fmt_table
* @{
*
* @file
* @brief Implementation of the table extensions of the string formatting
* library
*
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*
* @}
*/
#include <assert.h>
#include <stdarg.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include "fmt.h"
static const char fmt_table_spaces[16] = " ";
/**
* @brief Prints @p fill_size bytes of the given pattern, repeating the
* pattern if needed
* @param pat Pattern to print
* @param pat_size Size of the pattern in bytes
* @param fill_size Number of bytes to print (if bigger than @p pat_size, the
* pattern will be repeated)
*
* E.g. `print_pattern("ab", 2, 5);` will print `ababa` to the console.
* This can be used to fill table columns with spaces, draw lines, etc.
*/
static void print_pattern(const char *pat, size_t pat_size, size_t fill_size)
{
while (fill_size > pat_size) {
print(pat, pat_size);
}
print(pat, fill_size);
}
void print_col_u32_dec(uint32_t number, size_t width)
{
char sbuf[10]; /* "4294967295" */
size_t slen;
slen = fmt_u32_dec(sbuf, number);
if (width > slen) {
print_pattern(fmt_table_spaces, sizeof(fmt_table_spaces), width - slen);
}
print(sbuf, slen);
}
void print_col_s32_dec(int32_t number, size_t width)
{
char sbuf[11]; /* "-2147483648" */
size_t slen;
slen = fmt_s32_dec(sbuf, number);
if (width > slen) {
print_pattern(fmt_table_spaces, sizeof(fmt_table_spaces), width - slen);
}
print(sbuf, slen);
}

56
sys/include/fmt_table.h Normal file
View File

@ -0,0 +1,56 @@
/*
* Copyright 2019 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup sys_fmt_table Table extension of the string formatting API (fmt_table)
* @ingroup sys_fmt
* @brief Provides utilities to print tables.
*
* \note The print functions in this library do not buffer any output.
* Mixing calls to standard @c printf from stdio.h with the @c print_xxx
* functions in fmt, especially on the same output line, may cause garbled
* output.
*
* @{
*
* @file
* @brief Table extension of the string formatting API
*
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*/
#ifndef FMT_TABLE_H
#define FMT_TABLE_H
#include <stdint.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Print a table column with the given number as decimal
* @param number Number to print in the column
* @param width Width of the column
*/
void print_col_u32_dec(uint32_t number, size_t width);
/**
* @brief Print a table column with the given number as decimal
* @param number Number to print in the column
* @param width Width of the column
*/
void print_col_s32_dec(int32_t number, size_t width);
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* FMT_TABLE_H */