home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / imdisp / source / labutil.c < prev    next >
Text File  |  1990-12-02  |  48KB  |  1,401 lines

  1. /*************************************************************/
  2. /*  Copyright (C) 1989, California Institute of Technology   */
  3. /*  U. S. Government Sponsorship under NASA Contract         */
  4. /*  NAS7-918 is acknowledged.                                */
  5. /*************************************************************/
  6.  
  7. /***  IMDISP module LABUTIL.C
  8.  
  9.         LABUTIL contains routines for processing PDS and FITS labels.
  10.  
  11. ***/
  12.  
  13. /* * * * INCLUDE files * * * */
  14.  
  15. #include <io.h>
  16. #include <malloc.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include "imdef.h"
  21. #include "imdisp.h"
  22. #include "dispio.h"
  23. #include "disputil.h"
  24. #include "fileio.h"
  25. #include "refresh.h"
  26.  
  27. /* * * * External functions * * * */
  28.  
  29. /* * * * Function declarations * * * */
  30.  
  31. int ExtractLabelString (char *, int, char *, int *, char *);
  32. int GetLabelInteger (char *, int, char *, int, int *, int *);
  33. int GetLabelLong (char *, int, char *, long, long *, int *);
  34. int GetLabelReal (char *, int, char *, float, float *, int *);
  35. int GetLabelString (char *, int, char *, char *, char *, int *);
  36. int InterpretLabel (char *, int, unsigned int *, int *, int *, int *,
  37.                    int *, int *, char *, char *, int);
  38. void   DoNoLabel (char *, int, unsigned int *, int *, int *, int *, int *,
  39.                 int *, char *, int *);
  40. void   DoFITSLabel (char *, int, unsigned int *, int *, int *, int *, int *,
  41.                   int *, int *, int);
  42. void   DoVicarLabel(char *, int, unsigned int *, int *, int *, int *,
  43.                   int *, int *, int *);
  44. int PutLabel (char *, int *, char *);
  45. int PutLabelValue (char *, int *, char *, int);
  46. int MakeLabel (char *, int *, int, int, int, int);
  47. int GetCommand (char *,char *);
  48. int ExtractKeywordString (char *, char *, int *, char *);
  49. int GetKeywordInteger (char *,char *,int ,int *,int *);
  50. int GetKeywordLong (char *, char *, long, long *, int *);
  51. int GetKeywordReal (char *, char *, float, float *, int *);
  52. int GetKeywordString (char *, char *, char *, char *, int *);
  53. int GetKeywordSubcommand (char *, char *, int *);
  54.  
  55. /* * * * Global Variables * * * */
  56.  
  57. int   PDSused = 0, switched = 1;
  58. long  GiantLabel;
  59. int   LastSearch;
  60. float maxlat, minlat, maxlon, minlon;
  61. char  Object[32];
  62. float xstart, xinterval;
  63. char  xunit[30];
  64.  
  65.  
  66. int ExtractLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
  67.                         int * p_flag, char * value)
  68.  
  69. /*  ExtractLabelString extracts the string "value" following the
  70.     keyword in the label buffer.
  71.  
  72. */
  73.  
  74. {
  75.     int i, j, k, l, m;
  76.     char  *searchptr;
  77.  
  78.  
  79.     /* Find where the keyword is in the buffer */
  80.     LabelBuf[LabelBufLen-1] = 0; /* add a null */
  81.     searchptr = strstr (LabelBuf, Keyword);
  82.     if (searchptr == NULL)
  83.        *p_flag = -1;
  84.     else
  85.     {
  86.        j = searchptr - LabelBuf;
  87.        if ((j > 0) && (LabelBuf[j-1] > ' '))
  88.        {
  89.        /* found something like end_object */
  90.            *p_flag = 0;
  91.            LastSearch += j+6;
  92.        }
  93.        else
  94.        {
  95.            LastSearch += j+6;
  96.            *p_flag = 1;
  97.            l = LabelBufLen - 1;
  98.            do         /* scan past the keyword */
  99.                j++;
  100.            while ((LabelBuf[j] != ' ') && (LabelBuf[j] != '=') && (j != l));
  101.            if (j == l)  *p_flag = 0;
  102.  
  103.            do    /* scan to the next non-blank, paren, or non equal char */
  104.                j++;
  105.            while ( ((LabelBuf[j] <= ' ') || (LabelBuf[j] == '=') ||
  106.                     (LabelBuf[j] == '(')) && (j < l) );
  107.  
  108.            k = j;
  109.            if (LabelBuf[j] == '"')
  110.            {
  111.                j++;     /* if value begins with a quote, read to next quote */
  112.                do
  113.                     k++;
  114.                while ( (LabelBuf[k] != '"') && (k <= l) );
  115.  
  116.                m=k+1;
  117.                if (LabelBuf[m] == ':' || LabelBuf[m] == ',') /* check for file record offset */
  118.                {
  119.                    do
  120.                        m++;
  121.                    while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
  122.                    strncpy(value,LabelBuf+k+2,m-k+2);
  123.                    value[m-k+2] = 0;      /* add a null */
  124.                    *p_flag = atoi(value) + 1;
  125.                }
  126.            }
  127.            else if (LabelBuf[j] == '\'')
  128.            {
  129.                j++;  /* if value begins with a single quote, read to next quote */
  130.                do
  131.                    k++;
  132.                while ( (LabelBuf[k] != '\'') && (k <= l) );
  133.                m=k+1;
  134.                if (LabelBuf[m] == ':' || LabelBuf[m] == ',')
  135.                /* check for file record offset */
  136.                {
  137.                    do
  138.                        m++;
  139.                    while ((LabelBuf[m] != ' ' && LabelBuf[m] !=')') && (m <= l) );
  140.                    strncpy(value,LabelBuf+k+2,m-k+2);
  141.                    value[m-k+2] = 0;      /* add a null */
  142.                    *p_flag = atoi(value) + 1;
  143.                }
  144.            }
  145.            else
  146.            {
  147.                do               /* scan past the value to next space */
  148.                    k++;
  149.                while ( (LabelBuf[k] > ' ') && (k <= l) );
  150.            }
  151.  
  152.            strncpy (value, LabelBuf+j, k-j);
  153.            value[k-j] = 0;
  154.        }
  155.     }
  156. }
  157.  
  158.  
  159.  
  160. int GetLabelInteger (char * LabelBuf, int LabelBufLen, char * Keyword,
  161.                      int defaul, int * p_value, int * p_flag)
  162.  
  163. /***  GetLabelInteger returns the integer value following the keyword in
  164.       the label buffer.  If the keyword is not there then the default
  165.       value is returned.  The flag indicates whether the keyword is
  166.       there.
  167.  
  168.    Parameter    Type    In/out   Description
  169.     LabelBuf  char ptr   in      The buffer containing the label
  170.     LabelBufLen  int     in      The length in bytes of the label buffer
  171.     Keyword   char ptr   in      The keyword string to match
  172.     defaul       int     in      The value to return if no keyword or value
  173.     p_value    int ptr   out     The returned value
  174.     p_flag     int ptr   out     The flag: -1 if no keyword match
  175.                                    0 if illegal value, 1 if keyword and value
  176.  
  177. ***/
  178.  
  179. {
  180.     char   stringvalue[80];
  181.  
  182.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  183.     if (*p_flag >= 1)
  184.     {
  185.         if (sscanf (stringvalue, "%d", p_value) == 0)
  186.         {
  187.             *p_flag = 0;
  188.             *p_value = defaul;
  189.         }
  190.     }
  191.     else
  192.     {
  193.         *p_value = defaul;
  194.     }
  195. }
  196.  
  197. int GetLabelLong (char * LabelBuf, int LabelBufLen, char * Keyword,
  198.                   long defaul, long * p_value, int * p_flag)
  199.  
  200. /***  GetLabelInteger returns the integer value following the keyword in
  201.       the label buffer.  If the keyword is not there then the default
  202.       value is returned.  The flag indicates whether the keyword is
  203.       there.
  204.  
  205.    Parameter    Type    In/out   Description
  206.     LabelBuf  char ptr   in      The buffer containing the label
  207.     LabelBufLen  int     in      The length in bytes of the label buffer
  208.     Keyword   char ptr   in      The keyword string to match
  209.     defaul       long    in      The value to return if no keyword or value
  210.     p_value    long ptr  out     The returned value
  211.     p_flag     int  ptr  out     The flag: -1 if no keyword match
  212.                                    0 if illegal value, 1 if keyword and value
  213.  
  214. ***/
  215.  
  216. {
  217.     char   stringvalue[80];
  218.  
  219.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  220.     if (*p_flag == 1)
  221.     {
  222.         if (sscanf (stringvalue, "%D", p_value) == 0)
  223.         {
  224.             *p_flag = 0;
  225.             *p_value = defaul;
  226.         }
  227.     }
  228.     else
  229.     {
  230.         *p_value = defaul;
  231.     }
  232. }
  233.  
  234.  
  235.  
  236.  
  237. int GetLabelReal (char * LabelBuf, int LabelBufLen, char * Keyword,
  238.                   float defaul, float * p_value, int * p_flag)
  239.  
  240. /***  GetLabelReal returns the real value following the keyword in the
  241.       label buffer.  If the keyword is not there then the default value
  242.       is returned.  The flag indicates whether the keyword is there.
  243.  
  244.    Parameter    Type    In/out   Description
  245.     LabelBuf  char ptr   in      The buffer containing the label
  246.     LabelBufLen  int     in      The length in bytes of the label buffer
  247.     Keyword   char ptr   in      The keyword string to match
  248.     defaul      float    in      The value to return if no keyword or value
  249.     p_value   float ptr  out     The returned value
  250.     p_flag     int ptr   out     The flag: -1 if no keyword match
  251.                                    0 if illegal value, 1 if keyword and value
  252.  
  253. ***/
  254.  
  255. {
  256.     char   stringvalue[80];
  257.  
  258.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  259.     if (*p_flag == 1)
  260.     {
  261.         if (sscanf (stringvalue, "%f", p_value) == 0)
  262.         {
  263.             *p_flag = 0;
  264.             *p_value = defaul;
  265.         }
  266.     }
  267.     else
  268.     {
  269.         *p_value = defaul;
  270.     }
  271. }
  272.  
  273. int GetLabelString (char * LabelBuf, int LabelBufLen, char * Keyword,
  274.                     char * defaul, char * value, int * p_flag)
  275.  
  276. /***  GetLabelString returns the string value following the keyword in
  277.       the label buffer.  If the keyword is not there then the default
  278.       value is returned.  The flag indicates whether the keyword is
  279.       there.
  280.  
  281.    Parameter    Type    In/out   Description
  282.     LabelBuf  char ptr   in      The buffer containing the label
  283.     LabelBufLen  int     in      The length in bytes of the label buffer
  284.     Keyword   char ptr   in      The keyword string to match
  285.     defaul    char ptr   in      The value to return if no keyword or value
  286.     p_value   char ptr   out     The returned value
  287.     p_flag     int ptr   out     The flag: -1 if no keyword match
  288.                                    0 if illegal value, 1 if keyword and value
  289.                                    >1 returns record offset +1
  290. ***/
  291.  
  292. {
  293.     char   stringvalue[255];
  294.  
  295.     ExtractLabelString (LabelBuf, LabelBufLen, Keyword, p_flag, stringvalue);
  296.     if (*p_flag >= 1)
  297.     {
  298.         strcpy (value, stringvalue);
  299.         strupr (value);
  300.     }
  301.     else
  302.     {
  303.         strcpy (value, defaul);
  304.     }
  305.   return(*p_flag);
  306. }
  307.  
  308. int InterpretLabel (char * buf, int len, unsigned int * p_labelsize,
  309.                     int * p_nline, int * p_nsamp, int * p_bitsperpix,
  310.                     int * p_reclen, int * p_lineheadbytes,
  311.                     char * DetachedFileName, char * DetachedPaletteName,
  312.                     int unit)
  313.  
  314. /***  InterpretLabel interprets the label in the buffer to extract the
  315.       image information.  First something is identified to determine
  316.       whether the image has a PDS, FITS or Vicar2 label or no label.
  317.       If the image is labeled then the appropriate keyword values are
  318.       extracted, otherwise the user is prompted for the into.
  319.  
  320.    Parameter         Type    In/out  Description
  321.     buf             char ptr   in    The label buffer
  322.     len               int      in    The length in bytes of the label buffer
  323.   p_labelsize      uns int ptr out   The size of the label or header to skip
  324.   p_nline            int ptr   out   The number of lines in the image
  325.   p_nsamp            int ptr   out   The number of samples in the image
  326.   p_bitsperpix       int ptr   out   The pixel format (16,8,4,1)
  327.   p_reclen           int ptr   out   The record length in bytes
  328.  p_lineheadbytes     int ptr   out   The header bytes at beginning of each line
  329. DetachedFileName    char ptr   out   The name of the file of data
  330.                                          null string if no detached label
  331. DetachedPaletteName char ptr   out   The name of the detached color palette
  332.                                          null string if no detached palette
  333.    unit               int      in    The number to the current file being
  334.                                          processed
  335.  
  336. ***/
  337.  
  338. {
  339.     char    FileType[32], SampleType[32], inpstr[32];
  340.     char    ObjectName[5][32];
  341.     int     image_loc[5];
  342.     char    *object_loc;
  343.     int     images=0;
  344.     int     i, j, k, flag, irflag, labelrec, bitspixdef;
  345.     int     totalrecs, labelrecs, labelreclen;
  346.     int     headerrecs, headerreclen, nlinedef, linereclen, image_offset=1;
  347.     int     imagerecs, imagereclen, imrecldef;
  348.     int     *p_linetrailbytes;
  349.     int     done, bytecount, read_an_end, finallength;
  350.     char    *tempbufptr;
  351.     char    line[81];
  352.     FILE    *labels;
  353.     char    tmpch;
  354.     char    *count, tmpname[32];
  355.     char    colname[32];
  356.  
  357.     while ((p_linetrailbytes = (int *) malloc(sizeof(int)))==NULL)
  358.        FreeRefresh("trail");
  359.  
  360. /* check for variable length file */
  361.     GetLabelString (buf,len,"RECORD_TYPE","FIXED_LENGTH",FileType,&flag);
  362.     if (strncmp(FileType,"VARIABLE_LENGTH",15) == 0)
  363.     {
  364.        TextLine = TextHeight + 5;  TextSample = 1;
  365.        ClearDisplay(0);
  366.        WriteText ("This is a compressed image.");
  367.        WriteText (" ");
  368.        WriteText ("Use the PCDCOMP program in the SOFTWARE directory");
  369.        WriteText ("to decompress it, then display the uncompressed image.");
  370.        *p_reclen = 0; *p_bitsperpix = 8;
  371.        return(0);
  372.     }
  373.  
  374.     strcpy (DetachedFileName, "");
  375.     strcpy (DetachedPaletteName,"");
  376.     GiantLabel = 0L;
  377.     LastSearch = 0;
  378.     strcpy(Object,"^");
  379. /* Check for multiple objects */
  380.     do
  381.     {
  382.     /*** fix this to not need object + trailing blank */
  383.        GetLabelString (buf+LastSearch, len-LastSearch, "OBJECT ",
  384.                        "", ObjectName[images], &flag);
  385.        if (flag > 0)            /* if an object = argument has been found */
  386.        {
  387.           strcpy(tmpname, ObjectName[images]);
  388.           if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
  389.           {
  390.           /* _ not found */
  391.                tmpch= tmpname[0];
  392.           }
  393.           else
  394.           {
  395.           /* put last keyword string into tmpname */
  396.               strcpy(tmpname,count+1);
  397.               tmpch= tmpname[0];
  398.           }
  399.  
  400.           /* Check for objects other than IMAGE */
  401.           switch(tmpch)
  402.           {
  403.           case 'I':
  404.  
  405.               if     (tmpname[1] == 'M')  /* IMAGE */
  406.                   object_loc = strstr(ObjectName[images],"IMAGE");
  407.               else if(tmpname[1] == 'N')  /* INTENSITY_SPECTRUM */
  408.                   object_loc = strstr(ObjectName[images],"INTEN");
  409.               else
  410.                   object_loc = NULL;
  411.               break;
  412.  
  413.           case 'S':
  414.  
  415.               object_loc = strstr(ObjectName[images],"SPECT");
  416.               break;
  417.  
  418.           case 'P':
  419.  
  420.               object_loc = strstr(ObjectName[images],"POSIT");
  421.               break;
  422.  
  423.          case 'T':
  424.  
  425.               if   (tmpname[1] == 'A')  /* TABLE */
  426.               {
  427.                   object_loc = strstr(ObjectName[images],"TABLE");
  428.               /* Added the next line to ignore tables for now, take out if
  429.                  you want tables to be included  - 06/23/90 Ron Baalke   */
  430.                   object_loc = NULL;
  431.               }
  432.               else if(tmpname[1] == 'E')  /* TEXT */
  433.                   object_loc = strstr(ObjectName[images],"TEXT");
  434.               else
  435.                   object_loc = NULL;
  436.               break;
  437.  
  438.           case 'L':
  439.  
  440.               if     (tmpname[1] == 'A')  /* LABEL */
  441. /*                  object_loc = strstr(ObjectName[images],"LABEL"); */
  442.                   object_loc = NULL; /* don't process labels */
  443.               else if(tmpname[1] == 'H') /* LHC_POLARIZATION_SPECTRUM */
  444.                   object_loc = strstr(ObjectName[images],"LHC_P");
  445.               else
  446.                   object_loc = NULL;
  447.               break;
  448.  
  449.           case 'R':  /* RHC_POLARIZATION_SPECTRUM */
  450.  
  451.               object_loc = strstr(ObjectName[images],"RHC_P");
  452.               break;
  453.  
  454.           default:
  455.  
  456.               object_loc = NULL;
  457.               break;
  458.           }
  459.  
  460.  
  461.           if (object_loc != NULL &&
  462.              ((i=object_loc - ObjectName[images]) ==
  463.              (strlen(ObjectName[images])-strlen(tmpname) )) )
  464.           {
  465.               image_loc[images] = LastSearch;
  466.               images++;
  467.           }
  468.        }  /* end OBJECT found */
  469.     } while (flag >= 0);
  470.  
  471.     if (images == 1) strcat(Object,ObjectName[0]);
  472.     if (images > 1)
  473.     {
  474.        ClearDisplay(0);
  475.        TextLine = TextHeight + 5;  TextSample = 1;
  476.        WriteText ("More than one image object in file.");
  477.        for (i=0;i<images;i++)
  478.        {
  479.            sprintf(line,"  %d. %s",i+1,ObjectName[i]);
  480.            WriteText (line);
  481.        }
  482.        TypeText ("Select the image to be displayed: ");
  483.        do
  484.        {
  485.            AcceptText (inpstr);
  486.            flag = sscanf (inpstr, "%d", &i);
  487.        } while (!flag);
  488.        i--;
  489.        if (i < 0 || i > images) i = 0;
  490.        strcat(Object,ObjectName[i]);
  491.        strcpy(line,ObjectName[i]);
  492.        strcpy(ObjectName[0],line);
  493.        for (j=0;j<images;j++)
  494.        {
  495.            if (j != i) /* blank out labels for this object */
  496.            {
  497.                object_loc = strstr(buf+image_loc[j],"END_OBJECT");
  498.                if (object_loc != NULL)
  499.                    for (k=image_loc[j];k<(int)(object_loc-buf);k++) buf[k] = ' ';
  500.             }
  501.        }
  502.     }
  503.  
  504. /* This part for PDS labels */
  505.     GetLabelString (buf, len, "FILE_TYPE", " ", FileType, &flag);
  506.     if (flag < 0 && Object[1] != '\0')
  507.     {
  508.        strcat(Object," ");
  509.  
  510. /*       tmpch=ObjectName[0][0]; */
  511. /*  Multiple object fix (start) */
  512.           strcpy(tmpname, ObjectName[0]);
  513.           if ( (count = strrchr(tmpname,'_')) == NULL) /* Look for last _ */
  514.           {
  515.           /* _ not found */
  516.                tmpch= tmpname[0];
  517.           }
  518.           else
  519.           {
  520.           /* put last keyword string into tmpname */
  521.               strcpy(tmpname,count+1);
  522.               tmpch= tmpname[0];
  523.           }
  524. /*  Multiple object fix (end) */
  525.        GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
  526.        if (flag < 0)
  527.        {
  528.            switch(tmpch)
  529.            {
  530.            case 'E':
  531.                strcpy(Object,"^TABLE");
  532.                break;
  533.            case 'I':
  534.  
  535. /*               if(ObjectName[0][1] == 'M') */
  536.                if(tmpname[1] == 'M')
  537.                    strcpy(Object,"^IMAGE"); /* if pointer not found try this */
  538.                if(ObjectName[0][1] == 'N')
  539.                    strcpy(Object,"^INTEN");
  540.                break;
  541.  
  542.            case 'S':
  543.  
  544.                strcpy(Object,"^SPECT");
  545.                break;
  546.  
  547.            case 'P':
  548.  
  549.                strcpy(Object,"^POSIT");
  550.                break;
  551.  
  552.            case 'T':
  553.  
  554.                if(ObjectName[0][1] == 'A')
  555.                    strcpy(Object,"^TABLE");
  556.                if(ObjectName[0][1] == 'E')
  557.                    strcpy(Object,"^TEXT");
  558.                break;
  559.  
  560.            case 'L':
  561.  
  562.                if(ObjectName[0][1] == 'A')
  563.                    strcpy(Object,"^LABEL");
  564.                if(ObjectName[0][1] == 'H')
  565.                    strcpy(Object,"^LHC_P");
  566.                break;
  567.  
  568.            case 'R':
  569.  
  570.                strcpy(Object,"^RHC_P");
  571.                break;
  572.            }
  573.  
  574.            GetLabelInteger (buf, len, Object, 0, &image_offset, &flag);
  575.        }
  576.  
  577.        /* Switch over the various objects */
  578.        if (flag >= 0)
  579.        switch(tmpch)
  580.        {
  581.            case 'E':
  582.                strcpy(FileType,"TABLE");
  583.                break;
  584.            case 'I':
  585.  
  586. /*               if(ObjectName[0][1] == 'M') */
  587.                if(tmpname[1] == 'M')
  588.                    strcpy(FileType,"IMAGE");
  589.                if(ObjectName[0][1] == 'N')
  590.                    strcpy(FileType,"INTEN");
  591.                break;
  592.  
  593.            case 'S':
  594.  
  595.                strcpy(FileType,"SPECT");
  596.                break;
  597.  
  598.            case 'P':
  599.  
  600.                strcpy(FileType,"POSIT");
  601.                break;
  602.  
  603.            case 'T':
  604.  
  605.                if(ObjectName[0][1] == 'A')
  606.                    strcpy(FileType,"TABLE");
  607.                if(ObjectName[0][1] == 'E')
  608.                    strcpy(FileType,"TEXT");
  609.                break;
  610.  
  611.            case 'L':
  612.  
  613.                if(ObjectName[0][1] == 'A')
  614.                    strcpy(FileType,"LABEL");
  615.                if(ObjectName[0][1] == 'H')
  616.                    strcpy(FileType,"LHC_P");
  617.                break;
  618.  
  619.            case 'R':
  620.  
  621.                strcpy(FileType,"RHC_P");
  622.                break;
  623.        }
  624.        if (flag == 0)
  625.        {
  626.            GetLabelString (buf, len, Object, "", DetachedFileName, &flag);
  627.            if (flag >  1)
  628.                image_offset = flag - 1;
  629.            if (flag == 1)
  630.                image_offset = 1;
  631.        }
  632.     }
  633.  
  634.     if ( strncmp( FileType, "IMAGE", 5) == 0
  635.       || strncmp( FileType, "SPECT", 5) == 0
  636.       || strncmp( FileType, "POSIT", 5) == 0
  637.       || strncmp( FileType, "INTEN", 5) == 0
  638.       || strncmp( FileType, "LHC_P", 5) == 0
  639.       || strncmp( FileType, "RHC_P", 5) == 0
  640.       || strncmp( FileType, "TABLE", 5) == 0
  641.       || strncmp( FileType, "TEXT",  4) == 0
  642.       || strncmp( FileType, "LABEL", 5) == 0 )
  643.     {
  644.        GetLabelInteger (buf, len, "RECORD_BYTES", 512, p_reclen, &flag);
  645.        GetLabelInteger (buf, len, "FILE_RECORDS", 513, &totalrecs, &flag);
  646.        GetLabelInteger (buf, len, "LABEL_RECORDS", 1, &labelrecs, &flag);
  647.        GetLabelInteger (buf, len, "LABEL_RECORD_BYTES", *p_reclen,
  648.                          &labelreclen, &flag);
  649.        GetLabelInteger (buf, len, "HEADER_RECORDS", 0,
  650.                          &headerrecs, &flag);
  651.        GetLabelInteger (buf, len, "HEADER_RECORD_BYTES", *p_reclen,
  652.                          &headerreclen, &flag);
  653.        GetLabelLong    (buf, len, "^IMAGE_HISTOGRAM", 0L,
  654.                          &Histogram, &flag);
  655.        if (Histogram != 0L) Histogram = (Histogram-1)* (long)*p_reclen;
  656.        GetLabelLong    (buf, len, "^PALETTE", 0L,&Palette, &flag);
  657.        if (Palette != 0L) Palette = (Palette-1)* (long)*p_reclen;
  658.        if (flag == 0)
  659. /*         GetLabelString(buf,len,"^PALETTE","",PaletteFileName,&flag); */
  660.          GetLabelString(buf,len,"^PALETTE","",DetachedPaletteName,&flag);
  661.        if (Object[1] == '\0')
  662.        {
  663.            GetLabelInteger (buf, len, "^IMAGE ",0,&image_offset, &flag);
  664.            if(flag < 1)
  665.                GetLabelInteger(buf,len,"^SPECT",0,&image_offset,&flag);
  666.            if(flag < 1)
  667.                GetLabelInteger(buf,len,"^POSIT",0,&image_offset,&flag);
  668.            if(flag < 1)
  669.                GetLabelInteger(buf,len,"^INTEN",0,&image_offset,&flag);
  670.            if(flag < 1)
  671.                GetLabelInteger(buf,len,"^LHC_P",0,&image_offset,&flag);
  672.            if(flag < 1)
  673.                GetLabelInteger(buf,len,"^RHC_P",0,&image_offset,&flag);
  674.            if(flag < 1)
  675.                GetLabelInteger(buf,len,"^TABLE",0,&image_offset,&flag);
  676.            if(flag < 1)
  677.                GetLabelInteger(buf,len,"^TEXT",0,&image_offset,&flag);
  678.            if(flag < 1)
  679.                GetLabelInteger(buf,len,"^LABEL",0,&image_offset,&flag);
  680.        }
  681.  
  682.        if (image_offset == 0)
  683.            GiantLabel  = (long)labelrecs*(long)labelreclen
  684.                        + (long)headerrecs*(long)headerreclen;
  685.        else
  686.            GiantLabel  = (long)(image_offset-1) * (long)*p_reclen;
  687.  
  688.        if (GiantLabel < 65535L)
  689.            *p_labelsize = (unsigned int)GiantLabel;
  690.        else
  691.            *p_labelsize = 0;
  692.  
  693.     /* Find nline */
  694.        if (*p_labelsize == 0)
  695.            nlinedef = totalrecs;
  696.        else
  697.            nlinedef = totalrecs - (((*p_labelsize-1) / *p_reclen) + 1);
  698.  
  699.        GetLabelInteger (buf, len, "IMAGE_RECORDS", nlinedef, &imagerecs, &irflag);
  700.        GetLabelInteger (buf, len, "IMAGE_LINES", imagerecs, p_nline, &flag);
  701.        if (flag < 1)
  702.            GetLabelInteger (buf, len, "LINES", imagerecs,p_nline, &flag);
  703.        if (flag < 1)
  704.            GetLabelInteger(buf,len,"ROWS",imagerecs,p_nline,&flag);
  705.  
  706.        if (irflag < 1)  imagerecs = *p_nline;
  707.  
  708.     /* Find reclen */
  709.        GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen,p_reclen, &flag);
  710.        if (flag < 1)
  711.            GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_reclen,&flag);
  712.        GetLabelInteger (buf, len, "RECORD_BYTES", *p_reclen, p_reclen, &flag);
  713.        imrecldef = *p_reclen * (*p_nline / imagerecs);
  714.        GetLabelInteger (buf, len, "IMAGE_RECORD_BYTES", imrecldef,
  715.                          &imagereclen, &flag);
  716.        *p_reclen = imagereclen/ (*p_nline /imagerecs);
  717.  
  718.     /* Find nsamp */
  719.        GetLabelInteger (buf, len, "LINE_SAMPLES", *p_reclen, p_nsamp, &flag);
  720.        if (flag < 1)
  721.            GetLabelInteger(buf,len,"ROW_COLUMNS",*p_reclen,p_nsamp,&flag);
  722.  
  723.     /* Find bitsperpix */
  724.        bitspixdef      = (8L* (*p_reclen)) / *p_nsamp;
  725.        if (bitspixdef <= 1)
  726.            bitspixdef = 1;
  727.        else
  728.            if (bitspixdef <= 4)
  729.                bitspixdef = 4;
  730.  
  731. /*  removed next line, AEE 4-21-89
  732.            else
  733.                bitspixdef = 8;
  734. */
  735.        GetLabelInteger (buf, len, "SAMPLE_BITS", bitspixdef,
  736.                         p_bitsperpix, &flag);
  737.        if (flag < 1)
  738.            GetLabelInteger(buf,len,"COLUMN_BITS",bitspixdef,p_bitsperpix,&flag);
  739.  
  740.        GetLabelString(buf,len,"COLUMN_NAME"," ",colname,&flag);
  741.        if (strncmp(colname,"(WAVELENGTH",11)==0
  742.         || strncmp(colname,"(POSITION",9)==0)
  743.        {
  744.            switched= 0;
  745.            ByteSwap= TRUE;
  746.        }
  747.        else
  748.        {
  749.            switched= 1;
  750.            ByteSwap= FALSE;
  751.        }
  752.  
  753.        GetLabelInteger (buf, len, "LINE_PREFIX_BYTES", 0,
  754.                          p_lineheadbytes, &flag);
  755.        GetLabelInteger (buf, len, "LINE_SUFFIX_BYTES", 0,
  756.                          p_linetrailbytes, &flag);
  757.        if (p_lineheadbytes != 0 || p_linetrailbytes != 0)
  758.            *p_reclen = (((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1)
  759.                      +(long)*p_lineheadbytes + (long)*p_linetrailbytes;
  760.  
  761.     /* Check for non-byte swapped sample value */
  762.        GetLabelString (buf, len, "SAMPLE_TYPE", "LSB_INTEGER",
  763.             SampleType, &flag);
  764.        if (flag < 1)
  765.            GetLabelString(buf,len,"COLUMN_TYPE","LSB_INTEGER",SampleType,&flag);
  766.        if (flag > 0)
  767.        {
  768.            if (strncmp(SampleType,"MSB_INTEGER",11) == 0)      ByteSwap = TRUE;
  769.            if (strncmp(SampleType,"UNSIGNED_INTEGER",11) == 0) ByteSwap = TRUE;
  770.            if (strncmp(SampleType,"VAX_INTEGER",11) == 0)      ByteSwap = FALSE;
  771.            if (strncmp(SampleType,"LSB_INTEGER",11) == 0)      ByteSwap = FALSE;
  772.        }
  773.  
  774. /* Get plot information for X-axis */
  775.  
  776.        GetLabelReal( buf, len, "START_WAVELENGTH", 0.0, &xstart, &flag);
  777.        GetLabelReal( buf, len, "WAVELENGTH_INTERVAL", 1.0, &xinterval, &flag);
  778.        GetLabelString( buf, len, "WAVELENGTH_UNIT", "PIXEL", xunit, &flag);
  779.  
  780. /*
  781.        GetLabelReal(buf,len,"START_",1.0,&xstart,&flag);
  782.        if(flag >= 1)
  783.        {
  784.            sprintf(xlabel,"%.12s",strstr(buf,"START_")+6);
  785.            strtok(xlabel," ");
  786.  
  787.            strcpy(intervaltoken,xlabel);
  788.            strcat(intervaltoken,"_INTERVAL");
  789.  
  790.            strcpy(unittoken,xlabel);
  791.            strcat(unittoken,"_UNIT");
  792.            GetLabelReal(buf,len,intervaltoken,1.0,&xinterval,&flag);
  793.            GetLabelString(buf,len,unittoken,"",xunit,&flag);
  794.        }
  795. */
  796.     /* Find maxlat and lon */
  797.        GetLabelReal(buf, len, "MAXIMUM_LATITUDE", 0.0, &maxlat, &flag);
  798.        GetLabelReal(buf, len, "MINIMUM_LATITUDE", 0.0, &minlat, &flag);
  799.        GetLabelReal(buf, len, "MAXIMUM_LONGITUDE", 0.0, &maxlon, &flag);
  800.        GetLabelReal(buf, len, "MINIMUM_LONGITUDE", 0.0, &minlon, &flag);
  801.        flag = 0;
  802.        if (strlen(DetachedFileName) == 0)
  803.            GetLabelString (buf, len, "IMAGE_POINTER", "",
  804.                     DetachedFileName, &flag);
  805.        if (flag > 0)
  806.        {
  807.            GiantLabel = (long)headerrecs*(long)headerreclen;
  808.            if (GiantLabel < 65535L)
  809.                *p_labelsize = (unsigned int)GiantLabel;
  810.            else
  811.                *p_labelsize = 0;
  812.        }
  813.        if (strlen(DetachedPaletteName) == 0)
  814.            GetLabelString(buf, len, "PALETTE_POINTER", "",
  815.                     DetachedPaletteName, &flag);
  816.  
  817.        free(p_linetrailbytes);
  818.        PDSused= 1;
  819.        return;
  820.     }
  821.  
  822. /* This part for VICAR2 labels */
  823.     GetLabelString (buf, len, "TYPE", " ", FileType, &flag);
  824.     if ( stricmp (FileType, "IMAGE") == 0 )
  825.     {
  826.        DoVicarLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
  827.              p_reclen, p_lineheadbytes, p_linetrailbytes);
  828.        return;
  829.     }
  830.  
  831. /* This part for FITS labels */
  832.     GetLabelString (buf, len, "SIMPLE", " ", FileType, &flag);
  833.     if ( (stricmp (FileType, "T") == 0) ||
  834.          (stricmp (FileType, "F") == 0) ) /* this is a FITS file */
  835.     {
  836.        DoFITSLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
  837.              p_reclen, p_lineheadbytes, p_linetrailbytes, unit);
  838.  
  839.        PDSused= 0;
  840.        return;
  841.     }
  842.  
  843. /* Do this if no recognized label */
  844.     DoNoLabel( buf, len, p_labelsize, p_nline, p_nsamp, p_bitsperpix,
  845.              p_reclen, p_lineheadbytes, FileType, p_linetrailbytes);
  846. }
  847.  
  848.  
  849.  
  850. void   DoVicarLabel (char * buf, int len, unsigned int * p_labelsize,
  851.                int * p_nline, int * p_nsamp, int * p_bitsperpix,
  852.                int * p_reclen, int * p_lineheadbytes, int * p_linetrailbytes)
  853.  
  854. /***  DoVicarLabel interprets the Vicar label in the buffer to extract the
  855.       image information.
  856.  
  857.    Parameter     Type    In/out  Description
  858.     buf         char ptr   in    The label buffer
  859.     len           int      in    The length in bytes of the label buffer
  860.   p_labelsize  uns int ptr out   The size of the label or header to skip
  861.   p_nline        int ptr   out   The number of lines in the image
  862.   p_nsamp        int ptr   out   The number of samples in the image
  863.   p_bitsperpix   int ptr   out   The pixel format (16,8,4,1)
  864.   p_reclen       int ptr   out   The record length in bytes
  865. p_lineheadbytes  int ptr   out   The header bytes at beginning of each line
  866. p_linetrailbytes int ptr   in    The number of bytes at the end of each line
  867.  
  868. ***/
  869. {
  870.     char   format[16];
  871.     int    flag;
  872.  
  873.     GetLabelInteger (buf, len, "LBLSIZE", 0, p_labelsize, &flag);
  874.     GetLabelInteger (buf, len, "NL", 1, p_nline, &flag);
  875.     GetLabelInteger (buf, len, "NS", 1, p_nsamp, &flag);
  876.     GetLabelString (buf, len, "FORMAT", "BYTE", format, &flag);
  877.     if (format[0] == 'H')
  878.         *p_bitsperpix = 16;
  879.     else
  880.         *p_bitsperpix = 8;
  881.  
  882.     *p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
  883.     GetLabelInteger (buf, len, "RECSIZE", *p_reclen, p_reclen, &flag);
  884.     *p_lineheadbytes = 0;
  885.  
  886.     free(p_linetrailbytes);
  887. }
  888.  
  889.  
  890. void   DoFITSLabel (char * buf, int len, unsigned int * p_labelsize, int * p_nline,
  891.                int * p_nsamp, int * p_bitsperpix, int * p_reclen,
  892.                int * p_lineheadbytes, int * p_linetrailbytes, int unit)
  893.  
  894. /***  DoFITSLabel interprets the FITS label in the buffer to extract the
  895.       image information.
  896.  
  897.    Parameter     Type    In/out  Description
  898.     buf         char ptr   in    The label buffer
  899.     len           int      in    The length in bytes of the label buffer
  900.   p_labelsize  uns int ptr out   The size of the label or header to skip
  901.   p_nline        int ptr   out   The number of lines in the image
  902.   p_nsamp        int ptr   out   The number of samples in the image
  903.   p_bitsperpix   int ptr   out   The pixel format (16,8,4,1)
  904.   p_reclen       int ptr   out   The record length in bytes
  905. p_lineheadbytes  int ptr   out   The header bytes at beginning of each line
  906. p_linetrailbytes int ptr   in    The number of bytes at the end of each line
  907.    unit          int       in    The number to the current file being
  908.                                     processed
  909.  
  910. ***/
  911. {
  912.  
  913.     int    flag, naxis, bytecount, i;
  914.     char   line[81];
  915.  
  916.     ByteSwap = TRUE;
  917.     GetLabelInteger (buf, len, "NAXIS",0, &naxis, &flag);
  918.     if (naxis == 1)
  919.     {
  920.         GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
  921.         GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
  922.         *p_nline = 1;
  923.     }
  924.     else if (naxis == 2)
  925.     {
  926.         GetLabelInteger (buf, len, "NAXIS1", 1, p_nsamp, &flag);
  927.         GetLabelInteger (buf, len, "NAXIS2", 1, p_nline, &flag);
  928.         GetLabelInteger (buf, len, "BITPIX", 16,p_bitsperpix, &flag);
  929.     }
  930.     else /* naxis 0 or greater than 2, can't display */
  931.     {
  932.         *p_nline      = 0;
  933.         *p_nsamp      = 0;
  934.         *p_bitsperpix = 16;
  935.         ClearDisplay(0);
  936.         StatusLine(0,"No lines in image, or too many axes.");
  937.     }
  938.  
  939.     *p_reclen = ((long)*p_nsamp*(long)*p_bitsperpix - 1)/8 + 1;
  940.     *p_lineheadbytes = 0;
  941.  
  942. /** added by DIN /revised - mdm/ to read END in FITS header **/
  943.  
  944.     bytecount = 0;
  945.     i = 0; /* changed from i= 1 to i= 0 to handle END when it is in the
  946.               last line of label. AEE, 4-4-89 */
  947.  
  948. /*    labels = fdopen(FCB[unit].handle,"r"); */
  949.     lseek(FCB[unit].handle,0L,SEEK_SET);
  950.     do
  951.     {
  952.         read(FCB[unit].handle,line,80);
  953.         ++i;
  954.     } while (strnicmp(line,"END",3) != 0);
  955.  
  956.     bytecount = i*80;
  957.     i         = bytecount/2880;
  958.     if (bytecount % 2880 != 0) i++;
  959.     *p_labelsize = i*2880;
  960.     free(p_linetrailbytes);
  961. }
  962.  
  963.  
  964. void   DoNoLabel(char * buf, int len, unsigned int * p_labelsize,
  965.                int * p_nline, int * p_nsamp, int * p_bitsperpix,
  966.                int * p_reclen, int * p_lineheadbytes, char * FileType,
  967.                int * p_linetrailbytes)
  968.  
  969. /***  DoNoLabel prompts the user to enter the image parameters.
  970.       PDS-format compressed images are handled (skipped) here, too.
  971.  
  972.    Parameter     Type    In/out  Description
  973.     buf         char ptr   in    The label buffer
  974.     len           int      in    The length in bytes of the label buffer
  975.   p_labelsize  uns int ptr out   The size of the label or header to skip
  976.   p_nline        int ptr   out   The number of lines in the image
  977.   p_nsamp        int ptr   out   The number of samples in the image
  978.   p_bitsperpix   int ptr   out   The pixel format (16,8,4,1)
  979.   p_reclen       int ptr   out   The record length in bytes
  980. p_lineheadbytes  int ptr   out   The header bytes at beginning of each line
  981.    FileType      char ptr  in    The type of the file of data (from FILE_TYPE)
  982. p_linetrailbytes int ptr   in    The number of bytes at the end of each line
  983.  
  984. ***/
  985. {
  986.  
  987.     int    flag;
  988.     char   inpstr[32];
  989.  
  990.     *p_bitsperpix = 8;
  991.     *p_labelsize = 0;
  992.  
  993.     if (OneScreen)
  994.     {
  995.         ClearDisplay(0);
  996.         TextLine = TextHeight + 5;  TextSample = 1;
  997.         TypeText ("Image file does not have a proper label.");
  998.         TextLine += TextHeight + 5;  TextSample = 1;
  999.         TypeText ("Input number of lines : ");
  1000.         AcceptText (inpstr);
  1001.         sscanf (inpstr, "%d", p_nline);
  1002.         TextLine += TextHeight + 5;  TextSample = 1;
  1003.         TypeText ("Input number of samples : ");
  1004.         AcceptText (inpstr);
  1005.         sscanf (inpstr, "%d", p_nsamp);
  1006.         TextLine += TextHeight + 5;  TextSample = 1;
  1007.         TypeText ("Input bits per pixel : ");
  1008.         AcceptText (inpstr);
  1009.         sscanf (inpstr, "%d", p_bitsperpix);
  1010.         TextLine += TextHeight + 5;  TextSample = 1;
  1011.         TypeText ("Input number of header bytes : ");
  1012.         AcceptText (inpstr);
  1013.         sscanf (inpstr, "%d", p_labelsize);
  1014.         ClearDisplay(0);
  1015.     }
  1016.     else
  1017.     {
  1018.         printf ("%s", "Image file does not have a proper label.\n");
  1019.         printf ("%s", "Input number of lines : ");
  1020.         gets (inpstr);
  1021.         sscanf (inpstr, "%d", p_nline);
  1022.  
  1023.         printf ("%s", "Input number of samples : ");
  1024.         gets (inpstr);
  1025.         sscanf (inpstr, "%d", p_nsamp);
  1026.  
  1027.         printf ("%s", "Input bits per pixel : ");
  1028.         gets (inpstr);
  1029.         sscanf (inpstr, "%d", p_bitsperpix);
  1030.  
  1031.         printf ("%s", "Input number of header bytes : ");
  1032.         gets (inpstr);
  1033.         sscanf (inpstr, "%d", p_labelsize);
  1034.     }
  1035.     *p_reclen = ( (long)*p_nsamp*(long)*p_bitsperpix - 1 )/8 + 1;
  1036.     *p_lineheadbytes = 0;
  1037.  
  1038.     free(p_linetrailbytes);
  1039. }
  1040.  
  1041. int PutLabel (char * LabelBuf, int * p_pos, char * LabelString)
  1042.  
  1043. {
  1044.     int   i;
  1045.  
  1046.     for (i = 0; LabelString[i] > 0; i++)
  1047.         LabelBuf[(*p_pos)++] = LabelString[i];
  1048.     LabelBuf[(*p_pos)++] = 13;
  1049.     LabelBuf[(*p_pos)++] = 10;
  1050. }
  1051.  
  1052.  
  1053. int PutLabelValue (char * LabelBuf, int * p_pos, char * LabelString, int value)
  1054.  
  1055. {
  1056.     char     temp1[128], temp2[32];
  1057.  
  1058.     strcpy (temp1, LabelString);
  1059.     sprintf (temp2, "%d", value);
  1060.     strcat (temp1, temp2);
  1061.     PutLabel (LabelBuf, p_pos, temp1);
  1062. }
  1063.  
  1064.  
  1065.  
  1066.  
  1067. int MakeLabel (char * Buf, int * p_labelsize, int nline, int nsamp,
  1068.                int bitsperpix, int reclen)
  1069.  
  1070. /***  MakeLabel creates a PDS label in the buffer, having the
  1071.       appropriate image parameters.
  1072.  
  1073.    Parameter    Type   In/out  Description
  1074.     Buf        char ptr  out   The label buffer
  1075.   p_labelsize  int ptr   out   The size of the label
  1076.     nline       int      out   The number of lines in the image
  1077.     nsamp       int      out   The number of samples in the image
  1078.    bitsperpix   int      out   The pixel format (16,8,4,1)
  1079.     reclen      int      out   The record length in bytes
  1080. ***/
  1081.  
  1082. {
  1083.     int       j, labelrecs, totalrecs;
  1084.     long int  filesize;
  1085.     char      labstr[128];
  1086.  
  1087.  
  1088.     labelrecs = (2047/reclen) + 1;
  1089.     *p_labelsize = reclen*labelrecs;
  1090.     totalrecs = nline + labelrecs;
  1091.     filesize = (long)reclen*(long)totalrecs - 20;
  1092.  
  1093.     j = 0;
  1094.  
  1095.     sprintf (labstr,   "NJPL1I00PDS1%08ld            = SFDU_LABEL",
  1096.                                                                   filesize);
  1097.     PutLabel (Buf, &j, labstr);
  1098.     PutLabel (Buf, &j, "RECORD_TYPE                     = FIXED_LENGTH");
  1099.     PutLabelValue (Buf, &j, "RECORD_BYTES                    = ", reclen);
  1100.     PutLabelValue (Buf, &j, "FILE_RECORDS                    = ", totalrecs);
  1101.     PutLabelValue (Buf, &j, "LABEL_RECORDS                   = ", labelrecs);
  1102.     PutLabelValue (Buf, &j, "^IMAGE                          = ", labelrecs+1);
  1103.     PutLabel (Buf, &j,      "OBJECT                          = IMAGE");
  1104.     PutLabelValue (Buf, &j, "  LINES                         = ", nline);
  1105.     PutLabelValue (Buf, &j, "  LINE_SAMPLES                  = ", nsamp);
  1106.     PutLabelValue (Buf, &j, "  SAMPLE_BITS                   = ", bitsperpix);
  1107.     PutLabel (Buf, &j, "END_OBJECT");
  1108.     PutLabel (Buf, &j, "END");
  1109.  
  1110.     while (j < *p_labelsize)
  1111.         Buf[j++] = ' ';
  1112. }
  1113.  
  1114.  
  1115. int GetCommand (char * CommandString, char * CommandList)
  1116.  
  1117. /***  GetCommand is an integer function that returns the number of the
  1118.       command that is in the beginning of the CommandString parameter.
  1119.       The command is assigned a number according to the list of commands
  1120.       in the CommandList string parameter.  The list of commands should
  1121.       be in uppercase and have spaces between the commmand.
  1122.  
  1123. ***/
  1124.  
  1125. {
  1126.     int i, j, k, l, n;
  1127.     char    List[NCOMMANDS][32],  Word[32], temp[32]; /* Max # of commands */
  1128.  
  1129.    /* Parse the command list string up into a list of strings */
  1130.     n = 0;
  1131.     l = strlen(CommandList) - 1;
  1132.     k = 0;
  1133.     do
  1134.     {
  1135.        j = k;
  1136.        while ( (CommandList[j] == ' ') && (j < l) )
  1137.             j++;
  1138.        if (j == l)  break;
  1139.        k = j;
  1140.        do
  1141.             k++;
  1142.        while ( (CommandList[k] != ' ') && (k<=l) );
  1143.        strncpy (List[n], &CommandList[j], k-j);
  1144.        List[n][k-j] = 0;
  1145.        n++;
  1146.     } while (k <= l);
  1147.  
  1148.  
  1149.     /* Get the first word from the command string */
  1150.     l = strlen(CommandString);
  1151.     j = 0;
  1152.     while ( (CommandString[j] == ' ') && (j < l) )
  1153.         j++;
  1154.     k = j;
  1155.     do
  1156.         k++;
  1157.     while ( (CommandString[k] != ' ') && (k<=l) );
  1158.     strncpy (Word, &CommandString[j], k-j);
  1159.     Word[k-j] = 0;
  1160.  
  1161.           /* Search through the list of commands to find a match */
  1162.     i = n-1;
  1163.     do
  1164.     {
  1165.         strcpy (temp, Word);
  1166.         temp[strlen(List[i])] = 0;
  1167.     } while ( (stricmp(temp, List[i]) != 0) && (--i >= 0) );
  1168.  
  1169.     return (i+1);
  1170. }
  1171.  
  1172. int ExtractKeywordString (char * CommandString, char * Keyword, int * p_flag,
  1173.                           char * value)
  1174.  
  1175. /*  ExtractKeywordString extracts the string following the keyword from
  1176.     the CommandString.
  1177.     flag=-1 if there is no keyword match.
  1178.     flag= 0 if keyword with no value.
  1179.     flag= 1 if keyword and value string.
  1180.  
  1181. */
  1182.  
  1183. {
  1184.     int i, j, k, l;
  1185.     char  *searchptr;
  1186.  
  1187.  
  1188.     /* Find where the keyword is in the buffer */
  1189.     searchptr = strstr (CommandString, Keyword);
  1190.     j = searchptr - CommandString;
  1191.  
  1192.     if ( (searchptr == NULL) || ((j > 0) && (CommandString[j-1] > ' ')) )
  1193.        *p_flag = -1;
  1194.     else
  1195.     {
  1196.         *p_flag = 1;
  1197.         l = strlen(CommandString) - 1;
  1198.         do         /* scan past the keyword */
  1199.             j++;
  1200.         while ( (CommandString[j] != ' ') &&
  1201.                 (CommandString[j] != '=') && (j != l) );
  1202.  
  1203.         if (j == l)  *p_flag = 0;
  1204.  
  1205.         do        /* scan to the next non blank and non equal char */
  1206.             j++;
  1207.         while ( ( (CommandString[j] <= ' ') ||
  1208.                   (CommandString[j] == '=') )   && (j < l) );
  1209.  
  1210.  
  1211.         k = j;
  1212.         if (CommandString[j] == '"')
  1213.         {
  1214.             j++;     /* if value begins with a quote, read to next quote */
  1215.             do
  1216.                 k++;
  1217.             while ( (CommandString[k] != '"') && (k <= l) );
  1218.         }
  1219.         else
  1220.         {
  1221.             do               /* scan past the value to next space */
  1222.                 k++;
  1223.             while ( (CommandString[k] > ' ') && (k <= l) );
  1224.         }
  1225.  
  1226.         strncpy (value, CommandString+j, k-j);
  1227.         value[k-j] = 0;
  1228.     }
  1229.  
  1230. }
  1231.  
  1232. int GetKeywordInteger (char * CommandString, char * Keyword, int defaul,
  1233.                        int * p_value, int * p_flag)
  1234.  
  1235. /***  GetKeywordInteger scans the CommandString for the Keyword and
  1236.       returns the value of the following integer.
  1237.  
  1238.    Parameter       Type      In/out   Description
  1239.   CommandString   char ptr    in      The command string to scan thru
  1240.    Keyword        char ptr    in      The keyword string to match
  1241.    defaul          int        in      The default value to return if
  1242.                                            no keyword or illegal value
  1243.    p_value        int ptr     out     The returned value
  1244.    p_flag         int ptr     out     The flag:  -1 for no keyword match
  1245.                                            0 for keyword with no/bad value
  1246.                                            1 for keyword and good value
  1247.  
  1248. ***/
  1249.  
  1250. {
  1251.     char   stringvalue[80];
  1252.  
  1253.     ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
  1254.     if (*p_flag == 1)
  1255.     {
  1256.         if (sscanf (stringvalue, "%d", p_value) == 0)
  1257.         {
  1258.             *p_flag = 0;
  1259.             *p_value = defaul;
  1260.         }
  1261.     }
  1262.     else
  1263.     {
  1264.         *p_value = defaul;
  1265.     }
  1266. }
  1267.  
  1268. int GetKeywordLong (char * CommandString, char * Keyword, long defaul,
  1269.                        long * p_value, int * p_flag)
  1270.  
  1271. /***  GetKeywordInteger scans the CommandString for the Keyword and
  1272.       returns the value of the following integer.
  1273.  
  1274.    Parameter       Type      In/out   Description
  1275.   CommandString   char ptr    in      The command string to scan thru
  1276.    Keyword        char ptr    in      The keyword string to match
  1277.    defaul          long       in      The default value to return if
  1278.                                            no keyword or illegal value
  1279.    p_value       long ptr     out     The returned value
  1280.    p_flag         int ptr     out     The flag:  -1 for no keyword match
  1281.                                            0 for keyword with no/bad value
  1282.                                            1 for keyword and good value
  1283.  
  1284. ***/
  1285.  
  1286. {
  1287.     char   stringvalue[80];
  1288.  
  1289.     ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
  1290.     if (*p_flag == 1)
  1291.     {
  1292.         if (sscanf (stringvalue, "%ld", p_value) == 0)
  1293.         {
  1294.             *p_flag = 0;
  1295.             *p_value = defaul;
  1296.         }
  1297.     }
  1298.     else
  1299.     {
  1300.         *p_value = defaul;
  1301.     }
  1302. }
  1303.  
  1304. int GetKeywordReal (char * CommandString, char * Keyword, float defaul,
  1305.                     float * p_value, int * p_flag)
  1306.  
  1307. /***  GetKeywordReal scans the CommandString for the Keyword and returns
  1308.       the value of the following real.
  1309.  
  1310.    Parameter       Type      In/out   Description
  1311.  CommandString    char ptr    in      The command string to scan thru
  1312.    Keyword        char ptr    in      The keyword string to match
  1313.    defaul          float      in      The default value to return if
  1314.                                            no keyword or illegal value
  1315.    p_value        float ptr   out     The returned value
  1316.    p_flag         int ptr     out     The flag:  -1 for no keyword match
  1317.                                            0 for keyword with no/bad value
  1318.                                            1 for keyword and good value
  1319.  
  1320. ***/
  1321. {
  1322.     char   stringvalue[80];
  1323.  
  1324.     ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
  1325.     if (*p_flag == 1)
  1326.     {
  1327.         if (sscanf (stringvalue, "%f", p_value) == 0)
  1328.         {
  1329.             *p_flag = 0;
  1330.             *p_value = defaul;
  1331.         }
  1332.     }
  1333.     else
  1334.     {
  1335.         *p_value = defaul;
  1336.     }
  1337. }
  1338.  
  1339. int GetKeywordString (char * CommandString, char * Keyword, char * defaul,
  1340.                       char * value, int * p_flag)
  1341.  
  1342. /***  GetKeywordString scans the CommandString for the Keyword
  1343.     and returns the value of the following string.
  1344.  
  1345.    Parameter       Type      In/out   Description
  1346.     CommandString  char ptr    in      The command string to scan thru
  1347.     Keyword        char ptr    in      The keyword string to match
  1348.     defaul         char ptr    in      The default value to return if
  1349.                                            no keyword or illegal value
  1350.     value          char ptr    out     The returned value
  1351.     p_flag         int ptr     out     The flag:  -1 for no keyword match
  1352.                                            0 for keyword with no/bad value
  1353.                                            1 for keyword and good value
  1354.  
  1355. ***/
  1356.  
  1357. {
  1358.     char   stringvalue[255];
  1359.  
  1360.     ExtractKeywordString (CommandString, Keyword, p_flag, stringvalue);
  1361.     if (*p_flag == 1)
  1362.     {
  1363.         strcpy (value, stringvalue);
  1364.     }
  1365.     else
  1366.     {
  1367.         strcpy (value, defaul);
  1368.     }
  1369. }
  1370.  
  1371.  
  1372. int GetKeywordSubcommand (char * CommandString, char * Keyword, int * p_flag)
  1373.  
  1374. /***  GetKeywordSubcommand scans the CommandString for the Keyword
  1375.     and returns the value of the following string.
  1376.  
  1377.    Parameter       Type      In/out   Description
  1378.     CommandString  char ptr    in      The command string to scan thru
  1379.     Keyword        char ptr    in      The keyword string to match
  1380.     p_flag         int ptr     out     The flag:  -1 for no keyword match
  1381.                                                    1 for keyword found
  1382.  
  1383. ***/
  1384.  
  1385. {
  1386.     int   j;
  1387.     char  *searchptr;
  1388.  
  1389.  
  1390.     /* Find where the keyword is in the buffer */
  1391.     searchptr = strstr (CommandString, Keyword);
  1392.     j = searchptr - CommandString;
  1393.  
  1394.     if ( (searchptr == NULL) || ((j > 0) && (CommandString[j-1] > ' ')) )
  1395.        *p_flag = -1;
  1396.     else
  1397.     {
  1398.        *p_flag = 1;
  1399.     }
  1400. }
  1401.