home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / imdisp79.zip / BROWSE.C < prev    next >
C/C++ Source or Header  |  1993-02-15  |  15KB  |  413 lines

  1. /***  IMDISP module BROWSE.C
  2.  
  3.     BROWSE.C contains all the routines for browsing images.
  4.     Written by Ron Baalke - 10/30/90.
  5.  
  6. ***/
  7.  
  8. #define __MSC
  9.  
  10. /* * * * INCLUDE files * * * */
  11.  
  12. #include <direct.h>
  13. #include <dos.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "imdef.h"
  17. #include "imdisp.h"
  18. #include "dispio.h"
  19. #include "fileutil.h"
  20. #include "keywutil.h"
  21. #include "textutil.h"
  22. #include "buffer.h"
  23.  
  24. /* * * * External functions * * * */
  25.  
  26. /* * * * Function declarations * * * */
  27.  
  28. int DoBrowse (void);
  29. int Browse (void);
  30. int Add_Browse (char *);
  31. void Browse_Buffers(void);
  32.  
  33. /**************************************************************************/
  34. /* These variables are purposely placed here to make them global to the   */
  35. /* browse routines, but not seen by any other routines.  Since the browse */
  36. /* routine is called recursively, and recursive routines make heavy use   */
  37. /* of the stack, there variables were made global so that the chances for */
  38. /* of a stack overflow are reduced.         Ron Baalke   10/30/90         */
  39. /**************************************************************************/
  40.  
  41. #define PROTECT 60     /* # lines at the bottom of the screen to be       */
  42.                        /* protected from being overwritten by the images  */
  43.  
  44. FILE    *fp;
  45. char    mask[80], directory[80];
  46. int     i, file_number, sub;
  47. int     set_flag, all_flag, pause_flag;
  48. int     auto_amount, auto_stretch_flag;
  49. int     pause_value;
  50.  
  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.     char          dummy[40];
  79.     int           i, flag;
  80.     int           end;
  81.     int           select_flag;
  82.     int           file_flag;
  83.     int           buffer_flag;
  84.     int           done;
  85.     int           protect;  /* # lines at the bottom of the screen to be     */
  86.                             /* protected from being overwritten by the images*/
  87.  
  88.     file_number = 0;
  89.     set_flag = 0;
  90.     labflag = 1;
  91.     protect = TextHeight * 4;
  92.  
  93.     /* Open the browse file */
  94.  
  95.     fp     =  fopen (BrowseName,"w");
  96.     if (fp == NULL)
  97.     {
  98.        StatusLine(0,"File can not be opened. Use SET BROWSE filename");
  99.        return(1);
  100.     }
  101.  
  102.     /* Retrieve all the options the user typed in */
  103.  
  104.     GetKeywordInteger(CommandString, "SIZ", dispns, &size, &flag);
  105.     GetKeywordInteger(CommandString, "SUB",  1, &sub,  &flag);
  106.     GetKeywordString (CommandString, "NOL", "",dummy, &labflag);
  107.     GetKeywordString (CommandString, "BRO", "*.*",filename,&flag);
  108.     GetKeywordString (CommandString, "ALL", "",dummy, &all_flag);
  109.     GetKeywordString (CommandString, "SEL", "",dummy, &select_flag);
  110.     GetKeywordString (CommandString, "BUFFERS","", dummy, &buffer_flag);
  111.     GetKeywordString (CommandString, "FIL", "",label, &file_flag);
  112.     GetKeywordInteger(CommandString, "PAU", 0, &pause_value, &pause_flag);
  113.     GetKeywordLong(CommandString, "LO", DNlow , &DNlow, &flag);
  114.     if (flag < 0)
  115.        GetKeywordLong(CommandString, "DNL", DNlow , &DNlow, &flag);
  116.     if (flag > 0)
  117.        set_flag = 1;
  118.     GetKeywordLong(CommandString, "HI", DNhigh , &DNhigh, &flag);
  119.     if (flag < 0)
  120.        GetKeywordLong(CommandString, "DNH", DNhigh , &DNhigh, &flag);
  121.     if (flag > 0)
  122.        set_flag = 1;
  123.     GetKeywordInteger (CommandString, "AUT", 5,&auto_amount, &auto_stretch_flag);
  124.  
  125.     /* make sure parameters don't get interpreted as a filemask. */
  126.  
  127.     if (strncmp(filename,"SIZ",3) == 0 || strncmp(filename,"SUB", 3) == 0 ||
  128.         strncmp(filename,"LO", 2) == 0 || strncmp(filename,"DNLO",4) == 0 ||
  129.         strncmp(filename,"HI", 2) == 0 || strncmp(filename,"DNHI",4) == 0 ||
  130.         strncmp(filename,"NOL",3) == 0 || strncmp(filename,"ALL", 3) == 0 ||
  131.         strncmp(filename,"PAU",3) == 0 || strncmp(filename,"SEL", 3) == 0 ||
  132.         strncmp(filename,"FIL",3) == 0 || strncmp(filename,"AUT", 3) == 0)
  133.         strcpy(filename,"*.*");
  134.  
  135.     /* This might take a while, so tell the user to wait */
  136.     ClearDisplay(0);
  137.     StatusLine(0,"Please Wait, Building Browse File......");
  138.  
  139.     /* Calculate the variables used for the placement of the images
  140.        on the screen   */
  141.  
  142.     across     = dispns/size;
  143.     if (across == 0)
  144.        across = 1;
  145.     down       = (dispnl-protect)/size;
  146.     if (down   == 0)
  147.        down   = 1;
  148.     pics       = across * down;
  149.  
  150.     if (buffer_flag != -1)
  151.     {
  152.        Browse_Buffers();
  153.        fclose(fp);        /* Close batch file and then execute it */
  154.        strcpy(CommandString,"BATCH ");
  155.        strcat(CommandString,BrowseName);
  156.        ClearDisplay(0);
  157.        DoBatch();
  158.        return(0);
  159.     }
  160.  
  161.     /* If SELECT option selected, then put SELECT into browse file */
  162.  
  163.     if (select_flag != -1)
  164.        fprintf(fp,"SELECT\n");
  165.  
  166.  
  167.     /* If FILE option is selected, then attempt to open the file
  168.        specified by the user              */
  169.  
  170.     if (file_flag == 1)
  171.     {
  172.        fp1     =  fopen(label,"r");
  173.        if (fp1 == NULL)
  174.        {
  175.           StatusLine(0,"File can not be opened.");
  176.           fclose(fp);
  177.           return(1);
  178.        }
  179.     }
  180.  
  181.     done = FALSE;
  182.     while (!done)
  183.     {
  184.        /* If FILE option, read in the filename */
  185.  
  186.        if (file_flag == 1)
  187.           fscanf(fp1,"%s",filename);
  188.  
  189.        /* Attempt to move to the directory where the file resides */
  190.  
  191.        flag = ChangeDir(filename);
  192.        if (flag != 0)
  193.        {
  194.           StatusLine(0,"Bad Pathname, Try Again");
  195.           fclose(fp);
  196.           if (file_flag == 1) fclose(fp1);
  197.           return(1);
  198.        }
  199.  
  200.        /* Extract out the filename portion to use as the mask */
  201.  
  202.        strcpy(mask,filename);
  203.        end = strlen(filename) - 1;
  204.        for (i=end; i > 0; i--)
  205.        {
  206.           if ((strnicmp(&filename[i],"\\",1) == 0) ||
  207.              (strnicmp(&filename[i],":",1) == 0))
  208.           {
  209.              strncpy(&mask[0],&filename[i+1],end-i);
  210.              mask[end-i] = '\0';
  211.              break;
  212.           }
  213.        }
  214.  
  215.        /* Find all the files matching the mask. Browse() is a recursive routine*/
  216.  
  217.        Browse();
  218.  
  219.        if ((file_flag != 1) ||
  220.            ((file_flag == 1) && (feof(fp1)))) done = TRUE;
  221.  
  222.     } /* end while loop */
  223.  
  224.     /* If no files are found, then tell the user and exit */
  225.  
  226.     if (file_number == 0)
  227.     {
  228.        StatusLine(0,"No files found for browse file specification.");
  229.        fclose(fp);
  230.        if (file_flag == 1) fclose(fp1);
  231.        return(1);
  232.     }
  233.  
  234.     /* Close batch file and then execute it */
  235.  
  236.     fclose(fp);
  237.     if (file_flag == 1) fclose(fp1);
  238.     strcpy(CommandString,"BATCH ");
  239.     strcat(CommandString,BrowseName);
  240.     ClearDisplay(0);
  241.     DoBatch();
  242. }
  243.  
  244. /***************************************************************************/
  245. /* Browse                                                                  */
  246. /*                                                                         */
  247. /* Written by Ron Baalke   10/30/90                                        */
  248. /*                                                                         */
  249. /* This routine is a recursive routine that will find all of the files     */
  250. /* matching the mask and then write them out to the browse batch file.     */
  251. /* The recursive part is used to traverse through all of the               */
  252. /* subdirectories if the ALL option is selected.  The _dos_findfirst &     */
  253. /* _dos_findnext routines are used to find the files matching the mask,    */
  254. /* and are also used to find the subdirectories.                           */
  255. /***************************************************************************/
  256. int Browse()
  257. {
  258.     struct find_t    fileinfo;
  259.  
  260.     /* Find the first file matching the mask in the current directory */
  261.  
  262.     if (!_dos_findfirst(mask, 0, &fileinfo))
  263.     {
  264.        getcwd(directory,80);
  265.        StatusLine(1,directory);
  266.        fprintf(fp,"text line %d samp 1 \'%-s\'\n",
  267.                dispnl-(TextHeight+12),directory);
  268.        Add_Browse(fileinfo.name);             /* Add to browse batch file */
  269.  
  270.        while (!_dos_findnext(&fileinfo))      /* Find the remaining files */
  271.        {
  272.           Add_Browse(fileinfo.name);          /* Add them, too            */
  273.        }
  274.     }
  275.  
  276.     /* If the ALL option is selected, then parse through all of the  */
  277.     /* subdirectories to find more files matching the mask           */
  278.  
  279.     if (all_flag != -1)     /* If ALL option selected */
  280.     {
  281.        if (!_dos_findfirst("*.*", 0x10, &fileinfo))
  282.        {
  283.           if (fileinfo.attrib & _A_SUBDIR) /* Check if file is subdirectory */
  284.           {
  285.              if (fileinfo.name[0] != '.')  /* Skip current & parent directory*/
  286.              {
  287.                 chdir(fileinfo.name);  /* Cd into directory */
  288.                 Browse();              /* Look for files in this subdirectory*/
  289.                 chdir("..");           /* Move back up to previous directory */
  290.              }
  291.           }
  292.  
  293.           /* Do same thing for the other subdirectories */
  294.  
  295.           while (!_dos_findnext(&fileinfo))
  296.           {
  297.              if (fileinfo.attrib & _A_SUBDIR)
  298.              {
  299.                 if (fileinfo.name[0] != '.')
  300.                 {
  301.                    chdir(fileinfo.name);
  302.                    Browse();
  303.                    chdir("..");
  304.                 }
  305.              }
  306.           }
  307.        }
  308.     }
  309. }
  310.  
  311. /****************************************************************************/
  312. /* Add_Browse                                                               */
  313. /*                                                                          */
  314. /* Written by Ron Baalke         10/30/90                                   */
  315. /*                                                                          */
  316. /* This routine will write out the filename to the browse batch file that   */
  317. /* the Browse() routine found.  It will also determine where the image      */
  318. /* will appear on the screen, dependent on the screen size and the options  */
  319. /* the user entered in with the BROWSE command.                             */
  320. /****************************************************************************/
  321. int Add_Browse( char *file )
  322. {
  323.    /* Calculate where image will appear on the screen */
  324.  
  325.    dsl = 1+((file_number%pics)/across)*size;
  326.    dss = 1+(file_number%across)*size;
  327.  
  328.    /* Process PAUSE option */
  329.  
  330.    if ((pause_flag != -1) && (dsl == 1) && (dss == 1)  && (file_number > 0))
  331.    {
  332.       if (pause_value == 0)
  333.          fprintf(fp,"pause\n");
  334.       else
  335.          fprintf(fp,"pause %d\n",pause_value);
  336.    }
  337.  
  338.    /* Add FILE and DISP commands to the browse file */
  339.    if (strlen(directory) == 3) /* root dir, get rid of \ */
  340.      directory[2] = '\0';
  341.    fprintf(fp,"file %s\\%s nom\n",directory,file);
  342.    if (set_flag)
  343.       fprintf(fp,"set dnlo %d dnhi %d\n",DNlow,DNhigh);
  344.    if (auto_stretch_flag >= 0)
  345.      fprintf(fp,"disp dsl %d dss %d sub %d auto %d\n",dsl,dss,sub,auto_amount);
  346.    else
  347.      fprintf(fp,"disp dsl %d dss %d sub %d\n",dsl,dss,sub);
  348.  
  349.    /* Put image filename on image */
  350.  
  351.    if (labflag == -1) /* put image file name on image */
  352.    {
  353.       loc = strstr(file,".");
  354.       if (loc != NULL)
  355.       *loc = '\0'; /* discard extension */
  356.       fprintf(fp,"text line %d samp %d \'%s\'\n",dsl+TextHeight*2+1,dss,file);
  357.    }
  358.    file_number++;
  359. }
  360. /****************************************************************************/
  361. /* Browse_Buffers                                                           */
  362. /*                                                                          */
  363. /* Written by Ron Baalke         06/17/91                                   */
  364. /*                                                                          */
  365. /* This routine will browse through the buffers and display any images      */
  366. /* found.  Most of the options used with the normal BROWSE command are      */
  367. /* still applicable.                                                        */
  368. /****************************************************************************/
  369.  
  370. void Browse_Buffers(void)
  371. {
  372.    int i;
  373.    char letter[2];
  374.  
  375.    for (i=0; i<MAXBUFFERS; i++)
  376.    {
  377.       if (Buffers[i].location != NOWHERE)
  378.       {
  379.          /* Calculate where image will appear on the screen */
  380.  
  381.           dsl = 1+((file_number%pics)/across)*size;
  382.           dss = 1+(file_number%across)*size;
  383.  
  384.          /* Process PAUSE option */
  385.  
  386.          if ((pause_flag != -1) && (dsl == 1) && (dss == 1)  && (file_number > 0))
  387.          {
  388.             if (pause_value == 0)
  389.                fprintf(fp,"pause\n");
  390.             else
  391.                fprintf(fp,"pause %d\n",pause_value);
  392.             fprintf(fp,"pause\n");
  393.          }
  394.  
  395.          /* Add DISP commands to the browse file */
  396.  
  397.          if (set_flag)
  398.             fprintf(fp,"set dnlo %d dnhi %d\n",DNlow,DNhigh);
  399.          strcpy(letter,"A");
  400.          letter[0] += i;
  401.          fprintf(fp,"disp %s dsl %d dss %d sub %d\n",letter,dsl,dss,sub);
  402.  
  403.          /* Put buffer name on image */
  404.  
  405.          if (labflag == -1) /* put image file name on image */
  406.          {
  407.             fprintf(fp,"text line %d samp %d \'%s\'\n",dsl+TextHeight*2+1,dss,letter);
  408.          }
  409.          file_number++;
  410.       }
  411.    }
  412. }
  413.