otherio.c 4.4 KB

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