Mercurial > hg > th-libs
view th_util.c @ 161:a765a51cbd5c
Make th_print_wrap() function public and move the code to th_util.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 09 Feb 2015 18:41:00 +0200 |
parents | 79e904905dbf |
children | 578d9298cc1e |
line wrap: on
line source
/* * Generic utility-functions, macros and defaults * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2002-2015 Tecnic Software productions (TNSP) * * Please read file 'COPYING' for information on license and distribution. */ #include "th_util.h" #include <stdio.h> #include <errno.h> /* Default settings */ static BOOL th_initialized = FALSE; int th_verbosityLevel = 2; char *th_prog_name = NULL, *th_prog_desc = NULL, *th_prog_version = NULL, *th_prog_author = NULL, *th_prog_license = NULL; /* Initialize th_util-library and global variables */ void th_init(char *name, char *desc, char *version, char *author, char *license) { th_prog_name = name; th_prog_desc = desc; th_prog_version = version; if (author) th_prog_author = author; else th_prog_author = TH_PROG_AUTHOR; if (license) th_prog_license = license; else th_prog_license = TH_PROG_LICENSE; th_initialized = TRUE; } void th_print_banner(FILE *outFile, const char *name, const char *usage) { fprintf(outFile, "\n%s v%s (%s)\n" "%s\n" "%s\n" "Usage: %s %s\n", th_prog_name, th_prog_version, th_prog_desc, th_prog_author, th_prog_license, name, usage); } int th_term_width() { char *var = getenv("COLUMNS"); int res = (var != NULL) ? atoi(var) : 80; if (res < 5) res = 80; return res; } int th_term_height() { char *var = getenv("LINESS"); int res = (var != NULL) ? atoi(var) : 25; if (res < 1) res = 1; return res; } /* Print formatted error, warning and information messages * TODO: Implement th_vfprintf() and friends? */ void THERR_V(const char *fmt, va_list ap) { assert(th_initialized == TRUE); fprintf(stderr, "%s: ", th_prog_name); vfprintf(stderr, fmt, ap); } void THMSG_V(int level, const char *fmt, va_list ap) { assert(th_initialized == TRUE); if (th_verbosityLevel >= level) { fprintf(stderr, "%s: ", th_prog_name); vfprintf(stderr, fmt, ap); } } void THPRINT_V(int level, const char *fmt, va_list ap) { assert(th_initialized == TRUE); if (th_verbosityLevel >= level) { vfprintf(stderr, fmt, ap); } } void THERR(const char *fmt, ...) { va_list ap; assert(th_initialized == TRUE); va_start(ap, fmt); THERR_V(fmt, ap); va_end(ap); } void THMSG(int level, const char *fmt, ...) { va_list ap; assert(th_initialized == TRUE); va_start(ap, fmt); THMSG_V(level, fmt, ap); va_end(ap); } void THPRINT(int level, const char *fmt, ...) { va_list ap; assert(th_initialized == TRUE); va_start(ap, fmt); THPRINT_V(level, fmt, ap); va_end(ap); } static void th_pad(FILE *outFile, int count) { while (count--) fputc(' ', outFile); } void th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width) { size_t pos = 0; BOOL first = TRUE; while (str[pos]) { // Pre-pad line int linelen = first ? spad : rpad; th_pad(fh, first ? 0 : rpad); first = FALSE; // Skip whitespace at line start while (th_isspace(str[pos]) || str[pos] == '\n') pos++; // Handle each word while (str[pos] && str[pos] != '\n') { size_t next; int wlen; // Find word length and next break for (wlen = 0, next = pos; str[next] && !th_isspace(str[next]) && str[next] != '\n'; next++, wlen++); // Check if we have too much of text? if (linelen + wlen >= width) break; // Print what we have for (;pos < next; pos++, linelen++) fputc(str[pos], fh); // Check if we are at end of input or hard linefeed if (str[next] == '\n' || str[next] == 0) break; else { fputc(str[pos], fh); pos++; linelen++; } } fprintf(fh, "\n"); } } /* Error handling */ int th_get_error() { return TH_SYSTEM_ERRORS + errno; } int th_errno_to_error(int error) { return TH_SYSTEM_ERRORS + error; } const char *th_error_str(int error) { if (error >= TH_SYSTEM_ERRORS) return strerror(error - TH_SYSTEM_ERRORS); switch (error) { case THERR_OK: return "No error"; case THERR_FOPEN: return "File open error"; case THERR_FREAD: return "Read error"; case THERR_FWRITE: return "Write error"; case THERR_FSEEK: return "Seek error"; case THERR_NOT_FOUND: return "Resource not found"; case THERR_INVALID_DATA: return "Invalid data"; case THERR_MALLOC: return "Memory allocation failure"; case THERR_ALREADY_INIT: return "Already initialized"; case THERR_INIT_FAIL: return "Initialization failed"; case THERR_INVALID_ARGS: return "Invalid arguments"; case THERR_NULLPTR: return "NULL pointer"; case THERR_NOT_SUPPORTED: return "Operation not supported"; case THERR_OUT_OF_DATA: return "Out of data"; case THERR_EXTRA_DATA: return "Extra data"; case THERR_BOUNDS: return "Bounds check failed"; case THERR_TIMED_OUT: return "Operation timed out"; case THERR_AUTH_FAILED: return "Authentication failed"; default: return "Unknown error"; } } /* Memory handling routines */ void *th_malloc(size_t l) { return malloc(l); } void *th_malloc0(size_t l) { return calloc(1, l); } void *th_calloc(size_t n, size_t l) { return calloc(n, l); } void *th_realloc(void *p, size_t l) { return realloc(p, l); } void th_free(void *p) { /* Check for NULL pointers for portability due to some libc * implementations not handling free(NULL) too well. */ if (p) free(p); } /* Doubly linked list handling * * In this implementation first node's prev points to last node of the list, * and last node's next is NULL. This way we can semi-efficiently traverse to * beginning and end of the list, assuming user does not do weird things. */ th_llist_t * th_llist_new(void *data) { th_llist_t *res = th_malloc0(sizeof(th_llist_t)); res->data = data; return res; } void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *)) { th_llist_t *curr = list; while (curr != NULL) { th_llist_t *next = curr->next; freefunc(curr); curr = next; } } void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data)) { th_llist_t *curr = list; while (curr != NULL) { th_llist_t *next = curr->next; if (curr->data != NULL) freefunc(curr->data); th_free(curr); curr = next; } } void th_llist_free(th_llist_t *list) { th_llist_t *curr = list; while (curr != NULL) { th_llist_t *next = curr->next; th_free(curr); curr = next; } } void th_llist_append_node(th_llist_t **list, th_llist_t *node) { if (*list != NULL) { node->prev = (*list)->prev; (*list)->prev->next = node; (*list)->prev = node; (*list)->num++; } else { *list = node; node->prev = node; (*list)->num = 1; } node->next = NULL; } th_llist_t *th_llist_append(th_llist_t **list, void *data) { th_llist_t *node = th_llist_new(data); th_llist_append_node(list, node); return node; } void th_llist_prepend_node(th_llist_t **list, th_llist_t *node) { if (*list != NULL) { node->prev = (*list)->prev; node->next = *list; (*list)->prev = node; node->num = (*list)->num + 1; *list = node; } else { *list = node->prev = node; node->next = NULL; (*list)->num = 1; } } th_llist_t *th_llist_prepend(th_llist_t **list, void *data) { th_llist_t *node = th_llist_new(data); th_llist_prepend_node(list, node); return node; } /* 1) Remove a middle node node0->prev->next = node->next (node1) node0->next->prev = node->prev (list) node2 <- list <=> node0 <=> node1 <=> node2 -> NULL node2 <- list <=> node1 <=> node2 -> NULL 2) Remove first node when many items node2 <- list <=> node0 <=> node1 <=> node2 -> NULL node2 <- node0 <=> node1 <=> node2 -> NULL *list = node0 3) Remove last node in list if (node->next == NULL) { list->prev = node->prev; node->prev->next = NULL; } node2 <- list <=> node0 <=> node1 <=> node2 -> NULL node1 <- list <=> node0 <=> node1 -> NULL 4) Remove last list <- list -> NULL */ void th_llist_delete_node_fast(th_llist_t **list, th_llist_t *node) { if (node == *list) { // First node in list th_llist_t *tmp = (*list)->next; if (tmp != NULL) { tmp->num = (*list)->num - 1; tmp->prev = (*list)->prev; } *list = tmp; } else { // Somewhere in middle or end if (node->prev != NULL) node->prev->next = node->next; if (node->next != NULL) node->next->prev = node->prev; else (*list)->prev = node; // Last node (*list)->num--; } node->next = node->prev = NULL; } void th_llist_delete_node(th_llist_t **list, th_llist_t *node) { th_llist_t *curr = *list; while (curr != NULL) { th_llist_t *next = curr->next; if (curr == node) { th_llist_delete_node_fast(list, curr); th_free(node); break; } curr = next; } } void th_llist_delete(th_llist_t **list, const void *data) { th_llist_t *curr = *list; while (curr != NULL) { th_llist_t *next = curr->next; if (curr->data == data) { th_llist_delete_node_fast(list, curr); th_free(curr); break; } curr = next; } } th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n) { th_llist_t *curr = list; size_t i; for (i = 0; curr != NULL && i < n; curr = curr->next, i++); return curr; } size_t th_llist_length(const th_llist_t *list) { if (list == NULL) return 0; else return list->num; } ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node) { const th_llist_t *curr = list; ssize_t i = 0; while (curr != NULL) { if (curr == node) return i; else i++; curr = curr->next; } return -1; } void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data) { th_llist_t *curr = list; while (curr != NULL) { func(curr, data); curr = curr->next; } } int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_t **ret) { th_llist_t *curr = list; while (curr != NULL) { int res = func(curr, data); if (res != 0) { *ret = curr; return res; } curr = curr->next; } return 0; } th_llist_t * th_llist_find(th_llist_t *list, const void *data) { th_llist_t *curr = list; while (curr != NULL) { if (curr->data == data) return curr; curr = curr->next; } return NULL; } th_llist_t * th_llist_find_func(th_llist_t *list, const void *userdata, int (compare)(const void *, const void *)) { th_llist_t *curr = list; while (curr != NULL) { if (compare(curr->data, userdata) == 0) return curr; curr = curr->next; } return NULL; } /* * Ringbuffers */ th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data)) { th_ringbuf_t *res = th_malloc0(sizeof(th_ringbuf_t)); res->data = (char **) th_calloc(size, sizeof(char *)); res->size = size; res->n = 0; res->deallocator = mdeallocator; return res; } BOOL th_ringbuf_grow(th_ringbuf_t *buf, const size_t n) { buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *)); if (buf->data != NULL) { memset(buf->data + buf->size, 0, sizeof(char *) * n); buf->size += n; return TRUE; } else return FALSE; } void th_ringbuf_free(th_ringbuf_t *buf) { int i; for (i = 0; i < buf->size; i++) { if (buf->data[i] != NULL) buf->deallocator(buf->data[i]); } th_free(buf->data); th_free(buf); } void th_ringbuf_add(th_ringbuf_t *buf, void *ptr) { if (buf->n < buf->size) buf->n++; th_free(buf->data[0]); memmove(&(buf->data[0]), &(buf->data[1]), (buf->size - 1) * sizeof(void *)); buf->data[buf->size - 1] = ptr; } /* * Simple legacy string growing buffer */ BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) { if (*buf == NULL) *bufsize = *len = 0; if (*buf == NULL || *len + grow >= *bufsize) { *bufsize += grow + TH_BUFGROW; *buf = th_realloc(*buf, *bufsize); if (*buf == NULL) return FALSE; } return TRUE; } BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch) { if (!th_strbuf_grow(buf, bufsize, len, 1)) return FALSE; (*buf)[*len] = ch; (*len)++; return TRUE; } BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str) { size_t slen; if (str == NULL) return FALSE; slen = strlen(str); if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) return FALSE; memcpy(*buf + *len, str, slen + 1); (*len) += slen; return TRUE; } /* * Growing buffer */ void th_growbuf_clear(th_growbuf_t *buf) { // Simply reset the current "length" buf->len = 0; } void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow) { // Initialize the buffer structure memset(buf, 0, sizeof(th_growbuf_t)); buf->mingrow = mingrow; } th_growbuf_t *th_growbuf_new(const size_t mingrow) { th_growbuf_t *buf; if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) return NULL; th_growbuf_init(buf, mingrow); buf->allocated = TRUE; return buf; } void th_growbuf_free(th_growbuf_t *buf) { th_free(buf->data); if (buf->allocated) th_free(buf); } BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow) { if (buf == NULL) return FALSE; if (buf->data == NULL || buf->len + grow >= buf->size) { buf->size += grow + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW); buf->data = (uint8_t *) th_realloc(buf->data, buf->size); if (buf->data == NULL) return FALSE; } return TRUE; } BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos) { size_t slen; if (str == NULL) return FALSE; slen = strlen(str); if (!th_growbuf_grow(buf, slen + 1)) return FALSE; memcpy(buf->data + buf->len, str, slen + 1); buf->len += eos ? (slen + 1) : slen; return TRUE; } BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch) { if (!th_growbuf_grow(buf, sizeof(char))) return FALSE; buf->data[buf->len++] = (uint8_t) ch; return TRUE; } BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len) { if (s == NULL) return FALSE; if (!th_growbuf_grow(buf, len + 1)) return FALSE; memcpy(buf->data + buf->len, s, len + 1); buf->len += len; return TRUE; } BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val) { if (!th_growbuf_grow(buf, sizeof(uint8_t))) return FALSE; buf->data[buf->len++] = val; return TRUE; } BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val) { if (!th_growbuf_grow(buf, sizeof(uint16_t))) return FALSE; buf->data[buf->len++] = (val >> 8) & 0xff; buf->data[buf->len++] = val & 0xff; return TRUE; } BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val) { if (!th_growbuf_grow(buf, sizeof(uint16_t))) return FALSE; buf->data[buf->len++] = val & 0xff; buf->data[buf->len++] = (val >> 8) & 0xff; return TRUE; } BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val) { if (!th_growbuf_grow(buf, sizeof(uint32_t))) return FALSE; buf->data[buf->len++] = (val >> 24) & 0xff; buf->data[buf->len++] = (val >> 16) & 0xff; buf->data[buf->len++] = (val >> 8) & 0xff; buf->data[buf->len++] = val & 0xff; return TRUE; } BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val) { if (!th_growbuf_grow(buf, sizeof(uint32_t))) return FALSE; buf->data[buf->len++] = val & 0xff; buf->data[buf->len++] = (val >> 8) & 0xff; buf->data[buf->len++] = (val >> 16) & 0xff; buf->data[buf->len++] = (val >> 24) & 0xff; return TRUE; }