view th_util.h @ 322:b9c15c57dc8f

Clean up message functions, add new printMsgQ() helper function for messages that should not go into the log file. Add skeleton help function, accessible via F1 key. And other cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 11 Jun 2011 09:48:26 +0300
parents 21528fe535fb
children
line wrap: on
line source

/*
 * Generic utility-functions, macros and defaults
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2002-2010 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#ifndef _TH_UTIL_H
#define _TH_UTIL_H

#ifdef __cplusplus
extern "C" {
#endif

#include "th_types.h"
#include <stdarg.h>
#include <stdlib.h>
#include <sys/types.h>
#ifndef HAVE_NO_ASSERT
#include <assert.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#define HAVE_MEMSET 1
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif

#ifdef HAVE_MEMORY_H
#include <memory.h>
#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 2011 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

/* Global variables
 */
extern int  th_verbosityLevel;
extern char *th_prog_name,
            *th_prog_fullname,
            *th_prog_version,
            *th_prog_author,
            *th_prog_license;

/* Functions
 */
void    th_init(char *progName, char *progFullName, char *progVersion,
               char *progAuthor, char *progLicense);
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_calloc(size_t, size_t);
void    *th_realloc(void *, size_t);
void    th_free(void *);

#ifdef HAVE_MEMSET
#define th_memset memset
#else
void    *th_memset(void *, int, size_t);
#endif


/* 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));

qlist_t * th_llist_append(qlist_t **list, void *data);
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);

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);

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);


#ifdef __cplusplus
}
#endif
#endif /* _TH_UTIL_H */