libtabular-c

C tabular formatting library
git clone https://git.sinitax.com/sinitax/libtabular-c
Log | Files | Refs | Submodules | sfeed.txt

util.c (2506B)


      1#include "util.h"
      2
      3#include <stddef.h>
      4#include <stdio.h>
      5#include <stdint.h>
      6#include <stdlib.h>
      7
      8size_t
      9u8strlen(const char *str)
     10{
     11	size_t i, len;
     12
     13	if (!str) return 0;
     14
     15	len = 0;
     16	for (i = 0; str[i]; i++) {
     17		if ((str[i] & 0xC0) != 0x80)
     18			len += 1;
     19	}
     20
     21	return len;
     22}
     23
     24size_t
     25u8strnlen(const char *str, size_t max)
     26{
     27	size_t i, len;
     28
     29	if (!str) return 0;
     30
     31	len = 0;
     32	for (i = 0; str[i] && i < max; i++) {
     33		if ((str[i] & 0xC0) != 0x80)
     34			len += 1;
     35	}
     36
     37	return len;
     38}
     39
     40size_t
     41u8rawlen(const char *str, size_t max)
     42{
     43	size_t i, len;
     44
     45	if (!str) return 0;
     46
     47	len = 0;
     48	for (i = 0; str[i] && len < max; i++)
     49		if ((str[i] & 0xC0) != 0x80) {
     50			len += 1;
     51	}
     52
     53	return i;
     54}
     55
     56size_t
     57print_pad(FILE *file, size_t len)
     58{
     59	fprintf(file, "%*.s", (int) len, "");
     60
     61	return len;
     62}
     63
     64size_t
     65print_hex(FILE *file, const void *bytes, size_t len)
     66{
     67	size_t i;
     68
     69	for (i = 0; i < len; i++)
     70		fprintf(file, "%02X", *(uint8_t *)(bytes + i));
     71
     72	return len * 2;
     73}
     74
     75size_t
     76print_trunc(FILE *file, const char *str, size_t width)
     77{
     78	size_t len;
     79
     80	len = u8strlen(str);
     81	if (len > width && width >= 2) {
     82		fprintf(file, "%*.*s..", (int) width - 2, (int) width - 2, str);
     83	} else if (len > width&& width < 2) {
     84		fprintf(file, "%*.*s", (int) width, (int) width, "..");
     85	} else {
     86		fprintf(file, "%s", str);
     87	}
     88
     89	return MIN(len, width);
     90}
     91
     92size_t
     93print_left(FILE *file, const char *str, size_t width, size_t padwidth)
     94{
     95	size_t rawlen, u8len;
     96	ssize_t ret;
     97
     98	u8len = MIN(u8strlen(str), width);
     99	rawlen = u8rawlen(str, u8len);
    100
    101	/* make up for invisible bytes */
    102	padwidth += rawlen - u8len;
    103
    104	ret = fprintf(file, "%-*.*s", (int) padwidth, (int) rawlen, str);
    105
    106	return (size_t) MAX(0, ret);
    107}
    108
    109size_t
    110print_center(FILE *file, const char *str, size_t width, size_t padwidth)
    111{
    112	size_t u8len, rawlen;
    113	size_t lpad, rpad;
    114	ssize_t ret;
    115
    116	u8len = MIN(width, u8strlen(str));
    117	rawlen = u8rawlen(str, u8len);
    118	lpad = MAX(padwidth - u8len, 0) / 2;
    119	rpad = MAX(padwidth - u8len - lpad, 0);
    120
    121	/* make up for invisible bytes */
    122	rpad += rawlen - u8len;
    123
    124	ret = fprintf(file, "%*s%*.*s%*s", (int) lpad, "",
    125		(int) rawlen, (int) rawlen, str, (int) rpad, "");
    126
    127	return (size_t) MAX(0, ret);
    128}
    129
    130size_t
    131print_right(FILE *file, const char *str, size_t width, size_t padwidth)
    132{
    133	size_t rawlen, u8len;
    134	ssize_t ret;
    135
    136	u8len = MIN(width, u8strlen(str));
    137	rawlen = u8rawlen(str, u8len);
    138
    139	/* make up for invisible bytes */
    140	padwidth += rawlen - u8len;
    141
    142	ret = fprintf(file, "%*.*s", (int) padwidth, (int) rawlen, str);
    143
    144	return (size_t) MAX(0, ret);
    145}
    146