home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / mesch12a.zip / otherio.c < prev    next >
C/C++ Source or Header  |  1994-01-13  |  4KB  |  165 lines

  1.  
  2. /**************************************************************************
  3. **
  4. ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
  5. **
  6. **                 Meschach Library
  7. ** 
  8. ** This Meschach Library is provided "as is" without any express 
  9. ** or implied warranty of any kind with respect to this software. 
  10. ** In particular the authors shall not be liable for any direct, 
  11. ** indirect, special, incidental or consequential damages arising 
  12. ** in any way from use of the software.
  13. ** 
  14. ** Everyone is granted permission to copy, modify and redistribute this
  15. ** Meschach Library, provided:
  16. **  1.  All copies contain this copyright notice.
  17. **  2.  All modified copies shall carry a notice stating who
  18. **      made the last modification and the date of such modification.
  19. **  3.  No charge is made for this software or works derived from it.  
  20. **      This clause shall not be construed as constraining other software
  21. **      distributed on the same medium as this software, nor is a
  22. **      distribution fee considered a charge.
  23. **
  24. ***************************************************************************/
  25.  
  26.  
  27. /*
  28.     File for doing assorted I/O operations not invlolving
  29.     MAT/VEC/PERM objects
  30. */
  31. static    char    rcsid[] = "$Id: otherio.c,v 1.2 1994/01/13 05:34:52 des Exp $";
  32.  
  33. #include    <stdio.h>
  34. #include    <ctype.h>
  35. #include    "matrix.h"
  36.  
  37.  
  38.  
  39. /* scratch area -- enough for a single line */
  40. static    char    scratch[MAXLINE+1];
  41.  
  42. /* default value for fy_or_n */
  43. static    int    y_n_dflt = TRUE;
  44.  
  45. /* fy_or_n -- yes-or-no to question is string s
  46.     -- question written to stderr, input from fp 
  47.     -- if fp is NOT a tty then return y_n_dflt */
  48. int    fy_or_n(fp,s)
  49. FILE    *fp;
  50. char    *s;
  51. {
  52.     char    *cp;
  53.  
  54.     if ( ! isatty(fileno(fp)) )
  55.         return y_n_dflt;
  56.  
  57.     for ( ; ; )
  58.     {
  59.         fprintf(stderr,"%s (y/n) ? ",s);
  60.         if ( fgets(scratch,MAXLINE,fp)==NULL )
  61.             error(E_INPUT,"fy_or_n");
  62.         cp = scratch;
  63.         while ( isspace(*cp) )
  64.             cp++;
  65.         if ( *cp == 'y' || *cp == 'Y' )
  66.             return TRUE;
  67.         if ( *cp == 'n' || *cp == 'N' )
  68.             return FALSE;
  69.         fprintf(stderr,"Please reply with 'y' or 'Y' for yes ");
  70.         fprintf(stderr,"and 'n' or 'N' for no.\n");
  71.     }
  72. }
  73.  
  74. /* yn_dflt -- sets the value of y_n_dflt to val */
  75. int    yn_dflt(val)
  76. int    val;
  77. {    return y_n_dflt = val;        }
  78.  
  79. /* fin_int -- return integer read from file/stream fp
  80.     -- prompt s on stderr if fp is a tty
  81.     -- check that x lies between low and high: re-prompt if
  82.         fp is a tty, error exit otherwise
  83.     -- ignore check if low > high        */
  84. int    fin_int(fp,s,low,high)
  85. FILE    *fp;
  86. char    *s;
  87. int    low, high;
  88. {
  89.     int    retcode, x;
  90.  
  91.     if ( ! isatty(fileno(fp)) )
  92.     {
  93.         skipjunk(fp);
  94.         if ( (retcode=fscanf(fp,"%d",&x)) == EOF )
  95.             error(E_INPUT,"fin_int");
  96.         if ( retcode <= 0 )
  97.             error(E_FORMAT,"fin_int");
  98.         if ( low <= high && ( x < low || x > high ) )
  99.             error(E_BOUNDS,"fin_int");
  100.         return x;
  101.     }
  102.  
  103.     for ( ; ; )
  104.     {
  105.         fprintf(stderr,"%s: ",s);
  106.         if ( fgets(scratch,MAXLINE,stdin)==NULL )
  107.             error(E_INPUT,"fin_int");
  108.         retcode = sscanf(scratch,"%d",&x);
  109.         if ( ( retcode==1 && low > high ) ||
  110.                     ( x >= low && x <= high ) )
  111.             return x;
  112.         fprintf(stderr,"Please type an integer in range [%d,%d].\n",
  113.                             low,high);
  114.     }
  115. }
  116.  
  117.  
  118. /* fin_double -- return double read from file/stream fp
  119.     -- prompt s on stderr if fp is a tty
  120.     -- check that x lies between low and high: re-prompt if
  121.         fp is a tty, error exit otherwise
  122.     -- ignore check if low > high        */
  123. double    fin_double(fp,s,low,high)
  124. FILE    *fp;
  125. char    *s;
  126. double    low, high;
  127. {
  128.     Real    retcode, x;
  129.  
  130.     if ( ! isatty(fileno(fp)) )
  131.     {
  132.         skipjunk(fp);
  133. #if REAL == DOUBLE
  134.         if ( (retcode=fscanf(fp,"%lf",&x)) == EOF )
  135. #elif REAL == FLOAT
  136.         if ( (retcode=fscanf(fp,"%f",&x)) == EOF )
  137. #endif
  138.             error(E_INPUT,"fin_double");
  139.         if ( retcode <= 0 )
  140.             error(E_FORMAT,"fin_double");
  141.         if ( low <= high && ( x < low || x > high ) )
  142.             error(E_BOUNDS,"fin_double");
  143.         return (double)x;
  144.     }
  145.  
  146.     for ( ; ; )
  147.     {
  148.         fprintf(stderr,"%s: ",s);
  149.         if ( fgets(scratch,MAXLINE,stdin)==NULL )
  150.             error(E_INPUT,"fin_double");
  151. #if REAL == DOUBLE
  152.         retcode = sscanf(scratch,"%lf",&x);
  153. #elif REAL == FLOAT 
  154.         retcode = sscanf(scratch,"%f",&x);
  155. #endif
  156.         if ( ( retcode==1 && low > high ) ||
  157.                     ( x >= low && x <= high ) )
  158.             return (double)x;
  159.         fprintf(stderr,"Please type an double in range [%g,%g].\n",
  160.                             low,high);
  161.     }
  162. }
  163.  
  164.  
  165.