home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / programs / amiga / pastex / pastex14-beta-6 / specialhost / source / parse.c < prev    next >
C/C++ Source or Header  |  1994-07-14  |  12KB  |  537 lines

  1. /*
  2. **    SpecialHost for PasTeX
  3. **
  4. **    Copyright © by Olaf Barthel & Georg Heßmann
  5. */
  6.  
  7. #include "Global.h"
  8.  
  9. enum    {    ARG_IFFFILE,ARG_PSFILE,ARG_HSIZE,ARG_VSIZE,ARG_HOFFSET,
  10.         ARG_VOFFSET,ARG_SCALE,ARG_HSCALE,ARG_VSCALE,ARG_ANGLE,ARG_CLIP,ARG_MODE,
  11.         ARG_BRIGHT,ARG_CONTRAST,ARG_GAMMA,ARG_RED,ARG_GREEN,ARG_BLUE,
  12.         ARG_TRANSFER,ARG_RENDERING,ARG_INVERT,ARG_BASEDPI,ARG_THRESHOLD,
  13.         ARG_PATCHCOLOURS,ARG_LLX,ARG_LLY,ARG_URX,ARG_URY,ARG_RWI,ARG_RHI,
  14.         ARG_DITHEROPT,ARG_PSINITSTRING,ARG_PSINITFILE,
  15.  
  16.         ARGCOUNT
  17.     };
  18.  
  19. #define TEMPLATE "IMAGE=IFFFILE/K,PSFILE/K,HSIZE/K,VSIZE/K,HOFFSET/K,VOFFSET/K,SCALE/K,HSCALE/K,VSCALE/K,ANGLE/K,CLIP/S,MODE/K,BRIGHT/K,CONTRAST/K,GAMMA/K,RED/K,GREEN/K,BLUE/K,TRANSFER/K,RENDER/K,INVERT/K,BASEDPI/K,THRESHOLD/K,PATCH=PATCHCOLOURS/S,LLX/K,LLY/K,URX/K,URY/K,RWI/K,RHI/K,DITHEROPT/K/N,PSINITSTRING/K,PSINITFILE/K"
  20.  
  21. STATIC BOOL __regargs
  22. GetDimension(STRPTR Argument,float *Storage,float Unit)
  23. {
  24.     UBYTE LocalBuffer[40];
  25.     LONG Len;
  26.  
  27.     memcpy(LocalBuffer,Argument,39);
  28.  
  29.     LocalBuffer[39] = 0;
  30.  
  31.     Len = strlen(LocalBuffer);
  32.  
  33.     while(Len > 0 && LocalBuffer[Len - 1] == ' ')
  34.         Len--;
  35.  
  36.     LocalBuffer[Len] = 0;
  37.  
  38.     if(Len > 2) /* if(Len > 2 && isalpha(LocalBuffer[Len-1])) */
  39.     {
  40.         STATIC struct { STRPTR Name; float Unit; } UnitTable[] =
  41.         {
  42.             "pt",    72.27,
  43.             "pc",    6.0225,     /* 72.27/12.0 */
  44.             "in",    1.0,
  45.             "bp",    72.0,
  46.             "cm",    2.54,
  47.             "mm",    25.4,
  48.             "dd",    67.54151,   /* 72.27*1157.0/1238.0 */
  49.             "cc",    5.62846,    /* 72.27*1157.0/1238.0/12.0 */
  50.                         "sp",   4736286.7   /* 72.27 * 65536.0 */
  51.         };
  52.  
  53.         LONG i;
  54.  
  55.         for(i = 0 ; i < 9 ; i++)
  56.         {
  57.             if(!Stricmp(&LocalBuffer[Len - 2],UnitTable[i] . Name))
  58.             {
  59.                 Unit = UnitTable[i] . Unit;
  60.  
  61.                 LocalBuffer[Len - 2] = 0;
  62.  
  63.                 break;
  64.             }
  65.         }
  66.     }
  67.  
  68.     *Storage = atof(LocalBuffer);
  69.  
  70.     switch(_FPERR)
  71.     {
  72.         case 0:
  73.  
  74.             *Storage /= Unit;
  75.  
  76.             return(TRUE);
  77.  
  78.         case _FPEUND:
  79.  
  80.             PrintLine("\33bFloating point underflow \"%s\".\33n",Argument);
  81.             return(FALSE);
  82.  
  83.         case _FPEOVF:
  84.  
  85.             PrintLine("\33bFloating point overflow \"%s\".\33n",Argument);
  86.             return(FALSE);
  87.  
  88.         case _FPEZDV:
  89.  
  90.             PrintLine("\33bDivision by zero \"%s\".\33n",Argument);
  91.             return(FALSE);
  92.  
  93.         case _FPENAN:
  94.  
  95.             PrintLine("\33bInvalid operation \"%s\".\33n",Argument);
  96.             return(FALSE);
  97.  
  98.         case _FPECOM:
  99.  
  100.             PrintLine("\33bFloating point value not comparable \"%s\".\33n",Argument);
  101.             return(FALSE);
  102.     }
  103. }
  104.  
  105. STATIC BOOL __regargs
  106. GetScale(STRPTR Argument,float *Storage, float Unit)
  107. {
  108.     *Storage = atof(Argument);
  109.  
  110.     switch(_FPERR)
  111.     {
  112.         case 0:
  113.  
  114.             if(*Storage <= 0.0)
  115.             {
  116.                 PrintLine("\33bIllegal scale value \"%s\".\33n",Argument);
  117.  
  118.                 return(FALSE);
  119.             }
  120.             else
  121.             {
  122.                 *Storage /= Unit;
  123.  
  124.                 return(TRUE);
  125.             }
  126.  
  127.         case _FPEUND:
  128.  
  129.             PrintLine("\33bFloating point underflow \"%s\".\33n",Argument);
  130.             return(FALSE);
  131.  
  132.         case _FPEOVF:
  133.  
  134.             PrintLine("\33bFloating point overflow \"%s\".\33n",Argument);
  135.             return(FALSE);
  136.  
  137.         case _FPEZDV:
  138.  
  139.             PrintLine("\33bDivision by zero \"%s\".\33n",Argument);
  140.             return(FALSE);
  141.  
  142.         case _FPENAN:
  143.  
  144.             PrintLine("\33bInvalid operation \"%s\".\33n",Argument);
  145.             return(FALSE);
  146.  
  147.         case _FPECOM:
  148.  
  149.             PrintLine("\33bFloating point value not comparable \"%s\".\33n",Argument);
  150.             return(FALSE);
  151.     }
  152. }
  153.  
  154. STATIC BOOL __regargs
  155. GetInteger(STRPTR Argument,LONG *Storage,LONG Min,LONG Max)
  156. {
  157.     if(StrToLong(Argument,Storage) < 1)
  158.     {
  159.         PrintLine("\33bIllegal value \"%s\".\33n",Argument);
  160.  
  161.         return(FALSE);
  162.     }
  163.     else
  164.     {
  165.         if(Min != Max)
  166.         {
  167.             if(*Storage < Min)
  168.                 *Storage = Min;
  169.             else
  170.             {
  171.                 if(*Storage > Max)
  172.                     *Storage = Max;
  173.             }
  174.         }
  175.  
  176.         return(TRUE);
  177.     }
  178. }
  179.  
  180. BOOL
  181. ParseSpecial(STRPTR OldString,struct parse_result *Result)
  182. {
  183.     STRPTR NewString;
  184.     LONG Len = strlen(OldString);
  185.     BOOL Success = FALSE;
  186.  
  187.     if(NewString = (STRPTR)AllocVecPooled(Len + 2,MEMF_ANY))
  188.     {
  189.         STRPTR Args[ARGCOUNT];
  190.         struct RDArgs *ArgsPtr;
  191.  
  192.         memset(Args,0,sizeof(Args));
  193.  
  194.         while(*OldString == ' ')
  195.             OldString++;
  196.  
  197.         strcpy(NewString,OldString);
  198.  
  199.         Len = strlen(NewString);
  200.  
  201.         while(Len > 0 && NewString[Len - 1] == ' ')
  202.             Len--;
  203.  
  204.         NewString[Len] = '\n';
  205.         NewString[Len + 1] = 0;
  206.  
  207.         if(ArgsPtr = (struct RDArgs *)AllocDosObjectTags(DOS_RDARGS,TAG_DONE))
  208.         {
  209.                 /* Don't prompt for input! */
  210.     
  211.             ArgsPtr -> RDA_Flags |= RDAF_NOPROMPT;
  212.     
  213.                 /* Set up for local parsing. */
  214.     
  215.             ArgsPtr -> RDA_Source . CS_Buffer    = NewString;
  216.             ArgsPtr -> RDA_Source . CS_Length    = Len + 1;
  217.             ArgsPtr -> RDA_Source . CS_CurChr    = 0;
  218.     
  219.                 /* Read the arguments. */
  220.     
  221.             if(ReadArgs(TEMPLATE,(LONG *)Args,ArgsPtr))
  222.             {
  223.                 BOOL    PostScript;
  224.                 float    Unit;
  225.                 float    ScaleUnit;
  226.  
  227.                 if(Args[ARG_PATCHCOLOURS])
  228.                     Result -> patch_colours = TRUE;
  229.  
  230.                 if(Args[ARG_DITHEROPT])
  231.                     Result -> dither_opt = *(LONG *)Args[ARG_DITHEROPT];
  232.  
  233.                 if(Args[ARG_PSINITSTRING])
  234.                     strcpy(Result -> psinit_string,Args[ARG_PSINITSTRING]);
  235.  
  236.                 if(Args[ARG_PSINITFILE])
  237.                     strcpy(Result -> psinit_file,Args[ARG_PSINITFILE]);
  238.  
  239.                 if(Args[ARG_IFFFILE] && Args[ARG_PSFILE])
  240.                     PrintLine("\33bYou can either use \"IFFFILE=...\" or \"PSFILE=...\" but not both.\33n");
  241.                 else
  242.                 {
  243.                     Success = TRUE;
  244.  
  245.                     if(Args[ARG_IFFFILE])
  246.                     {
  247.                         strcpy(Result -> iffile,Args[ARG_IFFFILE]);
  248.  
  249.                         Unit = 1.0;    // `small points'
  250.  
  251.                         ScaleUnit = 1.0;
  252.  
  253.                         PostScript = FALSE;
  254.                     }
  255.  
  256.                     if(Args[ARG_PSFILE])
  257.                     {
  258.                         strcpy(Result -> psfile,Args[ARG_PSFILE]);
  259.  
  260.                         Unit = 72.0;    // `big points'
  261.  
  262.                         ScaleUnit = 100.0; // percentage unit
  263.  
  264.                         PostScript = TRUE;
  265.                     }
  266.                 }
  267.  
  268.                 if(Args[ARG_HSIZE])
  269.                 {
  270.                     if(Success = GetDimension(Args[ARG_HSIZE],&Result -> hsize,Unit))
  271.                         Result -> gotcontrol |= GOT_HSIZE;
  272.                 }
  273.  
  274.                 if(Args[ARG_VSIZE] && Success)
  275.                 {
  276.                     if(Success = GetDimension(Args[ARG_VSIZE],&Result -> vsize,Unit))
  277.                         Result -> gotcontrol |= GOT_VSIZE;
  278.                 }
  279.  
  280.                 if(Args[ARG_HOFFSET] && Success)
  281.                 {
  282.                     if(Success = GetDimension(Args[ARG_HOFFSET],&Result -> hoffset,Unit))
  283.                         Result -> gotcontrol |= GOT_HOFFSET;
  284.                 }
  285.  
  286.                 if(Args[ARG_VOFFSET] && Success)
  287.                 {
  288.                     if(Success = GetDimension(Args[ARG_VOFFSET],&Result -> voffset,Unit))
  289.                         Result -> gotcontrol |= GOT_VOFFSET;
  290.                 }
  291.  
  292.                 if(Args[ARG_SCALE] && Success)
  293.                 {
  294.                     if(Success = GetScale(Args[ARG_SCALE],&Result -> scale, 1.0f))
  295.                         Result -> gotcontrol |= GOT_SCALE;
  296.                 }
  297.  
  298.                 if(Args[ARG_HSCALE] && Success)
  299.                 {
  300.                     if (Success = GetScale(Args[ARG_HSCALE],&Result -> hscale,ScaleUnit))
  301.                         Result -> gotcontrol |= GOT_HSCALE;
  302.                 }
  303.  
  304.                 if(Args[ARG_VSCALE] && Success)
  305.                 {
  306.                     if(Success = GetScale(Args[ARG_VSCALE],&Result -> vscale,ScaleUnit))
  307.                         Result -> gotcontrol |= GOT_VSCALE;
  308.                 }
  309.  
  310.                 if(Args[ARG_MODE] && Success)
  311.                 {
  312.                     STATIC struct { STRPTR Name; LONG Mode; } ModeTable[] =
  313.                     {
  314.                         "bw",        BandW,
  315.                         "gray",        FS,
  316.                         "fs",        FS,
  317.                         "burkes",    Burkes,
  318.                         "sierra",    Sierra,
  319.                         "jarvis",    JJN,
  320.                         "jjn",        JJN,
  321.                         "whitenoise",    RandomNoise,
  322.                         "bluenoise",    BlueNoise,
  323.                         "stucki",    Stucki,
  324.                         "ordered",    Ordered,
  325.                         "halftone",    Halftone,
  326.                         "random",    RandomNoise,
  327.                         "bckbrick",    BckBrick,
  328.                         "fwdbrick",    FwdBrick,
  329.                         "hexagon",    Hexagon,
  330.                         "spiraldot",    SpiralDot,
  331.                         "horizontal",    Horizontal,
  332.                         "stevenson",    StevensonArce,
  333.                         "sa",        StevensonArce
  334.                     };
  335.  
  336.                     LONG i;
  337.  
  338.                     Success = FALSE;
  339.  
  340.                     for(i = 0 ; i < 20 ; i++)
  341.                     {
  342.                         if(!Stricmp(Args[ARG_MODE],ModeTable[i] . Name))
  343.                         {
  344.                             Result -> mode = ModeTable[i] . Mode;
  345.  
  346.                             Success = TRUE;
  347.  
  348.                             break;
  349.                         }
  350.                     }
  351.  
  352.                     if(!Success)
  353.                         PrintLine("\33bUnknown shading mode \"%s\".\33n",Args[ARG_MODE]);
  354.                 }
  355.  
  356.                 if(Args[ARG_LLX] && Success)
  357.                 {
  358.                     if(Success = GetDimension(Args[ARG_LLX],&Result -> llx,Unit))
  359.                             Result -> gotcontrol |= GOT_LLX;
  360.  
  361.                 }
  362.  
  363.                 if(Args[ARG_LLY] && Success)
  364.                 {
  365.                     if(Success = GetDimension(Args[ARG_LLY],&Result -> lly,Unit))
  366.                         Result -> gotcontrol |= GOT_LLY;
  367.                 }
  368.  
  369.                 if(Args[ARG_URX] && Success)
  370.                 {
  371.                     if(Success = GetDimension(Args[ARG_URX],&Result -> urx,Unit))
  372.                         Result -> gotcontrol |= GOT_URX;
  373.                 }
  374.  
  375.                 if(Args[ARG_URY] && Success)
  376.                 {
  377.                     if(Success = GetDimension(Args[ARG_URY],&Result -> ury,Unit))
  378.                         Result -> gotcontrol |= GOT_URY;
  379.                 }
  380.  
  381.                 if(Args[ARG_RWI] && Success)
  382.                 {
  383.                     if(Success = GetDimension(Args