
/*
	file: misc.lib.c
	purpose: This file contains low level utility routines for VADAR 
*/

#include "stdvadar.h"
#include <math.h>
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
#include <malloc.h>
#endif

void memory_error();
// char LIB_PATH[MAX_LINE_LEN];


/*************************************************************/
/* calloc with error check
*/
int *ecalloc(len, size, msg)		
unsigned len, size;
char *msg;
{
	int	*sp;

	if ((sp = (int *)calloc(len, size)) != NULL)
		return(sp);
	memory_error(msg);
	return(NULL);

}	/* ecalloc */


/*************************************************************/
/* malloc with error check
*/
char *emalloc(size, msg)		
unsigned size;
char *msg;
{
	char *sp;

	if ((sp = (char *)malloc(size)) != NULL)
		return(sp);
	memory_error(msg);
	return(NULL);

}	/* emalloc */


/*************************************************************/
/* prints out of memory error message and aborts. 
*/
void memory_error(msg)		
char *msg;
{

	fprintf(stdout, "\n");
	fprintf(stdout, "\n");
	fprintf(stdout, " *** Fatal Error: Out of memory...aborting\n");
	fprintf(stdout, " *** Module: %s\n", msg);
	fprintf(stdout, "\n");
	fprintf(stdout, " ----------------------------------------------------------------\n");
	fprintf(stdout, "\n");
	exit(1);

}	/* memory_error */

/*************************************************************/
/* write a message to the screen. 
*/
void show_msg(msg)
char	*msg;
{

	fprintf(stdout, msg);

}	/* show_msg */


/*************************************************************/
/* generic error handler 
*/
void show_err(dieFlag, sModule, msg)
int		dieFlag;
char	*sModule, *msg;
{

	fprintf(stdout, "\n");
	fprintf(stdout, "\n");
	if (dieFlag == TRUE)
		fprintf(stdout, " *** Fatal Error Detected!\n");
	else
		fprintf(stdout, " *** Warning, Error Detected!\n");
	fprintf(stdout, " *** Module.: %s\n", sModule);
	fprintf(stdout, " *** Message: %s\n", msg);
	fprintf(stdout, "\n");
	fprintf(stdout, " ----------------------------------------------------------------\n");
	fprintf(stdout, "\n");
	if (dieFlag == TRUE)
		exit(1);

}	/* show_err */


/*************************************************************/
/* out of memory error handler 
*/
void show_err_mem(dieFlag, sModule, routine, id)
int		dieFlag;
char	*sModule, *routine, *id;
{

	fprintf(stdout, "\n");
	fprintf(stdout, "**************************************************\n");
	fprintf(stdout, "\n");
	fprintf(stdout, " Error: Out of memory!\n");
	fprintf(stdout, "\n");
	fprintf(stdout, " Module.: %s\n", sModule);
	fprintf(stdout, " Routine: %s\n", routine);
	fprintf(stdout, " Protein: %s\n", id);
	fprintf(stdout, "\n");
	fprintf(stdout, "**************************************************\n");
	fprintf(stdout, "\n");
	if (dieFlag == TRUE)
		exit(1);

}	/* show_err_mem */


/*************************************************************/
/* generic file opening routine
*/
FILE *open_file(fileName, fileOp)
char *fileName, *fileOp;
{
	FILE 	*fp;
	char	sVal[MAX_LINE_LEN];

	fp = fopen(fileName, fileOp);
	if (fp == NULL) {
		sprintf(sVal, "\nError, cannot open file '%s'\n\n", fileName);
		show_msg(sVal);
	}

	return(fp);

}	/* open_file */


/*************************************************************/
/* prompt user with yes/no question. 
*/
int yes_no_query(msg)
char *msg;
{
	char	str[MAX_LINE_LEN];

	do {

		show_msg("\n");
		show_msg(msg);
		show_msg(PROMPT);
		fscanf(stdin, "%s", str);

	} while ((*str != 'Y') && (*str != 'y') && (*str != 'n') && (*str != 'N'));

	if ( (*str == 'Y') || (*str == 'y') )
		return(TRUE);
	return(FALSE);

}	/* yes_no_query */



/*********************************************************************
* PURPOSE:  generic routine for calculating standard deviation
* INPUT:    y = sum of all values
*           y2 = sum of squares of all values
*           n = size of sample
*/ 
float calc_sd(y, y2, n)
float y, y2;
int n;
{
float s2;
float ret;

	if (y == 0)
		return(0);

	s2  =  ( y2 - ( (y * y) / n) ) / (n-1) ;
	// ret = (float) sqrt( (double) s2) ;
	ret =  sqrtf( s2) ;

	return( ret );

}   /* calc_sd */

/*********************************************************************
* PURPOSE: to find where the library files are located
*/
/*
void find_lib()
{
char homelib[MAX_LINE_LEN];
char installLib[MAX_LINE_LEN];

	sprintf(installLib, "%s/vadar", getenv("PENCEMRC_LIB"));
	sprintf(homelib, "%s/lib/vadar", getenv("HOME"));

	if (checkExist(installLib))
		strcpy(LIB_PATH, installLib);
	else if (checkExist(homelib))
		strcpy(LIB_PATH, homelib);
	else if (checkExist("/usr/local/lib/vadar"))
		strcpy(LIB_PATH, "/usr/local/lib/vadar");
	else {
		printf("ERROR: cannot find where the library files are located.\n");
		printf("       The VADAR library files must be in either:\n");
		printf("       - %s \n", installLib);
		printf("       - %s \n", homelib);
		printf("       - %s. \n", "/usr/local/lib/vadar");
	}

}

*/
