/**********************************************************************
Common module

$Id: common.c,v 1.4 2003/01/19 23:17:09 purnhage Exp $

Source file: common.c

(c) Heiko Purnhagen 1996-2003
e-mail: purnhage@tnt.uni-hannover.de
WWW:    http://www.tnt.uni-hannover.de/~purnhage/

This module is available under the terms of the GNU Lesser General
Public License. See file COPYING-LGPL for further details. All usual
disclaimers apply!

Authors:
HP    Heiko Purnhagen, Uni Hannover <purnhage@tnt.uni-hannover.de>

Changes:
06-jun-96   HP    new module based on ErrorMsg() from cmdline module
25-jun-96   HP    added CommonFreeAlloc()
26-aug-96   HP    CVS
25-jun-97   HP    added random()
16-oct-98   HP    fflush(stderr) in warning/exit
15-feb-00   HP    added random1init()
**********************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "common.h"		/* common module */


/* ---------- variables ---------- */

static char *CMprogName = "<progname>";		/* program name */


/* ---------- functions ---------- */

/* CommonFreeAlloc() */
/* Free previously allocated memory if present, then allocate new memory. */

void *CommonFreeAlloc (
  void **ptr,			/* in/out: addr of memory pointer */
  size_t size)			/* in: memory size */
				/* returns: memory pointer */
				/*          or NULL if error*/
{
  if (*ptr != NULL)
    free(*ptr);
  return *ptr = malloc(size);
}


/* CommonProgName() */
/* Set program name for error/warning message. */

void CommonProgName (
  char *progName)		/* in: program name */
{
  CMprogName = progName;
}


/* CommonWarning() */
/* Print program name and warning message to stderr. */

void CommonWarning (
  char *message,		/* in: warning message */
  ...)				/* in: args as for printf */
{
  va_list args;

  va_start(args,message);
  fflush(stdout);
  fprintf(stderr,"%s: WARNING: ",CMprogName);
  vfprintf(stderr,message,args);
  fprintf(stderr,"\n");
  fflush(stderr);
  va_end(args);
}


/* CommonExit() */
/* Print program name and error message to stderr and exit program. */

void CommonExit (
  int errorCode,		/* in: error code for exit() */
  char *message,		/* in: error message */
  ...)				/* in: args as for printf */
{
  va_list args;

  va_start(args,message);
  fflush(stdout);
  fprintf(stderr,"%s: ERROR[%d]: ",CMprogName,errorCode);
  vfprintf(stderr,message,args);
  fprintf(stderr,"\n");
  fflush(stderr);
  va_end(args);
  exit (errorCode);
}


/* random1() */
/* Generate random long (uniform distribution 0..RND_MAX). */

static long common_m4a_randx = 1;
static long common_m4a_randx_p = 0;
static long common_m4a_randx_s = 0;
static long common_m4a_randx_i = 0;

long random1 (void)
{
  if (common_m4a_randx_p) {
    common_m4a_randx_i++;
    if (common_m4a_randx_i >= common_m4a_randx_p) {
      common_m4a_randx = common_m4a_randx_s;
      common_m4a_randx_i = 0;
    }
  }

  common_m4a_randx = (common_m4a_randx * 1103515245) + 12345;
  return (common_m4a_randx & RND_MAX);
}


/* random1init() */
/* Init seed / period of random1(). */

void random1init (
  long seed,		/* in: random seed */
  long period)		/* in: period of rand. sequence (0=+inf) */
{
  common_m4a_randx_s = common_m4a_randx = 1+seed;
  common_m4a_randx_p = period;
}


/* end of common.c */

