/*
 * ----------------------------------------------------------------------------
 *
 *  			Copyright 1995 Division Limited.
 *			      All Rights Reserved
 *
 *
 *  System        : 
 *  Module        : 
 *  Object Name   : $RCSfile: pgeneral.h,v $
 *  Revision      : $Revision: 1.1 $
 *  Date          : $Date: 95/04/27 23:01:16 $
 *  Author        : $Author: bill $
 *  Last Modified : <260495.2024>
 *
 *  Description	
 *
 *  Notes
 *
 *  History
 *	
 *
 * ----------------------------------------------------------------------------
 */

/*
 * The following functions are provided in this module
 *  
 * Return flags - to be used in all libs and converters
 *     dpgSUCCESS=0, dpgERROR=-1
 * 
 * Verbose output setup (special "stderr" & "stdout")
 *     dpgSetOutput(n)
 *     
 * Verbose warning setup and usage
 *     dpgSetWarnLevel(n)
 *     dpgWarn(n,(print stuff))
 *     
 * Verbose error setup and usage
 *     variable dpgErrNo - set to the last error num ;
 *     dpgSetErrorLevel(n)
 *     dpgError(num, n,(print stuff))
 * 
 * Verbose monitering setup and usage
 *     dpgSetMonitorFlag(n)
 *     dpgMonitor(flag,(print stuff))
 * 
 * Verbose bad parameter passed error handler
 *     dpgBadAddress(addr,String)
 *     
 * Dying routine, outputs to the log and if not stderr, out to stderr
 *     dpgDie(level,(print stuff))
 * 
 * malloc and calloc routines that print out an error and exit on failure.
 *     dpgMalloc(size,string)
 *     dpgRealloc(pnt,size,string)
 *     dpgCalloc(nel,size,string)
 * 
 * dm macro Extensions
 * 
 */

#ifndef __PGENERAL_H__
#define __PGENERAL_H__

#include <dsys/divtypes.h>
#include <dsys/dm.h>

/* The program name must be defined and setup by the program */
extern char *progname ; 

#define dpgSUCCESS  0
#define dpgERROR   -1

/* General error numbers */

#define dpgENO_NULL 	0x1001
#define dpgENO_ROPEN 	0x1002
#define dpgENO_READ 	0x1003
#define dpgENO_WOPEN 	0x1004
#define dpgENO_WRITE 	0x1005

/* Verbose output setup (special "stderr" & "stdout") - returns dpgERROR
 * if couldn't open the file, SUCCESS otherwise
 */
extern FILE  *dpgLog ;

int32
dpgSetOutput(char *name) ;
#define dpgFlush() fflush(dpgLog)
/* internal functions */

/* If the next variable is non-zero then when any printing is done, a new
 * line will be inserted and the variable will be reset to 0 */
extern int32 dpgNewLine ;
#define dpgSetNewLine(v) (dpgNewLine=(v))

/* Verbose warning setup and usage */
extern int32 dpgWarnLvl ;

/* general printing routine */
extern void dpgPrint(char *format, ...) ;
void
dpgPrintActivity(int32 Cur, int32 Tot) ;

#define dpgIncWarnLevel()  (dpgWarnLvl++)
#define dpgSetWarnLevel(n) (dpgWarnLvl = n)

extern int32 __dpgWarn(char *format, ...) ;
#define dpgWarn(lvl,p)                                                       \
((lvl <= dpgWarnLvl) ? (__dpgWarn p):dpgSUCCESS)

/* Verbose error setup and usage */
extern int32 dpgErrorLvl ;
extern int32 dpgErrNo ;

#define dpgIncErrorLevel()  (dpgErrorLvl++)
#define dpgSetErrorLevel(n) (dpgErrorLvl = n)
extern int32 __dpgError(char *format, ...) ;
#define dpgError(num,lvl,p)                                                  \
((dpgErrNo = num),((lvl <= dpgErrorLvl) ? (__dpgError p):dpgERROR))

/* Verbose monitor setup and usage */
extern int32 dpgMonitorFlg ;

#define dpgSetMonitorFlag(n) (dpgMonitorFlg = n)
extern int32 __dpgMonitor(char *format, ...) ;
#define dpgMonitor(flag,p)                                                   \
((flag & dpgMonitorFlg) ? (__dpgMonitor p):dpgSUCCESS)
#define dpgTestMonitor(flag)                                                 \
((flag & dpgMonitorFlg) ? dpgERROR:dpgSUCCESS)

/* Dying routine, outputs to the log and if not stderr, out to stderr - exits
 */
extern void dpgDie(char *format, ...) ;

#define dpgQDie()                                                            \
do {                                                                         \
    fprintf(stderr,"Press q to quit.\n") ;                                   \
    if(getchar() == 'q')                                                     \
        dpgDie("User requested death\n") ;                                   \
} while(0)

/* Verbose bad parameter passed error handler - returns ERROR if addr is
 * NULL also does dpgError, returns SUCCESS otherwise
 */
int32
__dpgBadAddress(char *posStr) ;
#define dpgBadAddress(addr,posStr)                                           \
((addr == NULL) ? __dpgBadAddress(posStr) : dpgSUCCESS)
    


/* malloc and calloc routines that print out an error and exit on failure. */
void *
dpgMalloc(size_t size, char *posStr) ;
void *
dpgRealloc(void *pnt, size_t size, char *posStr) ;
void *
dpgCalloc(size_t noElm, size_t size, char *posStr) ;


/* The following are useful dm  extensions */

#define dmVectorSqrLen(a)                       \
    (dmSqr((a)[DM_X]) + dmSqr((a)[DM_Y]) + dmSqr((a)[DM_Z]))

#define dmVectorLen(a)           dmVectorLength(a)

#define dmVectorUnit(a)                         \
do {                                            \
    float32 mod ;                               \
    mod = dmLength3(a) ;                        \
    (a)[DM_X] /= mod ;                          \
    (a)[DM_Y] /= mod ;                          \
    (a)[DM_Z] /= mod ;                          \
} while (0)

#define dmVectorCmp(a,b)           memcmp(a,b,sizeof(dmVector))


#endif /* __PGENERAL_H__ */

