home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GRIPS 2: Government Rast…rocessing Software & Data
/
GRIPS_2.cdr
/
dos
/
imdisp
/
source
/
fileutil.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-11-20
|
16KB
|
467 lines
/*************************************************************/
/* Copyright (C) 1989, California Institute of Technology */
/* U. S. Government Sponsorship under NASA Contract */
/* NAS7-918 is acknowledged. */
/* */
/* Copyright only applies to the subroutine FILEUTIL.C */
/*************************************************************/
/*** Module FILEUTIL.C
This module provides utility routines for browsing through
directories and selecting files for subsequent display.
It also contains public domain programs for locating files
meeting wildcard specifications, and for determining the
current default drive and path name.
***/
/********************************************************************/
/* The file selection subroutine allows the user to browse */
/* directories and select image files for display in IMDISP. */
/* Also contains subroutines wildexp and drpath, used by DoBrowse. */
/* */
/* Written by: Dan Nakamura, Jet Propulsion Lab, October 10, 1987 */
/********************************************************************/
/* * * * INCLUDE files * * * */
#include <conio.h>
#include <ctype.h>
#include <direct.h>
#include <dos.h>
#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "imdef.h"
#include "imdisp.h"
#include "dispio.h"
#include "disputil.h"
#include "imageio.h"
#include "labutil.h"
#include "refresh.h"
/* * * * External functions * * * */
/* * * * Function declarations * * * */
int FileSel (char *);
int GetFile (void);
int ChangeDir (char *);
/* * * * Global Variables * * * */
int FileSel(char * selFilename)
{
union REGS inregs, outregs;
struct SREGS segregs;
char *selection[MAXNUMFILES]; /* Filename selected */
char tempFileName[80]; /* Full filename to be passed back */
char defDir[51]; /* Directory user enters from */
char curDir[51]; /* Path to dir with wanted filename */
char dta[128];
char choice[10];
char scratchbuf[20]; /* Buf used for small manipulations */
char pBuff[80]; /* printing Buffer */
char *attribute, *fname;
char subMask[4];
char dchar;
int defDrive, curDrive, newDrive, numDrivesInstal;
int topList, lowList;
int index, numResponse;
int error, okFilename, done, initDispFirst, i;
int isSubDir[MAXNUMFILES];
int dummy;
int tltemp, len;
/** Initialize Parameters **/
strcpy( subMask, "*.*");
if (getcwd( defDir, 50) == NULL)
perror( "Unable to register default directory" );
strcpy( curDir, defDir); /* Init current directory */
defDrive = 0x00FF & bdos( WHAT_DRIVE, 0, 0); /* Init default Drive */
numDrivesInstal = 0x00FF & /* Find drives */
bdos( SEL_DRIVE, defDrive, 0);
curDrive = defDrive; /* Init current drive */
done = FALSE;
initDispFirst = TRUE;
/** Setup for Loop **/
bdos( ALLOC_DTA, (unsigned) dta, 0); /* Setup Disk Transfer Area */
/** Main Loop For Valid Filename **/
while( !done )
{
/** Looking for SubDirectorie Names **/
index = 1; /* Set index for file array */
inregs.h.ah = SRCH_FIRST;
inregs.x.dx = (unsigned) subMask;
inregs.h.cl = 0x10;
intdos( &inregs, &outregs);
error = outregs.x.ax;
if ( !error )
{
while( !error )
{
attribute = dta + 21; /* Ptr to attribute sect of DTA */
fname = dta + 30; /* Ptr to filename sect of DTA */
if ( (*attribute & 0x10) == 0x10)
{
isSubDir[index] = TRUE;
while ((selection[index] = malloc(strlen(fname)+3)) == NULL)
/* use +3 to leave room for d:\ */
FreeRefresh( "filesel" );
strcpy( selection[index++], fname );
if (index > 511)
{
for (dummy=1; dummy<index; dummy++)
free( selection[dummy] );
StatusLine(0, "Too many files in directory." );
return(0);
}
}
error = bdos( SRCH_NEXT, 0, 0);
}
}
/** Find files with dirMask specs **/
error = bdos( SRCH_FIRST, (unsigned) dirMask, 0);
if (!error)
{
while(!error)
{
attribute = dta + 21;
fname = dta + 30;
if ((*attribute & 0x10) == 0x00)
{
isSubDir[index] = FALSE;
while ((selection[index] = malloc(strlen(fname)+1)) == NULL)
FreeRefresh( "filesel2" );
strcpy( selection[index++], fname);
if (index > 511)
{
for (dummy = 1; dummy < index; dummy++)
free( selection[dummy] );
StatusLine(0,"Too many files in directory.");
return(0);
}
}
error = bdos( SRCH_NEXT, 0, 0);
}
}
/** Graphics display dependent portion of the directory listing for
file selection. (Only need to change this section plus other
extraneous prompts in the program to use this in any MS-Dos
environment.
**/
if (index > 1)
{
if (initDispFirst)
{
topList = 1;
lowList = (index-1 < DISPMAXNUM) ? index-1 : DISPMAXNUM;
initDispFirst = FALSE;
}
ClearDisplay(0);
TextLine = TextHeight+5;
TextSample = 1;
sprintf( pBuff, "Current Path: %s #files: %d", curDir, index-1);
WriteText( pBuff );
strcpy(pBuff,"");
for (i=topList; i <= lowList; i++)
{
if (isSubDir[i])
{
if ( !strcmp( selection[i], ".") )
sprintf( scratchbuf, "%3d) %-10s<d>", i, "ROOT");
else if ( !strcmp( selection[i], "..") )
sprintf( scratchbuf, "%3d) %-10s<d>", i, "PARENT");
else
sprintf( scratchbuf, "%3d) %-10s<d>", i, selection[i]);
}
else
sprintf( scratchbuf, "%3d) %-13s", i, selection[i]);
strcat(pBuff, scratchbuf);
if ((i%3)==0)
{
WriteText( pBuff );
strcpy( pBuff, "");
}
}
if ( (i%3) != 1)
WriteText( pBuff );
if (DisplayDevice != CGA)
WriteText( " " );
WriteText( "<#>, M(ask), D(rive), P(rev), N(ext), Q(uit)");
tltemp = TextLine;
WriteText( "OPTION>>" );
TextLine = tltemp;
LengthText( "OPTION>> ", TextHeight, &len);
TextSample = len;
AcceptText(choice);
}
else
{
ClearDisplay(0);
WriteText( "?? SYSTEM FAILURE ??" );
WriteText( "Press any key to return to Imdisp" );
getch();
strcpy( choice, "Quit");
}
/* End of most Graphics dependent portion */
/** Process user choice **/
switch( choice[0] )
{
case 'M':
case 'm':
WriteText( "Please enter new mask" );
AcceptText( dirMask );
initDispFirst = TRUE;
break;
case 'D':
case 'd':
WriteText( "Please enter letter of desired drive (ex. A)" );
AcceptText( scratchbuf );
dchar = scratchbuf[0];
if ( isalpha(dchar) )
{
newDrive = toupper(dchar)-'A';
bdos( SEL_DRIVE, newDrive, 0);
curDrive = newDrive;
if (getcwd( curDir, 50) == NULL)
perror( "Directory lookup failure" );
}
initDispFirst = TRUE;
break;
case 'P':
case 'p':
topList = ((topList-DISPMAXNUM) < 1) ? 1 : topList-DISPMAXNUM;
lowList = ((topList+DISPMAXNUM) >= index-1) ? index-1 :
topList+DISPMAXNUM-1;
break;
case 'N':
case 'n':
topList = ((topList+DISPMAXNUM) >= index-1) ? index-1 :
topList+DISPMAXNUM;
lowList = ((topList+DISPMAXNUM-1) >= index-1) ? index-1 :
topList+DISPMAXNUM-1;
break;
case 'Q':
case 'q':
/* Dealloc space saved for sel */
for (dummy = 1; dummy<index; dummy++)
free( selection[dummy] );
ClearDisplay(0);
return (0);
default:
if (isdigit( choice[0] ))
if (numResponse = atoi(choice))
{
okFilename = TRUE; /* init before checks */
if (numResponse > 0 && numResponse < index)
{
if (isSubDir[numResponse])
{
if (numResponse == 1 &&
!strcmp( selection[numResponse], ".") )
{
strncpy( selection[numResponse], curDir, 3);
selection[numResponse][3] = '\0';
}
segregs.ds = FP_SEG( selection[numResponse] );
inregs.h.ah = 0x3B;
inregs.x.dx = (unsigned) selection[numResponse];
intdosx( &inregs, &outregs, &segregs);
getcwd( curDir, 50);
okFilename = FALSE;
initDispFirst = TRUE;
}
if (okFilename)
{
strcpy( tempFileName, curDir);
if (tempFileName[3] != '\0')
strcat( tempFileName, "\\");
strcat( tempFileName, selection[numResponse]);
strcpy( selFilename, tempFileName);
/* Dealloc space saved for sel */
for (dummy = 1; dummy<index; dummy++)
free( selection[dummy] );
ClearDisplay(0);
return(1);
}
}
}
break;
}
for (dummy = 1; dummy<index; dummy++) /* Dealloc space saved for sel */
free(selection[dummy]);
}
}
int GetFile(void)
/* GetFile gets the filename from the command string, opens the
image file, and prints out the image info.
*/
{
char filename[128], status[128], dispstr[128];
char savemask[30];
int flag, sel;
int i;
int done;
Histogram = 0; /* Make sure to reinit var when new file */
Palette = 0L;
strcpy(PaletteFileName,"");
ByteSwap = FALSE;
GetKeywordString (CommandString, "FIL", " ", filename, &flag);
if (flag < 1)
{
sel = FileSel(filename);
if (sel==0) return (0);
}
else /* Ron Baalke - 07/13/90 - Added wildcard processing */
{
done = FALSE;
for (i=0; (i<strlen(filename) && !done); i++)
{
if (strncmp(&filename[i],"*",1) == 0)
{
strcpy(savemask,dirMask);
strcpy(dirMask,filename);
sel = ChangeDir(filename);
if (sel != 0)
{
strcpy(dirMask,savemask);
strcpy(status,"Bad Pathname");
TextLine = 30; TextSample = 1;
WriteText (status);
return(0);
}
sel = FileSel(filename);
if (sel == 0)
{
strcpy(dirMask,savemask);
return(0);
}
done = TRUE;
}
}
}
if (OpenFileFlag)
{
CloseImage (0, status);
if (BadStatus (status)) return;
OpenFileFlag = 0;
}
OpenImage (filename, 0, "read", &nl, &ns, &bitsperpix, status);
if (BadStatus (status))
{
if (BatchFlag == 1)
{
strcpy(status,"Bad file name in batch file, aborting batch.");
StatusLine(0,status);
abort_disp = 1;
}
return;
}
OpenFileFlag = 1;
if ((bitsperpix != 8) && (bitsperpix != 16) &&
(bitsperpix != 4) && (bitsperpix != 1) &&
(bitsperpix != 32) )
{
BadStatus ("Non valid bits per sample");
CloseImage (0, status);
if (BadStatus (status)) return;
OpenFileFlag = 0;
return;
}
DNlow = 0;
/* Fix for full dynamic range - AW3
DNlow = -1 * ( 1 << ((bitsperpix < 14) ? bitsperpix : 14) );
*/
DNhigh = ( 1 << ((bitsperpix < 14) ? bitsperpix : 14) ) - 1;
if (!BatchFlag)
{
sprintf (dispstr, "Lines : %5d Samples : %5d Sample_Bits : %2d",
nl, ns, bitsperpix );
StatusLine(1,dispstr);
}
if (PaletteFileName[0] != NULL || Palette != 0)
{
strcpy(CommandString,"PAL LOAD ");
strcat(CommandString,PaletteFileName);
DoPalette();
}
strcpy (ImageFileName, filename);
}
int ChangeDir(char * pathname)
/****************************************************************************/
/* ChangeDir */
/* */
/* Written by Ron Baalke - 07/29/90 */
/* Given a full pathname (with the filename embedded in the pathname) this */
/* routine will change directories, ignoring the filename. It will first */
/* change directory to the drive, and then to the directory itself. It */
/* returns 0 on success, non-zero if unsuccessful. */
/****************************************************************************/
{
char drive[2];
char directory[81];
int i;
int begin=0;
int end;
int error;
strcpy(drive,"");
strcpy(directory,"");
if (strnicmp(&pathname[1],":",1) == 0)
{
drive[0] = pathname[0];
drive[1] = '\0';
begin = begin + 2;
error = bdos(14,(int)drive[0]-'A',0);
}
end = strlen(pathname) - 1;
for (i=end; i>begin; i--)
{
if (strnicmp(&pathname[i],"\\",1) == 0)
{
strncpy(&directory[0],&pathname[begin],i-begin);
directory[i-begin] = '\0';
error = chdir(directory);
if (error) return(-1);
break;
}
}
return(0);
}