home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / mainutil.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  4KB  |  154 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    mainutil.c (Main Utilities)
  6.  * Purpose:    Basic utility calls
  7.  * Subroutine:    calloc_errchk()            returns: char *
  8.  * Subroutine:    exit_errmsg()            returns: void
  9.  * Subroutine:    not_different()            returns: int
  10.  * Subroutine:    statemotion_in_disp()        returns: Bool
  11.  * Subroutine:    specificmotion_in_disp()    returns: Bool
  12.  * Subroutine:    statedmotion_in_disp()        returns: Bool
  13.  * Xlib calls:    none
  14.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  15.  *        You may do anything you like with this file except remove
  16.  *        this copyright.  The Smithsonian Astrophysical Observatory
  17.  *        makes no representations about the suitability of this
  18.  *        software for any purpose.  It is provided "as is" without
  19.  *        express or implied warranty.
  20.  * Modified:    {0} Michael VanHilst    initial version          11 May 1989
  21.  *        {n} <who> -- <does what> -- <when>
  22.  */
  23.  
  24. #include <stdio.h>        /* stderr, FILE, NULL, etc. */
  25. #include <X11/Xlib.h>        /* X window stuff */
  26. #include <X11/Xutil.h>        /* X window manager stuff */
  27. #include "hfiles/define.h"    /* define SMALL_NUMBER */
  28. #include "hfiles/struct.h"    /* declare structure types */
  29. #include "hfiles/extern.h"    /* extern main parameter structures */
  30.  
  31. static char *errnote = " allocation failure\n";
  32. /*
  33.  * Subroutine:    calloc_errchk
  34.  * Purpose:    Calloc with printf'less error message and exit for failure
  35.  * Note:    If message is given, print it and exit on failure
  36.  * Note:    If no message is given, return 0 on failure
  37.  */
  38. char *calloc_errchk ( count, size, errmess )
  39.      int count;
  40.      unsigned int size;
  41.      char *errmess;
  42. {
  43.   char *space;
  44.   char *calloc();
  45.  
  46.   if( (space = (char *)calloc((unsigned)count, size)) == NULL ) {
  47.     if( errmess == NULL )
  48.       return(0);
  49.     else {
  50.       fputs (errmess, stderr);
  51.       fputs (errnote, stderr);
  52.       exit( 100 );
  53.     }
  54.   }
  55.   return( space );
  56. }
  57.  
  58. static char *exitnote = "SAO_IMAGE EXIT ON ERROR: ";
  59. static char *exitcr = "\n";
  60. /*
  61.  * Subroutine:    exit_errmsg
  62.  * Purpose:    Exit with a message
  63.  */
  64. void exit_errmsg ( errmess )
  65.      char *errmess;
  66. {
  67.   void say_goodbye();
  68.  
  69.   fputs (exitnote, stderr);
  70.   if( errmess != NULL )
  71.     fputs (errmess, stderr);
  72.   fputs (exitcr, stderr);
  73.   say_goodbye( 10 );
  74. }
  75.  
  76. /*
  77.  * Subroutine:    not_different
  78.  * Purpose:    compare two values for a significant difference
  79.  * Returns:    1 if there is virtually no difference, else 0
  80.  */
  81. int not_different ( val1, val2 )
  82.      double val1;
  83.      double val2;
  84. {
  85.   if( val1 > val2 ) {
  86.     if( (val1 - val2) > SMALL_NUMBER )
  87.       return( 0 );
  88.   } else {
  89.     if( (val2 - val1) > SMALL_NUMBER )
  90.       return( 0 );
  91.   }
  92.   return( 1 );
  93. }
  94.  
  95. /*
  96.  * Subroutine:    statemotion_in_disp
  97.  * Purpose:    Subroutine to use with XCheckIfEvent to get the latest
  98.  *        ButtonMotion event in the dispbox.  Track until all masked
  99.  *        buttons or states return to 0.
  100.  */
  101. Bool statemotion_in_disp ( display, event, arg )
  102.      Display *display;
  103.      XEvent *event;
  104.      char *arg;
  105. {
  106.   unsigned int mask = *((unsigned int *)arg);
  107.   if( (event->type == MotionNotify) &&
  108.       (event->xmotion.window == dispbox.ID) &&
  109.       ((event->xmotion.state & mask) != 0) )
  110.     return( True );
  111.   else
  112.     return( False );
  113. }
  114.  
  115. /*
  116.  * Subroutine:    specificmotion_in_disp
  117.  * Purpose:    Subroutine to use with XCheckIfEvent to get the latest
  118.  *        ButtonMotion event in the dispbox.  Track until any masked
  119.  *        button or state returns to 0.
  120.  */
  121. Bool specificmotion_in_disp ( display, event, arg )
  122.      Display *display;
  123.      XEvent *event;
  124.      char *arg;
  125. {
  126.   unsigned int mask = *((unsigned int *)arg);
  127.   if( (event->type == MotionNotify) &&
  128.       (event->xmotion.window == dispbox.ID) &&
  129.       ((event->xmotion.state & mask) == mask) )
  130.     return( True );
  131.   else
  132.     return( False );
  133. }
  134.  
  135. /*
  136.  * Subroutine:    statedmotion_in_disp
  137.  * Purpose:    Subroutine to use with XCheckIfEvent to get the latest
  138.  *        ButtonMotion event in the dispbox.  Track until any state
  139.  *        differs from initial mask.
  140.  */
  141. Bool statedmotion_in_disp ( display, event, arg )
  142.      Display *display;
  143.      XEvent *event;
  144.      char *arg;
  145. {
  146.   unsigned int mask = *((unsigned int *)arg);
  147.   if( (event->type == MotionNotify) &&
  148.       (event->xmotion.window == dispbox.ID) &&
  149.       (event->xmotion.state == mask) )
  150.     return( True );
  151.   else
  152.     return( False );
  153. }
  154.