Mercurial > hg > th-libs
view th_util.h @ 143:c878cdcfea9d
Implement terminal width.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 07 Dec 2014 20:49:10 +0200 |
parents | aa2d608fb3f3 |
children | fdea1c3acc53 |
line wrap: on
line source
/* * Generic utility-functions, macros and defaults * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2002-2014 Tecnic Software productions (TNSP) * * Please read file 'COPYING' for information on license and distribution. */ #ifndef TH_UTIL_H #define TH_UTIL_H #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "th_types.h" #include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #ifndef HAVE_NO_ASSERT #include <assert.h> #endif #ifdef HAVE_STRING_H #include <string.h> #else #ifdef HAVE_STRINGS_H #include <strings.h> #endif #endif #ifdef HAVE_MEMORY_H #include <memory.h> #endif #ifdef __cplusplus extern "C" { #endif #ifdef TH_NO_DEFAULTS #define TH_PROG_AUTHOR NULL #define TH_PROG_LICENSE NULL #else #define TH_PROG_AUTHOR "By Matti 'ccr' Hämäläinen (C) Copyright 2014 TNSP" #define TH_PROG_LICENSE "This software is licensed under GNU GPL version 2" #endif // Replacement for assert() #ifdef HAVE_NO_ASSERT # ifdef NDEBUG # define assert(NEXPR) // stub # else # define assert(NEXPR) do { if (!(NEXPR)) { fprintf(stderr, "[%s:%d] assert(" # NEXPR ") failed!\n", __FILE__, __LINE__); abort(); } } while (0) # endif #endif /* Error codes */ enum { // General error codes THERR_OK = 0, THERR_PROGRESS, // Status OK, but operation in progress THERR_INTERNAL, THERR_FOPEN, THERR_FREAD, THERR_FWRITE, THERR_FSEEK, THERR_NOT_FOUND, // Resource/data not found THERR_INVALID_DATA, // Some data was invalid THERR_MALLOC, // Memory allocation failure THERR_ALREADY_INIT, // Resource has already been initialized THERR_INIT_FAIL, // General initialization failure THERR_INVALID_ARGS, THERR_NULLPTR, // NULL pointer specified in critical argument THERR_NOT_SUPPORTED,// Operation not supported THERR_OUT_OF_DATA, THERR_EXTRA_DATA, THERR_BOUNDS, THERR_TIMED_OUT, // Network errors THERR_AUTH_FAILED, }; #define TH_SYSTEM_ERRORS 100000 /* Log levels */ enum { THLOG_NONE = 0, THLOG_ERROR, THLOG_WARNING, THLOG_INFO, THLOG_DEBUG, }; /* Global variables */ extern int th_verbosityLevel; extern char *th_prog_name, *th_prog_desc, *th_prog_version, *th_prog_author, *th_prog_license; /* Functions */ void th_init(char *name, char *desc, char *version, char *author, char *license); void th_print_banner(FILE *outFile, const char *binName, const char *usage); int th_term_width(); int th_term_height(); int th_get_error(); int th_errno_to_error(int error); const char *th_error_str(int error); void THERR(const char *, ...); void THMSG(int, const char *, ...); void THPRINT(int, const char *, ...); void THERR_V(const char *, va_list); void THMSG_V(int, const char *, va_list); void THPRINT_V(int, const char *, va_list); void *th_malloc(size_t); void *th_malloc0(size_t); void *th_calloc(size_t, size_t); void *th_realloc(void *, size_t); void th_free(void *); /* Doubly linked list handling */ typedef struct _qlist_t { void *data; size_t num; struct _qlist_t *prev, *next; } qlist_t; qlist_t * th_llist_new(void *data); void th_llist_free(qlist_t *list); void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data)); void th_llist_append_node(qlist_t **list, qlist_t *node); qlist_t * th_llist_append(qlist_t **list, void *data); void th_llist_prepend_node(qlist_t **list, qlist_t *node); qlist_t * th_llist_prepend(qlist_t **list, void *data); void th_llist_delete(qlist_t **list, const void *data); void th_llist_delete_node(qlist_t **list, qlist_t *node); void th_llist_delete_node_fast(qlist_t **list, qlist_t *node); qlist_t * th_llist_get_nth(qlist_t *list, const size_t n); size_t th_llist_length(const qlist_t *list); ssize_t th_llist_position(const qlist_t *list, const qlist_t *node); void th_llist_foreach(qlist_t *list, void (*func)(qlist_t *node, void *userdata), void *data); int th_llist_foreach_cond(qlist_t *list, int (*func)(qlist_t *node, void *userdata), void *data, qlist_t **res); qlist_t * th_llist_find(qlist_t *list, const void *data); qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *)); /* Ringbuffer implementation */ typedef struct { char **data; int n, size; void (*deallocator)(void *); } qringbuf_t; qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *)); BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n); void th_ringbuf_free(qringbuf_t *buf); void th_ringbuf_add(qringbuf_t *buf, void *ptr); /* Growing buffers */ #define TH_BUFGROW (32) typedef struct { BOOL allocated; uint8_t *data; size_t size, len, mingrow; } th_growbuf_t; /* Simple growing string buffer */ BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, const size_t grow); BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch); BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str); /* Growing byte buffer */ void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow); void th_growbuf_clear(th_growbuf_t *buf); th_growbuf_t *th_growbuf_new(const size_t mingrow); void th_growbuf_free(th_growbuf_t *buf); BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow); BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos); BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch); BOOL th_growbuf_put_str(th_growbuf_t *buf, const void *s, const size_t len); BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val); BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val); BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val); BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val); BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val); #ifdef __cplusplus } #endif #endif // TH_UTIL_H