/**---------------------------------------------------------------------------
 ** 
 ** msg.c -- Miscellaneous message utilities 
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/07/14 16:36:07
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/07/14 23:23:00
 ** Update Count    : 20
 ** Directory       : /home/pego/pcd1/codas3c/gfi/src/libs/misc/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    This file containsmiscellaneous utilities to output messages during run 
 **    time. Some of them will become obsolete.
 ** 
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 **------------------------------------------------------------------------**/
 
#include "msg.h"

/*
  Number of types used for the runtime information function short_info().
*/
#define NBR_INFOS_TYPES_MAX 10

/* ------------------------------------------------------------------
   Uses function report_msg() to display a formatted error message informing
   about the function in which the error occured, and the error message.
   Input arguments can be NULL, in which case its associated output is not
   performed. 

   RETURNS: 0

   NOTE: This function is obsolete. Already existing functions like
   error_found are sufficient. A better solution for handling error messages
   and associated information is also being developped currently
   ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int error_msg(/* Name of function */
              char *in_func, 
              /* Message to display */
              char *msg)
#else
int error_msg(in_func, msg) 
char *in_func; 
char *msg;
#endif
{
	char str[132];

	if(in_func!=NULL){
		sprintf(str, "\nERROR: (in %s)\n", in_func);
		report_msg(str);
	}
	if(msg != NULL){
		sprintf(str, "ERROR:\t%s\n", msg);
		report_msg(str);
	}
	/* Flushes the output */
	fflush(stdout);

	return(0);
}

/* ------------------------------------------------------------------
	 --Warning Message:

   Uses function report_msg() to display a formatted warning message informing
   about the function in which a problem occured and a warning message.
   Input arguments can be NULL, in which case its associated output is not
   performed. 

   RETURNS: 0

   NOTE: This function is obsolete. A better solution for handling warning
   messages is on its way.
	------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int warning_msg(/* Function name */
                char *in_func, 
                /* Warning message to display */
                char *msg)
#else
int warning_msg(in_func, msg) 
char *in_func; 
char *msg;
#endif
{
	char str[132];

	if(in_func!=NULL){
		sprintf(str, "\nWARNING: (in %s)\n", in_func);
		report_msg(str);
	}
	if(msg != NULL){
		sprintf(str, "WARNING:\t%s\n", msg);
		report_msg(str);
	}
	/* Flushes the output */
	fflush(stdout);

	return(0);
}

/* ------------------------------------------------------------------ 
   Displays to stdout (only) a short information about current processing,
   according to the given type. Normally, the info is printed inside '[' and
   ']', new lines being inserted according to the settings saved in function
   infos_per_line(). However, if the value of type is negative, it is printed
   over the previous one. If the value of info is NULL, a newline is printed.

   RETURNS: 0
   ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int short_info(/* Information to print */
               void *info, 
               /* Type of the information */
               int type)
#else
int short_info(info, type)
void *info;
int type;
#endif
{

	char func[]="short_info";
  int i;

	if(info==NULL){
		printf("\n");
		return(0);
	}
	switch (type){
	case (CHAR_VALUE_CODE):
		printf("[%c]", *((CHAR *) info));
		break;
	case (-CHAR_VALUE_CODE):
		printf("%c", '\b');
		printf("%c", *((CHAR *) info));
		break;
	case (USHORT_VALUE_CODE):
		printf("[%-hu]", *((USHORT *) info));
		break;
	case (-USHORT_VALUE_CODE):
		for(i=0; i<6; i++) printf("%c", '\b');
		printf("%6hu", *((USHORT *) info));
		break;
	case (ULONG_VALUE_CODE):
		printf("[%-lu]", *((ULONG *) info));
		break;
	case (-ULONG_VALUE_CODE):
		for(i=0; i<11; i++) printf("%c", '\b');
		printf("%11lu", *((ULONG *) info));
		break;
	default:
		error_msg(func, "no type associated to this code");
		break;
	}

	/* Check for newline character */
	if(type >= 0)
		infos_per_line(0, type);

	/* Flush output */
	fflush(stdout);

	return(0);
}

/* ------------------------------------------------------------------
   This function is used to set the number of information that can be printed
   before a new line should be inserted. Such a number is associated to each
   implemented type of information. If calling the function with zero value,
   the number of information printed on the current line is updated and a new
   line eventually inserted.
 
   RETURN: 0
   ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int infos_per_line(/* Number of lines for printing information */
                   int num, 
                   /* Type of information */
                   int type)
#else
int infos_per_line(num, type)
int num;
int type;
#endif
{

	static int init=0;
	static int max_infos[NBR_INFOS_TYPES_MAX];
	static int infos_number[NBR_INFOS_TYPES_MAX];

	int i;

	/* Initialize the static arrays */
	if(init==0){
		init=1;
		for(i=0; i<NBR_INFOS_TYPES_MAX; i++){
			max_infos[i]=0;
			infos_number[i]=0;
		}
	}
	
	/* Set the max info variable */
	if(num>0){
		max_infos[type-1]=num;
		infos_number[type-1]=0;
		short_info((void *)NULL, type);
	}
	
	/* Check for new line character */
	if((num==0) && (max_infos[type-1]>0)){
		infos_number[type-1]++;
		if(infos_number[type-1]>max_infos[type-1]){
			short_info((void *) NULL, type);
			infos_number[type-1]=0;
		}
	}

	return(0);
}

