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

  1. /***  IMDISP module IMDUTIL.C
  2.  
  3.       General Purpose Utility routines Routines
  4.  
  5.     Contains routines such initialization, error messages and status
  6.     checking.
  7.  
  8.     Moved to separate module from IMDISP.C by A. Warnock, 1/91
  9.  
  10. ***/
  11.  
  12. #define __MSC
  13.  
  14. #include <direct.h>
  15. #include <dos.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <process.h>
  20. #include <ctype.h>
  21. #include "mshell.h"
  22. #include "imdef.h"
  23. #include "imdisp.h"
  24. #include "dispio.h"
  25. #include "imageio.h"
  26. #include "disputil.h"
  27. #include "fileutil.h"
  28. #include "textutil.h"
  29. #include "refresh.h"
  30. #include "timer.h"
  31. #include "buffer.h"
  32. #include "display.h"
  33.  
  34. int     StartUp (int, char **);
  35. void    FatalError (char *);
  36. int     BadStatus (char *);
  37. int     ctrl_c (void);
  38. void    Sleep (float);
  39. void    Delay (int);
  40. void    insert (int, int *);
  41. int     match(char *, char *);
  42. char   *strrtrim(char *);
  43. char   *strltrim(char *);
  44. void    OpenHelp();
  45.  
  46. extern unsigned char *buffer1;
  47. extern unsigned char *buffer2;
  48. extern unsigned char *buffer3;
  49. extern unsigned char *buffer4;
  50. extern unsigned char *buffer5;
  51. extern unsigned char *buffer6;
  52. extern unsigned char *buffer7;
  53. extern unsigned char *outbuffer;
  54.  
  55. int StartUp (int argc, char **argv)
  56. /*  StartUp sets up some things for the main program.
  57.     The display is turned on, variables initialized, and the
  58.     welcome message is printed or the file opened instead.
  59. */
  60. {
  61.     int    i;
  62.     char   *dot_ptr, *batchname;
  63.     char   f_extension[4];
  64.     char   temp[40];
  65.  
  66.  
  67.     if ((buffer1 = malloc(MAXDISPNS)) == NULL)
  68.     {
  69.         FatalError( "Not enough memory for pixel buffer.\n");
  70.     }
  71.  
  72.     if ((buffer2 = malloc(MAXDISPNS)) == NULL)
  73.     {
  74.         FatalError( "Not enough memory for pixel buffer.\n");
  75.     }
  76.  
  77.     if ((buffer3 = malloc(MAXDISPNS)) == NULL)
  78.     {
  79.         FatalError( "Not enough memory for pixel buffer.\n");
  80.     }
  81.  
  82.     if ((buffer4 = malloc(MAXDISPNS)) == NULL)
  83.     {
  84.         FatalError( "Not enough memory for pixel buffer.\n");
  85.     }
  86.  
  87.     if ((buffer5 = malloc(MAXDISPNS)) == NULL)
  88.     {
  89.         FatalError( "Not enough memory for pixel buffer.\n");
  90.     }
  91.  
  92.     if ((buffer6 = malloc(MAXDISPNS)) == NULL)
  93.     {
  94.         FatalError( "Not enough memory for pixel buffer.\n");
  95.     }
  96.  
  97.     if ((buffer7 = malloc(MAXDISPNS)) == NULL)
  98.     {
  99.         FatalError( "Not enough memory for pixel buffer.\n");
  100.     }
  101.  
  102.     if ((outbuffer = malloc(MAXDISPNS)) == NULL)
  103.     {
  104.         FatalError( "Not enough memory for pixel buffer.\n");
  105.     }
  106.  
  107.     InitDisplay();
  108.     thefont = -1;
  109.     Font (0);
  110.     RefreshLines = 0;
  111.     Image_Line = 1;
  112.     Image_Sample = 1;
  113.     Image_Height = dispnl;
  114.     Image_Length = dispns;
  115.     Image_Zoom = 1;
  116.     Image_Subsample = 1;
  117.     CommandPtr = 0;
  118.     CommandFlag = FALSE;
  119.     for (i=0; i<MAXRECALL; i++)
  120.        strcpy(CommandList[i],"");
  121.  
  122. /*  Save the starting directory */
  123.     getcwd( DefaultDir, 66);
  124. #ifdef __TURBOC__
  125.     DefaultDrive = getdisk() + 'A';
  126. #else
  127.     _dos_getdrive( &DefaultDrive );
  128. #endif
  129.  
  130.  
  131.     for (i=0; i<MAXBUFFERS; i++)
  132.        Buffers[i].location = NOWHERE;
  133.  
  134.     OpenFileFlag = 0;
  135.     nl = 0; ns = 0; sl = 1; ss = 1; sldd = 1; ssdd = 1;
  136.     subsample = 1; zoom = 1;
  137.     centerline = -1;  centersamp = -1;  lastsl = 1;   lastss = 1;
  138.     DNlow = 0;  DNhigh = 255;
  139.     if ((batchname = getenv("IMREFRESH")) != NULL)
  140.     {
  141.         strcpy(RefreshName, batchname);
  142.         strcat(RefreshName, "REFRESH.TMP");
  143.     }
  144.     if ((batchname = getenv("IMBROWSE")) != NULL)
  145.     {
  146.         strcpy(BrowseName, batchname);
  147.         strcat(BrowseName, "BROWSE.CMD");
  148.         strcpy(SelectName, batchname);
  149.         strcat(SelectName, "IMDISP.SEL");
  150.     }
  151.     if ((batchname = getenv("IMPALETTE")) != NULL)
  152.     {
  153.         strcpy(PalettePath, batchname);
  154.     }
  155.     if (argc > 1)
  156.     {
  157.         if((dot_ptr = strpbrk(argv[1],".")) == NULL)
  158.         {
  159.             StatusLine(0,"Illegal command line entry");
  160.         }
  161.         dot_ptr++;   /* Move ptr to the extension of the command file */
  162.         strncpy(f_extension, dot_ptr, 3);   /* Get copy of extension */
  163.         f_extension[3] = '\0';
  164.         if (!strcmp(strupr(f_extension),"MNU"))
  165.         {
  166.             strcpy (CommandString, "MENU ");
  167.             strcat (CommandString, argv[1]);
  168.             strupr (CommandString);
  169.             DoMenu();
  170.         }
  171.         else if (!strcmp(strupr(f_extension),"CMD") ||
  172.                  !strcmp(strupr(f_extension),"BAT"))
  173.         {
  174.             strcpy (CommandString, "BATCH ");
  175.             strcat (CommandString, argv[1]);
  176.             strupr (CommandString);
  177.             DoBatch();
  178.         }
  179.         else
  180.         {
  181.         /* default will make the filename an image */
  182.             strcpy (CommandString, "FILE ");
  183.             strcat (CommandString, argv[1]);
  184.             strupr (CommandString);
  185.             GetFile();
  186.         }
  187.     }
  188.     else
  189.     {
  190.         Font (0);
  191.  
  192.         DrawText ("Welcome to IMDISP",
  193.                    30,  50, BigChars, 0, numDN-1);
  194.         DrawText ("The MS-DOS Image Display Program",
  195.                    50,  40, TextHeight, 0, numDN-1);
  196.         DrawText ("Version 7.9g",
  197.                    72,  80, TextHeight, 0, numDN-1);
  198.         DrawText ("Use HELP SET to see display options",
  199.                    120, 10, SmallChars, 0, numDN-1);
  200.  
  201.     }
  202.     RefreshLines = AllocRefresh(1);
  203.     if (argc == 1)
  204.        StatusLine(1,"Type HELP for command description or HELP OPTION for a summary");
  205. }
  206.  
  207. void FatalError (char * status)
  208. /*  Switches to text mode, prints the status message and
  209.     terminates execution.
  210. */
  211. {
  212.    int i;
  213.    char stat[80];
  214.  
  215. /*  Deallocate any buffers */
  216.     for (i=0; i<MAXBUFFERS; i++)
  217.     {
  218.        if (Buffers[i].location != NOWHERE)
  219.           DeallocBuffer(i);
  220.     }
  221.  
  222. /*  Close up any open files */
  223.  
  224.     strcpy(stat,"");
  225.     if (OpenFileFlag)
  226.     {
  227.        CloseImage (0, stat);
  228.        BadStatus (stat);         /* Ron took this out? */
  229.     }
  230.  
  231.     if (logflag)
  232.     {
  233.        fflush( LogFile );
  234.        fclose( LogFile );
  235.     }
  236.  
  237.     for (i=0; i<BatchFileNumber+1; i++)
  238.        fclose(BatchFile[i]);
  239.  
  240. /* Deallocate refresh buffer */
  241.  
  242.     FreeRefresh();
  243.     DeallocRefresh();
  244.  
  245. /*  Restore the starting directory and drive */
  246.     chdir( DefaultDir );
  247. #ifdef __TURBOC__
  248.     NumDrives = setdisk( DefaultDrive );
  249. #else
  250.     _dos_setdrive( DefaultDrive, &NumDrives );
  251. #endif
  252.  
  253. /*  Back to text mode */
  254.     DisplayOff();
  255.     if (Mem_Used() != 0)
  256.     {
  257.        printf( "*** Memory list not empty ***\n" );
  258.        Mem_Display( stdout );
  259.     }
  260.  
  261.     printf("\n%s\n", status );
  262.     exit(2);
  263. }
  264.  
  265. int BadStatus (char * status)
  266. /* Prints out error message if there is one.
  267.    Returns true if error.
  268. */
  269.  
  270. {
  271.     if (strlen(status) > 0)
  272.     {
  273.         StatusLine(0,status);
  274.         return (1);
  275.     }
  276.     else
  277.         return (0);
  278. }
  279.  
  280. int ctrl_c(void)
  281. /* interrupt routine. */
  282. {
  283.      FatalError("User Aborted");
  284. }
  285.  
  286. void Delay(int millisec)
  287. /* Calls timer routine to delay the specified
  288.    number of milliseconds (only good to about
  289.    4 billion nanoseconds (about 1 hour) before
  290.    overflow.
  291.  
  292.    Written 2/91 by A. Warnock, ST Systems Corp.
  293. */
  294. {
  295.     int  i;
  296.     unsigned long start, elapsed, wait_ns;
  297.  
  298.     wait_ns = 1000L * (unsigned long) millisec ;
  299.     i=0;
  300.     elapsed = 0L;
  301.     time_int();
  302.     start = elaptime();
  303.     do
  304.     {
  305.        elapsed = elapsed + (unsigned long) (838L * elaptime() / 1000L) ;
  306.     } while ( elapsed < wait_ns );
  307. }
  308.  
  309. /**************************************************************************/
  310. /* Sleep                                                                  */
  311. /* This routine will pause by the number of seconds specified by the      */
  312. /* caller.  It is accurate to 1/10 second.                                */
  313. /*                                                                        */
  314. /* Originally written by Ron Baalke, December 1986 in Turbo C 1.0         */
  315. /* Converted to Microsoft C, Februray 3, 1991.                            */
  316. /**************************************************************************/
  317.  
  318. void Sleep(float num_seconds)
  319. {
  320. #define TICKS_PER_SECOND 18.2
  321.     int        count;                  /* Number of clock chip counts    */
  322.     union REGS  regs;                   /* BIOS call registers            */
  323.     long    old_time;            /* Time when routine called.      */
  324.     union {
  325.         long  longword;
  326.         short word[2];
  327.     } overlay;
  328.  
  329. /* Convert to clock ticks */
  330.  
  331.     count = (num_seconds * TICKS_PER_SECOND);
  332. /**************************************************************************/
  333. /*  Get current time of day                                               */
  334. /**************************************************************************/
  335.     regs.h.ah = 0;
  336.     int86(26, ®s, ®s);
  337.     overlay.word[0] = regs.x.dx;
  338.     overlay.word[1] = regs.x.cx;
  339.     old_time = overlay.longword;
  340. /**************************************************************************/
  341. /*  Loop until count clock ticks have occurred.                           */
  342. /**************************************************************************/
  343.     while (old_time <= overlay.longword &&
  344.            old_time > overlay.longword - count) {
  345.         regs.h.ah = 0;
  346.         int86(26, ®s, ®s);
  347.         overlay.word[0] = regs.x.dx;
  348.         overlay.word[1] = regs.x.cx;
  349.     }
  350.  
  351.     return;
  352. }
  353.  
  354.  
  355. /*  inserti.c - insertion sort for an array of integers  */
  356.  
  357. void  insert( int na, int *a)
  358. /*  na     number of integers to be sorted  */
  359. /*  a[]     array of integers to be sorted  */
  360. {
  361.     int   i  ,  j   ;        /* indices for loops    */
  362.     int   temp  ;        /* holds one element of array temporarily */
  363.  
  364.     for( i=1 ; i < na ; i++ )
  365.     {  /* insert the i-th element into the array */
  366.        temp = a[i]  ;
  367.        j = i - 1  ;
  368.        while( ( j >= 0 ) && ( temp < a[j] ) )
  369.        {
  370.            a[j+1] = a[j]  ;
  371.            j--  ;
  372.        }
  373.        a[j+1] = temp  ;
  374.     }
  375. }
  376.  
  377. int match(char *target, char *test)
  378. /*
  379.    The 'test' string is matched against the 'target' string.  The test string
  380.    must match (case insensitively) the target up to the end of the target.
  381. */
  382. {  /*
  383.       Test for exception, i.e. null target string NEVER matches.
  384.    */
  385.    if (*target == '\0' && *test != '\0')
  386.       return(FALSE);
  387.  
  388.    /*
  389.       Match until end-of-string is found
  390.    */
  391.    for ( ; *target != '\0'; target++, test++)
  392.       if (*test == '\0' || toupper(*target) != toupper(*test))
  393.          return(FALSE);
  394.  
  395.    return(TRUE);
  396. }
  397.  
  398. char   *strrtrim(char *string)
  399. /*
  400.    Trim off trailing non-ASCII characters and blanks
  401. */
  402. {
  403.    char *ptr;
  404.    int  len;
  405.  
  406.    len = strlen( string );
  407.  
  408. /*
  409.    Check backward from last character, looking for printable
  410.    nonblanks.
  411. */
  412.    while ( (!isgraph(string[len-1])) && (len > 0) )
  413.    {
  414.        len--;
  415.    }
  416.  
  417. /* Stick a NULL on the end */
  418.    string[len] = '\0';
  419.  
  420. /* Point to the trimmed string and return */
  421.    ptr = string;
  422.    return( ptr );
  423.  
  424. }
  425.  
  426.  
  427. char   *strltrim(char *string)
  428. /*
  429.    Trim off leading non-ASCII characters and blanks
  430. */
  431. {
  432.    char *ptr;
  433.    int  ind;
  434.  
  435. /*
  436.    Check forward from first character, looking for printable
  437.    nonblanks.
  438. */
  439.    ind = 0;
  440.    while ( !isgraph(string[ind]) )
  441.    {
  442.        string++;
  443.        ind++;
  444.    }
  445.  
  446. /* Point to the trimmed string and return */
  447.    ptr = string;
  448.    return( ptr );
  449.  
  450. }
  451.  
  452. void OpenHelp()
  453. {
  454.     char   temp[64];
  455.     char   *imhelp;
  456.     int    len;
  457.  
  458.     if ((imhelp = getenv("IMHELP")) != NULL)
  459.     {
  460.        strcpy(temp, imhelp);
  461.        len = strlen(temp);
  462.        if (strpbrk(temp,".") == NULL) /* No file name in IMHELP */
  463.        {
  464.            if (temp[len-1] != '\\')
  465.               strcat( temp, "\\");
  466.            strcat(temp, helpfilename);
  467.        }
  468.  
  469. /*  Try to open the file pointed to by IMHELP */
  470.        if((helpfile = fopen(temp, "r+t")) == NULL)
  471.        {
  472.           /*  If that fails, try in the default directory */
  473.           if((helpfile = fopen(helpfilename, "r+t")) == NULL)
  474.           {
  475.               /*  Or look in the same directory as the program */
  476.               len = strlen(ProgramName);
  477.               if (len > 0)
  478.               {
  479.                    ProgramName[len-3] = '\0';
  480.                    strcpy(temp, ProgramName);
  481.                    strcat(temp, "HLP");
  482.                    if((helpfile = fopen(temp, "r+t")) == NULL)
  483.                    {
  484.                        sprintf( temp, "Error: help file %s not found.", helpfilename);
  485.                        StatusLine(1, temp);
  486.                    }
  487.               }
  488.               else
  489.               {
  490.                   sprintf( temp, "Error: help file %s not found.", helpfilename);
  491.                   StatusLine(1, temp);
  492.               }
  493.           }
  494.        }
  495.     }
  496.  
  497. /*  If IMHELP not set, just look in default directory */
  498.     else
  499.     {
  500.        if((helpfile = fopen(helpfilename, "r+t")) == NULL)
  501.        {
  502.            /*  Or look in the same directory as the program */
  503.            len = strlen(ProgramName);
  504.            if (len > 0)
  505.            {
  506.                ProgramName[len-3] = '\0';
  507.                strcpy(temp, ProgramName);
  508.                strcat(temp, "HLP");
  509.                if((helpfile = fopen(temp, "r+t")) == NULL)
  510.                {
  511.                    sprintf( temp, "Error: help file %s not found.", helpfilename);
  512.                    StatusLine(1, temp);
  513.                }
  514.            }
  515.        }
  516.     }
  517.  
  518.     return;
  519. }
  520.