scdoc2mdoc

A fork of scdoc to output mdoc(7)
git clone git://git.sgregoratto.me/scdoc2mdoc
Log | Files | Refs | README | LICENSE

string.c (958B)


      1 #include "config.h"
      2 #include <ctype.h>
      3 #if HAVE_ERR
      4 #include <err.h>
      5 #endif
      6 #include <stdio.h>
      7 #include <stdint.h>
      8 #include <stdlib.h>
      9 #include "string.h"
     10 #include "utf8.h"
     11 
     12 str_t *
     13 str_create()
     14 {
     15 	str_t *str;
     16 	if ((str = calloc(sizeof(str_t), 1)) == NULL)
     17 		err(EXIT_FAILURE, NULL);
     18 	if ((str->str = calloc(16, 1)) == NULL)
     19 		err(EXIT_FAILURE, NULL);
     20 
     21 	str->size = 16;
     22 	str->len = 0;
     23 	return str;
     24 }
     25 
     26 void
     27 str_free(str_t *str)
     28 {
     29 	if (!str)
     30 		return;
     31 	free(str->str);
     32 	free(str);
     33 }
     34 
     35 void
     36 str_append_ch(str_t *str, uint32_t ch)
     37 {
     38 	size_t chsize = utf8_chsize(ch);
     39 	if (str->len + chsize + 1 >= str->size) {
     40 		size_t newsize = str->size * 2;
     41 		if ((str->str = reallocarray(str->str, newsize, 1)) == NULL)
     42 			err(EXIT_FAILURE, NULL);
     43 		str->size = newsize;
     44 	}
     45 
     46 	utf8_encode(&str->str[str->len], ch);
     47 	str->len += chsize;
     48 	str->str[str->len] = '\0';
     49 }
     50 
     51 void
     52 str_toupper(str_t *str)
     53 {
     54 	for (size_t i = 0; i < str->len; i++)
     55 		str->str[i] = toupper(str->str[i]);
     56 }