/*
 * File: rdefault.c
 *  Contents: defcset, deffile, defint, defshort, defstr
 */

#include "..\h\config.h"
#include "..\h\rt.h"
#include "rproto.h"

/*
 * defcset(dp,cp,buf,def) - if dp is null, default to def;
 *  otherwise, convert to cset or die trying.
 */

int defcset(dp, cp, buf, def)
dptr dp;
int **cp;
int *buf, *def;
   {
   if (ChkNull(*dp)) {
      *cp = def;
      return Defaulted;
      }
   if (cvcset(dp, cp, buf) == CvtFail) 
      RetError(104, *dp);
   return Success;
   }

/*
 * deffile - if dp is null, default to def; otherwise, make sure it's a file.
 */

int deffile(dp, def)
dptr dp, def;
   {
   if (ChkNull(*dp)) {
      *dp = *def;
      return Defaulted;
      }
   if (dp->dword != D_File)
      RetError(105, *dp);
   return Success;
   }

/*
 * defint - if dp is null, default to def; otherwise, convert to integer.
 *  Note that *lp gets the value.
 */

int defint(dp, lp, def)
dptr dp;
long *lp;
word def;
   {
   if (ChkNull(*dp)) {
      *lp = (long)def;
      return Defaulted;
      }
   if (cvint(dp) == CvtFail)
      RetError(101, *dp);
   *lp = IntVal(*dp);
   return Success;
   }

/*
 * defshort - if dp is null, default to def; otherwise, convert to short
 *  integer.  The result is an integer value in *dp.
 */

int defshort(dp, def)
dptr dp;
int def;
   {
   if (ChkNull(*dp)) {
      MakeInt((int)def, dp);
      return Defaulted;
      }
   switch (cvint(dp)) {

      case T_Integer:
	 return Success;

      default:
	 RetError(101, *dp);
      }
   }

/*
 * defstr - if dp is null, default to def; otherwise, convert to string.
 *  *dp gets a descriptor for the resulting string.  buf is used as
 *  a scratch buffer for the conversion (if necessary).
 */

int defstr(dp, buf, def)
dptr dp;
char *buf;
dptr def;
   {
   int retcode;

   if (ChkNull(*dp)) {
      *dp = *def;
      return Defaulted;
      }

   retcode = cvstr(dp, buf);
   if (retcode == CvtFail) {
      RetError(103, *dp);
      }
   else
      return retcode;
   }
