1: # include   "../symbol.h"
   2: 
   3: convert(inp, outp, sf, slen, df, dlen)
   4: 
   5: char    *inp;   /* pointer to input */
   6: char    *outp;  /* pointer to the output area */
   7: int sf; /* format of the source number */
   8: int slen;   /* length of the source number */
   9: int df; /* format of the dest */
  10: int dlen;   /* length of the dest */
  11: 
  12: /*
  13: **	convert  converts  numeric values of one type and length
  14: **	to a different type and length.
  15: **
  16: **	The source numeric can be i1,i2,i4,f4,or f8.
  17: **	The source number will be converted to the
  18: **	type and length specified in the destination.
  19: **	It also  must be one of i1,i2,i4,f4, or f8.
  20: **
  21: **	convert returns 0 is no overflow occured,
  22: **	  else it returns -1
  23: */
  24: 
  25: {
  26:     char        number[8];  /* dummy buffer */
  27:     register char   *num;
  28:     register int    sl;
  29:     register int    dl;
  30: 
  31: #	define    i1deref(x)  (*((char *)(x)))
  32: #	define    i2deref(x)  (*((int *)(x)))
  33: #	define    i4deref(x)  (*((long *)(x)))
  34: #	define    f4deref(x)  (*((float *)(x)))
  35: #	define    f8deref(x)  (*((double *)(x)))
  36: 
  37:     dl = dlen;
  38:     sl = slen;
  39:     num = number;
  40:     bmove(inp, num,  sl);   /* copy number into buffer */
  41: 
  42:     if (sf != df)
  43:     {
  44:         /* if the source and destination formats are
  45: 		   different then the source must be converted
  46: 		   to i4 if the dest is int, otherwise to f8 */
  47: 
  48:         if (df == FLOAT)
  49:         {
  50:             switch (sl)
  51:             {
  52: 
  53:               case 1:
  54:                 f8deref(num) = i1deref(num);    /* i1 to f8 */
  55:                 break;
  56: 
  57:               case 2:
  58:                 f8deref(num) = i2deref(num);    /* i2 to f8 */
  59:                 break;
  60: 
  61:               case 4:
  62:                 f8deref(num) = i4deref(num);    /* i4 to f8 */
  63:             }
  64:         sl = 8;
  65:         }
  66:         else
  67:         {
  68:             /* check if float >  2**31 */
  69:             if (sl == 8)
  70:                 f4deref(num) = f8deref(num);    /* f8 to f4 */
  71: 
  72:             if (f4deref(num) > 2147483647.0 | f4deref(num) < -2147483648.0)
  73:                 return (-1);
  74:             i4deref(num) = f4deref(num);
  75:             sl = 4;
  76:         }
  77:     }
  78: 
  79:     /* source is now the same type as destination */
  80:     /* convert lengths to match */
  81: 
  82:     if (sl != dl)
  83:     {
  84:         /* lengths don't match. convert. */
  85:         if (df == FLOAT)
  86:         {
  87:             if (dl == 8)
  88:                 f8deref(num) = f4deref(num);    /* f4 to f8 */
  89:             else
  90:                 f4deref(num) = f8deref(num);    /* f8 to f4 with rounding */
  91:         }
  92:         else
  93:         {
  94:             switch (dl)
  95:             {
  96: 
  97:               case 1:
  98:                 if (sl == 2)
  99:                 {
 100:                     if (i2deref(num) > 127 | i2deref(num) < -128)
 101:                         return (-1);
 102:                     i1deref(num) = i2deref(num);    /* i2 to i1 */
 103:                 }
 104:                 else
 105:                 {
 106:                     if (i4deref(num) > 127 | i4deref(num) < -128)
 107:                         return (-1);
 108:                     i1deref(num) = i4deref(num);    /* i4 to i1 */
 109:                 }
 110:                 break;
 111: 
 112:               case 2:
 113:                 if (sl == 1)
 114:                 {
 115:                     i2deref(num) = i1deref(num);    /* i1 to i2 */
 116:                 }
 117:                 else
 118:                 {
 119:                     if (i4deref(num) > 32767 | i4deref(num) < -32768)
 120:                         return (-1);
 121:                     i2deref(num) = i4deref(num);    /* i4 to i2 */
 122:                 }
 123:                 break;
 124: 
 125:               case 4:
 126:                 if (sl == 1)
 127:                     i4deref(num) = i1deref(num);    /* i1 to i4 */
 128:                 else
 129:                     i4deref(num) = i2deref(num);    /* i2 to i4 */
 130:             }
 131:         }
 132:     }
 133: 
 134:     /* conversion is complete */
 135:     /* copy the result into outp */
 136: 
 137:     bmove(num, outp, dl);
 138:     return (0);
 139: }

Defined functions

convert defined in line 3; used 1 times

Defined macros

f4deref defined in line 34; used 6 times
f8deref defined in line 35; used 6 times
i1deref defined in line 31; used 5 times
i2deref defined in line 32; used 7 times
i4deref defined in line 33; used 10 times
Last modified: 1995-02-11
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2227
Valid CSS Valid XHTML 1.0 Strict