view th_util.c @ 100:ed4067c10a8a

Remove useless buffer usage from error reporting function.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 17 Nov 2009 23:09:10 +0200
parents 0313fabd8049
children 4ec36204d34e
line wrap: on
line source

/*
 * Generic utility-functions, macros and defaults
 * Programmed and designed by Matti 'ccr' Hamalainen
 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP)
 *
 * Please read file 'COPYING' for information on license and distribution.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "th_util.h"
#include <stdio.h>

/* Default settings
 */
static BOOL    th_initialized = FALSE;
int    th_verbosityLevel = 2;
char    *th_prog_name = NULL,
    *th_prog_fullname = NULL,
    *th_prog_version = NULL,
    *th_prog_author = NULL,
    *th_prog_license = NULL;


/* Initialize th_util-library and global variables
 */
void th_init(char *progName, char *progFullName, char *progVersion,
    char *progAuthor, char *progLicense)
{
    th_prog_name = progName;
    th_prog_fullname = progFullName;
    th_prog_version = progVersion;

    if (progAuthor)
        th_prog_author = progAuthor;
    else
        th_prog_author = TH_PROG_AUTHOR;

    if (progLicense)
        th_prog_license = progLicense;
    else
        th_prog_license = TH_PROG_LICENSE;

    th_initialized = TRUE;
}


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


/* Memory handling routines
 */
void *th_malloc(size_t l)
{
    return malloc(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);
}


#ifndef HAVE_MEMSET
void *th_memset(void *p, int c, size_t n)
{
    unsigned char *dp = (unsigned char *) p;
    
    while (n--)
        *(dp++) = c;

    return p;
}
#endif