home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / imdisp / source / browse.c < prev    next >
C/C++ Source or Header  |  1991-01-04  |  12KB  |  336 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.  
  8. /* * * * INCLUDE files * * * */
  9.  
  10.  
  11. #include <conio.h>
  12. #include <ctype.h>
  13. #include <direct.h>
  14. #include <dos.h>
  15. #include <direct.h>
  16. #include <io.h>
  17. #include <malloc.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "imdef.h"
  22. #include "imdisp.h"
  23. #include "dispio.h"
  24. #include "disputil.h"
  25. #include "fileio.h"
  26. #include "refresh.h"
  27.  
  28. /* * * * External functions * * * */
  29.  
  30. /* * * * Function declarations * * * */
  31.  
  32. int DoBrowse (void);
  33. int Browse (void);
  34. int Add_Browse (char *);
  35.  
  36. /**************************************************************************/
  37. /* These variables are purposely placed here to make them global to the   */
  38. /* browse routines, but not seen by any other routines.  Since the browse */
  39. /* routine is called recursively, and recursive routines make heavy use   */
  40. /* of the stack, there variables were made global so that the chances for */
  41. /* of a stack overflow are reduced.         Ron Baalke   10/30/90         */
  42. /**************************************************************************/
  43.  
  44. #define PROTECT 60     /* # lines at the bottom of the screen to be       */
  45.                        /* protected from being overwritten by the images  */
  46.  
  47. FILE    *fp;
  48. char    mask[80], directory[80];
  49. int     i, file_number, sub;
  50. int     set_flag, all_flag, pause_flag;
  51. int     dsl, dss, size, pics, across, down, labflag;
  52. char    *loc;
  53.  
  54. /*************************************************************************/
  55. /* DoBrowse                                                              */
  56. /*                                                                       */
  57. /* Modified extensively by Ron Baalke   10/30/90                         */
  58. /*                                                                       */
  59. /* This routine will execute the BROWSE command by building a            */
  60. /* BROWSE.CMD batch file on the user's disk (normally the C: drive), and */
  61. /* then executing the batch file.  The BROWSE command is normally used   */
  62. /* with wildcards, and if the ALL option is specified, then the all of   */
  63. /* the subdirectories are also traversed.  The Browse() routine is       */
  64. /* called recursively in such a case.                                    */
  65. /*                                                                       */
  66. /* Four new options have been added to the BROWSE command:  ALL, PAUSE   */
  67. /* SELECT and FILE.                                                      */
  68. /*                                                                       */
  69. /* Note: some of the processing for the SELECT option is done in the     */
  70. /*       DisplayImage() routine in DISPLAY.C                             */
  71. /*************************************************************************/
  72.  
  73. int DoBrowse(void)
  74. {
  75.     FILE          *fp1;
  76.     char          filename[80];
  77.     char          label[80];
  78.     int           i, flag;
  79.     int           end;
  80.     int           select_flag;
  81.     int           file_flag;
  82.     int           done;
  83.     int           protect;  /* # lines at the bottom of the screen to be     */
  84.                             /* protected from being overwritten by the images*/
  85.  
  86.     file_number = 0;
  87.     set_flag = 0;
  88.     labflag = 1;
  89.     protect = TextHeight * 4;
  90.  
  91.     /* Open the browse file */
  92.  
  93.     fp     =  fopen (BrowseName,"w");
  94.     if (fp == NULL)
  95.     {
  96.        StatusLine(0,"File can not be opened. Use SET BROWSE filename");
  97.        return;
  98.     }
  99.  
  100.     /* Retrieve all the options the user typed in */
  101.  
  102.     GetKeywordInteger(CommandString, "SIZ", dispns, &size, &flag);
  103.     GetKeywordInteger(CommandString, "SUB",  1, &sub,  &flag);
  104.     GetKeywordString (CommandString, "NOL", "",label, &labflag);
  105.     GetKeywordString (CommandString, "BRO", "*.*",filename,&flag);
  106.     GetKeywordString (CommandString, "ALL", "",label, &all_flag);
  107.     GetKeywordString (CommandString, "PAU", "",label, &pause_flag);
  108.     GetKeywordString (CommandString, "SEL", "",label, &select_flag);
  109.     GetKeywordString (CommandString, "FIL", "",label, &file_flag);
  110.     GetKeywordInteger(CommandString, "LO", DNlow , &DNlow, &flag);
  111.     if (flag < 0)
  112.        GetKeywordInteger(CommandString, "DNL", DNlow , &DNlow, &flag);
  113.     if (flag > 0)
  114.        set_flag = 1;
  115.     GetKeywordInteger(CommandString, "HI", DNhigh , &DNhigh, &flag);
  116.     if (flag < 0)
  117.        GetKeywordInteger(CommandString, "DNH", DNhigh , &DNhigh, &flag);
  118.     if (flag > 0)
  119.        set_flag = 1;
  120.  
  121.     /* make sure parameters don't get interpreted as a filemask. */
  122.  
  123.     if (strncmp(filename,"SIZ",3) == 0 || strncmp(filename,"SUB", 3) == 0 ||
  124.         strncmp(filename,"LO", 2) == 0 || strncmp(filename,"DNLO",4) == 0 ||
  125.         strncmp(filename,"HI", 2) == 0 || strncmp(filename,"DNHI",4) == 0 ||
  126.         strncmp(filename,"NOL",3) == 0 || strncmp(filename,"ALL", 3) == 0 ||
  127.         strncmp(filename,"PAU",3) == 0 || strncmp(filename,"SEL", 3) == 0 ||
  128.         strncmp(filename,"FIL",3) == 0)
  129.            strcpy(filename,"*.*");
  130.  
  131.     /* This might take a while, so tell the user to wait */
  132.     ClearDisplay(0);
  133.     StatusLine(0,"Please Wait, Building Browse File......");
  134.  
  135.     /* If SELECT option selected, then put SELECT into browse file */
  136.  
  137.     if (select_flag != -1)
  138.        fprintf(fp,"SELECT\n");
  139.  
  140.     /* Calculate the variables used for the placement of the images
  141.        on the screen   */
  142.  
  143.     across     = dispns/size;
  144.     if (across == 0)
  145.        across = 1;
  146.     down       = (dispnl-protect)/size;
  147.     if (down   == 0)
  148.        down   = 1;
  149.     pics       = across * down;
  150.  
  151.     /* If FILE option is selected, then attempt to open the file
  152.        specified by the user              */
  153.  
  154.     if (file_flag == 1)
  155.     {
  156.        fp1     =  fopen(label,"r");
  157.        if (fp1 == NULL)
  158.        {
  159.           StatusLine(0,"File can not be opened.");
  160.           fclose(fp);
  161.           return;
  162.        }
  163.     }
  164.  
  165.     done = FALSE;
  166.     while (!done)
  167.     {
  168.        /* If FILE option, read in the filename */
  169.  
  170.        if (file_flag == 1)
  171.           fscanf(fp1,"%s",filename);
  172.  
  173.        /* Attempt to move to the directory where the file resides */
  174.  
  175.        flag = ChangeDir(filename);
  176.        if (flag != 0)
  177.        {
  178.           StatusLine(0,"Bad Pathname, Try Again");
  179.           fclose(fp);
  180.           if (file_flag == 1) fclose(fp1);
  181.           return;
  182.        }
  183.  
  184.        /* Extract out the filename portion to use as the mask */
  185.  
  186.        strcpy(mask,filename);
  187.        end = strlen(filename) - 1;
  188.        for (i=end; i > 0; i--)
  189.        {
  190.           if ((strnicmp(&filename[i],"\\",1) == 0) ||
  191.              (strnicmp(&filename[i],":",1) == 0))
  192.           {
  193.              strncpy(&mask[0],&filename[i+1],end-i);
  194.              mask[end-i] = '\0';
  195.              break;
  196.           }
  197.        }
  198.  
  199.        /* Find all the files matching the mask. Browse() is a recursive routine*/
  200.  
  201.        Browse();
  202.  
  203.        if ((file_flag != 1) ||
  204.            ((file_flag == 1) && (feof(fp1)))) done = TRUE;
  205.  
  206.     } /* end while loop */
  207.  
  208.     /* If no files are found, then tell the user and exit */
  209.  
  210.     if (file_number == 0)
  211.     {
  212.        StatusLine(0,"No files found for browse file specification.");
  213.        fclose(fp);
  214.        if (file_flag == 1) fclose(fp1);
  215.        return;
  216.     }
  217.  
  218.     /* Close batch file and then execute it */
  219.  
  220.     fclose(fp);
  221.     if (file_flag == 1) fclose(fp1);
  222.     strcpy(CommandString,"BATCH ");
  223.     strcat(CommandString,BrowseName);
  224.     ClearDisplay(0);
  225.     DoBatch();
  226. }
  227.  
  228. /***************************************************************************/
  229. /* Browse                                                                  */
  230. /*                                                                         */
  231. /* Written by Ron Baalke   10/30/90                                        */
  232. /*                                                                         */
  233. /* This routine is a recursive routine that will find all of the files     */
  234. /* matching the mask and then write them out to the browse batch file.     */
  235. /* The recursive part is used to traverse through all of the               */
  236. /* subdirectories if the ALL option is selected.  The _dos_findfirst &     */
  237. /* _dos_findnext routines are used to find the files matching the mask,    */
  238. /* and are also used to find the subdirectories.                           */
  239. /***************************************************************************/
  240. int Browse()
  241. {
  242.     struct find_t    fileinfo;
  243.  
  244.     /* Find the first file matching the mask in the current directory */
  245.  
  246.     if (!_dos_findfirst(mask, 0, &fileinfo))
  247.     {
  248.        getcwd(directory,80);
  249.        StatusLine(1,directory);
  250.        fprintf(fp,"text line %d samp 1 \'%-s\'\n",
  251.                dispnl-(TextHeight+12),directory);
  252.        Add_Browse(fileinfo.name);             /* Add to browse batch file */
  253.  
  254.        while (!_dos_findnext(&fileinfo))      /* Find the remaining files */
  255.        {
  256.           Add_Browse(fileinfo.name);          /* Add them, too            */
  257.        }
  258.     }
  259.  
  260.     /* If the ALL option is selected, then parse through all of the  */
  261.     /* subdirectories to find more files matching the mask           */
  262.  
  263.     if (all_flag != -1)     /* If ALL option selected */
  264.     {
  265.        if (!_dos_findfirst("*.*", 0x10, &fileinfo))
  266.        {
  267.           if (fileinfo.attrib & _A_SUBDIR) /* Check if file is subdirectory */
  268.           {
  269.              if (fileinfo.name[0] != '.')  /* Skip current & parent directory*/
  270.              {
  271.                 chdir(fileinfo.name);  /* Cd into directory */
  272.                 Browse();              /* Look for files in this subdirectory*/
  273.                 chdir("..");           /* Move back up to previous directory */
  274.              }
  275.           }
  276.  
  277.           /* Do same thing for the other subdirectories */
  278.  
  279.           while (!_dos_findnext(&fileinfo))
  280.           {
  281.              if (fileinfo.attrib & _A_SUBDIR)
  282.              {
  283.                 if (fileinfo.name[0] != '.')
  284.                 {
  285.                    chdir(fileinfo.name);
  286.                    Browse();
  287.                    chdir("..");
  288.                 }
  289.              }
  290.           }
  291.        }
  292.     }
  293. }
  294.  
  295. /****************************************************************************/
  296. /* Add_Browse                                                               */
  297. /*                                                                          */
  298. /* Written by Ron Baalke         10/30/90                                   */
  299. /*                                                                          */
  300. /* This routine will write out the filename to the browse batch file that   */
  301. /* the Browse() routine found.  It will also determine where the image      */
  302. /* will appear on the screen, dependent on the screen size and the options  */
  303. /* the user entered in with the BROWSE command.                             */
  304. /****************************************************************************/
  305. int Add_Browse( char *file )
  306. {
  307.    /* Calculate where image will appear on the screen */
  308.  
  309.    dsl = 1+((file_number%pics)/across)*size;
  310.    dss = 1+(file_number%across)*size;
  311.  
  312.    /* Process PAUSE option */
  313.  
  314.    if ((pause_flag != -1) && (dsl == 1) && (dss == 1)  && (file_number > 0))
  315.       fprintf(fp,"pause\n");
  316.  
  317.    /* Add FILE and DISP commands to the browse file */
  318.    if (strlen(directory) == 3) /* root dir, get rid of \ */
  319.      directory[2] = '\0';
  320.    fprintf(fp,"file %s\\%s\n",directory,file);
  321.    if (set_flag)
  322.       fprintf(fp,"set dnlo %d dnhi %d\n",DNlow,DNhigh);
  323.    fprintf(fp,"disp dsl %d dss %d sub %d\n",dsl,dss,sub);
  324.  
  325.    /* Put image filename on image */
  326.  
  327.    if (labflag == -1) /* put image file name on image */
  328.    {
  329.       loc = strstr(file,".");
  330.       if (loc != NULL)
  331.       *loc = '\0'; /* discard extension */
  332.       fprintf(fp,"text line %d samp %d \'%s\'\n",dsl+25,dss,file);
  333.    }
  334.    file_number++;
  335. }
  336.