view th_util.c @ 0:728243125263

Import.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 20 Mar 2008 00:15:03 +0000
parents
children ecfa4e3597e3
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
 */
#define	TH_PROG_NAME		"program"
#define	TH_PROG_FULLNAME	"A Program"
#define TH_PROG_VERSION		"0.0.0"
#define TH_PROG_AUTHOR		"By Matti 'ccr' Hämäläinen (C) Copyright 2008 TNSP"
#define TH_PROG_LICENSE		"This software is licensed under GNU GPL version 2"

BOOL	th_isInitialized = 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)
{
	/* Free previous values */
	if (progName)
		th_prog_name = progName;
	else
		th_prog_name = TH_PROG_NAME;

	if (progFullName)
		th_prog_fullname = progFullName;
	else
		th_prog_fullname = TH_PROG_FULLNAME;

	if (progVersion)
		th_prog_version = progVersion;
	else
		th_prog_version = TH_PROG_VERSION;

	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_isInitialized = TRUE;
}


/* Print formatted error, warning and information messages
 * TODO: Implement th_vfprintf() and friends?
 */
void THERR(const char *pcFormat, ...)
{
	va_list ap;
	assert(th_isInitialized);

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

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

	/* 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
 * TODO: Write alternate implementations for calloc and realloc,
 * to improve portability to platforms which don't implement them.
 */
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