home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: Science
/
Science.zip
/
imdisp79.zip
/
IMDUTIL.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-14
|
14KB
|
520 lines
/*** IMDISP module IMDUTIL.C
General Purpose Utility routines Routines
Contains routines such initialization, error messages and status
checking.
Moved to separate module from IMDISP.C by A. Warnock, 1/91
***/
#define __MSC
#include <direct.h>
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include <ctype.h>
#include "mshell.h"
#include "imdef.h"
#include "imdisp.h"
#include "dispio.h"
#include "imageio.h"
#include "disputil.h"
#include "fileutil.h"
#include "textutil.h"
#include "refresh.h"
#include "timer.h"
#include "buffer.h"
#include "display.h"
int StartUp (int, char **);
void FatalError (char *);
int BadStatus (char *);
int ctrl_c (void);
void Sleep (float);
void Delay (int);
void insert (int, int *);
int match(char *, char *);
char *strrtrim(char *);
char *strltrim(char *);
void OpenHelp();
extern unsigned char *buffer1;
extern unsigned char *buffer2;
extern unsigned char *buffer3;
extern unsigned char *buffer4;
extern unsigned char *buffer5;
extern unsigned char *buffer6;
extern unsigned char *buffer7;
extern unsigned char *outbuffer;
int StartUp (int argc, char **argv)
/* StartUp sets up some things for the main program.
The display is turned on, variables initialized, and the
welcome message is printed or the file opened instead.
*/
{
int i;
char *dot_ptr, *batchname;
char f_extension[4];
char temp[40];
if ((buffer1 = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if ((buffer2 = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if ((buffer3 = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if ((buffer4 = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if ((buffer5 = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if ((buffer6 = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if ((buffer7 = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
if ((outbuffer = malloc(MAXDISPNS)) == NULL)
{
FatalError( "Not enough memory for pixel buffer.\n");
}
InitDisplay();
thefont = -1;
Font (0);
RefreshLines = 0;
Image_Line = 1;
Image_Sample = 1;
Image_Height = dispnl;
Image_Length = dispns;
Image_Zoom = 1;
Image_Subsample = 1;
CommandPtr = 0;
CommandFlag = FALSE;
for (i=0; i<MAXRECALL; i++)
strcpy(CommandList[i],"");
/* Save the starting directory */
getcwd( DefaultDir, 66);
#ifdef __TURBOC__
DefaultDrive = getdisk() + 'A';
#else
_dos_getdrive( &DefaultDrive );
#endif
for (i=0; i<MAXBUFFERS; i++)
Buffers[i].location = NOWHERE;
OpenFileFlag = 0;
nl = 0; ns = 0; sl = 1; ss = 1; sldd = 1; ssdd = 1;
subsample = 1; zoom = 1;
centerline = -1; centersamp = -1; lastsl = 1; lastss = 1;
DNlow = 0; DNhigh = 255;
if ((batchname = getenv("IMREFRESH")) != NULL)
{
strcpy(RefreshName, batchname);
strcat(RefreshName, "REFRESH.TMP");
}
if ((batchname = getenv("IMBROWSE")) != NULL)
{
strcpy(BrowseName, batchname);
strcat(BrowseName, "BROWSE.CMD");
strcpy(SelectName, batchname);
strcat(SelectName, "IMDISP.SEL");
}
if ((batchname = getenv("IMPALETTE")) != NULL)
{
strcpy(PalettePath, batchname);
}
if (argc > 1)
{
if((dot_ptr = strpbrk(argv[1],".")) == NULL)
{
StatusLine(0,"Illegal command line entry");
}
dot_ptr++; /* Move ptr to the extension of the command file */
strncpy(f_extension, dot_ptr, 3); /* Get copy of extension */
f_extension[3] = '\0';
if (!strcmp(strupr(f_extension),"MNU"))
{
strcpy (CommandString, "MENU ");
strcat (CommandString, argv[1]);
strupr (CommandString);
DoMenu();
}
else if (!strcmp(strupr(f_extension),"CMD") ||
!strcmp(strupr(f_extension),"BAT"))
{
strcpy (CommandString, "BATCH ");
strcat (CommandString, argv[1]);
strupr (CommandString);
DoBatch();
}
else
{
/* default will make the filename an image */
strcpy (CommandString, "FILE ");
strcat (CommandString, argv[1]);
strupr (CommandString);
GetFile();
}
}
else
{
Font (0);
DrawText ("Welcome to IMDISP",
30, 50, BigChars, 0, numDN-1);
DrawText ("The MS-DOS Image Display Program",
50, 40, TextHeight, 0, numDN-1);
DrawText ("Version 7.9g",
72, 80, TextHeight, 0, numDN-1);
DrawText ("Use HELP SET to see display options",
120, 10, SmallChars, 0, numDN-1);
}
RefreshLines = AllocRefresh(1);
if (argc == 1)
StatusLine(1,"Type HELP for command description or HELP OPTION for a summary");
}
void FatalError (char * status)
/* Switches to text mode, prints the status message and
terminates execution.
*/
{
int i;
char stat[80];
/* Deallocate any buffers */
for (i=0; i<MAXBUFFERS; i++)
{
if (Buffers[i].location != NOWHERE)
DeallocBuffer(i);
}
/* Close up any open files */
strcpy(stat,"");
if (OpenFileFlag)
{
CloseImage (0, stat);
BadStatus (stat); /* Ron took this out? */
}
if (logflag)
{
fflush( LogFile );
fclose( LogFile );
}
for (i=0; i<BatchFileNumber+1; i++)
fclose(BatchFile[i]);
/* Deallocate refresh buffer */
FreeRefresh();
DeallocRefresh();
/* Restore the starting directory and drive */
chdir( DefaultDir );
#ifdef __TURBOC__
NumDrives = setdisk( DefaultDrive );
#else
_dos_setdrive( DefaultDrive, &NumDrives );
#endif
/* Back to text mode */
DisplayOff();
if (Mem_Used() != 0)
{
printf( "*** Memory list not empty ***\n" );
Mem_Display( stdout );
}
printf("\n%s\n", status );
exit(2);
}
int BadStatus (char * status)
/* Prints out error message if there is one.
Returns true if error.
*/
{
if (strlen(status) > 0)
{
StatusLine(0,status);
return (1);
}
else
return (0);
}
int ctrl_c(void)
/* interrupt routine. */
{
FatalError("User Aborted");
}
void Delay(int millisec)
/* Calls timer routine to delay the specified
number of milliseconds (only good to about
4 billion nanoseconds (about 1 hour) before
overflow.
Written 2/91 by A. Warnock, ST Systems Corp.
*/
{
int i;
unsigned long start, elapsed, wait_ns;
wait_ns = 1000L * (unsigned long) millisec ;
i=0;
elapsed = 0L;
time_int();
start = elaptime();
do
{
elapsed = elapsed + (unsigned long) (838L * elaptime() / 1000L) ;
} while ( elapsed < wait_ns );
}
/**************************************************************************/
/* Sleep */
/* This routine will pause by the number of seconds specified by the */
/* caller. It is accurate to 1/10 second. */
/* */
/* Originally written by Ron Baalke, December 1986 in Turbo C 1.0 */
/* Converted to Microsoft C, Februray 3, 1991. */
/**************************************************************************/
void Sleep(float num_seconds)
{
#define TICKS_PER_SECOND 18.2
int count; /* Number of clock chip counts */
union REGS regs; /* BIOS call registers */
long old_time; /* Time when routine called. */
union {
long longword;
short word[2];
} overlay;
/* Convert to clock ticks */
count = (num_seconds * TICKS_PER_SECOND);
/**************************************************************************/
/* Get current time of day */
/**************************************************************************/
regs.h.ah = 0;
int86(26, ®s, ®s);
overlay.word[0] = regs.x.dx;
overlay.word[1] = regs.x.cx;
old_time = overlay.longword;
/**************************************************************************/
/* Loop until count clock ticks have occurred. */
/**************************************************************************/
while (old_time <= overlay.longword &&
old_time > overlay.longword - count) {
regs.h.ah = 0;
int86(26, ®s, ®s);
overlay.word[0] = regs.x.dx;
overlay.word[1] = regs.x.cx;
}
return;
}
/* inserti.c - insertion sort for an array of integers */
void insert( int na, int *a)
/* na number of integers to be sorted */
/* a[] array of integers to be sorted */
{
int i , j ; /* indices for loops */
int temp ; /* holds one element of array temporarily */
for( i=1 ; i < na ; i++ )
{ /* insert the i-th element into the array */
temp = a[i] ;
j = i - 1 ;
while( ( j >= 0 ) && ( temp < a[j] ) )
{
a[j+1] = a[j] ;
j-- ;
}
a[j+1] = temp ;
}
}
int match(char *target, char *test)
/*
The 'test' string is matched against the 'target' string. The test string
must match (case insensitively) the target up to the end of the target.
*/
{ /*
Test for exception, i.e. null target string NEVER matches.
*/
if (*target == '\0' && *test != '\0')
return(FALSE);
/*
Match until end-of-string is found
*/
for ( ; *target != '\0'; target++, test++)
if (*test == '\0' || toupper(*target) != toupper(*test))
return(FALSE);
return(TRUE);
}
char *strrtrim(char *string)
/*
Trim off trailing non-ASCII characters and blanks
*/
{
char *ptr;
int len;
len = strlen( string );
/*
Check backward from last character, looking for printable
nonblanks.
*/
while ( (!isgraph(string[len-1])) && (len > 0) )
{
len--;
}
/* Stick a NULL on the end */
string[len] = '\0';
/* Point to the trimmed string and return */
ptr = string;
return( ptr );
}
char *strltrim(char *string)
/*
Trim off leading non-ASCII characters and blanks
*/
{
char *ptr;
int ind;
/*
Check forward from first character, looking for printable
nonblanks.
*/
ind = 0;
while ( !isgraph(string[ind]) )
{
string++;
ind++;
}
/* Point to the trimmed string and return */
ptr = string;
return( ptr );
}
void OpenHelp()
{
char temp[64];
char *imhelp;
int len;
if ((imhelp = getenv("IMHELP")) != NULL)
{
strcpy(temp, imhelp);
len = strlen(temp);
if (strpbrk(temp,".") == NULL) /* No file name in IMHELP */
{
if (temp[len-1] != '\\')
strcat( temp, "\\");
strcat(temp, helpfilename);
}
/* Try to open the file pointed to by IMHELP */
if((helpfile = fopen(temp, "r+t")) == NULL)
{
/* If that fails, try in the default directory */
if((helpfile = fopen(helpfilename, "r+t")) == NULL)
{
/* Or look in the same directory as the program */
len = strlen(ProgramName);
if (len > 0)
{
ProgramName[len-3] = '\0';
strcpy(temp, ProgramName);
strcat(temp, "HLP");
if((helpfile = fopen(temp, "r+t")) == NULL)
{
sprintf( temp, "Error: help file %s not found.", helpfilename);
StatusLine(1, temp);
}
}
else
{
sprintf( temp, "Error: help file %s not found.", helpfilename);
StatusLine(1, temp);
}
}
}
}
/* If IMHELP not set, just look in default directory */
else
{
if((helpfile = fopen(helpfilename, "r+t")) == NULL)
{
/* Or look in the same directory as the program */
len = strlen(ProgramName);
if (len > 0)
{
ProgramName[len-3] = '\0';
strcpy(temp, ProgramName);
strcat(temp, "HLP");
if((helpfile = fopen(temp, "r+t")) == NULL)
{
sprintf( temp, "Error: help file %s not found.", helpfilename);
StatusLine(1, temp);
}
}
}
}
return;
}