home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / imdisp / source / fileutil.c < prev    next >
C/C++ Source or Header  |  1990-11-20  |  16KB  |  467 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. /*  Copyright only applies to the subroutine FILEUTIL.C      */
  7. /*************************************************************/
  8.  
  9. /*** Module FILEUTIL.C 
  10.  
  11.      This module provides utility routines for browsing through
  12.      directories and selecting files for subsequent display.
  13.      It also contains public domain programs for locating files
  14.      meeting wildcard specifications, and for determining the
  15.      current default drive and path name.
  16.      
  17. ***/
  18.  
  19. /********************************************************************/
  20. /* The file selection subroutine allows the user to browse          */
  21. /* directories and select image files for display in IMDISP.        */
  22. /* Also contains subroutines wildexp and drpath, used by DoBrowse.  */ 
  23. /*                                                                  */
  24. /* Written by: Dan Nakamura, Jet Propulsion Lab, October 10, 1987   */
  25. /********************************************************************/
  26.  
  27. /* * * * INCLUDE files * * * */
  28.  
  29. #include <conio.h>
  30. #include <ctype.h>
  31. #include <direct.h>
  32. #include <dos.h>
  33. #include <errno.h>
  34. #include <malloc.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "imdef.h"
  39. #include "imdisp.h"
  40. #include "dispio.h"
  41. #include "disputil.h"
  42. #include "imageio.h"
  43. #include "labutil.h"
  44. #include "refresh.h"
  45.  
  46. /* * * * External functions * * * */
  47.  
  48. /* * * * Function declarations * * * */
  49.  
  50. int  FileSel (char *);
  51. int  GetFile (void);
  52. int  ChangeDir (char *);
  53.  
  54. /* * * * Global Variables * * * */
  55.  
  56.  
  57. int FileSel(char * selFilename)
  58.  
  59. {
  60.     union  REGS  inregs, outregs;
  61.     struct SREGS segregs;
  62.     char  *selection[MAXNUMFILES];   /* Filename selected */
  63.     char   tempFileName[80];         /* Full filename to be passed back */
  64.     char   defDir[51];               /* Directory user enters from */
  65.     char   curDir[51];               /* Path to dir with wanted filename */
  66.     char   dta[128];
  67.     char   choice[10];
  68.     char   scratchbuf[20];           /* Buf used for small manipulations */
  69.     char   pBuff[80];                /* printing Buffer */
  70.     char  *attribute, *fname;
  71.     char   subMask[4];
  72.     char   dchar;
  73.     int    defDrive, curDrive, newDrive, numDrivesInstal;
  74.     int    topList, lowList;
  75.     int    index, numResponse;
  76.     int    error, okFilename, done, initDispFirst, i;
  77.     int    isSubDir[MAXNUMFILES];
  78.     int    dummy;
  79.     int    tltemp, len;
  80.  
  81.     /** Initialize Parameters **/
  82.     strcpy( subMask, "*.*");
  83.  
  84.     if (getcwd( defDir, 50) == NULL)
  85.        perror( "Unable to register default directory" );
  86.     strcpy( curDir, defDir);                        /* Init current directory */
  87.     defDrive = 0x00FF & bdos( WHAT_DRIVE, 0, 0);    /* Init default Drive     */
  88.     numDrivesInstal = 0x00FF &                      /* Find drives            */
  89.        bdos( SEL_DRIVE, defDrive, 0);
  90.     curDrive = defDrive;                            /* Init current drive     */
  91.     done     = FALSE;
  92.     initDispFirst = TRUE;
  93.  
  94. /** Setup for Loop **/
  95.  
  96.     bdos( ALLOC_DTA, (unsigned) dta, 0);          /* Setup Disk Transfer Area */
  97.  
  98. /** Main Loop For Valid Filename **/
  99.     while( !done )
  100.     {
  101.        /** Looking for SubDirectorie Names **/
  102.        index       = 1;                           /* Set index for file array */
  103.        inregs.h.ah = SRCH_FIRST;
  104.        inregs.x.dx = (unsigned) subMask;
  105.        inregs.h.cl = 0x10;
  106.        intdos( &inregs, &outregs);
  107.        error = outregs.x.ax;
  108.  
  109.        if ( !error )
  110.        {
  111.            while( !error )
  112.            {
  113.                attribute = dta + 21;   /* Ptr to attribute sect of DTA */
  114.                fname     = dta + 30;   /* Ptr to filename sect of DTA */
  115.                if ( (*attribute & 0x10) == 0x10)
  116.                {
  117.                    isSubDir[index] = TRUE;
  118.                    while ((selection[index] = malloc(strlen(fname)+3)) == NULL)
  119.                                         /* use +3 to leave room for d:\ */
  120.                        FreeRefresh( "filesel" );
  121.                    strcpy( selection[index++], fname );
  122.                    if (index > 511)
  123.                    {
  124.                        for (dummy=1; dummy<index; dummy++)
  125.                            free( selection[dummy] );
  126.                        StatusLine(0, "Too many files in directory." );
  127.                        return(0);
  128.                    }
  129.                }
  130.                error = bdos( SRCH_NEXT, 0, 0);
  131.            }
  132.        }
  133.     
  134.        /** Find files with dirMask specs **/
  135.        error = bdos( SRCH_FIRST, (unsigned) dirMask, 0);
  136.        if (!error)
  137.        {
  138.            while(!error)
  139.            {
  140.                attribute = dta + 21;
  141.                fname     = dta + 30;
  142.                if ((*attribute & 0x10) == 0x00)
  143.                {
  144.                    isSubDir[index] = FALSE;
  145.                    while ((selection[index] = malloc(strlen(fname)+1)) == NULL)
  146.                        FreeRefresh( "filesel2" );
  147.                    strcpy( selection[index++], fname);
  148.                    if (index > 511)
  149.                    {
  150.                        for (dummy = 1; dummy < index; dummy++)
  151.                            free( selection[dummy] );
  152.                        StatusLine(0,"Too many files in directory.");
  153.                        return(0);
  154.                    }
  155.                }
  156.                error = bdos( SRCH_NEXT, 0, 0);
  157.            }
  158.        }
  159.  
  160. /** Graphics display dependent portion of the directory listing for
  161.     file selection.  (Only need to change this section plus other
  162.     extraneous prompts in the program to use this in any MS-Dos
  163.     environment.
  164. **/
  165.  
  166.        if (index > 1)
  167.        {
  168.            if (initDispFirst)
  169.            {
  170.                topList = 1;
  171.                lowList = (index-1 < DISPMAXNUM) ? index-1 : DISPMAXNUM;
  172.                initDispFirst = FALSE;
  173.            }
  174.            ClearDisplay(0);
  175.            TextLine   = TextHeight+5;
  176.            TextSample = 1;
  177.            sprintf( pBuff, "Current Path: %s #files: %d", curDir, index-1);
  178.            WriteText( pBuff );
  179.  
  180.            strcpy(pBuff,"");
  181.            for (i=topList; i <= lowList; i++)
  182.            {
  183.                if (isSubDir[i])
  184.                {
  185.  
  186.                    if ( !strcmp( selection[i], ".") )
  187.                        sprintf( scratchbuf, "%3d) %-10s<d>", i, "ROOT");
  188.                    else if ( !strcmp( selection[i], "..") )
  189.                        sprintf( scratchbuf, "%3d) %-10s<d>", i, "PARENT");
  190.                    else
  191.                        sprintf( scratchbuf, "%3d) %-10s<d>", i, selection[i]);
  192.                }
  193.                else
  194.                    sprintf( scratchbuf, "%3d) %-13s", i, selection[i]);
  195.                strcat(pBuff, scratchbuf);
  196.                if ((i%3)==0)
  197.                {
  198.                    WriteText( pBuff );
  199.                    strcpy( pBuff, "");
  200.                }
  201.            }
  202.            if ( (i%3) != 1)
  203.                WriteText( pBuff );
  204.            if (DisplayDevice != CGA)
  205.                WriteText( " " );
  206.            WriteText( "<#>, M(ask), D(rive), P(rev), N(ext), Q(uit)");
  207.            tltemp = TextLine;
  208.            WriteText( "OPTION>>" );
  209.            TextLine = tltemp;
  210.            LengthText( "OPTION>> ", TextHeight, &len);
  211.            TextSample = len;
  212.            AcceptText(choice);
  213.        }
  214.        else
  215.        {
  216.            ClearDisplay(0);
  217.            WriteText( "?? SYSTEM FAILURE ??" );
  218.            WriteText( "Press any key to return to Imdisp" );
  219.            getch();
  220.            strcpy( choice, "Quit");
  221.        }
  222. /* End of most Graphics dependent portion */
  223.  
  224.        /** Process user choice **/
  225.        switch( choice[0] )
  226.        {
  227.        case 'M':
  228.        case 'm':
  229.            WriteText( "Please enter new mask" );
  230.            AcceptText( dirMask );
  231.            initDispFirst = TRUE;
  232.            break;
  233.  
  234.        case 'D':
  235.        case 'd':
  236.            WriteText( "Please enter letter of desired drive (ex. A)" );
  237.            AcceptText( scratchbuf );
  238.            dchar = scratchbuf[0];
  239.            if ( isalpha(dchar) )
  240.            {
  241.                newDrive = toupper(dchar)-'A';
  242.                bdos( SEL_DRIVE, newDrive, 0);
  243.                curDrive = newDrive;
  244.                if (getcwd( curDir, 50) == NULL)
  245.                    perror( "Directory lookup failure" );
  246.            }
  247.            initDispFirst = TRUE;
  248.            break;
  249.  
  250.        case 'P':
  251.        case 'p':
  252.            topList = ((topList-DISPMAXNUM) < 1) ? 1 : topList-DISPMAXNUM;
  253.            lowList = ((topList+DISPMAXNUM) >= index-1) ? index-1 :
  254.                                                       topList+DISPMAXNUM-1;
  255.            break;
  256.  
  257.        case 'N':
  258.        case 'n':
  259.            topList = ((topList+DISPMAXNUM) >= index-1) ? index-1 :
  260.                                                      topList+DISPMAXNUM;
  261.            lowList = ((topList+DISPMAXNUM-1) >= index-1) ? index-1 :
  262.                                                      topList+DISPMAXNUM-1;
  263.            break;
  264.  
  265.        case 'Q':
  266.        case 'q':
  267.        /* Dealloc space saved for sel */
  268.            for (dummy = 1; dummy<index; dummy++)
  269.                free( selection[dummy] );
  270.            ClearDisplay(0);
  271.            return (0);
  272.  
  273.        default:
  274.            if (isdigit( choice[0] ))
  275.                if (numResponse = atoi(choice))
  276.                {
  277.                    okFilename = TRUE; /* init before checks */
  278.                    if (numResponse > 0 && numResponse < index)
  279.                    {
  280.                        if (isSubDir[numResponse])
  281.                        {
  282.                            if (numResponse == 1 &&
  283.                                !strcmp( selection[numResponse], ".") )
  284.                            {
  285.                                strncpy( selection[numResponse], curDir, 3);
  286.                                selection[numResponse][3] = '\0';
  287.                            }
  288.                            segregs.ds  = FP_SEG( selection[numResponse] );
  289.                            inregs.h.ah = 0x3B;
  290.                            inregs.x.dx = (unsigned) selection[numResponse];
  291.                            intdosx( &inregs, &outregs, &segregs);
  292.                            getcwd( curDir, 50);
  293.                            okFilename    = FALSE;
  294.                            initDispFirst = TRUE;
  295.                        }
  296.                    if (okFilename)
  297.                    {
  298.                        strcpy( tempFileName, curDir);
  299.                        if (tempFileName[3] != '\0')
  300.                            strcat( tempFileName, "\\");
  301.                        strcat( tempFileName, selection[numResponse]);
  302.                        strcpy( selFilename, tempFileName);
  303.                        /* Dealloc space saved for sel */
  304.                        for (dummy = 1; dummy<index; dummy++)
  305.                            free( selection[dummy] );
  306.                        ClearDisplay(0);
  307.                        return(1);
  308.                    }
  309.                }
  310.            }
  311.            break;
  312.        }
  313.        for (dummy = 1; dummy<index; dummy++) /* Dealloc space saved for sel */
  314.            free(selection[dummy]);
  315.     }
  316. }
  317.  
  318.  
  319.  
  320. int GetFile(void)
  321. /* GetFile gets the filename from the command string, opens the
  322.     image file, and prints out the image info.
  323. */
  324.  
  325. {
  326.     char  filename[128], status[128], dispstr[128];
  327.     char  savemask[30];
  328.     int   flag, sel;
  329.     int   i;
  330.     int   done;
  331.  
  332.     Histogram = 0;     /* Make sure to reinit var when new file */
  333.     Palette   = 0L;
  334.     strcpy(PaletteFileName,"");
  335.     ByteSwap  = FALSE;
  336.     GetKeywordString (CommandString, "FIL", " ", filename, &flag);
  337.     if (flag < 1)
  338.     {
  339.        sel = FileSel(filename);
  340.        if (sel==0) return (0);
  341.     }
  342.     else          /* Ron Baalke - 07/13/90 - Added wildcard processing */
  343.     {
  344.        done = FALSE;
  345.        for (i=0; (i<strlen(filename) && !done); i++)
  346.        {
  347.            if (strncmp(&filename[i],"*",1) == 0)
  348.            {
  349.                strcpy(savemask,dirMask);
  350.                strcpy(dirMask,filename);
  351.                sel = ChangeDir(filename);
  352.                if (sel != 0)
  353.                {
  354.                    strcpy(dirMask,savemask);
  355.                    strcpy(status,"Bad Pathname");
  356.                    TextLine = 30;   TextSample = 1;
  357.                    WriteText (status);
  358.                    return(0);
  359.                }
  360.                sel = FileSel(filename);
  361.                if (sel == 0)
  362.                {
  363.                    strcpy(dirMask,savemask);
  364.                    return(0);
  365.                }
  366.                done = TRUE;
  367.            }
  368.        }
  369.     }
  370.  
  371.     if (OpenFileFlag)
  372.     {
  373.        CloseImage (0, status);
  374.        if (BadStatus (status)) return;
  375.        OpenFileFlag = 0;
  376.     }
  377.  
  378.     OpenImage (filename, 0, "read", &nl, &ns, &bitsperpix, status);
  379.     if (BadStatus (status))
  380.     {
  381.        if (BatchFlag == 1)
  382.        {
  383.            strcpy(status,"Bad file name in batch file, aborting batch.");
  384.            StatusLine(0,status);
  385.            abort_disp = 1;
  386.        }
  387.        return;
  388.     }
  389.     OpenFileFlag = 1;
  390.  
  391.     if ((bitsperpix != 8) && (bitsperpix != 16) &&
  392.     (bitsperpix != 4) && (bitsperpix != 1) &&
  393.     (bitsperpix != 32) )
  394.     {
  395.        BadStatus ("Non valid bits per sample");
  396.        CloseImage (0, status);
  397.        if (BadStatus (status)) return;
  398.        OpenFileFlag = 0;
  399.        return;
  400.     }
  401.  
  402.     DNlow = 0;
  403. /*  Fix for full dynamic range - AW3
  404.     DNlow  = -1 * ( 1 << ((bitsperpix < 14) ? bitsperpix : 14) );
  405. */
  406.     DNhigh =      ( 1 << ((bitsperpix < 14) ? bitsperpix : 14) ) - 1;
  407.     if (!BatchFlag)
  408.     {
  409.        sprintf (dispstr, "Lines : %5d   Samples : %5d  Sample_Bits : %2d",
  410.                           nl, ns, bitsperpix );
  411.        StatusLine(1,dispstr);
  412.     }
  413.     if (PaletteFileName[0] != NULL || Palette != 0)
  414.     {
  415.        strcpy(CommandString,"PAL LOAD ");
  416.        strcat(CommandString,PaletteFileName);
  417.        DoPalette();
  418.     }
  419.     strcpy (ImageFileName, filename);
  420. }
  421.  
  422. int ChangeDir(char * pathname)
  423. /****************************************************************************/
  424. /* ChangeDir                                                                */
  425. /*                                                                          */
  426. /* Written by Ron Baalke - 07/29/90                                         */
  427. /* Given a full pathname (with the filename embedded in the pathname) this  */
  428. /* routine will change directories, ignoring the filename.  It will first   */
  429. /* change directory to the drive, and then to the directory itself.  It     */
  430. /* returns 0 on success, non-zero if unsuccessful.                          */
  431. /****************************************************************************/
  432. {
  433.  char drive[2];
  434.  char directory[81];
  435.  int i;
  436.  int begin=0;
  437.  int end;
  438.  int error;
  439.  
  440.  strcpy(drive,"");
  441.  strcpy(directory,"");
  442.  
  443.  if (strnicmp(&pathname[1],":",1) == 0)
  444.  {
  445.    drive[0] = pathname[0];
  446.    drive[1] = '\0';
  447.    begin = begin + 2;
  448.    error = bdos(14,(int)drive[0]-'A',0);
  449.  }
  450.  
  451.  end = strlen(pathname) - 1;
  452.  
  453.  for (i=end; i>begin; i--)
  454.  {
  455.   if (strnicmp(&pathname[i],"\\",1) == 0)
  456.   {
  457.    strncpy(&directory[0],&pathname[begin],i-begin);
  458.    directory[i-begin] = '\0';
  459.    error = chdir(directory);
  460.    if (error) return(-1);
  461.    break;
  462.   }
  463.  }
  464.  
  465.  return(0);
  466. }
  467.