Mercurial > hg > th-libs
view th_util.h @ 184:b256db93cf25
Initial import of th_file module.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 20 Jan 2016 16:40:05 +0200 |
parents | 8baec5a454c1 |
children | 7d25d43a3ce2 |
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. */ #ifndef TH_UTIL_H #define TH_UTIL_H #ifdef HAVE_CONFIG_H #include "config.h" #endif #if defined(__WIN64) || defined(_WIN64) || defined(__WIN32) || defined(_WIN32) # define TH_PLAT_WINDOWS 1 #else # define TH_PLAT_UNIX 1 #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 2015 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, THERR_BUSY, THERR_IO_ERROR, // 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 _th_llist_t { void *data; size_t num; struct _th_llist_t *prev, *next; } th_llist_t; th_llist_t * th_llist_new(void *data); void th_llist_free(th_llist_t *list); void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data)); void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *node)); void th_llist_append_node(th_llist_t **list, th_llist_t *node); th_llist_t * th_llist_append(th_llist_t **list, void *data); void th_llist_prepend_node(th_llist_t **list, th_llist_t *node); th_llist_t * th_llist_prepend(th_llist_t **list, void *data); void th_llist_delete(th_llist_t **list, const void *data); void th_llist_delete_node(th_llist_t **list, th_llist_t *node); void th_llist_delete_node_fast(th_llist_t **list, th_llist_t *node); th_llist_t * th_llist_get_nth(th_llist_t *list, const size_t n); size_t th_llist_length(const th_llist_t *list); ssize_t th_llist_position(const th_llist_t *list, const th_llist_t *node); void th_llist_foreach(th_llist_t *list, void (*func)(th_llist_t *node, void *userdata), void *data); int th_llist_foreach_cond(th_llist_t *list, int (*func)(th_llist_t *node, void *userdata), void *data, th_llist_t **res); th_llist_t * th_llist_find(th_llist_t *list, const void *data); th_llist_t * th_llist_find_func(th_llist_t *list, const void *userdata, int (compare)(const void *, const void *)); /* Ringbuffer implementation */ typedef struct { char **data; int n, size; void (*deallocator)(void *); } th_ringbuf_t; th_ringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *)); BOOL th_ringbuf_grow(th_ringbuf_t *buf, const size_t n); void th_ringbuf_free(th_ringbuf_t *buf); void th_ringbuf_add(th_ringbuf_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