view th_util.c @ 77:e8c9d7d13866

Handle certain key sequences better.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 13 Dec 2008 11:19:19 +0200
parents dd59868059d5
children 69aed051f84d
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(const char *pcFormat, ...)
{
	va_list ap;
	assert(th_initialized);

	va_start(ap, pcFormat);
	fprintf(stderr, "%s: ", th_prog_name);
	vfprintf(stderr, pcFormat, ap);
	va_end(ap);
}


void THMSG(int verbLevel, const char *pcFormat, ...)
{
	va_list ap;
	assert(th_initialized);

	/* Check if the current verbosity level is enough */
	if (th_verbosityLevel >= verbLevel) {
		va_start(ap, pcFormat);
		fprintf(stderr, "%s: ", th_prog_name);
		vfprintf(stderr, pcFormat, ap);
		va_end(ap);
	}
}


void THPRINT(int verbLevel, const char *pcFormat, ...)
{
	va_list ap;
	assert(th_initialized);

	/* Check if the current verbosity level is enough */
	if (th_verbosityLevel >= verbLevel) {
		va_start(ap, pcFormat);
		vfprintf(stderr, pcFormat, 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