Mercurial > hg > th-libs
diff th_util.c @ 0:bd61a80a6c54
Initial import into Mercurial repository. Discarding old cvs/svn history
here, because it's cluttered and commit messages are mostly crap.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 26 Mar 2008 04:41:58 +0200 |
parents | |
children | 67f4a4233372 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/th_util.c Wed Mar 26 04:41:58 2008 +0200 @@ -0,0 +1,140 @@ +/* + * 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 + */ +#ifdef TH_NO_DEFAULTS +#define TH_PROG_AUTHOR "" +#define TH_PROG_LICENSE "" +#else +#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" +#endif + +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) +{ + 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_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 + */ +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