home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / bit_blt / amiga / parse.c < prev    next >
C/C++ Source or Header  |  1987-04-24  |  5KB  |  195 lines

  1. /*
  2.  *   Parse.c of BlitLab software package.  This routine handles
  3.  *   parsing the strings into integers, in any of the possible
  4.  *   formats.
  5.  */
  6. #include "structures.h"
  7. /*
  8.  *   Externals we use.
  9.  */
  10. extern short *realbits ;
  11. extern char *bufarr[] ;
  12. extern long gvals[] ;
  13. extern struct blitregs blitregs ;
  14. extern char errorbuf[] ;
  15. /*
  16.  *   This is the main parse routine.  First, a static to indicate if
  17.  *   we saw a parse error or not.
  18.  */
  19. static int parseerr ;
  20. /*
  21.  *   We allow the following formats:
  22.  *
  23.  *      (M+)?-?[$%]?[0-9a-f]+
  24.  *      (~?[ABC])(+(~?[ABC]))*
  25.  */
  26. long parse(s)
  27. register char *s ;
  28. {
  29.    int negative = 1 ;
  30.    int c ;
  31.    int radix = 10 ;
  32.    long toadd = 0 ;
  33.    long val ;
  34.    static varr[] = { 0xf0, 0xcc, 0xaa } ;
  35.  
  36.    parseerr = 0 ;
  37.    while (*s == ' ')
  38.       s++ ;
  39.    if (*s=='~' || *s=='A' || *s=='B' || *s=='C' || *s=='a' || *s=='b'
  40.                || *s=='c') {
  41.       val = 0 ;
  42.       while (1) {
  43.          toadd = 255 ;
  44.          while (1) {
  45.             if (*s == '~') {
  46.                negative = 255 ;
  47.                s++ ;
  48.             } else
  49.                negative = 0 ;
  50.             c = *s++ ;
  51.             if (c == 0 || c == '+') {
  52.                if (toadd == 255)
  53.                   parseerr = 1 ;
  54.                break ;
  55.             }
  56.             if (c >= 'a' && c <= 'z')
  57.                c -= 'a' - 'A' ;
  58.             if (c < 'A' || c > 'C') {
  59.                parseerr = 1 ;
  60.                break ;
  61.             }
  62.             toadd &= negative ^ varr[c-'A'] ;
  63.          }
  64.          val |= toadd ;
  65.          if (c != '+') {
  66.             if (c != 0)
  67.                parseerr = 1 ;
  68.             break ;
  69.          }
  70.       }
  71.       return(val) ;
  72.    } else {
  73.       if (*s == 'm' || *s == 'M') {
  74.          if (s[1]=='+') {
  75.             s += 2 ;
  76.             toadd = (long)realbits ;
  77.          } else if (s[1]==0)
  78.             return((long)realbits) ;
  79.          else {
  80.             parseerr = 1 ;
  81.             return(0) ;
  82.          }
  83.       }
  84.       if (*s == '-') {
  85.          negative = -1 ;
  86.          s++ ;
  87.       }
  88.       if (*s == '$') {
  89.          radix = 16 ;
  90.          s++ ;
  91.       } else if (*s == '%') {
  92.          radix = 2 ;
  93.          s++ ;
  94.       }
  95.       val = 0 ;
  96.       if (*s == 0) {
  97.          parseerr = 1 ;
  98.          return(val) ;
  99.       }
  100.       while (1) {
  101.          c = *s ++ ;
  102.          if (c == 0)
  103.             break ;
  104.          if (c >= 'a' && c <= 'z')
  105.             c -= 'a' - 'A' ;
  106.          if (c >= 'A' && c <= 'F')
  107.             c -= 'A' - 10 ;
  108.          else
  109.             c -= '0' ;
  110.          if (c < 0 || c >= radix) {
  111.             parseerr = 1 ;
  112.             break ;
  113.          }
  114.          val = val * radix + c ;
  115.       }
  116.       return(toadd + negative * val) ;
  117.    }
  118. }
  119. /*
  120.  *   This routine parses all of the string gadgets.  If it is successful,
  121.  *   it returns 1, otherwise it returns 0.
  122.  */
  123. int parseall() {
  124.    int i ;
  125.  
  126.    for (i=0; i<MAXGADG; i++)
  127.       if (bufarr[i] != NULL) {
  128.          gvals[i] = parse(bufarr[i]) ;
  129.          if (parseerr) {
  130.             sprintf(errorbuf, "I can't parse %s", bufarr[i]) ;
  131.             error(errorbuf) ;
  132.             return(0) ;
  133.          }
  134.       }
  135.    updateregs() ;
  136.    return(1) ;
  137. }
  138. /*
  139.  *   This routine writes a four-digit hexadecimal value to the
  140.  *   screen.
  141.  */
  142. static char *hex = "0123456789abcdef" ;
  143. static char tmp[5] ;
  144. static writefour(x, y, val)
  145. int x, y, val ;
  146. {
  147.    tmp[3] = hex[val & 15] ;
  148.    val >>= 4 ;
  149.    tmp[2] = hex[val & 15] ;
  150.    val >>= 4 ;
  151.    tmp[1] = hex[val & 15] ;
  152.    val >>= 4 ;
  153.    tmp[0] = hex[val & 15] ;
  154.    drawtext(x, y, tmp) ;
  155. }
  156. /*
  157.  *   This routine calculates and writes out *all* of the blitter
  158.  *   register values.
  159.  */
  160. updateregs() {
  161.    int i ;
  162.  
  163.    blitregs.con0 = ((gvals[GDGASH] & 15) << 12) + (gvals[GDGUSEA] << 11) +
  164.       (gvals[GDGUSEB] << 10) + (gvals[GDGUSEC] << 9) + (gvals[GDGUSED] << 8) +
  165.       (gvals[GDGFUNC] & 255) ;
  166.    blitregs.con1 = ((gvals[GDGBSH] & 15) << 12) + (gvals[GDGEFE] << 4) +
  167.       (gvals[GDGIFE] << 3) + (gvals[GDGFCI] << 2) + (gvals[GDGDESC] << 1) +
  168.       gvals[GDGLINE] ;
  169.    blitregs.size = ((gvals[GDGV] & 1023) << 6) + (gvals[GDGH] & 63) ;
  170.    blitregs.afwm = (gvals[GDGAFWM] & 65535) ;
  171.    blitregs.alwm = (gvals[GDGALWM] & 65535) ;
  172.    for (i=0; i<4; i++) {
  173.       blitregs.pth[i] = ((gvals[GDGAPT+i] >> 16) & 65535) ;
  174.       blitregs.ptl[i] = (gvals[GDGAPT+i] & 65535) ;
  175.       blitregs.mod[i] = (gvals[GDGAMOD+i] & 65535) ;
  176.    }
  177.    for (i=0; i<3; i++)
  178.       blitregs.dat[i] = (gvals[GDGADAT+i] & 65535) ;
  179. /*
  180.  *   Now we write out the values.
  181.  */
  182.    writefour(HRVC2, VRVL1, blitregs.con0) ;
  183.    writefour(HRVC2, VRVL2, blitregs.con1) ;
  184.    writefour(HRVC2, VRVL3, blitregs.size) ;
  185.    writefour(HRVC2, VRVL4, blitregs.afwm) ;
  186.    writefour(HRVC2, VRVL5, blitregs.alwm) ;
  187.    for (i=0; i<4; i++) {
  188.       writefour(HRVC4, VRVL2 + 9 * i, blitregs.pth[i]) ;
  189.       writefour(HRVC5, VRVL2 + 9 * i, blitregs.ptl[i]) ;
  190.       writefour(HRVC6, VRVL2 + 9 * i, blitregs.mod[i]) ;
  191.    }
  192.    for (i=0; i<3; i++)
  193.       writefour(HRVC6B, VRVL2 + 9 * i, blitregs.dat[i]) ;
  194. }
  195.